OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2011 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/renderer/extensions/extension_dispatcher.h" |
| 6 |
| 7 #include "base/command_line.h" |
| 8 #include "chrome/common/child_process_logging.h" |
| 9 #include "chrome/common/chrome_switches.h" |
| 10 #include "chrome/common/extensions/extension.h" |
| 11 #include "chrome/common/extensions/extension_messages.h" |
| 12 #include "chrome/renderer/extension_groups.h" |
| 13 #include "chrome/renderer/extensions/chrome_app_bindings.h" |
| 14 #include "chrome/renderer/extensions/event_bindings.h" |
| 15 #include "chrome/renderer/extensions/extension_process_bindings.h" |
| 16 #include "chrome/renderer/extensions/js_only_v8_extensions.h" |
| 17 #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
| 18 #include "chrome/renderer/render_thread.h" |
| 19 #include "chrome/renderer/user_script_slave.h" |
| 20 |
| 21 namespace { |
| 22 static const double kInitialExtensionIdleHandlerDelayS = 5.0 /* seconds */; |
| 23 static const int64 kMaxExtensionIdleHandlerDelayS = 5*60 /* seconds */; |
| 24 static ExtensionDispatcher* g_extension_dispatcher; |
| 25 } |
| 26 |
| 27 ExtensionDispatcher* ExtensionDispatcher::Get() { |
| 28 return g_extension_dispatcher; |
| 29 } |
| 30 |
| 31 ExtensionDispatcher::ExtensionDispatcher() { |
| 32 g_extension_dispatcher = this; |
| 33 |
| 34 std::string type_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII( |
| 35 switches::kProcessType); |
| 36 is_extension_process_ = type_str == switches::kExtensionProcess || |
| 37 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess); |
| 38 |
| 39 if (is_extension_process_) { |
| 40 RenderThread::current()->set_idle_notification_delay_in_s( |
| 41 kInitialExtensionIdleHandlerDelayS); |
| 42 } |
| 43 |
| 44 user_script_slave_.reset(new UserScriptSlave(&extensions_)); |
| 45 } |
| 46 |
| 47 ExtensionDispatcher::~ExtensionDispatcher() { |
| 48 g_extension_dispatcher = NULL; |
| 49 } |
| 50 |
| 51 bool ExtensionDispatcher::OnControlMessageReceived( |
| 52 const IPC::Message& message) { |
| 53 bool handled = true; |
| 54 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) |
| 55 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) |
| 56 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) |
| 57 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) |
| 58 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) |
| 59 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, |
| 60 OnSetScriptingWhitelist) |
| 61 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePageActions, OnPageActionsUpdated) |
| 62 IPC_MESSAGE_HANDLER(ExtensionMsg_SetAPIPermissions, OnSetAPIPermissions) |
| 63 IPC_MESSAGE_HANDLER(ExtensionMsg_SetHostPermissions, OnSetHostPermissions) |
| 64 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) |
| 65 IPC_MESSAGE_UNHANDLED(handled = false) |
| 66 IPC_END_MESSAGE_MAP() |
| 67 |
| 68 return handled; |
| 69 } |
| 70 |
| 71 void ExtensionDispatcher::OnRenderProcessShutdown() { |
| 72 delete this; |
| 73 } |
| 74 |
| 75 void ExtensionDispatcher::WebKitInitialized() { |
| 76 // For extensions, we want to ensure we call the IdleHandler every so often, |
| 77 // even if the extension keeps up activity. |
| 78 if (is_extension_process_) { |
| 79 forced_idle_timer_.Start( |
| 80 base::TimeDelta::FromSeconds(kMaxExtensionIdleHandlerDelayS), |
| 81 RenderThread::current(), &RenderThread::IdleHandler); |
| 82 } |
| 83 |
| 84 RenderThread::current()->RegisterExtension( |
| 85 extensions_v8::ChromeAppExtension::Get(), false); |
| 86 |
| 87 // Add v8 extensions related to chrome extensions. |
| 88 RenderThread::current()->RegisterExtension( |
| 89 ExtensionProcessBindings::Get(), true); |
| 90 RenderThread::current()->RegisterExtension( |
| 91 BaseJsV8Extension::Get(), true); |
| 92 RenderThread::current()->RegisterExtension( |
| 93 JsonSchemaJsV8Extension::Get(), true); |
| 94 RenderThread::current()->RegisterExtension( |
| 95 EventBindings::Get(), true); |
| 96 RenderThread::current()->RegisterExtension( |
| 97 RendererExtensionBindings::Get(), true); |
| 98 RenderThread::current()->RegisterExtension( |
| 99 ExtensionApiTestV8Extension::Get(), true); |
| 100 } |
| 101 |
| 102 bool ExtensionDispatcher::AllowScriptExtension( |
| 103 const std::string& v8_extension_name, |
| 104 const GURL& url, |
| 105 int extension_group) { |
| 106 // Extension-only bindings should be restricted to content scripts and |
| 107 // extension-blessed URLs. |
| 108 if (extension_group == EXTENSION_GROUP_CONTENT_SCRIPTS || |
| 109 extensions_.ExtensionBindingsAllowed(url)) { |
| 110 return true; |
| 111 } |
| 112 |
| 113 return false; |
| 114 } |
| 115 |
| 116 void ExtensionDispatcher::IdleNotification() { |
| 117 if (is_extension_process_) { |
| 118 // Dampen the forced delay as well if the extension stays idle for long |
| 119 // periods of time. |
| 120 int64 forced_delay_s = std::max(static_cast<int64>( |
| 121 RenderThread::current()->idle_notification_delay_in_s()), |
| 122 kMaxExtensionIdleHandlerDelayS); |
| 123 forced_idle_timer_.Stop(); |
| 124 forced_idle_timer_.Start( |
| 125 base::TimeDelta::FromSeconds(forced_delay_s), |
| 126 RenderThread::current(), &RenderThread::IdleHandler); |
| 127 } |
| 128 } |
| 129 |
| 130 void ExtensionDispatcher::OnSetFunctionNames( |
| 131 const std::vector<std::string>& names) { |
| 132 ExtensionProcessBindings::SetFunctionNames(names); |
| 133 } |
| 134 |
| 135 void ExtensionDispatcher::OnMessageInvoke(const std::string& extension_id, |
| 136 const std::string& function_name, |
| 137 const ListValue& args, |
| 138 const GURL& event_url) { |
| 139 RendererExtensionBindings::Invoke( |
| 140 extension_id, function_name, args, NULL, event_url); |
| 141 |
| 142 // Reset the idle handler each time there's any activity like event or message |
| 143 // dispatch, for which Invoke is the chokepoint. |
| 144 if (is_extension_process_) { |
| 145 RenderThread::current()->ScheduleIdleHandler( |
| 146 kInitialExtensionIdleHandlerDelayS); |
| 147 } |
| 148 } |
| 149 |
| 150 void ExtensionDispatcher::OnLoaded(const ExtensionMsg_Loaded_Params& params) { |
| 151 scoped_refptr<const Extension> extension(params.ConvertToExtension()); |
| 152 if (!extension) { |
| 153 // This can happen if extension parsing fails for any reason. One reason |
| 154 // this can legitimately happen is if the |
| 155 // --enable-experimental-extension-apis changes at runtime, which happens |
| 156 // during browser tests. Existing renderers won't know about the change. |
| 157 return; |
| 158 } |
| 159 |
| 160 extensions_.Insert(extension); |
| 161 } |
| 162 |
| 163 void ExtensionDispatcher::OnUnloaded(const std::string& id) { |
| 164 extensions_.Remove(id); |
| 165 } |
| 166 |
| 167 void ExtensionDispatcher::OnSetScriptingWhitelist( |
| 168 const Extension::ScriptingWhitelist& extension_ids) { |
| 169 Extension::SetScriptingWhitelist(extension_ids); |
| 170 } |
| 171 |
| 172 void ExtensionDispatcher::OnPageActionsUpdated( |
| 173 const std::string& extension_id, |
| 174 const std::vector<std::string>& page_actions) { |
| 175 ExtensionProcessBindings::SetPageActions(extension_id, page_actions); |
| 176 } |
| 177 |
| 178 void ExtensionDispatcher::OnSetAPIPermissions( |
| 179 const std::string& extension_id, |
| 180 const std::set<std::string>& permissions) { |
| 181 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions); |
| 182 |
| 183 // This is called when starting a new extension page, so start the idle |
| 184 // handler ticking. |
| 185 RenderThread::current()->ScheduleIdleHandler( |
| 186 kInitialExtensionIdleHandlerDelayS); |
| 187 |
| 188 UpdateActiveExtensions(); |
| 189 } |
| 190 |
| 191 void ExtensionDispatcher::OnSetHostPermissions( |
| 192 const GURL& extension_url, const std::vector<URLPattern>& permissions) { |
| 193 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions); |
| 194 } |
| 195 |
| 196 void ExtensionDispatcher::OnUpdateUserScripts( |
| 197 base::SharedMemoryHandle scripts) { |
| 198 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle"; |
| 199 user_script_slave_->UpdateScripts(scripts); |
| 200 UpdateActiveExtensions(); |
| 201 } |
| 202 |
| 203 void ExtensionDispatcher::UpdateActiveExtensions() { |
| 204 // In single-process mode, the browser process reports the active extensions. |
| 205 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess)) |
| 206 return; |
| 207 |
| 208 std::set<std::string> active_extensions; |
| 209 user_script_slave_->GetActiveExtensions(&active_extensions); |
| 210 ExtensionProcessBindings::GetActiveExtensions(&active_extensions); |
| 211 child_process_logging::SetActiveExtensions(active_extensions); |
| 212 } |
OLD | NEW |