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