blob: 3540f3cfafeef76528978b7686d8cb7ad644cff0 [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]237a14852012-04-28 02:56:3812#include "base/allocator/allocator_extension.h"
[email protected]06533c0b2009-03-05 21:39:1113#include "base/command_line.h"
[email protected]58580352010-10-26 04:07:5014#include "base/debug/trace_event.h"
[email protected]94f9a0f682009-06-15 18:30:3015#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0316#include "base/logging.h"
[email protected]835d7c82010-10-14 04:38:3817#include "base/metrics/field_trial.h"
[email protected]6cf19311f2011-04-14 23:06:0218#include "base/metrics/histogram.h"
[email protected]835d7c82010-10-14 04:38:3819#include "base/metrics/stats_table.h"
[email protected]433df472012-03-07 20:33:3920#include "base/path_service.h"
initial.commit09911bf2008-07-26 23:55:2921#include "base/shared_memory.h"
[email protected]8c380582011-12-02 03:16:1022#include "base/string_number_conversions.h" // Temporary
[email protected]1357c322010-12-30 22:18:5623#include "base/threading/thread_local.h"
[email protected]58436a12012-03-21 17:10:2624#include "base/utf_string_conversions.h"
[email protected]7a4de7a62010-08-17 18:38:2425#include "base/values.h"
[email protected]1a771262011-10-31 09:11:1226#include "base/win/scoped_com_initializer.h"
[email protected]e93e04e2011-03-14 00:27:1027#include "content/common/appcache/appcache_dispatcher.h"
[email protected]bdae9812011-10-15 00:33:0328#include "content/common/child_process_messages.h"
[email protected]37666cf2011-03-13 21:51:4229#include "content/common/database_messages.h"
30#include "content/common/db_message_filter.h"
[email protected]127dd582011-03-16 21:32:1031#include "content/common/dom_storage_messages.h"
[email protected]c26ad882012-02-07 06:41:2032#include "content/common/gpu/client/gpu_channel_host.h"
[email protected]202b54ff2011-04-22 21:36:3833#include "content/common/gpu/gpu_messages.h"
[email protected]988aacf2012-02-12 18:10:0334#include "content/common/indexed_db/indexed_db_dispatcher.h"
35#include "content/common/indexed_db/indexed_db_message_filter.h"
[email protected]d3fd748b2011-09-20 17:39:1736#include "content/common/npobject_util.h"
[email protected]105303e2011-03-14 22:16:1037#include "content/common/plugin_messages.h"
[email protected]359dfa32011-10-12 01:10:1538#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3239#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5040#include "content/common/view_messages.h"
[email protected]e93e04e2011-03-14 00:27:1041#include "content/common/web_database_observer_impl.h"
[email protected]433df472012-03-07 20:33:3942#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2943#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3144#include "content/public/common/renderer_preferences.h"
[email protected]58436a12012-03-21 17:10:2645#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3446#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3647#include "content/public/renderer/render_process_observer.h"
48#include "content/public/renderer/render_view_visitor.h"
[email protected]82622452011-07-22 09:57:2049#include "content/renderer/devtools_agent_filter.h"
[email protected]a9fb30aa2011-10-06 06:58:4650#include "content/renderer/gpu/compositor_thread.h"
[email protected]f7eb0a392011-07-12 10:19:5151#include "content/renderer/media/audio_input_message_filter.h"
52#include "content/renderer/media/audio_message_filter.h"
[email protected]d8cd8372012-03-09 10:49:5153#include "content/renderer/media/media_stream_center.h"
[email protected]80b161a2011-06-27 17:42:1154#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3455#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3556#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0557#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:2858#include "content/renderer/render_view_impl.h"
[email protected]8d6cba42011-09-02 10:05:1959#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]60e52332012-04-19 20:36:5960#include "content/renderer/renderer_webstoragearea_impl.h"
[email protected]345ff222012-04-26 01:24:0061#include "content/renderer/renderer_webstoragenamespace_impl.h"
[email protected]766a7082012-02-03 23:39:1562#include "grit/content_resources.h"
[email protected]46f36a492010-07-28 19:36:4163#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1764#include "ipc/ipc_platform_file.h"
[email protected]433df472012-03-07 20:33:3965#include "media/base/media.h"
[email protected]620161e2011-03-07 18:05:2666#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5467#include "net/base/net_util.h"
[email protected]f8c648b2012-02-28 03:46:4568#include "third_party/WebKit/Source/WebKit/chromium/public/WebColorName.h"
[email protected]028217e2011-11-19 00:19:0869#include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositor.h"
[email protected]8bd0fe62011-01-17 06:44:3770#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
71#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3772#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
73#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0974#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]4bd55a32011-07-28 13:28:3875#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3776#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
77#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]58436a12012-03-21 17:10:2678#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
[email protected]8bd0fe62011-01-17 06:44:3779#include "third_party/WebKit/Source/WebKit/chromium/public/WebStorageEventDispatcher.h"
[email protected]e6e90dc2011-12-03 00:01:3780#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
[email protected]8bd0fe62011-01-17 06:44:3781#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]18ad6772011-09-20 21:51:3282#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:5083#include "v8/include/v8.h"
[email protected]d471190a2011-02-16 14:52:3084#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5085
[email protected]94dc971d2011-03-05 19:08:3286// TODO(port)
[email protected]bdae9812011-10-15 00:33:0387#if !defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1588#include "base/memory/scoped_handle.h"
[email protected]099587b72011-09-20 00:40:5089#include "content/common/np_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3290#endif
91
[email protected]da00a2882009-03-09 17:51:1992#if defined(OS_WIN)
93#include <windows.h>
94#include <objbase.h>
95#endif
96
[email protected]6217d392010-03-25 22:08:3597#if defined(OS_POSIX)
98#include "ipc/ipc_channel_posix.h"
99#endif
100
[email protected]b6cb3a842011-06-24 18:28:41101using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:01102using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:09103using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:00104using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:51105using WebKit::WebScriptController;
[email protected]58436a12012-03-21 17:10:26106using WebKit::WebSecurityPolicy;
[email protected]2c434b32009-03-19 06:27:47107using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:34108using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:03109using WebKit::WebView;
[email protected]64ffa0442011-10-03 22:08:36110using content::RenderProcessObserver;
initial.commit09911bf2008-07-26 23:55:29111
[email protected]42f1d7822009-07-23 18:17:55112namespace {
[email protected]da9ccfb2012-01-28 00:34:40113
114const int64 kInitialIdleHandlerDelayMs = 1000;
115const int64 kShortIdleHandlerDelayMs = 1000;
116const int64 kLongIdleHandlerDelayMs = 30*1000;
117const int kIdleCPUUsageThresholdInPercents = 3;
[email protected]bee16aab2009-08-26 15:55:03118
[email protected]f1a29a02011-10-06 23:08:44119// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16120// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40121base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49122 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54123
[email protected]64ffa0442011-10-03 22:08:36124class RenderViewZoomer : public content::RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51125 public:
[email protected]4e2a25a2012-01-27 00:42:08126 RenderViewZoomer(const std::string& host, double zoom_level)
127 : host_(host), zoom_level_(zoom_level) {
[email protected]40bd6582009-12-04 23:49:51128 }
129
[email protected]a2ef54c2011-10-10 16:20:31130 virtual bool Visit(content::RenderView* render_view) {
131 WebView* webview = render_view->GetWebView();
[email protected]b6cb3a842011-06-24 18:28:41132 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25133
134 // Don't set zoom level for full-page plugin since they don't use the same
135 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41136 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25137 return true;
138
[email protected]b6cb3a842011-06-24 18:28:41139 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51140 webview->setZoomLevel(false, zoom_level_);
141 return true;
142 }
143
144 private:
145 std::string host_;
[email protected]b75b8292010-10-01 07:28:25146 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51147
148 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
149};
[email protected]0478d0162010-08-28 08:29:40150
[email protected]42f1d7822009-07-23 18:17:55151} // namespace
152
[email protected]d1b8fccc2011-08-03 01:20:13153static void* CreateHistogram(
154 const char *name, int min, int max, size_t buckets) {
155 if (min <= 0)
156 min = 1;
157 base::Histogram* histogram = base::Histogram::FactoryGet(
158 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
159 return histogram;
160}
161
162static void AddHistogramSample(void* hist, int sample) {
163 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
164 histogram->Add(sample);
165}
166
[email protected]f1a29a02011-10-06 23:08:44167RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06168 return lazy_tls.Pointer()->Get();
169}
170
[email protected]42f1d7822009-07-23 18:17:55171// When we run plugins in process, we actually run them on the render thread,
172// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44173RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55174 Init();
175}
176
[email protected]f1a29a02011-10-06 23:08:44177RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55178 : ChildThread(channel_name) {
179 Init();
180}
[email protected]5fa1c542009-05-05 20:36:07181
[email protected]f1a29a02011-10-06 23:08:44182void RenderThreadImpl::Init() {
183 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33184
[email protected]d3f09092012-02-16 01:52:17185#if defined(OS_MACOSX) || defined(OS_ANDROID)
186 // On Mac and Android, the select popups are rendered by the browser.
[email protected]53c607c2011-03-21 23:19:04187 WebKit::WebView::setUseExternalPopupMenus(true);
188#endif
189
[email protected]94f9a0f682009-06-15 18:30:30190 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28191
[email protected]2c62b562009-01-27 19:04:50192#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34193 // If you are running plugins in this thread you need COM active but in
194 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28195 if (RenderProcessImpl::InProcessPlugins())
[email protected]1a771262011-10-31 09:11:12196 initialize_com_.reset(new base::win::ScopedCOMInitializer());
[email protected]2c62b562009-01-27 19:04:50197#endif
initial.commit09911bf2008-07-26 23:55:29198
[email protected]06c694d2012-02-01 22:26:16199 // Register this object as the main thread.
200 ChildProcess::current()->set_main_thread(this);
201
[email protected]31f87132010-04-21 23:36:21202 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50203 suspend_webkit_shared_timer_ = true;
204 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55205 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03206 widget_count_ = 0;
207 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44208 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48209 idle_notifications_to_skip_ = 0;
[email protected]2db58cf92011-12-01 21:39:01210 compositor_initialized_ = false;
[email protected]8d86fce2009-02-26 23:37:55211
[email protected]526476902011-10-06 20:34:06212 appcache_dispatcher_.reset(new AppCacheDispatcher(Get()));
[email protected]a9bbd0ee2012-04-13 02:16:08213 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13214
[email protected]d8cd8372012-03-09 10:49:51215 media_stream_center_ = NULL;
216
[email protected]017022b2009-07-27 23:06:34217 db_message_filter_ = new DBMessageFilter();
218 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13219
[email protected]80b161a2011-06-27 17:42:11220 vc_manager_ = new VideoCaptureImplManager();
221 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46222
[email protected]f7eb0a392011-07-12 10:19:51223 audio_input_message_filter_ = new AudioInputMessageFilter();
224 AddFilter(audio_input_message_filter_.get());
225
226 audio_message_filter_ = new AudioMessageFilter();
227 AddFilter(audio_message_filter_.get());
228
[email protected]82622452011-07-22 09:57:20229 devtools_agent_message_filter_ = new DevToolsAgentFilter();
230 AddFilter(devtools_agent_message_filter_.get());
231
[email protected]31bfae72011-12-16 02:04:38232 AddFilter(new IndexedDBMessageFilter);
233
[email protected]8d97ade2011-04-14 18:17:08234 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24235
[email protected]433df472012-03-07 20:33:39236 // Note that under Linux, the media library will normally already have
237 // been initialized by the Zygote before this instance became a Renderer.
238 FilePath media_path;
239 PathService::Get(content::DIR_MEDIA_LIBS, &media_path);
240 if (!media_path.empty())
241 media::InitializeMediaLibrary(media_path);
242
[email protected]f1a29a02011-10-06 23:08:44243 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29244}
245
[email protected]f1a29a02011-10-06 23:08:44246RenderThreadImpl::~RenderThreadImpl() {
[email protected]1223d6ef2011-03-28 16:47:50247 FOR_EACH_OBSERVER(
248 RenderProcessObserver, observers_, OnRenderProcessShutdown());
249
[email protected]12cbfda32010-01-30 01:04:25250 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08251 if (web_database_observer_impl_.get())
252 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25253
[email protected]8d86fce2009-02-26 23:37:55254 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20255 RemoveFilter(devtools_agent_message_filter_.get());
256 devtools_agent_message_filter_ = NULL;
257
[email protected]f7eb0a392011-07-12 10:19:51258 RemoveFilter(audio_input_message_filter_.get());
259 audio_input_message_filter_ = NULL;
260
261 RemoveFilter(audio_message_filter_.get());
262 audio_message_filter_ = NULL;
263
[email protected]80b161a2011-06-27 17:42:11264 RemoveFilter(vc_manager_->video_capture_message_filter());
265
[email protected]017022b2009-07-27 23:06:34266 RemoveFilter(db_message_filter_.get());
267 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25268
[email protected]c6a7b862010-08-20 22:19:38269 // Shutdown the file thread if it's running.
270 if (file_thread_.get())
271 file_thread_->Stop();
272
[email protected]2db58cf92011-12-01 21:39:01273 if (compositor_initialized_) {
274 WebKit::WebCompositor::shutdown();
275 compositor_initialized_ = false;
276 }
[email protected]a9fb30aa2011-10-06 06:58:46277 if (compositor_thread_.get()) {
278 RemoveFilter(compositor_thread_->GetMessageFilter());
279 compositor_thread_.reset();
280 }
281
[email protected]8d6cba42011-09-02 10:05:19282 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22283 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22284
[email protected]94f9a0f682009-06-15 18:30:30285 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30286
[email protected]8d86fce2009-02-26 23:37:55287 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50288#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29289 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50290 NPChannelBase::CleanupChannels();
[email protected]2c62b562009-01-27 19:04:50291#endif
initial.commit09911bf2008-07-26 23:55:29292}
293
[email protected]f1a29a02011-10-06 23:08:44294bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14295 // Certain synchronous messages cannot always be processed synchronously by
296 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50297 // This could cause a complete hang of Chrome if a windowed plug-in is trying
298 // to communicate with the renderer thread since the browser's UI thread
299 // could be stuck (within a Windows API call) trying to synchronously
300 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14301 // thread while the browser is processing this request. This creates an
302 // opportunity for re-entrancy into WebKit, so we need to take care to disable
303 // callbacks, timers, and pending network loads that could trigger such
304 // callbacks.
[email protected]38b592902011-04-16 02:08:42305 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50306 if (msg->is_sync()) {
307 if (msg->is_caller_pumping_messages()) {
308 pumping_events = true;
309 } else {
[email protected]38b592902011-04-16 02:08:42310 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
311 msg->type() == ViewHostMsg_GetRawCookies::ID ||
312 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
313 content::GetContentClient()->renderer()->
314 ShouldPumpEventsDuringCookieMessage()) {
315 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50316 }
317 }
[email protected]c1f50aa2010-02-18 03:46:57318 }
319
[email protected]80fc08c52010-03-09 07:43:50320 bool suspend_webkit_shared_timer = true; // default value
321 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57322
[email protected]80fc08c52010-03-09 07:43:50323 bool notify_webkit_of_modal_loop = true; // default value
324 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
325
326 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57327
328 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50329 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19330 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57331
[email protected]39065d012010-07-09 11:22:46332 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57333 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57334
[email protected]80fc08c52010-03-09 07:43:50335 RenderWidget* widget =
336 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
337 if (widget) {
338 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57339 PluginChannelHost::Broadcast(
340 new PluginMsg_SignalModalDialogEvent(host_window));
341 }
342 }
343
344 bool rv = ChildThread::Send(msg);
345
346 if (pumping_events) {
347 if (host_window) {
348 PluginChannelHost::Broadcast(
349 new PluginMsg_ResetModalDialogEvent(host_window));
350 }
351
[email protected]39065d012010-07-09 11:22:46352 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57353 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57354
[email protected]80fc08c52010-03-09 07:43:50355 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19356 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57357 }
358
359 return rv;
360}
361
[email protected]f1a29a02011-10-06 23:08:44362MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06363 return message_loop();
364}
365
[email protected]f1a29a02011-10-06 23:08:44366IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06367 return channel();
368}
369
[email protected]f1a29a02011-10-06 23:08:44370std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06371 // The browser process should have passed the locale to the renderer via the
372 // --lang command line flag. In single process mode, this will return the
373 // wrong value. TODO(tc): Fix this for single process mode.
374 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
375 const std::string& lang =
376 parsed_command_line.GetSwitchValueASCII(switches::kLang);
377 DCHECK(!lang.empty() ||
378 (!parsed_command_line.HasSwitch(switches::kRendererProcess) &&
379 !parsed_command_line.HasSwitch(switches::kPluginProcess)));
380 return lang;
381}
382
[email protected]07bb6332012-01-21 01:07:57383IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
384 return sync_message_filter();
385}
386
[email protected]f1a29a02011-10-06 23:08:44387void RenderThreadImpl::AddRoute(int32 routing_id,
388 IPC::Channel::Listener* listener) {
[email protected]c1f50aa2010-02-18 03:46:57389 widget_count_++;
390 return ChildThread::AddRoute(routing_id, listener);
391}
392
[email protected]f1a29a02011-10-06 23:08:44393void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]c1f50aa2010-02-18 03:46:57394 widget_count_--;
395 return ChildThread::RemoveRoute(routing_id);
396}
397
[email protected]77fc9b92011-10-15 16:20:37398int RenderThreadImpl::GenerateRoutingID() {
399 int routing_id = MSG_ROUTING_NONE;
400 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
401 return routing_id;
402}
403
[email protected]f1a29a02011-10-06 23:08:44404void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55405 channel()->AddFilter(filter);
406}
407
[email protected]f1a29a02011-10-06 23:08:44408void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55409 channel()->RemoveFilter(filter);
410}
411
[email protected]f1a29a02011-10-06 23:08:44412void RenderThreadImpl::SetOutgoingMessageFilter(
[email protected]526476902011-10-06 20:34:06413 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
414}
415
[email protected]f1a29a02011-10-06 23:08:44416void RenderThreadImpl::AddObserver(content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06417 observers_.AddObserver(observer);
418}
419
[email protected]f1a29a02011-10-06 23:08:44420void RenderThreadImpl::RemoveObserver(
421 content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06422 observers_.RemoveObserver(observer);
423}
424
[email protected]359dfa32011-10-12 01:10:15425void RenderThreadImpl::SetResourceDispatcherDelegate(
426 content::ResourceDispatcherDelegate* delegate) {
427 resource_dispatcher()->set_delegate(delegate);
428}
429
[email protected]f1a29a02011-10-06 23:08:44430void RenderThreadImpl::WidgetHidden() {
[email protected]bee16aab2009-08-26 15:55:03431 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50432 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24433
434 if (!content::GetContentClient()->renderer()->
435 RunIdleHandlerWhenWidgetsHidden()) {
436 return;
437 }
438
439 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]6593ae12011-11-14 12:09:44440 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
[email protected]bee16aab2009-08-26 15:55:03441}
442
[email protected]f1a29a02011-10-06 23:08:44443void RenderThreadImpl::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08444 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03445 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24446 if (!content::GetContentClient()->renderer()->
447 RunIdleHandlerWhenWidgetsHidden()) {
448 return;
449 }
[email protected]bee16aab2009-08-26 15:55:03450
[email protected]1784b2f2011-11-24 10:53:48451 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]e4be2dd2010-12-14 00:44:39452}
453
[email protected]f1a29a02011-10-06 23:08:44454void RenderThreadImpl::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19455 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13456 return;
457
458 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
459 v8::V8::SetCreateHistogramFunction(CreateHistogram);
460 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
461
[email protected]8d6cba42011-09-02 10:05:19462 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
463 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13464
[email protected]72787e392012-03-23 05:55:43465 bool has_enable = CommandLine::ForCurrentProcess()->HasSwitch(
466 switches::kEnableThreadedCompositing);
467 bool has_disable = CommandLine::ForCurrentProcess()->HasSwitch(
468 switches::kDisableThreadedCompositing);
469 bool enable = has_enable && (!has_disable);
470 if (enable) {
[email protected]f3150172011-10-22 02:28:45471 compositor_thread_.reset(new CompositorThread(this));
472 AddFilter(compositor_thread_->GetMessageFilter());
[email protected]028217e2011-11-19 00:19:08473 WebKit::WebCompositor::initialize(compositor_thread_->GetWebThread());
[email protected]91acd1c2012-03-14 08:32:39474 } else
[email protected]028217e2011-11-19 00:19:08475 WebKit::WebCompositor::initialize(NULL);
[email protected]2db58cf92011-12-01 21:39:01476 compositor_initialized_ = true;
[email protected]a9fb30aa2011-10-06 06:58:46477
[email protected]d1b8fccc2011-08-03 01:20:13478 WebScriptController::enableV8SingleThreadMode();
479
[email protected]58436a12012-03-21 17:10:26480 RenderThreadImpl::RegisterSchemes();
481
[email protected]d1b8fccc2011-08-03 01:20:13482 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
483
484 webkit_glue::EnableWebCoreLogChannels(
485 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
486
[email protected]766a7082012-02-03 23:39:15487 if (CommandLine::ForCurrentProcess()->HasSwitch(
488 switches::kDomAutomationController)) {
489 base::StringPiece extension = content::GetContentClient()->GetDataResource(
490 IDR_DOM_AUTOMATION_JS);
491 RegisterExtension(new v8::Extension(
492 "dom_automation.js", extension.data(), 0, NULL, extension.size()));
493 }
494
[email protected]fa1c17e42012-03-06 22:29:54495 web_database_observer_impl_.reset(
496 new WebDatabaseObserverImpl(sync_message_filter()));
[email protected]d1b8fccc2011-08-03 01:20:13497 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
498
499 WebRuntimeFeatures::enableSockets(
500 !command_line.HasSwitch(switches::kDisableWebSockets));
501
502 WebRuntimeFeatures::enableDatabase(
503 !command_line.HasSwitch(switches::kDisableDatabases));
504
505 WebRuntimeFeatures::enableDataTransferItems(
506 !command_line.HasSwitch(switches::kDisableDataTransferItems));
507
508 WebRuntimeFeatures::enableApplicationCache(
509 !command_line.HasSwitch(switches::kDisableApplicationCache));
510
511 WebRuntimeFeatures::enableNotifications(
512 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
513
514 WebRuntimeFeatures::enableLocalStorage(
515 !command_line.HasSwitch(switches::kDisableLocalStorage));
516 WebRuntimeFeatures::enableSessionStorage(
517 !command_line.HasSwitch(switches::kDisableSessionStorage));
518
[email protected]b3796c82012-01-19 08:34:33519 WebRuntimeFeatures::enableIndexedDatabase(true);
[email protected]d1b8fccc2011-08-03 01:20:13520
521 WebRuntimeFeatures::enableGeolocation(
522 !command_line.HasSwitch(switches::kDisableGeolocation));
523
[email protected]6aa03b32011-10-27 21:44:44524 WebKit::WebRuntimeFeatures::enableMediaSource(
525 command_line.HasSwitch(switches::kEnableMediaSource));
526
[email protected]433df472012-03-07 20:33:39527 WebRuntimeFeatures::enableMediaPlayer(
528 media::IsMediaLibraryInitialized());
529
[email protected]d1b8fccc2011-08-03 01:20:13530 WebKit::WebRuntimeFeatures::enableMediaStream(
531 command_line.HasSwitch(switches::kEnableMediaStream));
532
[email protected]efdc0282011-08-26 00:36:12533 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21534 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12535
[email protected]e4e68dbb2011-11-18 01:50:22536 WebKit::WebRuntimeFeatures::enablePointerLock(
537 command_line.HasSwitch(switches::kEnablePointerLock));
538
[email protected]f5da41d2011-10-08 17:40:07539 WebKit::WebRuntimeFeatures::enableVideoTrack(
540 command_line.HasSwitch(switches::kEnableVideoTrack));
541
[email protected]9f5b7822012-04-18 23:39:03542 WebKit::WebRuntimeFeatures::enableEncryptedMedia(
543 command_line.HasSwitch(switches::kEnableEncryptedMedia));
544
[email protected]d1b8fccc2011-08-03 01:20:13545 WebRuntimeFeatures::enableWebAudio(
546 !command_line.HasSwitch(switches::kDisableWebAudio));
[email protected]d1b8fccc2011-08-03 01:20:13547
548 WebRuntimeFeatures::enablePushState(true);
549
[email protected]18e9781a2012-02-28 05:27:53550 WebRuntimeFeatures::enableTouch(
551 command_line.HasSwitch(switches::kEnableTouchEvents));
[email protected]d1b8fccc2011-08-03 01:20:13552
553 WebRuntimeFeatures::enableDeviceMotion(
554 command_line.HasSwitch(switches::kEnableDeviceMotion));
555
556 WebRuntimeFeatures::enableDeviceOrientation(
557 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
558
559 WebRuntimeFeatures::enableSpeechInput(
560 !command_line.HasSwitch(switches::kDisableSpeechInput));
561
[email protected]9b7b57d2012-03-21 13:17:48562 WebRuntimeFeatures::enableScriptedSpeech(
563 command_line.HasSwitch(switches::kEnableScriptedSpeech));
564
[email protected]d1b8fccc2011-08-03 01:20:13565 WebRuntimeFeatures::enableFileSystem(
566 !command_line.HasSwitch(switches::kDisableFileSystem));
567
568 WebRuntimeFeatures::enableJavaScriptI18NAPI(
569 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
570
[email protected]0205fbf2011-11-30 21:51:36571 WebRuntimeFeatures::enableGamepad(
572 command_line.HasSwitch(switches::kEnableGamepad));
573
[email protected]d1b8fccc2011-08-03 01:20:13574 WebRuntimeFeatures::enableQuota(true);
575
[email protected]d2edc6702012-01-30 09:13:16576 WebRuntimeFeatures::enableShadowDOM(
577 command_line.HasSwitch(switches::kEnableShadowDOM));
578
[email protected]4961218f2012-02-23 10:11:07579 WebRuntimeFeatures::enableStyleScoped(
580 command_line.HasSwitch(switches::kEnableStyleScoped));
581
[email protected]d1b8fccc2011-08-03 01:20:13582 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]1784b2f2011-11-24 10:53:48583
584 if (content::GetContentClient()->renderer()->
585 RunIdleHandlerWhenWidgetsHidden()) {
586 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
587 }
[email protected]d1b8fccc2011-08-03 01:20:13588}
589
[email protected]58436a12012-03-21 17:10:26590void RenderThreadImpl::RegisterSchemes() {
591 // swappedout: pages should not be accessible, and should also
592 // be treated as empty documents that can commit synchronously.
593 WebString swappedout_scheme(ASCIIToUTF16(chrome::kSwappedOutScheme));
594 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(swappedout_scheme);
595 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(swappedout_scheme);
596}
597
[email protected]f1a29a02011-10-06 23:08:44598void RenderThreadImpl::RecordUserMetrics(const std::string& action) {
[email protected]526476902011-10-06 20:34:06599 Send(new ViewHostMsg_UserMetricsRecordAction(action));
600}
601
[email protected]00614a82011-10-07 22:39:31602base::SharedMemoryHandle RenderThreadImpl::HostAllocateSharedMemoryBuffer(
[email protected]bdae9812011-10-15 00:33:03603 uint32 buffer_size) {
[email protected]00614a82011-10-07 22:39:31604 base::SharedMemoryHandle mem_handle;
[email protected]bdae9812011-10-15 00:33:03605 Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
606 buffer_size, &mem_handle));
[email protected]00614a82011-10-07 22:39:31607 return mem_handle;
608}
609
[email protected]f1a29a02011-10-06 23:08:44610void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06611 WebScriptController::registerExtension(extension);
612 v8_extensions_.insert(extension->name());
613}
614
[email protected]f1a29a02011-10-06 23:08:44615bool RenderThreadImpl::IsRegisteredExtension(
[email protected]526476902011-10-06 20:34:06616 const std::string& v8_extension_name) const {
617 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
618}
619
[email protected]6593ae12011-11-14 12:09:44620void RenderThreadImpl::ScheduleIdleHandler(int64 initial_delay_ms) {
621 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:06622 idle_timer_.Stop();
623 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:44624 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:44625 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06626}
627
[email protected]f1a29a02011-10-06 23:08:44628void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:48629 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
630 content::GetContentClient()->renderer()->
631 RunIdleHandlerWhenWidgetsHidden();
632 if (run_in_foreground_tab) {
633 IdleHandlerInForegroundTab();
634 return;
635 }
[email protected]237a14852012-04-28 02:56:38636
637 base::allocator::ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:06638
639 v8::V8::IdleNotification();
640
641 // Schedule next invocation.
[email protected]6593ae12011-11-14 12:09:44642 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:06643 // delay = delay + 1 / (delay + 2)
644 // Using floor(delay) has a dampening effect such as:
645 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
[email protected]6593ae12011-11-14 12:09:44646 // If the delay is in milliseconds, the above formula is equivalent to:
647 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
648 // which is equivalent to
649 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
650 // Note that idle_notification_delay_in_ms_ would be reset to
651 // kInitialIdleHandlerDelayMs in RenderThreadImpl::WidgetHidden.
652 ScheduleIdleHandler(idle_notification_delay_in_ms_ +
653 1000000 / (idle_notification_delay_in_ms_ + 2000));
[email protected]526476902011-10-06 20:34:06654
655 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
656}
657
[email protected]1784b2f2011-11-24 10:53:48658void RenderThreadImpl::IdleHandlerInForegroundTab() {
659 // Increase the delay in the same way as in IdleHandler,
660 // but make it periodic by reseting it once it is too big.
661 int64 new_delay_ms = idle_notification_delay_in_ms_ +
662 1000000 / (idle_notification_delay_in_ms_ + 2000);
663 if (new_delay_ms >= kLongIdleHandlerDelayMs)
664 new_delay_ms = kShortIdleHandlerDelayMs;
665
[email protected]1784b2f2011-11-24 10:53:48666 if (idle_notifications_to_skip_ > 0) {
667 idle_notifications_to_skip_--;
[email protected]7e967f82011-12-01 09:35:14668 } else {
[email protected]a47105c832011-12-07 17:24:30669 int cpu_usage = 0;
[email protected]7e967f82011-12-01 09:35:14670 Send(new ViewHostMsg_GetCPUUsage(&cpu_usage));
[email protected]1393c4a2012-02-07 10:12:58671 // Idle notification hint roughly specifies the expected duration of the
672 // idle pause. We set it proportional to the idle timer delay.
673 int idle_hint = static_cast<int>(new_delay_ms / 10);
[email protected]d7e59252012-03-06 22:26:02674 if (cpu_usage < kIdleCPUUsageThresholdInPercents) {
[email protected]237a14852012-04-28 02:56:38675 base::allocator::ReleaseFreeMemory();
[email protected]d7e59252012-03-06 22:26:02676 if (v8::V8::IdleNotification(idle_hint)) {
677 // V8 finished collecting garbage.
678 new_delay_ms = kLongIdleHandlerDelayMs;
679 }
[email protected]1784b2f2011-11-24 10:53:48680 }
681 }
[email protected]1784b2f2011-11-24 10:53:48682 ScheduleIdleHandler(new_delay_ms);
683}
684
[email protected]6593ae12011-11-14 12:09:44685int64 RenderThreadImpl::GetIdleNotificationDelayInMs() const {
686 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:06687}
688
[email protected]6593ae12011-11-14 12:09:44689void RenderThreadImpl::SetIdleNotificationDelayInMs(
690 int64 idle_notification_delay_in_ms) {
691 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:08692}
693
[email protected]1784b2f2011-11-24 10:53:48694void RenderThreadImpl::PostponeIdleNotification() {
695 idle_notifications_to_skip_ = 2;
696}
697
[email protected]4a7d6392011-09-19 20:55:08698#if defined(OS_WIN)
[email protected]f1a29a02011-10-06 23:08:44699void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:06700 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:21701}
702
[email protected]f1a29a02011-10-06 23:08:44703void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:06704 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:08705}
[email protected]526476902011-10-06 20:34:06706
[email protected]4a7d6392011-09-19 20:55:08707#endif // OS_WIN
708
[email protected]988aacf2012-02-12 18:10:03709bool RenderThreadImpl::IsWebFrameValid(WebKit::WebFrame* web_frame) {
710 if (!web_frame)
711 return false; // We must be shutting down.
712
713 RenderViewImpl* render_view = RenderViewImpl::FromWebView(web_frame->view());
714 if (!render_view)
715 return false; // We must be shutting down.
716
717 return true;
718}
719
[email protected]b3e83de2012-02-07 03:33:28720bool RenderThreadImpl::IsMainThread() {
721 return !!current();
722}
723
724bool RenderThreadImpl::IsIOThread() {
725 return MessageLoop::current() == ChildProcess::current()->io_message_loop();
726}
727
728MessageLoop* RenderThreadImpl::GetMainLoop() {
729 return message_loop();
730}
[email protected]5b040e592012-02-10 02:56:10731
732scoped_refptr<base::MessageLoopProxy> RenderThreadImpl::GetIOLoopProxy() {
[email protected]b3e83de2012-02-07 03:33:28733 return ChildProcess::current()->io_message_loop_proxy();
734}
735
736base::WaitableEvent* RenderThreadImpl::GetShutDownEvent() {
737 return ChildProcess::current()->GetShutDownEvent();
738}
739
740scoped_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
741 uint32 size) {
[email protected]b3e83de2012-02-07 03:33:28742 base::SharedMemoryHandle handle;
[email protected]8b10b332012-02-29 21:02:14743 bool success;
744 IPC::Message* message =
745 new ChildProcessHostMsg_SyncAllocateSharedMemory(size, &handle);
746
747 // Allow calling this from the compositor thread.
748 if (MessageLoop::current() == message_loop())
749 success = ChildThread::Send(message);
750 else
751 success = sync_message_filter()->Send(message);
752
753 if (!success)
[email protected]b3e83de2012-02-07 03:33:28754 return scoped_ptr<base::SharedMemory>();
[email protected]8b10b332012-02-29 21:02:14755
[email protected]b3e83de2012-02-07 03:33:28756 if (!base::SharedMemory::IsHandleValid(handle))
757 return scoped_ptr<base::SharedMemory>();
758 return scoped_ptr<base::SharedMemory>(new base::SharedMemory(handle, false));
759}
760
761int32 RenderThreadImpl::CreateViewCommandBuffer(
762 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) {
763 int32 route_id = MSG_ROUTING_NONE;
[email protected]8b10b332012-02-29 21:02:14764 IPC::Message* message = new GpuHostMsg_CreateViewCommandBuffer(
[email protected]b3e83de2012-02-07 03:33:28765 surface_id,
766 init_params,
[email protected]8b10b332012-02-29 21:02:14767 &route_id);
768
769 // Allow calling this from the compositor thread.
770 if (MessageLoop::current() == message_loop())
771 ChildThread::Send(message);
772 else
773 sync_message_filter()->Send(message);
774
[email protected]b3e83de2012-02-07 03:33:28775 return route_id;
776}
777
[email protected]f1a29a02011-10-06 23:08:44778int32 RenderThreadImpl::RoutingIDForCurrentContext() {
[email protected]526476902011-10-06 20:34:06779 int32 routing_id = MSG_ROUTING_CONTROL;
780 if (v8::Context::InContext()) {
781 WebFrame* frame = WebFrame::frameForCurrentContext();
782 if (frame) {
[email protected]310ebd6302011-10-10 19:06:28783 RenderViewImpl* view = RenderViewImpl::FromWebView(frame->view());
[email protected]526476902011-10-06 20:34:06784 if (view)
785 routing_id = view->routing_id();
786 }
787 } else {
788 DLOG(WARNING) << "Not called within a script context!";
789 }
790 return routing_id;
791}
792
[email protected]f1a29a02011-10-06 23:08:44793void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:06794 suspend_webkit_shared_timer_ = false;
795}
796
[email protected]f1a29a02011-10-06 23:08:44797void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:06798 notify_webkit_of_modal_loop_ = false;
799}
800
[email protected]4e2a25a2012-01-27 00:42:08801void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const std::string& host,
[email protected]f1a29a02011-10-06 23:08:44802 double zoom_level) {
[email protected]4e2a25a2012-01-27 00:42:08803 RenderViewZoomer zoomer(host, zoom_level);
[email protected]310ebd6302011-10-10 19:06:28804 content::RenderView::ForEach(&zoomer);
[email protected]526476902011-10-06 20:34:06805}
806
[email protected]f1a29a02011-10-06 23:08:44807void RenderThreadImpl::OnDOMStorageEvent(
[email protected]526476902011-10-06 20:34:06808 const DOMStorageMsg_Event_Params& params) {
[email protected]345ff222012-04-26 01:24:00809 EnsureWebKitInitialized();
810
811 bool originated_in_process = params.connection_id != 0;
812 RendererWebStorageAreaImpl* originating_area = NULL;
813 if (originated_in_process) {
814 originating_area = RendererWebStorageAreaImpl::FromConnectionId(
815 params.connection_id);
[email protected]0654cc82011-11-17 09:20:59816 }
[email protected]60e52332012-04-19 20:36:59817
[email protected]345ff222012-04-26 01:24:00818 if (params.namespace_id == dom_storage::kLocalStorageNamespaceId) {
819 WebStorageEventDispatcher::dispatchLocalStorageEvent(
820 params.key,
821 params.old_value,
822 params.new_value,
823 params.origin,
824 params.page_url,
825 originating_area,
826 originated_in_process);
827 } else if (originated_in_process) {
828 // TODO(michaeln): For now, we only raise session storage events into the
829 // process which caused the event to occur. However there are cases where
830 // sessions can span process boundaries, so there are correctness issues.
831 RendererWebStorageNamespaceImpl
832 session_namespace_for_event_dispatch(params.namespace_id);
833 WebStorageEventDispatcher::dispatchSessionStorageEvent(
834 params.key,
835 params.old_value,
836 params.new_value,
837 params.origin,
838 params.page_url,
839 session_namespace_for_event_dispatch,
840 originating_area,
841 originated_in_process);
842 }
[email protected]526476902011-10-06 20:34:06843}
844
[email protected]f1a29a02011-10-06 23:08:44845bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50846 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
847 RenderProcessObserver* observer;
848 while ((observer = it.GetNext()) != NULL) {
849 if (observer->OnControlMessageReceived(msg))
850 return true;
851 }
852
[email protected]70c19a932010-05-14 12:59:11853 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31854 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]a95986a82010-12-24 06:19:28855 return true;
[email protected]1edc16b82009-04-07 17:45:54856
[email protected]a95986a82010-12-24 06:19:28857 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:44858 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:54859 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
860 OnSetZoomLevelForCurrentURL)
[email protected]b9ab10c2009-08-07 18:09:55861 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28862 // TODO(port): removed from render_messages_internal.h;
863 // is there a new non-windows message I should add here?
864 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45865 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09866 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]3e267192011-03-25 01:55:45867 IPC_MESSAGE_HANDLER(DOMStorageMsg_Event, OnDOMStorageEvent)
[email protected]b69934e2011-10-29 02:51:52868 IPC_MESSAGE_HANDLER(ViewMsg_TempCrashWithData, OnTempCrashWithData)
[email protected]a95986a82010-12-24 06:19:28869 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28870 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28871 return handled;
initial.commit09911bf2008-07-26 23:55:29872}
873
[email protected]b9ab10c2009-08-07 18:09:55874// Called when to register CSS Color name->system color mappings.
875// We update the colors one by one and then tell WebKit to refresh all render
876// views.
[email protected]f1a29a02011-10-06 23:08:44877void RenderThreadImpl::OnSetCSSColors(
[email protected]b9ab10c2009-08-07 18:09:55878 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59879 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55880 size_t num_colors = colors.size();
881 scoped_array<WebKit::WebColorName> color_names(
882 new WebKit::WebColorName[num_colors]);
883 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
884 size_t i = 0;
885 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
886 colors.begin();
887 it != colors.end();
888 ++it, ++i) {
889 color_names[i] = it->first;
890 web_colors[i] = it->second;
891 }
892 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
893}
894
[email protected]f1a29a02011-10-06 23:08:44895void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47896 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33897 // When bringing in render_view, also bring in webkit's glue and jsbindings.
[email protected]310ebd6302011-10-10 19:06:28898 RenderViewImpl::Create(
[email protected]8ab04652010-06-12 02:47:26899 params.parent_window,
900 MSG_ROUTING_NONE,
901 params.renderer_preferences,
902 params.web_preferences,
903 new SharedRenderViewCounter(0),
904 params.view_id,
[email protected]9f4f3322012-01-18 22:29:56905 params.surface_id,
[email protected]8ab04652010-06-12 02:47:26906 params.session_storage_namespace_id,
[email protected]74ce1ad2011-12-16 21:51:46907 params.frame_name,
[email protected]6fd35b72012-03-01 19:46:41908 params.next_page_id,
[email protected]8cca3da2012-03-20 08:26:34909 params.screen_info,
[email protected]3e3c4522012-04-13 21:16:29910 params.guest,
911 params.accessibility_mode);
[email protected]7f874dec2009-02-06 01:48:27912}
[email protected]4274e582009-01-27 22:09:56913
[email protected]f1a29a02011-10-06 23:08:44914GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50915 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]6217d392010-03-25 22:08:35916 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22917 // Do nothing if we already have a GPU channel or are already
918 // establishing one.
[email protected]e09cee42010-11-09 01:50:08919 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
920 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04921 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35922
923 // Recreate the channel if it has been lost.
[email protected]894e8fc2012-02-24 13:29:50924 gpu_channel_ = NULL;
[email protected]6217d392010-03-25 22:08:35925 }
926
[email protected]6217d392010-03-25 22:08:35927 // Ask the browser for the channel name.
[email protected]894e8fc2012-02-24 13:29:50928 int client_id = 0;
[email protected]b42a9f62011-06-08 20:36:04929 IPC::ChannelHandle channel_handle;
[email protected]a80f5ece2011-10-20 23:56:55930 content::GPUInfo gpu_info;
[email protected]b42a9f62011-06-08 20:36:04931 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
[email protected]894e8fc2012-02-24 13:29:50932 &client_id,
[email protected]b42a9f62011-06-08 20:36:04933 &channel_handle,
[email protected]b42a9f62011-06-08 20:36:04934 &gpu_info)) ||
[email protected]8bc550e2012-02-07 11:05:56935#if defined(OS_POSIX)
936 channel_handle.socket.fd == -1 ||
937#endif
[email protected]cd0bd792012-04-14 00:52:16938 channel_handle.name.empty()) {
[email protected]b42a9f62011-06-08 20:36:04939 // Otherwise cancel the connection.
940 gpu_channel_ = NULL;
941 return NULL;
942 }
[email protected]6217d392010-03-25 22:08:35943
[email protected]03dc8882012-03-08 23:46:29944 gpu_channel_ = new GpuChannelHost(this, 0, client_id);
[email protected]b42a9f62011-06-08 20:36:04945 gpu_channel_->set_gpu_info(gpu_info);
946 content::GetContentClient()->SetGpuInfo(gpu_info);
947
948 // Connect to the GPU process if a channel name was received.
[email protected]cd0bd792012-04-14 00:52:16949 gpu_channel_->Connect(channel_handle);
[email protected]b42a9f62011-06-08 20:36:04950
[email protected]f9a2b2fe2010-07-15 21:13:23951 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34952}
953
[email protected]d8cd8372012-03-09 10:49:51954WebKit::WebMediaStreamCenter* RenderThreadImpl::CreateMediaStreamCenter(
955 WebKit::WebMediaStreamCenterClient* client) {
956#if defined(ENABLE_WEBRTC)
957 if (!CommandLine::ForCurrentProcess()->HasSwitch(
958 switches::kEnableMediaStream)) {
959 return NULL;
960 }
961 if (!media_stream_center_)
962 media_stream_center_ = new content::MediaStreamCenter(client);
963#endif
964 return media_stream_center_;
965}
966
[email protected]f1a29a02011-10-06 23:08:44967GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]6217d392010-03-25 22:08:35968 if (!gpu_channel_.get())
969 return NULL;
970
[email protected]e09cee42010-11-09 01:50:08971 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:35972 return NULL;
973
974 return gpu_channel_.get();
975}
976
[email protected]f1a29a02011-10-06 23:08:44977void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:59978 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:27979 // The call below will cause a GetPlugins call with refresh=true, but at this
980 // point we already know that the browser has refreshed its list, so disable
981 // refresh temporarily to prevent each renderer process causing the list to be
982 // regenerated.
983 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:45984 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:27985 plugin_refresh_allowed_ = true;
[email protected]3b48dbc2012-01-06 16:34:17986
987 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:27988}
[email protected]85c55dc2009-11-06 03:05:46989
[email protected]f1a29a02011-10-06 23:08:44990void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:09991 EnsureWebKitInitialized();
992 WebNetworkStateNotifier::setOnLine(online);
993}
994
[email protected]b69934e2011-10-29 02:51:52995void RenderThreadImpl::OnTempCrashWithData(const GURL& data) {
[email protected]74ce1ad2011-12-16 21:51:46996 content::GetContentClient()->SetActiveURL(data);
[email protected]b69934e2011-10-29 02:51:52997 CHECK(false);
998}
999
[email protected]c6a7b862010-08-20 22:19:381000scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:441001RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]c6a7b862010-08-20 22:19:381002 DCHECK(message_loop() == MessageLoop::current());
1003 if (!file_thread_.get()) {
1004 file_thread_.reset(new base::Thread("Renderer::FILE"));
1005 file_thread_->Start();
1006 }
1007 return file_thread_->message_loop_proxy();
1008}