blob: 6448cf5f82bafcb9916999aae92f404c30919b00 [file] [log] [blame]
[email protected]f85f0702010-01-30 09:31:011// Copyright (c) 2010 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]61a9b2d82010-02-26 00:31:0810#include <limits>
[email protected]75e126b932009-09-28 19:38:4911#include <map>
[email protected]da00a2882009-03-09 17:51:1912#include <vector>
13
[email protected]adf00bc2009-11-02 18:35:0014#if defined(USE_SYSTEM_SQLITE)
15#include <sqlite3.h>
16#else
17#include "third_party/sqlite/preprocessed/sqlite3.h"
18#endif
19
[email protected]06533c0b2009-03-05 21:39:1120#include "base/command_line.h"
[email protected]94f9a0f682009-06-15 18:30:3021#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0322#include "base/logging.h"
[email protected]b7c7bcf2009-10-03 07:07:3423#include "base/nullable_string16.h"
[email protected]d41041092009-10-08 06:56:5724#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2925#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1926#include "base/stats_table.h"
[email protected]b7c7bcf2009-10-03 07:07:3427#include "base/string_util.h"
[email protected]94f9a0f682009-06-15 18:30:3028#include "base/thread_local.h"
[email protected]f430b5712009-08-21 21:46:3129#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]c8865962009-12-16 07:47:3930#include "chrome/common/child_process_logging.h"
[email protected]06533c0b2009-03-05 21:39:1131#include "chrome/common/chrome_switches.h"
[email protected]017022b2009-07-27 23:06:3432#include "chrome/common/db_message_filter.h"
[email protected]c1f50aa2010-02-18 03:46:5733#include "chrome/common/plugin_messages.h"
[email protected]e09ba552009-02-05 03:26:2934#include "chrome/common/render_messages.h"
[email protected]9b6f40e2009-06-11 15:54:2635#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4736#include "chrome/common/url_constants.h"
[email protected]8930d472009-02-21 08:05:2837#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5038// TODO(port)
39#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2940#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5041#else
[email protected]2c62b562009-01-27 19:04:5042#include "base/scoped_handle.h"
43#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5044#endif
[email protected]a9602de2010-03-18 23:43:1145#include "chrome/renderer/automation/dom_automation_v8_extension.h"
[email protected]dd9241932010-02-24 19:23:1346#include "chrome/renderer/cookie_message_filter.h"
[email protected]a8624712009-04-17 00:51:3547#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1548#include "chrome/renderer/extension_groups.h"
[email protected]a40caa972009-04-08 18:35:3449#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2750#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1751#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4352#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4253#include "chrome/renderer/external_extension.h"
[email protected]0bc46552009-04-07 21:56:4254#include "chrome/renderer/loadtimes_extension_bindings.h"
initial.commit09911bf2008-07-26 23:55:2955#include "chrome/renderer/net/render_dns_master.h"
[email protected]c1f50aa2010-02-18 03:46:5756#include "chrome/renderer/plugin_channel_host.h"
[email protected]00c39612010-03-06 02:53:2857#include "chrome/renderer/render_process_impl.h"
initial.commit09911bf2008-07-26 23:55:2958#include "chrome/renderer/render_view.h"
[email protected]40bd6582009-12-04 23:49:5159#include "chrome/renderer/render_view_visitor.h"
[email protected]8d86fce2009-02-26 23:37:5560#include "chrome/renderer/renderer_webkitclient_impl.h"
[email protected]e2b2d4a2009-10-24 03:32:5961#include "chrome/renderer/renderer_web_database_observer.h"
[email protected]85c55dc2009-11-06 03:05:4662#include "chrome/renderer/spellchecker/spellcheck.h"
[email protected]0938d3c2009-01-09 20:37:3563#include "chrome/renderer/user_script_slave.h"
[email protected]d55aaa132009-09-28 21:08:0464#include "ipc/ipc_message.h"
[email protected]cb6037d2009-11-16 22:55:1765#include "ipc/ipc_platform_file.h"
[email protected]1b1f3eb2009-12-01 13:48:0466#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]418ed5ab2009-11-12 01:14:4967#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"
68#include "third_party/WebKit/WebKit/chromium/public/WebColor.h"
69#include "third_party/WebKit/WebKit/chromium/public/WebCrossOriginPreflightResultCache.h"
70#include "third_party/WebKit/WebKit/chromium/public/WebDatabase.h"
71#include "third_party/WebKit/WebKit/chromium/public/WebFontCache.h"
[email protected]40bd6582009-12-04 23:49:5172#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
[email protected]418ed5ab2009-11-12 01:14:4973#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
74#include "third_party/WebKit/WebKit/chromium/public/WebRuntimeFeatures.h"
75#include "third_party/WebKit/WebKit/chromium/public/WebScriptController.h"
76#include "third_party/WebKit/WebKit/chromium/public/WebSecurityPolicy.h"
77#include "third_party/WebKit/WebKit/chromium/public/WebStorageEventDispatcher.h"
78#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
[email protected]b07f29092009-06-05 07:33:2179#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1180#include "webkit/extensions/v8/gears_extension.h"
81#include "webkit/extensions/v8/interval_extension.h"
82#include "webkit/extensions/v8/playback_extension.h"
[email protected]2c62b562009-01-27 19:04:5083
[email protected]da00a2882009-03-09 17:51:1984#if defined(OS_WIN)
85#include <windows.h>
86#include <objbase.h>
87#endif
88
[email protected]fe819f52009-12-15 07:58:1189#if defined(OS_MACOSX)
90#include "chrome/app/breakpad_mac.h"
91#endif
92
[email protected]2c434b32009-03-19 06:27:4793using WebKit::WebCache;
[email protected]fede6ca12009-10-08 18:24:2694using WebKit::WebCrossOriginPreflightResultCache;
95using WebKit::WebFontCache;
[email protected]f85f0702010-01-30 09:31:0196using WebKit::WebFrame;
[email protected]adf00bc2009-11-02 18:35:0097using WebKit::WebRuntimeFeatures;
[email protected]204758c2009-10-22 03:56:3098using WebKit::WebSecurityPolicy;
[email protected]98d7127b2009-10-23 18:26:5199using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:47100using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:34101using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:03102using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:29103
[email protected]42f1d7822009-07-23 18:17:55104namespace {
[email protected]2c434b32009-03-19 06:27:47105static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:03106static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
[email protected]71d6d852009-12-07 22:12:36107static const double kInitialExtensionIdleHandlerDelayS = 5.0 /* seconds */;
108static const int64 kMaxExtensionIdleHandlerDelayS = 5*60 /* seconds */;
[email protected]bee16aab2009-08-26 15:55:03109
[email protected]94f9a0f682009-06-15 18:30:30110static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
111 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54112
[email protected]5fa1c542009-05-05 20:36:07113#if defined(OS_POSIX)
114class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
115 void OnChannelError() {
116 // On POSIX, at least, one can install an unload handler which loops
117 // forever and leave behind a renderer process which eats 100% CPU forever.
118 //
119 // This is because the terminate signals (ViewMsg_ShouldClose and the error
120 // from the IPC channel) are routed to the main message loop but never
121 // processed (because that message loop is stuck in V8).
122 //
123 // One could make the browser SIGKILL the renderers, but that leaves open a
124 // large window where a browser failure (or a user, manually terminating
125 // the browser because "it's stuck") will leave behind a process eating all
126 // the CPU.
127 //
128 // So, we install a filter on the channel so that we can process this event
129 // here and kill the process.
[email protected]fe819f52009-12-15 07:58:11130
131#if defined(OS_MACOSX)
132 // TODO(viettrungluu): crbug.com/28547: The following is needed, as a
133 // stopgap, to avoid leaking due to not releasing Breakpad properly.
134 // TODO(viettrungluu): Investigate why this is being called.
135 if (IsCrashReporterEnabled()) {
136 LOG(INFO) << "Cleaning up Breakpad.";
137 DestructCrashReporter();
138 } else {
139 LOG(INFO) << "Breakpad not enabled; no clean-up needed.";
140 }
141#endif // OS_MACOSX
142
[email protected]5fa1c542009-05-05 20:36:07143 _exit(0);
144 }
145};
146#endif
[email protected]40bd6582009-12-04 23:49:51147
[email protected]f85f0702010-01-30 09:31:01148class RenderViewContentSettingsSetter : public RenderViewVisitor {
149 public:
150 RenderViewContentSettingsSetter(const std::string& host,
151 const ContentSettings& content_settings)
152 : host_(host),
153 content_settings_(content_settings) {
154 }
155
156 virtual bool Visit(RenderView* render_view) {
[email protected]433819d2010-01-30 20:20:01157 if (GURL(render_view->webview()->mainFrame()->url()).host() == host_)
158 render_view->SetContentSettings(content_settings_);
[email protected]f85f0702010-01-30 09:31:01159 return true;
160 }
161
162 private:
163 std::string host_;
164 ContentSettings content_settings_;
165
166 DISALLOW_COPY_AND_ASSIGN(RenderViewContentSettingsSetter);
167};
168
[email protected]40bd6582009-12-04 23:49:51169class RenderViewZoomer : public RenderViewVisitor {
170 public:
171 RenderViewZoomer(const std::string& host, int zoom_level)
172 : host_(host),
173 zoom_level_(zoom_level) {
174 }
175
176 virtual bool Visit(RenderView* render_view) {
177 WebView* webview = render_view->webview(); // Guaranteed non-NULL.
178 if (GURL(webview->mainFrame()->url()).host() == host_)
179 webview->setZoomLevel(false, zoom_level_);
180 return true;
181 }
182
183 private:
184 std::string host_;
185 int zoom_level_;
186
187 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
188};
[email protected]42f1d7822009-07-23 18:17:55189} // namespace
190
191// When we run plugins in process, we actually run them on the render thread,
192// which means that we need to make the render thread pump UI events.
193RenderThread::RenderThread() {
194 Init();
195}
196
197RenderThread::RenderThread(const std::string& channel_name)
198 : ChildThread(channel_name) {
199 Init();
200}
[email protected]5fa1c542009-05-05 20:36:07201
initial.commit09911bf2008-07-26 23:55:29202void RenderThread::Init() {
[email protected]94f9a0f682009-06-15 18:30:30203 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50204#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34205 // If you are running plugins in this thread you need COM active but in
206 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28207 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34208 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50209#endif
initial.commit09911bf2008-07-26 23:55:29210
[email protected]71d6d852009-12-07 22:12:36211 std::string type_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
212 switches::kProcessType);
213 is_extension_process_ = type_str == switches::kExtensionProcess;
[email protected]b2a74ca2010-03-12 17:57:09214 is_incognito_process_ = false;
[email protected]80fc08c52010-03-09 07:43:50215 suspend_webkit_shared_timer_ = true;
216 notify_webkit_of_modal_loop_ = true;
[email protected]c1f50aa2010-02-18 03:46:57217 did_notify_webkit_of_modal_loop_ = false;
[email protected]42f1d7822009-07-23 18:17:55218 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03219 cache_stats_task_pending_ = false;
220 widget_count_ = 0;
221 hidden_widget_count_ = 0;
[email protected]71d6d852009-12-07 22:12:36222 idle_notification_delay_in_s_ = is_extension_process_ ?
223 kInitialExtensionIdleHandlerDelayS : kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03224 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55225
[email protected]8d86fce2009-02-26 23:37:55226 visited_link_slave_.reset(new VisitedLinkSlave());
227 user_script_slave_.reset(new UserScriptSlave());
228 dns_master_.reset(new RenderDnsMaster());
229 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31230 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]dd9241932010-02-24 19:23:13231 spellchecker_.reset(new SpellCheck());
232
[email protected]a8624712009-04-17 00:51:35233 devtools_agent_filter_ = new DevToolsAgentFilter();
234 AddFilter(devtools_agent_filter_.get());
[email protected]dd9241932010-02-24 19:23:13235
[email protected]017022b2009-07-27 23:06:34236 db_message_filter_ = new DBMessageFilter();
237 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13238
239 cookie_message_filter_ = new CookieMessageFilter();
240 AddFilter(cookie_message_filter_.get());
[email protected]5fa1c542009-05-05 20:36:07241
242#if defined(OS_POSIX)
243 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
244 AddFilter(suicide_on_channel_error_filter_.get());
245#endif
initial.commit09911bf2008-07-26 23:55:29246}
247
[email protected]42f1d7822009-07-23 18:17:55248RenderThread::~RenderThread() {
[email protected]12cbfda32010-01-30 01:04:25249 // Wait for all databases to be closed.
250 if (renderer_web_database_observer_.get())
251 renderer_web_database_observer_->WaitForAllDatabasesToClose();
252
[email protected]8d86fce2009-02-26 23:37:55253 // Shutdown in reverse of the initialization order.
[email protected]a8624712009-04-17 00:51:35254 RemoveFilter(devtools_agent_filter_.get());
[email protected]017022b2009-07-27 23:06:34255 RemoveFilter(db_message_filter_.get());
256 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25257
[email protected]42f1d7822009-07-23 18:17:55258 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22259 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22260
[email protected]94f9a0f682009-06-15 18:30:30261 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30262
[email protected]8d86fce2009-02-26 23:37:55263 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50264#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29265 // Clean up plugin channels before this thread goes away.
266 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34267 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28268 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34269 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50270#endif
initial.commit09911bf2008-07-26 23:55:29271}
272
[email protected]42f1d7822009-07-23 18:17:55273RenderThread* RenderThread::current() {
274 return lazy_tls.Pointer()->Get();
275}
276
[email protected]c1f50aa2010-02-18 03:46:57277int32 RenderThread::RoutingIDForCurrentContext() {
278 int32 routing_id = MSG_ROUTING_CONTROL;
279 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29280 WebFrame* frame = WebFrame::frameForCurrentContext();
281 if (frame) {
282 RenderView* view = RenderView::FromWebView(frame->view());
283 if (view)
284 routing_id = view->routing_id();
285 }
[email protected]c1f50aa2010-02-18 03:46:57286 } else {
287 DLOG(WARNING) << "Not called within a script context!";
288 }
289 return routing_id;
290}
291
292bool RenderThread::Send(IPC::Message* msg) {
[email protected]80fc08c52010-03-09 07:43:50293 // Certain synchronous messages can result in an app-modal cookie prompt.
294 // This could cause a complete hang of Chrome if a windowed plug-in is trying
295 // to communicate with the renderer thread since the browser's UI thread
296 // could be stuck (within a Windows API call) trying to synchronously
297 // communicate with the plug-in. The remedy is to pump messages on this
298 // thread while the cookie prompt is showing. This creates an opportunity
299 // for re-entrancy into WebKit, so we need to take care to disable callbacks,
300 // timers, and pending network loads that could trigger such callbacks.
[email protected]c1f50aa2010-02-18 03:46:57301
[email protected]80fc08c52010-03-09 07:43:50302 bool pumping_events = false, may_show_cookie_prompt = false;
303 if (msg->is_sync()) {
304 if (msg->is_caller_pumping_messages()) {
305 pumping_events = true;
306 } else {
307 switch (msg->type()) {
308 case ViewHostMsg_GetCookies::ID:
309 case ViewHostMsg_GetRawCookies::ID:
310 case ViewHostMsg_DOMStorageSetItem::ID:
311 case ViewHostMsg_SyncLoad::ID:
312 may_show_cookie_prompt = true;
313 pumping_events = true;
314 break;
315 }
316 }
[email protected]c1f50aa2010-02-18 03:46:57317 }
318
[email protected]80fc08c52010-03-09 07:43:50319 bool suspend_webkit_shared_timer = true; // default value
320 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57321
[email protected]80fc08c52010-03-09 07:43:50322 bool notify_webkit_of_modal_loop = true; // default value
323 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
324
325 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57326
327 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50328 // See ViewMsg_SignalCookiePromptEvent.
329 if (may_show_cookie_prompt) {
330 static_cast<IPC::SyncMessage*>(msg)->set_pump_messages_event(
331 cookie_message_filter_->pump_messages_event());
332 }
333
334 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57335 webkit_client_->SuspendSharedTimer();
336
337 // WebKit does not like nested calls to willEnterModalLoop.
338 // TODO(darin): Fix WebKit to allow nesting.
[email protected]80fc08c52010-03-09 07:43:50339 if (notify_webkit_of_modal_loop && !did_notify_webkit_of_modal_loop_) {
[email protected]c1f50aa2010-02-18 03:46:57340 WebView::willEnterModalLoop();
341 did_notify_webkit_of_modal_loop_ = true;
342 }
343
[email protected]80fc08c52010-03-09 07:43:50344 RenderWidget* widget =
345 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
346 if (widget) {
347 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57348 PluginChannelHost::Broadcast(
349 new PluginMsg_SignalModalDialogEvent(host_window));
350 }
351 }
352
353 bool rv = ChildThread::Send(msg);
354
355 if (pumping_events) {
356 if (host_window) {
357 PluginChannelHost::Broadcast(
358 new PluginMsg_ResetModalDialogEvent(host_window));
359 }
360
[email protected]80fc08c52010-03-09 07:43:50361 if (notify_webkit_of_modal_loop && did_notify_webkit_of_modal_loop_) {
[email protected]c1f50aa2010-02-18 03:46:57362 WebView::didExitModalLoop();
363 did_notify_webkit_of_modal_loop_ = false;
364 }
365
[email protected]80fc08c52010-03-09 07:43:50366 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57367 webkit_client_->ResumeSharedTimer();
[email protected]80fc08c52010-03-09 07:43:50368
369 // We may end up nesting calls to Send, so we defer the reset until we
370 // return to the top-most message loop.
371 if (may_show_cookie_prompt &&
372 cookie_message_filter_->pump_messages_event()->IsSignaled()) {
373 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
374 NewRunnableMethod(cookie_message_filter_.get(),
375 &CookieMessageFilter::ResetPumpMessagesEvent));
376 }
[email protected]c1f50aa2010-02-18 03:46:57377 }
378
379 return rv;
380}
381
382void RenderThread::AddRoute(int32 routing_id,
383 IPC::Channel::Listener* listener) {
384 widget_count_++;
385 return ChildThread::AddRoute(routing_id, listener);
386}
387
388void RenderThread::RemoveRoute(int32 routing_id) {
389 widget_count_--;
390 return ChildThread::RemoveRoute(routing_id);
391}
392
[email protected]42f1d7822009-07-23 18:17:55393void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
394 channel()->AddFilter(filter);
395}
396
397void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
398 channel()->RemoveFilter(filter);
399}
400
[email protected]bee16aab2009-08-26 15:55:03401void RenderThread::WidgetHidden() {
402 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50403 hidden_widget_count_++;
[email protected]71d6d852009-12-07 22:12:36404 if (!is_extension_process() &&
405 widget_count_ && hidden_widget_count_ == widget_count_)
406 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03407}
408
409void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08410 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03411 hidden_widget_count_--;
[email protected]71d6d852009-12-07 22:12:36412 if (!is_extension_process())
413 idle_timer_.Stop();
[email protected]bee16aab2009-08-26 15:55:03414}
415
[email protected]c1f50aa2010-02-18 03:46:57416void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50417 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57418}
419
420void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50421 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57422}
423
[email protected]42f1d7822009-07-23 18:17:55424void RenderThread::Resolve(const char* name, size_t length) {
425 return dns_master_->Resolve(name, length);
426}
427
428void RenderThread::SendHistograms(int sequence_number) {
429 return histogram_snapshots_->SendHistograms(sequence_number);
430}
431
[email protected]176aa482008-11-14 03:25:15432void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20433 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29434 visited_link_slave_->Init(table);
435}
436
[email protected]3e90d4a2009-07-03 17:38:39437void RenderThread::OnAddVisitedLinks(
438 const VisitedLinkSlave::Fingerprints& fingerprints) {
439 for (size_t i = 0; i < fingerprints.size(); ++i)
[email protected]50ae00ef2009-10-19 05:11:03440 WebView::updateVisitedLinkState(fingerprints[i]);
[email protected]3e90d4a2009-07-03 17:38:39441}
442
443void RenderThread::OnResetVisitedLinks() {
[email protected]50ae00ef2009-10-19 05:11:03444 WebView::resetVisitedLinkState();
[email protected]3e90d4a2009-07-03 17:38:39445}
446
[email protected]f85f0702010-01-30 09:31:01447void RenderThread::OnSetContentSettingsForCurrentHost(
448 const std::string& host,
449 const ContentSettings& content_settings) {
450 RenderViewContentSettingsSetter setter(host, content_settings);
451 RenderView::ForEach(&setter);
452}
453
[email protected]40bd6582009-12-04 23:49:51454void RenderThread::OnSetZoomLevelForCurrentHost(const std::string& host,
455 int zoom_level) {
456 RenderViewZoomer zoomer(host, zoom_level);
457 RenderView::ForEach(&zoomer);
458}
459
[email protected]b2a74ca2010-03-12 17:57:09460void RenderThread::OnUpdateUserScripts(base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20461 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]b2a74ca2010-03-12 17:57:09462 user_script_slave_->UpdateScripts(scripts);
[email protected]c8865962009-12-16 07:47:39463 UpdateActiveExtensions();
[email protected]1e0f70402008-10-16 23:57:47464}
465
[email protected]703e807a2009-03-28 19:56:51466void RenderThread::OnSetExtensionFunctionNames(
467 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04468 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51469}
470
[email protected]45776222009-07-15 20:21:58471void RenderThread::OnPageActionsUpdated(
472 const std::string& extension_id,
473 const std::vector<std::string>& page_actions) {
474 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
475}
476
[email protected]cccf90932009-08-23 17:56:25477void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19478 const std::string& extension_id,
479 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25480 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
[email protected]71d6d852009-12-07 22:12:36481
482 // This is called when starting a new extension page, so start the idle
483 // handler ticking.
[email protected]71d6d852009-12-07 22:12:36484 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]c8865962009-12-16 07:47:39485
486 UpdateActiveExtensions();
[email protected]cccf90932009-08-23 17:56:25487}
488
489void RenderThread::OnExtensionSetHostPermissions(
490 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
491 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19492}
493
[email protected]db7331a2010-02-25 22:10:50494void RenderThread::OnExtensionSetIncognitoEnabled(
495 const std::string& extension_id, bool enabled) {
496 ExtensionProcessBindings::SetIncognitoEnabled(extension_id, enabled);
497}
498
[email protected]c61cc652009-11-04 05:44:40499void RenderThread::OnDOMStorageEvent(
500 const ViewMsg_DOMStorageEvent_Params& params) {
501 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34502 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]c61cc652009-11-04 05:44:40503 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key_,
[email protected]ee2be5b32009-11-05 09:13:12504 params.old_value_, params.new_value_, params.origin_, params.url_,
[email protected]c61cc652009-11-04 05:44:40505 params.storage_type_ == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34506}
507
[email protected]8930d472009-02-21 08:05:28508void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1edc16b82009-04-07 17:45:54509 // App cache messages are handled by a delegate.
[email protected]f430b5712009-08-21 21:46:31510 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54511 return;
512
[email protected]8930d472009-02-21 08:05:28513 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
514 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39515 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
516 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]f85f0702010-01-30 09:31:01517 IPC_MESSAGE_HANDLER(ViewMsg_SetContentSettingsForCurrentHost,
518 OnSetContentSettingsForCurrentHost)
[email protected]40bd6582009-12-04 23:49:51519 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentHost,
520 OnSetZoomLevelForCurrentHost)
[email protected]b2a74ca2010-03-12 17:57:09521 IPC_MESSAGE_HANDLER(ViewMsg_SetIsIncognitoProcess, OnSetIsIncognitoProcess)
[email protected]8930d472009-02-21 08:05:28522 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55523 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28524 // TODO(port): removed from render_messages_internal.h;
525 // is there a new non-windows message I should add here?
526 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
527 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17528 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46529 OnGetRendererHistograms)
[email protected]d41041092009-10-08 06:56:57530#if defined(USE_TCMALLOC)
531 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererTcmalloc,
532 OnGetRendererTcmalloc)
533#endif
[email protected]38b48a82009-11-11 01:51:32534 IPC_MESSAGE_HANDLER(ViewMsg_GetV8HeapStats, OnGetV8HeapStats)
[email protected]8930d472009-02-21 08:05:28535 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
536 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02537 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28538 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28539 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
540 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43541 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
542 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51543 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
544 OnSetExtensionFunctionNames)
[email protected]fede6ca12009-10-08 18:24:26545 IPC_MESSAGE_HANDLER(ViewMsg_PurgeMemory, OnPurgeMemory)
[email protected]b547fd42009-04-23 23:16:27546 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
547 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58548 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
549 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25550 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
551 OnExtensionSetAPIPermissions)
552 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
553 OnExtensionSetHostPermissions)
[email protected]db7331a2010-02-25 22:10:50554 IPC_MESSAGE_HANDLER(ViewMsg_Extension_ExtensionSetIncognitoEnabled,
555 OnExtensionSetIncognitoEnabled)
[email protected]b7c7bcf2009-10-03 07:07:34556 IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent,
557 OnDOMStorageEvent)
[email protected]d55aaa132009-09-28 21:08:04558#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21559 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04560 OnSetIPCLoggingEnabled)
561#endif
[email protected]85c55dc2009-11-06 03:05:46562 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_Init,
563 OnInitSpellChecker)
564 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_WordAdded,
565 OnSpellCheckWordAdded)
566 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_EnableAutoSpellCorrect,
567 OnSpellCheckEnableAutoSpellCorrect)
[email protected]8930d472009-02-21 08:05:28568 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29569}
570
571void RenderThread::OnSetNextPageID(int32 next_page_id) {
572 // This should only be called at process initialization time, so we shouldn't
573 // have to worry about thread-safety.
574 RenderView::SetNextPageID(next_page_id);
575}
576
[email protected]b9ab10c2009-08-07 18:09:55577// Called when to register CSS Color name->system color mappings.
578// We update the colors one by one and then tell WebKit to refresh all render
579// views.
580void RenderThread::OnSetCSSColors(
581 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59582 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55583 size_t num_colors = colors.size();
584 scoped_array<WebKit::WebColorName> color_names(
585 new WebKit::WebColorName[num_colors]);
586 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
587 size_t i = 0;
588 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
589 colors.begin();
590 it != colors.end();
591 ++it, ++i) {
592 color_names[i] = it->first;
593 web_colors[i] = it->second;
594 }
595 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
596}
597
[email protected]4e6419c2010-01-15 04:50:34598void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47599 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33600 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29601 RenderView::Create(
[email protected]4e6419c2010-01-15 04:50:34602 this, params.parent_window, MSG_ROUTING_NONE, params.renderer_preferences,
603 params.web_preferences, new SharedRenderViewCounter(0), params.view_id,
604 params.session_storage_namespace_id);
[email protected]7f874dec2009-02-06 01:48:27605}
[email protected]4274e582009-01-27 22:09:56606
initial.commit09911bf2008-07-26 23:55:29607void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
608 size_t max_dead_capacity,
609 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47610 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47611 WebCache::setCapacities(
612 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29613}
614
615void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47616 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47617 WebCache::ResourceTypeStats stats;
618 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29619 Send(new ViewHostMsg_ResourceTypeStats(stats));
620}
621
[email protected]c9a3ef82009-05-28 22:02:46622void RenderThread::OnGetRendererHistograms(int sequence_number) {
623 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17624}
625
[email protected]d41041092009-10-08 06:56:57626#if defined(USE_TCMALLOC)
627void RenderThread::OnGetRendererTcmalloc() {
628 std::string result;
629 char buffer[1024 * 32];
[email protected]a4dc33f2009-10-20 15:09:55630 base::ProcessId pid = base::GetCurrentProcId();
[email protected]d41041092009-10-08 06:56:57631 MallocExtension::instance()->GetStats(buffer, sizeof(buffer));
632 result.append(buffer);
633 Send(new ViewHostMsg_RendererTcmalloc(pid, result));
634}
635#endif
636
[email protected]38b48a82009-11-11 01:51:32637void RenderThread::OnGetV8HeapStats() {
638 v8::HeapStatistics heap_stats;
639 v8::V8::GetHeapStatistics(&heap_stats);
640 Send(new ViewHostMsg_V8HeapStats(heap_stats.total_heap_size(),
641 heap_stats.used_heap_size()));
642}
643
initial.commit09911bf2008-07-26 23:55:29644void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47645 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47646 WebCache::UsageStats stats;
647 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29648 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03649 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29650}
651
652void RenderThread::InformHostOfCacheStatsLater() {
653 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03654 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29655 return;
656
[email protected]bee16aab2009-08-26 15:55:03657 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29658 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03659 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29660 &RenderThread::InformHostOfCacheStats),
661 kCacheStatsDelayMS);
662}
[email protected]90a3fbb12009-02-28 01:13:47663
[email protected]c40acc32010-01-14 01:02:53664void RenderThread::CloseCurrentConnections() {
665 Send(new ViewHostMsg_CloseCurrentConnections());
[email protected]b07f29092009-06-05 07:33:21666}
667
668void RenderThread::SetCacheMode(bool enabled) {
669 Send(new ViewHostMsg_SetCacheMode(enabled));
670}
671
[email protected]c8865962009-12-16 07:47:39672void RenderThread::UpdateActiveExtensions() {
673 // In single-process mode, the browser process reports the active extensions.
674 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
675 return;
676
677 std::set<std::string> active_extensions;
678 user_script_slave_->GetActiveExtensions(&active_extensions);
679 ExtensionProcessBindings::GetActiveExtensions(&active_extensions);
680 child_process_logging::SetActiveExtensions(active_extensions);
681}
682
[email protected]3df0c202009-03-31 23:51:26683static void* CreateHistogram(
684 const char *name, int min, int max, size_t buckets) {
[email protected]e8829a192009-12-06 00:09:37685 if (min <= 0)
686 min = 1;
[email protected]2753b392009-12-28 06:59:52687 scoped_refptr<Histogram> histogram = Histogram::FactoryGet(
688 name, min, max, buckets, Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:37689 // We'll end up leaking these histograms, unless there is some code hiding in
690 // there to do the dec-ref.
[email protected]2753b392009-12-28 06:59:52691 // TODO(jar): Handle reference counting in webkit glue.
[email protected]e8829a192009-12-06 00:09:37692 histogram->AddRef();
693 return histogram.get();
[email protected]3df0c202009-03-31 23:51:26694}
695
696static void AddHistogramSample(void* hist, int sample) {
697 Histogram* histogram = static_cast<Histogram *>(hist);
698 histogram->Add(sample);
699}
700
[email protected]90a3fbb12009-02-28 01:13:47701void RenderThread::EnsureWebKitInitialized() {
702 if (webkit_client_.get())
703 return;
[email protected]da00a2882009-03-09 17:51:19704
[email protected]71d6d852009-12-07 22:12:36705 // For extensions, we want to ensure we call the IdleHandler every so often,
706 // even if the extension keeps up activity.
707 if (is_extension_process()) {
708 forced_idle_timer_.Start(
709 base::TimeDelta::FromSeconds(kMaxExtensionIdleHandlerDelayS),
710 this, &RenderThread::IdleHandler);
711 }
712
[email protected]da00a2882009-03-09 17:51:19713 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26714 v8::V8::SetCreateHistogramFunction(CreateHistogram);
715 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19716
[email protected]90a3fbb12009-02-28 01:13:47717 webkit_client_.reset(new RendererWebKitClientImpl);
718 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44719
[email protected]98d7127b2009-10-23 18:26:51720 WebScriptController::enableV8SingleThreadMode();
[email protected]4ea00e82009-07-30 22:59:34721
[email protected]60e448982009-05-06 04:21:16722 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44723 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16724 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47725 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]204758c2009-10-22 03:56:30726 WebSecurityPolicy::registerURLSchemeAsLocal(chrome_ui_scheme);
727 WebSecurityPolicy::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19728
[email protected]37e5a9a2010-03-13 09:42:39729 // chrome-extension: resources shouldn't trigger mixed content warnings.
730 WebString extension_scheme(ASCIIToUTF16(chrome::kExtensionScheme));
731 WebSecurityPolicy::registerURLSchemeAsSecure(extension_scheme);
732
[email protected]d53d8492009-07-14 01:51:32733 // print: pages should be not accessible by normal context.
734 WebString print_ui_scheme(ASCIIToUTF16(chrome::kPrintScheme));
[email protected]204758c2009-10-22 03:56:30735 WebSecurityPolicy::registerURLSchemeAsLocal(print_ui_scheme);
736 WebSecurityPolicy::registerURLSchemeAsNoAccess(print_ui_scheme);
[email protected]d53d8492009-07-14 01:51:32737
[email protected]6cf8b1a22009-05-26 23:36:00738#if defined(OS_WIN)
739 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]98d7127b2009-10-23 18:26:51740 WebScriptController::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00741#endif
[email protected]98d7127b2009-10-23 18:26:51742 WebScriptController::registerExtension(
743 extensions_v8::IntervalExtension::Get());
744 WebScriptController::registerExtension(
745 extensions_v8::LoadTimesExtension::Get());
746 WebScriptController::registerExtension(
747 extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27748
[email protected]ad1f9bd2009-07-30 20:23:15749 const WebKit::WebString kExtensionScheme =
750 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
751
[email protected]98d7127b2009-10-23 18:26:51752 WebScriptController::registerExtension(
753 ExtensionProcessBindings::Get(), kExtensionScheme);
[email protected]ad1f9bd2009-07-30 20:23:15754
[email protected]98d7127b2009-10-23 18:26:51755 WebScriptController::registerExtension(
756 BaseJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
757 WebScriptController::registerExtension(
758 BaseJsV8Extension::Get(), kExtensionScheme);
759 WebScriptController::registerExtension(
760 JsonSchemaJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
761 WebScriptController::registerExtension(JsonSchemaJsV8Extension::Get(),
762 kExtensionScheme);
763 WebScriptController::registerExtension(
764 EventBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
765 WebScriptController::registerExtension(EventBindings::Get(),
766 kExtensionScheme);
767 WebScriptController::registerExtension(
768 RendererExtensionBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
769 WebScriptController::registerExtension(
770 RendererExtensionBindings::Get(), kExtensionScheme);
771 WebScriptController::registerExtension(
772 ExtensionApiTestV8Extension::Get(), kExtensionScheme);
773 WebScriptController::registerExtension(
774 ExtensionApiTestV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04775
[email protected]e2b2d4a2009-10-24 03:32:59776 renderer_web_database_observer_.reset(new RendererWebDatabaseObserver(this));
777 WebKit::WebDatabase::setObserver(renderer_web_database_observer_.get());
778
[email protected]06533c0b2009-03-05 21:39:11779 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04780
[email protected]98d7127b2009-10-23 18:26:51781 if (command_line.HasSwitch(switches::kEnableBenchmarking)) {
782 WebScriptController::registerExtension(
783 extensions_v8::BenchmarkingExtension::Get());
784 }
[email protected]b07f29092009-06-05 07:33:21785
[email protected]06533c0b2009-03-05 21:39:11786 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18787 command_line.HasSwitch(switches::kRecordMode) ||
788 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]98d7127b2009-10-23 18:26:51789 WebScriptController::registerExtension(
790 extensions_v8::PlaybackExtension::Get());
[email protected]06533c0b2009-03-05 21:39:11791 }
[email protected]2cb82332009-03-18 17:24:55792
[email protected]a9602de2010-03-18 23:43:11793 if (command_line.HasSwitch(switches::kDomAutomationController)) {
794 WebScriptController::registerExtension(DomAutomationV8Extension::Get());
795 }
796
[email protected]b1b13212009-10-30 06:42:01797 WebRuntimeFeatures::enableMediaPlayer(
[email protected]00c39612010-03-06 02:53:28798 RenderProcess::current()->HasInitializedMediaLibrary());
[email protected]3553c6cc2009-10-20 05:51:55799
[email protected]b1b13212009-10-30 06:42:01800 WebRuntimeFeatures::enableSockets(
[email protected]cdf24782009-11-05 09:32:47801 !command_line.HasSwitch(switches::kDisableWebSockets));
[email protected]0afb9932009-10-24 00:37:58802
[email protected]b1b13212009-10-30 06:42:01803 WebRuntimeFeatures::enableDatabase(
[email protected]90a7ad522009-11-20 22:42:53804 !command_line.HasSwitch(switches::kDisableDatabases));
[email protected]bfa69d492009-10-31 03:27:19805
[email protected]13bc0d822009-11-07 01:17:14806 WebRuntimeFeatures::enableApplicationCache(
[email protected]425c270642010-01-26 20:42:37807 !command_line.HasSwitch(switches::kDisableApplicationCache));
[email protected]13bc0d822009-11-07 01:17:14808
[email protected]bfa69d492009-10-31 03:27:19809 WebRuntimeFeatures::enableNotifications(
[email protected]380f1862009-11-14 02:48:01810 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
[email protected]7ee66e62009-11-05 09:16:30811
812 WebRuntimeFeatures::enableLocalStorage(
[email protected]5cf18362009-11-06 18:45:01813 !command_line.HasSwitch(switches::kDisableLocalStorage));
[email protected]7ee66e62009-11-05 09:16:30814 WebRuntimeFeatures::enableSessionStorage(
[email protected]03567802010-02-24 11:48:53815 !command_line.HasSwitch(switches::kDisableSessionStorage));
[email protected]2236bd1f2010-01-20 15:17:08816
[email protected]582fab732010-02-02 02:50:28817 WebRuntimeFeatures::enableIndexedDatabase(
818 command_line.HasSwitch(switches::kEnableIndexedDatabase));
819
[email protected]2236bd1f2010-01-20 15:17:08820 WebRuntimeFeatures::enableGeolocation(
821 command_line.HasSwitch(switches::kEnableGeolocation));
[email protected]7a716c82010-03-03 10:33:44822
823 WebRuntimeFeatures::enableWebGL(
824 command_line.HasSwitch(switches::kEnableExperimentalWebGL));
[email protected]ccbe04e2010-03-17 17:58:43825
826 WebRuntimeFeatures::enablePushState(true);
[email protected]90a3fbb12009-02-28 01:13:47827}
[email protected]75e5a872009-04-02 23:56:11828
[email protected]bee16aab2009-08-26 15:55:03829void RenderThread::IdleHandler() {
[email protected]61a9b2d82010-02-26 00:31:08830#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41831 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22832#endif
[email protected]bee16aab2009-08-26 15:55:03833
[email protected]f5e4b9bf2009-10-08 00:59:59834 LOG(INFO) << "RenderThread calling v8 IdleNotification for " << this;
835 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22836
837 // Schedule next invocation.
838 // Dampen the delay using the algorithm:
839 // delay = delay + 1 / (delay + 2)
840 // Using floor(delay) has a dampening effect such as:
841 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
842 // Note that idle_notification_delay_in_s_ would be reset to
843 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36844 ScheduleIdleHandler(idle_notification_delay_in_s_ +
845 1.0 / (idle_notification_delay_in_s_ + 2.0));
846 if (is_extension_process()) {
847 // Dampen the forced delay as well if the extension stays idle for long
848 // periods of time.
849 int64 forced_delay_s =
850 std::max(static_cast<int64>(idle_notification_delay_in_s_),
851 kMaxExtensionIdleHandlerDelayS);
852 forced_idle_timer_.Stop();
853 forced_idle_timer_.Start(
854 base::TimeDelta::FromSeconds(forced_delay_s),
855 this, &RenderThread::IdleHandler);
856 }
857}
[email protected]05001182009-09-15 23:34:22858
[email protected]71d6d852009-12-07 22:12:36859void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
860 idle_notification_delay_in_s_ = initial_delay_s;
861 idle_timer_.Stop();
862 idle_timer_.Start(
863 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
864 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03865}
866
[email protected]dfcb62a2009-06-17 19:32:43867void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
868 const ListValue& args) {
[email protected]7120f132009-07-20 21:05:37869 RendererExtensionBindings::Invoke(function_name, args, NULL);
[email protected]71d6d852009-12-07 22:12:36870
871 // Reset the idle handler each time there's any activity like event or message
872 // dispatch, for which Invoke is the chokepoint.
873 if (is_extension_process())
874 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]b68d5ed2009-04-16 02:41:28875}
[email protected]b547fd42009-04-23 23:16:27876
[email protected]fede6ca12009-10-08 18:24:26877void RenderThread::OnPurgeMemory() {
[email protected]85c55dc2009-11-06 03:05:46878 spellchecker_.reset(new SpellCheck());
[email protected]85c55dc2009-11-06 03:05:46879
[email protected]fede6ca12009-10-08 18:24:26880 EnsureWebKitInitialized();
881
882 // Clear the object cache (as much as possible; some live objects cannot be
883 // freed).
884 WebCache::clear();
885
886 // Clear the font/glyph cache.
887 WebFontCache::clear();
888
889 // Clear the Cross-Origin Preflight cache.
890 WebCrossOriginPreflightResultCache::clear();
891
[email protected]adf00bc2009-11-02 18:35:00892 // Release all freeable memory from the SQLite process-global page cache (a
893 // low-level object which backs the Connection-specific page caches).
[email protected]4a3dab22009-11-11 17:36:50894 while (sqlite3_release_memory(std::numeric_limits<int>::max()) > 0) {
895 }
[email protected]adf00bc2009-11-02 18:35:00896
[email protected]fede6ca12009-10-08 18:24:26897 // Repeatedly call the V8 idle notification until it returns true ("nothing
898 // more to free"). Note that it makes more sense to do this than to implement
899 // a new "delete everything" pass because object references make it difficult
900 // to free everything possible in just one pass.
[email protected]4a3dab22009-11-11 17:36:50901 while (!v8::V8::IdleNotification()) {
902 }
[email protected]fede6ca12009-10-08 18:24:26903
[email protected]61a9b2d82010-02-26 00:31:08904#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]fede6ca12009-10-08 18:24:26905 // Tell tcmalloc to release any free pages it's still holding.
906 MallocExtension::instance()->ReleaseFreeMemory();
907#endif
908}
909
[email protected]b78e168b2009-09-21 22:05:45910void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59911 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27912 // The call below will cause a GetPlugins call with refresh=true, but at this
913 // point we already know that the browser has refreshed its list, so disable
914 // refresh temporarily to prevent each renderer process causing the list to be
915 // regenerated.
916 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45917 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27918 plugin_refresh_allowed_ = true;
919}
[email protected]85c55dc2009-11-06 03:05:46920
[email protected]85c55dc2009-11-06 03:05:46921void RenderThread::OnInitSpellChecker(
[email protected]cb6037d2009-11-16 22:55:17922 IPC::PlatformFileForTransit bdict_file,
[email protected]85c55dc2009-11-06 03:05:46923 const std::vector<std::string>& custom_words,
924 const std::string& language,
925 bool auto_spell_correct) {
[email protected]cb6037d2009-11-16 22:55:17926 spellchecker_->Init(IPC::PlatformFileForTransitToPlatformFile(bdict_file),
927 custom_words, language);
[email protected]85c55dc2009-11-06 03:05:46928 spellchecker_->EnableAutoSpellCorrect(auto_spell_correct);
929}
930
931void RenderThread::OnSpellCheckWordAdded(const std::string& word) {
932 spellchecker_->WordAdded(word);
933}
934
935void RenderThread::OnSpellCheckEnableAutoSpellCorrect(bool enable) {
936 spellchecker_->EnableAutoSpellCorrect(enable);
937}
[email protected]b2a74ca2010-03-12 17:57:09938
939void RenderThread::OnSetIsIncognitoProcess(bool is_incognito_process) {
940 is_incognito_process_ = is_incognito_process;
941}