OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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/browser_plugin/chrome_browser_plugin_guest_observer.h" |
| 6 |
| 7 #include <stdio.h> |
| 8 |
| 9 #include "base/values.h" |
| 10 #include "chrome/browser/extensions/extension_service.h" |
| 11 #include "chrome/common/chrome_browser_plugin_messages.h" |
| 12 #include "chrome/common/extensions/api/tabs.h" |
| 13 #include "chrome/common/extensions/extension_messages.h" |
| 14 #include "chrome/common/extensions/value_builder.h" |
| 15 #include "content/public/browser/browser_plugin/browser_plugin_guest.h" |
| 16 |
| 17 using extensions::api::tabs::InjectDetails; |
| 18 |
| 19 ChromeBrowserPluginGuestObserver::ChromeBrowserPluginGuestObserver( |
| 20 content::BrowserPluginGuest* guest) |
| 21 : content::BrowserPluginGuestObserver(guest), |
| 22 script_executor_(new extensions::ScriptExecutor( |
| 23 guest->GetWebContents(), &script_observers_)) { |
| 24 AddRef(); |
| 25 guest->RequestMessageFromEmbedder( |
| 26 ChromeBrowserPluginHostMsg_ExecuteScript::ID); |
| 27 |
| 28 // Install a component extension to support executeScript in guests. |
| 29 scoped_ptr<base::DictionaryValue> manifest = extensions::DictionaryBuilder() |
| 30 .Set("name", "BrowserPlugin") |
| 31 .Set("version", "1") |
| 32 .Set("description", "BrowserPlugin Extension API") |
| 33 .Set("permissions", extensions::ListBuilder() |
| 34 .Append("tabs") |
| 35 .Append("http://*/*") |
| 36 .Append("https://*/*")) |
| 37 .Build(); |
| 38 |
| 39 std::string error; |
| 40 extension_ = extensions::Extension::Create( |
| 41 FilePath(), extensions::Extension::COMPONENT, *manifest, |
| 42 extensions::Extension::NO_FLAGS, &error); |
| 43 CHECK(extension_); |
| 44 |
| 45 std::vector<ExtensionMsg_Loaded_Params> extensions; |
| 46 extensions.push_back(ExtensionMsg_Loaded_Params(extension_)); |
| 47 Send(new ExtensionMsg_Loaded(extensions)); |
| 48 } |
| 49 |
| 50 ChromeBrowserPluginGuestObserver::~ChromeBrowserPluginGuestObserver() { |
| 51 } |
| 52 |
| 53 void ChromeBrowserPluginGuestObserver::OnDestruct() { |
| 54 Release(); |
| 55 } |
| 56 |
| 57 bool ChromeBrowserPluginGuestObserver::OnMessageReceivedFromEmbedder( |
| 58 const IPC::Message& message) { |
| 59 bool handled = true; |
| 60 IPC_BEGIN_MESSAGE_MAP(ChromeBrowserPluginGuestObserver, message) |
| 61 IPC_MESSAGE_HANDLER(ChromeBrowserPluginHostMsg_ExecuteScript, |
| 62 OnExecuteScript) |
| 63 IPC_MESSAGE_UNHANDLED(handled = false) |
| 64 IPC_END_MESSAGE_MAP() |
| 65 return handled; |
| 66 } |
| 67 |
| 68 void ChromeBrowserPluginGuestObserver::OnExecuteScript( |
| 69 int instance_id, int request_id, const base::ListValue& list) { |
| 70 // |details| are not optional. |
| 71 const DictionaryValue* details_value = NULL; |
| 72 if (!list.GetDictionary(0, &details_value)) |
| 73 return; |
| 74 scoped_ptr<InjectDetails> details(new InjectDetails()); |
| 75 if (!InjectDetails::Populate(*details_value, details.get())) |
| 76 return; |
| 77 extensions::ScriptExecutor::FrameScope frame_scope = |
| 78 details->all_frames.get() && *details->all_frames ? |
| 79 extensions::ScriptExecutor::ALL_FRAMES : |
| 80 extensions::ScriptExecutor::TOP_FRAME; |
| 81 |
| 82 extensions::UserScript::RunLocation run_at = |
| 83 extensions::UserScript::UNDEFINED; |
| 84 switch (details->run_at) { |
| 85 case InjectDetails::RUN_AT_NONE: |
| 86 case InjectDetails::RUN_AT_DOCUMENT_IDLE: |
| 87 run_at = extensions::UserScript::DOCUMENT_IDLE; |
| 88 break; |
| 89 case InjectDetails::RUN_AT_DOCUMENT_START: |
| 90 run_at = extensions::UserScript::DOCUMENT_START; |
| 91 break; |
| 92 case InjectDetails::RUN_AT_DOCUMENT_END: |
| 93 run_at = extensions::UserScript::DOCUMENT_END; |
| 94 break; |
| 95 } |
| 96 CHECK_NE(extensions::UserScript::UNDEFINED, run_at); |
| 97 |
| 98 script_executor_->ExecuteScript( |
| 99 extension_->id(), |
| 100 extensions::ScriptExecutor::JAVASCRIPT, |
| 101 *details->code.get(), |
| 102 frame_scope, |
| 103 run_at, |
| 104 extensions::ScriptExecutor::ISOLATED_WORLD, |
| 105 base::Bind( |
| 106 &ChromeBrowserPluginGuestObserver::OnExecuteCodeFinished, |
| 107 this, request_id)); |
| 108 } |
| 109 |
| 110 void ChromeBrowserPluginGuestObserver::OnExecuteCodeFinished( |
| 111 int request_id, |
| 112 const std::string& error, |
| 113 int32 on_page_id, |
| 114 const GURL& on_url, |
| 115 const ListValue& result) { |
| 116 SendMessageToEmbedder(new ChromeBrowserPluginHostMsg_ExecuteScript_Response( |
| 117 browser_plugin_guest()->GetEmbedderWebContents()->GetRoutingID(), |
| 118 instance_id(), request_id, result)); |
| 119 } |
| 120 |
OLD | NEW |