blob: 5488fb0d45192df48b247eb6f9c004b0bc483f21 [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]b7c4f032012-08-07 16:38:5643#include "content/public/common/compositor_util.h"
[email protected]a458504b2012-07-23 19:57:0644#include "content/public/common/content_constants.h"
[email protected]433df472012-03-07 20:33:3945#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2946#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3147#include "content/public/common/renderer_preferences.h"
[email protected]58436a12012-03-21 17:10:2648#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3449#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3650#include "content/public/renderer/render_process_observer.h"
51#include "content/public/renderer/render_view_visitor.h"
[email protected]14cf0f12012-06-18 17:15:5752#include "content/renderer/browser_plugin/old/browser_plugin_channel_manager.h"
53#include "content/renderer/browser_plugin/old/browser_plugin_registry.h"
[email protected]82622452011-07-22 09:57:2054#include "content/renderer/devtools_agent_filter.h"
[email protected]1910fe82012-05-10 00:04:1055#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
56#include "content/renderer/dom_storage/webstoragearea_impl.h"
57#include "content/renderer/dom_storage/webstoragenamespace_impl.h"
[email protected]a9fb30aa2011-10-06 06:58:4658#include "content/renderer/gpu/compositor_thread.h"
[email protected]1842fe22012-08-13 23:24:3559#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]e54ab492012-06-12 19:40:0160#include "content/renderer/gpu/gpu_benchmarking_extension.h"
[email protected]3958e972012-07-17 00:25:4161#include "content/renderer/media/audio_hardware.h"
[email protected]f7eb0a392011-07-12 10:19:5162#include "content/renderer/media/audio_input_message_filter.h"
63#include "content/renderer/media/audio_message_filter.h"
[email protected]3958e972012-07-17 00:25:4164#include "content/renderer/media/audio_renderer_mixer_manager.h"
[email protected]d8cd8372012-03-09 10:49:5165#include "content/renderer/media/media_stream_center.h"
[email protected]80b161a2011-06-27 17:42:1166#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3467#include "content/renderer/media/video_capture_message_filter.h"
[email protected]6f516082011-03-17 19:15:3568#include "content/renderer/plugin_channel_host.h"
[email protected]8704f89b2011-04-15 00:30:0569#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:2870#include "content/renderer/render_view_impl.h"
[email protected]8d6cba42011-09-02 10:05:1971#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]766a7082012-02-03 23:39:1572#include "grit/content_resources.h"
[email protected]46f36a492010-07-28 19:36:4173#include "ipc/ipc_channel_handle.h"
[email protected]1842fe22012-08-13 23:24:3574#include "ipc/ipc_forwarding_message_filter.h"
[email protected]cb6037d2009-11-16 22:55:1775#include "ipc/ipc_platform_file.h"
[email protected]433df472012-03-07 20:33:3976#include "media/base/media.h"
[email protected]620161e2011-03-07 18:05:2677#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5478#include "net/base/net_util.h"
[email protected]1332623f12012-08-10 03:45:1379#include "third_party/WebKit/Source/Platform/chromium/public/WebCompositor.h"
80#include "third_party/WebKit/Source/Platform/chromium/public/WebString.h"
[email protected]f8c648b2012-02-28 03:46:4581#include "third_party/WebKit/Source/WebKit/chromium/public/WebColorName.h"
[email protected]8bd0fe62011-01-17 06:44:3782#include "third_party/WebKit/Source/WebKit/chromium/public/WebDatabase.h"
83#include "third_party/WebKit/Source/WebKit/chromium/public/WebDocument.h"
[email protected]8bd0fe62011-01-17 06:44:3784#include "third_party/WebKit/Source/WebKit/chromium/public/WebFrame.h"
85#include "third_party/WebKit/Source/WebKit/chromium/public/WebKit.h"
[email protected]6eac57a2011-07-12 21:15:0986#include "third_party/WebKit/Source/WebKit/chromium/public/WebNetworkStateNotifier.h"
[email protected]4bd55a32011-07-28 13:28:3887#include "third_party/WebKit/Source/WebKit/chromium/public/WebPopupMenu.h"
[email protected]8bd0fe62011-01-17 06:44:3788#include "third_party/WebKit/Source/WebKit/chromium/public/WebRuntimeFeatures.h"
89#include "third_party/WebKit/Source/WebKit/chromium/public/WebScriptController.h"
[email protected]58436a12012-03-21 17:10:2690#include "third_party/WebKit/Source/WebKit/chromium/public/WebSecurityPolicy.h"
[email protected]61a796c42012-05-17 21:28:2091#include "third_party/WebKit/Source/WebKit/chromium/public/WebView.h"
[email protected]c49201a2012-05-24 11:04:5792#include "ui/base/layout.h"
[email protected]18ad6772011-09-20 21:51:3293#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:5094#include "v8/include/v8.h"
[email protected]d471190a2011-02-16 14:52:3095#include "webkit/glue/webkit_glue.h"
[email protected]2c62b562009-01-27 19:04:5096
[email protected]94dc971d2011-03-05 19:08:3297// TODO(port)
[email protected]bdae9812011-10-15 00:33:0398#if !defined(OS_WIN)
[email protected]3b63f8f42011-03-28 01:54:1599#include "base/memory/scoped_handle.h"
[email protected]099587b72011-09-20 00:40:50100#include "content/common/np_channel_base.h"
[email protected]94dc971d2011-03-05 19:08:32101#endif
102
[email protected]da00a2882009-03-09 17:51:19103#if defined(OS_WIN)
104#include <windows.h>
105#include <objbase.h>
106#endif
107
[email protected]6217d392010-03-25 22:08:35108#if defined(OS_POSIX)
109#include "ipc/ipc_channel_posix.h"
110#endif
111
[email protected]b6cb3a842011-06-24 18:28:41112using WebKit::WebDocument;
[email protected]f85f0702010-01-30 09:31:01113using WebKit::WebFrame;
[email protected]6eac57a2011-07-12 21:15:09114using WebKit::WebNetworkStateNotifier;
[email protected]adf00bc2009-11-02 18:35:00115using WebKit::WebRuntimeFeatures;
[email protected]98d7127b2009-10-23 18:26:51116using WebKit::WebScriptController;
[email protected]58436a12012-03-21 17:10:26117using WebKit::WebSecurityPolicy;
[email protected]2c434b32009-03-19 06:27:47118using WebKit::WebString;
[email protected]50ae00ef2009-10-19 05:11:03119using WebKit::WebView;
[email protected]3958e972012-07-17 00:25:41120using content::AudioRendererMixerManager;
[email protected]64ffa0442011-10-03 22:08:36121using content::RenderProcessObserver;
initial.commit09911bf2008-07-26 23:55:29122
[email protected]42f1d7822009-07-23 18:17:55123namespace {
[email protected]da9ccfb2012-01-28 00:34:40124
125const int64 kInitialIdleHandlerDelayMs = 1000;
126const int64 kShortIdleHandlerDelayMs = 1000;
127const int64 kLongIdleHandlerDelayMs = 30*1000;
128const int kIdleCPUUsageThresholdInPercents = 3;
[email protected]bee16aab2009-08-26 15:55:03129
[email protected]f1a29a02011-10-06 23:08:44130// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16131// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40132base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49133 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54134
[email protected]64ffa0442011-10-03 22:08:36135class RenderViewZoomer : public content::RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51136 public:
[email protected]4e2a25a2012-01-27 00:42:08137 RenderViewZoomer(const std::string& host, double zoom_level)
138 : host_(host), zoom_level_(zoom_level) {
[email protected]40bd6582009-12-04 23:49:51139 }
140
[email protected]a2ef54c2011-10-10 16:20:31141 virtual bool Visit(content::RenderView* render_view) {
142 WebView* webview = render_view->GetWebView();
[email protected]b6cb3a842011-06-24 18:28:41143 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25144
145 // Don't set zoom level for full-page plugin since they don't use the same
146 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41147 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25148 return true;
149
[email protected]b6cb3a842011-06-24 18:28:41150 if (net::GetHostOrSpecFromURL(GURL(document.url())) == host_)
[email protected]40bd6582009-12-04 23:49:51151 webview->setZoomLevel(false, zoom_level_);
152 return true;
153 }
154
155 private:
156 std::string host_;
[email protected]b75b8292010-10-01 07:28:25157 double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51158
159 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
160};
[email protected]0478d0162010-08-28 08:29:40161
[email protected]42f1d7822009-07-23 18:17:55162} // namespace
163
[email protected]d1b8fccc2011-08-03 01:20:13164static void* CreateHistogram(
165 const char *name, int min, int max, size_t buckets) {
166 if (min <= 0)
167 min = 1;
168 base::Histogram* histogram = base::Histogram::FactoryGet(
169 name, min, max, buckets, base::Histogram::kUmaTargetedHistogramFlag);
170 return histogram;
171}
172
173static void AddHistogramSample(void* hist, int sample) {
174 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
175 histogram->Add(sample);
176}
177
[email protected]fe476b02012-08-13 21:21:38178class RenderThreadImpl::GpuVDAContextLostCallback
179 : public WebKit::WebGraphicsContext3D::WebGraphicsContextLostCallback {
180 public:
181 GpuVDAContextLostCallback() {}
182 virtual ~GpuVDAContextLostCallback() {}
183 virtual void onContextLost() {
184 ChildThread::current()->message_loop()->PostTask(FROM_HERE, base::Bind(
185 &RenderThreadImpl::OnGpuVDAContextLoss));
186 }
187};
188
[email protected]f1a29a02011-10-06 23:08:44189RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06190 return lazy_tls.Pointer()->Get();
191}
192
[email protected]42f1d7822009-07-23 18:17:55193// When we run plugins in process, we actually run them on the render thread,
194// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44195RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55196 Init();
197}
198
[email protected]f1a29a02011-10-06 23:08:44199RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55200 : ChildThread(channel_name) {
201 Init();
202}
[email protected]5fa1c542009-05-05 20:36:07203
[email protected]f1a29a02011-10-06 23:08:44204void RenderThreadImpl::Init() {
205 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33206
[email protected]c7e948b42012-07-12 09:20:20207 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
208 v8::V8::SetCreateHistogramFunction(CreateHistogram);
209 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
210
[email protected]d3f09092012-02-16 01:52:17211#if defined(OS_MACOSX) || defined(OS_ANDROID)
212 // On Mac and Android, the select popups are rendered by the browser.
[email protected]53c607c2011-03-21 23:19:04213 WebKit::WebView::setUseExternalPopupMenus(true);
214#endif
215
[email protected]94f9a0f682009-06-15 18:30:30216 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28217
[email protected]2c62b562009-01-27 19:04:50218#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34219 // If you are running plugins in this thread you need COM active but in
220 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28221 if (RenderProcessImpl::InProcessPlugins())
[email protected]1a771262011-10-31 09:11:12222 initialize_com_.reset(new base::win::ScopedCOMInitializer());
[email protected]2c62b562009-01-27 19:04:50223#endif
initial.commit09911bf2008-07-26 23:55:29224
[email protected]06c694d2012-02-01 22:26:16225 // Register this object as the main thread.
226 ChildProcess::current()->set_main_thread(this);
227
[email protected]31f87132010-04-21 23:36:21228 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50229 suspend_webkit_shared_timer_ = true;
230 notify_webkit_of_modal_loop_ = true;
[email protected]bee16aab2009-08-26 15:55:03231 widget_count_ = 0;
232 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44233 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48234 idle_notifications_to_skip_ = 0;
[email protected]2db58cf92011-12-01 21:39:01235 compositor_initialized_ = false;
[email protected]8d86fce2009-02-26 23:37:55236
[email protected]526476902011-10-06 20:34:06237 appcache_dispatcher_.reset(new AppCacheDispatcher(Get()));
[email protected]1910fe82012-05-10 00:04:10238 dom_storage_dispatcher_.reset(new DomStorageDispatcher());
[email protected]a9bbd0ee2012-04-13 02:16:08239 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13240
[email protected]cb25aa12012-08-07 19:13:43241 browser_plugin_registry_.reset(new content::old::BrowserPluginRegistry());
[email protected]468e4902012-05-23 01:49:31242 browser_plugin_channel_manager_.reset(
[email protected]cb25aa12012-08-07 19:13:43243 new content::old::BrowserPluginChannelManager());
[email protected]468e4902012-05-23 01:49:31244 AddObserver(browser_plugin_channel_manager_.get());
245
[email protected]d8cd8372012-03-09 10:49:51246 media_stream_center_ = NULL;
247
[email protected]017022b2009-07-27 23:06:34248 db_message_filter_ = new DBMessageFilter();
249 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13250
[email protected]80b161a2011-06-27 17:42:11251 vc_manager_ = new VideoCaptureImplManager();
252 AddFilter(vc_manager_->video_capture_message_filter());
[email protected]e25f4d72011-06-08 20:58:46253
[email protected]f7eb0a392011-07-12 10:19:51254 audio_input_message_filter_ = new AudioInputMessageFilter();
255 AddFilter(audio_input_message_filter_.get());
256
257 audio_message_filter_ = new AudioMessageFilter();
258 AddFilter(audio_message_filter_.get());
259
[email protected]82622452011-07-22 09:57:20260 devtools_agent_message_filter_ = new DevToolsAgentFilter();
261 AddFilter(devtools_agent_message_filter_.get());
262
[email protected]31bfae72011-12-16 02:04:38263 AddFilter(new IndexedDBMessageFilter);
264
[email protected]8d97ade2011-04-14 18:17:08265 content::GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24266
[email protected]302fe422012-06-11 14:49:11267 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]e54ab492012-06-12 19:40:01268 if (command_line.HasSwitch(switches::kEnableGpuBenchmarking))
269 RegisterExtension(content::GpuBenchmarkingExtension::Get());
270
[email protected]302fe422012-06-11 14:49:11271 WebKit::WebCompositor::setAcceleratedAnimationEnabled(
272 !command_line.HasSwitch(switches::kDisableThreadedAnimation));
273 WebKit::WebCompositor::setPerTilePaintingEnabled(
274 command_line.HasSwitch(switches::kEnablePerTilePainting));
275 WebKit::WebCompositor::setPartialSwapEnabled(
276 command_line.HasSwitch(switches::kEnablePartialSwap));
[email protected]302fe422012-06-11 14:49:11277
[email protected]fe476b02012-08-13 21:21:38278 context_lost_cb_.reset(new GpuVDAContextLostCallback());
279
[email protected]433df472012-03-07 20:33:39280 // Note that under Linux, the media library will normally already have
281 // been initialized by the Zygote before this instance became a Renderer.
282 FilePath media_path;
283 PathService::Get(content::DIR_MEDIA_LIBS, &media_path);
284 if (!media_path.empty())
285 media::InitializeMediaLibrary(media_path);
286
[email protected]f1a29a02011-10-06 23:08:44287 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29288}
289
[email protected]f1a29a02011-10-06 23:08:44290RenderThreadImpl::~RenderThreadImpl() {
[email protected]1223d6ef2011-03-28 16:47:50291 FOR_EACH_OBSERVER(
292 RenderProcessObserver, observers_, OnRenderProcessShutdown());
293
[email protected]12cbfda32010-01-30 01:04:25294 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08295 if (web_database_observer_impl_.get())
296 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25297
[email protected]8d86fce2009-02-26 23:37:55298 // Shutdown in reverse of the initialization order.
[email protected]82622452011-07-22 09:57:20299 RemoveFilter(devtools_agent_message_filter_.get());
300 devtools_agent_message_filter_ = NULL;
301
[email protected]f7eb0a392011-07-12 10:19:51302 RemoveFilter(audio_input_message_filter_.get());
303 audio_input_message_filter_ = NULL;
304
305 RemoveFilter(audio_message_filter_.get());
306 audio_message_filter_ = NULL;
307
[email protected]80b161a2011-06-27 17:42:11308 RemoveFilter(vc_manager_->video_capture_message_filter());
309
[email protected]017022b2009-07-27 23:06:34310 RemoveFilter(db_message_filter_.get());
311 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25312
[email protected]c6a7b862010-08-20 22:19:38313 // Shutdown the file thread if it's running.
314 if (file_thread_.get())
315 file_thread_->Stop();
316
[email protected]23d2b7e2012-08-14 00:34:09317 if (compositor_output_surface_filter_.get()) {
318 RemoveFilter(compositor_output_surface_filter_.get());
319 compositor_output_surface_filter_ = NULL;
320 }
[email protected]1842fe22012-08-13 23:24:35321
[email protected]2db58cf92011-12-01 21:39:01322 if (compositor_initialized_) {
323 WebKit::WebCompositor::shutdown();
324 compositor_initialized_ = false;
325 }
[email protected]a9fb30aa2011-10-06 06:58:46326 if (compositor_thread_.get()) {
327 RemoveFilter(compositor_thread_->GetMessageFilter());
328 compositor_thread_.reset();
329 }
330
[email protected]8d6cba42011-09-02 10:05:19331 if (webkit_platform_support_.get())
[email protected]9291ed12009-07-23 17:33:22332 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22333
[email protected]94f9a0f682009-06-15 18:30:30334 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30335
[email protected]8d86fce2009-02-26 23:37:55336 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50337#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29338 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50339 NPChannelBase::CleanupChannels();
[email protected]2c62b562009-01-27 19:04:50340#endif
initial.commit09911bf2008-07-26 23:55:29341}
342
[email protected]f1a29a02011-10-06 23:08:44343bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14344 // Certain synchronous messages cannot always be processed synchronously by
345 // the browser, e.g., Chrome frame communicating with the embedding browser.
[email protected]80fc08c52010-03-09 07:43:50346 // This could cause a complete hang of Chrome if a windowed plug-in is trying
347 // to communicate with the renderer thread since the browser's UI thread
348 // could be stuck (within a Windows API call) trying to synchronously
349 // communicate with the plug-in. The remedy is to pump messages on this
[email protected]f23d4da92010-11-24 21:36:14350 // thread while the browser is processing this request. This creates an
351 // opportunity for re-entrancy into WebKit, so we need to take care to disable
352 // callbacks, timers, and pending network loads that could trigger such
353 // callbacks.
[email protected]38b592902011-04-16 02:08:42354 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50355 if (msg->is_sync()) {
356 if (msg->is_caller_pumping_messages()) {
357 pumping_events = true;
358 } else {
[email protected]38b592902011-04-16 02:08:42359 if ((msg->type() == ViewHostMsg_GetCookies::ID ||
360 msg->type() == ViewHostMsg_GetRawCookies::ID ||
361 msg->type() == ViewHostMsg_CookiesEnabled::ID) &&
362 content::GetContentClient()->renderer()->
363 ShouldPumpEventsDuringCookieMessage()) {
364 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50365 }
366 }
[email protected]c1f50aa2010-02-18 03:46:57367 }
368
[email protected]80fc08c52010-03-09 07:43:50369 bool suspend_webkit_shared_timer = true; // default value
370 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57371
[email protected]80fc08c52010-03-09 07:43:50372 bool notify_webkit_of_modal_loop = true; // default value
373 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
374
375 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57376
377 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50378 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19379 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57380
[email protected]39065d012010-07-09 11:22:46381 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57382 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57383
[email protected]80fc08c52010-03-09 07:43:50384 RenderWidget* widget =
385 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
386 if (widget) {
387 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57388 PluginChannelHost::Broadcast(
389 new PluginMsg_SignalModalDialogEvent(host_window));
390 }
391 }
392
393 bool rv = ChildThread::Send(msg);
394
395 if (pumping_events) {
396 if (host_window) {
397 PluginChannelHost::Broadcast(
398 new PluginMsg_ResetModalDialogEvent(host_window));
399 }
400
[email protected]39065d012010-07-09 11:22:46401 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57402 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57403
[email protected]80fc08c52010-03-09 07:43:50404 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19405 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57406 }
407
408 return rv;
409}
410
[email protected]f1a29a02011-10-06 23:08:44411MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06412 return message_loop();
413}
414
[email protected]f1a29a02011-10-06 23:08:44415IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06416 return channel();
417}
418
[email protected]f1a29a02011-10-06 23:08:44419std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06420 // The browser process should have passed the locale to the renderer via the
421 // --lang command line flag. In single process mode, this will return the
422 // wrong value. TODO(tc): Fix this for single process mode.
423 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
424 const std::string& lang =
425 parsed_command_line.GetSwitchValueASCII(switches::kLang);
426 DCHECK(!lang.empty() ||
427 (!parsed_command_line.HasSwitch(switches::kRendererProcess) &&
428 !parsed_command_line.HasSwitch(switches::kPluginProcess)));
429 return lang;
430}
431
[email protected]07bb6332012-01-21 01:07:57432IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
433 return sync_message_filter();
434}
435
[email protected]96191d52012-05-17 01:37:11436scoped_refptr<base::MessageLoopProxy>
437 RenderThreadImpl::GetIOMessageLoopProxy() {
438 return ChildProcess::current()->io_message_loop_proxy();
439}
440
[email protected]c47317e2012-06-20 22:35:31441void RenderThreadImpl::AddRoute(int32 routing_id, IPC::Listener* listener) {
[email protected]c1f50aa2010-02-18 03:46:57442 widget_count_++;
443 return ChildThread::AddRoute(routing_id, listener);
444}
445
[email protected]f1a29a02011-10-06 23:08:44446void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]c1f50aa2010-02-18 03:46:57447 widget_count_--;
448 return ChildThread::RemoveRoute(routing_id);
449}
450
[email protected]77fc9b92011-10-15 16:20:37451int RenderThreadImpl::GenerateRoutingID() {
452 int routing_id = MSG_ROUTING_NONE;
453 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
454 return routing_id;
455}
456
[email protected]f1a29a02011-10-06 23:08:44457void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55458 channel()->AddFilter(filter);
459}
460
[email protected]f1a29a02011-10-06 23:08:44461void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55462 channel()->RemoveFilter(filter);
463}
464
[email protected]f1a29a02011-10-06 23:08:44465void RenderThreadImpl::SetOutgoingMessageFilter(
[email protected]526476902011-10-06 20:34:06466 IPC::ChannelProxy::OutgoingMessageFilter* filter) {
467}
468
[email protected]f1a29a02011-10-06 23:08:44469void RenderThreadImpl::AddObserver(content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06470 observers_.AddObserver(observer);
471}
472
[email protected]f1a29a02011-10-06 23:08:44473void RenderThreadImpl::RemoveObserver(
474 content::RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06475 observers_.RemoveObserver(observer);
476}
477
[email protected]359dfa32011-10-12 01:10:15478void RenderThreadImpl::SetResourceDispatcherDelegate(
479 content::ResourceDispatcherDelegate* delegate) {
480 resource_dispatcher()->set_delegate(delegate);
481}
482
[email protected]f1a29a02011-10-06 23:08:44483void RenderThreadImpl::WidgetHidden() {
[email protected]bee16aab2009-08-26 15:55:03484 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50485 hidden_widget_count_++;
[email protected]6779aa12011-03-29 17:32:24486
487 if (!content::GetContentClient()->renderer()->
488 RunIdleHandlerWhenWidgetsHidden()) {
489 return;
490 }
491
492 if (widget_count_ && hidden_widget_count_ == widget_count_)
[email protected]6593ae12011-11-14 12:09:44493 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
[email protected]bee16aab2009-08-26 15:55:03494}
495
[email protected]f1a29a02011-10-06 23:08:44496void RenderThreadImpl::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08497 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03498 hidden_widget_count_--;
[email protected]6779aa12011-03-29 17:32:24499 if (!content::GetContentClient()->renderer()->
500 RunIdleHandlerWhenWidgetsHidden()) {
501 return;
502 }
[email protected]bee16aab2009-08-26 15:55:03503
[email protected]1784b2f2011-11-24 10:53:48504 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]e4be2dd2010-12-14 00:44:39505}
506
[email protected]f1a29a02011-10-06 23:08:44507void RenderThreadImpl::EnsureWebKitInitialized() {
[email protected]8d6cba42011-09-02 10:05:19508 if (webkit_platform_support_.get())
[email protected]d1b8fccc2011-08-03 01:20:13509 return;
510
[email protected]8d6cba42011-09-02 10:05:19511 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
512 WebKit::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13513
[email protected]3e407b42012-06-25 20:43:26514 // TODO(fsamuel): Guests don't currently support threaded compositing.
515 // This should go away with the new design of the browser plugin.
516 // The new design can be tracked at: http://crbug.com/134492.
517 bool is_guest = CommandLine::ForCurrentProcess()->HasSwitch(
518 switches::kGuestRenderer);
[email protected]b7c4f032012-08-07 16:38:56519 bool enable = content::IsThreadedCompositingEnabled() && !is_guest;
[email protected]72787e392012-03-23 05:55:43520 if (enable) {
[email protected]f3150172011-10-22 02:28:45521 compositor_thread_.reset(new CompositorThread(this));
522 AddFilter(compositor_thread_->GetMessageFilter());
[email protected]028217e2011-11-19 00:19:08523 WebKit::WebCompositor::initialize(compositor_thread_->GetWebThread());
[email protected]a458504b2012-07-23 19:57:06524 } else {
[email protected]028217e2011-11-19 00:19:08525 WebKit::WebCompositor::initialize(NULL);
[email protected]a458504b2012-07-23 19:57:06526 }
[email protected]2db58cf92011-12-01 21:39:01527 compositor_initialized_ = true;
[email protected]a9fb30aa2011-10-06 06:58:46528
[email protected]1842fe22012-08-13 23:24:35529 MessageLoop* output_surface_loop = enable ?
530 compositor_thread_->message_loop() :
531 MessageLoop::current();
532
533 compositor_output_surface_filter_ = CompositorOutputSurface::CreateFilter(
534 output_surface_loop->message_loop_proxy());
535 AddFilter(compositor_output_surface_filter_.get());
536
[email protected]d1b8fccc2011-08-03 01:20:13537 WebScriptController::enableV8SingleThreadMode();
538
[email protected]58436a12012-03-21 17:10:26539 RenderThreadImpl::RegisterSchemes();
540
[email protected]d1b8fccc2011-08-03 01:20:13541 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
542
543 webkit_glue::EnableWebCoreLogChannels(
544 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
545
[email protected]766a7082012-02-03 23:39:15546 if (CommandLine::ForCurrentProcess()->HasSwitch(
547 switches::kDomAutomationController)) {
548 base::StringPiece extension = content::GetContentClient()->GetDataResource(
[email protected]c49201a2012-05-24 11:04:57549 IDR_DOM_AUTOMATION_JS, ui::SCALE_FACTOR_NONE);
[email protected]766a7082012-02-03 23:39:15550 RegisterExtension(new v8::Extension(
551 "dom_automation.js", extension.data(), 0, NULL, extension.size()));
552 }
553
[email protected]fa1c17e42012-03-06 22:29:54554 web_database_observer_impl_.reset(
555 new WebDatabaseObserverImpl(sync_message_filter()));
[email protected]d1b8fccc2011-08-03 01:20:13556 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
557
558 WebRuntimeFeatures::enableSockets(
559 !command_line.HasSwitch(switches::kDisableWebSockets));
560
561 WebRuntimeFeatures::enableDatabase(
562 !command_line.HasSwitch(switches::kDisableDatabases));
563
564 WebRuntimeFeatures::enableDataTransferItems(
565 !command_line.HasSwitch(switches::kDisableDataTransferItems));
566
567 WebRuntimeFeatures::enableApplicationCache(
568 !command_line.HasSwitch(switches::kDisableApplicationCache));
569
570 WebRuntimeFeatures::enableNotifications(
571 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
572
573 WebRuntimeFeatures::enableLocalStorage(
574 !command_line.HasSwitch(switches::kDisableLocalStorage));
575 WebRuntimeFeatures::enableSessionStorage(
576 !command_line.HasSwitch(switches::kDisableSessionStorage));
577
[email protected]b3796c82012-01-19 08:34:33578 WebRuntimeFeatures::enableIndexedDatabase(true);
[email protected]d1b8fccc2011-08-03 01:20:13579
580 WebRuntimeFeatures::enableGeolocation(
581 !command_line.HasSwitch(switches::kDisableGeolocation));
582
[email protected]6aa03b32011-10-27 21:44:44583 WebKit::WebRuntimeFeatures::enableMediaSource(
584 command_line.HasSwitch(switches::kEnableMediaSource));
585
[email protected]433df472012-03-07 20:33:39586 WebRuntimeFeatures::enableMediaPlayer(
587 media::IsMediaLibraryInitialized());
588
[email protected]7082fb942012-05-16 23:44:59589 WebKit::WebRuntimeFeatures::enableMediaStream(true);
[email protected]d1b8fccc2011-08-03 01:20:13590
[email protected]8ea05a12012-05-02 09:17:47591 WebKit::WebRuntimeFeatures::enablePeerConnection(
592 command_line.HasSwitch(switches::kEnablePeerConnection));
593
[email protected]efdc0282011-08-26 00:36:12594 WebKit::WebRuntimeFeatures::enableFullScreenAPI(
[email protected]5e5671a2011-09-08 02:12:21595 !command_line.HasSwitch(switches::kDisableFullScreen));
[email protected]efdc0282011-08-26 00:36:12596
[email protected]e4e68dbb2011-11-18 01:50:22597 WebKit::WebRuntimeFeatures::enablePointerLock(
[email protected]e4a81662012-06-25 23:57:07598 !command_line.HasSwitch(switches::kDisablePointerLock));
[email protected]e4e68dbb2011-11-18 01:50:22599
[email protected]f5da41d2011-10-08 17:40:07600 WebKit::WebRuntimeFeatures::enableVideoTrack(
601 command_line.HasSwitch(switches::kEnableVideoTrack));
602
[email protected]9f5b7822012-04-18 23:39:03603 WebKit::WebRuntimeFeatures::enableEncryptedMedia(
604 command_line.HasSwitch(switches::kEnableEncryptedMedia));
605
[email protected]634a3bd2012-07-26 04:49:01606#if defined(OS_ANDROID)
607 WebRuntimeFeatures::enableWebAudio(
608 command_line.HasSwitch(switches::kEnableWebAudio) &&
609 media::IsMediaLibraryInitialized());
610#else
[email protected]d1b8fccc2011-08-03 01:20:13611 WebRuntimeFeatures::enableWebAudio(
[email protected]6a1f9cf2012-05-24 21:23:45612 !command_line.HasSwitch(switches::kDisableWebAudio) &&
613 media::IsMediaLibraryInitialized());
[email protected]634a3bd2012-07-26 04:49:01614#endif
[email protected]d1b8fccc2011-08-03 01:20:13615
616 WebRuntimeFeatures::enablePushState(true);
617
[email protected]18e9781a2012-02-28 05:27:53618 WebRuntimeFeatures::enableTouch(
619 command_line.HasSwitch(switches::kEnableTouchEvents));
[email protected]d1b8fccc2011-08-03 01:20:13620
621 WebRuntimeFeatures::enableDeviceMotion(
622 command_line.HasSwitch(switches::kEnableDeviceMotion));
623
624 WebRuntimeFeatures::enableDeviceOrientation(
625 !command_line.HasSwitch(switches::kDisableDeviceOrientation));
626
627 WebRuntimeFeatures::enableSpeechInput(
628 !command_line.HasSwitch(switches::kDisableSpeechInput));
629
[email protected]9b7b57d2012-03-21 13:17:48630 WebRuntimeFeatures::enableScriptedSpeech(
631 command_line.HasSwitch(switches::kEnableScriptedSpeech));
632
[email protected]d1b8fccc2011-08-03 01:20:13633 WebRuntimeFeatures::enableFileSystem(
634 !command_line.HasSwitch(switches::kDisableFileSystem));
635
636 WebRuntimeFeatures::enableJavaScriptI18NAPI(
637 !command_line.HasSwitch(switches::kDisableJavaScriptI18NAPI));
638
[email protected]d7a35b72012-05-17 15:59:57639 WebRuntimeFeatures::enableGamepad(true);
[email protected]0205fbf2011-11-30 21:51:36640
[email protected]d1b8fccc2011-08-03 01:20:13641 WebRuntimeFeatures::enableQuota(true);
642
[email protected]d2edc6702012-01-30 09:13:16643 WebRuntimeFeatures::enableShadowDOM(
644 command_line.HasSwitch(switches::kEnableShadowDOM));
645
[email protected]4961218f2012-02-23 10:11:07646 WebRuntimeFeatures::enableStyleScoped(
647 command_line.HasSwitch(switches::kEnableStyleScoped));
648
[email protected]c76f58ba2012-08-11 01:11:57649 WebRuntimeFeatures::enableCSSExclusions(
650 command_line.HasSwitch(switches::kEnableCssExclusions));
651
[email protected]d1b8fccc2011-08-03 01:20:13652 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]1784b2f2011-11-24 10:53:48653
654 if (content::GetContentClient()->renderer()->
655 RunIdleHandlerWhenWidgetsHidden()) {
656 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
657 }
[email protected]d1b8fccc2011-08-03 01:20:13658}
659
[email protected]58436a12012-03-21 17:10:26660void RenderThreadImpl::RegisterSchemes() {
661 // swappedout: pages should not be accessible, and should also
662 // be treated as empty documents that can commit synchronously.
663 WebString swappedout_scheme(ASCIIToUTF16(chrome::kSwappedOutScheme));
664 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(swappedout_scheme);
665 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(swappedout_scheme);
666}
667
[email protected]f1a29a02011-10-06 23:08:44668void RenderThreadImpl::RecordUserMetrics(const std::string& action) {
[email protected]526476902011-10-06 20:34:06669 Send(new ViewHostMsg_UserMetricsRecordAction(action));
670}
671
[email protected]00614a82011-10-07 22:39:31672base::SharedMemoryHandle RenderThreadImpl::HostAllocateSharedMemoryBuffer(
[email protected]bdae9812011-10-15 00:33:03673 uint32 buffer_size) {
[email protected]00614a82011-10-07 22:39:31674 base::SharedMemoryHandle mem_handle;
[email protected]bdae9812011-10-15 00:33:03675 Send(new ChildProcessHostMsg_SyncAllocateSharedMemory(
676 buffer_size, &mem_handle));
[email protected]00614a82011-10-07 22:39:31677 return mem_handle;
678}
679
[email protected]f1a29a02011-10-06 23:08:44680void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06681 WebScriptController::registerExtension(extension);
[email protected]526476902011-10-06 20:34:06682}
683
[email protected]6593ae12011-11-14 12:09:44684void RenderThreadImpl::ScheduleIdleHandler(int64 initial_delay_ms) {
685 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:06686 idle_timer_.Stop();
687 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:44688 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:44689 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06690}
691
[email protected]f1a29a02011-10-06 23:08:44692void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:48693 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
694 content::GetContentClient()->renderer()->
695 RunIdleHandlerWhenWidgetsHidden();
696 if (run_in_foreground_tab) {
697 IdleHandlerInForegroundTab();
698 return;
699 }
[email protected]237a14852012-04-28 02:56:38700
701 base::allocator::ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:06702
703 v8::V8::IdleNotification();
704
705 // Schedule next invocation.
[email protected]6593ae12011-11-14 12:09:44706 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:06707 // delay = delay + 1 / (delay + 2)
708 // Using floor(delay) has a dampening effect such as:
709 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
[email protected]6593ae12011-11-14 12:09:44710 // If the delay is in milliseconds, the above formula is equivalent to:
711 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
712 // which is equivalent to
713 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
714 // Note that idle_notification_delay_in_ms_ would be reset to
715 // kInitialIdleHandlerDelayMs in RenderThreadImpl::WidgetHidden.
716 ScheduleIdleHandler(idle_notification_delay_in_ms_ +
717 1000000 / (idle_notification_delay_in_ms_ + 2000));
[email protected]526476902011-10-06 20:34:06718
719 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
720}
721
[email protected]1784b2f2011-11-24 10:53:48722void RenderThreadImpl::IdleHandlerInForegroundTab() {
723 // Increase the delay in the same way as in IdleHandler,
724 // but make it periodic by reseting it once it is too big.
725 int64 new_delay_ms = idle_notification_delay_in_ms_ +
726 1000000 / (idle_notification_delay_in_ms_ + 2000);
727 if (new_delay_ms >= kLongIdleHandlerDelayMs)
728 new_delay_ms = kShortIdleHandlerDelayMs;
729
[email protected]1784b2f2011-11-24 10:53:48730 if (idle_notifications_to_skip_ > 0) {
731 idle_notifications_to_skip_--;
[email protected]7e967f82011-12-01 09:35:14732 } else {
[email protected]a47105c832011-12-07 17:24:30733 int cpu_usage = 0;
[email protected]7e967f82011-12-01 09:35:14734 Send(new ViewHostMsg_GetCPUUsage(&cpu_usage));
[email protected]1393c4a2012-02-07 10:12:58735 // Idle notification hint roughly specifies the expected duration of the
736 // idle pause. We set it proportional to the idle timer delay.
737 int idle_hint = static_cast<int>(new_delay_ms / 10);
[email protected]d7e59252012-03-06 22:26:02738 if (cpu_usage < kIdleCPUUsageThresholdInPercents) {
[email protected]237a14852012-04-28 02:56:38739 base::allocator::ReleaseFreeMemory();
[email protected]d7e59252012-03-06 22:26:02740 if (v8::V8::IdleNotification(idle_hint)) {
741 // V8 finished collecting garbage.
742 new_delay_ms = kLongIdleHandlerDelayMs;
743 }
[email protected]1784b2f2011-11-24 10:53:48744 }
745 }
[email protected]1784b2f2011-11-24 10:53:48746 ScheduleIdleHandler(new_delay_ms);
747}
748
[email protected]6593ae12011-11-14 12:09:44749int64 RenderThreadImpl::GetIdleNotificationDelayInMs() const {
750 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:06751}
752
[email protected]6593ae12011-11-14 12:09:44753void RenderThreadImpl::SetIdleNotificationDelayInMs(
754 int64 idle_notification_delay_in_ms) {
755 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:08756}
757
[email protected]a1a7ff32012-07-19 14:03:51758void RenderThreadImpl::ToggleWebKitSharedTimer(bool suspend) {
759 if (suspend_webkit_shared_timer_) {
760 EnsureWebKitInitialized();
761 if (suspend) {
762 webkit_platform_support_->SuspendSharedTimer();
763 } else {
764 webkit_platform_support_->ResumeSharedTimer();
765 }
766 }
767}
768
769void RenderThreadImpl::UpdateHistograms(int sequence_number) {
770 child_histogram_message_filter()->SendHistograms(sequence_number);
771}
772
[email protected]1784b2f2011-11-24 10:53:48773void RenderThreadImpl::PostponeIdleNotification() {
774 idle_notifications_to_skip_ = 2;
775}
776
[email protected]fe476b02012-08-13 21:21:38777/* static */
778void RenderThreadImpl::OnGpuVDAContextLoss() {
779 RenderThreadImpl* self = RenderThreadImpl::current();
780 DCHECK(self);
781 if (!self->gpu_vda_context3d_.get())
782 return;
783 if (self->compositor_thread()) {
784 self->compositor_thread()->GetWebThread()->message_loop()->DeleteSoon(
785 FROM_HERE, self->gpu_vda_context3d_.release());
786 } else {
787 self->gpu_vda_context3d_.reset();
[email protected]3b2afc32012-06-18 21:31:16788 }
[email protected]fe476b02012-08-13 21:21:38789}
790
791WebGraphicsContext3DCommandBufferImpl*
792RenderThreadImpl::GetGpuVDAContext3D() {
[email protected]3b2afc32012-06-18 21:31:16793 if (!gpu_vda_context3d_.get()) {
794 gpu_vda_context3d_.reset(
795 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
[email protected]d1a9e062012-07-13 00:45:14796 this, WebKit::WebGraphicsContext3D::Attributes(),
797 GURL("chrome://gpu/RenderThreadImpl::GetGpuVDAContext3D")));
[email protected]fe476b02012-08-13 21:21:38798 gpu_vda_context3d_->setContextLostCallback(context_lost_cb_.get());
[email protected]3b2afc32012-06-18 21:31:16799 }
[email protected]fe476b02012-08-13 21:21:38800 return gpu_vda_context3d_.get();
[email protected]3b2afc32012-06-18 21:31:16801}
802
[email protected]3958e972012-07-17 00:25:41803content::AudioRendererMixerManager*
804RenderThreadImpl::GetAudioRendererMixerManager() {
805 if (!audio_renderer_mixer_manager_.get()) {
806 audio_renderer_mixer_manager_.reset(new AudioRendererMixerManager(
807 audio_hardware::GetOutputSampleRate(),
808 audio_hardware::GetOutputBufferSize()));
809 }
810
811 return audio_renderer_mixer_manager_.get();
812}
813
[email protected]4a7d6392011-09-19 20:55:08814#if defined(OS_WIN)
[email protected]f1a29a02011-10-06 23:08:44815void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:06816 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:21817}
818
[email protected]f1a29a02011-10-06 23:08:44819void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:06820 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:08821}
[email protected]526476902011-10-06 20:34:06822
[email protected]4a7d6392011-09-19 20:55:08823#endif // OS_WIN
824
[email protected]988aacf2012-02-12 18:10:03825bool RenderThreadImpl::IsWebFrameValid(WebKit::WebFrame* web_frame) {
826 if (!web_frame)
827 return false; // We must be shutting down.
828
829 RenderViewImpl* render_view = RenderViewImpl::FromWebView(web_frame->view());
830 if (!render_view)
831 return false; // We must be shutting down.
832
833 return true;
834}
835
[email protected]b3e83de2012-02-07 03:33:28836bool RenderThreadImpl::IsMainThread() {
837 return !!current();
838}
839
840bool RenderThreadImpl::IsIOThread() {
841 return MessageLoop::current() == ChildProcess::current()->io_message_loop();
842}
843
844MessageLoop* RenderThreadImpl::GetMainLoop() {
845 return message_loop();
846}
[email protected]5b040e592012-02-10 02:56:10847
848scoped_refptr<base::MessageLoopProxy> RenderThreadImpl::GetIOLoopProxy() {
[email protected]b3e83de2012-02-07 03:33:28849 return ChildProcess::current()->io_message_loop_proxy();
850}
851
852base::WaitableEvent* RenderThreadImpl::GetShutDownEvent() {
853 return ChildProcess::current()->GetShutDownEvent();
854}
855
856scoped_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
857 uint32 size) {
[email protected]b3e83de2012-02-07 03:33:28858 base::SharedMemoryHandle handle;
[email protected]8b10b332012-02-29 21:02:14859 bool success;
860 IPC::Message* message =
861 new ChildProcessHostMsg_SyncAllocateSharedMemory(size, &handle);
862
863 // Allow calling this from the compositor thread.
864 if (MessageLoop::current() == message_loop())
865 success = ChildThread::Send(message);
866 else
867 success = sync_message_filter()->Send(message);
868
869 if (!success)
[email protected]b3e83de2012-02-07 03:33:28870 return scoped_ptr<base::SharedMemory>();
[email protected]8b10b332012-02-29 21:02:14871
[email protected]b3e83de2012-02-07 03:33:28872 if (!base::SharedMemory::IsHandleValid(handle))
873 return scoped_ptr<base::SharedMemory>();
874 return scoped_ptr<base::SharedMemory>(new base::SharedMemory(handle, false));
875}
876
877int32 RenderThreadImpl::CreateViewCommandBuffer(
878 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) {
[email protected]d13f35d2012-05-18 02:28:15879 TRACE_EVENT1("gpu",
880 "RenderThreadImpl::CreateViewCommandBuffer",
881 "surface_id",
882 surface_id);
883
[email protected]b3e83de2012-02-07 03:33:28884 int32 route_id = MSG_ROUTING_NONE;
[email protected]8b10b332012-02-29 21:02:14885 IPC::Message* message = new GpuHostMsg_CreateViewCommandBuffer(
[email protected]b3e83de2012-02-07 03:33:28886 surface_id,
887 init_params,
[email protected]8b10b332012-02-29 21:02:14888 &route_id);
889
890 // Allow calling this from the compositor thread.
891 if (MessageLoop::current() == message_loop())
892 ChildThread::Send(message);
893 else
894 sync_message_filter()->Send(message);
895
[email protected]b3e83de2012-02-07 03:33:28896 return route_id;
897}
898
[email protected]f1a29a02011-10-06 23:08:44899void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:06900 suspend_webkit_shared_timer_ = false;
901}
902
[email protected]f1a29a02011-10-06 23:08:44903void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:06904 notify_webkit_of_modal_loop_ = false;
905}
906
[email protected]4e2a25a2012-01-27 00:42:08907void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const std::string& host,
[email protected]f1a29a02011-10-06 23:08:44908 double zoom_level) {
[email protected]4e2a25a2012-01-27 00:42:08909 RenderViewZoomer zoomer(host, zoom_level);
[email protected]310ebd6302011-10-10 19:06:28910 content::RenderView::ForEach(&zoomer);
[email protected]526476902011-10-06 20:34:06911}
912
[email protected]f1a29a02011-10-06 23:08:44913bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:50914 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
915 RenderProcessObserver* observer;
916 while ((observer = it.GetNext()) != NULL) {
917 if (observer->OnControlMessageReceived(msg))
918 return true;
919 }
920
[email protected]70c19a932010-05-14 12:59:11921 // Some messages are handled by delegates.
[email protected]1910fe82012-05-10 00:04:10922 if (appcache_dispatcher_->OnMessageReceived(msg) ||
923 dom_storage_dispatcher_->OnMessageReceived(msg)) {
[email protected]a95986a82010-12-24 06:19:28924 return true;
[email protected]1910fe82012-05-10 00:04:10925 }
[email protected]1edc16b82009-04-07 17:45:54926
[email protected]a95986a82010-12-24 06:19:28927 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:44928 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:54929 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
930 OnSetZoomLevelForCurrentURL)
[email protected]b9ab10c2009-08-07 18:09:55931 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28932 // TODO(port): removed from render_messages_internal.h;
933 // is there a new non-windows message I should add here?
934 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:45935 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:09936 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]b69934e2011-10-29 02:51:52937 IPC_MESSAGE_HANDLER(ViewMsg_TempCrashWithData, OnTempCrashWithData)
[email protected]a95986a82010-12-24 06:19:28938 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:28939 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:28940 return handled;
initial.commit09911bf2008-07-26 23:55:29941}
942
[email protected]b9ab10c2009-08-07 18:09:55943// Called when to register CSS Color name->system color mappings.
944// We update the colors one by one and then tell WebKit to refresh all render
945// views.
[email protected]f1a29a02011-10-06 23:08:44946void RenderThreadImpl::OnSetCSSColors(
[email protected]b9ab10c2009-08-07 18:09:55947 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59948 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55949 size_t num_colors = colors.size();
950 scoped_array<WebKit::WebColorName> color_names(
951 new WebKit::WebColorName[num_colors]);
952 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
953 size_t i = 0;
954 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
955 colors.begin();
956 it != colors.end();
957 ++it, ++i) {
958 color_names[i] = it->first;
959 web_colors[i] = it->second;
960 }
961 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
962}
963
[email protected]f1a29a02011-10-06 23:08:44964void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47965 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33966 // When bringing in render_view, also bring in webkit's glue and jsbindings.
[email protected]468e4902012-05-23 01:49:31967 if (!params.embedder_channel_name.empty()) {
968 browser_plugin_channel_manager()->CreateRenderView(params);
969 } else {
970 RenderViewImpl::Create(
971 params.parent_window,
972 params.opener_route_id,
973 params.renderer_preferences,
974 params.web_preferences,
975 new SharedRenderViewCounter(0),
976 params.view_id,
977 params.surface_id,
978 params.session_storage_namespace_id,
979 params.frame_name,
980 false,
981 params.swapped_out,
982 params.next_page_id,
[email protected]842f10652012-06-06 01:54:04983 params.screen_info,
[email protected]468e4902012-05-23 01:49:31984 NULL,
985 params.accessibility_mode);
986 }
[email protected]7f874dec2009-02-06 01:48:27987}
[email protected]4274e582009-01-27 22:09:56988
[email protected]f1a29a02011-10-06 23:08:44989GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]7f3a2cf2011-04-06 00:10:50990 content::CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]d13f35d2012-05-18 02:28:15991 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
992
[email protected]6217d392010-03-25 22:08:35993 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22994 // Do nothing if we already have a GPU channel or are already
995 // establishing one.
[email protected]e09cee42010-11-09 01:50:08996 if (gpu_channel_->state() == GpuChannelHost::kUnconnected ||
997 gpu_channel_->state() == GpuChannelHost::kConnected)
[email protected]b42a9f62011-06-08 20:36:04998 return GetGpuChannel();
[email protected]6217d392010-03-25 22:08:35999
1000 // Recreate the channel if it has been lost.
[email protected]894e8fc2012-02-24 13:29:501001 gpu_channel_ = NULL;
[email protected]6217d392010-03-25 22:08:351002 }
1003
[email protected]6217d392010-03-25 22:08:351004 // Ask the browser for the channel name.
[email protected]894e8fc2012-02-24 13:29:501005 int client_id = 0;
[email protected]b42a9f62011-06-08 20:36:041006 IPC::ChannelHandle channel_handle;
[email protected]a80f5ece2011-10-20 23:56:551007 content::GPUInfo gpu_info;
[email protected]b42a9f62011-06-08 20:36:041008 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
[email protected]894e8fc2012-02-24 13:29:501009 &client_id,
[email protected]b42a9f62011-06-08 20:36:041010 &channel_handle,
[email protected]b42a9f62011-06-08 20:36:041011 &gpu_info)) ||
[email protected]8bc550e2012-02-07 11:05:561012#if defined(OS_POSIX)
1013 channel_handle.socket.fd == -1 ||
1014#endif
[email protected]cd0bd792012-04-14 00:52:161015 channel_handle.name.empty()) {
[email protected]b42a9f62011-06-08 20:36:041016 // Otherwise cancel the connection.
1017 gpu_channel_ = NULL;
1018 return NULL;
1019 }
[email protected]6217d392010-03-25 22:08:351020
[email protected]03dc8882012-03-08 23:46:291021 gpu_channel_ = new GpuChannelHost(this, 0, client_id);
[email protected]b42a9f62011-06-08 20:36:041022 gpu_channel_->set_gpu_info(gpu_info);
1023 content::GetContentClient()->SetGpuInfo(gpu_info);
1024
1025 // Connect to the GPU process if a channel name was received.
[email protected]cd0bd792012-04-14 00:52:161026 gpu_channel_->Connect(channel_handle);
[email protected]b42a9f62011-06-08 20:36:041027
[email protected]f9a2b2fe2010-07-15 21:13:231028 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:341029}
1030
[email protected]d8cd8372012-03-09 10:49:511031WebKit::WebMediaStreamCenter* RenderThreadImpl::CreateMediaStreamCenter(
1032 WebKit::WebMediaStreamCenterClient* client) {
1033#if defined(ENABLE_WEBRTC)
[email protected]d8cd8372012-03-09 10:49:511034 if (!media_stream_center_)
1035 media_stream_center_ = new content::MediaStreamCenter(client);
1036#endif
1037 return media_stream_center_;
1038}
1039
[email protected]f1a29a02011-10-06 23:08:441040GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]6217d392010-03-25 22:08:351041 if (!gpu_channel_.get())
1042 return NULL;
1043
[email protected]e09cee42010-11-09 01:50:081044 if (gpu_channel_->state() != GpuChannelHost::kConnected)
[email protected]6217d392010-03-25 22:08:351045 return NULL;
1046
1047 return gpu_channel_.get();
1048}
1049
[email protected]f1a29a02011-10-06 23:08:441050void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:591051 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:271052 // The call below will cause a GetPlugins call with refresh=true, but at this
1053 // point we already know that the browser has refreshed its list, so disable
1054 // refresh temporarily to prevent each renderer process causing the list to be
1055 // regenerated.
[email protected]a70b4352012-07-31 22:34:531056 webkit_platform_support_->set_plugin_refresh_allowed(false);
[email protected]b78e168b2009-09-21 22:05:451057 WebKit::resetPluginCache(reload_pages);
[email protected]a70b4352012-07-31 22:34:531058 webkit_platform_support_->set_plugin_refresh_allowed(true);
[email protected]3b48dbc2012-01-06 16:34:171059
1060 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:271061}
[email protected]85c55dc2009-11-06 03:05:461062
[email protected]f1a29a02011-10-06 23:08:441063void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:091064 EnsureWebKitInitialized();
1065 WebNetworkStateNotifier::setOnLine(online);
1066}
1067
[email protected]b69934e2011-10-29 02:51:521068void RenderThreadImpl::OnTempCrashWithData(const GURL& data) {
[email protected]74ce1ad2011-12-16 21:51:461069 content::GetContentClient()->SetActiveURL(data);
[email protected]b69934e2011-10-29 02:51:521070 CHECK(false);
1071}
1072
[email protected]c6a7b862010-08-20 22:19:381073scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:441074RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]c6a7b862010-08-20 22:19:381075 DCHECK(message_loop() == MessageLoop::current());
1076 if (!file_thread_.get()) {
1077 file_thread_.reset(new base::Thread("Renderer::FILE"));
1078 file_thread_->Start();
1079 }
1080 return file_thread_->message_loop_proxy();
1081}