blob: 3e11f98f82f09ac4624566e7e7ec37987aa7e528 [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]82622452011-07-22 09:57:2037#include "content/renderer/devtools_agent_filter.h"
[email protected]a03a2222011-05-25 21:26:4038#include "content/renderer/gpu/gpu_channel_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"
[email protected]8d6cba42011-09-02 10:05:1950#include "content/renderer/renderer_webkitplatformsupport_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]4bd55a32011-07-28 13:28:3862#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3763#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
64#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]8bd0fe62011-01-17 06:44:3765#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
66#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
67#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]7f3a2cf2011-04-06 00:10:5068#include "v8/include/v8.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]099587b72011-09-20 00:40:5077#include "content/common/np_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]4bd55a32011-07-28 13:28:38101#if defined(TOUCH_UI)
102static const int kPopupListBoxMinimumRowHeight = 60;
103#endif
104
[email protected]f3ede412010-06-21 22:52:16105// Keep the global RenderThread in a TLS slot so it is impossible to access
106// incorrectly from the wrong thread.
[email protected]94f9a0f682009-06-15 18:30:30107static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
108 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54109
[email protected]40bd6582009-12-04 23:49:51110class RenderViewZoomer : public RenderViewVisitor {
111 public:
[email protected]b75b8292010-10-01 07:28:25112 RenderViewZoomer(const GURL& url, double zoom_level)
[email protected]9d797f32010-04-23 07:17:54113 : zoom_level_(zoom_level) {
114 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51115 }
116
117 virtual bool Visit(RenderView* render_view) {
[email protected]b6cb3a842011-06-24 18:28:41118 WebView* webview = render_view->webview();
119 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25120
121 // Don't set zoom level for full-page plugin since they don't use the same
122 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41123 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25124 return true;
125
[email protected]b6cb3a842011-06-24 18:28:41126 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51127 webview->setZoomLevel(false, zoom_level_);
128 return true;
129 }
130
131 private:
132 std::string host_;
[email protected]b75b8292010-10-01 07:28:25133 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51134
135 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
136};
[email protected]0478d0162010-08-28 08:29:40137
[email protected]42f1d7822009-07-23 18:17:55138} // namespace
139
[email protected]d1b8fccc2011-08-03 01:20:13140static void* CreateHistogram(
141 const char *name, int min, int max, size_t buckets) {
142 if (min <= 0)
143 min = 1;
144 base::Histogram* histogram = base::Histogram::FactoryGet(
145 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
146 return histogram;
147}
148
149static void AddHistogramSample(void* hist, int sample) {
150 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
151 histogram->Add(sample);
152}
153
[email protected]42f1d7822009-07-23 18:17:55154// When we run plugins in process, we actually run them on the render thread,
155// which means that we need to make the render thread pump UI events.
156RenderThread::RenderThread() {
157 Init();
158}
159
160RenderThread::RenderThread(const std::string& channel_name)
161 : ChildThread(channel_name) {
162 Init();
163}
[email protected]5fa1c542009-05-05 20:36:07164
initial.commit09911bf2008-07-26 23:55:29165void RenderThread::Init() {
[email protected]366ae242011-05-10 02:23:58166 TRACE_EVENT_BEGIN_ETW("RenderThread::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33167
[email protected]53c607c2011-03-21 23:19:04168#if defined(OS_MACOSX)
169 // On Mac, the select popups are rendered by the browser.
170 WebKit::WebView::setUseExternalPopupMenus(true);
171#endif
172
[email protected]94f9a0f682009-06-15 18:30:30173 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50174#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34175 // If you are running plugins in this thread you need COM active but in
176 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28177 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34178 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50179#endif
initial.commit09911bf2008-07-26 23:55:29180
[email protected]31f87132010-04-21 23:36:21181 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50182 suspend_webkit_shared_timer_ = true;
183 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55184 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03185 widget_count_ = 0;
186 hidden_widget_count_ = 0;
[email protected]6779aa12011-03-29 17:32:24187 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03188 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55189
[email protected]f430b5712009-08-21 21:46:31190 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]70c19a932010-05-14 12:59:11191 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13192
[email protected]017022b2009-07-27 23:06:34193 db_message_filter_ = new DBMessageFilter();
194 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13195
[email protected]80b161a2011-06-27 17:42:11196 vc_manager_ = new VideoCaptureImplManager();
197 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46198
[email protected]f7eb0a392011-07-12 10:19:51199 audio_input_message_filter_ = new AudioInputMessageFilter();
200 AddFilter(audio_input_message_filter_.get());
201
202 audio_message_filter_ = new AudioMessageFilter();
203 AddFilter(audio_message_filter_.get());
204
[email protected]82622452011-07-22 09:57:20205 devtools_agent_message_filter_ = new DevToolsAgentFilter();
206 AddFilter(devtools_agent_message_filter_.get());
207
[email protected]8d97ade2011-04-14 18:17:08208 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24209
[email protected]366ae242011-05-10 02:23:58210 TRACE_EVENT_END_ETW("RenderThread::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29211}
212
[email protected]42f1d7822009-07-23 18:17:55213RenderThread::~RenderThread() {
[email protected]1223d6ef2011-03-28 16:47:50214 FOR_EACH_OBSERVER(
215 RenderProcessObserver, observers_, OnRenderProcessShutdown());
216
[email protected]12cbfda32010-01-30 01:04:25217 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08218 if (web_database_observer_impl_.get())
219 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25220
[email protected]8d86fce2009-02-26 23:37:55221 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20222 RemoveFilter(devtools_agent_message_filter_.get());
223 devtools_agent_message_filter_ = NULL;
224
[email protected]f7eb0a392011-07-12 10:19:51225 RemoveFilter(audio_input_message_filter_.get());
226 audio_input_message_filter_ = NULL;
227
228 RemoveFilter(audio_message_filter_.get());
229 audio_message_filter_ = NULL;
230
[email protected]80b161a2011-06-27 17:42:11231 RemoveFilter(vc_manager_->video_capture_message_filter());
232
[email protected]017022b2009-07-27 23:06:34233 RemoveFilter(db_message_filter_.get());
234 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25235
[email protected]c6a7b862010-08-20 22:19:38236 // Shutdown the file thread if it's running.
237 if (file_thread_.get())
238 file_thread_->Stop();
239
[email protected]8d6cba42011-09-02 10:05:19240 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22241 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22242
[email protected]94f9a0f682009-06-15 18:30:30243 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30244
[email protected]8d86fce2009-02-26 23:37:55245 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50246#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29247 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50248 NPChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34249 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28250 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34251 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50252#endif
initial.commit09911bf2008-07-26 23:55:29253}
254
[email protected]42f1d7822009-07-23 18:17:55255RenderThread* RenderThread::current() {
256 return lazy_tls.Pointer()->Get();
257}
258
[email protected]c1f50aa2010-02-18 03:46:57259int32 RenderThread::RoutingIDForCurrentContext() {
260 int32 routing_id = MSG_ROUTING_CONTROL;
261 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29262 WebFrame* frame = WebFrame::frameForCurrentContext();
263 if (frame) {
264 RenderView* view = RenderView::FromWebView(frame->view());
265 if (view)
266 routing_id = view->routing_id();
267 }
[email protected]c1f50aa2010-02-18 03:46:57268 } else {
269 DLOG(WARNING) << "Not called within a script context!";
270 }
271 return routing_id;
272}
273
274bool RenderThread::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14275 // Certain synchronous messages cannot always be processed synchronously by
276 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50277 // This could cause a complete hang of Chrome if a windowed plug-in is trying
278 // to communicate with the renderer thread since the browser's UI thread
279 // could be stuck (within a Windows API call) trying to synchronously
280 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14281 // thread while the browser is processing this request. This creates an
282 // opportunity for re-entrancy into WebKit, so we need to take care to disable
283 // callbacks, timers, and pending network loads that could trigger such
284 // callbacks.
[email protected]38b592902011-04-16 02:08:42285 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50286 if (msg->is_sync()) {
287 if (msg->is_caller_pumping_messages()) {
288 pumping_events = true;
289 } else {
[email protected]38b592902011-04-16 02:08:42290 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
291 msg->type() == ViewHostMsg_GetRawCookies::ID ||
292 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
293 content::GetContentClient()->renderer()->
294 ShouldPumpEventsDuringCookieMessage()) {
295 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50296 }
297 }
[email protected]c1f50aa2010-02-18 03:46:57298 }
299
[email protected]80fc08c52010-03-09 07:43:50300 bool suspend_webkit_shared_timer = true; // default value
301 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57302
[email protected]80fc08c52010-03-09 07:43:50303 bool notify_webkit_of_modal_loop = true; // default value
304 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
305
306 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57307
308 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50309 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19310 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57311
[email protected]39065d012010-07-09 11:22:46312 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57313 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57314
[email protected]80fc08c52010-03-09 07:43:50315 RenderWidget* widget =
316 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
317 if (widget) {
318 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57319 PluginChannelHost::Broadcast(
320 new PluginMsg_SignalModalDialogEvent(host_window));
321 }
322 }
323
324 bool rv = ChildThread::Send(msg);
325
326 if (pumping_events) {
327 if (host_window) {
328 PluginChannelHost::Broadcast(
329 new PluginMsg_ResetModalDialogEvent(host_window));
330 }
331
[email protected]39065d012010-07-09 11:22:46332 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57333 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57334
[email protected]80fc08c52010-03-09 07:43:50335 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19336 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57337 }
338
339 return rv;
340}
341
342void RenderThread::AddRoute(int32 routing_id,
343 IPC::Channel::Listener* listener) {
344 widget_count_++;
345 return ChildThread::AddRoute(routing_id, listener);
346}
347
348void RenderThread::RemoveRoute(int32 routing_id) {
349 widget_count_--;
350 return ChildThread::RemoveRoute(routing_id);
351}
352
[email protected]42f1d7822009-07-23 18:17:55353void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
354 channel()->AddFilter(filter);
355}
356
357void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
358 channel()->RemoveFilter(filter);
359}
360
[email protected]bee16aab2009-08-26 15:55:03361void RenderThread::WidgetHidden() {
362 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50363 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24364
365 if (!content::GetContentClient()->renderer()->
366 RunIdleHandlerWhenWidgetsHidden()) {
367 return;
368 }
369
370 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]71d6d852009-12-07 22:12:36371 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03372}
373
374void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08375 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03376 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24377 if (!content::GetContentClient()->renderer()->
378 RunIdleHandlerWhenWidgetsHidden()) {
379 return;
380 }
[email protected]bee16aab2009-08-26 15:55:03381
[email protected]6779aa12011-03-29 17:32:24382 idle_timer_.Stop();
[email protected]e4be2dd2010-12-14 00:44:39383}
384
[email protected]1223d6ef2011-03-28 16:47:50385void RenderThread::AddObserver(RenderProcessObserver* observer) {
386 observers_.AddObserver(observer);
387}
388
389void RenderThread::RemoveObserver(RenderProcessObserver* observer) {
390 observers_.RemoveObserver(observer);
391}
392
[email protected]c1f50aa2010-02-18 03:46:57393void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50394 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57395}
396
397void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50398 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57399}
400
[email protected]9d797f32010-04-23 07:17:54401void RenderThread::OnSetZoomLevelForCurrentURL(const GURL& url,
[email protected]b75b8292010-10-01 07:28:25402 double zoom_level) {
[email protected]9d797f32010-04-23 07:17:54403 RenderViewZoomer zoomer(url, zoom_level);
[email protected]40bd6582009-12-04 23:49:51404 RenderView::ForEach(&zoomer);
405}
406
[email protected]c61cc652009-11-04 05:44:40407void RenderThread::OnDOMStorageEvent(
[email protected]56879f932010-12-13 21:05:37408 const DOMStorageMsg_Event_Params& params) {
[email protected]c61cc652009-11-04 05:44:40409 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34410 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]56879f932010-12-13 21:05:37411 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key,
412 params.old_value, params.new_value, params.origin, params.url,
413 params.storage_type == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34414}
415
[email protected]d1b8fccc2011-08-03 01:20:13416void RenderThread::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19417 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13418 return;
419
420 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
421 v8::V8::SetCreateHistogramFunction(CreateHistogram);
422 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
423
[email protected]8d6cba42011-09-02 10:05:19424 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
425 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13426
427 WebScriptController::enableV8SingleThreadMode();
428
429 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
430
431 webkit_glue::EnableWebCoreLogChannels(
432 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
433
[email protected]d1b8fccc2011-08-03 01:20:13434 if (command_line.HasSwitch(switches::kPlaybackMode) ||
435 command_line.HasSwitch(switches::kRecordMode) ||
436 command_line.HasSwitch(switches::kNoJsRandomness)) {
437 RegisterExtension(extensions_v8::PlaybackExtension::Get());
438 }
439
440 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(this));
441 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
442
443 WebRuntimeFeatures::enableSockets(
444 !command_line.HasSwitch(switches::kDisableWebSockets));
445
446 WebRuntimeFeatures::enableDatabase(
447 !command_line.HasSwitch(switches::kDisableDatabases));
448
449 WebRuntimeFeatures::enableDataTransferItems(
450 !command_line.HasSwitch(switches::kDisableDataTransferItems));
451
452 WebRuntimeFeatures::enableApplicationCache(
453 !command_line.HasSwitch(switches::kDisableApplicationCache));
454
455 WebRuntimeFeatures::enableNotifications(
456 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
457
458 WebRuntimeFeatures::enableLocalStorage(
459 !command_line.HasSwitch(switches::kDisableLocalStorage));
460 WebRuntimeFeatures::enableSessionStorage(
461 !command_line.HasSwitch(switches::kDisableSessionStorage));
462
463 WebRuntimeFeatures::enableIndexedDatabase(
464 !command_line.HasSwitch(switches::kDisableIndexedDatabase));
465
466 WebRuntimeFeatures::enableGeolocation(
467 !command_line.HasSwitch(switches::kDisableGeolocation));
468
469 WebKit::WebRuntimeFeatures::enableMediaStream(
470 command_line.HasSwitch(switches::kEnableMediaStream));
471
[email protected]efdc0282011-08-26 00:36:12472 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21473 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12474
[email protected]d1b8fccc2011-08-03 01:20:13475#if defined(OS_CHROMEOS)
476 // TODO(crogers): enable once Web Audio has been tested and optimized.
477 WebRuntimeFeatures::enableWebAudio(false);
478#else
479 WebRuntimeFeatures::enableWebAudio(
480 !command_line.HasSwitch(switches::kDisableWebAudio));
481#endif
482
483 WebRuntimeFeatures::enablePushState(true);
484
485#ifdef TOUCH_UI
486 WebRuntimeFeatures::enableTouch(true);
487 WebKit::WebPopupMenu::setMinimumRowHeight(kPopupListBoxMinimumRowHeight);
488#else
489 // TODO(saintlou): in the future touch should always be enabled
490 WebRuntimeFeatures::enableTouch(false);
491#endif
492
493 WebRuntimeFeatures::enableDeviceMotion(
494 command_line.HasSwitch(switches::kEnableDeviceMotion));
495
496 WebRuntimeFeatures::enableDeviceOrientation(
497 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
498
499 WebRuntimeFeatures::enableSpeechInput(
500 !command_line.HasSwitch(switches::kDisableSpeechInput));
501
502 WebRuntimeFeatures::enableFileSystem(
503 !command_line.HasSwitch(switches::kDisableFileSystem));
504
505 WebRuntimeFeatures::enableJavaScriptI18NAPI(
506 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
507
508 WebRuntimeFeatures::enableQuota(true);
509
510 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
511}
512
[email protected]4a7d6392011-09-19 20:55:08513// static
514void RenderThread::RecordUserMetrics(const std::string& action) {
515 RenderThread::current()->Send(
516 new ViewHostMsg_UserMetricsRecordAction(action));
517}
518
519#if defined(OS_WIN)
520// static
521bool RenderThread::PreCacheFont(const LOGFONT& log_font) {
522 return RenderThread::current()->Send(new ViewHostMsg_PreCacheFont(log_font));
523}
524#endif // OS_WIN
525
[email protected]a95986a82010-12-24 06:19:28526bool RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50527 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
528 RenderProcessObserver* observer;
529 while ((observer = it.GetNext()) != NULL) {
530 if (observer->OnControlMessageReceived(msg))
531 return true;
532 }
533
[email protected]70c19a932010-05-14 12:59:11534 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31535 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28536 return true;
[email protected]70c19a932010-05-14 12:59:11537 if (indexed_db_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28538 return true;
[email protected]1edc16b82009-04-07 17:45:54539
[email protected]a95986a82010-12-24 06:19:28540 bool handled = true;
[email protected]8930d472009-02-21 08:05:28541 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
[email protected]9d797f32010-04-23 07:17:54542 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
543 OnSetZoomLevelForCurrentURL)
[email protected]8930d472009-02-21 08:05:28544 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55545 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28546 // TODO(port): removed from render_messages_internal.h;
547 // is there a new non-windows message I should add here?
548 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45549 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09550 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45551 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]a95986a82010-12-24 06:19:28552 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28553 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28554 return handled;
initial.commit09911bf2008-07-26 23:55:29555}
556
557void RenderThread::OnSetNextPageID(int32 next_page_id) {
558 // This should only be called at process initialization time, so we shouldn't
559 // have to worry about thread-safety.
560 RenderView::SetNextPageID(next_page_id);
561}
562
[email protected]b9ab10c2009-08-07 18:09:55563// Called when to register CSS Color name->system color mappings.
564// We update the colors one by one and then tell WebKit to refresh all render
565// views.
566void RenderThread::OnSetCSSColors(
567 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59568 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55569 size_t num_colors = colors.size();
570 scoped_array<WebKit::WebColorName> color_names(
571 new WebKit::WebColorName[num_colors]);
572 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
573 size_t i = 0;
574 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
575 colors.begin();
576 it != colors.end();
577 ++it, ++i) {
578 color_names[i] = it->first;
579 web_colors[i] = it->second;
580 }
581 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
582}
583
[email protected]4e6419c2010-01-15 04:50:34584void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47585 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33586 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29587 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26588 this,
589 params.parent_window,
590 MSG_ROUTING_NONE,
591 params.renderer_preferences,
592 params.web_preferences,
593 new SharedRenderViewCounter(0),
594 params.view_id,
595 params.session_storage_namespace_id,
596 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27597}
[email protected]4274e582009-01-27 22:09:56598
[email protected]b42a9f62011-06-08 20:36:04599GpuChannelHost* RenderThread::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50600 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35601 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22602 // Do nothing if we already have a GPU channel or are already
603 // establishing one.
[email protected]e09cee42010-11-09 01:50:08604 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
605 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04606 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35607
608 // Recreate the channel if it has been lost.
[email protected]e09cee42010-11-09 01:50:08609 if (gpu_channel_->state() == GpuChannelHost::kLost)
[email protected]6217d392010-03-25 22:08:35610 gpu_channel_ = NULL;
611 }
612
613 if (!gpu_channel_.get())
614 gpu_channel_ = new GpuChannelHost;
615
616 // Ask the browser for the channel name.
[email protected]b42a9f62011-06-08 20:36:04617 IPC::ChannelHandle channel_handle;
618 base::ProcessHandle renderer_process_for_gpu;
619 GPUInfo gpu_info;
620 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
621 &channel_handle,
622 &renderer_process_for_gpu,
623 &gpu_info)) ||
624 channel_handle.name.empty() ||
625 renderer_process_for_gpu == base::kNullProcessHandle) {
626 // Otherwise cancel the connection.
627 gpu_channel_ = NULL;
628 return NULL;
629 }
[email protected]6217d392010-03-25 22:08:35630
[email protected]b42a9f62011-06-08 20:36:04631 gpu_channel_->set_gpu_info(gpu_info);
632 content::GetContentClient()->SetGpuInfo(gpu_info);
633
634 // Connect to the GPU process if a channel name was received.
635 gpu_channel_->Connect(channel_handle, renderer_process_for_gpu);
636
[email protected]f9a2b2fe2010-07-15 21:13:23637 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34638}
639
[email protected]6217d392010-03-25 22:08:35640GpuChannelHost* RenderThread::GetGpuChannel() {
641 if (!gpu_channel_.get())
642 return NULL;
643
[email protected]e09cee42010-11-09 01:50:08644 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35645 return NULL;
646
647 return gpu_channel_.get();
648}
649
[email protected]bee16aab2009-08-26 15:55:03650void RenderThread::IdleHandler() {
[email protected]e63c4d72011-05-31 22:38:29651#if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41652 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22653#endif
[email protected]bee16aab2009-08-26 15:55:03654
[email protected]f5e4b9bf2009-10-08 00:59:59655 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22656
657 // Schedule next invocation.
658 // Dampen the delay using the algorithm:
659 // delay = delay + 1 / (delay + 2)
660 // Using floor(delay) has a dampening effect such as:
661 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
662 // Note that idle_notification_delay_in_s_ would be reset to
663 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36664 ScheduleIdleHandler(idle_notification_delay_in_s_ +
665 1.0 / (idle_notification_delay_in_s_ + 2.0));
[email protected]6779aa12011-03-29 17:32:24666
667 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
[email protected]71d6d852009-12-07 22:12:36668}
[email protected]05001182009-09-15 23:34:22669
[email protected]71d6d852009-12-07 22:12:36670void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
671 idle_notification_delay_in_s_ = initial_delay_s;
672 idle_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02673 idle_timer_.Start(FROM_HERE,
[email protected]71d6d852009-12-07 22:12:36674 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
675 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03676}
677
[email protected]b78e168b2009-09-21 22:05:45678void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59679 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27680 // The call below will cause a GetPlugins call with refresh=true, but at this
681 // point we already know that the browser has refreshed its list, so disable
682 // refresh temporarily to prevent each renderer process causing the list to be
683 // regenerated.
684 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45685 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27686 plugin_refresh_allowed_ = true;
687}
[email protected]85c55dc2009-11-06 03:05:46688
[email protected]6eac57a2011-07-12 21:15:09689void RenderThread::OnNetworkStateChanged(bool online) {
690 EnsureWebKitInitialized();
691 WebNetworkStateNotifier::setOnLine(online);
692}
693
[email protected]c6a7b862010-08-20 22:19:38694scoped_refptr<base::MessageLoopProxy>
695RenderThread::GetFileThreadMessageLoopProxy() {
696 DCHECK(message_loop() == MessageLoop::current());
697 if (!file_thread_.get()) {
698 file_thread_.reset(new base::Thread("Renderer::FILE"));
699 file_thread_->Start();
700 }
701 return file_thread_->message_loop_proxy();
702}
[email protected]af7eb3fb2010-09-23 21:31:06703
[email protected]6779aa12011-03-29 17:32:24704void RenderThread::RegisterExtension(v8::Extension* extension) {
[email protected]af7eb3fb2010-09-23 21:31:06705 WebScriptController::registerExtension(extension);
[email protected]6779aa12011-03-29 17:32:24706 v8_extensions_.insert(extension->name());
[email protected]af7eb3fb2010-09-23 21:31:06707}
[email protected]c6e27b92011-04-30 17:03:43708
709bool RenderThread::IsRegisteredExtension(
710 const std::string& v8_extension_name) const {
711 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
712}