blob: e93357367fefa2d1bffa5ff62ece9b9b6d3d8b00 [file] [log] [blame]
[email protected]7785b17f2011-03-23 01:20:281// Copyright (c) 2011 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
[email protected]10e6ab572011-04-14 23:42:005#include "content/renderer/render_thread.h"
initial.commit09911bf2008-07-26 23:55:296
[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]06533c0b2009-03-05 21:39:1112#include "base/command_line.h"
[email protected]58580352010-10-26 04:07:5013#include "base/debug/trace_event.h"
[email protected]94f9a0f682009-06-15 18:30:3014#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0315#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3816#include "base/metrics/field_trial.h"
[email protected]6cf19311f2011-04-14 23:06:0217#include "base/metrics/histogram.h"
[email protected]835d7c82010-10-14 04:38:3818#include "base/metrics/stats_table.h"
[email protected]d41041092009-10-08 06:56:5719#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2920#include "base/shared_memory.h"
[email protected]46f36a492010-07-28 19:36:4121#include "base/task.h"
[email protected]1357c322010-12-30 22:18:5622#include "base/threading/thread_local.h"
[email protected]7a4de7a62010-08-17 18:38:2423#include "base/values.h"
[email protected]e93e04e2011-03-14 00:27:1024#include "content/common/appcache/appcache_dispatcher.h"
[email protected]38b592902011-04-16 02:08:4225#include "content/common/content_switches.h"
[email protected]37666cf2011-03-13 21:51:4226#include "content/common/database_messages.h"
27#include "content/common/db_message_filter.h"
[email protected]127dd582011-03-16 21:32:1028#include "content/common/dom_storage_messages.h"
[email protected]202b54ff2011-04-22 21:36:3829#include "content/common/gpu/gpu_messages.h"
[email protected]105303e2011-03-14 22:16:1030#include "content/common/plugin_messages.h"
[email protected]60916042011-03-19 00:43:3631#include "content/common/renderer_preferences.h"
[email protected]94dc971d2011-03-05 19:08:3232#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5033#include "content/common/view_messages.h"
[email protected]e93e04e2011-03-14 00:27:1034#include "content/common/web_database_observer_impl.h"
[email protected]3c5c6d82011-03-16 17:23:5835#include "content/plugin/npobject_util.h"
[email protected]6779aa12011-03-29 17:32:2436#include "content/renderer/content_renderer_client.h"
[email protected]a03a2222011-05-25 21:26:4037#include "content/renderer/gpu/gpu_channel_host.h"
38#include "content/renderer/gpu/gpu_video_service_host.h"
[email protected]230b7ef2011-03-16 22:30:1939#include "content/renderer/indexed_db_dispatcher.h"
[email protected]f7eb0a392011-07-12 10:19:5140#include "content/renderer/media/audio_input_message_filter.h"
41#include "content/renderer/media/audio_message_filter.h"
[email protected]80b161a2011-06-27 17:42:1142#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3443#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3544#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0545#include "content/renderer/render_process_impl.h"
[email protected]1223d6ef2011-03-28 16:47:5046#include "content/renderer/render_process_observer.h"
[email protected]60916042011-03-19 00:43:3647#include "content/renderer/render_view.h"
48#include "content/renderer/render_view_visitor.h"
[email protected]acb94722011-03-18 01:33:3449#include "content/renderer/renderer_webidbfactory_impl.h"
50#include "content/renderer/renderer_webkitclient_impl.h"
[email protected]46f36a492010-07-28 19:36:4151#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1752#include "ipc/ipc_platform_file.h"
[email protected]620161e2011-03-07 18:05:2653#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5454#include "net/base/net_util.h"
[email protected]1b1f3eb2009-12-01 13:48:0455#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]8bd0fe62011-01-17 06:44:3756#include "third_party/WebKit/Source/WebKit/chromium/public/WebColor.h"
[email protected]8bd0fe62011-01-17 06:44:3757#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
58#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3759#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
60#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0961#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]8bd0fe62011-01-17 06:44:3762#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
63#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]8bd0fe62011-01-17 06:44:3764#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
65#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
66#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]7f3a2cf2011-04-06 00:10:5067#include "v8/include/v8.h"
[email protected]b07f29092009-06-05 07:33:2168#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1169#include "webkit/extensions/v8/playback_extension.h"
[email protected]d471190a2011-02-16 14:52:3070#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5071
[email protected]94dc971d2011-03-05 19:08:3272// TODO(port)
73#if defined(OS_WIN)
[email protected]3c5c6d82011-03-16 17:23:5874#include "content/plugin/plugin_channel.h"
[email protected]94dc971d2011-03-05 19:08:3275#else
[email protected]3b63f8f42011-03-28 01:54:1576#include "base/memory/scoped_handle.h"
[email protected]3c5c6d82011-03-16 17:23:5877#include "content/plugin/plugin_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3278#endif
79
[email protected]da00a2882009-03-09 17:51:1980#if defined(OS_WIN)
81#include <windows.h>
82#include <objbase.h>
83#endif
84
[email protected]6217d392010-03-25 22:08:3585#if defined(OS_POSIX)
86#include "ipc/ipc_channel_posix.h"
87#endif
88
[email protected]b6cb3a842011-06-24 18:28:4189using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:0190using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:0991using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:0092using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:5193using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:4794using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:3495using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:0396using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:2997
[email protected]42f1d7822009-07-23 18:17:5598namespace {
[email protected]bee16aab2009-08-26 15:55:0399static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
100
[email protected]f3ede412010-06-21 22:52:16101// Keep the global RenderThread in a TLS slot so it is impossible to access
102// incorrectly from the wrong thread.
[email protected]94f9a0f682009-06-15 18:30:30103static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
104 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54105
[email protected]40bd6582009-12-04 23:49:51106class RenderViewZoomer : public RenderViewVisitor {
107 public:
[email protected]b75b8292010-10-01 07:28:25108 RenderViewZoomer(const GURL& url, double zoom_level)
[email protected]9d797f32010-04-23 07:17:54109 : zoom_level_(zoom_level) {
110 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51111 }
112
113 virtual bool Visit(RenderView* render_view) {
[email protected]b6cb3a842011-06-24 18:28:41114 WebView* webview = render_view->webview();
115 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25116
117 // Don't set zoom level for full-page plugin since they don't use the same
118 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41119 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25120 return true;
121
[email protected]b6cb3a842011-06-24 18:28:41122 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51123 webview->setZoomLevel(false, zoom_level_);
124 return true;
125 }
126
127 private:
128 std::string host_;
[email protected]b75b8292010-10-01 07:28:25129 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51130
131 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
132};
[email protected]0478d0162010-08-28 08:29:40133
[email protected]42f1d7822009-07-23 18:17:55134} // namespace
135
136// When we run plugins in process, we actually run them on the render thread,
137// which means that we need to make the render thread pump UI events.
138RenderThread::RenderThread() {
139 Init();
140}
141
142RenderThread::RenderThread(const std::string& channel_name)
143 : ChildThread(channel_name) {
144 Init();
145}
[email protected]5fa1c542009-05-05 20:36:07146
initial.commit09911bf2008-07-26 23:55:29147void RenderThread::Init() {
[email protected]366ae242011-05-10 02:23:58148 TRACE_EVENT_BEGIN_ETW("RenderThread::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33149
[email protected]53c607c2011-03-21 23:19:04150#if defined(OS_MACOSX)
151 // On Mac, the select popups are rendered by the browser.
152 WebKit::WebView::setUseExternalPopupMenus(true);
153#endif
154
[email protected]94f9a0f682009-06-15 18:30:30155 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50156#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34157 // If you are running plugins in this thread you need COM active but in
158 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28159 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34160 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50161#endif
initial.commit09911bf2008-07-26 23:55:29162
[email protected]31f87132010-04-21 23:36:21163 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50164 suspend_webkit_shared_timer_ = true;
165 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55166 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03167 widget_count_ = 0;
168 hidden_widget_count_ = 0;
[email protected]6779aa12011-03-29 17:32:24169 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03170 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55171
[email protected]f430b5712009-08-21 21:46:31172 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]70c19a932010-05-14 12:59:11173 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13174
[email protected]017022b2009-07-27 23:06:34175 db_message_filter_ = new DBMessageFilter();
176 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13177
[email protected]80b161a2011-06-27 17:42:11178 vc_manager_ = new VideoCaptureImplManager();
179 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46180
[email protected]f7eb0a392011-07-12 10:19:51181 audio_input_message_filter_ = new AudioInputMessageFilter();
182 AddFilter(audio_input_message_filter_.get());
183
184 audio_message_filter_ = new AudioMessageFilter();
185 AddFilter(audio_message_filter_.get());
186
[email protected]8d97ade2011-04-14 18:17:08187 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24188
[email protected]366ae242011-05-10 02:23:58189 TRACE_EVENT_END_ETW("RenderThread::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29190}
191
[email protected]42f1d7822009-07-23 18:17:55192RenderThread::~RenderThread() {
[email protected]1223d6ef2011-03-28 16:47:50193 FOR_EACH_OBSERVER(
194 RenderProcessObserver, observers_, OnRenderProcessShutdown());
195
[email protected]12cbfda32010-01-30 01:04:25196 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08197 if (web_database_observer_impl_.get())
198 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25199
[email protected]8d86fce2009-02-26 23:37:55200 // Shutdown in reverse of the initialization order.
[email protected]f7eb0a392011-07-12 10:19:51201 RemoveFilter(audio_input_message_filter_.get());
202 audio_input_message_filter_ = NULL;
203
204 RemoveFilter(audio_message_filter_.get());
205 audio_message_filter_ = NULL;
206
[email protected]80b161a2011-06-27 17:42:11207 RemoveFilter(vc_manager_->video_capture_message_filter());
208
[email protected]017022b2009-07-27 23:06:34209 RemoveFilter(db_message_filter_.get());
210 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25211
[email protected]c6a7b862010-08-20 22:19:38212 // Shutdown the file thread if it's running.
213 if (file_thread_.get())
214 file_thread_->Stop();
215
[email protected]42f1d7822009-07-23 18:17:55216 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22217 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22218
[email protected]94f9a0f682009-06-15 18:30:30219 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30220
[email protected]8d86fce2009-02-26 23:37:55221 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50222#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29223 // Clean up plugin channels before this thread goes away.
224 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34225 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28226 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34227 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50228#endif
initial.commit09911bf2008-07-26 23:55:29229}
230
[email protected]42f1d7822009-07-23 18:17:55231RenderThread* RenderThread::current() {
232 return lazy_tls.Pointer()->Get();
233}
234
[email protected]c1f50aa2010-02-18 03:46:57235int32 RenderThread::RoutingIDForCurrentContext() {
236 int32 routing_id = MSG_ROUTING_CONTROL;
237 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29238 WebFrame* frame = WebFrame::frameForCurrentContext();
239 if (frame) {
240 RenderView* view = RenderView::FromWebView(frame->view());
241 if (view)
242 routing_id = view->routing_id();
243 }
[email protected]c1f50aa2010-02-18 03:46:57244 } else {
245 DLOG(WARNING) << "Not called within a script context!";
246 }
247 return routing_id;
248}
249
250bool RenderThread::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14251 // Certain synchronous messages cannot always be processed synchronously by
252 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50253 // This could cause a complete hang of Chrome if a windowed plug-in is trying
254 // to communicate with the renderer thread since the browser's UI thread
255 // could be stuck (within a Windows API call) trying to synchronously
256 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14257 // thread while the browser is processing this request. This creates an
258 // opportunity for re-entrancy into WebKit, so we need to take care to disable
259 // callbacks, timers, and pending network loads that could trigger such
260 // callbacks.
[email protected]38b592902011-04-16 02:08:42261 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50262 if (msg->is_sync()) {
263 if (msg->is_caller_pumping_messages()) {
264 pumping_events = true;
265 } else {
[email protected]38b592902011-04-16 02:08:42266 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
267 msg->type() == ViewHostMsg_GetRawCookies::ID ||
268 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
269 content::GetContentClient()->renderer()->
270 ShouldPumpEventsDuringCookieMessage()) {
271 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50272 }
273 }
[email protected]c1f50aa2010-02-18 03:46:57274 }
275
[email protected]80fc08c52010-03-09 07:43:50276 bool suspend_webkit_shared_timer = true; // default value
277 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57278
[email protected]80fc08c52010-03-09 07:43:50279 bool notify_webkit_of_modal_loop = true; // default value
280 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
281
282 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57283
284 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50285 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57286 webkit_client_->SuspendSharedTimer();
287
[email protected]39065d012010-07-09 11:22:46288 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57289 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57290
[email protected]80fc08c52010-03-09 07:43:50291 RenderWidget* widget =
292 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
293 if (widget) {
294 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57295 PluginChannelHost::Broadcast(
296 new PluginMsg_SignalModalDialogEvent(host_window));
297 }
298 }
299
300 bool rv = ChildThread::Send(msg);
301
302 if (pumping_events) {
303 if (host_window) {
304 PluginChannelHost::Broadcast(
305 new PluginMsg_ResetModalDialogEvent(host_window));
306 }
307
[email protected]39065d012010-07-09 11:22:46308 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57309 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57310
[email protected]80fc08c52010-03-09 07:43:50311 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57312 webkit_client_->ResumeSharedTimer();
313 }
314
315 return rv;
316}
317
318void RenderThread::AddRoute(int32 routing_id,
319 IPC::Channel::Listener* listener) {
320 widget_count_++;
321 return ChildThread::AddRoute(routing_id, listener);
322}
323
324void RenderThread::RemoveRoute(int32 routing_id) {
325 widget_count_--;
326 return ChildThread::RemoveRoute(routing_id);
327}
328
[email protected]42f1d7822009-07-23 18:17:55329void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
330 channel()->AddFilter(filter);
331}
332
333void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
334 channel()->RemoveFilter(filter);
335}
336
[email protected]bee16aab2009-08-26 15:55:03337void RenderThread::WidgetHidden() {
338 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50339 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24340
341 if (!content::GetContentClient()->renderer()->
342 RunIdleHandlerWhenWidgetsHidden()) {
343 return;
344 }
345
346 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]71d6d852009-12-07 22:12:36347 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03348}
349
350void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08351 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03352 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24353 if (!content::GetContentClient()->renderer()->
354 RunIdleHandlerWhenWidgetsHidden()) {
355 return;
356 }
[email protected]bee16aab2009-08-26 15:55:03357
[email protected]6779aa12011-03-29 17:32:24358 idle_timer_.Stop();
[email protected]e4be2dd2010-12-14 00:44:39359}
360
[email protected]1223d6ef2011-03-28 16:47:50361void RenderThread::AddObserver(RenderProcessObserver* observer) {
362 observers_.AddObserver(observer);
363}
364
365void RenderThread::RemoveObserver(RenderProcessObserver* observer) {
366 observers_.RemoveObserver(observer);
367}
368
[email protected]c1f50aa2010-02-18 03:46:57369void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50370 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57371}
372
373void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50374 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57375}
376
[email protected]9d797f32010-04-23 07:17:54377void RenderThread::OnSetZoomLevelForCurrentURL(const GURL& url,
[email protected]b75b8292010-10-01 07:28:25378 double zoom_level) {
[email protected]9d797f32010-04-23 07:17:54379 RenderViewZoomer zoomer(url, zoom_level);
[email protected]40bd6582009-12-04 23:49:51380 RenderView::ForEach(&zoomer);
381}
382
[email protected]c61cc652009-11-04 05:44:40383void RenderThread::OnDOMStorageEvent(
[email protected]56879f932010-12-13 21:05:37384 const DOMStorageMsg_Event_Params& params) {
[email protected]c61cc652009-11-04 05:44:40385 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34386 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]56879f932010-12-13 21:05:37387 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key,
388 params.old_value, params.new_value, params.origin, params.url,
389 params.storage_type == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34390}
391
[email protected]a95986a82010-12-24 06:19:28392bool RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50393 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
394 RenderProcessObserver* observer;
395 while ((observer = it.GetNext()) != NULL) {
396 if (observer->OnControlMessageReceived(msg))
397 return true;
398 }
399
[email protected]70c19a932010-05-14 12:59:11400 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31401 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28402 return true;
[email protected]70c19a932010-05-14 12:59:11403 if (indexed_db_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28404 return true;
[email protected]1edc16b82009-04-07 17:45:54405
[email protected]a95986a82010-12-24 06:19:28406 bool handled = true;
[email protected]8930d472009-02-21 08:05:28407 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
[email protected]9d797f32010-04-23 07:17:54408 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
409 OnSetZoomLevelForCurrentURL)
[email protected]8930d472009-02-21 08:05:28410 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55411 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28412 // TODO(port): removed from render_messages_internal.h;
413 // is there a new non-windows message I should add here?
414 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45415 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09416 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45417 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]a95986a82010-12-24 06:19:28418 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28419 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28420 return handled;
initial.commit09911bf2008-07-26 23:55:29421}
422
423void RenderThread::OnSetNextPageID(int32 next_page_id) {
424 // This should only be called at process initialization time, so we shouldn't
425 // have to worry about thread-safety.
426 RenderView::SetNextPageID(next_page_id);
427}
428
[email protected]b9ab10c2009-08-07 18:09:55429// Called when to register CSS Color name->system color mappings.
430// We update the colors one by one and then tell WebKit to refresh all render
431// views.
432void RenderThread::OnSetCSSColors(
433 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59434 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55435 size_t num_colors = colors.size();
436 scoped_array<WebKit::WebColorName> color_names(
437 new WebKit::WebColorName[num_colors]);
438 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
439 size_t i = 0;
440 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
441 colors.begin();
442 it != colors.end();
443 ++it, ++i) {
444 color_names[i] = it->first;
445 web_colors[i] = it->second;
446 }
447 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
448}
449
[email protected]4e6419c2010-01-15 04:50:34450void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47451 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33452 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29453 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26454 this,
455 params.parent_window,
[email protected]8ab04652010-06-12 02:47:26456 MSG_ROUTING_NONE,
457 params.renderer_preferences,
458 params.web_preferences,
459 new SharedRenderViewCounter(0),
460 params.view_id,
461 params.session_storage_namespace_id,
462 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27463}
[email protected]4274e582009-01-27 22:09:56464
[email protected]c40acc32010-01-14 01:02:53465void RenderThread::CloseCurrentConnections() {
466 Send(new ViewHostMsg_CloseCurrentConnections());
[email protected]b07f29092009-06-05 07:33:21467}
468
469void RenderThread::SetCacheMode(bool enabled) {
470 Send(new ViewHostMsg_SetCacheMode(enabled));
471}
472
[email protected]0e34852a2011-02-15 23:14:44473void RenderThread::ClearCache(bool preserve_ssl_host_info) {
[email protected]c5d79342010-06-05 01:27:34474 int rv;
[email protected]0e34852a2011-02-15 23:14:44475 Send(new ViewHostMsg_ClearCache(preserve_ssl_host_info, &rv));
[email protected]c5d79342010-06-05 01:27:34476}
477
[email protected]8990e4712011-03-28 20:50:55478void RenderThread::ClearHostResolverCache() {
479 int rv;
480 Send(new ViewHostMsg_ClearHostResolverCache(&rv));
481}
482
483void RenderThread::ClearPredictorCache() {
484 int rv;
485 Send(new ViewHostMsg_ClearPredictorCache(&rv));
486}
487
[email protected]12893c32010-08-19 17:30:54488void RenderThread::EnableSpdy(bool enable) {
489 Send(new ViewHostMsg_EnableSpdy(enable));
490}
491
[email protected]b42a9f62011-06-08 20:36:04492GpuChannelHost* RenderThread::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50493 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35494 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22495 // Do nothing if we already have a GPU channel or are already
496 // establishing one.
[email protected]e09cee42010-11-09 01:50:08497 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
498 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04499 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35500
501 // Recreate the channel if it has been lost.
[email protected]e09cee42010-11-09 01:50:08502 if (gpu_channel_->state() == GpuChannelHost::kLost)
[email protected]6217d392010-03-25 22:08:35503 gpu_channel_ = NULL;
504 }
505
506 if (!gpu_channel_.get())
507 gpu_channel_ = new GpuChannelHost;
508
509 // Ask the browser for the channel name.
[email protected]b42a9f62011-06-08 20:36:04510 IPC::ChannelHandle channel_handle;
511 base::ProcessHandle renderer_process_for_gpu;
512 GPUInfo gpu_info;
513 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
514 &channel_handle,
515 &renderer_process_for_gpu,
516 &gpu_info)) ||
517 channel_handle.name.empty() ||
518 renderer_process_for_gpu == base::kNullProcessHandle) {
519 // Otherwise cancel the connection.
520 gpu_channel_ = NULL;
521 return NULL;
522 }
[email protected]6217d392010-03-25 22:08:35523
[email protected]b42a9f62011-06-08 20:36:04524 gpu_channel_->set_gpu_info(gpu_info);
525 content::GetContentClient()->SetGpuInfo(gpu_info);
526
527 // Connect to the GPU process if a channel name was received.
528 gpu_channel_->Connect(channel_handle, renderer_process_for_gpu);
529
[email protected]f9a2b2fe2010-07-15 21:13:23530 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34531}
532
[email protected]6217d392010-03-25 22:08:35533GpuChannelHost* RenderThread::GetGpuChannel() {
534 if (!gpu_channel_.get())
535 return NULL;
536
[email protected]e09cee42010-11-09 01:50:08537 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35538 return NULL;
539
540 return gpu_channel_.get();
541}
542
[email protected]3df0c202009-03-31 23:51:26543static void* CreateHistogram(
544 const char *name, int min, int max, size_t buckets) {
[email protected]e8829a192009-12-06 00:09:37545 if (min <= 0)
546 min = 1;
[email protected]81ce9f3b2011-04-05 04:48:53547 base::Histogram* histogram = base::Histogram::FactoryGet(
[email protected]835d7c82010-10-14 04:38:38548 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
[email protected]81ce9f3b2011-04-05 04:48:53549 return histogram;
[email protected]3df0c202009-03-31 23:51:26550}
551
552static void AddHistogramSample(void* hist, int sample) {
[email protected]835d7c82010-10-14 04:38:38553 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
[email protected]3df0c202009-03-31 23:51:26554 histogram->Add(sample);
555}
556
[email protected]90a3fbb12009-02-28 01:13:47557void RenderThread::EnsureWebKitInitialized() {
558 if (webkit_client_.get())
559 return;
[email protected]da00a2882009-03-09 17:51:19560
[email protected]835d7c82010-10-14 04:38:38561 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26562 v8::V8::SetCreateHistogramFunction(CreateHistogram);
563 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19564
[email protected]90a3fbb12009-02-28 01:13:47565 webkit_client_.reset(new RendererWebKitClientImpl);
566 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44567
[email protected]98d7127b2009-10-23 18:26:51568 WebScriptController::enableV8SingleThreadMode();
[email protected]4ea00e82009-07-30 22:59:34569
[email protected]c8310d82011-01-13 23:42:28570 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
571
[email protected]d471190a2011-02-16 14:52:30572 webkit_glue::EnableWebCoreLogChannels(
573 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
574
[email protected]af7eb3fb2010-09-23 21:31:06575 if (command_line.HasSwitch(switches::kEnableBenchmarking))
[email protected]6779aa12011-03-29 17:32:24576 RegisterExtension(extensions_v8::BenchmarkingExtension::Get());
[email protected]b07f29092009-06-05 07:33:21577
[email protected]06533c0b2009-03-05 21:39:11578 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18579 command_line.HasSwitch(switches::kRecordMode) ||
580 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]6779aa12011-03-29 17:32:24581 RegisterExtension(extensions_v8::PlaybackExtension::Get());
[email protected]06533c0b2009-03-05 21:39:11582 }
[email protected]2cb82332009-03-18 17:24:55583
[email protected]af7eb3fb2010-09-23 21:31:06584 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(this));
585 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
[email protected]a9602de2010-03-18 23:43:11586
[email protected]b1b13212009-10-30 06:42:01587 WebRuntimeFeatures::enableSockets(
[email protected]cdf24782009-11-05 09:32:47588 !command_line.HasSwitch(switches::kDisableWebSockets));
[email protected]0afb9932009-10-24 00:37:58589
[email protected]b1b13212009-10-30 06:42:01590 WebRuntimeFeatures::enableDatabase(
[email protected]90a7ad522009-11-20 22:42:53591 !command_line.HasSwitch(switches::kDisableDatabases));
[email protected]bfa69d492009-10-31 03:27:19592
[email protected]27d27482011-03-08 22:45:39593 WebRuntimeFeatures::enableDataTransferItems(
[email protected]2d9b6ce2011-04-08 22:19:54594 !command_line.HasSwitch(switches::kDisableDataTransferItems));
[email protected]27d27482011-03-08 22:45:39595
[email protected]13bc0d822009-11-07 01:17:14596 WebRuntimeFeatures::enableApplicationCache(
[email protected]425c270642010-01-26 20:42:37597 !command_line.HasSwitch(switches::kDisableApplicationCache));
[email protected]13bc0d822009-11-07 01:17:14598
[email protected]bfa69d492009-10-31 03:27:19599 WebRuntimeFeatures::enableNotifications(
[email protected]380f1862009-11-14 02:48:01600 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
[email protected]7ee66e62009-11-05 09:16:30601
602 WebRuntimeFeatures::enableLocalStorage(
[email protected]5cf18362009-11-06 18:45:01603 !command_line.HasSwitch(switches::kDisableLocalStorage));
[email protected]7ee66e62009-11-05 09:16:30604 WebRuntimeFeatures::enableSessionStorage(
[email protected]03567802010-02-24 11:48:53605 !command_line.HasSwitch(switches::kDisableSessionStorage));
[email protected]2236bd1f2010-01-20 15:17:08606
[email protected]582fab732010-02-02 02:50:28607 WebRuntimeFeatures::enableIndexedDatabase(
[email protected]af1f9f32010-10-09 21:30:26608 !command_line.HasSwitch(switches::kDisableIndexedDatabase));
[email protected]582fab732010-02-02 02:50:28609
[email protected]2236bd1f2010-01-20 15:17:08610 WebRuntimeFeatures::enableGeolocation(
[email protected]d0569f42010-04-01 10:48:18611 !command_line.HasSwitch(switches::kDisableGeolocation));
[email protected]7a716c82010-03-03 10:33:44612
[email protected]24101d602011-06-23 15:40:24613 WebKit::WebRuntimeFeatures::enableMediaStream(
[email protected]f7eb0a392011-07-12 10:19:51614 command_line.HasSwitch(switches::kEnableMediaStream));
[email protected]24101d602011-06-23 15:40:24615
[email protected]104aaeac2011-06-27 22:03:01616#if defined(OS_CHROMEOS)
617 // TODO(crogers): enable once Web Audio has been tested and optimized.
618 WebRuntimeFeatures::enableWebAudio(false);
619#else
[email protected]8df51192011-01-22 20:05:03620 WebRuntimeFeatures::enableWebAudio(
[email protected]104aaeac2011-06-27 22:03:01621 !command_line.HasSwitch(switches::kDisableWebAudio));
622#endif
[email protected]8df51192011-01-22 20:05:03623
[email protected]ccbe04e2010-03-17 17:58:43624 WebRuntimeFeatures::enablePushState(true);
[email protected]f2e3d6c2010-04-30 14:23:04625
[email protected]7785b17f2011-03-23 01:20:28626#ifdef TOUCH_UI
627 WebRuntimeFeatures::enableTouch(true);
628#else
629 // TODO(saintlou): in the future touch should always be enabled
630 WebRuntimeFeatures::enableTouch(false);
631#endif
[email protected]66249e22010-07-22 09:55:33632
[email protected]1f501972010-08-09 05:40:02633 WebRuntimeFeatures::enableDeviceMotion(
634 command_line.HasSwitch(switches::kEnableDeviceMotion));
635
[email protected]66249e22010-07-22 09:55:33636 WebRuntimeFeatures::enableDeviceOrientation(
[email protected]71453202010-08-27 15:47:37637 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
[email protected]c8647272010-07-30 21:52:57638
[email protected]21e0cbf62011-03-04 07:55:03639 WebRuntimeFeatures::enableSpeechInput(
640 !command_line.HasSwitch(switches::kDisableSpeechInput));
[email protected]4c56ef52010-09-03 01:56:04641
642 WebRuntimeFeatures::enableFileSystem(
[email protected]2a524d22010-10-16 01:16:23643 !command_line.HasSwitch(switches::kDisableFileSystem));
[email protected]be8a9272011-02-10 22:06:07644
645 WebRuntimeFeatures::enableJavaScriptI18NAPI(
[email protected]7a0c1432011-03-10 21:33:41646 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
[email protected]6779aa12011-03-29 17:32:24647
[email protected]72654a62011-05-31 08:22:13648 WebRuntimeFeatures::enableQuota(true);
[email protected]8a009ee2011-04-27 08:53:43649
[email protected]6779aa12011-03-29 17:32:24650 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]90a3fbb12009-02-28 01:13:47651}
[email protected]75e5a872009-04-02 23:56:11652
[email protected]bee16aab2009-08-26 15:55:03653void RenderThread::IdleHandler() {
[email protected]e63c4d72011-05-31 22:38:29654#if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41655 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22656#endif
[email protected]bee16aab2009-08-26 15:55:03657
[email protected]f5e4b9bf2009-10-08 00:59:59658 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22659
660 // Schedule next invocation.
661 // Dampen the delay using the algorithm:
662 // delay = delay + 1 / (delay + 2)
663 // Using floor(delay) has a dampening effect such as:
664 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
665 // Note that idle_notification_delay_in_s_ would be reset to
666 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36667 ScheduleIdleHandler(idle_notification_delay_in_s_ +
668 1.0 / (idle_notification_delay_in_s_ + 2.0));
[email protected]6779aa12011-03-29 17:32:24669
670 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
[email protected]71d6d852009-12-07 22:12:36671}
[email protected]05001182009-09-15 23:34:22672
[email protected]71d6d852009-12-07 22:12:36673void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
674 idle_notification_delay_in_s_ = initial_delay_s;
675 idle_timer_.Stop();
676 idle_timer_.Start(
677 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
678 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03679}
680
[email protected]b78e168b2009-09-21 22:05:45681void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59682 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27683 // The call below will cause a GetPlugins call with refresh=true, but at this
684 // point we already know that the browser has refreshed its list, so disable
685 // refresh temporarily to prevent each renderer process causing the list to be
686 // regenerated.
687 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45688 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27689 plugin_refresh_allowed_ = true;
690}
[email protected]85c55dc2009-11-06 03:05:46691
[email protected]6eac57a2011-07-12 21:15:09692void RenderThread::OnNetworkStateChanged(bool online) {
693 EnsureWebKitInitialized();
694 WebNetworkStateNotifier::setOnLine(online);
695}
696
[email protected]c6a7b862010-08-20 22:19:38697scoped_refptr<base::MessageLoopProxy>
698RenderThread::GetFileThreadMessageLoopProxy() {
699 DCHECK(message_loop() == MessageLoop::current());
700 if (!file_thread_.get()) {
701 file_thread_.reset(new base::Thread("Renderer::FILE"));
702 file_thread_->Start();
703 }
704 return file_thread_->message_loop_proxy();
705}
[email protected]af7eb3fb2010-09-23 21:31:06706
[email protected]6779aa12011-03-29 17:32:24707void RenderThread::RegisterExtension(v8::Extension* extension) {
[email protected]af7eb3fb2010-09-23 21:31:06708 WebScriptController::registerExtension(extension);
[email protected]6779aa12011-03-29 17:32:24709 v8_extensions_.insert(extension->name());
[email protected]af7eb3fb2010-09-23 21:31:06710}
[email protected]c6e27b92011-04-30 17:03:43711
712bool RenderThread::IsRegisteredExtension(
713 const std::string& v8_extension_name) const {
714 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
715}