| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #include "extensions/renderer/dispatcher.h" | 5 #include "extensions/renderer/dispatcher.h" |
| 6 | 6 |
| 7 #include "base/bind.h" |
| 7 #include "base/callback.h" | 8 #include "base/callback.h" |
| 8 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 9 #include "base/debug/alias.h" | 10 #include "base/debug/alias.h" |
| 10 #include "base/memory/scoped_ptr.h" | 11 #include "base/memory/scoped_ptr.h" |
| 11 #include "base/metrics/user_metrics_action.h" | 12 #include "base/metrics/user_metrics_action.h" |
| 12 #include "base/strings/string_piece.h" | 13 #include "base/strings/string_piece.h" |
| 13 #include "base/strings/string_split.h" | 14 #include "base/strings/string_split.h" |
| 14 #include "base/strings/string_util.h" | 15 #include "base/strings/string_util.h" |
| 15 #include "base/values.h" | 16 #include "base/values.h" |
| 16 #include "content/public/common/content_switches.h" | 17 #include "content/public/common/content_switches.h" |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 59 #include "extensions/renderer/messaging_bindings.h" | 60 #include "extensions/renderer/messaging_bindings.h" |
| 60 #include "extensions/renderer/module_system.h" | 61 #include "extensions/renderer/module_system.h" |
| 61 #include "extensions/renderer/print_native_handler.h" | 62 #include "extensions/renderer/print_native_handler.h" |
| 62 #include "extensions/renderer/process_info_native_handler.h" | 63 #include "extensions/renderer/process_info_native_handler.h" |
| 63 #include "extensions/renderer/render_view_observer_natives.h" | 64 #include "extensions/renderer/render_view_observer_natives.h" |
| 64 #include "extensions/renderer/request_sender.h" | 65 #include "extensions/renderer/request_sender.h" |
| 65 #include "extensions/renderer/runtime_custom_bindings.h" | 66 #include "extensions/renderer/runtime_custom_bindings.h" |
| 66 #include "extensions/renderer/safe_builtins.h" | 67 #include "extensions/renderer/safe_builtins.h" |
| 67 #include "extensions/renderer/script_context.h" | 68 #include "extensions/renderer/script_context.h" |
| 68 #include "extensions/renderer/script_context_set.h" | 69 #include "extensions/renderer/script_context_set.h" |
| 70 #include "extensions/renderer/script_injection.h" |
| 71 #include "extensions/renderer/script_injection_manager.h" |
| 69 #include "extensions/renderer/send_request_natives.h" | 72 #include "extensions/renderer/send_request_natives.h" |
| 70 #include "extensions/renderer/set_icon_natives.h" | 73 #include "extensions/renderer/set_icon_natives.h" |
| 71 #include "extensions/renderer/test_features_native_handler.h" | 74 #include "extensions/renderer/test_features_native_handler.h" |
| 72 #include "extensions/renderer/user_gestures_native_handler.h" | 75 #include "extensions/renderer/user_gestures_native_handler.h" |
| 73 #include "extensions/renderer/user_script_slave.h" | |
| 74 #include "extensions/renderer/utils_native_handler.h" | 76 #include "extensions/renderer/utils_native_handler.h" |
| 75 #include "extensions/renderer/v8_context_native_handler.h" | 77 #include "extensions/renderer/v8_context_native_handler.h" |
| 76 #include "grit/extensions_renderer_resources.h" | 78 #include "grit/extensions_renderer_resources.h" |
| 77 #include "third_party/WebKit/public/platform/WebString.h" | 79 #include "third_party/WebKit/public/platform/WebString.h" |
| 78 #include "third_party/WebKit/public/platform/WebURLRequest.h" | 80 #include "third_party/WebKit/public/platform/WebURLRequest.h" |
| 79 #include "third_party/WebKit/public/web/WebCustomElement.h" | 81 #include "third_party/WebKit/public/web/WebCustomElement.h" |
| 80 #include "third_party/WebKit/public/web/WebDataSource.h" | 82 #include "third_party/WebKit/public/web/WebDataSource.h" |
| 81 #include "third_party/WebKit/public/web/WebDocument.h" | 83 #include "third_party/WebKit/public/web/WebDocument.h" |
| 82 #include "third_party/WebKit/public/web/WebFrame.h" | 84 #include "third_party/WebKit/public/web/WebFrame.h" |
| 83 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" | 85 #include "third_party/WebKit/public/web/WebRuntimeFeatures.h" |
| (...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 } | 172 } |
| 171 }; | 173 }; |
| 172 | 174 |
| 173 } // namespace | 175 } // namespace |
| 174 | 176 |
| 175 Dispatcher::Dispatcher(DispatcherDelegate* delegate) | 177 Dispatcher::Dispatcher(DispatcherDelegate* delegate) |
| 176 : delegate_(delegate), | 178 : delegate_(delegate), |
| 177 content_watcher_(new ContentWatcher()), | 179 content_watcher_(new ContentWatcher()), |
| 178 source_map_(&ResourceBundle::GetSharedInstance()), | 180 source_map_(&ResourceBundle::GetSharedInstance()), |
| 179 v8_schema_registry_(new V8SchemaRegistry), | 181 v8_schema_registry_(new V8SchemaRegistry), |
| 180 is_webkit_initialized_(false) { | 182 is_webkit_initialized_(false), |
| 183 user_script_injection_list_observer_(this) { |
| 181 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); | 184 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); |
| 182 is_extension_process_ = | 185 is_extension_process_ = |
| 183 command_line.HasSwitch(extensions::switches::kExtensionProcess) || | 186 command_line.HasSwitch(extensions::switches::kExtensionProcess) || |
| 184 command_line.HasSwitch(::switches::kSingleProcess); | 187 command_line.HasSwitch(::switches::kSingleProcess); |
| 185 | 188 |
| 186 if (is_extension_process_) { | 189 if (is_extension_process_) { |
| 187 RenderThread::Get()->SetIdleNotificationDelayInMs( | 190 RenderThread::Get()->SetIdleNotificationDelayInMs( |
| 188 kInitialExtensionIdleHandlerDelayMs); | 191 kInitialExtensionIdleHandlerDelayMs); |
| 189 } | 192 } |
| 190 | 193 |
| 191 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension()); | 194 RenderThread::Get()->RegisterExtension(SafeBuiltins::CreateV8Extension()); |
| 192 | 195 |
| 193 user_script_slave_.reset(new UserScriptSlave(&extensions_)); | 196 script_injection_manager_.reset(new ScriptInjectionManager(&extensions_)); |
| 197 user_script_injection_list_observer_.Add( |
| 198 script_injection_manager_->user_script_injection_list()); |
| 194 request_sender_.reset(new RequestSender(this)); | 199 request_sender_.reset(new RequestSender(this)); |
| 195 PopulateSourceMap(); | 200 PopulateSourceMap(); |
| 196 } | 201 } |
| 197 | 202 |
| 198 Dispatcher::~Dispatcher() { | 203 Dispatcher::~Dispatcher() { |
| 199 } | 204 } |
| 200 | 205 |
| 206 void Dispatcher::OnRenderViewCreated(content::RenderView* render_view) { |
| 207 script_injection_manager_->OnRenderViewCreated(render_view); |
| 208 } |
| 209 |
| 201 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const { | 210 bool Dispatcher::IsExtensionActive(const std::string& extension_id) const { |
| 202 bool is_active = | 211 bool is_active = |
| 203 active_extension_ids_.find(extension_id) != active_extension_ids_.end(); | 212 active_extension_ids_.find(extension_id) != active_extension_ids_.end(); |
| 204 if (is_active) | 213 if (is_active) |
| 205 CHECK(extensions_.Contains(extension_id)); | 214 CHECK(extensions_.Contains(extension_id)); |
| 206 return is_active; | 215 return is_active; |
| 207 } | 216 } |
| 208 | 217 |
| 209 std::string Dispatcher::GetExtensionID(const WebFrame* frame, int world_id) { | 218 std::string Dispatcher::GetExtensionID(const WebFrame* frame, int world_id) { |
| 210 if (world_id != 0) { | 219 if (world_id != 0) { |
| 211 // Isolated worlds (content script). | 220 // Isolated worlds (content script). |
| 212 return user_script_slave_->GetExtensionIdForIsolatedWorld(world_id); | 221 return ScriptInjection::GetExtensionIdForIsolatedWorld(world_id); |
| 213 } | 222 } |
| 214 | 223 |
| 215 // TODO(kalman): Delete this check. | 224 // TODO(kalman): Delete this check. |
| 216 if (frame->document().securityOrigin().isUnique()) | 225 if (frame->document().securityOrigin().isUnique()) |
| 217 return std::string(); | 226 return std::string(); |
| 218 | 227 |
| 219 // Extension pages (chrome-extension:// URLs). | 228 // Extension pages (chrome-extension:// URLs). |
| 220 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); | 229 GURL frame_url = ScriptContext::GetDataSourceURLForFrame(frame); |
| 221 return extensions_.GetExtensionOrAppIDByURL(frame_url); | 230 return extensions_.GetExtensionOrAppIDByURL(frame_url); |
| 222 } | 231 } |
| (...skipping 236 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 459 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, | 468 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, |
| 460 OnSetScriptingWhitelist) | 469 OnSetScriptingWhitelist) |
| 461 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSystemFont, OnSetSystemFont) | 470 IPC_MESSAGE_HANDLER(ExtensionMsg_SetSystemFont, OnSetSystemFont) |
| 462 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldSuspend, OnShouldSuspend) | 471 IPC_MESSAGE_HANDLER(ExtensionMsg_ShouldSuspend, OnShouldSuspend) |
| 463 IPC_MESSAGE_HANDLER(ExtensionMsg_Suspend, OnSuspend) | 472 IPC_MESSAGE_HANDLER(ExtensionMsg_Suspend, OnSuspend) |
| 464 IPC_MESSAGE_HANDLER(ExtensionMsg_TransferBlobs, OnTransferBlobs) | 473 IPC_MESSAGE_HANDLER(ExtensionMsg_TransferBlobs, OnTransferBlobs) |
| 465 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) | 474 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) |
| 466 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions) | 475 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions) |
| 467 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateTabSpecificPermissions, | 476 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateTabSpecificPermissions, |
| 468 OnUpdateTabSpecificPermissions) | 477 OnUpdateTabSpecificPermissions) |
| 469 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) | |
| 470 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI) | 478 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI) |
| 471 IPC_MESSAGE_FORWARD(ExtensionMsg_WatchPages, | 479 IPC_MESSAGE_FORWARD(ExtensionMsg_WatchPages, |
| 472 content_watcher_.get(), | 480 content_watcher_.get(), |
| 473 ContentWatcher::OnWatchPages) | 481 ContentWatcher::OnWatchPages) |
| 474 IPC_MESSAGE_UNHANDLED(handled = false) | 482 IPC_MESSAGE_UNHANDLED(handled = false) |
| 475 IPC_END_MESSAGE_MAP() | 483 IPC_END_MESSAGE_MAP() |
| 476 | 484 |
| 477 return handled; | 485 return handled; |
| 478 } | 486 } |
| 479 | 487 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 687 RenderThread::Get()->Send(new ExtensionHostMsg_TransferBlobsAck(blob_uuids)); | 695 RenderThread::Get()->Send(new ExtensionHostMsg_TransferBlobsAck(blob_uuids)); |
| 688 } | 696 } |
| 689 | 697 |
| 690 void Dispatcher::OnUnloaded(const std::string& id) { | 698 void Dispatcher::OnUnloaded(const std::string& id) { |
| 691 extensions_.Remove(id); | 699 extensions_.Remove(id); |
| 692 active_extension_ids_.erase(id); | 700 active_extension_ids_.erase(id); |
| 693 | 701 |
| 694 // If the extension is later reloaded with a different set of permissions, | 702 // If the extension is later reloaded with a different set of permissions, |
| 695 // we'd like it to get a new isolated world ID, so that it can pick up the | 703 // we'd like it to get a new isolated world ID, so that it can pick up the |
| 696 // changed origin whitelist. | 704 // changed origin whitelist. |
| 697 user_script_slave_->RemoveIsolatedWorld(id); | 705 ScriptInjection::RemoveIsolatedWorld(id); |
| 698 | 706 |
| 699 // Invalidate all of the contexts that were removed. | 707 // Invalidate all of the contexts that were removed. |
| 700 // TODO(kalman): add an invalidation observer interface to ScriptContext. | 708 // TODO(kalman): add an invalidation observer interface to ScriptContext. |
| 701 ScriptContextSet::ContextSet removed_contexts = | 709 ScriptContextSet::ContextSet removed_contexts = |
| 702 script_context_set_.OnExtensionUnloaded(id); | 710 script_context_set_.OnExtensionUnloaded(id); |
| 703 for (ScriptContextSet::ContextSet::iterator it = removed_contexts.begin(); | 711 for (ScriptContextSet::ContextSet::iterator it = removed_contexts.begin(); |
| 704 it != removed_contexts.end(); | 712 it != removed_contexts.end(); |
| 705 ++it) { | 713 ++it) { |
| 706 request_sender_->InvalidateSource(*it); | 714 request_sender_->InvalidateSource(*it); |
| 707 } | 715 } |
| (...skipping 51 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 759 | 767 |
| 760 void Dispatcher::OnUpdateTabSpecificPermissions( | 768 void Dispatcher::OnUpdateTabSpecificPermissions( |
| 761 int page_id, | 769 int page_id, |
| 762 int tab_id, | 770 int tab_id, |
| 763 const std::string& extension_id, | 771 const std::string& extension_id, |
| 764 const URLPatternSet& origin_set) { | 772 const URLPatternSet& origin_set) { |
| 765 delegate_->UpdateTabSpecificPermissions( | 773 delegate_->UpdateTabSpecificPermissions( |
| 766 this, page_id, tab_id, extension_id, origin_set); | 774 this, page_id, tab_id, extension_id, origin_set); |
| 767 } | 775 } |
| 768 | 776 |
| 769 void Dispatcher::OnUpdateUserScripts( | |
| 770 base::SharedMemoryHandle scripts, | |
| 771 const std::set<std::string>& extension_ids) { | |
| 772 if (!base::SharedMemory::IsHandleValid(scripts)) { | |
| 773 NOTREACHED() << "Bad scripts handle"; | |
| 774 return; | |
| 775 } | |
| 776 | |
| 777 for (std::set<std::string>::const_iterator iter = extension_ids.begin(); | |
| 778 iter != extension_ids.end(); | |
| 779 ++iter) { | |
| 780 if (!Extension::IdIsValid(*iter)) { | |
| 781 NOTREACHED() << "Invalid extension id: " << *iter; | |
| 782 return; | |
| 783 } | |
| 784 } | |
| 785 | |
| 786 user_script_slave_->UpdateScripts(scripts, extension_ids); | |
| 787 UpdateActiveExtensions(); | |
| 788 } | |
| 789 | |
| 790 void Dispatcher::OnUsingWebRequestAPI(bool adblock, | 777 void Dispatcher::OnUsingWebRequestAPI(bool adblock, |
| 791 bool adblock_plus, | 778 bool adblock_plus, |
| 792 bool other_webrequest) { | 779 bool other_webrequest) { |
| 793 delegate_->HandleWebRequestAPIUsage(adblock, adblock_plus, other_webrequest); | 780 delegate_->HandleWebRequestAPIUsage(adblock, adblock_plus, other_webrequest); |
| 794 } | 781 } |
| 795 | 782 |
| 783 void Dispatcher::OnUserScriptsUpdated( |
| 784 const std::set<std::string>& changed_extensions, |
| 785 const std::vector<UserScript*>& scripts) { |
| 786 UpdateActiveExtensions(); |
| 787 } |
| 788 |
| 796 void Dispatcher::UpdateActiveExtensions() { | 789 void Dispatcher::UpdateActiveExtensions() { |
| 797 std::set<std::string> active_extensions = active_extension_ids_; | 790 std::set<std::string> active_extensions = active_extension_ids_; |
| 798 user_script_slave_->GetActiveExtensions(&active_extensions); | 791 script_injection_manager_->user_script_injection_list() |
| 792 ->GetActiveExtensionIds(&active_extensions); |
| 799 delegate_->OnActiveExtensionsUpdated(active_extensions); | 793 delegate_->OnActiveExtensionsUpdated(active_extensions); |
| 800 } | 794 } |
| 801 | 795 |
| 802 void Dispatcher::InitOriginPermissions(const Extension* extension) { | 796 void Dispatcher::InitOriginPermissions(const Extension* extension) { |
| 803 delegate_->InitOriginPermissions(extension, | 797 delegate_->InitOriginPermissions(extension, |
| 804 IsExtensionActive(extension->id())); | 798 IsExtensionActive(extension->id())); |
| 805 UpdateOriginPermissions( | 799 UpdateOriginPermissions( |
| 806 UpdatedExtensionPermissionsInfo::ADDED, | 800 UpdatedExtensionPermissionsInfo::ADDED, |
| 807 extension, | 801 extension, |
| 808 extension->permissions_data()->GetEffectiveHostPermissions()); | 802 extension->permissions_data()->GetEffectiveHostPermissions()); |
| (...skipping 425 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 1234 return v8::Handle<v8::Object>(); | 1228 return v8::Handle<v8::Object>(); |
| 1235 | 1229 |
| 1236 if (bind_name) | 1230 if (bind_name) |
| 1237 *bind_name = split.back(); | 1231 *bind_name = split.back(); |
| 1238 | 1232 |
| 1239 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context)) | 1233 return bind_object.IsEmpty() ? AsObjectOrEmpty(GetOrCreateChrome(context)) |
| 1240 : bind_object; | 1234 : bind_object; |
| 1241 } | 1235 } |
| 1242 | 1236 |
| 1243 } // namespace extensions | 1237 } // namespace extensions |
| OLD | NEW |