blob: 60b7391e8fcdddb4f0a0f0e42a07112e555dbcce [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"
initial.commit09911bf2008-07-26 23:55:2914#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1915#include "base/stats_table.h"
[email protected]94f9a0f682009-06-15 18:30:3016#include "base/thread_local.h"
[email protected]f430b5712009-08-21 21:46:3117#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]06533c0b2009-03-05 21:39:1118#include "chrome/common/chrome_switches.h"
[email protected]017022b2009-07-27 23:06:3419#include "chrome/common/db_message_filter.h"
[email protected]e09ba552009-02-05 03:26:2920#include "chrome/common/render_messages.h"
[email protected]9b6f40e2009-06-11 15:54:2621#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4722#include "chrome/common/url_constants.h"
[email protected]8930d472009-02-21 08:05:2823#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5024// TODO(port)
25#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2926#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5027#else
[email protected]2c62b562009-01-27 19:04:5028#include "base/scoped_handle.h"
29#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5030#endif
[email protected]a8624712009-04-17 00:51:3531#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1532#include "chrome/renderer/extension_groups.h"
[email protected]a40caa972009-04-08 18:35:3433#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2734#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1735#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4336#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4237#include "chrome/renderer/external_extension.h"
[email protected]0bc46552009-04-07 21:56:4238#include "chrome/renderer/loadtimes_extension_bindings.h"
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/renderer/net/render_dns_master.h"
40#include "chrome/renderer/render_process.h"
41#include "chrome/renderer/render_view.h"
[email protected]8d86fce2009-02-26 23:37:5542#include "chrome/renderer/renderer_webkitclient_impl.h"
[email protected]0938d3c2009-01-09 20:37:3543#include "chrome/renderer/user_script_slave.h"
[email protected]d55aaa132009-09-28 21:08:0444#include "ipc/ipc_message.h"
[email protected]b9ab10c2009-08-07 18:09:5545#include "webkit/api/public/WebColor.h"
[email protected]afdcf5c2009-05-10 20:30:4146#include "webkit/api/public/WebCache.h"
47#include "webkit/api/public/WebKit.h"
48#include "webkit/api/public/WebString.h"
[email protected]b07f29092009-06-05 07:33:2149#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1150#include "webkit/extensions/v8/gears_extension.h"
51#include "webkit/extensions/v8/interval_extension.h"
52#include "webkit/extensions/v8/playback_extension.h"
[email protected]e94afbb92009-10-01 00:25:4153#include "third_party/tcmalloc/tcmalloc/src/google/malloc_extension.h"
[email protected]2c62b562009-01-27 19:04:5054
[email protected]da00a2882009-03-09 17:51:1955#if defined(OS_WIN)
56#include <windows.h>
57#include <objbase.h>
58#endif
59
[email protected]2c434b32009-03-19 06:27:4760using WebKit::WebCache;
61using WebKit::WebString;
initial.commit09911bf2008-07-26 23:55:2962
[email protected]42f1d7822009-07-23 18:17:5563namespace {
[email protected]2c434b32009-03-19 06:27:4764static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:0365static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
66
[email protected]94f9a0f682009-06-15 18:30:3067static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
68 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:5469
[email protected]5fa1c542009-05-05 20:36:0770#if defined(OS_POSIX)
71class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
72 void OnChannelError() {
73 // On POSIX, at least, one can install an unload handler which loops
74 // forever and leave behind a renderer process which eats 100% CPU forever.
75 //
76 // This is because the terminate signals (ViewMsg_ShouldClose and the error
77 // from the IPC channel) are routed to the main message loop but never
78 // processed (because that message loop is stuck in V8).
79 //
80 // One could make the browser SIGKILL the renderers, but that leaves open a
81 // large window where a browser failure (or a user, manually terminating
82 // the browser because "it's stuck") will leave behind a process eating all
83 // the CPU.
84 //
85 // So, we install a filter on the channel so that we can process this event
86 // here and kill the process.
87 _exit(0);
88 }
89};
90#endif
[email protected]42f1d7822009-07-23 18:17:5591} // namespace
92
93// When we run plugins in process, we actually run them on the render thread,
94// which means that we need to make the render thread pump UI events.
95RenderThread::RenderThread() {
96 Init();
97}
98
99RenderThread::RenderThread(const std::string& channel_name)
100 : ChildThread(channel_name) {
101 Init();
102}
[email protected]5fa1c542009-05-05 20:36:07103
initial.commit09911bf2008-07-26 23:55:29104void RenderThread::Init() {
[email protected]94f9a0f682009-06-15 18:30:30105 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50106#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34107 // If you are running plugins in this thread you need COM active but in
108 // the normal case you don't.
109 if (RenderProcess::InProcessPlugins())
110 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50111#endif
initial.commit09911bf2008-07-26 23:55:29112
[email protected]42f1d7822009-07-23 18:17:55113 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03114 cache_stats_task_pending_ = false;
115 widget_count_ = 0;
116 hidden_widget_count_ = 0;
117 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
118 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55119
[email protected]8d86fce2009-02-26 23:37:55120 visited_link_slave_.reset(new VisitedLinkSlave());
121 user_script_slave_.reset(new UserScriptSlave());
122 dns_master_.reset(new RenderDnsMaster());
123 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31124 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]a8624712009-04-17 00:51:35125 devtools_agent_filter_ = new DevToolsAgentFilter();
126 AddFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34127 db_message_filter_ = new DBMessageFilter();
128 AddFilter(db_message_filter_.get());
[email protected]5fa1c542009-05-05 20:36:07129
130#if defined(OS_POSIX)
131 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
132 AddFilter(suicide_on_channel_error_filter_.get());
133#endif
initial.commit09911bf2008-07-26 23:55:29134}
135
[email protected]42f1d7822009-07-23 18:17:55136RenderThread::~RenderThread() {
[email protected]8d86fce2009-02-26 23:37:55137 // Shutdown in reverse of the initialization order.
[email protected]a8624712009-04-17 00:51:35138 RemoveFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34139 RemoveFilter(db_message_filter_.get());
140 db_message_filter_ = NULL;
[email protected]42f1d7822009-07-23 18:17:55141 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22142 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22143
[email protected]94f9a0f682009-06-15 18:30:30144 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30145
[email protected]8d86fce2009-02-26 23:37:55146 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50147#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29148 // Clean up plugin channels before this thread goes away.
149 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34150 // Don't call COM if the renderer is in the sandbox.
151 if (RenderProcess::InProcessPlugins())
152 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50153#endif
initial.commit09911bf2008-07-26 23:55:29154}
155
[email protected]42f1d7822009-07-23 18:17:55156RenderThread* RenderThread::current() {
157 return lazy_tls.Pointer()->Get();
158}
159
160void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
161 channel()->AddFilter(filter);
162}
163
164void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
165 channel()->RemoveFilter(filter);
166}
167
[email protected]bee16aab2009-08-26 15:55:03168void RenderThread::WidgetHidden() {
169 DCHECK(hidden_widget_count_ < widget_count_);
170 hidden_widget_count_++ ;
171 if (widget_count_ && hidden_widget_count_ == widget_count_) {
172 // Reset the delay.
173 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
174
175 // Schedule the IdleHandler to wakeup in a bit.
176 MessageLoop::current()->PostDelayedTask(FROM_HERE,
177 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
178 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
179 }
180}
181
182void RenderThread::WidgetRestored() {
183 DCHECK(hidden_widget_count_ > 0);
184 hidden_widget_count_--;
185
186 // Note: we may have a timer pending to call the IdleHandler (see the
187 // WidgetHidden() code). But we don't bother to cancel it as it is
188 // benign and won't do anything if the tab is un-hidden when it is
189 // called.
190}
191
[email protected]42f1d7822009-07-23 18:17:55192void RenderThread::Resolve(const char* name, size_t length) {
193 return dns_master_->Resolve(name, length);
194}
195
196void RenderThread::SendHistograms(int sequence_number) {
197 return histogram_snapshots_->SendHistograms(sequence_number);
198}
199
[email protected]176aa482008-11-14 03:25:15200void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20201 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29202 visited_link_slave_->Init(table);
203}
204
[email protected]3e90d4a2009-07-03 17:38:39205void RenderThread::OnAddVisitedLinks(
206 const VisitedLinkSlave::Fingerprints& fingerprints) {
207 for (size_t i = 0; i < fingerprints.size(); ++i)
208 WebView::UpdateVisitedLinkState(fingerprints[i]);
209}
210
211void RenderThread::OnResetVisitedLinks() {
212 WebView::ResetVisitedLinkState();
213}
214
[email protected]0938d3c2009-01-09 20:37:35215void RenderThread::OnUpdateUserScripts(
[email protected]176aa482008-11-14 03:25:15216 base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20217 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]0938d3c2009-01-09 20:37:35218 user_script_slave_->UpdateScripts(scripts);
[email protected]1e0f70402008-10-16 23:57:47219}
220
[email protected]703e807a2009-03-28 19:56:51221void RenderThread::OnSetExtensionFunctionNames(
222 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04223 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51224}
225
[email protected]45776222009-07-15 20:21:58226void RenderThread::OnPageActionsUpdated(
227 const std::string& extension_id,
228 const std::vector<std::string>& page_actions) {
229 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
230}
231
[email protected]cccf90932009-08-23 17:56:25232void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19233 const std::string& extension_id,
234 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25235 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
236}
237
238void RenderThread::OnExtensionSetHostPermissions(
239 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
240 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19241}
242
[email protected]75e126b932009-09-28 19:38:49243void RenderThread::OnExtensionSetL10nMessages(
244 const std::string& extension_id,
245 const std::map<std::string, std::string>& l10n_messages) {
246 ExtensionProcessBindings::SetL10nMessages(extension_id, l10n_messages);
247}
248
[email protected]8930d472009-02-21 08:05:28249void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1edc16b82009-04-07 17:45:54250 // App cache messages are handled by a delegate.
[email protected]f430b5712009-08-21 21:46:31251 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54252 return;
253
[email protected]8930d472009-02-21 08:05:28254 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
255 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39256 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
257 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]8930d472009-02-21 08:05:28258 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55259 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28260 // TODO(port): removed from render_messages_internal.h;
261 // is there a new non-windows message I should add here?
262 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
263 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17264 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46265 OnGetRendererHistograms)
[email protected]8930d472009-02-21 08:05:28266 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
267 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02268 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28269 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28270 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
271 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43272 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
273 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51274 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
275 OnSetExtensionFunctionNames)
[email protected]b547fd42009-04-23 23:16:27276 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
277 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58278 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
279 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25280 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
281 OnExtensionSetAPIPermissions)
282 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
283 OnExtensionSetHostPermissions)
[email protected]75e126b932009-09-28 19:38:49284 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetL10nMessages,
285 OnExtensionSetL10nMessages)
[email protected]d55aaa132009-09-28 21:08:04286#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21287 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04288 OnSetIPCLoggingEnabled)
289#endif
[email protected]8930d472009-02-21 08:05:28290 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29291}
292
293void RenderThread::OnSetNextPageID(int32 next_page_id) {
294 // This should only be called at process initialization time, so we shouldn't
295 // have to worry about thread-safety.
296 RenderView::SetNextPageID(next_page_id);
297}
298
[email protected]b9ab10c2009-08-07 18:09:55299// Called when to register CSS Color name->system color mappings.
300// We update the colors one by one and then tell WebKit to refresh all render
301// views.
302void RenderThread::OnSetCSSColors(
303 const std::vector<CSSColors::CSSColorMapping>& colors) {
304
305 size_t num_colors = colors.size();
306 scoped_array<WebKit::WebColorName> color_names(
307 new WebKit::WebColorName[num_colors]);
308 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
309 size_t i = 0;
310 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
311 colors.begin();
312 it != colors.end();
313 ++it, ++i) {
314 color_names[i] = it->first;
315 web_colors[i] = it->second;
316 }
317 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
318}
319
[email protected]18bcc3c2009-01-27 21:39:15320void RenderThread::OnCreateNewView(gfx::NativeViewId parent_hwnd,
[email protected]80d96fa2009-06-10 22:34:51321 const RendererPreferences& renderer_prefs,
initial.commit09911bf2008-07-26 23:55:29322 const WebPreferences& webkit_prefs,
323 int32 view_id) {
[email protected]90a3fbb12009-02-28 01:13:47324 EnsureWebKitInitialized();
325
[email protected]be645db2009-02-06 20:36:33326 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29327 RenderView::Create(
[email protected]12636df2009-09-28 22:32:21328 this, parent_hwnd, MSG_ROUTING_NONE, renderer_prefs,
[email protected]80d96fa2009-06-10 22:34:51329 webkit_prefs, new SharedRenderViewCounter(0), view_id);
[email protected]7f874dec2009-02-06 01:48:27330}
[email protected]4274e582009-01-27 22:09:56331
initial.commit09911bf2008-07-26 23:55:29332void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
333 size_t max_dead_capacity,
334 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47335 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47336 WebCache::setCapacities(
337 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29338}
339
340void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47341 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47342 WebCache::ResourceTypeStats stats;
343 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29344 Send(new ViewHostMsg_ResourceTypeStats(stats));
345}
346
[email protected]c9a3ef82009-05-28 22:02:46347void RenderThread::OnGetRendererHistograms(int sequence_number) {
348 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17349}
350
initial.commit09911bf2008-07-26 23:55:29351void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47352 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47353 WebCache::UsageStats stats;
354 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29355 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03356 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29357}
358
359void RenderThread::InformHostOfCacheStatsLater() {
360 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03361 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29362 return;
363
[email protected]bee16aab2009-08-26 15:55:03364 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29365 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03366 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29367 &RenderThread::InformHostOfCacheStats),
368 kCacheStatsDelayMS);
369}
[email protected]90a3fbb12009-02-28 01:13:47370
[email protected]b07f29092009-06-05 07:33:21371void RenderThread::CloseIdleConnections() {
372 Send(new ViewHostMsg_CloseIdleConnections());
373}
374
375void RenderThread::SetCacheMode(bool enabled) {
376 Send(new ViewHostMsg_SetCacheMode(enabled));
377}
378
[email protected]3df0c202009-03-31 23:51:26379static void* CreateHistogram(
380 const char *name, int min, int max, size_t buckets) {
[email protected]56a27b22009-05-21 00:44:37381 Histogram* histogram = new Histogram(name, min, max, buckets);
382 if (histogram) {
383 histogram->SetFlags(kUmaTargetedHistogramFlag);
384 }
385 return histogram;
[email protected]3df0c202009-03-31 23:51:26386}
387
388static void AddHistogramSample(void* hist, int sample) {
389 Histogram* histogram = static_cast<Histogram *>(hist);
390 histogram->Add(sample);
391}
392
[email protected]90a3fbb12009-02-28 01:13:47393void RenderThread::EnsureWebKitInitialized() {
394 if (webkit_client_.get())
395 return;
[email protected]da00a2882009-03-09 17:51:19396
397 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26398 v8::V8::SetCreateHistogramFunction(CreateHistogram);
399 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19400
[email protected]90a3fbb12009-02-28 01:13:47401 webkit_client_.reset(new RendererWebKitClientImpl);
402 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44403
[email protected]4ea00e82009-07-30 22:59:34404 WebKit::enableV8SingleThreadMode();
405
[email protected]60e448982009-05-06 04:21:16406 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44407 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16408 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47409 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]8881eca82009-03-12 18:20:44410 WebKit::registerURLSchemeAsLocal(chrome_ui_scheme);
411 WebKit::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19412
[email protected]d53d8492009-07-14 01:51:32413 // print: pages should be not accessible by normal context.
414 WebString print_ui_scheme(ASCIIToUTF16(chrome::kPrintScheme));
415 WebKit::registerURLSchemeAsLocal(print_ui_scheme);
416 WebKit::registerURLSchemeAsNoAccess(print_ui_scheme);
417
[email protected]6cf8b1a22009-05-26 23:36:00418#if defined(OS_WIN)
419 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]06533c0b2009-03-05 21:39:11420 WebKit::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00421#endif
[email protected]06533c0b2009-03-05 21:39:11422 WebKit::registerExtension(extensions_v8::IntervalExtension::Get());
[email protected]c20210e62009-04-03 21:39:26423 WebKit::registerExtension(extensions_v8::LoadTimesExtension::Get());
[email protected]3c8e3702009-05-01 16:27:42424 WebKit::registerExtension(extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27425
[email protected]ad1f9bd2009-07-30 20:23:15426 const WebKit::WebString kExtensionScheme =
427 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
428
429 WebKit::registerExtension(ExtensionProcessBindings::Get(), kExtensionScheme);
430
431 WebKit::registerExtension(BaseJsV8Extension::Get(),
432 EXTENSION_GROUP_CONTENT_SCRIPTS);
433 WebKit::registerExtension(BaseJsV8Extension::Get(), kExtensionScheme);
434 WebKit::registerExtension(JsonSchemaJsV8Extension::Get(),
435 EXTENSION_GROUP_CONTENT_SCRIPTS);
436 WebKit::registerExtension(JsonSchemaJsV8Extension::Get(), kExtensionScheme);
437 WebKit::registerExtension(EventBindings::Get(),
438 EXTENSION_GROUP_CONTENT_SCRIPTS);
439 WebKit::registerExtension(EventBindings::Get(), kExtensionScheme);
440 WebKit::registerExtension(RendererExtensionBindings::Get(),
441 EXTENSION_GROUP_CONTENT_SCRIPTS);
442 WebKit::registerExtension(RendererExtensionBindings::Get(), kExtensionScheme);
[email protected]5c1cea3f2009-08-31 17:15:47443 WebKit::registerExtension(ExtensionApiTestV8Extension::Get(),
444 kExtensionScheme);
445 WebKit::registerExtension(ExtensionApiTestV8Extension::Get(),
446 EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04447
[email protected]06533c0b2009-03-05 21:39:11448 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04449
[email protected]b07f29092009-06-05 07:33:21450 if (command_line.HasSwitch(switches::kEnableBenchmarking))
451 WebKit::registerExtension(extensions_v8::BenchmarkingExtension::Get());
452
[email protected]06533c0b2009-03-05 21:39:11453 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18454 command_line.HasSwitch(switches::kRecordMode) ||
455 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]06533c0b2009-03-05 21:39:11456 WebKit::registerExtension(extensions_v8::PlaybackExtension::Get());
457 }
[email protected]2cb82332009-03-18 17:24:55458
[email protected]b08eebe2009-05-18 22:37:21459 if (RenderProcess::current()->initialized_media_library())
460 WebKit::enableMediaPlayer();
[email protected]90a3fbb12009-02-28 01:13:47461}
[email protected]75e5a872009-04-02 23:56:11462
[email protected]bee16aab2009-08-26 15:55:03463void RenderThread::IdleHandler() {
464 // It is possible that the timer was set while the widgets were idle,
465 // but that they are no longer idle. If so, just return.
466 if (!widget_count_ || hidden_widget_count_ < widget_count_)
467 return;
468
[email protected]05001182009-09-15 23:34:22469#if defined(OS_WIN)
[email protected]e94afbb92009-10-01 00:25:41470 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22471#endif
[email protected]bee16aab2009-08-26 15:55:03472
[email protected]05001182009-09-15 23:34:22473 if (!v8::V8::IsDead()) {
474 LOG(INFO) << "RenderThread calling v8 IdleNotification for " << this;
475 v8::V8::IdleNotification(false);
[email protected]bee16aab2009-08-26 15:55:03476 }
[email protected]05001182009-09-15 23:34:22477
478 // Schedule next invocation.
479 // Dampen the delay using the algorithm:
480 // delay = delay + 1 / (delay + 2)
481 // Using floor(delay) has a dampening effect such as:
482 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
483 // Note that idle_notification_delay_in_s_ would be reset to
484 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
485 idle_notification_delay_in_s_ +=
486 1.0 / (idle_notification_delay_in_s_ + 2.0);
487
488 // Schedule the next timer.
489 MessageLoop::current()->PostDelayedTask(FROM_HERE,
490 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
491 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
[email protected]bee16aab2009-08-26 15:55:03492}
493
[email protected]dfcb62a2009-06-17 19:32:43494void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
495 const ListValue& args) {
[email protected]7120f132009-07-20 21:05:37496 RendererExtensionBindings::Invoke(function_name, args, NULL);
[email protected]b68d5ed2009-04-16 02:41:28497}
[email protected]b547fd42009-04-23 23:16:27498
[email protected]b78e168b2009-09-21 22:05:45499void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]b547fd42009-04-23 23:16:27500 // The call below will cause a GetPlugins call with refresh=true, but at this
501 // point we already know that the browser has refreshed its list, so disable
502 // refresh temporarily to prevent each renderer process causing the list to be
503 // regenerated.
504 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45505 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27506 plugin_refresh_allowed_ = true;
507}