OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2013 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #include "chrome/browser/extensions/api/webview/webview_api.h" |
| 6 |
| 7 #include "chrome/common/extensions/api/webview.h" |
| 8 #include "content/public/browser/render_view_host.h" |
| 9 |
| 10 using namespace extensions::api::webview; |
| 11 |
| 12 WebviewExecuteScriptFunction::WebviewExecuteScriptFunction() { |
| 13 } |
| 14 |
| 15 WebviewExecuteScriptFunction::~WebviewExecuteScriptFunction() { |
| 16 } |
| 17 |
| 18 bool WebviewExecuteScriptFunction::RunImpl() { |
| 19 scoped_ptr<ExecuteScript::Params> params( |
| 20 extensions::api::webview::ExecuteScript::Params::Create(*args_)); |
| 21 EXTENSION_FUNCTION_VALIDATE(params.get()); |
| 22 |
| 23 content::RenderViewHost* guest_rvh = |
| 24 content::RenderViewHost::FromID(params->process_id, params->route_id); |
| 25 // If we haven't loaded a guest yet, then this will be NULL. |
| 26 if (!guest_rvh) |
| 27 return false; |
| 28 content::WebContents* guest_web_contents = |
| 29 content::WebContents::FromRenderViewHost(guest_rvh); |
| 30 CHECK(guest_web_contents); |
| 31 |
| 32 ObserverList<extensions::TabHelper::ScriptExecutionObserver> |
| 33 script_observers; |
| 34 scoped_ptr<extensions::ScriptExecutor> script_executor( |
| 35 new extensions::ScriptExecutor(guest_web_contents, &script_observers)); |
| 36 |
| 37 extensions::ScriptExecutor::FrameScope frame_scope = |
| 38 params->details.all_frames.get() && *params->details.all_frames ? |
| 39 extensions::ScriptExecutor::ALL_FRAMES : |
| 40 extensions::ScriptExecutor::TOP_FRAME; |
| 41 |
| 42 extensions::UserScript::RunLocation run_at = |
| 43 extensions::UserScript::UNDEFINED; |
| 44 switch (params->details.run_at) { |
| 45 case InjectDetails::RUN_AT_NONE: |
| 46 case InjectDetails::RUN_AT_DOCUMENT_IDLE: |
| 47 run_at = extensions::UserScript::DOCUMENT_IDLE; |
| 48 break; |
| 49 case InjectDetails::RUN_AT_DOCUMENT_START: |
| 50 run_at = extensions::UserScript::DOCUMENT_START; |
| 51 break; |
| 52 case InjectDetails::RUN_AT_DOCUMENT_END: |
| 53 run_at = extensions::UserScript::DOCUMENT_END; |
| 54 break; |
| 55 } |
| 56 CHECK_NE(extensions::UserScript::UNDEFINED, run_at); |
| 57 |
| 58 script_executor->ExecuteScript( |
| 59 GetExtension()->id(), |
| 60 extensions::ScriptExecutor::JAVASCRIPT, |
| 61 *params->details.code.get(), |
| 62 frame_scope, |
| 63 run_at, |
| 64 extensions::ScriptExecutor::ISOLATED_WORLD, |
| 65 true /* is_web_view */, |
| 66 base::Bind( |
| 67 &WebviewExecuteScriptFunction::OnExecuteCodeFinished, |
| 68 this)); |
| 69 |
| 70 // Balanced in OnExecuteCodeFinished. |
| 71 AddRef(); |
| 72 return true; |
| 73 } |
| 74 |
| 75 void WebviewExecuteScriptFunction::OnExecuteCodeFinished( |
| 76 const std::string& error, |
| 77 int32 on_page_id, |
| 78 const GURL& on_url, |
| 79 const ListValue& result) { |
| 80 if (error.empty()) { |
| 81 SetResult(result.DeepCopy()); |
| 82 } else { |
| 83 SetError(error); |
| 84 } |
| 85 SendResponse(error.empty()); |
| 86 Release(); // Added in RunImpl(). |
| 87 } |
| 88 |
OLD | NEW |