summaryrefslogtreecommitdiffstats
path: root/chrome/browser/extensions/api/tabs
diff options
context:
space:
mode:
authorfsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-13 05:04:11 +0000
committerfsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98>2013-05-13 05:04:11 +0000
commitc4c2312cfcb4ca3ca875384bb3fc3026ac8e4916 (patch)
tree625edc01ba4caf9adfaf68ebc66ce346bcd55cb3 /chrome/browser/extensions/api/tabs
parent7e0dc51b5a519aa6480fdb4fbc506c0226e17330 (diff)
downloadchromium_src-c4c2312cfcb4ca3ca875384bb3fc3026ac8e4916.zip
chromium_src-c4c2312cfcb4ca3ca875384bb3fc3026ac8e4916.tar.gz
chromium_src-c4c2312cfcb4ca3ca875384bb3fc3026ac8e4916.tar.bz2
Refactor chrome.(tabs | webview).executeScript to share more code
This patch also trivially adds chrome.webview.insertCSS. webview.insertCSS will be exposed in a subsequent CL. BUG=239968, 239688 Test=ExecuteScriptApiTest.*, WebViewTest.Shim Review URL: https://chromiumcodereview.appspot.com/15007010 git-svn-id: svn://svn.chromium.org/chrome/trunk/src@199653 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api/tabs')
-rw-r--r--chrome/browser/extensions/api/tabs/tabs_api.cc169
-rw-r--r--chrome/browser/extensions/api/tabs/tabs_api.h48
2 files changed, 34 insertions, 183 deletions
diff --git a/chrome/browser/extensions/api/tabs/tabs_api.cc b/chrome/browser/extensions/api/tabs/tabs_api.cc
index f558862..71703ac 100644
--- a/chrome/browser/extensions/api/tabs/tabs_api.cc
+++ b/chrome/browser/extensions/api/tabs/tabs_api.cc
@@ -1959,18 +1959,7 @@ bool ExecuteCodeInTabFunction::HasPermission() {
return ExtensionFunction::HasPermission();
}
-bool ExecuteCodeInTabFunction::RunImpl() {
- EXTENSION_FUNCTION_VALIDATE(Init());
-
- if (!details_->code.get() && !details_->file.get()) {
- error_ = keys::kNoCodeOrFileToExecuteError;
- return false;
- }
- if (details_->code.get() && details_->file.get()) {
- error_ = keys::kMoreThanOneValuesError;
- return false;
- }
-
+bool ExecuteCodeInTabFunction::CanExecuteScriptOnPage() {
content::WebContents* contents = NULL;
// If |tab_id| is specified, look for the tab. Otherwise default to selected
@@ -1982,9 +1971,10 @@ bool ExecuteCodeInTabFunction::RunImpl() {
return false;
}
+ CHECK(contents);
+
// NOTE: This can give the wrong answer due to race conditions, but it is OK,
// we check again in the renderer.
- CHECK(contents);
if (!GetExtension()->CanExecuteScriptOnPage(contents->GetURL(),
contents->GetURL(),
execute_tab_id_,
@@ -1993,32 +1983,29 @@ bool ExecuteCodeInTabFunction::RunImpl() {
return false;
}
- if (details_->code.get())
- return Execute(*details_->code);
+ return true;
+}
- CHECK(details_->file.get());
- resource_ = GetExtension()->GetResource(*details_->file);
+ScriptExecutor* ExecuteCodeInTabFunction::GetScriptExecutor() {
+ Browser* browser = NULL;
+ content::WebContents* contents = NULL;
- if (resource_.extension_root().empty() || resource_.relative_path().empty()) {
- error_ = keys::kNoCodeOrFileToExecuteError;
- return false;
- }
+ bool success = GetTabById(
+ execute_tab_id_, profile(), include_incognito(), &browser, NULL,
+ &contents, NULL, &error_) && contents && browser;
- scoped_refptr<FileReader> file_reader(new FileReader(
- resource_, base::Bind(&ExecuteCodeInTabFunction::DidLoadFile, this)));
- file_reader->Start();
+ if (!success)
+ return NULL;
- return true;
+ return TabHelper::FromWebContents(contents)->script_executor();
}
-void ExecuteCodeInTabFunction::OnExecuteCodeFinished(const std::string& error,
- int32 on_page_id,
- const GURL& on_url,
- const ListValue& result) {
- if (!error.empty())
- SetError(error);
+bool ExecuteCodeInTabFunction::IsWebView() const {
+ return false;
+}
- SendResponse(error.empty());
+bool TabsExecuteScriptFunction::ShouldInsertCSS() const {
+ return false;
}
void TabsExecuteScriptFunction::OnExecuteCodeFinished(const std::string& error,
@@ -2048,6 +2035,8 @@ bool ExecuteCodeInTabFunction::Init() {
if (!InjectDetails::Populate(*details_value, details.get()))
return false;
+ details_ = details.Pass();
+
// If the tab ID wasn't given then it needs to be converted to the
// currently active tab's ID.
if (tab_id == -1) {
@@ -2060,124 +2049,10 @@ bool ExecuteCodeInTabFunction::Init() {
}
execute_tab_id_ = tab_id;
- details_ = details.Pass();
return true;
}
-void ExecuteCodeInTabFunction::DidLoadFile(bool success,
- const std::string& data) {
- std::string function_name = name();
- const Extension* extension = GetExtension();
-
- // Check if the file is CSS and needs localization.
- if (success &&
- function_name == TabsInsertCSSFunction::function_name() &&
- extension != NULL &&
- data.find(MessageBundle::kMessageBegin) != std::string::npos) {
- BrowserThread::PostTask(
- BrowserThread::FILE, FROM_HERE,
- base::Bind(&ExecuteCodeInTabFunction::LocalizeCSS, this,
- data,
- extension->id(),
- extension->path(),
- LocaleInfo::GetDefaultLocale(extension)));
- } else {
- DidLoadAndLocalizeFile(success, data);
- }
-}
-
-void ExecuteCodeInTabFunction::LocalizeCSS(
- const std::string& data,
- const std::string& extension_id,
- const base::FilePath& extension_path,
- const std::string& extension_default_locale) {
- scoped_ptr<SubstitutionMap> localization_messages(
- extension_file_util::LoadMessageBundleSubstitutionMap(
- extension_path, extension_id, extension_default_locale));
-
- // We need to do message replacement on the data, so it has to be mutable.
- std::string css_data = data;
- std::string error;
- MessageBundle::ReplaceMessagesWithExternalDictionary(*localization_messages,
- &css_data,
- &error);
-
- // Call back DidLoadAndLocalizeFile on the UI thread. The success parameter
- // is always true, because if loading had failed, we wouldn't have had
- // anything to localize.
- BrowserThread::PostTask(
- BrowserThread::UI, FROM_HERE,
- base::Bind(&ExecuteCodeInTabFunction::DidLoadAndLocalizeFile, this,
- true, css_data));
-}
-
-void ExecuteCodeInTabFunction::DidLoadAndLocalizeFile(bool success,
- const std::string& data) {
- if (success) {
- if (!Execute(data))
- SendResponse(false);
- } else {
- // TODO(viettrungluu): bug: there's no particular reason the path should be
- // UTF-8, in which case this may fail.
- error_ = ErrorUtils::FormatErrorMessage(keys::kLoadFileError,
- resource_.relative_path().AsUTF8Unsafe());
- SendResponse(false);
- }
-}
-
-bool ExecuteCodeInTabFunction::Execute(const std::string& code_string) {
- content::WebContents* contents = NULL;
- Browser* browser = NULL;
-
- bool success = GetTabById(
- execute_tab_id_, profile(), include_incognito(), &browser, NULL,
- &contents, NULL, &error_) && contents && browser;
-
- if (!success)
- return false;
-
- const Extension* extension = GetExtension();
- if (!extension)
- return false;
-
- ScriptExecutor::ScriptType script_type = ScriptExecutor::JAVASCRIPT;
- std::string function_name = name();
- if (function_name == TabsInsertCSSFunction::function_name()) {
- script_type = ScriptExecutor::CSS;
- } else if (function_name != TabsExecuteScriptFunction::function_name()) {
- NOTREACHED();
- }
-
- ScriptExecutor::FrameScope frame_scope =
- details_->all_frames.get() && *details_->all_frames ?
- ScriptExecutor::ALL_FRAMES :
- ScriptExecutor::TOP_FRAME;
-
- UserScript::RunLocation run_at = UserScript::UNDEFINED;
- switch (details_->run_at) {
- case InjectDetails::RUN_AT_NONE:
- case InjectDetails::RUN_AT_DOCUMENT_IDLE:
- run_at = UserScript::DOCUMENT_IDLE;
- break;
- case InjectDetails::RUN_AT_DOCUMENT_START:
- run_at = UserScript::DOCUMENT_START;
- break;
- case InjectDetails::RUN_AT_DOCUMENT_END:
- run_at = UserScript::DOCUMENT_END;
- break;
- }
- CHECK_NE(UserScript::UNDEFINED, run_at);
-
- TabHelper::FromWebContents(contents)->
- script_executor()->ExecuteScript(
- extension->id(),
- script_type,
- code_string,
- frame_scope,
- run_at,
- ScriptExecutor::ISOLATED_WORLD,
- false /* is_web_view */,
- base::Bind(&ExecuteCodeInTabFunction::OnExecuteCodeFinished, this));
+bool TabsInsertCSSFunction::ShouldInsertCSS() const {
return true;
}
diff --git a/chrome/browser/extensions/api/tabs/tabs_api.h b/chrome/browser/extensions/api/tabs/tabs_api.h
index c96ef46..3b61e62 100644
--- a/chrome/browser/extensions/api/tabs/tabs_api.h
+++ b/chrome/browser/extensions/api/tabs/tabs_api.h
@@ -9,6 +9,7 @@
#include <vector>
#include "base/compiler_specific.h"
+#include "chrome/browser/extensions/api/execute_code_function.h"
#include "chrome/browser/extensions/extension_function.h"
#include "chrome/common/extensions/api/tabs.h"
#include "chrome/common/extensions/user_script.h"
@@ -223,7 +224,7 @@ class TabsCaptureVisibleTabFunction : public AsyncExtensionFunction {
};
// Implement API call tabs.executeScript and tabs.insertCSS.
-class ExecuteCodeInTabFunction : public AsyncExtensionFunction {
+class ExecuteCodeInTabFunction : public ExecuteCodeFunction {
public:
ExecuteCodeInTabFunction();
@@ -232,50 +233,23 @@ class ExecuteCodeInTabFunction : public AsyncExtensionFunction {
// ExtensionFunction:
virtual bool HasPermission() OVERRIDE;
- virtual bool RunImpl() OVERRIDE;
-
- // Message handler.
- virtual void OnExecuteCodeFinished(const std::string& error,
- int32 on_page_id,
- const GURL& on_url,
- const ListValue& script_result);
- private:
// Initialize the |execute_tab_id_| and |details_| if they haven't already
// been. Returns whether initialization was successful.
- bool Init();
-
- // Called when contents from the file whose path is specified in JSON
- // arguments has been loaded.
- void DidLoadFile(bool success, const std::string& data);
-
- // Runs on FILE thread. Loads message bundles for the extension and
- // localizes the CSS data. Calls back DidLoadAndLocalizeFile on the UI thread.
- void LocalizeCSS(
- const std::string& data,
- const std::string& extension_id,
- const base::FilePath& extension_path,
- const std::string& extension_default_locale);
-
- // Called when contents from the loaded file have been localized.
- void DidLoadAndLocalizeFile(bool success, const std::string& data);
-
- // Run in UI thread. Code string contains the code to be executed. Returns
- // true on success. If true is returned, this does an AddRef.
- bool Execute(const std::string& code_string);
+ virtual bool Init() OVERRIDE;
+ virtual bool CanExecuteScriptOnPage() OVERRIDE;
+ virtual ScriptExecutor* GetScriptExecutor() OVERRIDE;
+ virtual bool IsWebView() const OVERRIDE;
+ private:
// Id of tab which executes code.
int execute_tab_id_;
-
- // The injection details.
- scoped_ptr<api::tabs::InjectDetails> details_;
-
- // Contains extension resource built from path of file which is
- // specified in JSON arguments.
- ExtensionResource resource_;
};
class TabsExecuteScriptFunction : public ExecuteCodeInTabFunction {
+ protected:
+ virtual bool ShouldInsertCSS() const OVERRIDE;
+
private:
virtual ~TabsExecuteScriptFunction() {}
@@ -291,6 +265,8 @@ class TabsInsertCSSFunction : public ExecuteCodeInTabFunction {
private:
virtual ~TabsInsertCSSFunction() {}
+ virtual bool ShouldInsertCSS() const OVERRIDE;
+
DECLARE_EXTENSION_FUNCTION("tabs.insertCSS", TABS_INSERTCSS)
};