blob: adfaa5af3a35810553a449a02a9658cdb55ff7d3 [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]f1a29a02011-10-06 23:08:445#include "content/renderer/render_thread_impl.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]d3fd748b2011-09-20 17:39:1730#include "content/common/npobject_util.h"
[email protected]105303e2011-03-14 22:16:1031#include "content/common/plugin_messages.h"
[email protected]60916042011-03-19 00:43:3632#include "content/common/renderer_preferences.h"
[email protected]94dc971d2011-03-05 19:08:3233#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5034#include "content/common/view_messages.h"
[email protected]e93e04e2011-03-14 00:27:1035#include "content/common/web_database_observer_impl.h"
[email protected]d344114c2011-10-01 01:24:3436#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3637#include "content/public/renderer/render_process_observer.h"
38#include "content/public/renderer/render_view_visitor.h"
[email protected]82622452011-07-22 09:57:2039#include "content/renderer/devtools_agent_filter.h"
[email protected]a9fb30aa2011-10-06 06:58:4640#include "content/renderer/gpu/compositor_thread.h"
[email protected]a03a2222011-05-25 21:26:4041#include "content/renderer/gpu/gpu_channel_host.h"
[email protected]230b7ef2011-03-16 22:30:1942#include "content/renderer/indexed_db_dispatcher.h"
[email protected]f7eb0a392011-07-12 10:19:5143#include "content/renderer/media/audio_input_message_filter.h"
44#include "content/renderer/media/audio_message_filter.h"
[email protected]80b161a2011-06-27 17:42:1145#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3446#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3547#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0548#include "content/renderer/render_process_impl.h"
[email protected]60916042011-03-19 00:43:3649#include "content/renderer/render_view.h"
[email protected]acb94722011-03-18 01:33:3450#include "content/renderer/renderer_webidbfactory_impl.h"
[email protected]8d6cba42011-09-02 10:05:1951#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]46f36a492010-07-28 19:36:4152#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1753#include "ipc/ipc_platform_file.h"
[email protected]620161e2011-03-07 18:05:2654#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5455#include "net/base/net_util.h"
[email protected]1b1f3eb2009-12-01 13:48:0456#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]8bd0fe62011-01-17 06:44:3757#include "third_party/WebKit/Source/WebKit/chromium/public/WebColor.h"
[email protected]8bd0fe62011-01-17 06:44:3758#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
59#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3760#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
61#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0962#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]4bd55a32011-07-28 13:28:3863#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3764#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
65#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]8bd0fe62011-01-17 06:44:3766#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
67#include "third_party/WebKit/Source/WebKit/chromium/public/WebString.h"
68#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]18ad6772011-09-20 21:51:3269#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:5070#include "v8/include/v8.h"
[email protected]06533c0b2009-03-05 21:39:1171#include "webkit/extensions/v8/playback_extension.h"
[email protected]d471190a2011-02-16 14:52:3072#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5073
[email protected]94dc971d2011-03-05 19:08:3274// TODO(port)
75#if defined(OS_WIN)
[email protected]79fff822011-09-20 03:33:2176#include "content/common/child_process_messages.h"
[email protected]94dc971d2011-03-05 19:08:3277#else
[email protected]3b63f8f42011-03-28 01:54:1578#include "base/memory/scoped_handle.h"
[email protected]099587b72011-09-20 00:40:5079#include "content/common/np_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3280#endif
81
[email protected]da00a2882009-03-09 17:51:1982#if defined(OS_WIN)
83#include <windows.h>
84#include <objbase.h>
85#endif
86
[email protected]6217d392010-03-25 22:08:3587#if defined(OS_POSIX)
88#include "ipc/ipc_channel_posix.h"
89#endif
90
[email protected]b6cb3a842011-06-24 18:28:4191using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:0192using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:0993using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:0094using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:5195using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:4796using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:3497using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:0398using WebKit::WebView;
[email protected]64ffa0442011-10-03 22:08:3699using content::RenderProcessObserver;
initial.commit09911bf2008-07-26 23:55:29100
[email protected]42f1d7822009-07-23 18:17:55101namespace {
[email protected]bee16aab2009-08-26 15:55:03102static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
103
[email protected]4bd55a32011-07-28 13:28:38104#if defined(TOUCH_UI)
105static const int kPopupListBoxMinimumRowHeight = 60;
106#endif
107
[email protected]f1a29a02011-10-06 23:08:44108// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16109// incorrectly from the wrong thread.
[email protected]f1a29a02011-10-06 23:08:44110static base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> > lazy_tls(
[email protected]94f9a0f682009-06-15 18:30:30111 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54112
[email protected]64ffa0442011-10-03 22:08:36113class RenderViewZoomer : public content::RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51114 public:
[email protected]b75b8292010-10-01 07:28:25115 RenderViewZoomer(const GURL& url, double zoom_level)
[email protected]9d797f32010-04-23 07:17:54116 : zoom_level_(zoom_level) {
117 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51118 }
119
120 virtual bool Visit(RenderView* render_view) {
[email protected]b6cb3a842011-06-24 18:28:41121 WebView* webview = render_view->webview();
122 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25123
124 // Don't set zoom level for full-page plugin since they don't use the same
125 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41126 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25127 return true;
128
[email protected]b6cb3a842011-06-24 18:28:41129 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51130 webview->setZoomLevel(false, zoom_level_);
131 return true;
132 }
133
134 private:
135 std::string host_;
[email protected]b75b8292010-10-01 07:28:25136 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51137
138 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
139};
[email protected]0478d0162010-08-28 08:29:40140
[email protected]42f1d7822009-07-23 18:17:55141} // namespace
142
[email protected]d1b8fccc2011-08-03 01:20:13143static void* CreateHistogram(
144 const char *name, int min, int max, size_t buckets) {
145 if (min <= 0)
146 min = 1;
147 base::Histogram* histogram = base::Histogram::FactoryGet(
148 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
149 return histogram;
150}
151
152static void AddHistogramSample(void* hist, int sample) {
153 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
154 histogram->Add(sample);
155}
156
[email protected]f1a29a02011-10-06 23:08:44157RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06158 return lazy_tls.Pointer()->Get();
159}
160
[email protected]42f1d7822009-07-23 18:17:55161// When we run plugins in process, we actually run them on the render thread,
162// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44163RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55164 Init();
165}
166
[email protected]f1a29a02011-10-06 23:08:44167RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55168 : ChildThread(channel_name) {
169 Init();
170}
[email protected]5fa1c542009-05-05 20:36:07171
[email protected]f1a29a02011-10-06 23:08:44172void RenderThreadImpl::Init() {
173 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33174
[email protected]53c607c2011-03-21 23:19:04175#if defined(OS_MACOSX)
176 // On Mac, the select popups are rendered by the browser.
177 WebKit::WebView::setUseExternalPopupMenus(true);
178#endif
179
[email protected]94f9a0f682009-06-15 18:30:30180 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50181#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34182 // If you are running plugins in this thread you need COM active but in
183 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28184 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34185 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50186#endif
initial.commit09911bf2008-07-26 23:55:29187
[email protected]31f87132010-04-21 23:36:21188 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50189 suspend_webkit_shared_timer_ = true;
190 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55191 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03192 widget_count_ = 0;
193 hidden_widget_count_ = 0;
[email protected]6779aa12011-03-29 17:32:24194 idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS;
[email protected]f1a29a02011-10-06 23:08:44195 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThreadImpl>(this));
[email protected]8d86fce2009-02-26 23:37:55196
[email protected]526476902011-10-06 20:34:06197 appcache_dispatcher_.reset(new AppCacheDispatcher(Get()));
[email protected]70c19a932010-05-14 12:59:11198 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13199
[email protected]017022b2009-07-27 23:06:34200 db_message_filter_ = new DBMessageFilter();
201 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13202
[email protected]80b161a2011-06-27 17:42:11203 vc_manager_ = new VideoCaptureImplManager();
204 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46205
[email protected]f7eb0a392011-07-12 10:19:51206 audio_input_message_filter_ = new AudioInputMessageFilter();
207 AddFilter(audio_input_message_filter_.get());
208
209 audio_message_filter_ = new AudioMessageFilter();
210 AddFilter(audio_message_filter_.get());
211
[email protected]82622452011-07-22 09:57:20212 devtools_agent_message_filter_ = new DevToolsAgentFilter();
213 AddFilter(devtools_agent_message_filter_.get());
214
[email protected]8d97ade2011-04-14 18:17:08215 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24216
[email protected]f1a29a02011-10-06 23:08:44217 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29218}
219
[email protected]f1a29a02011-10-06 23:08:44220RenderThreadImpl::~RenderThreadImpl() {
[email protected]1223d6ef2011-03-28 16:47:50221 FOR_EACH_OBSERVER(
222 RenderProcessObserver, observers_, OnRenderProcessShutdown());
223
[email protected]12cbfda32010-01-30 01:04:25224 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08225 if (web_database_observer_impl_.get())
226 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25227
[email protected]8d86fce2009-02-26 23:37:55228 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20229 RemoveFilter(devtools_agent_message_filter_.get());
230 devtools_agent_message_filter_ = NULL;
231
[email protected]f7eb0a392011-07-12 10:19:51232 RemoveFilter(audio_input_message_filter_.get());
233 audio_input_message_filter_ = NULL;
234
235 RemoveFilter(audio_message_filter_.get());
236 audio_message_filter_ = NULL;
237
[email protected]80b161a2011-06-27 17:42:11238 RemoveFilter(vc_manager_->video_capture_message_filter());
239
[email protected]017022b2009-07-27 23:06:34240 RemoveFilter(db_message_filter_.get());
241 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25242
[email protected]c6a7b862010-08-20 22:19:38243 // Shutdown the file thread if it's running.
244 if (file_thread_.get())
245 file_thread_->Stop();
246
[email protected]a9fb30aa2011-10-06 06:58:46247 if (compositor_thread_.get()) {
248 RemoveFilter(compositor_thread_->GetMessageFilter());
249 compositor_thread_.reset();
250 }
251
[email protected]8d6cba42011-09-02 10:05:19252 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22253 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22254
[email protected]94f9a0f682009-06-15 18:30:30255 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30256
[email protected]8d86fce2009-02-26 23:37:55257 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50258#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29259 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50260 NPChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34261 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28262 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34263 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50264#endif
initial.commit09911bf2008-07-26 23:55:29265}
266
[email protected]f1a29a02011-10-06 23:08:44267bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14268 // Certain synchronous messages cannot always be processed synchronously by
269 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50270 // This could cause a complete hang of Chrome if a windowed plug-in is trying
271 // to communicate with the renderer thread since the browser's UI thread
272 // could be stuck (within a Windows API call) trying to synchronously
273 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14274 // thread while the browser is processing this request. This creates an
275 // opportunity for re-entrancy into WebKit, so we need to take care to disable
276 // callbacks, timers, and pending network loads that could trigger such
277 // callbacks.
[email protected]38b592902011-04-16 02:08:42278 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50279 if (msg->is_sync()) {
280 if (msg->is_caller_pumping_messages()) {
281 pumping_events = true;
282 } else {
[email protected]38b592902011-04-16 02:08:42283 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
284 msg->type() == ViewHostMsg_GetRawCookies::ID ||
285 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
286 content::GetContentClient()->renderer()->
287 ShouldPumpEventsDuringCookieMessage()) {
288 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50289 }
290 }
[email protected]c1f50aa2010-02-18 03:46:57291 }
292
[email protected]80fc08c52010-03-09 07:43:50293 bool suspend_webkit_shared_timer = true; // default value
294 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57295
[email protected]80fc08c52010-03-09 07:43:50296 bool notify_webkit_of_modal_loop = true; // default value
297 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
298
299 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57300
301 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50302 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19303 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57304
[email protected]39065d012010-07-09 11:22:46305 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57306 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57307
[email protected]80fc08c52010-03-09 07:43:50308 RenderWidget* widget =
309 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
310 if (widget) {
311 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57312 PluginChannelHost::Broadcast(
313 new PluginMsg_SignalModalDialogEvent(host_window));
314 }
315 }
316
317 bool rv = ChildThread::Send(msg);
318
319 if (pumping_events) {
320 if (host_window) {
321 PluginChannelHost::Broadcast(
322 new PluginMsg_ResetModalDialogEvent(host_window));
323 }
324
[email protected]39065d012010-07-09 11:22:46325 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57326 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57327
[email protected]80fc08c52010-03-09 07:43:50328 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19329 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57330 }
331
332 return rv;
333}
334
[email protected]f1a29a02011-10-06 23:08:44335MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06336 return message_loop();
337}
338
[email protected]f1a29a02011-10-06 23:08:44339IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06340 return channel();
341}
342
[email protected]f1a29a02011-10-06 23:08:44343ResourceDispatcher* RenderThreadImpl::GetResourceDispatcher() {
[email protected]526476902011-10-06 20:34:06344 return resource_dispatcher();
345}
346
[email protected]f1a29a02011-10-06 23:08:44347std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06348 // The browser process should have passed the locale to the renderer via the
349 // --lang command line flag. In single process mode, this will return the
350 // wrong value. TODO(tc): Fix this for single process mode.
351 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
352 const std::string& lang =
353 parsed_command_line.GetSwitchValueASCII(switches::kLang);
354 DCHECK(!lang.empty() ||
355 (!parsed_command_line.HasSwitch(switches::kRendererProcess) &&
356 !parsed_command_line.HasSwitch(switches::kPluginProcess)));
357 return lang;
358}
359
[email protected]f1a29a02011-10-06 23:08:44360void RenderThreadImpl::AddRoute(int32 routing_id,
361 IPC::Channel::Listener* listener) {
[email protected]c1f50aa2010-02-18 03:46:57362 widget_count_++;
363 return ChildThread::AddRoute(routing_id, listener);
364}
365
[email protected]f1a29a02011-10-06 23:08:44366void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]c1f50aa2010-02-18 03:46:57367 widget_count_--;
368 return ChildThread::RemoveRoute(routing_id);
369}
370
[email protected]f1a29a02011-10-06 23:08:44371void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55372 channel()->AddFilter(filter);
373}
374
[email protected]f1a29a02011-10-06 23:08:44375void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55376 channel()->RemoveFilter(filter);
377}
378
[email protected]f1a29a02011-10-06 23:08:44379void RenderThreadImpl::SetOutgoingMessageFilter(
[email protected]526476902011-10-06 20:34:06380 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
381}
382
[email protected]f1a29a02011-10-06 23:08:44383void RenderThreadImpl::AddObserver(content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06384 observers_.AddObserver(observer);
385}
386
[email protected]f1a29a02011-10-06 23:08:44387void RenderThreadImpl::RemoveObserver(
388 content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06389 observers_.RemoveObserver(observer);
390}
391
[email protected]f1a29a02011-10-06 23:08:44392void RenderThreadImpl::WidgetHidden() {
[email protected]bee16aab2009-08-26 15:55:03393 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50394 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24395
396 if (!content::GetContentClient()->renderer()->
397 RunIdleHandlerWhenWidgetsHidden()) {
398 return;
399 }
400
401 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]71d6d852009-12-07 22:12:36402 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03403}
404
[email protected]f1a29a02011-10-06 23:08:44405void RenderThreadImpl::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08406 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03407 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24408 if (!content::GetContentClient()->renderer()->
409 RunIdleHandlerWhenWidgetsHidden()) {
410 return;
411 }
[email protected]bee16aab2009-08-26 15:55:03412
[email protected]6779aa12011-03-29 17:32:24413 idle_timer_.Stop();
[email protected]e4be2dd2010-12-14 00:44:39414}
415
[email protected]f1a29a02011-10-06 23:08:44416void RenderThreadImpl::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
[email protected]a9fb30aa2011-10-06 06:58:46427 compositor_thread_.reset(new CompositorThread(this));
428 AddFilter(compositor_thread_->GetMessageFilter());
429
[email protected]d1b8fccc2011-08-03 01:20:13430 WebScriptController::enableV8SingleThreadMode();
431
432 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
433
434 webkit_glue::EnableWebCoreLogChannels(
435 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
436
[email protected]d1b8fccc2011-08-03 01:20:13437 if (command_line.HasSwitch(switches::kPlaybackMode) ||
438 command_line.HasSwitch(switches::kRecordMode) ||
439 command_line.HasSwitch(switches::kNoJsRandomness)) {
440 RegisterExtension(extensions_v8::PlaybackExtension::Get());
441 }
442
[email protected]526476902011-10-06 20:34:06443 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(Get()));
[email protected]d1b8fccc2011-08-03 01:20:13444 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
445
446 WebRuntimeFeatures::enableSockets(
447 !command_line.HasSwitch(switches::kDisableWebSockets));
448
449 WebRuntimeFeatures::enableDatabase(
450 !command_line.HasSwitch(switches::kDisableDatabases));
451
452 WebRuntimeFeatures::enableDataTransferItems(
453 !command_line.HasSwitch(switches::kDisableDataTransferItems));
454
455 WebRuntimeFeatures::enableApplicationCache(
456 !command_line.HasSwitch(switches::kDisableApplicationCache));
457
458 WebRuntimeFeatures::enableNotifications(
459 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
460
461 WebRuntimeFeatures::enableLocalStorage(
462 !command_line.HasSwitch(switches::kDisableLocalStorage));
463 WebRuntimeFeatures::enableSessionStorage(
464 !command_line.HasSwitch(switches::kDisableSessionStorage));
465
466 WebRuntimeFeatures::enableIndexedDatabase(
467 !command_line.HasSwitch(switches::kDisableIndexedDatabase));
468
469 WebRuntimeFeatures::enableGeolocation(
470 !command_line.HasSwitch(switches::kDisableGeolocation));
471
472 WebKit::WebRuntimeFeatures::enableMediaStream(
473 command_line.HasSwitch(switches::kEnableMediaStream));
474
[email protected]efdc0282011-08-26 00:36:12475 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21476 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12477
[email protected]d1b8fccc2011-08-03 01:20:13478#if defined(OS_CHROMEOS)
479 // TODO(crogers): enable once Web Audio has been tested and optimized.
480 WebRuntimeFeatures::enableWebAudio(false);
481#else
482 WebRuntimeFeatures::enableWebAudio(
483 !command_line.HasSwitch(switches::kDisableWebAudio));
484#endif
485
486 WebRuntimeFeatures::enablePushState(true);
487
488#ifdef TOUCH_UI
489 WebRuntimeFeatures::enableTouch(true);
490 WebKit::WebPopupMenu::setMinimumRowHeight(kPopupListBoxMinimumRowHeight);
491#else
492 // TODO(saintlou): in the future touch should always be enabled
493 WebRuntimeFeatures::enableTouch(false);
494#endif
495
496 WebRuntimeFeatures::enableDeviceMotion(
497 command_line.HasSwitch(switches::kEnableDeviceMotion));
498
499 WebRuntimeFeatures::enableDeviceOrientation(
500 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
501
502 WebRuntimeFeatures::enableSpeechInput(
503 !command_line.HasSwitch(switches::kDisableSpeechInput));
504
505 WebRuntimeFeatures::enableFileSystem(
506 !command_line.HasSwitch(switches::kDisableFileSystem));
507
508 WebRuntimeFeatures::enableJavaScriptI18NAPI(
509 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
510
511 WebRuntimeFeatures::enableQuota(true);
512
513 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
514}
515
[email protected]f1a29a02011-10-06 23:08:44516void RenderThreadImpl::RecordUserMetrics(const std::string& action) {
[email protected]526476902011-10-06 20:34:06517 Send(new ViewHostMsg_UserMetricsRecordAction(action));
518}
519
[email protected]f1a29a02011-10-06 23:08:44520void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06521 WebScriptController::registerExtension(extension);
522 v8_extensions_.insert(extension->name());
523}
524
[email protected]f1a29a02011-10-06 23:08:44525bool RenderThreadImpl::IsRegisteredExtension(
[email protected]526476902011-10-06 20:34:06526 const std::string& v8_extension_name) const {
527 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
528}
529
[email protected]f1a29a02011-10-06 23:08:44530void RenderThreadImpl::ScheduleIdleHandler(double initial_delay_s) {
[email protected]526476902011-10-06 20:34:06531 idle_notification_delay_in_s_ = initial_delay_s;
532 idle_timer_.Stop();
533 idle_timer_.Start(FROM_HERE,
534 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
[email protected]f1a29a02011-10-06 23:08:44535 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06536}
537
[email protected]f1a29a02011-10-06 23:08:44538void RenderThreadImpl::IdleHandler() {
[email protected]526476902011-10-06 20:34:06539 #if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
540 MallocExtension::instance()->ReleaseFreeMemory();
541#endif
542
543 v8::V8::IdleNotification();
544
545 // Schedule next invocation.
546 // Dampen the delay using the algorithm:
547 // delay = delay + 1 / (delay + 2)
548 // Using floor(delay) has a dampening effect such as:
549 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
550 // Note that idle_notification_delay_in_s_ would be reset to
[email protected]f1a29a02011-10-06 23:08:44551 // kInitialIdleHandlerDelayS in RenderThreadImpl::WidgetHidden.
[email protected]526476902011-10-06 20:34:06552 ScheduleIdleHandler(idle_notification_delay_in_s_ +
553 1.0 / (idle_notification_delay_in_s_ + 2.0));
554
555 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
556}
557
[email protected]f1a29a02011-10-06 23:08:44558double RenderThreadImpl::GetIdleNotificationDelayInS() const {
[email protected]526476902011-10-06 20:34:06559 return idle_notification_delay_in_s_;
560}
561
[email protected]f1a29a02011-10-06 23:08:44562void RenderThreadImpl::SetIdleNotificationDelayInS(
[email protected]526476902011-10-06 20:34:06563 double idle_notification_delay_in_s) {
564 idle_notification_delay_in_s_ = idle_notification_delay_in_s;
[email protected]4a7d6392011-09-19 20:55:08565}
566
567#if defined(OS_WIN)
[email protected]f1a29a02011-10-06 23:08:44568void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:06569 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:21570}
571
[email protected]f1a29a02011-10-06 23:08:44572void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:06573 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:08574}
[email protected]526476902011-10-06 20:34:06575
[email protected]4a7d6392011-09-19 20:55:08576#endif // OS_WIN
577
[email protected]f1a29a02011-10-06 23:08:44578int32 RenderThreadImpl::RoutingIDForCurrentContext() {
[email protected]526476902011-10-06 20:34:06579 int32 routing_id = MSG_ROUTING_CONTROL;
580 if (v8::Context::InContext()) {
581 WebFrame* frame = WebFrame::frameForCurrentContext();
582 if (frame) {
583 RenderView* view = RenderView::FromWebView(frame->view());
584 if (view)
585 routing_id = view->routing_id();
586 }
587 } else {
588 DLOG(WARNING) << "Not called within a script context!";
589 }
590 return routing_id;
591}
592
[email protected]f1a29a02011-10-06 23:08:44593void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:06594 suspend_webkit_shared_timer_ = false;
595}
596
[email protected]f1a29a02011-10-06 23:08:44597void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:06598 notify_webkit_of_modal_loop_ = false;
599}
600
[email protected]f1a29a02011-10-06 23:08:44601void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const GURL& url,
602 double zoom_level) {
[email protected]526476902011-10-06 20:34:06603 RenderViewZoomer zoomer(url, zoom_level);
604 RenderView::ForEach(&zoomer);
605}
606
[email protected]f1a29a02011-10-06 23:08:44607void RenderThreadImpl::OnDOMStorageEvent(
[email protected]526476902011-10-06 20:34:06608 const DOMStorageMsg_Event_Params& params) {
609 if (!dom_storage_event_dispatcher_.get())
610 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
611 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key,
612 params.old_value, params.new_value, params.origin, params.url,
613 params.storage_type == DOM_STORAGE_LOCAL);
614}
615
[email protected]f1a29a02011-10-06 23:08:44616bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50617 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
618 RenderProcessObserver* observer;
619 while ((observer = it.GetNext()) != NULL) {
620 if (observer->OnControlMessageReceived(msg))
621 return true;
622 }
623
[email protected]70c19a932010-05-14 12:59:11624 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31625 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28626 return true;
[email protected]70c19a932010-05-14 12:59:11627 if (indexed_db_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28628 return true;
[email protected]1edc16b82009-04-07 17:45:54629
[email protected]a95986a82010-12-24 06:19:28630 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:44631 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:54632 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
633 OnSetZoomLevelForCurrentURL)
[email protected]8930d472009-02-21 08:05:28634 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55635 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28636 // TODO(port): removed from render_messages_internal.h;
637 // is there a new non-windows message I should add here?
638 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45639 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09640 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45641 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]a95986a82010-12-24 06:19:28642 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28643 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28644 return handled;
initial.commit09911bf2008-07-26 23:55:29645}
646
[email protected]f1a29a02011-10-06 23:08:44647void RenderThreadImpl::OnSetNextPageID(int32 next_page_id) {
initial.commit09911bf2008-07-26 23:55:29648 // This should only be called at process initialization time, so we shouldn't
649 // have to worry about thread-safety.
650 RenderView::SetNextPageID(next_page_id);
651}
652
[email protected]b9ab10c2009-08-07 18:09:55653// Called when to register CSS Color name->system color mappings.
654// We update the colors one by one and then tell WebKit to refresh all render
655// views.
[email protected]f1a29a02011-10-06 23:08:44656void RenderThreadImpl::OnSetCSSColors(
[email protected]b9ab10c2009-08-07 18:09:55657 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59658 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55659 size_t num_colors = colors.size();
660 scoped_array<WebKit::WebColorName> color_names(
661 new WebKit::WebColorName[num_colors]);
662 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
663 size_t i = 0;
664 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
665 colors.begin();
666 it != colors.end();
667 ++it, ++i) {
668 color_names[i] = it->first;
669 web_colors[i] = it->second;
670 }
671 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
672}
673
[email protected]f1a29a02011-10-06 23:08:44674void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47675 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33676 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29677 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26678 this,
679 params.parent_window,
680 MSG_ROUTING_NONE,
681 params.renderer_preferences,
682 params.web_preferences,
683 new SharedRenderViewCounter(0),
684 params.view_id,
685 params.session_storage_namespace_id,
686 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27687}
[email protected]4274e582009-01-27 22:09:56688
[email protected]f1a29a02011-10-06 23:08:44689GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50690 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35691 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22692 // Do nothing if we already have a GPU channel or are already
693 // establishing one.
[email protected]e09cee42010-11-09 01:50:08694 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
695 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04696 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35697
698 // Recreate the channel if it has been lost.
[email protected]e09cee42010-11-09 01:50:08699 if (gpu_channel_->state() == GpuChannelHost::kLost)
[email protected]6217d392010-03-25 22:08:35700 gpu_channel_ = NULL;
701 }
702
703 if (!gpu_channel_.get())
704 gpu_channel_ = new GpuChannelHost;
705
706 // Ask the browser for the channel name.
[email protected]b42a9f62011-06-08 20:36:04707 IPC::ChannelHandle channel_handle;
708 base::ProcessHandle renderer_process_for_gpu;
709 GPUInfo gpu_info;
710 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
711 &channel_handle,
712 &renderer_process_for_gpu,
713 &gpu_info)) ||
714 channel_handle.name.empty() ||
715 renderer_process_for_gpu == base::kNullProcessHandle) {
716 // Otherwise cancel the connection.
717 gpu_channel_ = NULL;
718 return NULL;
719 }
[email protected]6217d392010-03-25 22:08:35720
[email protected]b42a9f62011-06-08 20:36:04721 gpu_channel_->set_gpu_info(gpu_info);
722 content::GetContentClient()->SetGpuInfo(gpu_info);
723
724 // Connect to the GPU process if a channel name was received.
725 gpu_channel_->Connect(channel_handle, renderer_process_for_gpu);
726
[email protected]f9a2b2fe2010-07-15 21:13:23727 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34728}
729
[email protected]f1a29a02011-10-06 23:08:44730GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]6217d392010-03-25 22:08:35731 if (!gpu_channel_.get())
732 return NULL;
733
[email protected]e09cee42010-11-09 01:50:08734 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35735 return NULL;
736
737 return gpu_channel_.get();
738}
739
[email protected]f1a29a02011-10-06 23:08:44740void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59741 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27742 // The call below will cause a GetPlugins call with refresh=true, but at this
743 // point we already know that the browser has refreshed its list, so disable
744 // refresh temporarily to prevent each renderer process causing the list to be
745 // regenerated.
746 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45747 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27748 plugin_refresh_allowed_ = true;
749}
[email protected]85c55dc2009-11-06 03:05:46750
[email protected]f1a29a02011-10-06 23:08:44751void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:09752 EnsureWebKitInitialized();
753 WebNetworkStateNotifier::setOnLine(online);
754}
755
[email protected]c6a7b862010-08-20 22:19:38756scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:44757RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]c6a7b862010-08-20 22:19:38758 DCHECK(message_loop() == MessageLoop::current());
759 if (!file_thread_.get()) {
760 file_thread_.reset(new base::Thread("Renderer::FILE"));
761 file_thread_->Start();
762 }
763 return file_thread_->message_loop_proxy();
764}