Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1397)

Unified Diff: chrome/browser/extensions/api/webview/webview_api.cc

Issue 11968054: <webview>: Implement ExecuteScript (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Updated stale browser_plugin_messages.h Created 7 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: chrome/browser/extensions/api/webview/webview_api.cc
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 0000000000000000000000000000000000000000..54578912f00d6c0b4861048b7c4c63e9fb0b4c76
--- /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().
+}
+

Powered by Google App Engine
This is Rietveld 408576698