Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(11)

Side by Side Diff: chrome/renderer/extensions/extension_dispatcher.cc

Issue 8574012: Schedule idle handler based on CPU usage. (Closed) Base URL: http://git.chromium.org/git/chromium.git@trunk
Patch Set: Created 9 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. 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 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 "chrome/renderer/extensions/extension_dispatcher.h" 5 #include "chrome/renderer/extensions/extension_dispatcher.h"
6 6
7 #include "base/command_line.h" 7 #include "base/command_line.h"
8 #include "chrome/common/child_process_logging.h" 8 #include "chrome/common/child_process_logging.h"
9 #include "chrome/common/chrome_switches.h" 9 #include "chrome/common/chrome_switches.h"
10 #include "chrome/common/extensions/extension.h" 10 #include "chrome/common/extensions/extension.h"
(...skipping 13 matching lines...) Expand all
24 #include "chrome/renderer/extensions/user_script_slave.h" 24 #include "chrome/renderer/extensions/user_script_slave.h"
25 #include "content/public/renderer/render_thread.h" 25 #include "content/public/renderer/render_thread.h"
26 #include "grit/renderer_resources.h" 26 #include "grit/renderer_resources.h"
27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h" 27 #include "third_party/WebKit/Source/WebKit/chromium/public/WebDataSource.h"
28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h" 28 #include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h" 29 #include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h" 30 #include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h" 31 #include "third_party/WebKit/Source/WebKit/chromium/public/WebURLRequest.h"
32 #include "v8/include/v8.h" 32 #include "v8/include/v8.h"
33 33
34 namespace {
35 static const int64 kInitialExtensionIdleHandlerDelayMs = 5*1000;
36 static const int64 kMaxExtensionIdleHandlerDelayMs = 5*60*1000;
37 }
38
39 using extensions::MiscellaneousBindings; 34 using extensions::MiscellaneousBindings;
40 using extensions::SchemaGeneratedBindings; 35 using extensions::SchemaGeneratedBindings;
41 using WebKit::WebDataSource; 36 using WebKit::WebDataSource;
42 using WebKit::WebFrame; 37 using WebKit::WebFrame;
43 using WebKit::WebSecurityPolicy; 38 using WebKit::WebSecurityPolicy;
44 using WebKit::WebString; 39 using WebKit::WebString;
45 using content::RenderThread; 40 using content::RenderThread;
46 41
47 ExtensionDispatcher::ExtensionDispatcher() 42 ExtensionDispatcher::ExtensionDispatcher()
48 : is_webkit_initialized_(false), 43 : is_webkit_initialized_(false),
49 webrequest_adblock_(false), 44 webrequest_adblock_(false),
50 webrequest_adblock_plus_(false), 45 webrequest_adblock_plus_(false),
51 webrequest_other_(false) { 46 webrequest_other_(false) {
52 const CommandLine& command_line = *(CommandLine::ForCurrentProcess()); 47 const CommandLine& command_line = *(CommandLine::ForCurrentProcess());
53 is_extension_process_ = 48 is_extension_process_ =
54 command_line.HasSwitch(switches::kExtensionProcess) || 49 command_line.HasSwitch(switches::kExtensionProcess) ||
55 command_line.HasSwitch(switches::kSingleProcess); 50 command_line.HasSwitch(switches::kSingleProcess);
56 51
57 if (is_extension_process_) {
58 RenderThread::Get()->SetIdleNotificationDelayInMs(
59 kInitialExtensionIdleHandlerDelayMs);
60 }
61
62 user_script_slave_.reset(new UserScriptSlave(&extensions_)); 52 user_script_slave_.reset(new UserScriptSlave(&extensions_));
63 } 53 }
64 54
65 ExtensionDispatcher::~ExtensionDispatcher() { 55 ExtensionDispatcher::~ExtensionDispatcher() {
66 } 56 }
67 57
68 bool ExtensionDispatcher::OnControlMessageReceived( 58 bool ExtensionDispatcher::OnControlMessageReceived(
69 const IPC::Message& message) { 59 const IPC::Message& message) {
70 bool handled = true; 60 bool handled = true;
71 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message) 61 IPC_BEGIN_MESSAGE_MAP(ExtensionDispatcher, message)
72 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke) 62 IPC_MESSAGE_HANDLER(ExtensionMsg_MessageInvoke, OnMessageInvoke)
73 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage) 63 IPC_MESSAGE_HANDLER(ExtensionMsg_DeliverMessage, OnDeliverMessage)
74 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames) 64 IPC_MESSAGE_HANDLER(ExtensionMsg_SetFunctionNames, OnSetFunctionNames)
75 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded) 65 IPC_MESSAGE_HANDLER(ExtensionMsg_Loaded, OnLoaded)
76 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded) 66 IPC_MESSAGE_HANDLER(ExtensionMsg_Unloaded, OnUnloaded)
77 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist, 67 IPC_MESSAGE_HANDLER(ExtensionMsg_SetScriptingWhitelist,
78 OnSetScriptingWhitelist) 68 OnSetScriptingWhitelist)
79 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension) 69 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateExtension, OnActivateExtension)
80 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateApplication, OnActivateApplication) 70 IPC_MESSAGE_HANDLER(ExtensionMsg_ActivateApplication, OnActivateApplication)
81 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions) 71 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdatePermissions, OnUpdatePermissions)
82 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts) 72 IPC_MESSAGE_HANDLER(ExtensionMsg_UpdateUserScripts, OnUpdateUserScripts)
83 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI) 73 IPC_MESSAGE_HANDLER(ExtensionMsg_UsingWebRequestAPI, OnUsingWebRequestAPI)
84 IPC_MESSAGE_UNHANDLED(handled = false) 74 IPC_MESSAGE_UNHANDLED(handled = false)
85 IPC_END_MESSAGE_MAP() 75 IPC_END_MESSAGE_MAP()
86 76
87 return handled; 77 return handled;
88 } 78 }
89 79
90 void ExtensionDispatcher::WebKitInitialized() { 80 void ExtensionDispatcher::WebKitInitialized() {
91 // For extensions, we want to ensure we call the IdleHandler every so often,
92 // even if the extension keeps up activity.
93 if (is_extension_process_) {
94 forced_idle_timer_.Start(FROM_HERE,
95 base::TimeDelta::FromMilliseconds(kMaxExtensionIdleHandlerDelayMs),
96 RenderThread::Get(), &RenderThread::IdleHandler);
97 }
98
99 RegisterExtension(new AppBindings(this), false); 81 RegisterExtension(new AppBindings(this), false);
100 RegisterExtension(ChromeWebstoreExtension::Get(), false); 82 RegisterExtension(ChromeWebstoreExtension::Get(), false);
101 83
102 // Add v8 extensions related to chrome extensions. 84 // Add v8 extensions related to chrome extensions.
103 RegisterExtension(new ChromeV8Extension( 85 RegisterExtension(new ChromeV8Extension(
104 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), true); 86 "extensions/json_schema.js", IDR_JSON_SCHEMA_JS, NULL), true);
105 RegisterExtension(EventBindings::Get(this), true); 87 RegisterExtension(EventBindings::Get(this), true);
106 RegisterExtension(new FileBrowserPrivateBindings(), true); 88 RegisterExtension(new FileBrowserPrivateBindings(), true);
107 RegisterExtension(MiscellaneousBindings::Get(this), true); 89 RegisterExtension(MiscellaneousBindings::Get(this), true);
108 RegisterExtension(SchemaGeneratedBindings::Get(this), true); 90 RegisterExtension(SchemaGeneratedBindings::Get(this), true);
109 RegisterExtension(new ChromeV8Extension( 91 RegisterExtension(new ChromeV8Extension(
110 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true); 92 "extensions/apitest.js", IDR_EXTENSION_APITEST_JS, NULL), true);
111 93
112 // Initialize host permissions for any extensions that were activated before 94 // Initialize host permissions for any extensions that were activated before
113 // WebKit was initialized. 95 // WebKit was initialized.
114 for (std::set<std::string>::iterator iter = active_extension_ids_.begin(); 96 for (std::set<std::string>::iterator iter = active_extension_ids_.begin();
115 iter != active_extension_ids_.end(); ++iter) { 97 iter != active_extension_ids_.end(); ++iter) {
116 const Extension* extension = extensions_.GetByID(*iter); 98 const Extension* extension = extensions_.GetByID(*iter);
117 if (extension) 99 if (extension)
118 InitOriginPermissions(extension); 100 InitOriginPermissions(extension);
119 } 101 }
120 102
121 is_webkit_initialized_ = true; 103 is_webkit_initialized_ = true;
122 } 104 }
123 105
124 void ExtensionDispatcher::IdleNotification() {
125 if (is_extension_process_) {
126 // Dampen the forced delay as well if the extension stays idle for long
127 // periods of time.
128 int64 forced_delay_ms = std::max(
129 RenderThread::Get()->GetIdleNotificationDelayInMs(),
130 kMaxExtensionIdleHandlerDelayMs);
131 forced_idle_timer_.Stop();
132 forced_idle_timer_.Start(FROM_HERE,
133 base::TimeDelta::FromMilliseconds(forced_delay_ms),
134 RenderThread::Get(), &RenderThread::IdleHandler);
135 }
136 }
137
138 void ExtensionDispatcher::OnSetFunctionNames( 106 void ExtensionDispatcher::OnSetFunctionNames(
139 const std::vector<std::string>& names) { 107 const std::vector<std::string>& names) {
140 function_names_.clear(); 108 function_names_.clear();
141 for (size_t i = 0; i < names.size(); ++i) 109 for (size_t i = 0; i < names.size(); ++i)
142 function_names_.insert(names[i]); 110 function_names_.insert(names[i]);
143 } 111 }
144 112
145 void ExtensionDispatcher::OnMessageInvoke(const std::string& extension_id, 113 void ExtensionDispatcher::OnMessageInvoke(const std::string& extension_id,
146 const std::string& function_name, 114 const std::string& function_name,
147 const ListValue& args, 115 const ListValue& args,
148 const GURL& event_url) { 116 const GURL& event_url) {
149 v8_context_set_.DispatchChromeHiddenMethod( 117 v8_context_set_.DispatchChromeHiddenMethod(
150 extension_id, function_name, args, NULL, event_url); 118 extension_id, function_name, args, NULL, event_url);
151 119
152 // Reset the idle handler each time there's any activity like event or message 120 // Reset the idle handler each time there's any activity like event or message
153 // dispatch, for which Invoke is the chokepoint. 121 // dispatch, for which Invoke is the chokepoint.
154 if (is_extension_process_) { 122 if (is_extension_process_) {
Aaron Boodman 2011/11/23 22:52:20 Nit: the braces can go away now.
155 RenderThread::Get()->ScheduleIdleHandler( 123 RenderThread::Get()->ResetIdleTimer();
156 kInitialExtensionIdleHandlerDelayMs);
Matt Perry 2011/11/15 23:44:33 The point of this code was to prevent the idle han
157 } 124 }
158 125
159 // Tell the browser process that the event is dispatched and we're idle. 126 // Tell the browser process that the event is dispatched and we're idle.
160 if (CommandLine::ForCurrentProcess()->HasSwitch( 127 if (CommandLine::ForCurrentProcess()->HasSwitch(
161 switches::kEnableLazyBackgroundPages) && 128 switches::kEnableLazyBackgroundPages) &&
162 function_name == "Event.dispatchJSON") { // may always be true 129 function_name == "Event.dispatchJSON") { // may always be true
163 RenderThread::Get()->Send( 130 RenderThread::Get()->Send(
164 new ExtensionHostMsg_ExtensionEventAck(extension_id)); 131 new ExtensionHostMsg_ExtensionEventAck(extension_id));
165 CheckIdleStatus(extension_id); 132 CheckIdleStatus(extension_id);
166 } 133 }
(...skipping 127 matching lines...) Expand 10 before | Expand all | Expand 10 after
294 const std::string& extension_id) { 261 const std::string& extension_id) {
295 active_application_ids_.insert(extension_id); 262 active_application_ids_.insert(extension_id);
296 } 263 }
297 264
298 void ExtensionDispatcher::OnActivateExtension( 265 void ExtensionDispatcher::OnActivateExtension(
299 const std::string& extension_id) { 266 const std::string& extension_id) {
300 active_extension_ids_.insert(extension_id); 267 active_extension_ids_.insert(extension_id);
301 268
302 // This is called when starting a new extension page, so start the idle 269 // This is called when starting a new extension page, so start the idle
303 // handler ticking. 270 // handler ticking.
304 RenderThread::Get()->ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayMs); 271 RenderThread::Get()->ResetIdleTimer();
305 272
306 UpdateActiveExtensions(); 273 UpdateActiveExtensions();
307 274
308 const Extension* extension = extensions_.GetByID(extension_id); 275 const Extension* extension = extensions_.GetByID(extension_id);
309 if (!extension) 276 if (!extension)
310 return; 277 return;
311 278
312 if (is_webkit_initialized_) 279 if (is_webkit_initialized_)
313 InitOriginPermissions(extension); 280 InitOriginPermissions(extension);
314 } 281 }
(...skipping 94 matching lines...) Expand 10 before | Expand all | Expand 10 after
409 376
410 RenderThread::Get()->RegisterExtension(extension); 377 RenderThread::Get()->RegisterExtension(extension);
411 } 378 }
412 379
413 void ExtensionDispatcher::OnUsingWebRequestAPI( 380 void ExtensionDispatcher::OnUsingWebRequestAPI(
414 bool adblock, bool adblock_plus, bool other) { 381 bool adblock, bool adblock_plus, bool other) {
415 webrequest_adblock_ = adblock; 382 webrequest_adblock_ = adblock;
416 webrequest_adblock_plus_ = adblock_plus; 383 webrequest_adblock_plus_ = adblock_plus;
417 webrequest_other_ = other; 384 webrequest_other_ = other;
418 } 385 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698