blob: 19522d5c96a3e9ba7d1cb9cca2a92e9bd1cbfaf2 [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]b07f29092009-06-05 07:33:2169#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1170#include "webkit/extensions/v8/playback_extension.h"
[email protected]d471190a2011-02-16 14:52:3071#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5072
[email protected]94dc971d2011-03-05 19:08:3273// TODO(port)
74#if defined(OS_WIN)
[email protected]3c5c6d82011-03-16 17:23:5875#include "content/plugin/plugin_channel.h"
[email protected]94dc971d2011-03-05 19:08:3276#else
[email protected]3b63f8f42011-03-28 01:54:1577#include "base/memory/scoped_handle.h"
[email protected]3c5c6d82011-03-16 17:23:5878#include "content/plugin/plugin_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3279#endif
80
[email protected]da00a2882009-03-09 17:51:1981#if defined(OS_WIN)
82#include <windows.h>
83#include <objbase.h>
84#endif
85
[email protected]6217d392010-03-25 22:08:3586#if defined(OS_POSIX)
87#include "ipc/ipc_channel_posix.h"
88#endif
89
[email protected]b6cb3a842011-06-24 18:28:4190using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:0191using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:0992using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:0093using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:5194using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:4795using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:3496using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:0397using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:2998
[email protected]42f1d7822009-07-23 18:17:5599namespace {
[email protected]bee16aab2009-08-26 15:55:03100static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
101
[email protected]4bd55a32011-07-28 13:28:38102#if defined(TOUCH_UI)
103static const int kPopupListBoxMinimumRowHeight = 60;
104#endif
105
[email protected]f3ede412010-06-21 22:52:16106// Keep the global RenderThread in a TLS slot so it is impossible to access
107// incorrectly from the wrong thread.
[email protected]94f9a0f682009-06-15 18:30:30108static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
109 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54110
[email protected]40bd6582009-12-04 23:49:51111class RenderViewZoomer : public RenderViewVisitor {
112 public:
[email protected]b75b8292010-10-01 07:28:25113 RenderViewZoomer(const GURL& url, double zoom_level)
[email protected]9d797f32010-04-23 07:17:54114 : zoom_level_(zoom_level) {
115 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51116 }
117
118 virtual bool Visit(RenderView* render_view) {
[email protected]b6cb3a842011-06-24 18:28:41119 WebView* webview = render_view->webview();
120 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25121
122 // Don't set zoom level for full-page plugin since they don't use the same
123 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41124 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25125 return true;
126
[email protected]b6cb3a842011-06-24 18:28:41127 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51128 webview->setZoomLevel(false, zoom_level_);
129 return true;
130 }
131
132 private:
133 std::string host_;
[email protected]b75b8292010-10-01 07:28:25134 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51135
136 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
137};
[email protected]0478d0162010-08-28 08:29:40138
[email protected]42f1d7822009-07-23 18:17:55139} // namespace
140
[email protected]d1b8fccc2011-08-03 01:20:13141static void* CreateHistogram(
142 const char *name, int min, int max, size_t buckets) {
143 if (min <= 0)
144 min = 1;
145 base::Histogram* histogram = base::Histogram::FactoryGet(
146 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
147 return histogram;
148}
149
150static void AddHistogramSample(void* hist, int sample) {
151 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
152 histogram->Add(sample);
153}
154
[email protected]42f1d7822009-07-23 18:17:55155// When we run plugins in process, we actually run them on the render thread,
156// which means that we need to make the render thread pump UI events.
157RenderThread::RenderThread() {
158 Init();
159}
160
161RenderThread::RenderThread(const std::string& channel_name)
162 : ChildThread(channel_name) {
163 Init();
164}
[email protected]5fa1c542009-05-05 20:36:07165
initial.commit09911bf2008-07-26 23:55:29166void RenderThread::Init() {
[email protected]366ae242011-05-10 02:23:58167 TRACE_EVENT_BEGIN_ETW("RenderThread::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33168
[email protected]53c607c2011-03-21 23:19:04169#if defined(OS_MACOSX)
170 // On Mac, the select popups are rendered by the browser.
171 WebKit::WebView::setUseExternalPopupMenus(true);
172#endif
173
[email protected]94f9a0f682009-06-15 18:30:30174 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50175#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34176 // If you are running plugins in this thread you need COM active but in
177 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28178 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34179 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50180#endif
initial.commit09911bf2008-07-26 23:55:29181
[email protected]31f87132010-04-21 23:36:21182 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50183 suspend_webkit_shared_timer_ = true;
184 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55185 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03186 widget_count_ = 0;
187 hidden_widget_count_ = 0;
[email protected]6779aa12011-03-29 17:32:24188 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03189 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55190
[email protected]f430b5712009-08-21 21:46:31191 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]70c19a932010-05-14 12:59:11192 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13193
[email protected]017022b2009-07-27 23:06:34194 db_message_filter_ = new DBMessageFilter();
195 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13196
[email protected]80b161a2011-06-27 17:42:11197 vc_manager_ = new VideoCaptureImplManager();
198 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46199
[email protected]f7eb0a392011-07-12 10:19:51200 audio_input_message_filter_ = new AudioInputMessageFilter();
201 AddFilter(audio_input_message_filter_.get());
202
203 audio_message_filter_ = new AudioMessageFilter();
204 AddFilter(audio_message_filter_.get());
205
[email protected]82622452011-07-22 09:57:20206 devtools_agent_message_filter_ = new DevToolsAgentFilter();
207 AddFilter(devtools_agent_message_filter_.get());
208
[email protected]8d97ade2011-04-14 18:17:08209 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24210
[email protected]366ae242011-05-10 02:23:58211 TRACE_EVENT_END_ETW("RenderThread::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29212}
213
[email protected]42f1d7822009-07-23 18:17:55214RenderThread::~RenderThread() {
[email protected]1223d6ef2011-03-28 16:47:50215 FOR_EACH_OBSERVER(
216 RenderProcessObserver, observers_, OnRenderProcessShutdown());
217
[email protected]12cbfda32010-01-30 01:04:25218 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08219 if (web_database_observer_impl_.get())
220 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25221
[email protected]8d86fce2009-02-26 23:37:55222 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20223 RemoveFilter(devtools_agent_message_filter_.get());
224 devtools_agent_message_filter_ = NULL;
225
[email protected]f7eb0a392011-07-12 10:19:51226 RemoveFilter(audio_input_message_filter_.get());
227 audio_input_message_filter_ = NULL;
228
229 RemoveFilter(audio_message_filter_.get());
230 audio_message_filter_ = NULL;
231
[email protected]80b161a2011-06-27 17:42:11232 RemoveFilter(vc_manager_->video_capture_message_filter());
233
[email protected]017022b2009-07-27 23:06:34234 RemoveFilter(db_message_filter_.get());
235 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25236
[email protected]c6a7b862010-08-20 22:19:38237 // Shutdown the file thread if it's running.
238 if (file_thread_.get())
239 file_thread_->Stop();
240
[email protected]8d6cba42011-09-02 10:05:19241 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22242 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22243
[email protected]94f9a0f682009-06-15 18:30:30244 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30245
[email protected]8d86fce2009-02-26 23:37:55246 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50247#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29248 // Clean up plugin channels before this thread goes away.
249 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34250 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28251 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34252 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50253#endif
initial.commit09911bf2008-07-26 23:55:29254}
255
[email protected]42f1d7822009-07-23 18:17:55256RenderThread* RenderThread::current() {
257 return lazy_tls.Pointer()->Get();
258}
259
[email protected]c1f50aa2010-02-18 03:46:57260int32 RenderThread::RoutingIDForCurrentContext() {
261 int32 routing_id = MSG_ROUTING_CONTROL;
262 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29263 WebFrame* frame = WebFrame::frameForCurrentContext();
264 if (frame) {
265 RenderView* view = RenderView::FromWebView(frame->view());
266 if (view)
267 routing_id = view->routing_id();
268 }
[email protected]c1f50aa2010-02-18 03:46:57269 } else {
270 DLOG(WARNING) << "Not called within a script context!";
271 }
272 return routing_id;
273}
274
275bool RenderThread::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14276 // Certain synchronous messages cannot always be processed synchronously by
277 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50278 // This could cause a complete hang of Chrome if a windowed plug-in is trying
279 // to communicate with the renderer thread since the browser's UI thread
280 // could be stuck (within a Windows API call) trying to synchronously
281 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14282 // thread while the browser is processing this request. This creates an
283 // opportunity for re-entrancy into WebKit, so we need to take care to disable
284 // callbacks, timers, and pending network loads that could trigger such
285 // callbacks.
[email protected]38b592902011-04-16 02:08:42286 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50287 if (msg->is_sync()) {
288 if (msg->is_caller_pumping_messages()) {
289 pumping_events = true;
290 } else {
[email protected]38b592902011-04-16 02:08:42291 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
292 msg->type() == ViewHostMsg_GetRawCookies::ID ||
293 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
294 content::GetContentClient()->renderer()->
295 ShouldPumpEventsDuringCookieMessage()) {
296 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50297 }
298 }
[email protected]c1f50aa2010-02-18 03:46:57299 }
300
[email protected]80fc08c52010-03-09 07:43:50301 bool suspend_webkit_shared_timer = true; // default value
302 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57303
[email protected]80fc08c52010-03-09 07:43:50304 bool notify_webkit_of_modal_loop = true; // default value
305 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
306
307 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57308
309 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50310 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19311 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57312
[email protected]39065d012010-07-09 11:22:46313 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57314 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57315
[email protected]80fc08c52010-03-09 07:43:50316 RenderWidget* widget =
317 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
318 if (widget) {
319 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57320 PluginChannelHost::Broadcast(
321 new PluginMsg_SignalModalDialogEvent(host_window));
322 }
323 }
324
325 bool rv = ChildThread::Send(msg);
326
327 if (pumping_events) {
328 if (host_window) {
329 PluginChannelHost::Broadcast(
330 new PluginMsg_ResetModalDialogEvent(host_window));
331 }
332
[email protected]39065d012010-07-09 11:22:46333 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57334 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57335
[email protected]80fc08c52010-03-09 07:43:50336 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19337 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57338 }
339
340 return rv;
341}
342
343void RenderThread::AddRoute(int32 routing_id,
344 IPC::Channel::Listener* listener) {
345 widget_count_++;
346 return ChildThread::AddRoute(routing_id, listener);
347}
348
349void RenderThread::RemoveRoute(int32 routing_id) {
350 widget_count_--;
351 return ChildThread::RemoveRoute(routing_id);
352}
353
[email protected]42f1d7822009-07-23 18:17:55354void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
355 channel()->AddFilter(filter);
356}
357
358void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
359 channel()->RemoveFilter(filter);
360}
361
[email protected]bee16aab2009-08-26 15:55:03362void RenderThread::WidgetHidden() {
363 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50364 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24365
366 if (!content::GetContentClient()->renderer()->
367 RunIdleHandlerWhenWidgetsHidden()) {
368 return;
369 }
370
371 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]71d6d852009-12-07 22:12:36372 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03373}
374
375void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08376 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03377 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24378 if (!content::GetContentClient()->renderer()->
379 RunIdleHandlerWhenWidgetsHidden()) {
380 return;
381 }
[email protected]bee16aab2009-08-26 15:55:03382
[email protected]6779aa12011-03-29 17:32:24383 idle_timer_.Stop();
[email protected]e4be2dd2010-12-14 00:44:39384}
385
[email protected]1223d6ef2011-03-28 16:47:50386void RenderThread::AddObserver(RenderProcessObserver* observer) {
387 observers_.AddObserver(observer);
388}
389
390void RenderThread::RemoveObserver(RenderProcessObserver* observer) {
391 observers_.RemoveObserver(observer);
392}
393
[email protected]c1f50aa2010-02-18 03:46:57394void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50395 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57396}
397
398void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50399 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57400}
401
[email protected]9d797f32010-04-23 07:17:54402void RenderThread::OnSetZoomLevelForCurrentURL(const GURL& url,
[email protected]b75b8292010-10-01 07:28:25403 double zoom_level) {
[email protected]9d797f32010-04-23 07:17:54404 RenderViewZoomer zoomer(url, zoom_level);
[email protected]40bd6582009-12-04 23:49:51405 RenderView::ForEach(&zoomer);
406}
407
[email protected]c61cc652009-11-04 05:44:40408void RenderThread::OnDOMStorageEvent(
[email protected]56879f932010-12-13 21:05:37409 const DOMStorageMsg_Event_Params& params) {
[email protected]c61cc652009-11-04 05:44:40410 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34411 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]56879f932010-12-13 21:05:37412 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key,
413 params.old_value, params.new_value, params.origin, params.url,
414 params.storage_type == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34415}
416
[email protected]d1b8fccc2011-08-03 01:20:13417void RenderThread::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19418 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13419 return;
420
421 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
422 v8::V8::SetCreateHistogramFunction(CreateHistogram);
423 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
424
[email protected]8d6cba42011-09-02 10:05:19425 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
426 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13427
428 WebScriptController::enableV8SingleThreadMode();
429
430 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
431
432 webkit_glue::EnableWebCoreLogChannels(
433 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
434
435 if (command_line.HasSwitch(switches::kEnableBenchmarking))
436 RegisterExtension(extensions_v8::BenchmarkingExtension::Get());
437
438 if (command_line.HasSwitch(switches::kPlaybackMode) ||
439 command_line.HasSwitch(switches::kRecordMode) ||
440 command_line.HasSwitch(switches::kNoJsRandomness)) {
441 RegisterExtension(extensions_v8::PlaybackExtension::Get());
442 }
443
444 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(this));
445 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
446
447 WebRuntimeFeatures::enableSockets(
448 !command_line.HasSwitch(switches::kDisableWebSockets));
449
450 WebRuntimeFeatures::enableDatabase(
451 !command_line.HasSwitch(switches::kDisableDatabases));
452
453 WebRuntimeFeatures::enableDataTransferItems(
454 !command_line.HasSwitch(switches::kDisableDataTransferItems));
455
456 WebRuntimeFeatures::enableApplicationCache(
457 !command_line.HasSwitch(switches::kDisableApplicationCache));
458
459 WebRuntimeFeatures::enableNotifications(
460 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
461
462 WebRuntimeFeatures::enableLocalStorage(
463 !command_line.HasSwitch(switches::kDisableLocalStorage));
464 WebRuntimeFeatures::enableSessionStorage(
465 !command_line.HasSwitch(switches::kDisableSessionStorage));
466
467 WebRuntimeFeatures::enableIndexedDatabase(
468 !command_line.HasSwitch(switches::kDisableIndexedDatabase));
469
470 WebRuntimeFeatures::enableGeolocation(
471 !command_line.HasSwitch(switches::kDisableGeolocation));
472
473 WebKit::WebRuntimeFeatures::enableMediaStream(
474 command_line.HasSwitch(switches::kEnableMediaStream));
475
[email protected]efdc0282011-08-26 00:36:12476 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
477 command_line.HasSwitch(switches::kEnableFullScreen));
478
[email protected]d1b8fccc2011-08-03 01:20:13479#if defined(OS_CHROMEOS)
480 // TODO(crogers): enable once Web Audio has been tested and optimized.
481 WebRuntimeFeatures::enableWebAudio(false);
482#else
483 WebRuntimeFeatures::enableWebAudio(
484 !command_line.HasSwitch(switches::kDisableWebAudio));
485#endif
486
487 WebRuntimeFeatures::enablePushState(true);
488
489#ifdef TOUCH_UI
490 WebRuntimeFeatures::enableTouch(true);
491 WebKit::WebPopupMenu::setMinimumRowHeight(kPopupListBoxMinimumRowHeight);
492#else
493 // TODO(saintlou): in the future touch should always be enabled
494 WebRuntimeFeatures::enableTouch(false);
495#endif
496
497 WebRuntimeFeatures::enableDeviceMotion(
498 command_line.HasSwitch(switches::kEnableDeviceMotion));
499
500 WebRuntimeFeatures::enableDeviceOrientation(
501 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
502
503 WebRuntimeFeatures::enableSpeechInput(
504 !command_line.HasSwitch(switches::kDisableSpeechInput));
505
506 WebRuntimeFeatures::enableFileSystem(
507 !command_line.HasSwitch(switches::kDisableFileSystem));
508
509 WebRuntimeFeatures::enableJavaScriptI18NAPI(
510 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
511
512 WebRuntimeFeatures::enableQuota(true);
513
514 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
515}
516
[email protected]a95986a82010-12-24 06:19:28517bool RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50518 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
519 RenderProcessObserver* observer;
520 while ((observer = it.GetNext()) != NULL) {
521 if (observer->OnControlMessageReceived(msg))
522 return true;
523 }
524
[email protected]70c19a932010-05-14 12:59:11525 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31526 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28527 return true;
[email protected]70c19a932010-05-14 12:59:11528 if (indexed_db_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28529 return true;
[email protected]1edc16b82009-04-07 17:45:54530
[email protected]a95986a82010-12-24 06:19:28531 bool handled = true;
[email protected]8930d472009-02-21 08:05:28532 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
[email protected]9d797f32010-04-23 07:17:54533 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
534 OnSetZoomLevelForCurrentURL)
[email protected]8930d472009-02-21 08:05:28535 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55536 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28537 // TODO(port): removed from render_messages_internal.h;
538 // is there a new non-windows message I should add here?
539 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45540 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09541 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45542 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]a95986a82010-12-24 06:19:28543 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28544 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28545 return handled;
initial.commit09911bf2008-07-26 23:55:29546}
547
548void RenderThread::OnSetNextPageID(int32 next_page_id) {
549 // This should only be called at process initialization time, so we shouldn't
550 // have to worry about thread-safety.
551 RenderView::SetNextPageID(next_page_id);
552}
553
[email protected]b9ab10c2009-08-07 18:09:55554// Called when to register CSS Color name->system color mappings.
555// We update the colors one by one and then tell WebKit to refresh all render
556// views.
557void RenderThread::OnSetCSSColors(
558 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59559 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55560 size_t num_colors = colors.size();
561 scoped_array<WebKit::WebColorName> color_names(
562 new WebKit::WebColorName[num_colors]);
563 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
564 size_t i = 0;
565 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
566 colors.begin();
567 it != colors.end();
568 ++it, ++i) {
569 color_names[i] = it->first;
570 web_colors[i] = it->second;
571 }
572 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
573}
574
[email protected]4e6419c2010-01-15 04:50:34575void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47576 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33577 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29578 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26579 this,
580 params.parent_window,
581 MSG_ROUTING_NONE,
582 params.renderer_preferences,
583 params.web_preferences,
584 new SharedRenderViewCounter(0),
585 params.view_id,
586 params.session_storage_namespace_id,
587 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27588}
[email protected]4274e582009-01-27 22:09:56589
[email protected]c40acc32010-01-14 01:02:53590void RenderThread::CloseCurrentConnections() {
591 Send(new ViewHostMsg_CloseCurrentConnections());
[email protected]b07f29092009-06-05 07:33:21592}
593
594void RenderThread::SetCacheMode(bool enabled) {
595 Send(new ViewHostMsg_SetCacheMode(enabled));
596}
597
[email protected]0e34852a2011-02-15 23:14:44598void RenderThread::ClearCache(bool preserve_ssl_host_info) {
[email protected]c5d79342010-06-05 01:27:34599 int rv;
[email protected]0e34852a2011-02-15 23:14:44600 Send(new ViewHostMsg_ClearCache(preserve_ssl_host_info, &rv));
[email protected]c5d79342010-06-05 01:27:34601}
602
[email protected]8990e4712011-03-28 20:50:55603void RenderThread::ClearHostResolverCache() {
604 int rv;
605 Send(new ViewHostMsg_ClearHostResolverCache(&rv));
606}
607
608void RenderThread::ClearPredictorCache() {
609 int rv;
610 Send(new ViewHostMsg_ClearPredictorCache(&rv));
611}
612
[email protected]12893c32010-08-19 17:30:54613void RenderThread::EnableSpdy(bool enable) {
614 Send(new ViewHostMsg_EnableSpdy(enable));
615}
616
[email protected]b42a9f62011-06-08 20:36:04617GpuChannelHost* RenderThread::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50618 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35619 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22620 // Do nothing if we already have a GPU channel or are already
621 // establishing one.
[email protected]e09cee42010-11-09 01:50:08622 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
623 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04624 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35625
626 // Recreate the channel if it has been lost.
[email protected]e09cee42010-11-09 01:50:08627 if (gpu_channel_->state() == GpuChannelHost::kLost)
[email protected]6217d392010-03-25 22:08:35628 gpu_channel_ = NULL;
629 }
630
631 if (!gpu_channel_.get())
632 gpu_channel_ = new GpuChannelHost;
633
634 // Ask the browser for the channel name.
[email protected]b42a9f62011-06-08 20:36:04635 IPC::ChannelHandle channel_handle;
636 base::ProcessHandle renderer_process_for_gpu;
637 GPUInfo gpu_info;
638 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
639 &channel_handle,
640 &renderer_process_for_gpu,
641 &gpu_info)) ||
642 channel_handle.name.empty() ||
643 renderer_process_for_gpu == base::kNullProcessHandle) {
644 // Otherwise cancel the connection.
645 gpu_channel_ = NULL;
646 return NULL;
647 }
[email protected]6217d392010-03-25 22:08:35648
[email protected]b42a9f62011-06-08 20:36:04649 gpu_channel_->set_gpu_info(gpu_info);
650 content::GetContentClient()->SetGpuInfo(gpu_info);
651
652 // Connect to the GPU process if a channel name was received.
653 gpu_channel_->Connect(channel_handle, renderer_process_for_gpu);
654
[email protected]f9a2b2fe2010-07-15 21:13:23655 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34656}
657
[email protected]6217d392010-03-25 22:08:35658GpuChannelHost* RenderThread::GetGpuChannel() {
659 if (!gpu_channel_.get())
660 return NULL;
661
[email protected]e09cee42010-11-09 01:50:08662 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35663 return NULL;
664
665 return gpu_channel_.get();
666}
667
[email protected]bee16aab2009-08-26 15:55:03668void RenderThread::IdleHandler() {
[email protected]e63c4d72011-05-31 22:38:29669#if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41670 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22671#endif
[email protected]bee16aab2009-08-26 15:55:03672
[email protected]f5e4b9bf2009-10-08 00:59:59673 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22674
675 // Schedule next invocation.
676 // Dampen the delay using the algorithm:
677 // delay = delay + 1 / (delay + 2)
678 // Using floor(delay) has a dampening effect such as:
679 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
680 // Note that idle_notification_delay_in_s_ would be reset to
681 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36682 ScheduleIdleHandler(idle_notification_delay_in_s_ +
683 1.0 / (idle_notification_delay_in_s_ + 2.0));
[email protected]6779aa12011-03-29 17:32:24684
685 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
[email protected]71d6d852009-12-07 22:12:36686}
[email protected]05001182009-09-15 23:34:22687
[email protected]71d6d852009-12-07 22:12:36688void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
689 idle_notification_delay_in_s_ = initial_delay_s;
690 idle_timer_.Stop();
[email protected]d323a172011-09-02 18:23:02691 idle_timer_.Start(FROM_HERE,
[email protected]71d6d852009-12-07 22:12:36692 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
693 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03694}
695
[email protected]b78e168b2009-09-21 22:05:45696void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59697 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27698 // The call below will cause a GetPlugins call with refresh=true, but at this
699 // point we already know that the browser has refreshed its list, so disable
700 // refresh temporarily to prevent each renderer process causing the list to be
701 // regenerated.
702 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45703 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27704 plugin_refresh_allowed_ = true;
705}
[email protected]85c55dc2009-11-06 03:05:46706
[email protected]6eac57a2011-07-12 21:15:09707void RenderThread::OnNetworkStateChanged(bool online) {
708 EnsureWebKitInitialized();
709 WebNetworkStateNotifier::setOnLine(online);
710}
711
[email protected]c6a7b862010-08-20 22:19:38712scoped_refptr<base::MessageLoopProxy>
713RenderThread::GetFileThreadMessageLoopProxy() {
714 DCHECK(message_loop() == MessageLoop::current());
715 if (!file_thread_.get()) {
716 file_thread_.reset(new base::Thread("Renderer::FILE"));
717 file_thread_->Start();
718 }
719 return file_thread_->message_loop_proxy();
720}
[email protected]af7eb3fb2010-09-23 21:31:06721
[email protected]6779aa12011-03-29 17:32:24722void RenderThread::RegisterExtension(v8::Extension* extension) {
[email protected]af7eb3fb2010-09-23 21:31:06723 WebScriptController::registerExtension(extension);
[email protected]6779aa12011-03-29 17:32:24724 v8_extensions_.insert(extension->name());
[email protected]af7eb3fb2010-09-23 21:31:06725}
[email protected]c6e27b92011-04-30 17:03:43726
727bool RenderThread::IsRegisteredExtension(
728 const std::string& v8_extension_name) const {
729 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
730}