blob: 5d4e77b8c320e5558f1dfd53674cb4b3a3df01d5 [file] [log] [blame]
[email protected]d3d6325d2012-01-03 21:17:361// Copyright (c) 2012 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"
initial.commit09911bf2008-07-26 23:55:2919#include "base/shared_memory.h"
[email protected]8c380582011-12-02 03:16:1020#include "base/string_number_conversions.h" // Temporary
[email protected]1357c322010-12-30 22:18:5621#include "base/threading/thread_local.h"
[email protected]7a4de7a62010-08-17 18:38:2422#include "base/values.h"
[email protected]1a771262011-10-31 09:11:1223#include "base/win/scoped_com_initializer.h"
[email protected]e93e04e2011-03-14 00:27:1024#include "content/common/appcache/appcache_dispatcher.h"
[email protected]bdae9812011-10-15 00:33:0325#include "content/common/child_process_messages.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]c26ad882012-02-07 06:41:2029#include "content/common/gpu/client/gpu_channel_host.h"
[email protected]202b54ff2011-04-22 21:36:3830#include "content/common/gpu/gpu_messages.h"
[email protected]988aacf2012-02-12 18:10:0331#include "content/common/indexed_db/indexed_db_dispatcher.h"
32#include "content/common/indexed_db/indexed_db_message_filter.h"
33#include "content/common/indexed_db/proxy_webidbfactory_impl.h"
[email protected]d3fd748b2011-09-20 17:39:1734#include "content/common/npobject_util.h"
[email protected]105303e2011-03-14 22:16:1035#include "content/common/plugin_messages.h"
[email protected]359dfa32011-10-12 01:10:1536#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3237#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5038#include "content/common/view_messages.h"
[email protected]e93e04e2011-03-14 00:27:1039#include "content/common/web_database_observer_impl.h"
[email protected]c08950d22011-10-13 22:20:2940#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3141#include "content/public/common/renderer_preferences.h"
[email protected]d344114c2011-10-01 01:24:3442#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3643#include "content/public/renderer/render_process_observer.h"
44#include "content/public/renderer/render_view_visitor.h"
[email protected]82622452011-07-22 09:57:2045#include "content/renderer/devtools_agent_filter.h"
[email protected]a9fb30aa2011-10-06 06:58:4646#include "content/renderer/gpu/compositor_thread.h"
[email protected]f7eb0a392011-07-12 10:19:5147#include "content/renderer/media/audio_input_message_filter.h"
48#include "content/renderer/media/audio_message_filter.h"
[email protected]80b161a2011-06-27 17:42:1149#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3450#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3551#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0552#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:2853#include "content/renderer/render_view_impl.h"
[email protected]8d6cba42011-09-02 10:05:1954#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]766a7082012-02-03 23:39:1555#include "grit/content_resources.h"
[email protected]46f36a492010-07-28 19:36:4156#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1757#include "ipc/ipc_platform_file.h"
[email protected]620161e2011-03-07 18:05:2658#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5459#include "net/base/net_util.h"
[email protected]1b1f3eb2009-12-01 13:48:0460#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]e6e90dc2011-12-03 00:01:3761#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebColor.h"
[email protected]028217e2011-11-19 00:19:0862#include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositor.h"
[email protected]8bd0fe62011-01-17 06:44:3763#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
64#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3765#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
66#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0967#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]4bd55a32011-07-28 13:28:3868#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3769#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
70#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]8bd0fe62011-01-17 06:44:3771#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
[email protected]e6e90dc2011-12-03 00:01:3772#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]8bd0fe62011-01-17 06:44:3773#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]18ad6772011-09-20 21:51:3274#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:5075#include "v8/include/v8.h"
[email protected]06533c0b2009-03-05 21:39:1176#include "webkit/extensions/v8/playback_extension.h"
[email protected]d471190a2011-02-16 14:52:3077#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5078
[email protected]94dc971d2011-03-05 19:08:3279// TODO(port)
[email protected]bdae9812011-10-15 00:33:0380#if !defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1581#include "base/memory/scoped_handle.h"
[email protected]099587b72011-09-20 00:40:5082#include "content/common/np_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3283#endif
84
[email protected]da00a2882009-03-09 17:51:1985#if defined(OS_WIN)
86#include <windows.h>
87#include <objbase.h>
88#endif
89
[email protected]6217d392010-03-25 22:08:3590#if defined(OS_POSIX)
91#include "ipc/ipc_channel_posix.h"
92#endif
93
[email protected]b6cb3a842011-06-24 18:28:4194using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:0195using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:0996using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:0097using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:5198using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:4799using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:34100using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:03101using WebKit::WebView;
[email protected]64ffa0442011-10-03 22:08:36102using content::RenderProcessObserver;
initial.commit09911bf2008-07-26 23:55:29103
[email protected]42f1d7822009-07-23 18:17:55104namespace {
[email protected]da9ccfb2012-01-28 00:34:40105
106const int64 kInitialIdleHandlerDelayMs = 1000;
107const int64 kShortIdleHandlerDelayMs = 1000;
108const int64 kLongIdleHandlerDelayMs = 30*1000;
109const int kIdleCPUUsageThresholdInPercents = 3;
[email protected]bee16aab2009-08-26 15:55:03110
[email protected]f1a29a02011-10-06 23:08:44111// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16112// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40113base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49114 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54115
[email protected]64ffa0442011-10-03 22:08:36116class RenderViewZoomer : public content::RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51117 public:
[email protected]4e2a25a2012-01-27 00:42:08118 RenderViewZoomer(const std::string& host, double zoom_level)
119 : host_(host), zoom_level_(zoom_level) {
[email protected]40bd6582009-12-04 23:49:51120 }
121
[email protected]a2ef54c2011-10-10 16:20:31122 virtual bool Visit(content::RenderView* render_view) {
123 WebView* webview = render_view->GetWebView();
[email protected]b6cb3a842011-06-24 18:28:41124 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25125
126 // Don't set zoom level for full-page plugin since they don't use the same
127 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41128 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25129 return true;
130
[email protected]b6cb3a842011-06-24 18:28:41131 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51132 webview->setZoomLevel(false, zoom_level_);
133 return true;
134 }
135
136 private:
137 std::string host_;
[email protected]b75b8292010-10-01 07:28:25138 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51139
140 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
141};
[email protected]0478d0162010-08-28 08:29:40142
[email protected]42f1d7822009-07-23 18:17:55143} // namespace
144
[email protected]d1b8fccc2011-08-03 01:20:13145static void* CreateHistogram(
146 const char *name, int min, int max, size_t buckets) {
147 if (min <= 0)
148 min = 1;
149 base::Histogram* histogram = base::Histogram::FactoryGet(
150 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
151 return histogram;
152}
153
154static void AddHistogramSample(void* hist, int sample) {
155 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
156 histogram->Add(sample);
157}
158
[email protected]f1a29a02011-10-06 23:08:44159RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06160 return lazy_tls.Pointer()->Get();
161}
162
[email protected]42f1d7822009-07-23 18:17:55163// When we run plugins in process, we actually run them on the render thread,
164// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44165RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55166 Init();
167}
168
[email protected]f1a29a02011-10-06 23:08:44169RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55170 : ChildThread(channel_name) {
171 Init();
172}
[email protected]5fa1c542009-05-05 20:36:07173
[email protected]f1a29a02011-10-06 23:08:44174void RenderThreadImpl::Init() {
175 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33176
[email protected]d3f09092012-02-16 01:52:17177#if defined(OS_MACOSX) || defined(OS_ANDROID)
178 // On Mac and Android, the select popups are rendered by the browser.
[email protected]53c607c2011-03-21 23:19:04179 WebKit::WebView::setUseExternalPopupMenus(true);
180#endif
181
[email protected]94f9a0f682009-06-15 18:30:30182 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28183 GpuChannelHostFactory::set_instance(this);
184
[email protected]2c62b562009-01-27 19:04:50185#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34186 // If you are running plugins in this thread you need COM active but in
187 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28188 if (RenderProcessImpl::InProcessPlugins())
[email protected]1a771262011-10-31 09:11:12189 initialize_com_.reset(new base::win::ScopedCOMInitializer());
[email protected]2c62b562009-01-27 19:04:50190#endif
initial.commit09911bf2008-07-26 23:55:29191
[email protected]06c694d2012-02-01 22:26:16192 // Register this object as the main thread.
193 ChildProcess::current()->set_main_thread(this);
194
[email protected]31f87132010-04-21 23:36:21195 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50196 suspend_webkit_shared_timer_ = true;
197 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55198 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03199 widget_count_ = 0;
200 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44201 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48202 idle_notifications_to_skip_ = 0;
[email protected]2db58cf92011-12-01 21:39:01203 compositor_initialized_ = false;
[email protected]8d86fce2009-02-26 23:37:55204
[email protected]526476902011-10-06 20:34:06205 appcache_dispatcher_.reset(new AppCacheDispatcher(Get()));
[email protected]31bfae72011-12-16 02:04:38206 main_thread_indexed_db_dispatcher_.reset(
207 IndexedDBDispatcher::ThreadSpecificInstance());
[email protected]dd9241932010-02-24 19:23:13208
[email protected]017022b2009-07-27 23:06:34209 db_message_filter_ = new DBMessageFilter();
210 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13211
[email protected]80b161a2011-06-27 17:42:11212 vc_manager_ = new VideoCaptureImplManager();
213 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46214
[email protected]f7eb0a392011-07-12 10:19:51215 audio_input_message_filter_ = new AudioInputMessageFilter();
216 AddFilter(audio_input_message_filter_.get());
217
218 audio_message_filter_ = new AudioMessageFilter();
219 AddFilter(audio_message_filter_.get());
220
[email protected]82622452011-07-22 09:57:20221 devtools_agent_message_filter_ = new DevToolsAgentFilter();
222 AddFilter(devtools_agent_message_filter_.get());
223
[email protected]31bfae72011-12-16 02:04:38224 AddFilter(new IndexedDBMessageFilter);
225
[email protected]8d97ade2011-04-14 18:17:08226 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24227
[email protected]f1a29a02011-10-06 23:08:44228 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29229}
230
[email protected]f1a29a02011-10-06 23:08:44231RenderThreadImpl::~RenderThreadImpl() {
[email protected]1223d6ef2011-03-28 16:47:50232 FOR_EACH_OBSERVER(
233 RenderProcessObserver, observers_, OnRenderProcessShutdown());
234
[email protected]12cbfda32010-01-30 01:04:25235 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08236 if (web_database_observer_impl_.get())
237 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25238
[email protected]8d86fce2009-02-26 23:37:55239 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20240 RemoveFilter(devtools_agent_message_filter_.get());
241 devtools_agent_message_filter_ = NULL;
242
[email protected]f7eb0a392011-07-12 10:19:51243 RemoveFilter(audio_input_message_filter_.get());
244 audio_input_message_filter_ = NULL;
245
246 RemoveFilter(audio_message_filter_.get());
247 audio_message_filter_ = NULL;
248
[email protected]80b161a2011-06-27 17:42:11249 RemoveFilter(vc_manager_->video_capture_message_filter());
250
[email protected]017022b2009-07-27 23:06:34251 RemoveFilter(db_message_filter_.get());
252 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25253
[email protected]c6a7b862010-08-20 22:19:38254 // Shutdown the file thread if it's running.
255 if (file_thread_.get())
256 file_thread_->Stop();
257
[email protected]028217e2011-11-19 00:19:08258#ifdef WEBCOMPOSITOR_HAS_INITIALIZE
[email protected]2db58cf92011-12-01 21:39:01259 if (compositor_initialized_) {
260 WebKit::WebCompositor::shutdown();
261 compositor_initialized_ = false;
262 }
[email protected]028217e2011-11-19 00:19:08263#endif
[email protected]a9fb30aa2011-10-06 06:58:46264 if (compositor_thread_.get()) {
265 RemoveFilter(compositor_thread_->GetMessageFilter());
266 compositor_thread_.reset();
267 }
268
[email protected]8d6cba42011-09-02 10:05:19269 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22270 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22271
[email protected]b3e83de2012-02-07 03:33:28272 GpuChannelHostFactory::set_instance(NULL);
[email protected]94f9a0f682009-06-15 18:30:30273 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30274
[email protected]8d86fce2009-02-26 23:37:55275 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50276#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29277 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50278 NPChannelBase::CleanupChannels();
[email protected]2c62b562009-01-27 19:04:50279#endif
initial.commit09911bf2008-07-26 23:55:29280}
281
[email protected]f1a29a02011-10-06 23:08:44282bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14283 // Certain synchronous messages cannot always be processed synchronously by
284 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50285 // This could cause a complete hang of Chrome if a windowed plug-in is trying
286 // to communicate with the renderer thread since the browser's UI thread
287 // could be stuck (within a Windows API call) trying to synchronously
288 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14289 // thread while the browser is processing this request. This creates an
290 // opportunity for re-entrancy into WebKit, so we need to take care to disable
291 // callbacks, timers, and pending network loads that could trigger such
292 // callbacks.
[email protected]38b592902011-04-16 02:08:42293 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50294 if (msg->is_sync()) {
295 if (msg->is_caller_pumping_messages()) {
296 pumping_events = true;
297 } else {
[email protected]38b592902011-04-16 02:08:42298 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
299 msg->type() == ViewHostMsg_GetRawCookies::ID ||
300 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
301 content::GetContentClient()->renderer()->
302 ShouldPumpEventsDuringCookieMessage()) {
303 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50304 }
305 }
[email protected]c1f50aa2010-02-18 03:46:57306 }
307
[email protected]80fc08c52010-03-09 07:43:50308 bool suspend_webkit_shared_timer = true; // default value
309 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57310
[email protected]80fc08c52010-03-09 07:43:50311 bool notify_webkit_of_modal_loop = true; // default value
312 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
313
314 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57315
316 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50317 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19318 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57319
[email protected]39065d012010-07-09 11:22:46320 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57321 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57322
[email protected]80fc08c52010-03-09 07:43:50323 RenderWidget* widget =
324 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
325 if (widget) {
326 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57327 PluginChannelHost::Broadcast(
328 new PluginMsg_SignalModalDialogEvent(host_window));
329 }
330 }
331
332 bool rv = ChildThread::Send(msg);
333
334 if (pumping_events) {
335 if (host_window) {
336 PluginChannelHost::Broadcast(
337 new PluginMsg_ResetModalDialogEvent(host_window));
338 }
339
[email protected]39065d012010-07-09 11:22:46340 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57341 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57342
[email protected]80fc08c52010-03-09 07:43:50343 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19344 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57345 }
346
347 return rv;
348}
349
[email protected]f1a29a02011-10-06 23:08:44350MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06351 return message_loop();
352}
353
[email protected]f1a29a02011-10-06 23:08:44354IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06355 return channel();
356}
357
[email protected]f1a29a02011-10-06 23:08:44358std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06359 // The browser process should have passed the locale to the renderer via the
360 // --lang command line flag. In single process mode, this will return the
361 // wrong value. TODO(tc): Fix this for single process mode.
362 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
363 const std::string& lang =
364 parsed_command_line.GetSwitchValueASCII(switches::kLang);
365 DCHECK(!lang.empty() ||
366 (!parsed_command_line.HasSwitch(switches::kRendererProcess) &&
367 !parsed_command_line.HasSwitch(switches::kPluginProcess)));
368 return lang;
369}
370
[email protected]07bb6332012-01-21 01:07:57371IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
372 return sync_message_filter();
373}
374
[email protected]f1a29a02011-10-06 23:08:44375void RenderThreadImpl::AddRoute(int32 routing_id,
376 IPC::Channel::Listener* listener) {
[email protected]c1f50aa2010-02-18 03:46:57377 widget_count_++;
378 return ChildThread::AddRoute(routing_id, listener);
379}
380
[email protected]f1a29a02011-10-06 23:08:44381void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]c1f50aa2010-02-18 03:46:57382 widget_count_--;
383 return ChildThread::RemoveRoute(routing_id);
384}
385
[email protected]77fc9b92011-10-15 16:20:37386int RenderThreadImpl::GenerateRoutingID() {
387 int routing_id = MSG_ROUTING_NONE;
388 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
389 return routing_id;
390}
391
[email protected]f1a29a02011-10-06 23:08:44392void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55393 channel()->AddFilter(filter);
394}
395
[email protected]f1a29a02011-10-06 23:08:44396void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55397 channel()->RemoveFilter(filter);
398}
399
[email protected]f1a29a02011-10-06 23:08:44400void RenderThreadImpl::SetOutgoingMessageFilter(
[email protected]526476902011-10-06 20:34:06401 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
402}
403
[email protected]f1a29a02011-10-06 23:08:44404void RenderThreadImpl::AddObserver(content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06405 observers_.AddObserver(observer);
406}
407
[email protected]f1a29a02011-10-06 23:08:44408void RenderThreadImpl::RemoveObserver(
409 content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06410 observers_.RemoveObserver(observer);
411}
412
[email protected]359dfa32011-10-12 01:10:15413void RenderThreadImpl::SetResourceDispatcherDelegate(
414 content::ResourceDispatcherDelegate* delegate) {
415 resource_dispatcher()->set_delegate(delegate);
416}
417
[email protected]f1a29a02011-10-06 23:08:44418void RenderThreadImpl::WidgetHidden() {
[email protected]bee16aab2009-08-26 15:55:03419 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50420 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24421
422 if (!content::GetContentClient()->renderer()->
423 RunIdleHandlerWhenWidgetsHidden()) {
424 return;
425 }
426
427 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]6593ae12011-11-14 12:09:44428 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
[email protected]bee16aab2009-08-26 15:55:03429}
430
[email protected]f1a29a02011-10-06 23:08:44431void RenderThreadImpl::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08432 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03433 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24434 if (!content::GetContentClient()->renderer()->
435 RunIdleHandlerWhenWidgetsHidden()) {
436 return;
437 }
[email protected]bee16aab2009-08-26 15:55:03438
[email protected]1784b2f2011-11-24 10:53:48439 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]e4be2dd2010-12-14 00:44:39440}
441
[email protected]f1a29a02011-10-06 23:08:44442void RenderThreadImpl::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19443 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13444 return;
445
446 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
447 v8::V8::SetCreateHistogramFunction(CreateHistogram);
448 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
449
[email protected]8d6cba42011-09-02 10:05:19450 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
451 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13452
[email protected]f3150172011-10-22 02:28:45453 if (CommandLine::ForCurrentProcess()->HasSwitch(
454 switches::kEnableThreadedCompositing)) {
455 compositor_thread_.reset(new CompositorThread(this));
456 AddFilter(compositor_thread_->GetMessageFilter());
[email protected]028217e2011-11-19 00:19:08457#ifdef WEBCOMPOSITOR_HAS_INITIALIZE
458 WebKit::WebCompositor::initialize(compositor_thread_->GetWebThread());
459#else
460 WebKit::WebCompositor::setThread(compositor_thread_->GetWebThread());
461#endif
462 } else {
463#ifdef WEBCOMPOSITOR_HAS_INITIALIZE
464 WebKit::WebCompositor::initialize(NULL);
465#endif
[email protected]f3150172011-10-22 02:28:45466 }
[email protected]2db58cf92011-12-01 21:39:01467 compositor_initialized_ = true;
[email protected]a9fb30aa2011-10-06 06:58:46468
[email protected]d1b8fccc2011-08-03 01:20:13469 WebScriptController::enableV8SingleThreadMode();
470
471 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
472
473 webkit_glue::EnableWebCoreLogChannels(
474 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
475
[email protected]d1b8fccc2011-08-03 01:20:13476 if (command_line.HasSwitch(switches::kPlaybackMode) ||
477 command_line.HasSwitch(switches::kRecordMode) ||
478 command_line.HasSwitch(switches::kNoJsRandomness)) {
479 RegisterExtension(extensions_v8::PlaybackExtension::Get());
480 }
481
[email protected]766a7082012-02-03 23:39:15482 if (CommandLine::ForCurrentProcess()->HasSwitch(
483 switches::kDomAutomationController)) {
484 base::StringPiece extension = content::GetContentClient()->GetDataResource(
485 IDR_DOM_AUTOMATION_JS);
486 RegisterExtension(new v8::Extension(
487 "dom_automation.js", extension.data(), 0, NULL, extension.size()));
488 }
489
[email protected]526476902011-10-06 20:34:06490 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(Get()));
[email protected]d1b8fccc2011-08-03 01:20:13491 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
492
493 WebRuntimeFeatures::enableSockets(
494 !command_line.HasSwitch(switches::kDisableWebSockets));
495
496 WebRuntimeFeatures::enableDatabase(
497 !command_line.HasSwitch(switches::kDisableDatabases));
498
499 WebRuntimeFeatures::enableDataTransferItems(
500 !command_line.HasSwitch(switches::kDisableDataTransferItems));
501
502 WebRuntimeFeatures::enableApplicationCache(
503 !command_line.HasSwitch(switches::kDisableApplicationCache));
504
505 WebRuntimeFeatures::enableNotifications(
506 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
507
508 WebRuntimeFeatures::enableLocalStorage(
509 !command_line.HasSwitch(switches::kDisableLocalStorage));
510 WebRuntimeFeatures::enableSessionStorage(
511 !command_line.HasSwitch(switches::kDisableSessionStorage));
512
[email protected]b3796c82012-01-19 08:34:33513 WebRuntimeFeatures::enableIndexedDatabase(true);
[email protected]d1b8fccc2011-08-03 01:20:13514
515 WebRuntimeFeatures::enableGeolocation(
516 !command_line.HasSwitch(switches::kDisableGeolocation));
517
[email protected]6aa03b32011-10-27 21:44:44518 WebKit::WebRuntimeFeatures::enableMediaSource(
519 command_line.HasSwitch(switches::kEnableMediaSource));
520
[email protected]d1b8fccc2011-08-03 01:20:13521 WebKit::WebRuntimeFeatures::enableMediaStream(
522 command_line.HasSwitch(switches::kEnableMediaStream));
523
[email protected]efdc0282011-08-26 00:36:12524 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21525 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12526
[email protected]e4e68dbb2011-11-18 01:50:22527 WebKit::WebRuntimeFeatures::enablePointerLock(
528 command_line.HasSwitch(switches::kEnablePointerLock));
529
[email protected]f5da41d2011-10-08 17:40:07530 WebKit::WebRuntimeFeatures::enableVideoTrack(
531 command_line.HasSwitch(switches::kEnableVideoTrack));
532
[email protected]d1b8fccc2011-08-03 01:20:13533#if defined(OS_CHROMEOS)
534 // TODO(crogers): enable once Web Audio has been tested and optimized.
535 WebRuntimeFeatures::enableWebAudio(false);
536#else
537 WebRuntimeFeatures::enableWebAudio(
538 !command_line.HasSwitch(switches::kDisableWebAudio));
539#endif
540
541 WebRuntimeFeatures::enablePushState(true);
542
[email protected]d1b8fccc2011-08-03 01:20:13543 WebRuntimeFeatures::enableTouch(false);
[email protected]d1b8fccc2011-08-03 01:20:13544
545 WebRuntimeFeatures::enableDeviceMotion(
546 command_line.HasSwitch(switches::kEnableDeviceMotion));
547
548 WebRuntimeFeatures::enableDeviceOrientation(
549 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
550
551 WebRuntimeFeatures::enableSpeechInput(
552 !command_line.HasSwitch(switches::kDisableSpeechInput));
553
554 WebRuntimeFeatures::enableFileSystem(
555 !command_line.HasSwitch(switches::kDisableFileSystem));
556
557 WebRuntimeFeatures::enableJavaScriptI18NAPI(
558 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
559
[email protected]0205fbf2011-11-30 21:51:36560 WebRuntimeFeatures::enableGamepad(
561 command_line.HasSwitch(switches::kEnableGamepad));
562
[email protected]d1b8fccc2011-08-03 01:20:13563 WebRuntimeFeatures::enableQuota(true);
564
[email protected]d2edc6702012-01-30 09:13:16565 WebRuntimeFeatures::enableShadowDOM(
566 command_line.HasSwitch(switches::kEnableShadowDOM));
567
[email protected]4961218f2012-02-23 10:11:07568 WebRuntimeFeatures::enableStyleScoped(
569 command_line.HasSwitch(switches::kEnableStyleScoped));
570
[email protected]d1b8fccc2011-08-03 01:20:13571 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]1784b2f2011-11-24 10:53:48572
573 if (content::GetContentClient()->renderer()->
574 RunIdleHandlerWhenWidgetsHidden()) {
575 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
576 }
[email protected]d1b8fccc2011-08-03 01:20:13577}
578
[email protected]f1a29a02011-10-06 23:08:44579void RenderThreadImpl::RecordUserMetrics(const std::string& action) {
[email protected]526476902011-10-06 20:34:06580 Send(new ViewHostMsg_UserMetricsRecordAction(action));
581}
582
[email protected]00614a82011-10-07 22:39:31583base::SharedMemoryHandle RenderThreadImpl::HostAllocateSharedMemoryBuffer(
[email protected]bdae9812011-10-15 00:33:03584 uint32 buffer_size) {
[email protected]00614a82011-10-07 22:39:31585 base::SharedMemoryHandle mem_handle;
[email protected]bdae9812011-10-15 00:33:03586 Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
587 buffer_size, &mem_handle));
[email protected]00614a82011-10-07 22:39:31588 return mem_handle;
589}
590
[email protected]f1a29a02011-10-06 23:08:44591void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06592 WebScriptController::registerExtension(extension);
593 v8_extensions_.insert(extension->name());
594}
595
[email protected]f1a29a02011-10-06 23:08:44596bool RenderThreadImpl::IsRegisteredExtension(
[email protected]526476902011-10-06 20:34:06597 const std::string& v8_extension_name) const {
598 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
599}
600
[email protected]6593ae12011-11-14 12:09:44601void RenderThreadImpl::ScheduleIdleHandler(int64 initial_delay_ms) {
602 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:06603 idle_timer_.Stop();
604 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:44605 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:44606 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06607}
608
[email protected]f1a29a02011-10-06 23:08:44609void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:48610 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
611 content::GetContentClient()->renderer()->
612 RunIdleHandlerWhenWidgetsHidden();
613 if (run_in_foreground_tab) {
614 IdleHandlerInForegroundTab();
615 return;
616 }
617#if !defined(OS_MACOSX) && defined(USE_TCMALLOC)
[email protected]526476902011-10-06 20:34:06618 MallocExtension::instance()->ReleaseFreeMemory();
619#endif
620
621 v8::V8::IdleNotification();
622
623 // Schedule next invocation.
[email protected]6593ae12011-11-14 12:09:44624 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:06625 // delay = delay + 1 / (delay + 2)
626 // Using floor(delay) has a dampening effect such as:
627 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
[email protected]6593ae12011-11-14 12:09:44628 // If the delay is in milliseconds, the above formula is equivalent to:
629 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
630 // which is equivalent to
631 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
632 // Note that idle_notification_delay_in_ms_ would be reset to
633 // kInitialIdleHandlerDelayMs in RenderThreadImpl::WidgetHidden.
634 ScheduleIdleHandler(idle_notification_delay_in_ms_ +
635 1000000 / (idle_notification_delay_in_ms_ + 2000));
[email protected]526476902011-10-06 20:34:06636
637 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
638}
639
[email protected]1784b2f2011-11-24 10:53:48640void RenderThreadImpl::IdleHandlerInForegroundTab() {
641 // Increase the delay in the same way as in IdleHandler,
642 // but make it periodic by reseting it once it is too big.
643 int64 new_delay_ms = idle_notification_delay_in_ms_ +
644 1000000 / (idle_notification_delay_in_ms_ + 2000);
645 if (new_delay_ms >= kLongIdleHandlerDelayMs)
646 new_delay_ms = kShortIdleHandlerDelayMs;
647
[email protected]1784b2f2011-11-24 10:53:48648 if (idle_notifications_to_skip_ > 0) {
649 idle_notifications_to_skip_--;
[email protected]7e967f82011-12-01 09:35:14650 } else {
[email protected]a47105c832011-12-07 17:24:30651 int cpu_usage = 0;
[email protected]7e967f82011-12-01 09:35:14652 Send(new ViewHostMsg_GetCPUUsage(&cpu_usage));
[email protected]1393c4a2012-02-07 10:12:58653 // Idle notification hint roughly specifies the expected duration of the
654 // idle pause. We set it proportional to the idle timer delay.
655 int idle_hint = static_cast<int>(new_delay_ms / 10);
[email protected]7e967f82011-12-01 09:35:14656 if (cpu_usage < kIdleCPUUsageThresholdInPercents &&
[email protected]1393c4a2012-02-07 10:12:58657 v8::V8::IdleNotification(idle_hint)) {
[email protected]1784b2f2011-11-24 10:53:48658 // V8 finished collecting garbage.
659 new_delay_ms = kLongIdleHandlerDelayMs;
660 }
661 }
[email protected]1784b2f2011-11-24 10:53:48662 ScheduleIdleHandler(new_delay_ms);
663}
664
[email protected]6593ae12011-11-14 12:09:44665int64 RenderThreadImpl::GetIdleNotificationDelayInMs() const {
666 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:06667}
668
[email protected]6593ae12011-11-14 12:09:44669void RenderThreadImpl::SetIdleNotificationDelayInMs(
670 int64 idle_notification_delay_in_ms) {
671 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:08672}
673
[email protected]1784b2f2011-11-24 10:53:48674void RenderThreadImpl::PostponeIdleNotification() {
675 idle_notifications_to_skip_ = 2;
676}
677
[email protected]4a7d6392011-09-19 20:55:08678#if defined(OS_WIN)
[email protected]f1a29a02011-10-06 23:08:44679void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:06680 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:21681}
682
[email protected]f1a29a02011-10-06 23:08:44683void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:06684 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:08685}
[email protected]526476902011-10-06 20:34:06686
[email protected]4a7d6392011-09-19 20:55:08687#endif // OS_WIN
688
[email protected]988aacf2012-02-12 18:10:03689bool RenderThreadImpl::IsWebFrameValid(WebKit::WebFrame* web_frame) {
690 if (!web_frame)
691 return false; // We must be shutting down.
692
693 RenderViewImpl* render_view = RenderViewImpl::FromWebView(web_frame->view());
694 if (!render_view)
695 return false; // We must be shutting down.
696
697 return true;
698}
699
[email protected]b3e83de2012-02-07 03:33:28700bool RenderThreadImpl::IsMainThread() {
701 return !!current();
702}
703
704bool RenderThreadImpl::IsIOThread() {
705 return MessageLoop::current() == ChildProcess::current()->io_message_loop();
706}
707
708MessageLoop* RenderThreadImpl::GetMainLoop() {
709 return message_loop();
710}
[email protected]5b040e592012-02-10 02:56:10711
712scoped_refptr<base::MessageLoopProxy> RenderThreadImpl::GetIOLoopProxy() {
[email protected]b3e83de2012-02-07 03:33:28713 return ChildProcess::current()->io_message_loop_proxy();
714}
715
716base::WaitableEvent* RenderThreadImpl::GetShutDownEvent() {
717 return ChildProcess::current()->GetShutDownEvent();
718}
719
720scoped_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
721 uint32 size) {
[email protected]b3e83de2012-02-07 03:33:28722 base::SharedMemoryHandle handle;
723 if (!ChildThread::Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
724 size,
725 &handle))) {
726 return scoped_ptr<base::SharedMemory>();
727 }
728 if (!base::SharedMemory::IsHandleValid(handle))
729 return scoped_ptr<base::SharedMemory>();
730 return scoped_ptr<base::SharedMemory>(new base::SharedMemory(handle, false));
731}
732
733int32 RenderThreadImpl::CreateViewCommandBuffer(
734 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) {
735 int32 route_id = MSG_ROUTING_NONE;
736 ChildThread::Send(new GpuHostMsg_CreateViewCommandBuffer(
737 surface_id,
738 init_params,
739 &route_id));
740 return route_id;
741}
742
[email protected]f1a29a02011-10-06 23:08:44743int32 RenderThreadImpl::RoutingIDForCurrentContext() {
[email protected]526476902011-10-06 20:34:06744 int32 routing_id = MSG_ROUTING_CONTROL;
745 if (v8::Context::InContext()) {
746 WebFrame* frame = WebFrame::frameForCurrentContext();
747 if (frame) {
[email protected]310ebd6302011-10-10 19:06:28748 RenderViewImpl* view = RenderViewImpl::FromWebView(frame->view());
[email protected]526476902011-10-06 20:34:06749 if (view)
750 routing_id = view->routing_id();
751 }
752 } else {
753 DLOG(WARNING) << "Not called within a script context!";
754 }
755 return routing_id;
756}
757
[email protected]f1a29a02011-10-06 23:08:44758void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:06759 suspend_webkit_shared_timer_ = false;
760}
761
[email protected]f1a29a02011-10-06 23:08:44762void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:06763 notify_webkit_of_modal_loop_ = false;
764}
765
[email protected]4e2a25a2012-01-27 00:42:08766void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const std::string& host,
[email protected]f1a29a02011-10-06 23:08:44767 double zoom_level) {
[email protected]4e2a25a2012-01-27 00:42:08768 RenderViewZoomer zoomer(host, zoom_level);
[email protected]310ebd6302011-10-10 19:06:28769 content::RenderView::ForEach(&zoomer);
[email protected]526476902011-10-06 20:34:06770}
771
[email protected]f1a29a02011-10-06 23:08:44772void RenderThreadImpl::OnDOMStorageEvent(
[email protected]526476902011-10-06 20:34:06773 const DOMStorageMsg_Event_Params& params) {
[email protected]0654cc82011-11-17 09:20:59774 if (!dom_storage_event_dispatcher_.get()) {
775 EnsureWebKitInitialized();
[email protected]526476902011-10-06 20:34:06776 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]0654cc82011-11-17 09:20:59777 }
[email protected]526476902011-10-06 20:34:06778 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key,
779 params.old_value, params.new_value, params.origin, params.url,
780 params.storage_type == DOM_STORAGE_LOCAL);
781}
782
[email protected]f1a29a02011-10-06 23:08:44783bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50784 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
785 RenderProcessObserver* observer;
786 while ((observer = it.GetNext()) != NULL) {
787 if (observer->OnControlMessageReceived(msg))
788 return true;
789 }
790
[email protected]70c19a932010-05-14 12:59:11791 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31792 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28793 return true;
[email protected]1edc16b82009-04-07 17:45:54794
[email protected]a95986a82010-12-24 06:19:28795 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:44796 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:54797 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
798 OnSetZoomLevelForCurrentURL)
[email protected]b9ab10c2009-08-07 18:09:55799 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28800 // TODO(port): removed from render_messages_internal.h;
801 // is there a new non-windows message I should add here?
802 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45803 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09804 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45805 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]b69934e2011-10-29 02:51:52806 IPC_MESSAGE_HANDLER(ViewMsg_TempCrashWithData, OnTempCrashWithData)
[email protected]a95986a82010-12-24 06:19:28807 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28808 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28809 return handled;
initial.commit09911bf2008-07-26 23:55:29810}
811
[email protected]b9ab10c2009-08-07 18:09:55812// Called when to register CSS Color name->system color mappings.
813// We update the colors one by one and then tell WebKit to refresh all render
814// views.
[email protected]f1a29a02011-10-06 23:08:44815void RenderThreadImpl::OnSetCSSColors(
[email protected]b9ab10c2009-08-07 18:09:55816 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59817 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55818 size_t num_colors = colors.size();
819 scoped_array<WebKit::WebColorName> color_names(
820 new WebKit::WebColorName[num_colors]);
821 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
822 size_t i = 0;
823 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
824 colors.begin();
825 it != colors.end();
826 ++it, ++i) {
827 color_names[i] = it->first;
828 web_colors[i] = it->second;
829 }
830 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
831}
832
[email protected]f1a29a02011-10-06 23:08:44833void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47834 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33835 // When bringing in render_view, also bring in webkit's glue and jsbindings.
[email protected]310ebd6302011-10-10 19:06:28836 RenderViewImpl::Create(
[email protected]8ab04652010-06-12 02:47:26837 params.parent_window,
838 MSG_ROUTING_NONE,
839 params.renderer_preferences,
840 params.web_preferences,
841 new SharedRenderViewCounter(0),
842 params.view_id,
[email protected]9f4f3322012-01-18 22:29:56843 params.surface_id,
[email protected]8ab04652010-06-12 02:47:26844 params.session_storage_namespace_id,
[email protected]74ce1ad2011-12-16 21:51:46845 params.frame_name,
846 params.next_page_id);
[email protected]7f874dec2009-02-06 01:48:27847}
[email protected]4274e582009-01-27 22:09:56848
[email protected]f1a29a02011-10-06 23:08:44849GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50850 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35851 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22852 // Do nothing if we already have a GPU channel or are already
853 // establishing one.
[email protected]e09cee42010-11-09 01:50:08854 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
855 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04856 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35857
858 // Recreate the channel if it has been lost.
[email protected]ca806632012-02-16 02:15:59859 if (gpu_channel_->state() == GpuChannelHost::kLost)
860 gpu_channel_ = NULL;
[email protected]6217d392010-03-25 22:08:35861 }
862
[email protected]ca806632012-02-16 02:15:59863 if (!gpu_channel_.get())
864 gpu_channel_ = new GpuChannelHost(this);
865
[email protected]6217d392010-03-25 22:08:35866 // Ask the browser for the channel name.
[email protected]b42a9f62011-06-08 20:36:04867 IPC::ChannelHandle channel_handle;
868 base::ProcessHandle renderer_process_for_gpu;
[email protected]a80f5ece2011-10-20 23:56:55869 content::GPUInfo gpu_info;
[email protected]b42a9f62011-06-08 20:36:04870 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
[email protected]b42a9f62011-06-08 20:36:04871 &channel_handle,
872 &renderer_process_for_gpu,
873 &gpu_info)) ||
874 channel_handle.name.empty() ||
[email protected]8bc550e2012-02-07 11:05:56875#if defined(OS_POSIX)
876 channel_handle.socket.fd == -1 ||
877#endif
[email protected]b42a9f62011-06-08 20:36:04878 renderer_process_for_gpu == base::kNullProcessHandle) {
879 // Otherwise cancel the connection.
880 gpu_channel_ = NULL;
881 return NULL;
882 }
[email protected]6217d392010-03-25 22:08:35883
[email protected]b42a9f62011-06-08 20:36:04884 gpu_channel_->set_gpu_info(gpu_info);
885 content::GetContentClient()->SetGpuInfo(gpu_info);
886
887 // Connect to the GPU process if a channel name was received.
888 gpu_channel_->Connect(channel_handle, renderer_process_for_gpu);
889
[email protected]f9a2b2fe2010-07-15 21:13:23890 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34891}
892
[email protected]f1a29a02011-10-06 23:08:44893GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]6217d392010-03-25 22:08:35894 if (!gpu_channel_.get())
895 return NULL;
896
[email protected]e09cee42010-11-09 01:50:08897 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35898 return NULL;
899
900 return gpu_channel_.get();
901}
902
[email protected]f1a29a02011-10-06 23:08:44903void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59904 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27905 // The call below will cause a GetPlugins call with refresh=true, but at this
906 // point we already know that the browser has refreshed its list, so disable
907 // refresh temporarily to prevent each renderer process causing the list to be
908 // regenerated.
909 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45910 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27911 plugin_refresh_allowed_ = true;
[email protected]3b48dbc2012-01-06 16:34:17912
913 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:27914}
[email protected]85c55dc2009-11-06 03:05:46915
[email protected]f1a29a02011-10-06 23:08:44916void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:09917 EnsureWebKitInitialized();
918 WebNetworkStateNotifier::setOnLine(online);
919}
920
[email protected]b69934e2011-10-29 02:51:52921void RenderThreadImpl::OnTempCrashWithData(const GURL& data) {
[email protected]74ce1ad2011-12-16 21:51:46922 content::GetContentClient()->SetActiveURL(data);
[email protected]b69934e2011-10-29 02:51:52923 CHECK(false);
924}
925
[email protected]c6a7b862010-08-20 22:19:38926scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:44927RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]c6a7b862010-08-20 22:19:38928 DCHECK(message_loop() == MessageLoop::current());
929 if (!file_thread_.get()) {
930 file_thread_.reset(new base::Thread("Renderer::FILE"));
931 file_thread_->Start();
932 }
933 return file_thread_->message_loop_proxy();
934}