Chromium Code Reviews| Index: chrome/browser/extensions/api/webview/execute_script_function.cc |
| diff --git a/chrome/browser/extensions/api/webview/execute_script_function.cc b/chrome/browser/extensions/api/webview/execute_script_function.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8f951f4ecadf5c92391afce4c515b0f932a4f664 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/api/webview/execute_script_function.cc |
| @@ -0,0 +1,107 @@ |
| +// 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/execute_script_function.h" |
| + |
| +#include "chrome/browser/extensions/extension_host.h" |
| +#include "chrome/browser/extensions/extension_process_manager.h" |
| +#include "chrome/browser/extensions/extension_system.h" |
| +#include "chrome/common/extensions/api/tabs.h" |
| +#include "chrome/common/extensions/extension_messages.h" |
| +#include "chrome/common/extensions/value_builder.h" |
| +#include "content/public/browser/render_view_host.h" |
| + |
| +using extensions::api::tabs::InjectDetails; |
| + |
| +namespace extensions { |
| + |
| +ExecuteScriptFunction::ExecuteScriptFunction() { |
| +} |
| + |
| +ExecuteScriptFunction::~ExecuteScriptFunction() { |
| +} |
| + |
| +bool ExecuteScriptFunction::RunImpl() { |
| + if (args_->empty()) |
| + return false; |
| + |
| + int process_id = 0; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(0, &process_id)); |
| + |
| + int route_id = 0; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetInteger(1, &route_id)); |
| + |
| + base::DictionaryValue* details_value = NULL; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(2, &details_value)); |
|
Matt Perry
2013/01/23 02:50:49
You can clean up some of this parsing logic using
Fady Samuel
2013/01/23 20:12:03
Done.
|
| + |
| + scoped_ptr<InjectDetails> details(new InjectDetails()); |
| + if (!InjectDetails::Populate(*details_value, details.get())) |
| + return false; |
| + |
| + content::RenderViewHost* guest_rvh = |
| + content::RenderViewHost::FromID(process_id, 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_; |
|
Matt Perry
2013/01/23 02:50:49
no trailing _
Fady Samuel
2013/01/23 20:12:03
Done.
|
| + scoped_ptr<extensions::ScriptExecutor> script_executor_( |
| + new extensions::ScriptExecutor(guest_web_contents, &script_observers_)); |
| + |
| + extensions::ScriptExecutor::FrameScope frame_scope = |
| + details->all_frames.get() && *details->all_frames ? |
| + extensions::ScriptExecutor::ALL_FRAMES : |
| + extensions::ScriptExecutor::TOP_FRAME; |
| + |
| + extensions::UserScript::RunLocation run_at = |
| + extensions::UserScript::UNDEFINED; |
| + switch (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, |
| + *details->code.get(), |
|
Matt Perry
2013/01/23 02:50:49
There's a version of executeScript that uses a fil
Fady Samuel
2013/01/23 20:12:03
I'll have to chat with creis@ and others and perha
|
| + frame_scope, |
| + run_at, |
| + extensions::ScriptExecutor::ISOLATED_WORLD, |
| + base::Bind( |
| + &ExecuteScriptFunction::OnExecuteCodeFinished, |
| + this)); |
| + |
| + // Balanced in OnExecuteCodeFinished. |
| + AddRef(); |
| + return true; |
| +} |
| + |
| +void ExecuteScriptFunction::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(). |
| +} |
| + |
| +} // namespace extensions |