blob: 09b043ab4b08ca1da8115646b7b7f93d48f98457 [file] [log] [blame]
[email protected]45776222009-07-15 20:21:581// Copyright (c) 2009 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
initial.commit09911bf2008-07-26 23:55:295#include "chrome/renderer/render_thread.h"
6
[email protected]da00a2882009-03-09 17:51:197#include <algorithm>
[email protected]75e126b932009-09-28 19:38:498#include <map>
[email protected]da00a2882009-03-09 17:51:199#include <vector>
10
[email protected]06533c0b2009-03-05 21:39:1111#include "base/command_line.h"
[email protected]94f9a0f682009-06-15 18:30:3012#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0313#include "base/logging.h"
[email protected]b7c7bcf2009-10-03 07:07:3414#include "base/nullable_string16.h"
initial.commit09911bf2008-07-26 23:55:2915#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1916#include "base/stats_table.h"
[email protected]b7c7bcf2009-10-03 07:07:3417#include "base/string_util.h"
[email protected]94f9a0f682009-06-15 18:30:3018#include "base/thread_local.h"
[email protected]f430b5712009-08-21 21:46:3119#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]06533c0b2009-03-05 21:39:1120#include "chrome/common/chrome_switches.h"
[email protected]017022b2009-07-27 23:06:3421#include "chrome/common/db_message_filter.h"
[email protected]e09ba552009-02-05 03:26:2922#include "chrome/common/render_messages.h"
[email protected]9b6f40e2009-06-11 15:54:2623#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4724#include "chrome/common/url_constants.h"
[email protected]8930d472009-02-21 08:05:2825#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5026// TODO(port)
27#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2928#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5029#else
[email protected]2c62b562009-01-27 19:04:5030#include "base/scoped_handle.h"
31#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5032#endif
[email protected]a8624712009-04-17 00:51:3533#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1534#include "chrome/renderer/extension_groups.h"
[email protected]a40caa972009-04-08 18:35:3435#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2736#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1737#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4338#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4239#include "chrome/renderer/external_extension.h"
[email protected]0bc46552009-04-07 21:56:4240#include "chrome/renderer/loadtimes_extension_bindings.h"
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/renderer/net/render_dns_master.h"
42#include "chrome/renderer/render_process.h"
43#include "chrome/renderer/render_view.h"
[email protected]8d86fce2009-02-26 23:37:5544#include "chrome/renderer/renderer_webkitclient_impl.h"
[email protected]0938d3c2009-01-09 20:37:3545#include "chrome/renderer/user_script_slave.h"
[email protected]d55aaa132009-09-28 21:08:0446#include "ipc/ipc_message.h"
[email protected]afdcf5c2009-05-10 20:30:4147#include "webkit/api/public/WebCache.h"
[email protected]f5e4b9bf2009-10-08 00:59:5948#include "webkit/api/public/WebColor.h"
[email protected]afdcf5c2009-05-10 20:30:4149#include "webkit/api/public/WebKit.h"
[email protected]b7c7bcf2009-10-03 07:07:3450#include "webkit/api/public/WebStorageEventDispatcher.h"
[email protected]afdcf5c2009-05-10 20:30:4151#include "webkit/api/public/WebString.h"
[email protected]b07f29092009-06-05 07:33:2152#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1153#include "webkit/extensions/v8/gears_extension.h"
54#include "webkit/extensions/v8/interval_extension.h"
55#include "webkit/extensions/v8/playback_extension.h"
[email protected]e94afbb92009-10-01 00:25:4156#include "third_party/tcmalloc/tcmalloc/src/google/malloc_extension.h"
[email protected]2c62b562009-01-27 19:04:5057
[email protected]da00a2882009-03-09 17:51:1958#if defined(OS_WIN)
59#include <windows.h>
60#include <objbase.h>
61#endif
62
[email protected]2c434b32009-03-19 06:27:4763using WebKit::WebCache;
64using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:3465using WebKit::WebStorageEventDispatcher;
initial.commit09911bf2008-07-26 23:55:2966
[email protected]42f1d7822009-07-23 18:17:5567namespace {
[email protected]2c434b32009-03-19 06:27:4768static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:0369static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
70
[email protected]94f9a0f682009-06-15 18:30:3071static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
72 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:5473
[email protected]5fa1c542009-05-05 20:36:0774#if defined(OS_POSIX)
75class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
76 void OnChannelError() {
77 // On POSIX, at least, one can install an unload handler which loops
78 // forever and leave behind a renderer process which eats 100% CPU forever.
79 //
80 // This is because the terminate signals (ViewMsg_ShouldClose and the error
81 // from the IPC channel) are routed to the main message loop but never
82 // processed (because that message loop is stuck in V8).
83 //
84 // One could make the browser SIGKILL the renderers, but that leaves open a
85 // large window where a browser failure (or a user, manually terminating
86 // the browser because "it's stuck") will leave behind a process eating all
87 // the CPU.
88 //
89 // So, we install a filter on the channel so that we can process this event
90 // here and kill the process.
91 _exit(0);
92 }
93};
94#endif
[email protected]42f1d7822009-07-23 18:17:5595} // namespace
96
97// When we run plugins in process, we actually run them on the render thread,
98// which means that we need to make the render thread pump UI events.
99RenderThread::RenderThread() {
100 Init();
101}
102
103RenderThread::RenderThread(const std::string& channel_name)
104 : ChildThread(channel_name) {
105 Init();
106}
[email protected]5fa1c542009-05-05 20:36:07107
initial.commit09911bf2008-07-26 23:55:29108void RenderThread::Init() {
[email protected]94f9a0f682009-06-15 18:30:30109 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50110#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34111 // If you are running plugins in this thread you need COM active but in
112 // the normal case you don't.
113 if (RenderProcess::InProcessPlugins())
114 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50115#endif
initial.commit09911bf2008-07-26 23:55:29116
[email protected]42f1d7822009-07-23 18:17:55117 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03118 cache_stats_task_pending_ = false;
119 widget_count_ = 0;
120 hidden_widget_count_ = 0;
121 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
122 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55123
[email protected]8d86fce2009-02-26 23:37:55124 visited_link_slave_.reset(new VisitedLinkSlave());
125 user_script_slave_.reset(new UserScriptSlave());
126 dns_master_.reset(new RenderDnsMaster());
127 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31128 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]a8624712009-04-17 00:51:35129 devtools_agent_filter_ = new DevToolsAgentFilter();
130 AddFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34131 db_message_filter_ = new DBMessageFilter();
132 AddFilter(db_message_filter_.get());
[email protected]5fa1c542009-05-05 20:36:07133
134#if defined(OS_POSIX)
135 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
136 AddFilter(suicide_on_channel_error_filter_.get());
137#endif
initial.commit09911bf2008-07-26 23:55:29138}
139
[email protected]42f1d7822009-07-23 18:17:55140RenderThread::~RenderThread() {
[email protected]8d86fce2009-02-26 23:37:55141 // Shutdown in reverse of the initialization order.
[email protected]a8624712009-04-17 00:51:35142 RemoveFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34143 RemoveFilter(db_message_filter_.get());
144 db_message_filter_ = NULL;
[email protected]42f1d7822009-07-23 18:17:55145 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22146 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22147
[email protected]94f9a0f682009-06-15 18:30:30148 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30149
[email protected]8d86fce2009-02-26 23:37:55150 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50151#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29152 // Clean up plugin channels before this thread goes away.
153 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34154 // Don't call COM if the renderer is in the sandbox.
155 if (RenderProcess::InProcessPlugins())
156 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50157#endif
initial.commit09911bf2008-07-26 23:55:29158}
159
[email protected]42f1d7822009-07-23 18:17:55160RenderThread* RenderThread::current() {
161 return lazy_tls.Pointer()->Get();
162}
163
164void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
165 channel()->AddFilter(filter);
166}
167
168void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
169 channel()->RemoveFilter(filter);
170}
171
[email protected]bee16aab2009-08-26 15:55:03172void RenderThread::WidgetHidden() {
173 DCHECK(hidden_widget_count_ < widget_count_);
174 hidden_widget_count_++ ;
175 if (widget_count_ && hidden_widget_count_ == widget_count_) {
176 // Reset the delay.
177 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
178
179 // Schedule the IdleHandler to wakeup in a bit.
180 MessageLoop::current()->PostDelayedTask(FROM_HERE,
181 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
182 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
183 }
184}
185
186void RenderThread::WidgetRestored() {
187 DCHECK(hidden_widget_count_ > 0);
188 hidden_widget_count_--;
189
190 // Note: we may have a timer pending to call the IdleHandler (see the
191 // WidgetHidden() code). But we don't bother to cancel it as it is
192 // benign and won't do anything if the tab is un-hidden when it is
193 // called.
194}
195
[email protected]42f1d7822009-07-23 18:17:55196void RenderThread::Resolve(const char* name, size_t length) {
197 return dns_master_->Resolve(name, length);
198}
199
200void RenderThread::SendHistograms(int sequence_number) {
201 return histogram_snapshots_->SendHistograms(sequence_number);
202}
203
[email protected]176aa482008-11-14 03:25:15204void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20205 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29206 visited_link_slave_->Init(table);
207}
208
[email protected]3e90d4a2009-07-03 17:38:39209void RenderThread::OnAddVisitedLinks(
210 const VisitedLinkSlave::Fingerprints& fingerprints) {
211 for (size_t i = 0; i < fingerprints.size(); ++i)
212 WebView::UpdateVisitedLinkState(fingerprints[i]);
213}
214
215void RenderThread::OnResetVisitedLinks() {
216 WebView::ResetVisitedLinkState();
217}
218
[email protected]0938d3c2009-01-09 20:37:35219void RenderThread::OnUpdateUserScripts(
[email protected]176aa482008-11-14 03:25:15220 base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20221 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]0938d3c2009-01-09 20:37:35222 user_script_slave_->UpdateScripts(scripts);
[email protected]1e0f70402008-10-16 23:57:47223}
224
[email protected]703e807a2009-03-28 19:56:51225void RenderThread::OnSetExtensionFunctionNames(
226 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04227 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51228}
229
[email protected]45776222009-07-15 20:21:58230void RenderThread::OnPageActionsUpdated(
231 const std::string& extension_id,
232 const std::vector<std::string>& page_actions) {
233 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
234}
235
[email protected]cccf90932009-08-23 17:56:25236void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19237 const std::string& extension_id,
238 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25239 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
240}
241
242void RenderThread::OnExtensionSetHostPermissions(
243 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
244 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19245}
246
[email protected]b7c7bcf2009-10-03 07:07:34247void RenderThread::OnDOMStorageEvent(const string16& key,
248 const NullableString16& old_value, const NullableString16& new_value,
249 const string16& origin, DOMStorageType dom_storage_type) {
250 if (!dom_storage_event_dispatcher_.get()) {
251 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
252 }
253 dom_storage_event_dispatcher_->dispatchStorageEvent(key, old_value, new_value,
254 origin, dom_storage_type == DOM_STORAGE_LOCAL);
255}
256
[email protected]75e126b932009-09-28 19:38:49257void RenderThread::OnExtensionSetL10nMessages(
258 const std::string& extension_id,
259 const std::map<std::string, std::string>& l10n_messages) {
260 ExtensionProcessBindings::SetL10nMessages(extension_id, l10n_messages);
261}
262
[email protected]8930d472009-02-21 08:05:28263void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1edc16b82009-04-07 17:45:54264 // App cache messages are handled by a delegate.
[email protected]f430b5712009-08-21 21:46:31265 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54266 return;
267
[email protected]8930d472009-02-21 08:05:28268 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
269 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39270 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
271 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]8930d472009-02-21 08:05:28272 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55273 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28274 // TODO(port): removed from render_messages_internal.h;
275 // is there a new non-windows message I should add here?
276 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
277 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17278 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46279 OnGetRendererHistograms)
[email protected]8930d472009-02-21 08:05:28280 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
281 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02282 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28283 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28284 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
285 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43286 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
287 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51288 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
289 OnSetExtensionFunctionNames)
[email protected]b547fd42009-04-23 23:16:27290 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
291 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58292 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
293 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25294 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
295 OnExtensionSetAPIPermissions)
296 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
297 OnExtensionSetHostPermissions)
[email protected]b7c7bcf2009-10-03 07:07:34298 IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent,
299 OnDOMStorageEvent)
[email protected]75e126b932009-09-28 19:38:49300 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetL10nMessages,
301 OnExtensionSetL10nMessages)
[email protected]d55aaa132009-09-28 21:08:04302#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21303 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04304 OnSetIPCLoggingEnabled)
305#endif
[email protected]8930d472009-02-21 08:05:28306 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29307}
308
309void RenderThread::OnSetNextPageID(int32 next_page_id) {
310 // This should only be called at process initialization time, so we shouldn't
311 // have to worry about thread-safety.
312 RenderView::SetNextPageID(next_page_id);
313}
314
[email protected]b9ab10c2009-08-07 18:09:55315// Called when to register CSS Color name->system color mappings.
316// We update the colors one by one and then tell WebKit to refresh all render
317// views.
318void RenderThread::OnSetCSSColors(
319 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59320 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55321 size_t num_colors = colors.size();
322 scoped_array<WebKit::WebColorName> color_names(
323 new WebKit::WebColorName[num_colors]);
324 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
325 size_t i = 0;
326 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
327 colors.begin();
328 it != colors.end();
329 ++it, ++i) {
330 color_names[i] = it->first;
331 web_colors[i] = it->second;
332 }
333 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
334}
335
[email protected]18bcc3c2009-01-27 21:39:15336void RenderThread::OnCreateNewView(gfx::NativeViewId parent_hwnd,
[email protected]80d96fa2009-06-10 22:34:51337 const RendererPreferences& renderer_prefs,
initial.commit09911bf2008-07-26 23:55:29338 const WebPreferences& webkit_prefs,
339 int32 view_id) {
[email protected]90a3fbb12009-02-28 01:13:47340 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33341 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29342 RenderView::Create(
[email protected]12636df2009-09-28 22:32:21343 this, parent_hwnd, MSG_ROUTING_NONE, renderer_prefs,
[email protected]80d96fa2009-06-10 22:34:51344 webkit_prefs, new SharedRenderViewCounter(0), view_id);
[email protected]7f874dec2009-02-06 01:48:27345}
[email protected]4274e582009-01-27 22:09:56346
initial.commit09911bf2008-07-26 23:55:29347void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
348 size_t max_dead_capacity,
349 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47350 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47351 WebCache::setCapacities(
352 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29353}
354
355void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47356 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47357 WebCache::ResourceTypeStats stats;
358 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29359 Send(new ViewHostMsg_ResourceTypeStats(stats));
360}
361
[email protected]c9a3ef82009-05-28 22:02:46362void RenderThread::OnGetRendererHistograms(int sequence_number) {
363 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17364}
365
initial.commit09911bf2008-07-26 23:55:29366void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47367 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47368 WebCache::UsageStats stats;
369 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29370 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03371 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29372}
373
374void RenderThread::InformHostOfCacheStatsLater() {
375 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03376 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29377 return;
378
[email protected]bee16aab2009-08-26 15:55:03379 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29380 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03381 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29382 &RenderThread::InformHostOfCacheStats),
383 kCacheStatsDelayMS);
384}
[email protected]90a3fbb12009-02-28 01:13:47385
[email protected]b07f29092009-06-05 07:33:21386void RenderThread::CloseIdleConnections() {
387 Send(new ViewHostMsg_CloseIdleConnections());
388}
389
390void RenderThread::SetCacheMode(bool enabled) {
391 Send(new ViewHostMsg_SetCacheMode(enabled));
392}
393
[email protected]3df0c202009-03-31 23:51:26394static void* CreateHistogram(
395 const char *name, int min, int max, size_t buckets) {
[email protected]56a27b22009-05-21 00:44:37396 Histogram* histogram = new Histogram(name, min, max, buckets);
397 if (histogram) {
398 histogram->SetFlags(kUmaTargetedHistogramFlag);
399 }
400 return histogram;
[email protected]3df0c202009-03-31 23:51:26401}
402
403static void AddHistogramSample(void* hist, int sample) {
404 Histogram* histogram = static_cast<Histogram *>(hist);
405 histogram->Add(sample);
406}
407
[email protected]90a3fbb12009-02-28 01:13:47408void RenderThread::EnsureWebKitInitialized() {
409 if (webkit_client_.get())
410 return;
[email protected]da00a2882009-03-09 17:51:19411
412 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26413 v8::V8::SetCreateHistogramFunction(CreateHistogram);
414 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19415
[email protected]90a3fbb12009-02-28 01:13:47416 webkit_client_.reset(new RendererWebKitClientImpl);
417 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44418
[email protected]4ea00e82009-07-30 22:59:34419 WebKit::enableV8SingleThreadMode();
420
[email protected]60e448982009-05-06 04:21:16421 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44422 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16423 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47424 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]8881eca82009-03-12 18:20:44425 WebKit::registerURLSchemeAsLocal(chrome_ui_scheme);
426 WebKit::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19427
[email protected]d53d8492009-07-14 01:51:32428 // print: pages should be not accessible by normal context.
429 WebString print_ui_scheme(ASCIIToUTF16(chrome::kPrintScheme));
430 WebKit::registerURLSchemeAsLocal(print_ui_scheme);
431 WebKit::registerURLSchemeAsNoAccess(print_ui_scheme);
432
[email protected]6cf8b1a22009-05-26 23:36:00433#if defined(OS_WIN)
434 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]06533c0b2009-03-05 21:39:11435 WebKit::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00436#endif
[email protected]06533c0b2009-03-05 21:39:11437 WebKit::registerExtension(extensions_v8::IntervalExtension::Get());
[email protected]c20210e62009-04-03 21:39:26438 WebKit::registerExtension(extensions_v8::LoadTimesExtension::Get());
[email protected]3c8e3702009-05-01 16:27:42439 WebKit::registerExtension(extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27440
[email protected]ad1f9bd2009-07-30 20:23:15441 const WebKit::WebString kExtensionScheme =
442 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
443
444 WebKit::registerExtension(ExtensionProcessBindings::Get(), kExtensionScheme);
445
446 WebKit::registerExtension(BaseJsV8Extension::Get(),
447 EXTENSION_GROUP_CONTENT_SCRIPTS);
448 WebKit::registerExtension(BaseJsV8Extension::Get(), kExtensionScheme);
449 WebKit::registerExtension(JsonSchemaJsV8Extension::Get(),
450 EXTENSION_GROUP_CONTENT_SCRIPTS);
451 WebKit::registerExtension(JsonSchemaJsV8Extension::Get(), kExtensionScheme);
452 WebKit::registerExtension(EventBindings::Get(),
453 EXTENSION_GROUP_CONTENT_SCRIPTS);
454 WebKit::registerExtension(EventBindings::Get(), kExtensionScheme);
455 WebKit::registerExtension(RendererExtensionBindings::Get(),
456 EXTENSION_GROUP_CONTENT_SCRIPTS);
457 WebKit::registerExtension(RendererExtensionBindings::Get(), kExtensionScheme);
[email protected]5c1cea3f2009-08-31 17:15:47458 WebKit::registerExtension(ExtensionApiTestV8Extension::Get(),
459 kExtensionScheme);
460 WebKit::registerExtension(ExtensionApiTestV8Extension::Get(),
461 EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04462
[email protected]06533c0b2009-03-05 21:39:11463 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04464
[email protected]b07f29092009-06-05 07:33:21465 if (command_line.HasSwitch(switches::kEnableBenchmarking))
466 WebKit::registerExtension(extensions_v8::BenchmarkingExtension::Get());
467
[email protected]06533c0b2009-03-05 21:39:11468 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18469 command_line.HasSwitch(switches::kRecordMode) ||
470 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]06533c0b2009-03-05 21:39:11471 WebKit::registerExtension(extensions_v8::PlaybackExtension::Get());
472 }
[email protected]2cb82332009-03-18 17:24:55473
[email protected]b08eebe2009-05-18 22:37:21474 if (RenderProcess::current()->initialized_media_library())
475 WebKit::enableMediaPlayer();
[email protected]90a3fbb12009-02-28 01:13:47476}
[email protected]75e5a872009-04-02 23:56:11477
[email protected]bee16aab2009-08-26 15:55:03478void RenderThread::IdleHandler() {
479 // It is possible that the timer was set while the widgets were idle,
480 // but that they are no longer idle. If so, just return.
481 if (!widget_count_ || hidden_widget_count_ < widget_count_)
482 return;
483
[email protected]05001182009-09-15 23:34:22484#if defined(OS_WIN)
[email protected]e94afbb92009-10-01 00:25:41485 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22486#endif
[email protected]bee16aab2009-08-26 15:55:03487
[email protected]f5e4b9bf2009-10-08 00:59:59488 LOG(INFO) << "RenderThread calling v8 IdleNotification for " << this;
489 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22490
491 // Schedule next invocation.
492 // Dampen the delay using the algorithm:
493 // delay = delay + 1 / (delay + 2)
494 // Using floor(delay) has a dampening effect such as:
495 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
496 // Note that idle_notification_delay_in_s_ would be reset to
497 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
498 idle_notification_delay_in_s_ +=
499 1.0 / (idle_notification_delay_in_s_ + 2.0);
500
501 // Schedule the next timer.
502 MessageLoop::current()->PostDelayedTask(FROM_HERE,
503 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
504 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
[email protected]bee16aab2009-08-26 15:55:03505}
506
[email protected]dfcb62a2009-06-17 19:32:43507void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
508 const ListValue& args) {
[email protected]7120f132009-07-20 21:05:37509 RendererExtensionBindings::Invoke(function_name, args, NULL);
[email protected]b68d5ed2009-04-16 02:41:28510}
[email protected]b547fd42009-04-23 23:16:27511
[email protected]b78e168b2009-09-21 22:05:45512void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59513 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27514 // The call below will cause a GetPlugins call with refresh=true, but at this
515 // point we already know that the browser has refreshed its list, so disable
516 // refresh temporarily to prevent each renderer process causing the list to be
517 // regenerated.
518 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45519 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27520 plugin_refresh_allowed_ = true;
521}