blob: 87155c3bf93697a5b553a4ab51733768953bf998 [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]da00a2882009-03-09 17:51:197#include <algorithm>
[email protected]61a9b2d82010-02-26 00:31:088#include <limits>
[email protected]75e126b932009-09-28 19:38:499#include <map>
[email protected]da00a2882009-03-09 17:51:1910#include <vector>
11
[email protected]adf00bc2009-11-02 18:35:0012#if defined(USE_SYSTEM_SQLITE)
13#include <sqlite3.h>
14#else
15#include "third_party/sqlite/preprocessed/sqlite3.h"
16#endif
17
[email protected]06533c0b2009-03-05 21:39:1118#include "base/command_line.h"
[email protected]94f9a0f682009-06-15 18:30:3019#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0320#include "base/logging.h"
[email protected]b7c7bcf2009-10-03 07:07:3421#include "base/nullable_string16.h"
[email protected]d41041092009-10-08 06:56:5722#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2923#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1924#include "base/stats_table.h"
[email protected]b7c7bcf2009-10-03 07:07:3425#include "base/string_util.h"
[email protected]94f9a0f682009-06-15 18:30:3026#include "base/thread_local.h"
[email protected]f430b5712009-08-21 21:46:3127#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]c8865962009-12-16 07:47:3928#include "chrome/common/child_process_logging.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]c1f50aa2010-02-18 03:46:5731#include "chrome/common/plugin_messages.h"
[email protected]e09ba552009-02-05 03:26:2932#include "chrome/common/render_messages.h"
[email protected]9b6f40e2009-06-11 15:54:2633#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4734#include "chrome/common/url_constants.h"
[email protected]2b437e232010-04-02 01:30:0835#include "chrome/common/web_database_observer_impl.h"
[email protected]8930d472009-02-21 08:05:2836#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5037// TODO(port)
38#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2939#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5040#else
[email protected]2c62b562009-01-27 19:04:5041#include "base/scoped_handle.h"
42#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5043#endif
[email protected]a9602de2010-03-18 23:43:1144#include "chrome/renderer/automation/dom_automation_v8_extension.h"
[email protected]dd9241932010-02-24 19:23:1345#include "chrome/renderer/cookie_message_filter.h"
[email protected]a8624712009-04-17 00:51:3546#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1547#include "chrome/renderer/extension_groups.h"
[email protected]a40caa972009-04-08 18:35:3448#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2749#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1750#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4351#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4252#include "chrome/renderer/external_extension.h"
[email protected]70c19a932010-05-14 12:59:1153#include "chrome/renderer/indexed_db_dispatcher.h"
[email protected]0bc46552009-04-07 21:56:4254#include "chrome/renderer/loadtimes_extension_bindings.h"
[email protected]3530cd92010-06-27 06:22:0155#include "chrome/renderer/net/renderer_net_predictor.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]70c19a932010-05-14 12:59:1160#include "chrome/renderer/renderer_webindexeddatabase_impl.h"
[email protected]8d86fce2009-02-26 23:37:5561#include "chrome/renderer/renderer_webkitclient_impl.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]9d797f32010-04-23 07:17:5466#include "net/base/net_util.h"
[email protected]1b1f3eb2009-12-01 13:48:0467#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]418ed5ab2009-11-12 01:14:4968#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"
69#include "third_party/WebKit/WebKit/chromium/public/WebColor.h"
70#include "third_party/WebKit/WebKit/chromium/public/WebCrossOriginPreflightResultCache.h"
71#include "third_party/WebKit/WebKit/chromium/public/WebDatabase.h"
72#include "third_party/WebKit/WebKit/chromium/public/WebFontCache.h"
[email protected]40bd6582009-12-04 23:49:5173#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
[email protected]418ed5ab2009-11-12 01:14:4974#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
75#include "third_party/WebKit/WebKit/chromium/public/WebRuntimeFeatures.h"
76#include "third_party/WebKit/WebKit/chromium/public/WebScriptController.h"
77#include "third_party/WebKit/WebKit/chromium/public/WebSecurityPolicy.h"
78#include "third_party/WebKit/WebKit/chromium/public/WebStorageEventDispatcher.h"
79#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
[email protected]b07f29092009-06-05 07:33:2180#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1181#include "webkit/extensions/v8/gears_extension.h"
82#include "webkit/extensions/v8/interval_extension.h"
83#include "webkit/extensions/v8/playback_extension.h"
[email protected]e0d92f282010-04-03 06:04:2784#include "v8/include/v8.h"
[email protected]2c62b562009-01-27 19:04:5085
[email protected]da00a2882009-03-09 17:51:1986#if defined(OS_WIN)
87#include <windows.h>
88#include <objbase.h>
89#endif
90
[email protected]fe819f52009-12-15 07:58:1191#if defined(OS_MACOSX)
92#include "chrome/app/breakpad_mac.h"
93#endif
94
[email protected]6217d392010-03-25 22:08:3595#if defined(OS_POSIX)
96#include "ipc/ipc_channel_posix.h"
97#endif
98
[email protected]2c434b32009-03-19 06:27:4799using WebKit::WebCache;
[email protected]fede6ca12009-10-08 18:24:26100using WebKit::WebCrossOriginPreflightResultCache;
101using WebKit::WebFontCache;
[email protected]f85f0702010-01-30 09:31:01102using WebKit::WebFrame;
[email protected]adf00bc2009-11-02 18:35:00103using WebKit::WebRuntimeFeatures;
[email protected]204758c2009-10-22 03:56:30104using WebKit::WebSecurityPolicy;
[email protected]98d7127b2009-10-23 18:26:51105using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:47106using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:34107using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:03108using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:29109
[email protected]42f1d7822009-07-23 18:17:55110namespace {
[email protected]2c434b32009-03-19 06:27:47111static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:03112static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
[email protected]71d6d852009-12-07 22:12:36113static const double kInitialExtensionIdleHandlerDelayS = 5.0 /* seconds */;
114static const int64 kMaxExtensionIdleHandlerDelayS = 5*60 /* seconds */;
[email protected]bee16aab2009-08-26 15:55:03115
[email protected]f3ede412010-06-21 22:52:16116// Keep the global RenderThread in a TLS slot so it is impossible to access
117// incorrectly from the wrong thread.
[email protected]94f9a0f682009-06-15 18:30:30118static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
119 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54120
[email protected]5fa1c542009-05-05 20:36:07121#if defined(OS_POSIX)
122class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
123 void OnChannelError() {
124 // On POSIX, at least, one can install an unload handler which loops
125 // forever and leave behind a renderer process which eats 100% CPU forever.
126 //
127 // This is because the terminate signals (ViewMsg_ShouldClose and the error
128 // from the IPC channel) are routed to the main message loop but never
129 // processed (because that message loop is stuck in V8).
130 //
131 // One could make the browser SIGKILL the renderers, but that leaves open a
132 // large window where a browser failure (or a user, manually terminating
133 // the browser because "it's stuck") will leave behind a process eating all
134 // the CPU.
135 //
136 // So, we install a filter on the channel so that we can process this event
137 // here and kill the process.
[email protected]fe819f52009-12-15 07:58:11138
139#if defined(OS_MACOSX)
140 // TODO(viettrungluu): crbug.com/28547: The following is needed, as a
141 // stopgap, to avoid leaking due to not releasing Breakpad properly.
142 // TODO(viettrungluu): Investigate why this is being called.
143 if (IsCrashReporterEnabled()) {
144 LOG(INFO) << "Cleaning up Breakpad.";
145 DestructCrashReporter();
146 } else {
147 LOG(INFO) << "Breakpad not enabled; no clean-up needed.";
148 }
149#endif // OS_MACOSX
150
[email protected]5fa1c542009-05-05 20:36:07151 _exit(0);
152 }
153};
154#endif
[email protected]40bd6582009-12-04 23:49:51155
[email protected]f85f0702010-01-30 09:31:01156class RenderViewContentSettingsSetter : public RenderViewVisitor {
157 public:
[email protected]0314ae02010-04-08 09:18:29158 RenderViewContentSettingsSetter(const GURL& url,
[email protected]f85f0702010-01-30 09:31:01159 const ContentSettings& content_settings)
[email protected]0314ae02010-04-08 09:18:29160 : url_(url),
[email protected]f85f0702010-01-30 09:31:01161 content_settings_(content_settings) {
162 }
163
164 virtual bool Visit(RenderView* render_view) {
[email protected]0314ae02010-04-08 09:18:29165 if (GURL(render_view->webview()->mainFrame()->url()) == url_)
[email protected]433819d2010-01-30 20:20:01166 render_view->SetContentSettings(content_settings_);
[email protected]f85f0702010-01-30 09:31:01167 return true;
168 }
169
170 private:
[email protected]0314ae02010-04-08 09:18:29171 GURL url_;
[email protected]f85f0702010-01-30 09:31:01172 ContentSettings content_settings_;
173
174 DISALLOW_COPY_AND_ASSIGN(RenderViewContentSettingsSetter);
175};
176
[email protected]40bd6582009-12-04 23:49:51177class RenderViewZoomer : public RenderViewVisitor {
178 public:
[email protected]9d797f32010-04-23 07:17:54179 RenderViewZoomer(const GURL& url, int zoom_level)
180 : zoom_level_(zoom_level) {
181 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51182 }
183
184 virtual bool Visit(RenderView* render_view) {
185 WebView* webview = render_view->webview(); // Guaranteed non-NULL.
[email protected]9d797f32010-04-23 07:17:54186 if (net::GetHostOrSpecFromURL(GURL(webview->mainFrame()->url())) == host_)
[email protected]40bd6582009-12-04 23:49:51187 webview->setZoomLevel(false, zoom_level_);
188 return true;
189 }
190
191 private:
192 std::string host_;
193 int zoom_level_;
194
195 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
196};
[email protected]42f1d7822009-07-23 18:17:55197} // namespace
198
199// When we run plugins in process, we actually run them on the render thread,
200// which means that we need to make the render thread pump UI events.
201RenderThread::RenderThread() {
202 Init();
203}
204
205RenderThread::RenderThread(const std::string& channel_name)
206 : ChildThread(channel_name) {
207 Init();
208}
[email protected]5fa1c542009-05-05 20:36:07209
initial.commit09911bf2008-07-26 23:55:29210void RenderThread::Init() {
[email protected]94f9a0f682009-06-15 18:30:30211 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50212#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34213 // If you are running plugins in this thread you need COM active but in
214 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28215 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34216 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50217#endif
initial.commit09911bf2008-07-26 23:55:29218
[email protected]71d6d852009-12-07 22:12:36219 std::string type_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
220 switches::kProcessType);
[email protected]31f87132010-04-21 23:36:21221 // In single process the single process is all there is.
222 is_extension_process_ = type_str == switches::kExtensionProcess ||
223 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
[email protected]b2a74ca2010-03-12 17:57:09224 is_incognito_process_ = false;
[email protected]80fc08c52010-03-09 07:43:50225 suspend_webkit_shared_timer_ = true;
226 notify_webkit_of_modal_loop_ = true;
[email protected]c1f50aa2010-02-18 03:46:57227 did_notify_webkit_of_modal_loop_ = false;
[email protected]42f1d7822009-07-23 18:17:55228 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03229 cache_stats_task_pending_ = false;
230 widget_count_ = 0;
231 hidden_widget_count_ = 0;
[email protected]71d6d852009-12-07 22:12:36232 idle_notification_delay_in_s_ = is_extension_process_ ?
233 kInitialExtensionIdleHandlerDelayS : kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03234 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55235
[email protected]8d86fce2009-02-26 23:37:55236 visited_link_slave_.reset(new VisitedLinkSlave());
[email protected]ee9080642010-04-07 00:23:06237 user_script_slave_.reset(new UserScriptSlave());
[email protected]74be069e82010-06-25 00:12:49238 renderer_net_predictor_.reset(new RendererNetPredictor());
[email protected]8d86fce2009-02-26 23:37:55239 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31240 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]70c19a932010-05-14 12:59:11241 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13242 spellchecker_.reset(new SpellCheck());
243
[email protected]a8624712009-04-17 00:51:35244 devtools_agent_filter_ = new DevToolsAgentFilter();
245 AddFilter(devtools_agent_filter_.get());
[email protected]dd9241932010-02-24 19:23:13246
[email protected]017022b2009-07-27 23:06:34247 db_message_filter_ = new DBMessageFilter();
248 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13249
250 cookie_message_filter_ = new CookieMessageFilter();
251 AddFilter(cookie_message_filter_.get());
[email protected]5fa1c542009-05-05 20:36:07252
253#if defined(OS_POSIX)
254 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
255 AddFilter(suicide_on_channel_error_filter_.get());
256#endif
initial.commit09911bf2008-07-26 23:55:29257}
258
[email protected]42f1d7822009-07-23 18:17:55259RenderThread::~RenderThread() {
[email protected]12cbfda32010-01-30 01:04:25260 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08261 if (web_database_observer_impl_.get())
262 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25263
[email protected]8d86fce2009-02-26 23:37:55264 // Shutdown in reverse of the initialization order.
[email protected]017022b2009-07-27 23:06:34265 RemoveFilter(db_message_filter_.get());
266 db_message_filter_ = NULL;
[email protected]2b437e232010-04-02 01:30:08267 RemoveFilter(devtools_agent_filter_.get());
[email protected]12cbfda32010-01-30 01:04:25268
[email protected]42f1d7822009-07-23 18:17:55269 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22270 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22271
[email protected]94f9a0f682009-06-15 18:30:30272 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30273
[email protected]8d86fce2009-02-26 23:37:55274 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50275#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29276 // Clean up plugin channels before this thread goes away.
277 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34278 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28279 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34280 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50281#endif
initial.commit09911bf2008-07-26 23:55:29282}
283
[email protected]42f1d7822009-07-23 18:17:55284RenderThread* RenderThread::current() {
285 return lazy_tls.Pointer()->Get();
286}
287
[email protected]c1f50aa2010-02-18 03:46:57288int32 RenderThread::RoutingIDForCurrentContext() {
289 int32 routing_id = MSG_ROUTING_CONTROL;
290 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29291 WebFrame* frame = WebFrame::frameForCurrentContext();
292 if (frame) {
293 RenderView* view = RenderView::FromWebView(frame->view());
294 if (view)
295 routing_id = view->routing_id();
296 }
[email protected]c1f50aa2010-02-18 03:46:57297 } else {
298 DLOG(WARNING) << "Not called within a script context!";
299 }
300 return routing_id;
301}
302
303bool RenderThread::Send(IPC::Message* msg) {
[email protected]80fc08c52010-03-09 07:43:50304 // Certain synchronous messages can result in an app-modal cookie prompt.
305 // This could cause a complete hang of Chrome if a windowed plug-in is trying
306 // to communicate with the renderer thread since the browser's UI thread
307 // could be stuck (within a Windows API call) trying to synchronously
308 // communicate with the plug-in. The remedy is to pump messages on this
309 // thread while the cookie prompt is showing. This creates an opportunity
310 // for re-entrancy into WebKit, so we need to take care to disable callbacks,
311 // timers, and pending network loads that could trigger such callbacks.
[email protected]c1f50aa2010-02-18 03:46:57312
[email protected]80fc08c52010-03-09 07:43:50313 bool pumping_events = false, may_show_cookie_prompt = false;
314 if (msg->is_sync()) {
315 if (msg->is_caller_pumping_messages()) {
316 pumping_events = true;
317 } else {
318 switch (msg->type()) {
319 case ViewHostMsg_GetCookies::ID:
320 case ViewHostMsg_GetRawCookies::ID:
321 case ViewHostMsg_DOMStorageSetItem::ID:
322 case ViewHostMsg_SyncLoad::ID:
[email protected]0a1a45432010-03-31 08:09:45323 case ViewHostMsg_AllowDatabase::ID:
[email protected]80fc08c52010-03-09 07:43:50324 may_show_cookie_prompt = true;
325 pumping_events = true;
326 break;
327 }
328 }
[email protected]c1f50aa2010-02-18 03:46:57329 }
330
[email protected]80fc08c52010-03-09 07:43:50331 bool suspend_webkit_shared_timer = true; // default value
332 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57333
[email protected]80fc08c52010-03-09 07:43:50334 bool notify_webkit_of_modal_loop = true; // default value
335 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
336
337 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57338
339 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50340 // See ViewMsg_SignalCookiePromptEvent.
341 if (may_show_cookie_prompt) {
342 static_cast<IPC::SyncMessage*>(msg)->set_pump_messages_event(
343 cookie_message_filter_->pump_messages_event());
344 }
345
346 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57347 webkit_client_->SuspendSharedTimer();
348
349 // WebKit does not like nested calls to willEnterModalLoop.
350 // TODO(darin): Fix WebKit to allow nesting.
[email protected]80fc08c52010-03-09 07:43:50351 if (notify_webkit_of_modal_loop && !did_notify_webkit_of_modal_loop_) {
[email protected]c1f50aa2010-02-18 03:46:57352 WebView::willEnterModalLoop();
353 did_notify_webkit_of_modal_loop_ = true;
354 }
355
[email protected]80fc08c52010-03-09 07:43:50356 RenderWidget* widget =
357 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
358 if (widget) {
359 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57360 PluginChannelHost::Broadcast(
361 new PluginMsg_SignalModalDialogEvent(host_window));
362 }
363 }
364
365 bool rv = ChildThread::Send(msg);
366
367 if (pumping_events) {
368 if (host_window) {
369 PluginChannelHost::Broadcast(
370 new PluginMsg_ResetModalDialogEvent(host_window));
371 }
372
[email protected]80fc08c52010-03-09 07:43:50373 if (notify_webkit_of_modal_loop && did_notify_webkit_of_modal_loop_) {
[email protected]c1f50aa2010-02-18 03:46:57374 WebView::didExitModalLoop();
375 did_notify_webkit_of_modal_loop_ = false;
376 }
377
[email protected]80fc08c52010-03-09 07:43:50378 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57379 webkit_client_->ResumeSharedTimer();
[email protected]80fc08c52010-03-09 07:43:50380
381 // We may end up nesting calls to Send, so we defer the reset until we
382 // return to the top-most message loop.
383 if (may_show_cookie_prompt &&
384 cookie_message_filter_->pump_messages_event()->IsSignaled()) {
385 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
386 NewRunnableMethod(cookie_message_filter_.get(),
387 &CookieMessageFilter::ResetPumpMessagesEvent));
388 }
[email protected]c1f50aa2010-02-18 03:46:57389 }
390
391 return rv;
392}
393
394void RenderThread::AddRoute(int32 routing_id,
395 IPC::Channel::Listener* listener) {
396 widget_count_++;
397 return ChildThread::AddRoute(routing_id, listener);
398}
399
400void RenderThread::RemoveRoute(int32 routing_id) {
401 widget_count_--;
402 return ChildThread::RemoveRoute(routing_id);
403}
404
[email protected]42f1d7822009-07-23 18:17:55405void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
406 channel()->AddFilter(filter);
407}
408
409void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
410 channel()->RemoveFilter(filter);
411}
412
[email protected]bee16aab2009-08-26 15:55:03413void RenderThread::WidgetHidden() {
414 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50415 hidden_widget_count_++;
[email protected]b8f41a192010-04-19 18:25:04416 if (!is_extension_process_ &&
[email protected]71d6d852009-12-07 22:12:36417 widget_count_ && hidden_widget_count_ == widget_count_)
418 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03419}
420
421void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08422 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03423 hidden_widget_count_--;
[email protected]b8f41a192010-04-19 18:25:04424 if (!is_extension_process_)
[email protected]71d6d852009-12-07 22:12:36425 idle_timer_.Stop();
[email protected]bee16aab2009-08-26 15:55:03426}
427
[email protected]c1f50aa2010-02-18 03:46:57428void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50429 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57430}
431
432void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50433 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57434}
435
[email protected]42f1d7822009-07-23 18:17:55436void RenderThread::Resolve(const char* name, size_t length) {
[email protected]74be069e82010-06-25 00:12:49437 return renderer_net_predictor_->Resolve(name, length);
[email protected]42f1d7822009-07-23 18:17:55438}
439
440void RenderThread::SendHistograms(int sequence_number) {
441 return histogram_snapshots_->SendHistograms(sequence_number);
442}
443
[email protected]176aa482008-11-14 03:25:15444void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20445 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29446 visited_link_slave_->Init(table);
447}
448
[email protected]3e90d4a2009-07-03 17:38:39449void RenderThread::OnAddVisitedLinks(
450 const VisitedLinkSlave::Fingerprints& fingerprints) {
451 for (size_t i = 0; i < fingerprints.size(); ++i)
[email protected]50ae00ef2009-10-19 05:11:03452 WebView::updateVisitedLinkState(fingerprints[i]);
[email protected]3e90d4a2009-07-03 17:38:39453}
454
455void RenderThread::OnResetVisitedLinks() {
[email protected]50ae00ef2009-10-19 05:11:03456 WebView::resetVisitedLinkState();
[email protected]3e90d4a2009-07-03 17:38:39457}
458
[email protected]0314ae02010-04-08 09:18:29459void RenderThread::OnSetContentSettingsForCurrentURL(
460 const GURL& url,
[email protected]f85f0702010-01-30 09:31:01461 const ContentSettings& content_settings) {
[email protected]0314ae02010-04-08 09:18:29462 RenderViewContentSettingsSetter setter(url, content_settings);
[email protected]f85f0702010-01-30 09:31:01463 RenderView::ForEach(&setter);
464}
465
[email protected]9d797f32010-04-23 07:17:54466void RenderThread::OnSetZoomLevelForCurrentURL(const GURL& url,
467 int zoom_level) {
468 RenderViewZoomer zoomer(url, zoom_level);
[email protected]40bd6582009-12-04 23:49:51469 RenderView::ForEach(&zoomer);
470}
471
[email protected]b2a74ca2010-03-12 17:57:09472void RenderThread::OnUpdateUserScripts(base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20473 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]ee9080642010-04-07 00:23:06474 user_script_slave_->UpdateScripts(scripts);
[email protected]c8865962009-12-16 07:47:39475 UpdateActiveExtensions();
[email protected]1e0f70402008-10-16 23:57:47476}
477
[email protected]703e807a2009-03-28 19:56:51478void RenderThread::OnSetExtensionFunctionNames(
479 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04480 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51481}
482
[email protected]3a8eecb2010-04-22 23:56:30483void RenderThread::OnExtensionExtentsUpdated(
484 const ViewMsg_ExtensionExtentsUpdated_Params& params) {
485 extension_extents_.resize(params.extension_apps.size());
486 for (size_t i = 0; i < params.extension_apps.size(); ++i) {
487 extension_extents_[i].extension_id = params.extension_apps[i].first;
488 extension_extents_[i].web_extent = params.extension_apps[i].second;
489 }
490}
491
[email protected]45776222009-07-15 20:21:58492void RenderThread::OnPageActionsUpdated(
493 const std::string& extension_id,
494 const std::vector<std::string>& page_actions) {
495 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
496}
497
[email protected]cccf90932009-08-23 17:56:25498void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19499 const std::string& extension_id,
500 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25501 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
[email protected]71d6d852009-12-07 22:12:36502
503 // This is called when starting a new extension page, so start the idle
504 // handler ticking.
[email protected]71d6d852009-12-07 22:12:36505 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]c8865962009-12-16 07:47:39506
507 UpdateActiveExtensions();
[email protected]cccf90932009-08-23 17:56:25508}
509
510void RenderThread::OnExtensionSetHostPermissions(
511 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
512 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19513}
514
[email protected]db7331a2010-02-25 22:10:50515void RenderThread::OnExtensionSetIncognitoEnabled(
516 const std::string& extension_id, bool enabled) {
517 ExtensionProcessBindings::SetIncognitoEnabled(extension_id, enabled);
518}
519
[email protected]c61cc652009-11-04 05:44:40520void RenderThread::OnDOMStorageEvent(
521 const ViewMsg_DOMStorageEvent_Params& params) {
522 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34523 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]c61cc652009-11-04 05:44:40524 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key_,
[email protected]ee2be5b32009-11-05 09:13:12525 params.old_value_, params.new_value_, params.origin_, params.url_,
[email protected]c61cc652009-11-04 05:44:40526 params.storage_type_ == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34527}
528
[email protected]8930d472009-02-21 08:05:28529void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]70c19a932010-05-14 12:59:11530 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31531 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54532 return;
[email protected]70c19a932010-05-14 12:59:11533 if (indexed_db_dispatcher_->OnMessageReceived(msg))
534 return;
[email protected]1edc16b82009-04-07 17:45:54535
[email protected]8930d472009-02-21 08:05:28536 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
537 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39538 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
539 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]0314ae02010-04-08 09:18:29540 IPC_MESSAGE_HANDLER(ViewMsg_SetContentSettingsForCurrentURL,
541 OnSetContentSettingsForCurrentURL)
[email protected]9d797f32010-04-23 07:17:54542 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
543 OnSetZoomLevelForCurrentURL)
[email protected]b2a74ca2010-03-12 17:57:09544 IPC_MESSAGE_HANDLER(ViewMsg_SetIsIncognitoProcess, OnSetIsIncognitoProcess)
[email protected]8930d472009-02-21 08:05:28545 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55546 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28547 // TODO(port): removed from render_messages_internal.h;
548 // is there a new non-windows message I should add here?
549 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
550 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17551 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46552 OnGetRendererHistograms)
[email protected]d41041092009-10-08 06:56:57553#if defined(USE_TCMALLOC)
554 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererTcmalloc,
555 OnGetRendererTcmalloc)
556#endif
[email protected]38b48a82009-11-11 01:51:32557 IPC_MESSAGE_HANDLER(ViewMsg_GetV8HeapStats, OnGetV8HeapStats)
[email protected]8930d472009-02-21 08:05:28558 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
559 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02560 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28561 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28562 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
563 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43564 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
565 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51566 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
567 OnSetExtensionFunctionNames)
[email protected]3a8eecb2010-04-22 23:56:30568 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionExtentsUpdated,
569 OnExtensionExtentsUpdated)
[email protected]fede6ca12009-10-08 18:24:26570 IPC_MESSAGE_HANDLER(ViewMsg_PurgeMemory, OnPurgeMemory)
[email protected]b547fd42009-04-23 23:16:27571 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
572 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58573 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
574 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25575 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
576 OnExtensionSetAPIPermissions)
577 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
578 OnExtensionSetHostPermissions)
[email protected]db7331a2010-02-25 22:10:50579 IPC_MESSAGE_HANDLER(ViewMsg_Extension_ExtensionSetIncognitoEnabled,
580 OnExtensionSetIncognitoEnabled)
[email protected]b7c7bcf2009-10-03 07:07:34581 IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent,
582 OnDOMStorageEvent)
[email protected]d55aaa132009-09-28 21:08:04583#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21584 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04585 OnSetIPCLoggingEnabled)
586#endif
[email protected]85c55dc2009-11-06 03:05:46587 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_Init,
588 OnInitSpellChecker)
589 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_WordAdded,
590 OnSpellCheckWordAdded)
591 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_EnableAutoSpellCorrect,
592 OnSpellCheckEnableAutoSpellCorrect)
[email protected]6217d392010-03-25 22:08:35593 IPC_MESSAGE_HANDLER(ViewMsg_GpuChannelEstablished, OnGpuChannelEstablished)
[email protected]8930d472009-02-21 08:05:28594 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29595}
596
597void RenderThread::OnSetNextPageID(int32 next_page_id) {
598 // This should only be called at process initialization time, so we shouldn't
599 // have to worry about thread-safety.
600 RenderView::SetNextPageID(next_page_id);
601}
602
[email protected]b9ab10c2009-08-07 18:09:55603// Called when to register CSS Color name->system color mappings.
604// We update the colors one by one and then tell WebKit to refresh all render
605// views.
606void RenderThread::OnSetCSSColors(
607 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59608 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55609 size_t num_colors = colors.size();
610 scoped_array<WebKit::WebColorName> color_names(
611 new WebKit::WebColorName[num_colors]);
612 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
613 size_t i = 0;
614 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
615 colors.begin();
616 it != colors.end();
617 ++it, ++i) {
618 color_names[i] = it->first;
619 web_colors[i] = it->second;
620 }
621 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
622}
623
[email protected]4e6419c2010-01-15 04:50:34624void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47625 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33626 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29627 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26628 this,
629 params.parent_window,
630 MSG_ROUTING_NONE,
631 params.renderer_preferences,
632 params.web_preferences,
633 new SharedRenderViewCounter(0),
634 params.view_id,
635 params.session_storage_namespace_id,
636 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27637}
[email protected]4274e582009-01-27 22:09:56638
initial.commit09911bf2008-07-26 23:55:29639void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
640 size_t max_dead_capacity,
641 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47642 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47643 WebCache::setCapacities(
644 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29645}
646
647void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47648 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47649 WebCache::ResourceTypeStats stats;
650 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29651 Send(new ViewHostMsg_ResourceTypeStats(stats));
652}
653
[email protected]c9a3ef82009-05-28 22:02:46654void RenderThread::OnGetRendererHistograms(int sequence_number) {
655 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17656}
657
[email protected]d41041092009-10-08 06:56:57658#if defined(USE_TCMALLOC)
659void RenderThread::OnGetRendererTcmalloc() {
660 std::string result;
661 char buffer[1024 * 32];
[email protected]a4dc33f2009-10-20 15:09:55662 base::ProcessId pid = base::GetCurrentProcId();
[email protected]d41041092009-10-08 06:56:57663 MallocExtension::instance()->GetStats(buffer, sizeof(buffer));
664 result.append(buffer);
665 Send(new ViewHostMsg_RendererTcmalloc(pid, result));
666}
667#endif
668
[email protected]38b48a82009-11-11 01:51:32669void RenderThread::OnGetV8HeapStats() {
670 v8::HeapStatistics heap_stats;
671 v8::V8::GetHeapStatistics(&heap_stats);
672 Send(new ViewHostMsg_V8HeapStats(heap_stats.total_heap_size(),
673 heap_stats.used_heap_size()));
674}
675
initial.commit09911bf2008-07-26 23:55:29676void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47677 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47678 WebCache::UsageStats stats;
679 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29680 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03681 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29682}
683
684void RenderThread::InformHostOfCacheStatsLater() {
685 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03686 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29687 return;
688
[email protected]bee16aab2009-08-26 15:55:03689 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29690 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03691 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29692 &RenderThread::InformHostOfCacheStats),
693 kCacheStatsDelayMS);
694}
[email protected]90a3fbb12009-02-28 01:13:47695
[email protected]c40acc32010-01-14 01:02:53696void RenderThread::CloseCurrentConnections() {
697 Send(new ViewHostMsg_CloseCurrentConnections());
[email protected]b07f29092009-06-05 07:33:21698}
699
700void RenderThread::SetCacheMode(bool enabled) {
701 Send(new ViewHostMsg_SetCacheMode(enabled));
702}
703
[email protected]c5d79342010-06-05 01:27:34704void RenderThread::ClearCache() {
705 int rv;
706 Send(new ViewHostMsg_ClearCache(&rv));
707}
708
[email protected]c8865962009-12-16 07:47:39709void RenderThread::UpdateActiveExtensions() {
710 // In single-process mode, the browser process reports the active extensions.
711 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
712 return;
713
714 std::set<std::string> active_extensions;
715 user_script_slave_->GetActiveExtensions(&active_extensions);
716 ExtensionProcessBindings::GetActiveExtensions(&active_extensions);
717 child_process_logging::SetActiveExtensions(active_extensions);
718}
719
[email protected]6217d392010-03-25 22:08:35720void RenderThread::EstablishGpuChannel() {
721 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22722 // Do nothing if we already have a GPU channel or are already
723 // establishing one.
724 if (gpu_channel_->state() == GpuChannelHost::UNCONNECTED ||
725 gpu_channel_->state() == GpuChannelHost::CONNECTED)
[email protected]6217d392010-03-25 22:08:35726 return;
727
728 // Recreate the channel if it has been lost.
729 if (gpu_channel_->state() == GpuChannelHost::LOST)
730 gpu_channel_ = NULL;
731 }
732
733 if (!gpu_channel_.get())
734 gpu_channel_ = new GpuChannelHost;
735
736 // Ask the browser for the channel name.
737 Send(new ViewHostMsg_EstablishGpuChannel());
738}
739
[email protected]3bf4d532010-03-27 00:23:34740GpuChannelHost* RenderThread::EstablishGpuChannelSync() {
[email protected]1082b1d2010-03-30 00:31:22741 // We may need to retry the connection establishment if an existing
742 // connection has gone bad, which will not be detected in
743 // EstablishGpuChannel since we do not send duplicate
744 // ViewHostMsg_EstablishGpuChannel messages -- doing so breaks the
745 // preexisting connection in bad ways.
746 bool retry = true;
747 for (int i = 0; i < 2 && retry; ++i) {
748 EstablishGpuChannel();
749 retry = !Send(new ViewHostMsg_SynchronizeGpu());
750 }
[email protected]3bf4d532010-03-27 00:23:34751 // TODO(kbr): the GPU channel is still in the unconnected state at this point.
752 // Need to figure out whether it is really safe to return it.
753 return gpu_channel_.get();
754}
755
[email protected]6217d392010-03-25 22:08:35756GpuChannelHost* RenderThread::GetGpuChannel() {
757 if (!gpu_channel_.get())
758 return NULL;
759
760 if (gpu_channel_->state() != GpuChannelHost::CONNECTED)
761 return NULL;
762
763 return gpu_channel_.get();
764}
765
[email protected]3df0c202009-03-31 23:51:26766static void* CreateHistogram(
767 const char *name, int min, int max, size_t buckets) {
[email protected]e8829a192009-12-06 00:09:37768 if (min <= 0)
769 min = 1;
[email protected]2753b392009-12-28 06:59:52770 scoped_refptr<Histogram> histogram = Histogram::FactoryGet(
771 name, min, max, buckets, Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:37772 // We'll end up leaking these histograms, unless there is some code hiding in
773 // there to do the dec-ref.
[email protected]2753b392009-12-28 06:59:52774 // TODO(jar): Handle reference counting in webkit glue.
[email protected]e8829a192009-12-06 00:09:37775 histogram->AddRef();
776 return histogram.get();
[email protected]3df0c202009-03-31 23:51:26777}
778
779static void AddHistogramSample(void* hist, int sample) {
780 Histogram* histogram = static_cast<Histogram *>(hist);
781 histogram->Add(sample);
782}
783
[email protected]90a3fbb12009-02-28 01:13:47784void RenderThread::EnsureWebKitInitialized() {
785 if (webkit_client_.get())
786 return;
[email protected]da00a2882009-03-09 17:51:19787
[email protected]71d6d852009-12-07 22:12:36788 // For extensions, we want to ensure we call the IdleHandler every so often,
789 // even if the extension keeps up activity.
[email protected]b8f41a192010-04-19 18:25:04790 if (is_extension_process_) {
[email protected]71d6d852009-12-07 22:12:36791 forced_idle_timer_.Start(
792 base::TimeDelta::FromSeconds(kMaxExtensionIdleHandlerDelayS),
793 this, &RenderThread::IdleHandler);
794 }
795
[email protected]da00a2882009-03-09 17:51:19796 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26797 v8::V8::SetCreateHistogramFunction(CreateHistogram);
798 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19799
[email protected]90a3fbb12009-02-28 01:13:47800 webkit_client_.reset(new RendererWebKitClientImpl);
801 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44802
[email protected]98d7127b2009-10-23 18:26:51803 WebScriptController::enableV8SingleThreadMode();
[email protected]4ea00e82009-07-30 22:59:34804
[email protected]60e448982009-05-06 04:21:16805 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44806 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16807 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47808 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]204758c2009-10-22 03:56:30809 WebSecurityPolicy::registerURLSchemeAsLocal(chrome_ui_scheme);
810 WebSecurityPolicy::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19811
[email protected]b4e75c12010-05-18 18:28:48812 // chrome-extension: resources shouldn't trigger insecure content warnings.
[email protected]37e5a9a2010-03-13 09:42:39813 WebString extension_scheme(ASCIIToUTF16(chrome::kExtensionScheme));
814 WebSecurityPolicy::registerURLSchemeAsSecure(extension_scheme);
815
[email protected]6cf8b1a22009-05-26 23:36:00816#if defined(OS_WIN)
817 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]98d7127b2009-10-23 18:26:51818 WebScriptController::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00819#endif
[email protected]98d7127b2009-10-23 18:26:51820 WebScriptController::registerExtension(
821 extensions_v8::IntervalExtension::Get());
822 WebScriptController::registerExtension(
823 extensions_v8::LoadTimesExtension::Get());
824 WebScriptController::registerExtension(
825 extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27826
[email protected]ad1f9bd2009-07-30 20:23:15827 const WebKit::WebString kExtensionScheme =
828 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
829
[email protected]98d7127b2009-10-23 18:26:51830 WebScriptController::registerExtension(
831 ExtensionProcessBindings::Get(), kExtensionScheme);
[email protected]ad1f9bd2009-07-30 20:23:15832
[email protected]98d7127b2009-10-23 18:26:51833 WebScriptController::registerExtension(
834 BaseJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
835 WebScriptController::registerExtension(
836 BaseJsV8Extension::Get(), kExtensionScheme);
837 WebScriptController::registerExtension(
838 JsonSchemaJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
839 WebScriptController::registerExtension(JsonSchemaJsV8Extension::Get(),
840 kExtensionScheme);
841 WebScriptController::registerExtension(
842 EventBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
843 WebScriptController::registerExtension(EventBindings::Get(),
844 kExtensionScheme);
845 WebScriptController::registerExtension(
846 RendererExtensionBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
847 WebScriptController::registerExtension(
848 RendererExtensionBindings::Get(), kExtensionScheme);
849 WebScriptController::registerExtension(
850 ExtensionApiTestV8Extension::Get(), kExtensionScheme);
851 WebScriptController::registerExtension(
852 ExtensionApiTestV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04853
[email protected]2b437e232010-04-02 01:30:08854 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(this));
855 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
[email protected]e2b2d4a2009-10-24 03:32:59856
[email protected]06533c0b2009-03-05 21:39:11857 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04858
[email protected]98d7127b2009-10-23 18:26:51859 if (command_line.HasSwitch(switches::kEnableBenchmarking)) {
860 WebScriptController::registerExtension(
861 extensions_v8::BenchmarkingExtension::Get());
862 }
[email protected]b07f29092009-06-05 07:33:21863
[email protected]06533c0b2009-03-05 21:39:11864 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18865 command_line.HasSwitch(switches::kRecordMode) ||
866 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]98d7127b2009-10-23 18:26:51867 WebScriptController::registerExtension(
868 extensions_v8::PlaybackExtension::Get());
[email protected]06533c0b2009-03-05 21:39:11869 }
[email protected]2cb82332009-03-18 17:24:55870
[email protected]a9602de2010-03-18 23:43:11871 if (command_line.HasSwitch(switches::kDomAutomationController)) {
872 WebScriptController::registerExtension(DomAutomationV8Extension::Get());
873 }
874
[email protected]b1b13212009-10-30 06:42:01875 WebRuntimeFeatures::enableMediaPlayer(
[email protected]00c39612010-03-06 02:53:28876 RenderProcess::current()->HasInitializedMediaLibrary());
[email protected]3553c6cc2009-10-20 05:51:55877
[email protected]b1b13212009-10-30 06:42:01878 WebRuntimeFeatures::enableSockets(
[email protected]cdf24782009-11-05 09:32:47879 !command_line.HasSwitch(switches::kDisableWebSockets));
[email protected]0afb9932009-10-24 00:37:58880
[email protected]b1b13212009-10-30 06:42:01881 WebRuntimeFeatures::enableDatabase(
[email protected]90a7ad522009-11-20 22:42:53882 !command_line.HasSwitch(switches::kDisableDatabases));
[email protected]bfa69d492009-10-31 03:27:19883
[email protected]13bc0d822009-11-07 01:17:14884 WebRuntimeFeatures::enableApplicationCache(
[email protected]425c270642010-01-26 20:42:37885 !command_line.HasSwitch(switches::kDisableApplicationCache));
[email protected]13bc0d822009-11-07 01:17:14886
[email protected]bfa69d492009-10-31 03:27:19887 WebRuntimeFeatures::enableNotifications(
[email protected]380f1862009-11-14 02:48:01888 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
[email protected]7ee66e62009-11-05 09:16:30889
890 WebRuntimeFeatures::enableLocalStorage(
[email protected]5cf18362009-11-06 18:45:01891 !command_line.HasSwitch(switches::kDisableLocalStorage));
[email protected]7ee66e62009-11-05 09:16:30892 WebRuntimeFeatures::enableSessionStorage(
[email protected]03567802010-02-24 11:48:53893 !command_line.HasSwitch(switches::kDisableSessionStorage));
[email protected]2236bd1f2010-01-20 15:17:08894
[email protected]582fab732010-02-02 02:50:28895 WebRuntimeFeatures::enableIndexedDatabase(
896 command_line.HasSwitch(switches::kEnableIndexedDatabase));
897
[email protected]2236bd1f2010-01-20 15:17:08898 WebRuntimeFeatures::enableGeolocation(
[email protected]d0569f42010-04-01 10:48:18899 !command_line.HasSwitch(switches::kDisableGeolocation));
[email protected]7a716c82010-03-03 10:33:44900
901 WebRuntimeFeatures::enableWebGL(
902 command_line.HasSwitch(switches::kEnableExperimentalWebGL));
[email protected]ccbe04e2010-03-17 17:58:43903
904 WebRuntimeFeatures::enablePushState(true);
[email protected]f2e3d6c2010-04-30 14:23:04905
906 WebRuntimeFeatures::enableTouch(
907 command_line.HasSwitch(switches::kEnableTouch));
[email protected]90a3fbb12009-02-28 01:13:47908}
[email protected]75e5a872009-04-02 23:56:11909
[email protected]bee16aab2009-08-26 15:55:03910void RenderThread::IdleHandler() {
[email protected]61a9b2d82010-02-26 00:31:08911#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41912 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22913#endif
[email protected]bee16aab2009-08-26 15:55:03914
[email protected]f5e4b9bf2009-10-08 00:59:59915 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22916
917 // Schedule next invocation.
918 // Dampen the delay using the algorithm:
919 // delay = delay + 1 / (delay + 2)
920 // Using floor(delay) has a dampening effect such as:
921 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
922 // Note that idle_notification_delay_in_s_ would be reset to
923 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36924 ScheduleIdleHandler(idle_notification_delay_in_s_ +
925 1.0 / (idle_notification_delay_in_s_ + 2.0));
[email protected]b8f41a192010-04-19 18:25:04926 if (is_extension_process_) {
[email protected]71d6d852009-12-07 22:12:36927 // Dampen the forced delay as well if the extension stays idle for long
928 // periods of time.
929 int64 forced_delay_s =
930 std::max(static_cast<int64>(idle_notification_delay_in_s_),
931 kMaxExtensionIdleHandlerDelayS);
932 forced_idle_timer_.Stop();
933 forced_idle_timer_.Start(
934 base::TimeDelta::FromSeconds(forced_delay_s),
935 this, &RenderThread::IdleHandler);
936 }
937}
[email protected]05001182009-09-15 23:34:22938
[email protected]71d6d852009-12-07 22:12:36939void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
940 idle_notification_delay_in_s_ = initial_delay_s;
941 idle_timer_.Stop();
942 idle_timer_.Start(
943 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
944 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03945}
946
[email protected]dfcb62a2009-06-17 19:32:43947void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
[email protected]d7259472010-03-24 08:40:49948 const ListValue& args,
[email protected]a807bbe2010-04-14 10:51:19949 bool requires_incognito_access,
950 const GURL& event_url) {
951 RendererExtensionBindings::Invoke(
952 function_name, args, NULL, requires_incognito_access, event_url);
[email protected]71d6d852009-12-07 22:12:36953
954 // Reset the idle handler each time there's any activity like event or message
955 // dispatch, for which Invoke is the chokepoint.
[email protected]b8f41a192010-04-19 18:25:04956 if (is_extension_process_)
[email protected]71d6d852009-12-07 22:12:36957 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]b68d5ed2009-04-16 02:41:28958}
[email protected]b547fd42009-04-23 23:16:27959
[email protected]fede6ca12009-10-08 18:24:26960void RenderThread::OnPurgeMemory() {
[email protected]85c55dc2009-11-06 03:05:46961 spellchecker_.reset(new SpellCheck());
[email protected]85c55dc2009-11-06 03:05:46962
[email protected]fede6ca12009-10-08 18:24:26963 EnsureWebKitInitialized();
964
965 // Clear the object cache (as much as possible; some live objects cannot be
966 // freed).
967 WebCache::clear();
968
969 // Clear the font/glyph cache.
970 WebFontCache::clear();
971
972 // Clear the Cross-Origin Preflight cache.
973 WebCrossOriginPreflightResultCache::clear();
974
[email protected]adf00bc2009-11-02 18:35:00975 // Release all freeable memory from the SQLite process-global page cache (a
976 // low-level object which backs the Connection-specific page caches).
[email protected]4a3dab22009-11-11 17:36:50977 while (sqlite3_release_memory(std::numeric_limits<int>::max()) > 0) {
978 }
[email protected]adf00bc2009-11-02 18:35:00979
[email protected]fede6ca12009-10-08 18:24:26980 // Repeatedly call the V8 idle notification until it returns true ("nothing
981 // more to free"). Note that it makes more sense to do this than to implement
982 // a new "delete everything" pass because object references make it difficult
983 // to free everything possible in just one pass.
[email protected]4a3dab22009-11-11 17:36:50984 while (!v8::V8::IdleNotification()) {
985 }
[email protected]fede6ca12009-10-08 18:24:26986
[email protected]61a9b2d82010-02-26 00:31:08987#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]fede6ca12009-10-08 18:24:26988 // Tell tcmalloc to release any free pages it's still holding.
989 MallocExtension::instance()->ReleaseFreeMemory();
990#endif
991}
992
[email protected]b78e168b2009-09-21 22:05:45993void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59994 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27995 // The call below will cause a GetPlugins call with refresh=true, but at this
996 // point we already know that the browser has refreshed its list, so disable
997 // refresh temporarily to prevent each renderer process causing the list to be
998 // regenerated.
999 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:451000 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:271001 plugin_refresh_allowed_ = true;
1002}
[email protected]85c55dc2009-11-06 03:05:461003
[email protected]85c55dc2009-11-06 03:05:461004void RenderThread::OnInitSpellChecker(
[email protected]cb6037d2009-11-16 22:55:171005 IPC::PlatformFileForTransit bdict_file,
[email protected]85c55dc2009-11-06 03:05:461006 const std::vector<std::string>& custom_words,
1007 const std::string& language,
1008 bool auto_spell_correct) {
[email protected]cb6037d2009-11-16 22:55:171009 spellchecker_->Init(IPC::PlatformFileForTransitToPlatformFile(bdict_file),
1010 custom_words, language);
[email protected]85c55dc2009-11-06 03:05:461011 spellchecker_->EnableAutoSpellCorrect(auto_spell_correct);
1012}
1013
1014void RenderThread::OnSpellCheckWordAdded(const std::string& word) {
1015 spellchecker_->WordAdded(word);
1016}
1017
1018void RenderThread::OnSpellCheckEnableAutoSpellCorrect(bool enable) {
1019 spellchecker_->EnableAutoSpellCorrect(enable);
1020}
[email protected]b2a74ca2010-03-12 17:57:091021
1022void RenderThread::OnSetIsIncognitoProcess(bool is_incognito_process) {
1023 is_incognito_process_ = is_incognito_process;
1024}
[email protected]6217d392010-03-25 22:08:351025
1026void RenderThread::OnGpuChannelEstablished(
1027 const IPC::ChannelHandle& channel_handle) {
1028#if defined(OS_POSIX)
1029 // If we received a ChannelHandle, register it now.
1030 if (channel_handle.socket.fd >= 0)
1031 IPC::AddChannelSocket(channel_handle.name, channel_handle.socket.fd);
1032#endif
1033
1034 if (channel_handle.name.size() != 0) {
1035 // Connect to the GPU process if a channel name was received.
1036 gpu_channel_->Connect(channel_handle.name);
1037 } else {
1038 // Otherwise cancel the connection.
1039 gpu_channel_ = NULL;
1040 }
1041}
[email protected]3a8eecb2010-04-22 23:56:301042
1043std::string RenderThread::GetExtensionIdForURL(const GURL& url) {
1044 if (url.SchemeIs(chrome::kExtensionScheme))
1045 return url.host();
1046
1047 for (size_t i = 0; i < extension_extents_.size(); ++i) {
1048 if (extension_extents_[i].web_extent.ContainsURL(url))
1049 return extension_extents_[i].extension_id;
1050 }
1051
1052 return std::string();
1053}