blob: 0be789fa2921f9a5e17af98cbf080147fa725af5 [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]83ab4a282012-07-12 18:19:4528#include "content/common/child_histogram_message_filter.h"
[email protected]bdae9812011-10-15 00:33:0329#include "content/common/child_process_messages.h"
[email protected]37666cf2011-03-13 21:51:4230#include "content/common/database_messages.h"
31#include "content/common/db_message_filter.h"
[email protected]127dd582011-03-16 21:32:1032#include "content/common/dom_storage_messages.h"
[email protected]c26ad882012-02-07 06:41:2033#include "content/common/gpu/client/gpu_channel_host.h"
[email protected]202b54ff2011-04-22 21:36:3834#include "content/common/gpu/gpu_messages.h"
[email protected]988aacf2012-02-12 18:10:0335#include "content/common/indexed_db/indexed_db_dispatcher.h"
36#include "content/common/indexed_db/indexed_db_message_filter.h"
[email protected]d3fd748b2011-09-20 17:39:1737#include "content/common/npobject_util.h"
[email protected]105303e2011-03-14 22:16:1038#include "content/common/plugin_messages.h"
[email protected]359dfa32011-10-12 01:10:1539#include "content/common/resource_dispatcher.h"
[email protected]94dc971d2011-03-05 19:08:3240#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5041#include "content/common/view_messages.h"
[email protected]e93e04e2011-03-14 00:27:1042#include "content/common/web_database_observer_impl.h"
[email protected]a458504b2012-07-23 19:57:0643#include "content/public/common/content_constants.h"
[email protected]433df472012-03-07 20:33:3944#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2945#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3146#include "content/public/common/renderer_preferences.h"
[email protected]58436a12012-03-21 17:10:2647#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3448#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3649#include "content/public/renderer/render_process_observer.h"
50#include "content/public/renderer/render_view_visitor.h"
[email protected]14cf0f12012-06-18 17:15:5751#include "content/renderer/browser_plugin/old/browser_plugin_channel_manager.h"
52#include "content/renderer/browser_plugin/old/browser_plugin_registry.h"
[email protected]82622452011-07-22 09:57:2053#include "content/renderer/devtools_agent_filter.h"
[email protected]1910fe82012-05-10 00:04:1054#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
55#include "content/renderer/dom_storage/webstoragearea_impl.h"
56#include "content/renderer/dom_storage/webstoragenamespace_impl.h"
[email protected]a9fb30aa2011-10-06 06:58:4657#include "content/renderer/gpu/compositor_thread.h"
[email protected]e54ab492012-06-12 19:40:0158#include "content/renderer/gpu/gpu_benchmarking_extension.h"
[email protected]3958e972012-07-17 00:25:4159#include "content/renderer/media/audio_hardware.h"
[email protected]f7eb0a392011-07-12 10:19:5160#include "content/renderer/media/audio_input_message_filter.h"
61#include "content/renderer/media/audio_message_filter.h"
[email protected]3958e972012-07-17 00:25:4162#include "content/renderer/media/audio_renderer_mixer_manager.h"
[email protected]d8cd8372012-03-09 10:49:5163#include "content/renderer/media/media_stream_center.h"
[email protected]80b161a2011-06-27 17:42:1164#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3465#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3566#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0567#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:2868#include "content/renderer/render_view_impl.h"
[email protected]8d6cba42011-09-02 10:05:1969#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]766a7082012-02-03 23:39:1570#include "grit/content_resources.h"
[email protected]46f36a492010-07-28 19:36:4171#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1772#include "ipc/ipc_platform_file.h"
[email protected]433df472012-03-07 20:33:3973#include "media/base/media.h"
[email protected]620161e2011-03-07 18:05:2674#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5475#include "net/base/net_util.h"
[email protected]f8c648b2012-02-28 03:46:4576#include "third_party/WebKit/Source/WebKit/chromium/public/WebColorName.h"
[email protected]028217e2011-11-19 00:19:0877#include "third_party/WebKit/Source/WebKit/chromium/public/WebCompositor.h"
[email protected]8bd0fe62011-01-17 06:44:3778#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
79#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3780#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
81#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0982#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]4bd55a32011-07-28 13:28:3883#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3784#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
85#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]58436a12012-03-21 17:10:2686#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
[email protected]61a796c42012-05-17 21:28:2087#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]c49201a2012-05-24 11:04:5788#include "third_party/WebKit/Source/WebKit/chromium/public/platform/WebString.h"
89#include "ui/base/layout.h"
[email protected]18ad6772011-09-20 21:51:3290#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:5091#include "v8/include/v8.h"
[email protected]d471190a2011-02-16 14:52:3092#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5093
[email protected]94dc971d2011-03-05 19:08:3294// TODO(port)
[email protected]bdae9812011-10-15 00:33:0395#if !defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1596#include "base/memory/scoped_handle.h"
[email protected]099587b72011-09-20 00:40:5097#include "content/common/np_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:3298#endif
99
[email protected]da00a2882009-03-09 17:51:19100#if defined(OS_WIN)
101#include <windows.h>
102#include <objbase.h>
103#endif
104
[email protected]6217d392010-03-25 22:08:35105#if defined(OS_POSIX)
106#include "ipc/ipc_channel_posix.h"
107#endif
108
[email protected]b6cb3a842011-06-24 18:28:41109using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:01110using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:09111using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:00112using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:51113using WebKit::WebScriptController;
[email protected]58436a12012-03-21 17:10:26114using WebKit::WebSecurityPolicy;
[email protected]2c434b32009-03-19 06:27:47115using WebKit::WebString;
[email protected]50ae00ef2009-10-19 05:11:03116using WebKit::WebView;
[email protected]3958e972012-07-17 00:25:41117using content::AudioRendererMixerManager;
[email protected]64ffa0442011-10-03 22:08:36118using content::RenderProcessObserver;
initial.commit09911bf2008-07-26 23:55:29119
[email protected]42f1d7822009-07-23 18:17:55120namespace {
[email protected]da9ccfb2012-01-28 00:34:40121
122const int64 kInitialIdleHandlerDelayMs = 1000;
123const int64 kShortIdleHandlerDelayMs = 1000;
124const int64 kLongIdleHandlerDelayMs = 30*1000;
125const int kIdleCPUUsageThresholdInPercents = 3;
[email protected]bee16aab2009-08-26 15:55:03126
[email protected]f1a29a02011-10-06 23:08:44127// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16128// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40129base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49130 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54131
[email protected]64ffa0442011-10-03 22:08:36132class RenderViewZoomer : public content::RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51133 public:
[email protected]4e2a25a2012-01-27 00:42:08134 RenderViewZoomer(const std::string& host, double zoom_level)
135 : host_(host), zoom_level_(zoom_level) {
[email protected]40bd6582009-12-04 23:49:51136 }
137
[email protected]a2ef54c2011-10-10 16:20:31138 virtual bool Visit(content::RenderView* render_view) {
139 WebView* webview = render_view->GetWebView();
[email protected]b6cb3a842011-06-24 18:28:41140 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25141
142 // Don't set zoom level for full-page plugin since they don't use the same
143 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41144 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25145 return true;
146
[email protected]b6cb3a842011-06-24 18:28:41147 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51148 webview->setZoomLevel(false, zoom_level_);
149 return true;
150 }
151
152 private:
153 std::string host_;
[email protected]b75b8292010-10-01 07:28:25154 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51155
156 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
157};
[email protected]0478d0162010-08-28 08:29:40158
[email protected]42f1d7822009-07-23 18:17:55159} // namespace
160
[email protected]d1b8fccc2011-08-03 01:20:13161static void* CreateHistogram(
162 const char *name, int min, int max, size_t buckets) {
163 if (min <= 0)
164 min = 1;
165 base::Histogram* histogram = base::Histogram::FactoryGet(
166 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
167 return histogram;
168}
169
170static void AddHistogramSample(void* hist, int sample) {
171 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
172 histogram->Add(sample);
173}
174
[email protected]f1a29a02011-10-06 23:08:44175RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06176 return lazy_tls.Pointer()->Get();
177}
178
[email protected]42f1d7822009-07-23 18:17:55179// When we run plugins in process, we actually run them on the render thread,
180// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44181RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55182 Init();
183}
184
[email protected]f1a29a02011-10-06 23:08:44185RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55186 : ChildThread(channel_name) {
187 Init();
188}
[email protected]5fa1c542009-05-05 20:36:07189
[email protected]f1a29a02011-10-06 23:08:44190void RenderThreadImpl::Init() {
191 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33192
[email protected]c7e948b42012-07-12 09:20:20193 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
194 v8::V8::SetCreateHistogramFunction(CreateHistogram);
195 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
196
[email protected]d3f09092012-02-16 01:52:17197#if defined(OS_MACOSX) || defined(OS_ANDROID)
198 // On Mac and Android, the select popups are rendered by the browser.
[email protected]53c607c2011-03-21 23:19:04199 WebKit::WebView::setUseExternalPopupMenus(true);
200#endif
201
[email protected]94f9a0f682009-06-15 18:30:30202 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28203
[email protected]2c62b562009-01-27 19:04:50204#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34205 // If you are running plugins in this thread you need COM active but in
206 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28207 if (RenderProcessImpl::InProcessPlugins())
[email protected]1a771262011-10-31 09:11:12208 initialize_com_.reset(new base::win::ScopedCOMInitializer());
[email protected]2c62b562009-01-27 19:04:50209#endif
initial.commit09911bf2008-07-26 23:55:29210
[email protected]06c694d2012-02-01 22:26:16211 // Register this object as the main thread.
212 ChildProcess::current()->set_main_thread(this);
213
[email protected]31f87132010-04-21 23:36:21214 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50215 suspend_webkit_shared_timer_ = true;
216 notify_webkit_of_modal_loop_ = true;
[email protected]bee16aab2009-08-26 15:55:03217 widget_count_ = 0;
218 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44219 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48220 idle_notifications_to_skip_ = 0;
[email protected]2db58cf92011-12-01 21:39:01221 compositor_initialized_ = false;
[email protected]8d86fce2009-02-26 23:37:55222
[email protected]526476902011-10-06 20:34:06223 appcache_dispatcher_.reset(new AppCacheDispatcher(Get()));
[email protected]1910fe82012-05-10 00:04:10224 dom_storage_dispatcher_.reset(new DomStorageDispatcher());
[email protected]a9bbd0ee2012-04-13 02:16:08225 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13226
[email protected]468e4902012-05-23 01:49:31227 browser_plugin_registry_.reset(new content::BrowserPluginRegistry());
228 browser_plugin_channel_manager_.reset(
229 new content::BrowserPluginChannelManager());
230 AddObserver(browser_plugin_channel_manager_.get());
231
[email protected]d8cd8372012-03-09 10:49:51232 media_stream_center_ = NULL;
233
[email protected]017022b2009-07-27 23:06:34234 db_message_filter_ = new DBMessageFilter();
235 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13236
[email protected]80b161a2011-06-27 17:42:11237 vc_manager_ = new VideoCaptureImplManager();
238 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46239
[email protected]f7eb0a392011-07-12 10:19:51240 audio_input_message_filter_ = new AudioInputMessageFilter();
241 AddFilter(audio_input_message_filter_.get());
242
243 audio_message_filter_ = new AudioMessageFilter();
244 AddFilter(audio_message_filter_.get());
245
[email protected]82622452011-07-22 09:57:20246 devtools_agent_message_filter_ = new DevToolsAgentFilter();
247 AddFilter(devtools_agent_message_filter_.get());
248
[email protected]31bfae72011-12-16 02:04:38249 AddFilter(new IndexedDBMessageFilter);
250
[email protected]8d97ade2011-04-14 18:17:08251 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24252
[email protected]302fe422012-06-11 14:49:11253 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]e54ab492012-06-12 19:40:01254 if (command_line.HasSwitch(switches::kEnableGpuBenchmarking))
255 RegisterExtension(content::GpuBenchmarkingExtension::Get());
256
257#if defined(WEBCOMPOSITOR_OWNS_SETTINGS)
[email protected]302fe422012-06-11 14:49:11258 WebKit::WebCompositor::setAcceleratedAnimationEnabled(
259 !command_line.HasSwitch(switches::kDisableThreadedAnimation));
260 WebKit::WebCompositor::setPerTilePaintingEnabled(
261 command_line.HasSwitch(switches::kEnablePerTilePainting));
262 WebKit::WebCompositor::setPartialSwapEnabled(
263 command_line.HasSwitch(switches::kEnablePartialSwap));
264#endif
265
[email protected]433df472012-03-07 20:33:39266 // Note that under Linux, the media library will normally already have
267 // been initialized by the Zygote before this instance became a Renderer.
268 FilePath media_path;
269 PathService::Get(content::DIR_MEDIA_LIBS, &media_path);
270 if (!media_path.empty())
271 media::InitializeMediaLibrary(media_path);
272
[email protected]f1a29a02011-10-06 23:08:44273 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29274}
275
[email protected]f1a29a02011-10-06 23:08:44276RenderThreadImpl::~RenderThreadImpl() {
[email protected]1223d6ef2011-03-28 16:47:50277 FOR_EACH_OBSERVER(
278 RenderProcessObserver, observers_, OnRenderProcessShutdown());
279
[email protected]12cbfda32010-01-30 01:04:25280 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08281 if (web_database_observer_impl_.get())
282 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25283
[email protected]8d86fce2009-02-26 23:37:55284 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20285 RemoveFilter(devtools_agent_message_filter_.get());
286 devtools_agent_message_filter_ = NULL;
287
[email protected]f7eb0a392011-07-12 10:19:51288 RemoveFilter(audio_input_message_filter_.get());
289 audio_input_message_filter_ = NULL;
290
291 RemoveFilter(audio_message_filter_.get());
292 audio_message_filter_ = NULL;
293
[email protected]80b161a2011-06-27 17:42:11294 RemoveFilter(vc_manager_->video_capture_message_filter());
295
[email protected]017022b2009-07-27 23:06:34296 RemoveFilter(db_message_filter_.get());
297 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25298
[email protected]c6a7b862010-08-20 22:19:38299 // Shutdown the file thread if it's running.
300 if (file_thread_.get())
301 file_thread_->Stop();
302
[email protected]2db58cf92011-12-01 21:39:01303 if (compositor_initialized_) {
304 WebKit::WebCompositor::shutdown();
305 compositor_initialized_ = false;
306 }
[email protected]a9fb30aa2011-10-06 06:58:46307 if (compositor_thread_.get()) {
308 RemoveFilter(compositor_thread_->GetMessageFilter());
309 compositor_thread_.reset();
310 }
311
[email protected]8d6cba42011-09-02 10:05:19312 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22313 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22314
[email protected]94f9a0f682009-06-15 18:30:30315 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30316
[email protected]8d86fce2009-02-26 23:37:55317 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50318#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29319 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50320 NPChannelBase::CleanupChannels();
[email protected]2c62b562009-01-27 19:04:50321#endif
initial.commit09911bf2008-07-26 23:55:29322}
323
[email protected]f1a29a02011-10-06 23:08:44324bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14325 // Certain synchronous messages cannot always be processed synchronously by
326 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50327 // This could cause a complete hang of Chrome if a windowed plug-in is trying
328 // to communicate with the renderer thread since the browser's UI thread
329 // could be stuck (within a Windows API call) trying to synchronously
330 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14331 // thread while the browser is processing this request. This creates an
332 // opportunity for re-entrancy into WebKit, so we need to take care to disable
333 // callbacks, timers, and pending network loads that could trigger such
334 // callbacks.
[email protected]38b592902011-04-16 02:08:42335 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50336 if (msg->is_sync()) {
337 if (msg->is_caller_pumping_messages()) {
338 pumping_events = true;
339 } else {
[email protected]38b592902011-04-16 02:08:42340 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
341 msg->type() == ViewHostMsg_GetRawCookies::ID ||
342 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
343 content::GetContentClient()->renderer()->
344 ShouldPumpEventsDuringCookieMessage()) {
345 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50346 }
347 }
[email protected]c1f50aa2010-02-18 03:46:57348 }
349
[email protected]80fc08c52010-03-09 07:43:50350 bool suspend_webkit_shared_timer = true; // default value
351 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57352
[email protected]80fc08c52010-03-09 07:43:50353 bool notify_webkit_of_modal_loop = true; // default value
354 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
355
356 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57357
358 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50359 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19360 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57361
[email protected]39065d012010-07-09 11:22:46362 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57363 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57364
[email protected]80fc08c52010-03-09 07:43:50365 RenderWidget* widget =
366 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
367 if (widget) {
368 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57369 PluginChannelHost::Broadcast(
370 new PluginMsg_SignalModalDialogEvent(host_window));
371 }
372 }
373
374 bool rv = ChildThread::Send(msg);
375
376 if (pumping_events) {
377 if (host_window) {
378 PluginChannelHost::Broadcast(
379 new PluginMsg_ResetModalDialogEvent(host_window));
380 }
381
[email protected]39065d012010-07-09 11:22:46382 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57383 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57384
[email protected]80fc08c52010-03-09 07:43:50385 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19386 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57387 }
388
389 return rv;
390}
391
[email protected]f1a29a02011-10-06 23:08:44392MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06393 return message_loop();
394}
395
[email protected]f1a29a02011-10-06 23:08:44396IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06397 return channel();
398}
399
[email protected]f1a29a02011-10-06 23:08:44400std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06401 // The browser process should have passed the locale to the renderer via the
402 // --lang command line flag. In single process mode, this will return the
403 // wrong value. TODO(tc): Fix this for single process mode.
404 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
405 const std::string& lang =
406 parsed_command_line.GetSwitchValueASCII(switches::kLang);
407 DCHECK(!lang.empty() ||
408 (!parsed_command_line.HasSwitch(switches::kRendererProcess) &&
409 !parsed_command_line.HasSwitch(switches::kPluginProcess)));
410 return lang;
411}
412
[email protected]07bb6332012-01-21 01:07:57413IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
414 return sync_message_filter();
415}
416
[email protected]96191d52012-05-17 01:37:11417scoped_refptr<base::MessageLoopProxy>
418 RenderThreadImpl::GetIOMessageLoopProxy() {
419 return ChildProcess::current()->io_message_loop_proxy();
420}
421
[email protected]c47317e2012-06-20 22:35:31422void RenderThreadImpl::AddRoute(int32 routing_id, IPC::Listener* listener) {
[email protected]c1f50aa2010-02-18 03:46:57423 widget_count_++;
424 return ChildThread::AddRoute(routing_id, listener);
425}
426
[email protected]f1a29a02011-10-06 23:08:44427void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]c1f50aa2010-02-18 03:46:57428 widget_count_--;
429 return ChildThread::RemoveRoute(routing_id);
430}
431
[email protected]77fc9b92011-10-15 16:20:37432int RenderThreadImpl::GenerateRoutingID() {
433 int routing_id = MSG_ROUTING_NONE;
434 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
435 return routing_id;
436}
437
[email protected]f1a29a02011-10-06 23:08:44438void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55439 channel()->AddFilter(filter);
440}
441
[email protected]f1a29a02011-10-06 23:08:44442void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55443 channel()->RemoveFilter(filter);
444}
445
[email protected]f1a29a02011-10-06 23:08:44446void RenderThreadImpl::SetOutgoingMessageFilter(
[email protected]526476902011-10-06 20:34:06447 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
448}
449
[email protected]f1a29a02011-10-06 23:08:44450void RenderThreadImpl::AddObserver(content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06451 observers_.AddObserver(observer);
452}
453
[email protected]f1a29a02011-10-06 23:08:44454void RenderThreadImpl::RemoveObserver(
455 content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06456 observers_.RemoveObserver(observer);
457}
458
[email protected]359dfa32011-10-12 01:10:15459void RenderThreadImpl::SetResourceDispatcherDelegate(
460 content::ResourceDispatcherDelegate* delegate) {
461 resource_dispatcher()->set_delegate(delegate);
462}
463
[email protected]f1a29a02011-10-06 23:08:44464void RenderThreadImpl::WidgetHidden() {
[email protected]bee16aab2009-08-26 15:55:03465 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50466 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24467
468 if (!content::GetContentClient()->renderer()->
469 RunIdleHandlerWhenWidgetsHidden()) {
470 return;
471 }
472
473 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]6593ae12011-11-14 12:09:44474 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
[email protected]bee16aab2009-08-26 15:55:03475}
476
[email protected]f1a29a02011-10-06 23:08:44477void RenderThreadImpl::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08478 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03479 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24480 if (!content::GetContentClient()->renderer()->
481 RunIdleHandlerWhenWidgetsHidden()) {
482 return;
483 }
[email protected]bee16aab2009-08-26 15:55:03484
[email protected]1784b2f2011-11-24 10:53:48485 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]e4be2dd2010-12-14 00:44:39486}
487
[email protected]f1a29a02011-10-06 23:08:44488void RenderThreadImpl::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19489 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13490 return;
491
[email protected]8d6cba42011-09-02 10:05:19492 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
493 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13494
[email protected]a458504b2012-07-23 19:57:06495 base::FieldTrial* thread_trial =
496 base::FieldTrialList::Find(content::kGpuCompositingFieldTrialName);
497 bool is_thread_trial = thread_trial && thread_trial->group_name() ==
498 content::kGpuCompositingFieldTrialThreadEnabledName;
[email protected]72787e392012-03-23 05:55:43499 bool has_enable = CommandLine::ForCurrentProcess()->HasSwitch(
500 switches::kEnableThreadedCompositing);
501 bool has_disable = CommandLine::ForCurrentProcess()->HasSwitch(
502 switches::kDisableThreadedCompositing);
[email protected]3e407b42012-06-25 20:43:26503 // TODO(fsamuel): Guests don't currently support threaded compositing.
504 // This should go away with the new design of the browser plugin.
505 // The new design can be tracked at: http://crbug.com/134492.
506 bool is_guest = CommandLine::ForCurrentProcess()->HasSwitch(
507 switches::kGuestRenderer);
[email protected]a458504b2012-07-23 19:57:06508 DCHECK(!is_thread_trial || !has_disable);
509 bool enable = (is_thread_trial || (has_enable && !has_disable)) && !is_guest;
[email protected]72787e392012-03-23 05:55:43510 if (enable) {
[email protected]f3150172011-10-22 02:28:45511 compositor_thread_.reset(new CompositorThread(this));
512 AddFilter(compositor_thread_->GetMessageFilter());
[email protected]028217e2011-11-19 00:19:08513 WebKit::WebCompositor::initialize(compositor_thread_->GetWebThread());
[email protected]a458504b2012-07-23 19:57:06514 } else {
[email protected]028217e2011-11-19 00:19:08515 WebKit::WebCompositor::initialize(NULL);
[email protected]a458504b2012-07-23 19:57:06516 }
[email protected]2db58cf92011-12-01 21:39:01517 compositor_initialized_ = true;
[email protected]a9fb30aa2011-10-06 06:58:46518
[email protected]d1b8fccc2011-08-03 01:20:13519 WebScriptController::enableV8SingleThreadMode();
520
[email protected]58436a12012-03-21 17:10:26521 RenderThreadImpl::RegisterSchemes();
522
[email protected]d1b8fccc2011-08-03 01:20:13523 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
524
525 webkit_glue::EnableWebCoreLogChannels(
526 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
527
[email protected]766a7082012-02-03 23:39:15528 if (CommandLine::ForCurrentProcess()->HasSwitch(
529 switches::kDomAutomationController)) {
530 base::StringPiece extension = content::GetContentClient()->GetDataResource(
[email protected]c49201a2012-05-24 11:04:57531 IDR_DOM_AUTOMATION_JS, ui::SCALE_FACTOR_NONE);
[email protected]766a7082012-02-03 23:39:15532 RegisterExtension(new v8::Extension(
533 "dom_automation.js", extension.data(), 0, NULL, extension.size()));
534 }
535
[email protected]fa1c17e42012-03-06 22:29:54536 web_database_observer_impl_.reset(
537 new WebDatabaseObserverImpl(sync_message_filter()));
[email protected]d1b8fccc2011-08-03 01:20:13538 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
539
540 WebRuntimeFeatures::enableSockets(
541 !command_line.HasSwitch(switches::kDisableWebSockets));
542
543 WebRuntimeFeatures::enableDatabase(
544 !command_line.HasSwitch(switches::kDisableDatabases));
545
546 WebRuntimeFeatures::enableDataTransferItems(
547 !command_line.HasSwitch(switches::kDisableDataTransferItems));
548
549 WebRuntimeFeatures::enableApplicationCache(
550 !command_line.HasSwitch(switches::kDisableApplicationCache));
551
552 WebRuntimeFeatures::enableNotifications(
553 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
554
555 WebRuntimeFeatures::enableLocalStorage(
556 !command_line.HasSwitch(switches::kDisableLocalStorage));
557 WebRuntimeFeatures::enableSessionStorage(
558 !command_line.HasSwitch(switches::kDisableSessionStorage));
559
[email protected]b3796c82012-01-19 08:34:33560 WebRuntimeFeatures::enableIndexedDatabase(true);
[email protected]d1b8fccc2011-08-03 01:20:13561
562 WebRuntimeFeatures::enableGeolocation(
563 !command_line.HasSwitch(switches::kDisableGeolocation));
564
[email protected]6aa03b32011-10-27 21:44:44565 WebKit::WebRuntimeFeatures::enableMediaSource(
566 command_line.HasSwitch(switches::kEnableMediaSource));
567
[email protected]433df472012-03-07 20:33:39568 WebRuntimeFeatures::enableMediaPlayer(
569 media::IsMediaLibraryInitialized());
570
[email protected]7082fb942012-05-16 23:44:59571 WebKit::WebRuntimeFeatures::enableMediaStream(true);
[email protected]d1b8fccc2011-08-03 01:20:13572
[email protected]8ea05a12012-05-02 09:17:47573 WebKit::WebRuntimeFeatures::enablePeerConnection(
574 command_line.HasSwitch(switches::kEnablePeerConnection));
575
[email protected]efdc0282011-08-26 00:36:12576 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21577 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12578
[email protected]e4e68dbb2011-11-18 01:50:22579 WebKit::WebRuntimeFeatures::enablePointerLock(
[email protected]e4a81662012-06-25 23:57:07580 !command_line.HasSwitch(switches::kDisablePointerLock));
[email protected]e4e68dbb2011-11-18 01:50:22581
[email protected]f5da41d2011-10-08 17:40:07582 WebKit::WebRuntimeFeatures::enableVideoTrack(
583 command_line.HasSwitch(switches::kEnableVideoTrack));
584
[email protected]9f5b7822012-04-18 23:39:03585 WebKit::WebRuntimeFeatures::enableEncryptedMedia(
586 command_line.HasSwitch(switches::kEnableEncryptedMedia));
587
[email protected]634a3bd2012-07-26 04:49:01588#if defined(OS_ANDROID)
589 WebRuntimeFeatures::enableWebAudio(
590 command_line.HasSwitch(switches::kEnableWebAudio) &&
591 media::IsMediaLibraryInitialized());
592#else
[email protected]d1b8fccc2011-08-03 01:20:13593 WebRuntimeFeatures::enableWebAudio(
[email protected]6a1f9cf2012-05-24 21:23:45594 !command_line.HasSwitch(switches::kDisableWebAudio) &&
595 media::IsMediaLibraryInitialized());
[email protected]634a3bd2012-07-26 04:49:01596#endif
[email protected]d1b8fccc2011-08-03 01:20:13597
598 WebRuntimeFeatures::enablePushState(true);
599
[email protected]18e9781a2012-02-28 05:27:53600 WebRuntimeFeatures::enableTouch(
601 command_line.HasSwitch(switches::kEnableTouchEvents));
[email protected]d1b8fccc2011-08-03 01:20:13602
603 WebRuntimeFeatures::enableDeviceMotion(
604 command_line.HasSwitch(switches::kEnableDeviceMotion));
605
606 WebRuntimeFeatures::enableDeviceOrientation(
607 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
608
609 WebRuntimeFeatures::enableSpeechInput(
610 !command_line.HasSwitch(switches::kDisableSpeechInput));
611
[email protected]9b7b57d2012-03-21 13:17:48612 WebRuntimeFeatures::enableScriptedSpeech(
613 command_line.HasSwitch(switches::kEnableScriptedSpeech));
614
[email protected]d1b8fccc2011-08-03 01:20:13615 WebRuntimeFeatures::enableFileSystem(
616 !command_line.HasSwitch(switches::kDisableFileSystem));
617
618 WebRuntimeFeatures::enableJavaScriptI18NAPI(
619 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
620
[email protected]d7a35b72012-05-17 15:59:57621 WebRuntimeFeatures::enableGamepad(true);
[email protected]0205fbf2011-11-30 21:51:36622
[email protected]d1b8fccc2011-08-03 01:20:13623 WebRuntimeFeatures::enableQuota(true);
624
[email protected]d2edc6702012-01-30 09:13:16625 WebRuntimeFeatures::enableShadowDOM(
626 command_line.HasSwitch(switches::kEnableShadowDOM));
627
[email protected]4961218f2012-02-23 10:11:07628 WebRuntimeFeatures::enableStyleScoped(
629 command_line.HasSwitch(switches::kEnableStyleScoped));
630
[email protected]d1b8fccc2011-08-03 01:20:13631 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]1784b2f2011-11-24 10:53:48632
633 if (content::GetContentClient()->renderer()->
634 RunIdleHandlerWhenWidgetsHidden()) {
635 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
636 }
[email protected]d1b8fccc2011-08-03 01:20:13637}
638
[email protected]58436a12012-03-21 17:10:26639void RenderThreadImpl::RegisterSchemes() {
640 // swappedout: pages should not be accessible, and should also
641 // be treated as empty documents that can commit synchronously.
642 WebString swappedout_scheme(ASCIIToUTF16(chrome::kSwappedOutScheme));
643 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(swappedout_scheme);
644 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(swappedout_scheme);
645}
646
[email protected]f1a29a02011-10-06 23:08:44647void RenderThreadImpl::RecordUserMetrics(const std::string& action) {
[email protected]526476902011-10-06 20:34:06648 Send(new ViewHostMsg_UserMetricsRecordAction(action));
649}
650
[email protected]00614a82011-10-07 22:39:31651base::SharedMemoryHandle RenderThreadImpl::HostAllocateSharedMemoryBuffer(
[email protected]bdae9812011-10-15 00:33:03652 uint32 buffer_size) {
[email protected]00614a82011-10-07 22:39:31653 base::SharedMemoryHandle mem_handle;
[email protected]bdae9812011-10-15 00:33:03654 Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
655 buffer_size, &mem_handle));
[email protected]00614a82011-10-07 22:39:31656 return mem_handle;
657}
658
[email protected]f1a29a02011-10-06 23:08:44659void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06660 WebScriptController::registerExtension(extension);
661 v8_extensions_.insert(extension->name());
662}
663
[email protected]f1a29a02011-10-06 23:08:44664bool RenderThreadImpl::IsRegisteredExtension(
[email protected]526476902011-10-06 20:34:06665 const std::string& v8_extension_name) const {
666 return v8_extensions_.find(v8_extension_name) != v8_extensions_.end();
667}
668
[email protected]6593ae12011-11-14 12:09:44669void RenderThreadImpl::ScheduleIdleHandler(int64 initial_delay_ms) {
670 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:06671 idle_timer_.Stop();
672 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:44673 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:44674 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06675}
676
[email protected]f1a29a02011-10-06 23:08:44677void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:48678 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
679 content::GetContentClient()->renderer()->
680 RunIdleHandlerWhenWidgetsHidden();
681 if (run_in_foreground_tab) {
682 IdleHandlerInForegroundTab();
683 return;
684 }
[email protected]237a14852012-04-28 02:56:38685
686 base::allocator::ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:06687
688 v8::V8::IdleNotification();
689
690 // Schedule next invocation.
[email protected]6593ae12011-11-14 12:09:44691 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:06692 // delay = delay + 1 / (delay + 2)
693 // Using floor(delay) has a dampening effect such as:
694 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
[email protected]6593ae12011-11-14 12:09:44695 // If the delay is in milliseconds, the above formula is equivalent to:
696 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
697 // which is equivalent to
698 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
699 // Note that idle_notification_delay_in_ms_ would be reset to
700 // kInitialIdleHandlerDelayMs in RenderThreadImpl::WidgetHidden.
701 ScheduleIdleHandler(idle_notification_delay_in_ms_ +
702 1000000 / (idle_notification_delay_in_ms_ + 2000));
[email protected]526476902011-10-06 20:34:06703
704 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
705}
706
[email protected]1784b2f2011-11-24 10:53:48707void RenderThreadImpl::IdleHandlerInForegroundTab() {
708 // Increase the delay in the same way as in IdleHandler,
709 // but make it periodic by reseting it once it is too big.
710 int64 new_delay_ms = idle_notification_delay_in_ms_ +
711 1000000 / (idle_notification_delay_in_ms_ + 2000);
712 if (new_delay_ms >= kLongIdleHandlerDelayMs)
713 new_delay_ms = kShortIdleHandlerDelayMs;
714
[email protected]1784b2f2011-11-24 10:53:48715 if (idle_notifications_to_skip_ > 0) {
716 idle_notifications_to_skip_--;
[email protected]7e967f82011-12-01 09:35:14717 } else {
[email protected]a47105c832011-12-07 17:24:30718 int cpu_usage = 0;
[email protected]7e967f82011-12-01 09:35:14719 Send(new ViewHostMsg_GetCPUUsage(&cpu_usage));
[email protected]1393c4a2012-02-07 10:12:58720 // Idle notification hint roughly specifies the expected duration of the
721 // idle pause. We set it proportional to the idle timer delay.
722 int idle_hint = static_cast<int>(new_delay_ms / 10);
[email protected]d7e59252012-03-06 22:26:02723 if (cpu_usage < kIdleCPUUsageThresholdInPercents) {
[email protected]237a14852012-04-28 02:56:38724 base::allocator::ReleaseFreeMemory();
[email protected]d7e59252012-03-06 22:26:02725 if (v8::V8::IdleNotification(idle_hint)) {
726 // V8 finished collecting garbage.
727 new_delay_ms = kLongIdleHandlerDelayMs;
728 }
[email protected]1784b2f2011-11-24 10:53:48729 }
730 }
[email protected]1784b2f2011-11-24 10:53:48731 ScheduleIdleHandler(new_delay_ms);
732}
733
[email protected]6593ae12011-11-14 12:09:44734int64 RenderThreadImpl::GetIdleNotificationDelayInMs() const {
735 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:06736}
737
[email protected]6593ae12011-11-14 12:09:44738void RenderThreadImpl::SetIdleNotificationDelayInMs(
739 int64 idle_notification_delay_in_ms) {
740 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:08741}
742
[email protected]a1a7ff32012-07-19 14:03:51743void RenderThreadImpl::ToggleWebKitSharedTimer(bool suspend) {
744 if (suspend_webkit_shared_timer_) {
745 EnsureWebKitInitialized();
746 if (suspend) {
747 webkit_platform_support_->SuspendSharedTimer();
748 } else {
749 webkit_platform_support_->ResumeSharedTimer();
750 }
751 }
752}
753
754void RenderThreadImpl::UpdateHistograms(int sequence_number) {
755 child_histogram_message_filter()->SendHistograms(sequence_number);
756}
757
[email protected]1784b2f2011-11-24 10:53:48758void RenderThreadImpl::PostponeIdleNotification() {
759 idle_notifications_to_skip_ = 2;
760}
761
[email protected]3b2afc32012-06-18 21:31:16762base::WeakPtr<WebGraphicsContext3DCommandBufferImpl>
763RenderThreadImpl::GetGpuVDAContext3D() {
764 // If we already handed out a pointer to a context and it's been lost, create
765 // a new one.
[email protected]71e06562012-07-10 03:12:50766 if (gpu_vda_context3d_.get() && gpu_vda_context3d_->isContextLost()) {
[email protected]3b2afc32012-06-18 21:31:16767 if (compositor_thread()) {
768 compositor_thread()->GetWebThread()->message_loop()->DeleteSoon(
769 FROM_HERE, gpu_vda_context3d_.release());
770 } else {
771 gpu_vda_context3d_.reset();
772 }
773 }
774 if (!gpu_vda_context3d_.get()) {
775 gpu_vda_context3d_.reset(
776 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
[email protected]d1a9e062012-07-13 00:45:14777 this, WebKit::WebGraphicsContext3D::Attributes(),
778 GURL("chrome://gpu/RenderThreadImpl::GetGpuVDAContext3D")));
[email protected]3b2afc32012-06-18 21:31:16779 }
[email protected]2b035302012-07-26 01:23:27780 if (!gpu_vda_context3d_.get())
781 return base::WeakPtr<WebGraphicsContext3DCommandBufferImpl>();
[email protected]3b2afc32012-06-18 21:31:16782 return gpu_vda_context3d_->AsWeakPtr();
783}
784
[email protected]3958e972012-07-17 00:25:41785content::AudioRendererMixerManager*
786RenderThreadImpl::GetAudioRendererMixerManager() {
787 if (!audio_renderer_mixer_manager_.get()) {
788 audio_renderer_mixer_manager_.reset(new AudioRendererMixerManager(
789 audio_hardware::GetOutputSampleRate(),
790 audio_hardware::GetOutputBufferSize()));
791 }
792
793 return audio_renderer_mixer_manager_.get();
794}
795
[email protected]4a7d6392011-09-19 20:55:08796#if defined(OS_WIN)
[email protected]f1a29a02011-10-06 23:08:44797void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:06798 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:21799}
800
[email protected]f1a29a02011-10-06 23:08:44801void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:06802 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:08803}
[email protected]526476902011-10-06 20:34:06804
[email protected]4a7d6392011-09-19 20:55:08805#endif // OS_WIN
806
[email protected]988aacf2012-02-12 18:10:03807bool RenderThreadImpl::IsWebFrameValid(WebKit::WebFrame* web_frame) {
808 if (!web_frame)
809 return false; // We must be shutting down.
810
811 RenderViewImpl* render_view = RenderViewImpl::FromWebView(web_frame->view());
812 if (!render_view)
813 return false; // We must be shutting down.
814
815 return true;
816}
817
[email protected]b3e83de2012-02-07 03:33:28818bool RenderThreadImpl::IsMainThread() {
819 return !!current();
820}
821
822bool RenderThreadImpl::IsIOThread() {
823 return MessageLoop::current() == ChildProcess::current()->io_message_loop();
824}
825
826MessageLoop* RenderThreadImpl::GetMainLoop() {
827 return message_loop();
828}
[email protected]5b040e592012-02-10 02:56:10829
830scoped_refptr<base::MessageLoopProxy> RenderThreadImpl::GetIOLoopProxy() {
[email protected]b3e83de2012-02-07 03:33:28831 return ChildProcess::current()->io_message_loop_proxy();
832}
833
834base::WaitableEvent* RenderThreadImpl::GetShutDownEvent() {
835 return ChildProcess::current()->GetShutDownEvent();
836}
837
838scoped_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
839 uint32 size) {
[email protected]b3e83de2012-02-07 03:33:28840 base::SharedMemoryHandle handle;
[email protected]8b10b332012-02-29 21:02:14841 bool success;
842 IPC::Message* message =
843 new ChildProcessHostMsg_SyncAllocateSharedMemory(size, &handle);
844
845 // Allow calling this from the compositor thread.
846 if (MessageLoop::current() == message_loop())
847 success = ChildThread::Send(message);
848 else
849 success = sync_message_filter()->Send(message);
850
851 if (!success)
[email protected]b3e83de2012-02-07 03:33:28852 return scoped_ptr<base::SharedMemory>();
[email protected]8b10b332012-02-29 21:02:14853
[email protected]b3e83de2012-02-07 03:33:28854 if (!base::SharedMemory::IsHandleValid(handle))
855 return scoped_ptr<base::SharedMemory>();
856 return scoped_ptr<base::SharedMemory>(new base::SharedMemory(handle, false));
857}
858
859int32 RenderThreadImpl::CreateViewCommandBuffer(
860 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) {
[email protected]d13f35d2012-05-18 02:28:15861 TRACE_EVENT1("gpu",
862 "RenderThreadImpl::CreateViewCommandBuffer",
863 "surface_id",
864 surface_id);
865
[email protected]b3e83de2012-02-07 03:33:28866 int32 route_id = MSG_ROUTING_NONE;
[email protected]8b10b332012-02-29 21:02:14867 IPC::Message* message = new GpuHostMsg_CreateViewCommandBuffer(
[email protected]b3e83de2012-02-07 03:33:28868 surface_id,
869 init_params,
[email protected]8b10b332012-02-29 21:02:14870 &route_id);
871
872 // Allow calling this from the compositor thread.
873 if (MessageLoop::current() == message_loop())
874 ChildThread::Send(message);
875 else
876 sync_message_filter()->Send(message);
877
[email protected]b3e83de2012-02-07 03:33:28878 return route_id;
879}
880
[email protected]f1a29a02011-10-06 23:08:44881void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:06882 suspend_webkit_shared_timer_ = false;
883}
884
[email protected]f1a29a02011-10-06 23:08:44885void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:06886 notify_webkit_of_modal_loop_ = false;
887}
888
[email protected]4e2a25a2012-01-27 00:42:08889void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const std::string& host,
[email protected]f1a29a02011-10-06 23:08:44890 double zoom_level) {
[email protected]4e2a25a2012-01-27 00:42:08891 RenderViewZoomer zoomer(host, zoom_level);
[email protected]310ebd6302011-10-10 19:06:28892 content::RenderView::ForEach(&zoomer);
[email protected]526476902011-10-06 20:34:06893}
894
[email protected]f1a29a02011-10-06 23:08:44895bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50896 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
897 RenderProcessObserver* observer;
898 while ((observer = it.GetNext()) != NULL) {
899 if (observer->OnControlMessageReceived(msg))
900 return true;
901 }
902
[email protected]70c19a932010-05-14 12:59:11903 // Some messages are handled by delegates.
[email protected]1910fe82012-05-10 00:04:10904 if (appcache_dispatcher_->OnMessageReceived(msg) ||
905 dom_storage_dispatcher_->OnMessageReceived(msg)) {
[email protected]a95986a82010-12-24 06:19:28906 return true;
[email protected]1910fe82012-05-10 00:04:10907 }
[email protected]1edc16b82009-04-07 17:45:54908
[email protected]a95986a82010-12-24 06:19:28909 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:44910 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:54911 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
912 OnSetZoomLevelForCurrentURL)
[email protected]b9ab10c2009-08-07 18:09:55913 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28914 // TODO(port): removed from render_messages_internal.h;
915 // is there a new non-windows message I should add here?
916 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45917 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09918 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]b69934e2011-10-29 02:51:52919 IPC_MESSAGE_HANDLER(ViewMsg_TempCrashWithData, OnTempCrashWithData)
[email protected]a95986a82010-12-24 06:19:28920 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28921 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28922 return handled;
initial.commit09911bf2008-07-26 23:55:29923}
924
[email protected]b9ab10c2009-08-07 18:09:55925// Called when to register CSS Color name->system color mappings.
926// We update the colors one by one and then tell WebKit to refresh all render
927// views.
[email protected]f1a29a02011-10-06 23:08:44928void RenderThreadImpl::OnSetCSSColors(
[email protected]b9ab10c2009-08-07 18:09:55929 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59930 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55931 size_t num_colors = colors.size();
932 scoped_array<WebKit::WebColorName> color_names(
933 new WebKit::WebColorName[num_colors]);
934 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
935 size_t i = 0;
936 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
937 colors.begin();
938 it != colors.end();
939 ++it, ++i) {
940 color_names[i] = it->first;
941 web_colors[i] = it->second;
942 }
943 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
944}
945
[email protected]f1a29a02011-10-06 23:08:44946void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47947 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33948 // When bringing in render_view, also bring in webkit's glue and jsbindings.
[email protected]468e4902012-05-23 01:49:31949 if (!params.embedder_channel_name.empty()) {
950 browser_plugin_channel_manager()->CreateRenderView(params);
951 } else {
952 RenderViewImpl::Create(
953 params.parent_window,
954 params.opener_route_id,
955 params.renderer_preferences,
956 params.web_preferences,
957 new SharedRenderViewCounter(0),
958 params.view_id,
959 params.surface_id,
960 params.session_storage_namespace_id,
961 params.frame_name,
962 false,
963 params.swapped_out,
964 params.next_page_id,
[email protected]842f10652012-06-06 01:54:04965 params.screen_info,
[email protected]468e4902012-05-23 01:49:31966 NULL,
967 params.accessibility_mode);
968 }
[email protected]7f874dec2009-02-06 01:48:27969}
[email protected]4274e582009-01-27 22:09:56970
[email protected]f1a29a02011-10-06 23:08:44971GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50972 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]d13f35d2012-05-18 02:28:15973 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
974
[email protected]6217d392010-03-25 22:08:35975 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22976 // Do nothing if we already have a GPU channel or are already
977 // establishing one.
[email protected]e09cee42010-11-09 01:50:08978 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
979 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04980 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35981
982 // Recreate the channel if it has been lost.
[email protected]894e8fc2012-02-24 13:29:50983 gpu_channel_ = NULL;
[email protected]6217d392010-03-25 22:08:35984 }
985
[email protected]6217d392010-03-25 22:08:35986 // Ask the browser for the channel name.
[email protected]894e8fc2012-02-24 13:29:50987 int client_id = 0;
[email protected]b42a9f62011-06-08 20:36:04988 IPC::ChannelHandle channel_handle;
[email protected]a80f5ece2011-10-20 23:56:55989 content::GPUInfo gpu_info;
[email protected]b42a9f62011-06-08 20:36:04990 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
[email protected]894e8fc2012-02-24 13:29:50991 &client_id,
[email protected]b42a9f62011-06-08 20:36:04992 &channel_handle,
[email protected]b42a9f62011-06-08 20:36:04993 &gpu_info)) ||
[email protected]8bc550e2012-02-07 11:05:56994#if defined(OS_POSIX)
995 channel_handle.socket.fd == -1 ||
996#endif
[email protected]cd0bd792012-04-14 00:52:16997 channel_handle.name.empty()) {
[email protected]b42a9f62011-06-08 20:36:04998 // Otherwise cancel the connection.
999 gpu_channel_ = NULL;
1000 return NULL;
1001 }
[email protected]6217d392010-03-25 22:08:351002
[email protected]03dc8882012-03-08 23:46:291003 gpu_channel_ = new GpuChannelHost(this, 0, client_id);
[email protected]b42a9f62011-06-08 20:36:041004 gpu_channel_->set_gpu_info(gpu_info);
1005 content::GetContentClient()->SetGpuInfo(gpu_info);
1006
1007 // Connect to the GPU process if a channel name was received.
[email protected]cd0bd792012-04-14 00:52:161008 gpu_channel_->Connect(channel_handle);
[email protected]b42a9f62011-06-08 20:36:041009
[email protected]f9a2b2fe2010-07-15 21:13:231010 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:341011}
1012
[email protected]d8cd8372012-03-09 10:49:511013WebKit::WebMediaStreamCenter* RenderThreadImpl::CreateMediaStreamCenter(
1014 WebKit::WebMediaStreamCenterClient* client) {
1015#if defined(ENABLE_WEBRTC)
[email protected]d8cd8372012-03-09 10:49:511016 if (!media_stream_center_)
1017 media_stream_center_ = new content::MediaStreamCenter(client);
1018#endif
1019 return media_stream_center_;
1020}
1021
[email protected]f1a29a02011-10-06 23:08:441022GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]6217d392010-03-25 22:08:351023 if (!gpu_channel_.get())
1024 return NULL;
1025
[email protected]e09cee42010-11-09 01:50:081026 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:351027 return NULL;
1028
1029 return gpu_channel_.get();
1030}
1031
[email protected]f1a29a02011-10-06 23:08:441032void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:591033 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:271034 // The call below will cause a GetPlugins call with refresh=true, but at this
1035 // point we already know that the browser has refreshed its list, so disable
1036 // refresh temporarily to prevent each renderer process causing the list to be
1037 // regenerated.
[email protected]a70b4352012-07-31 22:34:531038 webkit_platform_support_->set_plugin_refresh_allowed(false);
[email protected]b78e168b2009-09-21 22:05:451039 WebKit::resetPluginCache(reload_pages);
[email protected]a70b4352012-07-31 22:34:531040 webkit_platform_support_->set_plugin_refresh_allowed(true);
[email protected]3b48dbc2012-01-06 16:34:171041
1042 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:271043}
[email protected]85c55dc2009-11-06 03:05:461044
[email protected]f1a29a02011-10-06 23:08:441045void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:091046 EnsureWebKitInitialized();
1047 WebNetworkStateNotifier::setOnLine(online);
1048}
1049
[email protected]b69934e2011-10-29 02:51:521050void RenderThreadImpl::OnTempCrashWithData(const GURL& data) {
[email protected]74ce1ad2011-12-16 21:51:461051 content::GetContentClient()->SetActiveURL(data);
[email protected]b69934e2011-10-29 02:51:521052 CHECK(false);
1053}
1054
[email protected]c6a7b862010-08-20 22:19:381055scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:441056RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]c6a7b862010-08-20 22:19:381057 DCHECK(message_loop() == MessageLoop::current());
1058 if (!file_thread_.get()) {
1059 file_thread_.reset(new base::Thread("Renderer::FILE"));
1060 file_thread_->Start();
1061 }
1062 return file_thread_->message_loop_proxy();
1063}