blob: a388929e18659adfb4e8677b7fd0aa5ec759a95f [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]38b48a82009-11-11 01:51:327#include <v8.h>
8
[email protected]da00a2882009-03-09 17:51:199#include <algorithm>
[email protected]75e126b932009-09-28 19:38:4910#include <map>
[email protected]da00a2882009-03-09 17:51:1911#include <vector>
12
[email protected]adf00bc2009-11-02 18:35:0013#if defined(USE_SYSTEM_SQLITE)
14#include <sqlite3.h>
15#else
16#include "third_party/sqlite/preprocessed/sqlite3.h"
17#endif
18
[email protected]06533c0b2009-03-05 21:39:1119#include "base/command_line.h"
[email protected]94f9a0f682009-06-15 18:30:3020#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0321#include "base/logging.h"
[email protected]b7c7bcf2009-10-03 07:07:3422#include "base/nullable_string16.h"
[email protected]d41041092009-10-08 06:56:5723#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2924#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1925#include "base/stats_table.h"
[email protected]b7c7bcf2009-10-03 07:07:3426#include "base/string_util.h"
[email protected]94f9a0f682009-06-15 18:30:3027#include "base/thread_local.h"
[email protected]f430b5712009-08-21 21:46:3128#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]06533c0b2009-03-05 21:39:1129#include "chrome/common/chrome_switches.h"
[email protected]017022b2009-07-27 23:06:3430#include "chrome/common/db_message_filter.h"
[email protected]e09ba552009-02-05 03:26:2931#include "chrome/common/render_messages.h"
[email protected]9b6f40e2009-06-11 15:54:2632#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4733#include "chrome/common/url_constants.h"
[email protected]8930d472009-02-21 08:05:2834#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5035// TODO(port)
36#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2937#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5038#else
[email protected]2c62b562009-01-27 19:04:5039#include "base/scoped_handle.h"
40#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5041#endif
[email protected]a8624712009-04-17 00:51:3542#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1543#include "chrome/renderer/extension_groups.h"
[email protected]a40caa972009-04-08 18:35:3444#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2745#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1746#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4347#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4248#include "chrome/renderer/external_extension.h"
[email protected]0bc46552009-04-07 21:56:4249#include "chrome/renderer/loadtimes_extension_bindings.h"
initial.commit09911bf2008-07-26 23:55:2950#include "chrome/renderer/net/render_dns_master.h"
51#include "chrome/renderer/render_process.h"
52#include "chrome/renderer/render_view.h"
[email protected]8d86fce2009-02-26 23:37:5553#include "chrome/renderer/renderer_webkitclient_impl.h"
[email protected]e2b2d4a2009-10-24 03:32:5954#include "chrome/renderer/renderer_web_database_observer.h"
[email protected]327344ef2009-10-29 06:18:1155#include "chrome/renderer/socket_stream_dispatcher.h"
[email protected]85c55dc2009-11-06 03:05:4656#if defined(SPELLCHECKER_IN_RENDERER)
57#include "chrome/renderer/spellchecker/spellcheck.h"
58#endif
[email protected]0938d3c2009-01-09 20:37:3559#include "chrome/renderer/user_script_slave.h"
[email protected]d55aaa132009-09-28 21:08:0460#include "ipc/ipc_message.h"
[email protected]adf00bc2009-11-02 18:35:0061#include "third_party/tcmalloc/tcmalloc/src/google/malloc_extension.h"
[email protected]afdcf5c2009-05-10 20:30:4162#include "webkit/api/public/WebCache.h"
[email protected]f5e4b9bf2009-10-08 00:59:5963#include "webkit/api/public/WebColor.h"
[email protected]fede6ca12009-10-08 18:24:2664#include "webkit/api/public/WebCrossOriginPreflightResultCache.h"
[email protected]e2b2d4a2009-10-24 03:32:5965#include "webkit/api/public/WebDatabase.h"
[email protected]adf00bc2009-11-02 18:35:0066#include "webkit/api/public/WebFontCache.h"
[email protected]afdcf5c2009-05-10 20:30:4167#include "webkit/api/public/WebKit.h"
[email protected]adf00bc2009-11-02 18:35:0068#include "webkit/api/public/WebRuntimeFeatures.h"
[email protected]09cdd4f22009-10-16 04:01:4669#include "webkit/api/public/WebScriptController.h"
[email protected]204758c2009-10-22 03:56:3070#include "webkit/api/public/WebSecurityPolicy.h"
[email protected]b7c7bcf2009-10-03 07:07:3471#include "webkit/api/public/WebStorageEventDispatcher.h"
[email protected]afdcf5c2009-05-10 20:30:4172#include "webkit/api/public/WebString.h"
[email protected]b07f29092009-06-05 07:33:2173#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1174#include "webkit/extensions/v8/gears_extension.h"
75#include "webkit/extensions/v8/interval_extension.h"
76#include "webkit/extensions/v8/playback_extension.h"
[email protected]2c62b562009-01-27 19:04:5077
[email protected]da00a2882009-03-09 17:51:1978#if defined(OS_WIN)
79#include <windows.h>
80#include <objbase.h>
81#endif
82
[email protected]2c434b32009-03-19 06:27:4783using WebKit::WebCache;
[email protected]fede6ca12009-10-08 18:24:2684using WebKit::WebCrossOriginPreflightResultCache;
85using WebKit::WebFontCache;
[email protected]adf00bc2009-11-02 18:35:0086using WebKit::WebRuntimeFeatures;
[email protected]204758c2009-10-22 03:56:3087using WebKit::WebSecurityPolicy;
[email protected]98d7127b2009-10-23 18:26:5188using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:4789using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:3490using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:0391using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:2992
[email protected]42f1d7822009-07-23 18:17:5593namespace {
[email protected]2c434b32009-03-19 06:27:4794static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:0395static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
96
[email protected]94f9a0f682009-06-15 18:30:3097static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
98 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:5499
[email protected]5fa1c542009-05-05 20:36:07100#if defined(OS_POSIX)
101class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
102 void OnChannelError() {
103 // On POSIX, at least, one can install an unload handler which loops
104 // forever and leave behind a renderer process which eats 100% CPU forever.
105 //
106 // This is because the terminate signals (ViewMsg_ShouldClose and the error
107 // from the IPC channel) are routed to the main message loop but never
108 // processed (because that message loop is stuck in V8).
109 //
110 // One could make the browser SIGKILL the renderers, but that leaves open a
111 // large window where a browser failure (or a user, manually terminating
112 // the browser because "it's stuck") will leave behind a process eating all
113 // the CPU.
114 //
115 // So, we install a filter on the channel so that we can process this event
116 // here and kill the process.
117 _exit(0);
118 }
119};
120#endif
[email protected]42f1d7822009-07-23 18:17:55121} // namespace
122
123// When we run plugins in process, we actually run them on the render thread,
124// which means that we need to make the render thread pump UI events.
125RenderThread::RenderThread() {
126 Init();
127}
128
129RenderThread::RenderThread(const std::string& channel_name)
130 : ChildThread(channel_name) {
131 Init();
132}
[email protected]5fa1c542009-05-05 20:36:07133
initial.commit09911bf2008-07-26 23:55:29134void RenderThread::Init() {
[email protected]94f9a0f682009-06-15 18:30:30135 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50136#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34137 // If you are running plugins in this thread you need COM active but in
138 // the normal case you don't.
139 if (RenderProcess::InProcessPlugins())
140 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50141#endif
initial.commit09911bf2008-07-26 23:55:29142
[email protected]42f1d7822009-07-23 18:17:55143 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03144 cache_stats_task_pending_ = false;
145 widget_count_ = 0;
146 hidden_widget_count_ = 0;
147 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
148 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55149
[email protected]8d86fce2009-02-26 23:37:55150 visited_link_slave_.reset(new VisitedLinkSlave());
151 user_script_slave_.reset(new UserScriptSlave());
152 dns_master_.reset(new RenderDnsMaster());
153 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31154 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]327344ef2009-10-29 06:18:11155 socket_stream_dispatcher_.reset(new SocketStreamDispatcher());
[email protected]a8624712009-04-17 00:51:35156 devtools_agent_filter_ = new DevToolsAgentFilter();
157 AddFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34158 db_message_filter_ = new DBMessageFilter();
159 AddFilter(db_message_filter_.get());
[email protected]85c55dc2009-11-06 03:05:46160#if defined(SPELLCHECKER_IN_RENDERER)
161 spellchecker_.reset(new SpellCheck());
162#endif
[email protected]5fa1c542009-05-05 20:36:07163
164#if defined(OS_POSIX)
165 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
166 AddFilter(suicide_on_channel_error_filter_.get());
167#endif
initial.commit09911bf2008-07-26 23:55:29168}
169
[email protected]42f1d7822009-07-23 18:17:55170RenderThread::~RenderThread() {
[email protected]8d86fce2009-02-26 23:37:55171 // Shutdown in reverse of the initialization order.
[email protected]a8624712009-04-17 00:51:35172 RemoveFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34173 RemoveFilter(db_message_filter_.get());
174 db_message_filter_ = NULL;
[email protected]42f1d7822009-07-23 18:17:55175 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22176 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22177
[email protected]94f9a0f682009-06-15 18:30:30178 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30179
[email protected]8d86fce2009-02-26 23:37:55180 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50181#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29182 // Clean up plugin channels before this thread goes away.
183 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34184 // Don't call COM if the renderer is in the sandbox.
185 if (RenderProcess::InProcessPlugins())
186 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50187#endif
initial.commit09911bf2008-07-26 23:55:29188}
189
[email protected]42f1d7822009-07-23 18:17:55190RenderThread* RenderThread::current() {
191 return lazy_tls.Pointer()->Get();
192}
193
194void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
195 channel()->AddFilter(filter);
196}
197
198void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
199 channel()->RemoveFilter(filter);
200}
201
[email protected]bee16aab2009-08-26 15:55:03202void RenderThread::WidgetHidden() {
203 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50204 hidden_widget_count_++;
[email protected]bee16aab2009-08-26 15:55:03205 if (widget_count_ && hidden_widget_count_ == widget_count_) {
206 // Reset the delay.
207 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
208
209 // Schedule the IdleHandler to wakeup in a bit.
210 MessageLoop::current()->PostDelayedTask(FROM_HERE,
211 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
212 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
213 }
214}
215
216void RenderThread::WidgetRestored() {
217 DCHECK(hidden_widget_count_ > 0);
218 hidden_widget_count_--;
219
220 // Note: we may have a timer pending to call the IdleHandler (see the
221 // WidgetHidden() code). But we don't bother to cancel it as it is
222 // benign and won't do anything if the tab is un-hidden when it is
223 // called.
224}
225
[email protected]42f1d7822009-07-23 18:17:55226void RenderThread::Resolve(const char* name, size_t length) {
227 return dns_master_->Resolve(name, length);
228}
229
230void RenderThread::SendHistograms(int sequence_number) {
231 return histogram_snapshots_->SendHistograms(sequence_number);
232}
233
[email protected]176aa482008-11-14 03:25:15234void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20235 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29236 visited_link_slave_->Init(table);
237}
238
[email protected]3e90d4a2009-07-03 17:38:39239void RenderThread::OnAddVisitedLinks(
240 const VisitedLinkSlave::Fingerprints& fingerprints) {
241 for (size_t i = 0; i < fingerprints.size(); ++i)
[email protected]50ae00ef2009-10-19 05:11:03242 WebView::updateVisitedLinkState(fingerprints[i]);
[email protected]3e90d4a2009-07-03 17:38:39243}
244
245void RenderThread::OnResetVisitedLinks() {
[email protected]50ae00ef2009-10-19 05:11:03246 WebView::resetVisitedLinkState();
[email protected]3e90d4a2009-07-03 17:38:39247}
248
[email protected]0938d3c2009-01-09 20:37:35249void RenderThread::OnUpdateUserScripts(
[email protected]176aa482008-11-14 03:25:15250 base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20251 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]0938d3c2009-01-09 20:37:35252 user_script_slave_->UpdateScripts(scripts);
[email protected]1e0f70402008-10-16 23:57:47253}
254
[email protected]703e807a2009-03-28 19:56:51255void RenderThread::OnSetExtensionFunctionNames(
256 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04257 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51258}
259
[email protected]45776222009-07-15 20:21:58260void RenderThread::OnPageActionsUpdated(
261 const std::string& extension_id,
262 const std::vector<std::string>& page_actions) {
263 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
264}
265
[email protected]cccf90932009-08-23 17:56:25266void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19267 const std::string& extension_id,
268 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25269 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
270}
271
272void RenderThread::OnExtensionSetHostPermissions(
273 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
274 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19275}
276
[email protected]c61cc652009-11-04 05:44:40277void RenderThread::OnDOMStorageEvent(
278 const ViewMsg_DOMStorageEvent_Params& params) {
279 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34280 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]c61cc652009-11-04 05:44:40281 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key_,
[email protected]ee2be5b32009-11-05 09:13:12282 params.old_value_, params.new_value_, params.origin_, params.url_,
[email protected]c61cc652009-11-04 05:44:40283 params.storage_type_ == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34284}
285
[email protected]75e126b932009-09-28 19:38:49286void RenderThread::OnExtensionSetL10nMessages(
287 const std::string& extension_id,
288 const std::map<std::string, std::string>& l10n_messages) {
289 ExtensionProcessBindings::SetL10nMessages(extension_id, l10n_messages);
290}
291
[email protected]8930d472009-02-21 08:05:28292void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1edc16b82009-04-07 17:45:54293 // App cache messages are handled by a delegate.
[email protected]f430b5712009-08-21 21:46:31294 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54295 return;
[email protected]327344ef2009-10-29 06:18:11296 if (socket_stream_dispatcher_->OnMessageReceived(msg))
297 return;
[email protected]1edc16b82009-04-07 17:45:54298
[email protected]8930d472009-02-21 08:05:28299 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
300 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39301 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
302 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]8930d472009-02-21 08:05:28303 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55304 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28305 // TODO(port): removed from render_messages_internal.h;
306 // is there a new non-windows message I should add here?
307 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
308 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17309 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46310 OnGetRendererHistograms)
[email protected]d41041092009-10-08 06:56:57311#if defined(USE_TCMALLOC)
312 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererTcmalloc,
313 OnGetRendererTcmalloc)
314#endif
[email protected]38b48a82009-11-11 01:51:32315 IPC_MESSAGE_HANDLER(ViewMsg_GetV8HeapStats, OnGetV8HeapStats)
[email protected]8930d472009-02-21 08:05:28316 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
317 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02318 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28319 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28320 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
321 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43322 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
323 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51324 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
325 OnSetExtensionFunctionNames)
[email protected]fede6ca12009-10-08 18:24:26326 IPC_MESSAGE_HANDLER(ViewMsg_PurgeMemory, OnPurgeMemory)
[email protected]b547fd42009-04-23 23:16:27327 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
328 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58329 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
330 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25331 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
332 OnExtensionSetAPIPermissions)
333 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
334 OnExtensionSetHostPermissions)
[email protected]b7c7bcf2009-10-03 07:07:34335 IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent,
336 OnDOMStorageEvent)
[email protected]75e126b932009-09-28 19:38:49337 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetL10nMessages,
338 OnExtensionSetL10nMessages)
[email protected]d55aaa132009-09-28 21:08:04339#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21340 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04341 OnSetIPCLoggingEnabled)
342#endif
[email protected]85c55dc2009-11-06 03:05:46343#if defined(SPELLCHECKER_IN_RENDERER)
344 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_Init,
345 OnInitSpellChecker)
346 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_WordAdded,
347 OnSpellCheckWordAdded)
348 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_EnableAutoSpellCorrect,
349 OnSpellCheckEnableAutoSpellCorrect)
350#endif
[email protected]8930d472009-02-21 08:05:28351 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29352}
353
354void RenderThread::OnSetNextPageID(int32 next_page_id) {
355 // This should only be called at process initialization time, so we shouldn't
356 // have to worry about thread-safety.
357 RenderView::SetNextPageID(next_page_id);
358}
359
[email protected]b9ab10c2009-08-07 18:09:55360// Called when to register CSS Color name->system color mappings.
361// We update the colors one by one and then tell WebKit to refresh all render
362// views.
363void RenderThread::OnSetCSSColors(
364 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59365 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55366 size_t num_colors = colors.size();
367 scoped_array<WebKit::WebColorName> color_names(
368 new WebKit::WebColorName[num_colors]);
369 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
370 size_t i = 0;
371 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
372 colors.begin();
373 it != colors.end();
374 ++it, ++i) {
375 color_names[i] = it->first;
376 web_colors[i] = it->second;
377 }
378 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
379}
380
[email protected]18bcc3c2009-01-27 21:39:15381void RenderThread::OnCreateNewView(gfx::NativeViewId parent_hwnd,
[email protected]80d96fa2009-06-10 22:34:51382 const RendererPreferences& renderer_prefs,
initial.commit09911bf2008-07-26 23:55:29383 const WebPreferences& webkit_prefs,
384 int32 view_id) {
[email protected]90a3fbb12009-02-28 01:13:47385 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33386 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29387 RenderView::Create(
[email protected]12636df2009-09-28 22:32:21388 this, parent_hwnd, MSG_ROUTING_NONE, renderer_prefs,
[email protected]80d96fa2009-06-10 22:34:51389 webkit_prefs, new SharedRenderViewCounter(0), view_id);
[email protected]7f874dec2009-02-06 01:48:27390}
[email protected]4274e582009-01-27 22:09:56391
initial.commit09911bf2008-07-26 23:55:29392void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
393 size_t max_dead_capacity,
394 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47395 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47396 WebCache::setCapacities(
397 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29398}
399
400void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47401 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47402 WebCache::ResourceTypeStats stats;
403 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29404 Send(new ViewHostMsg_ResourceTypeStats(stats));
405}
406
[email protected]c9a3ef82009-05-28 22:02:46407void RenderThread::OnGetRendererHistograms(int sequence_number) {
408 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17409}
410
[email protected]d41041092009-10-08 06:56:57411#if defined(USE_TCMALLOC)
412void RenderThread::OnGetRendererTcmalloc() {
413 std::string result;
414 char buffer[1024 * 32];
[email protected]a4dc33f2009-10-20 15:09:55415 base::ProcessId pid = base::GetCurrentProcId();
[email protected]d41041092009-10-08 06:56:57416 MallocExtension::instance()->GetStats(buffer, sizeof(buffer));
417 result.append(buffer);
418 Send(new ViewHostMsg_RendererTcmalloc(pid, result));
419}
420#endif
421
[email protected]38b48a82009-11-11 01:51:32422void RenderThread::OnGetV8HeapStats() {
423 v8::HeapStatistics heap_stats;
424 v8::V8::GetHeapStatistics(&heap_stats);
425 Send(new ViewHostMsg_V8HeapStats(heap_stats.total_heap_size(),
426 heap_stats.used_heap_size()));
427}
428
initial.commit09911bf2008-07-26 23:55:29429void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47430 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47431 WebCache::UsageStats stats;
432 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29433 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03434 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29435}
436
437void RenderThread::InformHostOfCacheStatsLater() {
438 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03439 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29440 return;
441
[email protected]bee16aab2009-08-26 15:55:03442 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29443 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03444 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29445 &RenderThread::InformHostOfCacheStats),
446 kCacheStatsDelayMS);
447}
[email protected]90a3fbb12009-02-28 01:13:47448
[email protected]b07f29092009-06-05 07:33:21449void RenderThread::CloseIdleConnections() {
450 Send(new ViewHostMsg_CloseIdleConnections());
451}
452
453void RenderThread::SetCacheMode(bool enabled) {
454 Send(new ViewHostMsg_SetCacheMode(enabled));
455}
456
[email protected]85c55dc2009-11-06 03:05:46457#if defined(SPELLCHECKER_IN_RENDERER)
458void RenderThread::RequestSpellCheckDictionary() {
459 Send(new ViewHostMsg_SpellChecker_RequestDictionary);
460}
461#endif
462
[email protected]3df0c202009-03-31 23:51:26463static void* CreateHistogram(
464 const char *name, int min, int max, size_t buckets) {
[email protected]56a27b22009-05-21 00:44:37465 Histogram* histogram = new Histogram(name, min, max, buckets);
466 if (histogram) {
467 histogram->SetFlags(kUmaTargetedHistogramFlag);
468 }
469 return histogram;
[email protected]3df0c202009-03-31 23:51:26470}
471
472static void AddHistogramSample(void* hist, int sample) {
473 Histogram* histogram = static_cast<Histogram *>(hist);
474 histogram->Add(sample);
475}
476
[email protected]90a3fbb12009-02-28 01:13:47477void RenderThread::EnsureWebKitInitialized() {
478 if (webkit_client_.get())
479 return;
[email protected]da00a2882009-03-09 17:51:19480
481 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26482 v8::V8::SetCreateHistogramFunction(CreateHistogram);
483 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19484
[email protected]90a3fbb12009-02-28 01:13:47485 webkit_client_.reset(new RendererWebKitClientImpl);
486 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44487
[email protected]98d7127b2009-10-23 18:26:51488 WebScriptController::enableV8SingleThreadMode();
[email protected]4ea00e82009-07-30 22:59:34489
[email protected]60e448982009-05-06 04:21:16490 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44491 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16492 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47493 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]204758c2009-10-22 03:56:30494 WebSecurityPolicy::registerURLSchemeAsLocal(chrome_ui_scheme);
495 WebSecurityPolicy::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19496
[email protected]d53d8492009-07-14 01:51:32497 // print: pages should be not accessible by normal context.
498 WebString print_ui_scheme(ASCIIToUTF16(chrome::kPrintScheme));
[email protected]204758c2009-10-22 03:56:30499 WebSecurityPolicy::registerURLSchemeAsLocal(print_ui_scheme);
500 WebSecurityPolicy::registerURLSchemeAsNoAccess(print_ui_scheme);
[email protected]d53d8492009-07-14 01:51:32501
[email protected]6cf8b1a22009-05-26 23:36:00502#if defined(OS_WIN)
503 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]98d7127b2009-10-23 18:26:51504 WebScriptController::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00505#endif
[email protected]98d7127b2009-10-23 18:26:51506 WebScriptController::registerExtension(
507 extensions_v8::IntervalExtension::Get());
508 WebScriptController::registerExtension(
509 extensions_v8::LoadTimesExtension::Get());
510 WebScriptController::registerExtension(
511 extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27512
[email protected]ad1f9bd2009-07-30 20:23:15513 const WebKit::WebString kExtensionScheme =
514 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
515
[email protected]98d7127b2009-10-23 18:26:51516 WebScriptController::registerExtension(
517 ExtensionProcessBindings::Get(), kExtensionScheme);
[email protected]ad1f9bd2009-07-30 20:23:15518
[email protected]98d7127b2009-10-23 18:26:51519 WebScriptController::registerExtension(
520 BaseJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
521 WebScriptController::registerExtension(
522 BaseJsV8Extension::Get(), kExtensionScheme);
523 WebScriptController::registerExtension(
524 JsonSchemaJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
525 WebScriptController::registerExtension(JsonSchemaJsV8Extension::Get(),
526 kExtensionScheme);
527 WebScriptController::registerExtension(
528 EventBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
529 WebScriptController::registerExtension(EventBindings::Get(),
530 kExtensionScheme);
531 WebScriptController::registerExtension(
532 RendererExtensionBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
533 WebScriptController::registerExtension(
534 RendererExtensionBindings::Get(), kExtensionScheme);
535 WebScriptController::registerExtension(
536 ExtensionApiTestV8Extension::Get(), kExtensionScheme);
537 WebScriptController::registerExtension(
538 ExtensionApiTestV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04539
[email protected]e2b2d4a2009-10-24 03:32:59540 renderer_web_database_observer_.reset(new RendererWebDatabaseObserver(this));
541 WebKit::WebDatabase::setObserver(renderer_web_database_observer_.get());
542
[email protected]06533c0b2009-03-05 21:39:11543 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04544
[email protected]98d7127b2009-10-23 18:26:51545 if (command_line.HasSwitch(switches::kEnableBenchmarking)) {
546 WebScriptController::registerExtension(
547 extensions_v8::BenchmarkingExtension::Get());
548 }
[email protected]b07f29092009-06-05 07:33:21549
[email protected]06533c0b2009-03-05 21:39:11550 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18551 command_line.HasSwitch(switches::kRecordMode) ||
552 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]98d7127b2009-10-23 18:26:51553 WebScriptController::registerExtension(
554 extensions_v8::PlaybackExtension::Get());
[email protected]06533c0b2009-03-05 21:39:11555 }
[email protected]2cb82332009-03-18 17:24:55556
[email protected]b1b13212009-10-30 06:42:01557 WebRuntimeFeatures::enableMediaPlayer(
558 RenderProcess::current()->initialized_media_library());
[email protected]3553c6cc2009-10-20 05:51:55559
[email protected]b1b13212009-10-30 06:42:01560 WebRuntimeFeatures::enableSockets(
[email protected]cdf24782009-11-05 09:32:47561 !command_line.HasSwitch(switches::kDisableWebSockets));
[email protected]0afb9932009-10-24 00:37:58562
[email protected]b1b13212009-10-30 06:42:01563 WebRuntimeFeatures::enableDatabase(
564 command_line.HasSwitch(switches::kEnableDatabases));
[email protected]bfa69d492009-10-31 03:27:19565
[email protected]13bc0d822009-11-07 01:17:14566 WebRuntimeFeatures::enableApplicationCache(
567 command_line.HasSwitch(switches::kEnableApplicationCache));
568
[email protected]bfa69d492009-10-31 03:27:19569#if defined(OS_WIN)
570 // We don't yet support notifications on non-Windows, so hide it from pages.
571 WebRuntimeFeatures::enableNotifications(
572 command_line.HasSwitch(switches::kEnableDesktopNotifications));
573#endif
[email protected]7ee66e62009-11-05 09:16:30574
575 WebRuntimeFeatures::enableLocalStorage(
[email protected]5cf18362009-11-06 18:45:01576 !command_line.HasSwitch(switches::kDisableLocalStorage));
[email protected]7ee66e62009-11-05 09:16:30577 WebRuntimeFeatures::enableSessionStorage(
578 command_line.HasSwitch(switches::kEnableSessionStorage));
[email protected]90a3fbb12009-02-28 01:13:47579}
[email protected]75e5a872009-04-02 23:56:11580
[email protected]bee16aab2009-08-26 15:55:03581void RenderThread::IdleHandler() {
582 // It is possible that the timer was set while the widgets were idle,
583 // but that they are no longer idle. If so, just return.
584 if (!widget_count_ || hidden_widget_count_ < widget_count_)
585 return;
586
[email protected]05001182009-09-15 23:34:22587#if defined(OS_WIN)
[email protected]e94afbb92009-10-01 00:25:41588 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22589#endif
[email protected]bee16aab2009-08-26 15:55:03590
[email protected]f5e4b9bf2009-10-08 00:59:59591 LOG(INFO) << "RenderThread calling v8 IdleNotification for " << this;
592 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22593
594 // Schedule next invocation.
595 // Dampen the delay using the algorithm:
596 // delay = delay + 1 / (delay + 2)
597 // Using floor(delay) has a dampening effect such as:
598 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
599 // Note that idle_notification_delay_in_s_ would be reset to
600 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
601 idle_notification_delay_in_s_ +=
602 1.0 / (idle_notification_delay_in_s_ + 2.0);
603
604 // Schedule the next timer.
605 MessageLoop::current()->PostDelayedTask(FROM_HERE,
606 task_factory_->NewRunnableMethod(&RenderThread::IdleHandler),
607 static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000);
[email protected]bee16aab2009-08-26 15:55:03608}
609
[email protected]dfcb62a2009-06-17 19:32:43610void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
611 const ListValue& args) {
[email protected]7120f132009-07-20 21:05:37612 RendererExtensionBindings::Invoke(function_name, args, NULL);
[email protected]b68d5ed2009-04-16 02:41:28613}
[email protected]b547fd42009-04-23 23:16:27614
[email protected]fede6ca12009-10-08 18:24:26615void RenderThread::OnPurgeMemory() {
[email protected]85c55dc2009-11-06 03:05:46616#if defined(SPELLCHECKER_IN_RENDERER)
617 spellchecker_.reset(new SpellCheck());
618#endif
619
[email protected]fede6ca12009-10-08 18:24:26620 EnsureWebKitInitialized();
621
622 // Clear the object cache (as much as possible; some live objects cannot be
623 // freed).
624 WebCache::clear();
625
626 // Clear the font/glyph cache.
627 WebFontCache::clear();
628
629 // Clear the Cross-Origin Preflight cache.
630 WebCrossOriginPreflightResultCache::clear();
631
[email protected]adf00bc2009-11-02 18:35:00632 // Release all freeable memory from the SQLite process-global page cache (a
633 // low-level object which backs the Connection-specific page caches).
[email protected]4a3dab22009-11-11 17:36:50634 while (sqlite3_release_memory(std::numeric_limits<int>::max()) > 0) {
635 }
[email protected]adf00bc2009-11-02 18:35:00636
[email protected]fede6ca12009-10-08 18:24:26637 // Repeatedly call the V8 idle notification until it returns true ("nothing
638 // more to free"). Note that it makes more sense to do this than to implement
639 // a new "delete everything" pass because object references make it difficult
640 // to free everything possible in just one pass.
[email protected]4a3dab22009-11-11 17:36:50641 while (!v8::V8::IdleNotification()) {
642 }
[email protected]fede6ca12009-10-08 18:24:26643
644#if defined(OS_WIN)
645 // Tell tcmalloc to release any free pages it's still holding.
646 MallocExtension::instance()->ReleaseFreeMemory();
647#endif
648}
649
[email protected]b78e168b2009-09-21 22:05:45650void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59651 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27652 // The call below will cause a GetPlugins call with refresh=true, but at this
653 // point we already know that the browser has refreshed its list, so disable
654 // refresh temporarily to prevent each renderer process causing the list to be
655 // regenerated.
656 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45657 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27658 plugin_refresh_allowed_ = true;
659}
[email protected]85c55dc2009-11-06 03:05:46660
661#if defined(SPELLCHECKER_IN_RENDERER)
662void RenderThread::OnInitSpellChecker(
663 const base::FileDescriptor& bdict_fd,
664 const std::vector<std::string>& custom_words,
665 const std::string& language,
666 bool auto_spell_correct) {
667 spellchecker_->Init(bdict_fd, custom_words, language);
668 spellchecker_->EnableAutoSpellCorrect(auto_spell_correct);
669}
670
671void RenderThread::OnSpellCheckWordAdded(const std::string& word) {
672 spellchecker_->WordAdded(word);
673}
674
675void RenderThread::OnSpellCheckEnableAutoSpellCorrect(bool enable) {
676 spellchecker_->EnableAutoSpellCorrect(enable);
677}
678#endif