diff options
author | fsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-24 04:56:06 +0000 |
---|---|---|
committer | fsamuel@chromium.org <fsamuel@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2013-01-24 04:56:06 +0000 |
commit | 44703cc73a24cc39b3e5e832f447d8261701fa4c (patch) | |
tree | 79f249332af4fc3613b4f59878233668cbde1908 /chrome/browser/extensions/api/webview | |
parent | 363e33dcc1b59766434bab7b85f9f9b7ce108237 (diff) | |
download | chromium_src-44703cc73a24cc39b3e5e832f447d8261701fa4c.zip chromium_src-44703cc73a24cc39b3e5e832f447d8261701fa4c.tar.gz chromium_src-44703cc73a24cc39b3e5e832f447d8261701fa4c.tar.bz2 |
<webview>: Implement ExecuteScript
This patch implements executeScript for <webview> by using extensions bindings for forwarding requests to the browser process from the app process.
The <webview> shim passes the ProcessId and the RouteID of the guest process to the ExecuteScriptFunction object in the browser process. From there, ExecuteScriptFunction grabs the guest web contents, and creates a ScriptExecutor object attached to the guest WebContents.
The callback is supported trivially through the extension bindings.
When a new guest web contents is created, we inject the Chrome App's extension information into the guest process, so that executeScript knows to bypass permission requests when attempting to execute script within the guest (as suggested by mpcomplete@).
BUG=153530
Test=WebViewTest.Shim, webViewExecuteScript
Review URL: https://codereview.chromium.org/11968054
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@178520 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/extensions/api/webview')
-rw-r--r-- | chrome/browser/extensions/api/webview/webview_api.cc | 88 | ||||
-rw-r--r-- | chrome/browser/extensions/api/webview/webview_api.h | 31 |
2 files changed, 119 insertions, 0 deletions
diff --git a/chrome/browser/extensions/api/webview/webview_api.cc b/chrome/browser/extensions/api/webview/webview_api.cc new file mode 100644 index 0000000..5457891 --- /dev/null +++ b/chrome/browser/extensions/api/webview/webview_api.cc @@ -0,0 +1,88 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/extensions/api/webview/webview_api.h" + +#include "chrome/common/extensions/api/webview.h" +#include "content/public/browser/render_view_host.h" + +using namespace extensions::api::webview; + +WebviewExecuteScriptFunction::WebviewExecuteScriptFunction() { +} + +WebviewExecuteScriptFunction::~WebviewExecuteScriptFunction() { +} + +bool WebviewExecuteScriptFunction::RunImpl() { + scoped_ptr<ExecuteScript::Params> params( + extensions::api::webview::ExecuteScript::Params::Create(*args_)); + EXTENSION_FUNCTION_VALIDATE(params.get()); + + content::RenderViewHost* guest_rvh = + content::RenderViewHost::FromID(params->process_id, params->route_id); + // If we haven't loaded a guest yet, then this will be NULL. + if (!guest_rvh) + return false; + content::WebContents* guest_web_contents = + content::WebContents::FromRenderViewHost(guest_rvh); + CHECK(guest_web_contents); + + ObserverList<extensions::TabHelper::ScriptExecutionObserver> + script_observers; + scoped_ptr<extensions::ScriptExecutor> script_executor( + new extensions::ScriptExecutor(guest_web_contents, &script_observers)); + + extensions::ScriptExecutor::FrameScope frame_scope = + params->details.all_frames.get() && *params->details.all_frames ? + extensions::ScriptExecutor::ALL_FRAMES : + extensions::ScriptExecutor::TOP_FRAME; + + extensions::UserScript::RunLocation run_at = + extensions::UserScript::UNDEFINED; + switch (params->details.run_at) { + case InjectDetails::RUN_AT_NONE: + case InjectDetails::RUN_AT_DOCUMENT_IDLE: + run_at = extensions::UserScript::DOCUMENT_IDLE; + break; + case InjectDetails::RUN_AT_DOCUMENT_START: + run_at = extensions::UserScript::DOCUMENT_START; + break; + case InjectDetails::RUN_AT_DOCUMENT_END: + run_at = extensions::UserScript::DOCUMENT_END; + break; + } + CHECK_NE(extensions::UserScript::UNDEFINED, run_at); + + script_executor->ExecuteScript( + GetExtension()->id(), + extensions::ScriptExecutor::JAVASCRIPT, + *params->details.code.get(), + frame_scope, + run_at, + extensions::ScriptExecutor::ISOLATED_WORLD, + true /* is_web_view */, + base::Bind( + &WebviewExecuteScriptFunction::OnExecuteCodeFinished, + this)); + + // Balanced in OnExecuteCodeFinished. + AddRef(); + return true; +} + +void WebviewExecuteScriptFunction::OnExecuteCodeFinished( + const std::string& error, + int32 on_page_id, + const GURL& on_url, + const ListValue& result) { + if (error.empty()) { + SetResult(result.DeepCopy()); + } else { + SetError(error); + } + SendResponse(error.empty()); + Release(); // Added in RunImpl(). +} + diff --git a/chrome/browser/extensions/api/webview/webview_api.h b/chrome/browser/extensions/api/webview/webview_api.h new file mode 100644 index 0000000..794232f --- /dev/null +++ b/chrome/browser/extensions/api/webview/webview_api.h @@ -0,0 +1,31 @@ +// Copyright (c) 2013 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#ifndef CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ +#define CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ + +#include "chrome/browser/extensions/extension_function.h" +#include "chrome/browser/extensions/script_executor.h" + +class WebviewExecuteScriptFunction : public AsyncExtensionFunction { + public: + DECLARE_EXTENSION_FUNCTION("webview.executeScript", WEBVIEW_EXECUTESCRIPT) + + WebviewExecuteScriptFunction(); + + protected: + virtual ~WebviewExecuteScriptFunction(); + + // ExtensionFunction implementation. + virtual bool RunImpl() OVERRIDE; + + private: + + void OnExecuteCodeFinished(const std::string& error, + int32 on_page_id, + const GURL& on_url, + const ListValue& result); + +}; +#endif // CHROME_BROWSER_EXTENSIONS_API_WEBVIEW_WEBVIEW_API_H_ |