blob: bf37dc2c1b3d28ea56d911a14cbd2cabe7add338 [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]fd33b59ff2013-06-21 22:57:0517#include "base/memory/discardable_memory.h"
[email protected]8bac37b2013-07-18 19:13:5618#include "base/memory/shared_memory.h"
[email protected]835d7c82010-10-14 04:38:3819#include "base/metrics/field_trial.h"
[email protected]6cf19311f2011-04-14 23:06:0220#include "base/metrics/histogram.h"
[email protected]835d7c82010-10-14 04:38:3821#include "base/metrics/stats_table.h"
[email protected]433df472012-03-07 20:33:3922#include "base/path_service.h"
[email protected]21aa99682013-06-11 07:17:0123#include "base/strings/string16.h"
[email protected]6bd867b2013-07-24 22:10:2024#include "base/strings/string_tokenizer.h"
[email protected]1e1d1e12014-01-17 16:14:2925#include "base/strings/string_number_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0026#include "base/strings/utf_string_conversions.h"
[email protected]1357c322010-12-30 22:18:5627#include "base/threading/thread_local.h"
[email protected]d293572a2013-05-23 18:50:4528#include "base/threading/thread_restrictions.h"
[email protected]7a4de7a62010-08-17 18:38:2429#include "base/values.h"
[email protected]d72493152014-01-08 17:37:4530#include "cc/base/switches.h"
[email protected]1e1d1e12014-01-17 16:14:2931#include "cc/resources/worker_pool.h"
[email protected]a8cb3b73b2013-08-12 05:50:5032#include "content/child/appcache/appcache_dispatcher.h"
33#include "content/child/appcache/appcache_frontend_impl.h"
[email protected]10208ea2013-06-06 20:08:0334#include "content/child/child_histogram_message_filter.h"
[email protected]89c36f82013-07-17 06:23:2735#include "content/child/db_message_filter.h"
[email protected]c7199a6e2013-06-04 12:56:0136#include "content/child/indexed_db/indexed_db_dispatcher.h"
37#include "content/child/indexed_db/indexed_db_message_filter.h"
[email protected]defa597a2013-07-19 02:22:1338#include "content/child/npapi/npobject_util.h"
[email protected]c7199a6e2013-06-04 12:56:0139#include "content/child/plugin_messages.h"
[email protected]10208ea2013-06-06 20:08:0340#include "content/child/resource_dispatcher.h"
[email protected]643255da2013-06-12 20:55:1341#include "content/child/runtime_features.h"
[email protected]57cf97882013-06-26 20:39:0142#include "content/child/thread_safe_sender.h"
[email protected]c7199a6e2013-06-04 12:56:0143#include "content/child/web_database_observer_impl.h"
[email protected]bdae9812011-10-15 00:33:0344#include "content/common/child_process_messages.h"
[email protected]89c36f82013-07-17 06:23:2745#include "content/common/content_constants_internal.h"
[email protected]37666cf2011-03-13 21:51:4246#include "content/common/database_messages.h"
[email protected]5f2aa722013-08-07 16:59:4147#include "content/common/dom_storage/dom_storage_messages.h"
[email protected]c29b7ff2013-03-06 03:51:0448#include "content/common/gpu/client/context_provider_command_buffer.h"
[email protected]c26ad882012-02-07 06:41:2049#include "content/common/gpu/client/gpu_channel_host.h"
[email protected]bc10f462013-10-22 23:13:0950#include "content/common/gpu/client/gpu_memory_buffer_impl.h"
[email protected]202b54ff2011-04-22 21:36:3851#include "content/common/gpu/gpu_messages.h"
[email protected]96ab016c2013-10-23 00:50:2952#include "content/common/gpu/gpu_process_launch_causes.h"
[email protected]94dc971d2011-03-05 19:08:3253#include "content/common/resource_messages.h"
[email protected]778574e2011-03-21 22:03:5054#include "content/common/view_messages.h"
[email protected]a458504b2012-07-23 19:57:0655#include "content/public/common/content_constants.h"
[email protected]433df472012-03-07 20:33:3956#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2957#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3158#include "content/public/common/renderer_preferences.h"
[email protected]58436a12012-03-21 17:10:2659#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3460#include "content/public/renderer/content_renderer_client.h"
[email protected]64ffa0442011-10-03 22:08:3661#include "content/public/renderer/render_process_observer.h"
62#include "content/public/renderer/render_view_visitor.h"
[email protected]70019152012-12-19 11:44:1963#include "content/renderer/devtools/devtools_agent_filter.h"
[email protected]1910fe82012-05-10 00:04:1064#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
65#include "content/renderer/dom_storage/webstoragearea_impl.h"
66#include "content/renderer/dom_storage/webstoragenamespace_impl.h"
[email protected]ca998e5e2013-08-06 14:59:0967#include "content/renderer/gamepad_shared_memory_reader.h"
[email protected]1842fe22012-08-13 23:24:3568#include "content/renderer/gpu/compositor_output_surface.h"
[email protected]e54ab492012-06-12 19:40:0169#include "content/renderer/gpu/gpu_benchmarking_extension.h"
[email protected]7a72d452013-12-13 10:01:1370#include "content/renderer/input/input_event_filter.h"
71#include "content/renderer/input/input_handler_manager.h"
[email protected]f7eb0a392011-07-12 10:19:5172#include "content/renderer/media/audio_input_message_filter.h"
73#include "content/renderer/media/audio_message_filter.h"
[email protected]3958e972012-07-17 00:25:4174#include "content/renderer/media/audio_renderer_mixer_manager.h"
[email protected]d8cd8372012-03-09 10:49:5175#include "content/renderer/media/media_stream_center.h"
[email protected]6ee10bd2012-09-13 09:01:5376#include "content/renderer/media/media_stream_dependency_factory.h"
[email protected]a9875152013-06-22 04:03:0377#include "content/renderer/media/midi_message_filter.h"
[email protected]af089972013-01-10 04:04:4078#include "content/renderer/media/peer_connection_tracker.h"
[email protected]80b161a2011-06-27 17:42:1179#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:3480#include "content/renderer/media/video_capture_message_filter.h"
[email protected]921480f62013-07-20 03:42:5781#include "content/renderer/media/webrtc_identity_service.h"
[email protected]6941ac5c2013-02-07 03:59:5682#include "content/renderer/memory_benchmarking_extension.h"
[email protected]4761cf12012-09-12 10:37:5583#include "content/renderer/p2p/socket_dispatcher.h"
[email protected]8704f89b2011-04-15 00:30:0584#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:2885#include "content/renderer/render_view_impl.h"
[email protected]8d6cba42011-09-02 10:05:1986#include "content/renderer/renderer_webkitplatformsupport_impl.h"
[email protected]ddbb53342014-01-06 10:59:4787#include "content/renderer/service_worker/embedded_worker_context_message_filter.h"
[email protected]7670d4332013-11-29 04:35:0088#include "content/renderer/service_worker/embedded_worker_dispatcher.h"
[email protected]a8228252013-05-14 11:10:0589#include "content/renderer/skia_benchmarking_extension.h"
[email protected]766a7082012-02-03 23:39:1590#include "grit/content_resources.h"
[email protected]46f36a492010-07-28 19:36:4191#include "ipc/ipc_channel_handle.h"
[email protected]1842fe22012-08-13 23:24:3592#include "ipc/ipc_forwarding_message_filter.h"
[email protected]cb6037d2009-11-16 22:55:1793#include "ipc/ipc_platform_file.h"
[email protected]563d665f2013-02-01 04:42:3994#include "media/base/audio_hardware_config.h"
[email protected]433df472012-03-07 20:33:3995#include "media/base/media.h"
[email protected]1cad8802013-08-13 16:54:3296#include "media/filters/gpu_video_accelerator_factories.h"
[email protected]620161e2011-03-07 18:05:2697#include "net/base/net_errors.h"
[email protected]9d797f32010-04-23 07:17:5498#include "net/base/net_util.h"
[email protected]89c36f82013-07-17 06:23:2799#include "third_party/WebKit/public/platform/WebString.h"
[email protected]2255a9332013-06-17 05:12:31100#include "third_party/WebKit/public/web/WebColorName.h"
101#include "third_party/WebKit/public/web/WebDatabase.h"
102#include "third_party/WebKit/public/web/WebDocument.h"
103#include "third_party/WebKit/public/web/WebFrame.h"
[email protected]3311c542013-07-24 12:24:03104#include "third_party/WebKit/public/web/WebImageCache.h"
[email protected]2255a9332013-06-17 05:12:31105#include "third_party/WebKit/public/web/WebKit.h"
106#include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"
107#include "third_party/WebKit/public/web/WebPopupMenu.h"
108#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
109#include "third_party/WebKit/public/web/WebScriptController.h"
110#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
[email protected]2255a9332013-06-17 05:12:31111#include "third_party/WebKit/public/web/WebView.h"
[email protected]ddbb53342014-01-06 10:59:47112#include "third_party/skia/include/core/SkGraphics.h"
[email protected]c49201a2012-05-24 11:04:57113#include "ui/base/layout.h"
[email protected]18ad6772011-09-20 21:51:32114#include "ui/base/ui_base_switches.h"
[email protected]7f3a2cf2011-04-06 00:10:50115#include "v8/include/v8.h"
[email protected]a9bd323d2013-06-17 20:27:56116#include "webkit/child/worker_task_runner.h"
[email protected]2541d1a2013-07-10 07:33:27117#include "webkit/renderer/compositor_bindings/web_external_bitmap_impl.h"
[email protected]2c62b562009-01-27 19:04:50118
[email protected]da00a2882009-03-09 17:51:19119#if defined(OS_WIN)
120#include <windows.h>
121#include <objbase.h>
[email protected]5f7e4512012-10-01 20:51:37122#else
123// TODO(port)
124#include "base/memory/scoped_handle.h"
[email protected]defa597a2013-07-19 02:22:13125#include "content/child/npapi/np_channel_base.h"
[email protected]da00a2882009-03-09 17:51:19126#endif
127
[email protected]b0988b22013-11-08 08:25:58128#if defined(OS_MACOSX)
129#include "third_party/WebKit/public/web/mac/WebScrollbarTheme.h"
130#endif
131
[email protected]6217d392010-03-25 22:08:35132#if defined(OS_POSIX)
133#include "ipc/ipc_channel_posix.h"
134#endif
135
[email protected]913d99a2013-05-31 07:16:07136#if defined(OS_ANDROID)
[email protected]9a1af192013-06-06 03:09:59137#include <cpu-features.h>
[email protected]913d99a2013-05-31 07:16:07138#include "content/renderer/android/synchronous_compositor_factory.h"
[email protected]6ca6a082013-09-22 20:10:05139#include "content/renderer/media/android/renderer_demuxer_android.h"
[email protected]913d99a2013-05-31 07:16:07140#endif
141
[email protected]a80af12e2013-08-07 23:36:13142#if defined(ENABLE_PLUGINS)
143#include "content/renderer/npapi/plugin_channel_host.h"
144#endif
145
[email protected]d293572a2013-05-23 18:50:45146using base::ThreadRestrictions;
[email protected]180ef242013-11-07 06:50:46147using blink::WebDocument;
148using blink::WebFrame;
149using blink::WebNetworkStateNotifier;
150using blink::WebRuntimeFeatures;
151using blink::WebScriptController;
152using blink::WebSecurityPolicy;
153using blink::WebString;
154using blink::WebView;
[email protected]e9ff79c2012-10-19 21:31:26155
156namespace content {
initial.commit09911bf2008-07-26 23:55:29157
[email protected]42f1d7822009-07-23 18:17:55158namespace {
[email protected]da9ccfb2012-01-28 00:34:40159
160const int64 kInitialIdleHandlerDelayMs = 1000;
161const int64 kShortIdleHandlerDelayMs = 1000;
162const int64 kLongIdleHandlerDelayMs = 30*1000;
163const int kIdleCPUUsageThresholdInPercents = 3;
[email protected]1e1d1e12014-01-17 16:14:29164const int kMinRasterThreads = 1;
165const int kMaxRasterThreads = 64;
[email protected]bee16aab2009-08-26 15:55:03166
[email protected]f1a29a02011-10-06 23:08:44167// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16168// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40169base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49170 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54171
[email protected]e9ff79c2012-10-19 21:31:26172class RenderViewZoomer : public RenderViewVisitor {
[email protected]40bd6582009-12-04 23:49:51173 public:
[email protected]367c5c1d2013-03-11 18:59:02174 RenderViewZoomer(const std::string& scheme,
175 const std::string& host,
176 double zoom_level) : scheme_(scheme),
177 host_(host),
178 zoom_level_(zoom_level) {
[email protected]40bd6582009-12-04 23:49:51179 }
180
[email protected]c3e35892013-02-12 02:08:01181 virtual bool Visit(RenderView* render_view) OVERRIDE {
[email protected]a2ef54c2011-10-10 16:20:31182 WebView* webview = render_view->GetWebView();
[email protected]b6cb3a842011-06-24 18:28:41183 WebDocument document = webview->mainFrame()->document();
[email protected]b75b8292010-10-01 07:28:25184
185 // Don't set zoom level for full-page plugin since they don't use the same
186 // zoom settings.
[email protected]b6cb3a842011-06-24 18:28:41187 if (document.isPluginDocument())
[email protected]b75b8292010-10-01 07:28:25188 return true;
[email protected]367c5c1d2013-03-11 18:59:02189 GURL url(document.url());
190 // Empty scheme works as wildcard that matches any scheme,
191 if ((net::GetHostOrSpecFromURL(url) == host_) &&
192 (scheme_.empty() || scheme_ == url.scheme())) {
[email protected]91219702013-09-18 07:33:51193 webview->setZoomLevel(zoom_level_);
[email protected]367c5c1d2013-03-11 18:59:02194 }
[email protected]40bd6582009-12-04 23:49:51195 return true;
196 }
197
198 private:
[email protected]367c5c1d2013-03-11 18:59:02199 const std::string scheme_;
200 const std::string host_;
201 const double zoom_level_;
[email protected]40bd6582009-12-04 23:49:51202
203 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
204};
[email protected]0478d0162010-08-28 08:29:40205
[email protected]b2d98762012-09-03 17:04:06206std::string HostToCustomHistogramSuffix(const std::string& host) {
207 if (host == "mail.google.com")
208 return ".gmail";
209 if (host == "docs.google.com" || host == "drive.google.com")
210 return ".docs";
211 if (host == "plus.google.com")
212 return ".plus";
[email protected]007b3f82013-04-09 08:46:45213 return std::string();
[email protected]b2d98762012-09-03 17:04:06214}
[email protected]42f1d7822009-07-23 18:17:55215
[email protected]b2d98762012-09-03 17:04:06216void* CreateHistogram(
[email protected]d1b8fccc2011-08-03 01:20:13217 const char *name, int min, int max, size_t buckets) {
218 if (min <= 0)
219 min = 1;
[email protected]b2d98762012-09-03 17:04:06220 std::string histogram_name;
221 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
222 if (render_thread_impl) { // Can be null in tests.
223 histogram_name = render_thread_impl->
224 histogram_customizer()->ConvertToCustomHistogramName(name);
225 } else {
226 histogram_name = std::string(name);
227 }
[email protected]de415552013-01-23 04:12:17228 base::HistogramBase* histogram = base::Histogram::FactoryGet(
[email protected]b2d98762012-09-03 17:04:06229 histogram_name, min, max, buckets,
230 base::Histogram::kUmaTargetedHistogramFlag);
[email protected]d1b8fccc2011-08-03 01:20:13231 return histogram;
232}
233
[email protected]b2d98762012-09-03 17:04:06234void AddHistogramSample(void* hist, int sample) {
[email protected]d1b8fccc2011-08-03 01:20:13235 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
236 histogram->Add(sample);
237}
238
[email protected]2541d1a2013-07-10 07:33:27239scoped_ptr<base::SharedMemory> AllocateSharedMemoryFunction(size_t size) {
240 return RenderThreadImpl::Get()->HostAllocateSharedMemoryBuffer(size);
241}
242
[email protected]6bd867b2013-07-24 22:10:20243void EnableWebCoreLogChannels(const std::string& channels) {
244 if (channels.empty())
245 return;
246 base::StringTokenizer t(channels, ", ");
247 while (t.GetNext())
[email protected]180ef242013-11-07 06:50:46248 blink::enableLogChannel(t.token().c_str());
[email protected]6bd867b2013-07-24 22:10:20249}
250
[email protected]b2d98762012-09-03 17:04:06251} // namespace
252
[email protected]b2d98762012-09-03 17:04:06253RenderThreadImpl::HistogramCustomizer::HistogramCustomizer() {
254 custom_histograms_.insert("V8.MemoryExternalFragmentationTotal");
255 custom_histograms_.insert("V8.MemoryHeapSampleTotalCommitted");
256 custom_histograms_.insert("V8.MemoryHeapSampleTotalUsed");
257}
258
259RenderThreadImpl::HistogramCustomizer::~HistogramCustomizer() {}
260
261void RenderThreadImpl::HistogramCustomizer::RenderViewNavigatedToHost(
262 const std::string& host, size_t view_count) {
[email protected]9baee832012-12-10 11:07:15263 if (CommandLine::ForCurrentProcess()->HasSwitch(
264 switches::kDisableHistogramCustomizer)) {
265 return;
266 }
[email protected]b2d98762012-09-03 17:04:06267 // Check if all RenderViews are displaying a page from the same host. If there
268 // is only one RenderView, the common host is this view's host. If there are
269 // many, check if this one shares the common host of the other
270 // RenderViews. It's ok to not detect some cases where the RenderViews share a
271 // common host. This information is only used for producing custom histograms.
272 if (view_count == 1)
273 SetCommonHost(host);
274 else if (host != common_host_)
275 SetCommonHost(std::string());
276}
277
278std::string RenderThreadImpl::HistogramCustomizer::ConvertToCustomHistogramName(
279 const char* histogram_name) const {
280 std::string name(histogram_name);
281 if (!common_host_histogram_suffix_.empty() &&
282 custom_histograms_.find(name) != custom_histograms_.end())
283 name += common_host_histogram_suffix_;
284 return name;
285}
286
287void RenderThreadImpl::HistogramCustomizer::SetCommonHost(
288 const std::string& host) {
289 if (host != common_host_) {
290 common_host_ = host;
291 common_host_histogram_suffix_ = HostToCustomHistogramSuffix(host);
292 v8::V8::SetCreateHistogramFunction(CreateHistogram);
293 }
294}
295
[email protected]f1a29a02011-10-06 23:08:44296RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06297 return lazy_tls.Pointer()->Get();
298}
299
[email protected]42f1d7822009-07-23 18:17:55300// When we run plugins in process, we actually run them on the render thread,
301// which means that we need to make the render thread pump UI events.
[email protected]f1a29a02011-10-06 23:08:44302RenderThreadImpl::RenderThreadImpl() {
[email protected]42f1d7822009-07-23 18:17:55303 Init();
304}
305
[email protected]f1a29a02011-10-06 23:08:44306RenderThreadImpl::RenderThreadImpl(const std::string& channel_name)
[email protected]42f1d7822009-07-23 18:17:55307 : ChildThread(channel_name) {
308 Init();
309}
[email protected]5fa1c542009-05-05 20:36:07310
[email protected]f1a29a02011-10-06 23:08:44311void RenderThreadImpl::Init() {
312 TRACE_EVENT_BEGIN_ETW("RenderThreadImpl::Init", 0, "");
[email protected]a872ea1f2010-08-11 04:45:33313
[email protected]91a2aea2013-07-08 23:14:39314 base::debug::TraceLog::GetInstance()->SetThreadSortIndex(
315 base::PlatformThread::CurrentId(),
316 kTraceEventRendererMainThreadSortIndex);
317
[email protected]c7e948b42012-07-12 09:20:20318 v8::V8::SetCounterFunction(base::StatsTable::FindLocation);
319 v8::V8::SetCreateHistogramFunction(CreateHistogram);
320 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
321
[email protected]d3f09092012-02-16 01:52:17322#if defined(OS_MACOSX) || defined(OS_ANDROID)
323 // On Mac and Android, the select popups are rendered by the browser.
[email protected]180ef242013-11-07 06:50:46324 blink::WebView::setUseExternalPopupMenus(true);
[email protected]53c607c2011-03-21 23:19:04325#endif
326
[email protected]94f9a0f682009-06-15 18:30:30327 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28328
[email protected]06c694d2012-02-01 22:26:16329 // Register this object as the main thread.
330 ChildProcess::current()->set_main_thread(this);
331
[email protected]31f87132010-04-21 23:36:21332 // In single process the single process is all there is.
[email protected]80fc08c52010-03-09 07:43:50333 suspend_webkit_shared_timer_ = true;
334 notify_webkit_of_modal_loop_ = true;
[email protected]bee16aab2009-08-26 15:55:03335 widget_count_ = 0;
336 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44337 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48338 idle_notifications_to_skip_ = 0;
[email protected]70dee7e2013-05-29 18:28:30339 layout_test_mode_ = false;
[email protected]57cf97882013-06-26 20:39:01340 shutdown_event_ = NULL;
[email protected]8d86fce2009-02-26 23:37:55341
[email protected]d5b2fdf2013-06-05 09:36:55342 appcache_dispatcher_.reset(
[email protected]a8cb3b73b2013-08-12 05:50:50343 new AppCacheDispatcher(Get(), new AppCacheFrontendImpl()));
[email protected]1910fe82012-05-10 00:04:10344 dom_storage_dispatcher_.reset(new DomStorageDispatcher());
[email protected]1e8d91e2013-06-27 00:12:43345 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher(
346 thread_safe_sender()));
[email protected]7670d4332013-11-29 04:35:00347 embedded_worker_dispatcher_.reset(new EmbeddedWorkerDispatcher());
[email protected]dd9241932010-02-24 19:23:13348
[email protected]d8cd8372012-03-09 10:49:51349 media_stream_center_ = NULL;
350
[email protected]017022b2009-07-27 23:06:34351 db_message_filter_ = new DBMessageFilter();
352 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13353
[email protected]45048072014-01-14 13:51:29354 vc_manager_.reset(new VideoCaptureImplManager());
[email protected]c9c43a02013-12-17 08:59:54355 AddFilter(vc_manager_->video_capture_message_filter());
356
[email protected]4761cf12012-09-12 10:37:55357#if defined(ENABLE_WEBRTC)
[email protected]af089972013-01-10 04:04:40358 peer_connection_tracker_.reset(new PeerConnectionTracker());
359 AddObserver(peer_connection_tracker_.get());
360
[email protected]cadac622013-06-11 16:46:36361 p2p_socket_dispatcher_ =
362 new P2PSocketDispatcher(GetIOMessageLoopProxy().get());
[email protected]fc72bb12013-06-02 21:13:46363 AddFilter(p2p_socket_dispatcher_.get());
[email protected]921480f62013-07-20 03:42:57364
365 webrtc_identity_service_.reset(new WebRTCIdentityService());
[email protected]c9c43a02013-12-17 08:59:54366
367 media_stream_factory_.reset(new MediaStreamDependencyFactory(
[email protected]45048072014-01-14 13:51:29368 p2p_socket_dispatcher_.get()));
[email protected]c9c43a02013-12-17 08:59:54369 AddObserver(media_stream_factory_.get());
[email protected]4761cf12012-09-12 10:37:55370#endif // defined(ENABLE_WEBRTC)
[email protected]e25f4d72011-06-08 20:58:46371
[email protected]e0a938702013-02-25 21:15:56372 audio_input_message_filter_ =
373 new AudioInputMessageFilter(GetIOMessageLoopProxy());
[email protected]f7eb0a392011-07-12 10:19:51374 AddFilter(audio_input_message_filter_.get());
375
[email protected]e0a938702013-02-25 21:15:56376 audio_message_filter_ = new AudioMessageFilter(GetIOMessageLoopProxy());
[email protected]f7eb0a392011-07-12 10:19:51377 AddFilter(audio_message_filter_.get());
378
[email protected]a9875152013-06-22 04:03:03379 midi_message_filter_ = new MIDIMessageFilter(GetIOMessageLoopProxy());
380 AddFilter(midi_message_filter_.get());
381
[email protected]ce944c62013-11-19 16:34:36382 AddFilter((new IndexedDBMessageFilter(thread_safe_sender()))->GetFilter());
[email protected]31bfae72011-12-16 02:04:38383
[email protected]ddbb53342014-01-06 10:59:47384 AddFilter((new EmbeddedWorkerContextMessageFilter())->GetFilter());
385
[email protected]e9ff79c2012-10-19 21:31:26386 GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24387
[email protected]302fe422012-06-11 14:49:11388 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]e54ab492012-06-12 19:40:01389 if (command_line.HasSwitch(switches::kEnableGpuBenchmarking))
[email protected]e9ff79c2012-10-19 21:31:26390 RegisterExtension(GpuBenchmarkingExtension::Get());
[email protected]e54ab492012-06-12 19:40:01391
[email protected]a70ed8b32013-05-24 15:16:34392#if defined(USE_TCMALLOC) && (defined(OS_LINUX) || defined(OS_ANDROID))
[email protected]6941ac5c2013-02-07 03:59:56393 if (command_line.HasSwitch(switches::kEnableMemoryBenchmarking))
394 RegisterExtension(MemoryBenchmarkingExtension::Get());
[email protected]a70ed8b32013-05-24 15:16:34395#endif // USE_TCMALLOC
[email protected]6941ac5c2013-02-07 03:59:56396
[email protected]a8228252013-05-14 11:10:05397 if (command_line.HasSwitch(switches::kEnableSkiaBenchmarking)) {
398 LOG(WARNING) << "Enabling unsafe Skia benchmarking extension.";
399 RegisterExtension(SkiaBenchmarkingExtension::Get());
400 }
401
[email protected]433df472012-03-07 20:33:39402 // Note that under Linux, the media library will normally already have
403 // been initialized by the Zygote before this instance became a Renderer.
[email protected]c42de732013-02-16 06:26:31404 base::FilePath media_path;
[email protected]e9ff79c2012-10-19 21:31:26405 PathService::Get(DIR_MEDIA_LIBS, &media_path);
[email protected]433df472012-03-07 20:33:39406 if (!media_path.empty())
407 media::InitializeMediaLibrary(media_path);
408
[email protected]5071cb22013-07-10 02:19:06409 memory_pressure_listener_.reset(new base::MemoryPressureListener(
410 base::Bind(&RenderThreadImpl::OnMemoryPressure, base::Unretained(this))));
411
[email protected]7d08a9352013-10-15 08:24:56412 renderer_process_id_ = base::kNullProcessId;
413
[email protected]ed1cca4f2014-01-08 12:27:37414 std::vector<base::DiscardableMemoryType> supported_types;
415 base::DiscardableMemory::GetSupportedTypes(&supported_types);
416 DCHECK(!supported_types.empty());
417
418 // The default preferred type is always the first one in list.
419 base::DiscardableMemoryType type = supported_types[0];
420
421 if (command_line.HasSwitch(switches::kUseDiscardableMemory)) {
422 std::string requested_type_name = command_line.GetSwitchValueASCII(
423 switches::kUseDiscardableMemory);
424 base::DiscardableMemoryType requested_type =
425 base::DiscardableMemory::GetNamedType(requested_type_name);
426 if (std::find(supported_types.begin(),
427 supported_types.end(),
428 requested_type) != supported_types.end()) {
429 type = requested_type;
430 } else {
431 LOG(ERROR) << "Requested discardable memory type is not supported.";
432 }
433 }
434
435 base::DiscardableMemory::SetPreferredType(type);
436
[email protected]e185db72014-01-14 03:09:22437 // Allow discardable memory implementations to register memory pressure
438 // listeners.
439 base::DiscardableMemory::RegisterMemoryPressureListeners();
440
[email protected]88105162013-12-05 01:02:12441 // AllocateGpuMemoryBuffer must be used exclusively on one thread but
442 // it doesn't have to be the same thread RenderThreadImpl is created on.
443 allocate_gpu_memory_buffer_thread_checker_.DetachFromThread();
444
[email protected]1e1d1e12014-01-17 16:14:29445 if (command_line.HasSwitch(cc::switches::kNumRasterThreads)) {
446 int num_raster_threads;
447 std::string string_value =
448 command_line.GetSwitchValueASCII(cc::switches::kNumRasterThreads);
449 if (base::StringToInt(string_value, &num_raster_threads) &&
450 num_raster_threads >= kMinRasterThreads &&
451 num_raster_threads <= kMaxRasterThreads) {
452 cc::WorkerPool::SetNumRasterThreads(num_raster_threads);
453 } else {
454 LOG(WARNING) << "Failed to parse switch " <<
455 cc::switches::kNumRasterThreads << ": " << string_value;
456 }
457 }
458
[email protected]f1a29a02011-10-06 23:08:44459 TRACE_EVENT_END_ETW("RenderThreadImpl::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29460}
461
[email protected]f1a29a02011-10-06 23:08:44462RenderThreadImpl::~RenderThreadImpl() {
[email protected]ce79d8512013-04-22 22:44:41463}
464
465void RenderThreadImpl::Shutdown() {
[email protected]1223d6ef2011-03-28 16:47:50466 FOR_EACH_OBSERVER(
467 RenderProcessObserver, observers_, OnRenderProcessShutdown());
468
[email protected]0e7015f2013-07-23 21:51:11469 ChildThread::Shutdown();
470
[email protected]12cbfda32010-01-30 01:04:25471 // Wait for all databases to be closed.
[email protected]1406f892013-11-12 19:38:19472 if (webkit_platform_support_) {
473 webkit_platform_support_->web_database_observer_impl()->
474 WaitForAllDatabasesToClose();
475 }
[email protected]12cbfda32010-01-30 01:04:25476
[email protected]8d86fce2009-02-26 23:37:55477 // Shutdown in reverse of the initialization order.
[email protected]fc72bb12013-06-02 21:13:46478 if (devtools_agent_message_filter_.get()) {
[email protected]b146d6d2012-09-11 10:20:05479 RemoveFilter(devtools_agent_message_filter_.get());
480 devtools_agent_message_filter_ = NULL;
481 }
[email protected]82622452011-07-22 09:57:20482
[email protected]f7eb0a392011-07-12 10:19:51483 RemoveFilter(audio_input_message_filter_.get());
484 audio_input_message_filter_ = NULL;
485
486 RemoveFilter(audio_message_filter_.get());
487 audio_message_filter_ = NULL;
488
[email protected]45048072014-01-14 13:51:29489 // |media_stream_factory_| produces users of |vc_manager_| so it must be
490 // destroyed first.
491#if defined(ENABLE_WEBRTC)
492 media_stream_factory_.reset();
493#endif
[email protected]80b161a2011-06-27 17:42:11494 RemoveFilter(vc_manager_->video_capture_message_filter());
[email protected]45048072014-01-14 13:51:29495 vc_manager_.reset();
[email protected]80b161a2011-06-27 17:42:11496
[email protected]017022b2009-07-27 23:06:34497 RemoveFilter(db_message_filter_.get());
498 db_message_filter_ = NULL;
[email protected]12cbfda32010-01-30 01:04:25499
[email protected]c6a7b862010-08-20 22:19:38500 // Shutdown the file thread if it's running.
[email protected]59383c782013-04-17 16:43:27501 if (file_thread_)
[email protected]c6a7b862010-08-20 22:19:38502 file_thread_->Stop();
503
[email protected]fc72bb12013-06-02 21:13:46504 if (compositor_output_surface_filter_.get()) {
[email protected]23d2b7e2012-08-14 00:34:09505 RemoveFilter(compositor_output_surface_filter_.get());
506 compositor_output_surface_filter_ = NULL;
507 }
[email protected]1842fe22012-08-13 23:24:35508
[email protected]79a92b52013-04-08 19:09:02509 compositor_thread_.reset();
[email protected]b0255682013-06-07 21:24:08510 input_handler_manager_.reset();
[email protected]cadac622013-06-11 16:46:36511 if (input_event_filter_.get()) {
[email protected]b0255682013-06-07 21:24:08512 RemoveFilter(input_event_filter_.get());
513 input_event_filter_ = NULL;
[email protected]a9fb30aa2011-10-06 06:58:46514 }
515
[email protected]c5739b9e2013-11-07 06:30:22516 // Ramp down IDB before we ramp down WebKit (and V8), since IDB classes might
517 // hold pointers to V8 objects (e.g., via pending requests).
518 main_thread_indexed_db_dispatcher_.reset();
519
[email protected]59383c782013-04-17 16:43:27520 if (webkit_platform_support_)
[email protected]180ef242013-11-07 06:50:46521 blink::shutdown();
[email protected]9291ed12009-07-23 17:33:22522
[email protected]94f9a0f682009-06-15 18:30:30523 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30524
[email protected]8d86fce2009-02-26 23:37:55525 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50526#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29527 // Clean up plugin channels before this thread goes away.
[email protected]099587b72011-09-20 00:40:50528 NPChannelBase::CleanupChannels();
[email protected]2c62b562009-01-27 19:04:50529#endif
[email protected]00874dd2013-03-08 23:51:18530
531 // Leak shared contexts on other threads, as we can not get to the correct
532 // thread to destroy them.
[email protected]f10dc472013-09-27 03:31:59533 if (offscreen_compositor_contexts_.get())
534 offscreen_compositor_contexts_->set_leak_on_destroy();
initial.commit09911bf2008-07-26 23:55:29535}
536
[email protected]f1a29a02011-10-06 23:08:44537bool RenderThreadImpl::Send(IPC::Message* msg) {
[email protected]f23d4da92010-11-24 21:36:14538 // Certain synchronous messages cannot always be processed synchronously by
[email protected]af700462013-12-16 23:53:10539 // the browser, e.g., putting up UI and waiting for the user. This could cause
540 // a complete hang of Chrome if a windowed plug-in is trying to communicate
541 // with the renderer thread since the browser's UI thread could be stuck
542 // (within a Windows API call) trying to synchronously communicate with the
543 // plug-in. The remedy is to pump messages on this thread while the browser
544 // is processing this request. This creates an opportunity for re-entrancy
545 // into WebKit, so we need to take care to disable callbacks, timers, and
546 // pending network loads that could trigger such callbacks.
[email protected]38b592902011-04-16 02:08:42547 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:50548 if (msg->is_sync()) {
549 if (msg->is_caller_pumping_messages()) {
550 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:50551 }
[email protected]c1f50aa2010-02-18 03:46:57552 }
553
[email protected]80fc08c52010-03-09 07:43:50554 bool suspend_webkit_shared_timer = true; // default value
555 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57556
[email protected]80fc08c52010-03-09 07:43:50557 bool notify_webkit_of_modal_loop = true; // default value
558 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
559
[email protected]a80af12e2013-08-07 23:36:13560#if defined(ENABLE_PLUGINS)
[email protected]c48de1a2012-11-02 19:06:56561 int render_view_id = MSG_ROUTING_NONE;
[email protected]a80af12e2013-08-07 23:36:13562#endif
[email protected]c1f50aa2010-02-18 03:46:57563
564 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50565 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19566 webkit_platform_support_->SuspendSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57567
[email protected]39065d012010-07-09 11:22:46568 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57569 WebView::willEnterModalLoop();
[email protected]a80af12e2013-08-07 23:36:13570#if defined(ENABLE_PLUGINS)
[email protected]bae061f72013-04-26 16:22:29571 RenderViewImpl* render_view =
572 RenderViewImpl::FromRoutingID(msg->routing_id());
573 if (render_view) {
574 render_view_id = msg->routing_id();
[email protected]c1f50aa2010-02-18 03:46:57575 PluginChannelHost::Broadcast(
[email protected]c48de1a2012-11-02 19:06:56576 new PluginMsg_SignalModalDialogEvent(render_view_id));
[email protected]c1f50aa2010-02-18 03:46:57577 }
[email protected]a80af12e2013-08-07 23:36:13578#endif
[email protected]c1f50aa2010-02-18 03:46:57579 }
580
581 bool rv = ChildThread::Send(msg);
582
583 if (pumping_events) {
[email protected]a80af12e2013-08-07 23:36:13584#if defined(ENABLE_PLUGINS)
[email protected]c48de1a2012-11-02 19:06:56585 if (render_view_id != MSG_ROUTING_NONE) {
[email protected]c1f50aa2010-02-18 03:46:57586 PluginChannelHost::Broadcast(
[email protected]c48de1a2012-11-02 19:06:56587 new PluginMsg_ResetModalDialogEvent(render_view_id));
[email protected]c1f50aa2010-02-18 03:46:57588 }
[email protected]a80af12e2013-08-07 23:36:13589#endif
[email protected]c1f50aa2010-02-18 03:46:57590
[email protected]39065d012010-07-09 11:22:46591 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57592 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57593
[email protected]80fc08c52010-03-09 07:43:50594 if (suspend_webkit_shared_timer)
[email protected]8d6cba42011-09-02 10:05:19595 webkit_platform_support_->ResumeSharedTimer();
[email protected]c1f50aa2010-02-18 03:46:57596 }
597
598 return rv;
599}
600
[email protected]9e7154122013-05-30 23:11:04601base::MessageLoop* RenderThreadImpl::GetMessageLoop() {
[email protected]526476902011-10-06 20:34:06602 return message_loop();
603}
604
[email protected]f1a29a02011-10-06 23:08:44605IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:06606 return channel();
607}
608
[email protected]f1a29a02011-10-06 23:08:44609std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:06610 // The browser process should have passed the locale to the renderer via the
[email protected]dfd53652012-10-25 00:20:02611 // --lang command line flag.
[email protected]526476902011-10-06 20:34:06612 const CommandLine& parsed_command_line = *CommandLine::ForCurrentProcess();
613 const std::string& lang =
614 parsed_command_line.GetSwitchValueASCII(switches::kLang);
[email protected]dfd53652012-10-25 00:20:02615 DCHECK(!lang.empty());
[email protected]526476902011-10-06 20:34:06616 return lang;
617}
618
[email protected]07bb6332012-01-21 01:07:57619IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
620 return sync_message_filter();
621}
622
[email protected]96191d52012-05-17 01:37:11623scoped_refptr<base::MessageLoopProxy>
624 RenderThreadImpl::GetIOMessageLoopProxy() {
625 return ChildProcess::current()->io_message_loop_proxy();
626}
627
[email protected]c47317e2012-06-20 22:35:31628void RenderThreadImpl::AddRoute(int32 routing_id, IPC::Listener* listener) {
[email protected]b2db9272014-01-10 17:42:00629 ChildThread::AddRoute(routing_id, listener);
[email protected]c1f50aa2010-02-18 03:46:57630}
631
[email protected]f1a29a02011-10-06 23:08:44632void RenderThreadImpl::RemoveRoute(int32 routing_id) {
[email protected]b2db9272014-01-10 17:42:00633 ChildThread::RemoveRoute(routing_id);
[email protected]c1f50aa2010-02-18 03:46:57634}
[email protected]77fc9b92011-10-15 16:20:37635int RenderThreadImpl::GenerateRoutingID() {
636 int routing_id = MSG_ROUTING_NONE;
637 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
638 return routing_id;
639}
640
[email protected]f1a29a02011-10-06 23:08:44641void RenderThreadImpl::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55642 channel()->AddFilter(filter);
643}
644
[email protected]f1a29a02011-10-06 23:08:44645void RenderThreadImpl::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:55646 channel()->RemoveFilter(filter);
647}
648
[email protected]e9ff79c2012-10-19 21:31:26649void RenderThreadImpl::AddObserver(RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06650 observers_.AddObserver(observer);
651}
652
[email protected]e9ff79c2012-10-19 21:31:26653void RenderThreadImpl::RemoveObserver(RenderProcessObserver* observer) {
[email protected]526476902011-10-06 20:34:06654 observers_.RemoveObserver(observer);
655}
656
[email protected]359dfa32011-10-12 01:10:15657void RenderThreadImpl::SetResourceDispatcherDelegate(
[email protected]e9ff79c2012-10-19 21:31:26658 ResourceDispatcherDelegate* delegate) {
[email protected]359dfa32011-10-12 01:10:15659 resource_dispatcher()->set_delegate(delegate);
660}
661
[email protected]f1a29a02011-10-06 23:08:44662void RenderThreadImpl::EnsureWebKitInitialized() {
[email protected]59383c782013-04-17 16:43:27663 if (webkit_platform_support_)
[email protected]d1b8fccc2011-08-03 01:20:13664 return;
665
[email protected]8d6cba42011-09-02 10:05:19666 webkit_platform_support_.reset(new RendererWebKitPlatformSupportImpl);
[email protected]180ef242013-11-07 06:50:46667 blink::initialize(webkit_platform_support_.get());
[email protected]d1b8fccc2011-08-03 01:20:13668
[email protected]cd7ce3be2012-09-10 05:19:41669 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]cd7ce3be2012-09-10 05:19:41670
[email protected]0f21e8582013-01-11 11:06:56671 bool enable = command_line.HasSwitch(switches::kEnableThreadedCompositing);
[email protected]72787e392012-03-23 05:55:43672 if (enable) {
[email protected]913d99a2013-05-31 07:16:07673#if defined(OS_ANDROID)
674 if (SynchronousCompositorFactory* factory =
675 SynchronousCompositorFactory::GetInstance())
676 compositor_message_loop_proxy_ =
677 factory->GetCompositorMessageLoop();
678#endif
[email protected]cadac622013-06-11 16:46:36679 if (!compositor_message_loop_proxy_.get()) {
[email protected]ea5f70a2013-03-07 12:30:36680 compositor_thread_.reset(new base::Thread("Compositor"));
681 compositor_thread_->Start();
[email protected]b38864d2013-05-24 14:42:36682#if defined(OS_ANDROID)
683 compositor_thread_->SetPriority(base::kThreadPriority_Display);
684#endif
[email protected]ea5f70a2013-03-07 12:30:36685 compositor_message_loop_proxy_ =
686 compositor_thread_->message_loop_proxy();
[email protected]d293572a2013-05-23 18:50:45687 compositor_message_loop_proxy_->PostTask(
688 FROM_HERE,
689 base::Bind(base::IgnoreResult(&ThreadRestrictions::SetIOAllowed),
690 false));
[email protected]ea5f70a2013-03-07 12:30:36691 }
692
[email protected]b0255682013-06-07 21:24:08693 InputHandlerManagerClient* input_handler_manager_client = NULL;
694#if defined(OS_ANDROID)
695 if (SynchronousCompositorFactory* factory =
696 SynchronousCompositorFactory::GetInstance()) {
697 input_handler_manager_client = factory->GetInputHandlerManagerClient();
698 }
699#endif
700 if (!input_handler_manager_client) {
701 input_event_filter_ =
702 new InputEventFilter(this, compositor_message_loop_proxy_);
703 AddFilter(input_event_filter_.get());
704 input_handler_manager_client = input_event_filter_.get();
705 }
[email protected]913d99a2013-05-31 07:16:07706 input_handler_manager_.reset(
[email protected]b0255682013-06-07 21:24:08707 new InputHandlerManager(compositor_message_loop_proxy_,
708 input_handler_manager_client));
[email protected]a458504b2012-07-23 19:57:06709 }
[email protected]a9fb30aa2011-10-06 06:58:46710
[email protected]ea5f70a2013-03-07 12:30:36711 scoped_refptr<base::MessageLoopProxy> output_surface_loop;
[email protected]7f1f63f2013-03-07 06:07:29712 if (enable)
[email protected]ea5f70a2013-03-07 12:30:36713 output_surface_loop = compositor_message_loop_proxy_;
[email protected]7f1f63f2013-03-07 06:07:29714 else
715 output_surface_loop = base::MessageLoopProxy::current();
[email protected]1842fe22012-08-13 23:24:35716
[email protected]7f1f63f2013-03-07 06:07:29717 compositor_output_surface_filter_ =
[email protected]fc72bb12013-06-02 21:13:46718 CompositorOutputSurface::CreateFilter(output_surface_loop.get());
[email protected]1842fe22012-08-13 23:24:35719 AddFilter(compositor_output_surface_filter_.get());
720
[email protected]d1b8fccc2011-08-03 01:20:13721 WebScriptController::enableV8SingleThreadMode();
722
[email protected]58436a12012-03-21 17:10:26723 RenderThreadImpl::RegisterSchemes();
724
[email protected]6bd867b2013-07-24 22:10:20725 EnableWebCoreLogChannels(
[email protected]d1b8fccc2011-08-03 01:20:13726 command_line.GetSwitchValueASCII(switches::kWebCoreLogChannels));
727
[email protected]643255da2013-06-12 20:55:13728 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
[email protected]8027acd2013-04-18 08:35:15729
[email protected]78b86f9a2013-05-03 23:33:40730 if (!media::IsMediaLibraryInitialized()) {
731 WebRuntimeFeatures::enableMediaPlayer(false);
732 WebRuntimeFeatures::enableWebAudio(false);
733 }
[email protected]7a06d282013-05-03 04:39:33734
[email protected]d1b8fccc2011-08-03 01:20:13735 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, WebKitInitialized());
[email protected]1784b2f2011-11-24 10:53:48736
[email protected]b146d6d2012-09-11 10:20:05737 devtools_agent_message_filter_ = new DevToolsAgentFilter();
738 AddFilter(devtools_agent_message_filter_.get());
739
[email protected]e9ff79c2012-10-19 21:31:26740 if (GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
[email protected]1784b2f2011-11-24 10:53:48741 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]2541d1a2013-07-10 07:33:27742
743 webkit::SetSharedMemoryAllocationFunction(AllocateSharedMemoryFunction);
[email protected]d72493152014-01-08 17:37:45744
745 // Limit use of the scaled image cache to when deferred image decoding
746 // is enabled.
747 // TODO(reveman): Allow use of this cache on Android once
748 // SkDiscardablePixelRef is used for decoded images. crbug.com/330041
749 bool use_skia_scaled_image_cache = false;
750#if !defined(OS_ANDROID)
751 use_skia_scaled_image_cache =
752 command_line.HasSwitch(switches::kEnableDeferredImageDecoding) ||
753 cc::switches::IsImplSidePaintingEnabled();
754#endif
755 if (!use_skia_scaled_image_cache)
756 SkGraphics::SetImageCacheByteLimit(0u);
[email protected]d1b8fccc2011-08-03 01:20:13757}
758
[email protected]58436a12012-03-21 17:10:26759void RenderThreadImpl::RegisterSchemes() {
760 // swappedout: pages should not be accessible, and should also
761 // be treated as empty documents that can commit synchronously.
[email protected]32956122013-12-25 07:29:24762 WebString swappedout_scheme(base::ASCIIToUTF16(kSwappedOutScheme));
[email protected]58436a12012-03-21 17:10:26763 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(swappedout_scheme);
764 WebSecurityPolicy::registerURLSchemeAsEmptyDocument(swappedout_scheme);
765}
766
[email protected]e6e30ac2014-01-13 21:24:39767void RenderThreadImpl::RecordAction(const base::UserMetricsAction& action) {
[email protected]97880c82013-12-04 07:09:21768 Send(new ViewHostMsg_UserMetricsRecordAction(action.str_));
769}
770
771void RenderThreadImpl::RecordComputedAction(const std::string& action) {
[email protected]526476902011-10-06 20:34:06772 Send(new ViewHostMsg_UserMetricsRecordAction(action));
773}
774
[email protected]fb79aa22012-12-13 01:12:13775scoped_ptr<base::SharedMemory>
[email protected]3f892832013-01-11 03:23:59776 RenderThreadImpl::HostAllocateSharedMemoryBuffer(size_t size) {
777 if (size > static_cast<size_t>(std::numeric_limits<int>::max()))
778 return scoped_ptr<base::SharedMemory>();
779
[email protected]fb79aa22012-12-13 01:12:13780 base::SharedMemoryHandle handle;
781 bool success;
782 IPC::Message* message =
783 new ChildProcessHostMsg_SyncAllocateSharedMemory(size, &handle);
784
785 // Allow calling this from the compositor thread.
[email protected]dd32b1272013-05-04 14:17:11786 if (base::MessageLoop::current() == message_loop())
[email protected]fb79aa22012-12-13 01:12:13787 success = ChildThread::Send(message);
788 else
789 success = sync_message_filter()->Send(message);
790
791 if (!success)
792 return scoped_ptr<base::SharedMemory>();
793
794 if (!base::SharedMemory::IsHandleValid(handle))
795 return scoped_ptr<base::SharedMemory>();
796
797 return scoped_ptr<base::SharedMemory>(new base::SharedMemory(handle, false));
[email protected]00614a82011-10-07 22:39:31798}
799
[email protected]f1a29a02011-10-06 23:08:44800void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:06801 WebScriptController::registerExtension(extension);
[email protected]526476902011-10-06 20:34:06802}
803
[email protected]6593ae12011-11-14 12:09:44804void RenderThreadImpl::ScheduleIdleHandler(int64 initial_delay_ms) {
805 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:06806 idle_timer_.Stop();
807 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:44808 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:44809 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:06810}
811
[email protected]f1a29a02011-10-06 23:08:44812void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:48813 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
[email protected]e9ff79c2012-10-19 21:31:26814 GetContentClient()->renderer()->
[email protected]1784b2f2011-11-24 10:53:48815 RunIdleHandlerWhenWidgetsHidden();
816 if (run_in_foreground_tab) {
817 IdleHandlerInForegroundTab();
818 return;
819 }
[email protected]237a14852012-04-28 02:56:38820
821 base::allocator::ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:06822
823 v8::V8::IdleNotification();
824
825 // Schedule next invocation.
[email protected]6593ae12011-11-14 12:09:44826 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:06827 // delay = delay + 1 / (delay + 2)
828 // Using floor(delay) has a dampening effect such as:
829 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
[email protected]6593ae12011-11-14 12:09:44830 // If the delay is in milliseconds, the above formula is equivalent to:
831 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
832 // which is equivalent to
833 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
834 // Note that idle_notification_delay_in_ms_ would be reset to
835 // kInitialIdleHandlerDelayMs in RenderThreadImpl::WidgetHidden.
836 ScheduleIdleHandler(idle_notification_delay_in_ms_ +
837 1000000 / (idle_notification_delay_in_ms_ + 2000));
[email protected]526476902011-10-06 20:34:06838
839 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, IdleNotification());
840}
841
[email protected]1784b2f2011-11-24 10:53:48842void RenderThreadImpl::IdleHandlerInForegroundTab() {
843 // Increase the delay in the same way as in IdleHandler,
844 // but make it periodic by reseting it once it is too big.
845 int64 new_delay_ms = idle_notification_delay_in_ms_ +
846 1000000 / (idle_notification_delay_in_ms_ + 2000);
847 if (new_delay_ms >= kLongIdleHandlerDelayMs)
848 new_delay_ms = kShortIdleHandlerDelayMs;
849
[email protected]1784b2f2011-11-24 10:53:48850 if (idle_notifications_to_skip_ > 0) {
851 idle_notifications_to_skip_--;
[email protected]7e967f82011-12-01 09:35:14852 } else {
[email protected]a47105c832011-12-07 17:24:30853 int cpu_usage = 0;
[email protected]7e967f82011-12-01 09:35:14854 Send(new ViewHostMsg_GetCPUUsage(&cpu_usage));
[email protected]1393c4a2012-02-07 10:12:58855 // Idle notification hint roughly specifies the expected duration of the
856 // idle pause. We set it proportional to the idle timer delay.
857 int idle_hint = static_cast<int>(new_delay_ms / 10);
[email protected]d7e59252012-03-06 22:26:02858 if (cpu_usage < kIdleCPUUsageThresholdInPercents) {
[email protected]237a14852012-04-28 02:56:38859 base::allocator::ReleaseFreeMemory();
[email protected]d7e59252012-03-06 22:26:02860 if (v8::V8::IdleNotification(idle_hint)) {
861 // V8 finished collecting garbage.
862 new_delay_ms = kLongIdleHandlerDelayMs;
863 }
[email protected]1784b2f2011-11-24 10:53:48864 }
865 }
[email protected]1784b2f2011-11-24 10:53:48866 ScheduleIdleHandler(new_delay_ms);
867}
868
[email protected]6593ae12011-11-14 12:09:44869int64 RenderThreadImpl::GetIdleNotificationDelayInMs() const {
870 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:06871}
872
[email protected]6593ae12011-11-14 12:09:44873void RenderThreadImpl::SetIdleNotificationDelayInMs(
874 int64 idle_notification_delay_in_ms) {
875 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:08876}
877
[email protected]a1a7ff32012-07-19 14:03:51878void RenderThreadImpl::UpdateHistograms(int sequence_number) {
879 child_histogram_message_filter()->SendHistograms(sequence_number);
880}
881
[email protected]5b18406362013-06-18 18:46:43882int RenderThreadImpl::PostTaskToAllWebWorkers(const base::Closure& closure) {
[email protected]a9bd323d2013-06-17 20:27:56883 return webkit_glue::WorkerTaskRunner::Instance()->PostTaskToAllThreads(
884 closure);
885}
886
[email protected]b02f5902012-12-19 07:33:00887bool RenderThreadImpl::ResolveProxy(const GURL& url, std::string* proxy_list) {
888 bool result = false;
889 Send(new ViewHostMsg_ResolveProxy(url, &result, proxy_list));
890 return result;
891}
892
[email protected]1784b2f2011-11-24 10:53:48893void RenderThreadImpl::PostponeIdleNotification() {
894 idle_notifications_to_skip_ = 2;
895}
896
[email protected]1cad8802013-08-13 16:54:32897scoped_refptr<RendererGpuVideoAcceleratorFactories>
[email protected]c50edb962013-10-19 18:05:07898RenderThreadImpl::GetGpuFactories() {
[email protected]3bb8bb32013-07-11 13:13:03899 DCHECK(IsMainThread());
900
[email protected]96ab016c2013-10-23 00:50:29901 scoped_refptr<GpuChannelHost> gpu_channel_host = GetGpuChannel();
[email protected]3bb8bb32013-07-11 13:13:03902 const CommandLine* cmd_line = CommandLine::ForCurrentProcess();
[email protected]1cad8802013-08-13 16:54:32903 scoped_refptr<RendererGpuVideoAcceleratorFactories> gpu_factories;
[email protected]93274ae2013-09-19 00:53:55904 if (!cmd_line->HasSwitch(switches::kDisableAcceleratedVideoDecode)) {
905 if (!gpu_va_context_provider_ ||
906 gpu_va_context_provider_->DestroyedOnMainThread()) {
[email protected]96ab016c2013-10-23 00:50:29907 if (!gpu_channel_host) {
908 gpu_channel_host = EstablishGpuChannelSync(
909 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE);
910 }
[email protected]93274ae2013-09-19 00:53:55911 gpu_va_context_provider_ = ContextProviderCommandBuffer::Create(
912 make_scoped_ptr(
913 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
[email protected]96ab016c2013-10-23 00:50:29914 gpu_channel_host.get(),
[email protected]180ef242013-11-07 06:50:46915 blink::WebGraphicsContext3D::Attributes(),
[email protected]e91f6882013-11-07 18:13:40916 GURL("chrome://gpu/RenderThreadImpl::GetGpuVDAContext3D"),
917 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits())),
[email protected]93274ae2013-09-19 00:53:55918 "GPU-VideoAccelerator-Offscreen");
919 }
920 }
[email protected]1cad8802013-08-13 16:54:32921 if (gpu_channel_host) {
922 gpu_factories = new RendererGpuVideoAcceleratorFactories(
[email protected]96ab016c2013-10-23 00:50:29923 gpu_channel_host.get(), gpu_va_context_provider_);
[email protected]3bb8bb32013-07-11 13:13:03924 }
925 return gpu_factories;
926}
927
[email protected]c29b7ff2013-03-06 03:51:04928scoped_ptr<WebGraphicsContext3DCommandBufferImpl>
929RenderThreadImpl::CreateOffscreenContext3d() {
[email protected]180ef242013-11-07 06:50:46930 blink::WebGraphicsContext3D::Attributes attributes;
[email protected]c29b7ff2013-03-06 03:51:04931 attributes.shareResources = true;
932 attributes.depth = false;
933 attributes.stencil = false;
934 attributes.antialias = false;
935 attributes.noAutomaticFlushes = true;
936
[email protected]96ab016c2013-10-23 00:50:29937 scoped_refptr<GpuChannelHost> gpu_channel_host(EstablishGpuChannelSync(
938 CAUSE_FOR_GPU_LAUNCH_WEBGRAPHICSCONTEXT3DCOMMANDBUFFERIMPL_INITIALIZE));
[email protected]c29b7ff2013-03-06 03:51:04939 return make_scoped_ptr(
940 WebGraphicsContext3DCommandBufferImpl::CreateOffscreenContext(
[email protected]96ab016c2013-10-23 00:50:29941 gpu_channel_host.get(),
[email protected]c29b7ff2013-03-06 03:51:04942 attributes,
[email protected]e91f6882013-11-07 18:13:40943 GURL("chrome://gpu/RenderThreadImpl::CreateOffscreenContext3d"),
944 WebGraphicsContext3DCommandBufferImpl::SharedMemoryLimits()));
[email protected]c29b7ff2013-03-06 03:51:04945}
946
[email protected]788dba52013-06-07 21:48:49947scoped_refptr<cc::ContextProvider>
[email protected]f10dc472013-09-27 03:31:59948RenderThreadImpl::OffscreenCompositorContextProvider() {
[email protected]e06e1122013-03-15 17:12:38949 DCHECK(IsMainThread());
950
[email protected]788dba52013-06-07 21:48:49951#if defined(OS_ANDROID)
952 if (SynchronousCompositorFactory* factory =
953 SynchronousCompositorFactory::GetInstance()) {
[email protected]f10dc472013-09-27 03:31:59954 if (compositor_message_loop_proxy_)
955 return factory->GetOffscreenContextProviderForCompositorThread();
[email protected]788dba52013-06-07 21:48:49956 return factory->GetOffscreenContextProviderForMainThread();
957 }
958#endif
959
[email protected]f10dc472013-09-27 03:31:59960 if (!offscreen_compositor_contexts_.get() ||
961 offscreen_compositor_contexts_->DestroyedOnMainThread()) {
962 offscreen_compositor_contexts_ = ContextProviderCommandBuffer::Create(
[email protected]af13d832013-09-13 06:56:27963 CreateOffscreenContext3d(),
[email protected]f10dc472013-09-27 03:31:59964 "Compositor-Offscreen");
[email protected]e06e1122013-03-15 17:12:38965 }
[email protected]f10dc472013-09-27 03:31:59966 return offscreen_compositor_contexts_;
[email protected]c29b7ff2013-03-06 03:51:04967}
968
[email protected]9cdd7152014-01-14 14:20:43969scoped_refptr<webkit::gpu::ContextProviderWebContext>
[email protected]f10dc472013-09-27 03:31:59970RenderThreadImpl::SharedMainThreadContextProvider() {
[email protected]e06e1122013-03-15 17:12:38971 DCHECK(IsMainThread());
[email protected]788dba52013-06-07 21:48:49972#if defined(OS_ANDROID)
973 if (SynchronousCompositorFactory* factory =
[email protected]f10dc472013-09-27 03:31:59974 SynchronousCompositorFactory::GetInstance())
975 return factory->GetOffscreenContextProviderForMainThread();
[email protected]788dba52013-06-07 21:48:49976#endif
977
[email protected]f10dc472013-09-27 03:31:59978 if (!shared_main_thread_contexts_ ||
979 shared_main_thread_contexts_->DestroyedOnMainThread()) {
980 if (compositor_message_loop_proxy_) {
981 // In threaded compositing mode, we have to create a new ContextProvider
982 // to bind to the main thread since the compositor's is bound to the
983 // compositor thread.
984 shared_main_thread_contexts_ =
985 ContextProviderCommandBuffer::Create(CreateOffscreenContext3d(),
986 "Offscreen-MainThread");
987 } else {
988 // In single threaded mode, we can use the same context provider.
989 shared_main_thread_contexts_ =
990 static_cast<ContextProviderCommandBuffer*>(
991 OffscreenCompositorContextProvider().get());
992 }
[email protected]c29b7ff2013-03-06 03:51:04993 }
[email protected]f10dc472013-09-27 03:31:59994 if (shared_main_thread_contexts_ &&
995 !shared_main_thread_contexts_->BindToCurrentThread())
996 shared_main_thread_contexts_ = NULL;
997 return shared_main_thread_contexts_;
[email protected]c29b7ff2013-03-06 03:51:04998}
999
[email protected]e9ff79c2012-10-19 21:31:261000AudioRendererMixerManager* RenderThreadImpl::GetAudioRendererMixerManager() {
[email protected]59383c782013-04-17 16:43:271001 if (!audio_renderer_mixer_manager_) {
[email protected]3958e972012-07-17 00:25:411002 audio_renderer_mixer_manager_.reset(new AudioRendererMixerManager(
[email protected]563d665f2013-02-01 04:42:391003 GetAudioHardwareConfig()));
[email protected]3958e972012-07-17 00:25:411004 }
1005
1006 return audio_renderer_mixer_manager_.get();
1007}
1008
[email protected]563d665f2013-02-01 04:42:391009media::AudioHardwareConfig* RenderThreadImpl::GetAudioHardwareConfig() {
1010 if (!audio_hardware_config_) {
[email protected]06ea4f7c2013-03-13 20:36:511011 media::AudioParameters input_params;
1012 media::AudioParameters output_params;
[email protected]563d665f2013-02-01 04:42:391013 Send(new ViewHostMsg_GetAudioHardwareConfig(
[email protected]06ea4f7c2013-03-13 20:36:511014 &input_params, &output_params));
[email protected]563d665f2013-02-01 04:42:391015
1016 audio_hardware_config_.reset(new media::AudioHardwareConfig(
[email protected]06ea4f7c2013-03-13 20:36:511017 input_params, output_params));
[email protected]563d665f2013-02-01 04:42:391018 audio_message_filter_->SetAudioHardwareConfig(audio_hardware_config_.get());
1019 }
1020
1021 return audio_hardware_config_.get();
1022}
1023
[email protected]4a7d6392011-09-19 20:55:081024#if defined(OS_WIN)
[email protected]feb32372012-11-21 01:14:561025void RenderThreadImpl::PreCacheFontCharacters(const LOGFONT& log_font,
[email protected]fcf75d42013-12-03 20:11:261026 const base::string16& str) {
[email protected]feb32372012-11-21 01:14:561027 Send(new ViewHostMsg_PreCacheFontCharacters(log_font, str));
1028}
1029
[email protected]f1a29a02011-10-06 23:08:441030void RenderThreadImpl::PreCacheFont(const LOGFONT& log_font) {
[email protected]526476902011-10-06 20:34:061031 Send(new ChildProcessHostMsg_PreCacheFont(log_font));
[email protected]79fff822011-09-20 03:33:211032}
1033
[email protected]f1a29a02011-10-06 23:08:441034void RenderThreadImpl::ReleaseCachedFonts() {
[email protected]526476902011-10-06 20:34:061035 Send(new ChildProcessHostMsg_ReleaseCachedFonts());
[email protected]4a7d6392011-09-19 20:55:081036}
[email protected]526476902011-10-06 20:34:061037
[email protected]4a7d6392011-09-19 20:55:081038#endif // OS_WIN
1039
[email protected]b3e83de2012-02-07 03:33:281040bool RenderThreadImpl::IsMainThread() {
1041 return !!current();
1042}
1043
[email protected]9e7154122013-05-30 23:11:041044base::MessageLoop* RenderThreadImpl::GetMainLoop() {
[email protected]b3e83de2012-02-07 03:33:281045 return message_loop();
1046}
[email protected]5b040e592012-02-10 02:56:101047
1048scoped_refptr<base::MessageLoopProxy> RenderThreadImpl::GetIOLoopProxy() {
[email protected]57cf97882013-06-26 20:39:011049 return io_message_loop_proxy_;
[email protected]b3e83de2012-02-07 03:33:281050}
1051
1052base::WaitableEvent* RenderThreadImpl::GetShutDownEvent() {
[email protected]57cf97882013-06-26 20:39:011053 return shutdown_event_;
[email protected]b3e83de2012-02-07 03:33:281054}
1055
1056scoped_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
[email protected]3f892832013-01-11 03:23:591057 size_t size) {
[email protected]fb79aa22012-12-13 01:12:131058 return scoped_ptr<base::SharedMemory>(
1059 HostAllocateSharedMemoryBuffer(size));
[email protected]b3e83de2012-02-07 03:33:281060}
1061
1062int32 RenderThreadImpl::CreateViewCommandBuffer(
1063 int32 surface_id, const GPUCreateCommandBufferConfig& init_params) {
[email protected]d13f35d2012-05-18 02:28:151064 TRACE_EVENT1("gpu",
1065 "RenderThreadImpl::CreateViewCommandBuffer",
1066 "surface_id",
1067 surface_id);
1068
[email protected]b3e83de2012-02-07 03:33:281069 int32 route_id = MSG_ROUTING_NONE;
[email protected]8b10b332012-02-29 21:02:141070 IPC::Message* message = new GpuHostMsg_CreateViewCommandBuffer(
[email protected]b3e83de2012-02-07 03:33:281071 surface_id,
1072 init_params,
[email protected]8b10b332012-02-29 21:02:141073 &route_id);
1074
1075 // Allow calling this from the compositor thread.
[email protected]57cf97882013-06-26 20:39:011076 thread_safe_sender()->Send(message);
[email protected]8b10b332012-02-29 21:02:141077
[email protected]b3e83de2012-02-07 03:33:281078 return route_id;
1079}
1080
[email protected]09d50362012-10-18 20:54:371081void RenderThreadImpl::CreateImage(
1082 gfx::PluginWindowHandle window,
1083 int32 image_id,
1084 const CreateImageCallback& callback) {
1085 NOTREACHED();
1086}
1087
1088void RenderThreadImpl::DeleteImage(int32 image_id, int32 sync_point) {
1089 NOTREACHED();
1090}
1091
[email protected]bc10f462013-10-22 23:13:091092scoped_ptr<gfx::GpuMemoryBuffer> RenderThreadImpl::AllocateGpuMemoryBuffer(
1093 size_t width,
1094 size_t height,
1095 unsigned internalformat) {
[email protected]88105162013-12-05 01:02:121096 DCHECK(allocate_gpu_memory_buffer_thread_checker_.CalledOnValidThread());
[email protected]bc10f462013-10-22 23:13:091097
[email protected]88105162013-12-05 01:02:121098 if (!GpuMemoryBufferImpl::IsFormatValid(internalformat))
[email protected]bc10f462013-10-22 23:13:091099 return scoped_ptr<gfx::GpuMemoryBuffer>();
1100
1101 gfx::GpuMemoryBufferHandle handle;
1102 bool success;
1103 IPC::Message* message =
[email protected]88105162013-12-05 01:02:121104 new ChildProcessHostMsg_SyncAllocateGpuMemoryBuffer(width,
1105 height,
1106 internalformat,
1107 &handle);
[email protected]bc10f462013-10-22 23:13:091108
1109 // Allow calling this from the compositor thread.
1110 if (base::MessageLoop::current() == message_loop())
1111 success = ChildThread::Send(message);
1112 else
1113 success = sync_message_filter()->Send(message);
1114
1115 if (!success)
1116 return scoped_ptr<gfx::GpuMemoryBuffer>();
1117
[email protected]88105162013-12-05 01:02:121118 return GpuMemoryBufferImpl::Create(
1119 handle,
1120 gfx::Size(width, height),
1121 internalformat).PassAs<gfx::GpuMemoryBuffer>();
[email protected]bc10f462013-10-22 23:13:091122}
1123
[email protected]f1a29a02011-10-06 23:08:441124void RenderThreadImpl::DoNotSuspendWebKitSharedTimer() {
[email protected]526476902011-10-06 20:34:061125 suspend_webkit_shared_timer_ = false;
1126}
1127
[email protected]f1a29a02011-10-06 23:08:441128void RenderThreadImpl::DoNotNotifyWebKitOfModalLoop() {
[email protected]526476902011-10-06 20:34:061129 notify_webkit_of_modal_loop_ = false;
1130}
1131
[email protected]367c5c1d2013-03-11 18:59:021132void RenderThreadImpl::OnSetZoomLevelForCurrentURL(const std::string& scheme,
1133 const std::string& host,
[email protected]f1a29a02011-10-06 23:08:441134 double zoom_level) {
[email protected]367c5c1d2013-03-11 18:59:021135 RenderViewZoomer zoomer(scheme, host, zoom_level);
[email protected]e9ff79c2012-10-19 21:31:261136 RenderView::ForEach(&zoomer);
[email protected]526476902011-10-06 20:34:061137}
1138
[email protected]f1a29a02011-10-06 23:08:441139bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]1223d6ef2011-03-28 16:47:501140 ObserverListBase<RenderProcessObserver>::Iterator it(observers_);
1141 RenderProcessObserver* observer;
1142 while ((observer = it.GetNext()) != NULL) {
1143 if (observer->OnControlMessageReceived(msg))
1144 return true;
1145 }
1146
[email protected]70c19a932010-05-14 12:59:111147 // Some messages are handled by delegates.
[email protected]1910fe82012-05-10 00:04:101148 if (appcache_dispatcher_->OnMessageReceived(msg) ||
[email protected]7670d4332013-11-29 04:35:001149 dom_storage_dispatcher_->OnMessageReceived(msg) ||
1150 embedded_worker_dispatcher_->OnMessageReceived(msg)) {
[email protected]a95986a82010-12-24 06:19:281151 return true;
[email protected]1910fe82012-05-10 00:04:101152 }
[email protected]1edc16b82009-04-07 17:45:541153
[email protected]a95986a82010-12-24 06:19:281154 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:441155 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]9d797f32010-04-23 07:17:541156 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
1157 OnSetZoomLevelForCurrentURL)
[email protected]8930d472009-02-21 08:05:281158 // TODO(port): removed from render_messages_internal.h;
1159 // is there a new non-windows message I should add here?
1160 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
[email protected]3e267192011-03-25 01:55:451161 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
[email protected]6eac57a2011-07-12 21:15:091162 IPC_MESSAGE_HANDLER(ViewMsg_NetworkStateChanged, OnNetworkStateChanged)
[email protected]b69934e2011-10-29 02:51:521163 IPC_MESSAGE_HANDLER(ViewMsg_TempCrashWithData, OnTempCrashWithData)
[email protected]7d08a9352013-10-15 08:24:561164 IPC_MESSAGE_HANDLER(ViewMsg_SetRendererProcessID, OnSetRendererProcessID)
[email protected]b0988b22013-11-08 08:25:581165#if defined(OS_ANDROID)
[email protected]44363382013-03-12 07:22:471166 IPC_MESSAGE_HANDLER(ViewMsg_SetWebKitSharedTimersSuspended,
1167 OnSetWebKitSharedTimersSuspended)
[email protected]b0988b22013-11-08 08:25:581168#endif
1169#if defined(OS_MACOSX)
1170 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScrollbarTheme, OnUpdateScrollbarTheme)
1171#endif
[email protected]a95986a82010-12-24 06:19:281172 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:281173 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:281174 return handled;
initial.commit09911bf2008-07-26 23:55:291175}
1176
[email protected]f1a29a02011-10-06 23:08:441177void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:471178 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:331179 // When bringing in render_view, also bring in webkit's glue and jsbindings.
[email protected]6cd7c6b2012-10-25 03:26:231180 RenderViewImpl::Create(
[email protected]6cd7c6b2012-10-25 03:26:231181 params.opener_route_id,
1182 params.renderer_preferences,
1183 params.web_preferences,
[email protected]6cd7c6b2012-10-25 03:26:231184 params.view_id,
[email protected]227692c52013-05-31 22:43:041185 params.main_frame_routing_id,
[email protected]6cd7c6b2012-10-25 03:26:231186 params.surface_id,
1187 params.session_storage_namespace_id,
1188 params.frame_name,
1189 false,
1190 params.swapped_out,
[email protected]1ac10dca2013-08-20 20:47:041191 params.hidden,
[email protected]6cd7c6b2012-10-25 03:26:231192 params.next_page_id,
1193 params.screen_info,
[email protected]34bb3ac2013-03-08 02:41:281194 params.accessibility_mode,
1195 params.allow_partial_swap);
[email protected]7f874dec2009-02-06 01:48:271196}
[email protected]4274e582009-01-27 22:09:561197
[email protected]f1a29a02011-10-06 23:08:441198GpuChannelHost* RenderThreadImpl::EstablishGpuChannelSync(
[email protected]e9ff79c2012-10-19 21:31:261199 CauseForGpuLaunch cause_for_gpu_launch) {
[email protected]d13f35d2012-05-18 02:28:151200 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
1201
[email protected]fc72bb12013-06-02 21:13:461202 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:221203 // Do nothing if we already have a GPU channel or are already
1204 // establishing one.
[email protected]7951bfe2013-06-13 01:43:341205 if (!gpu_channel_->IsLost())
1206 return gpu_channel_.get();
[email protected]6217d392010-03-25 22:08:351207
1208 // Recreate the channel if it has been lost.
[email protected]894e8fc2012-02-24 13:29:501209 gpu_channel_ = NULL;
[email protected]6217d392010-03-25 22:08:351210 }
1211
[email protected]6217d392010-03-25 22:08:351212 // Ask the browser for the channel name.
[email protected]894e8fc2012-02-24 13:29:501213 int client_id = 0;
[email protected]b42a9f62011-06-08 20:36:041214 IPC::ChannelHandle channel_handle;
[email protected]d7b5cc72013-05-23 20:05:001215 gpu::GPUInfo gpu_info;
[email protected]b42a9f62011-06-08 20:36:041216 if (!Send(new GpuHostMsg_EstablishGpuChannel(cause_for_gpu_launch,
[email protected]894e8fc2012-02-24 13:29:501217 &client_id,
[email protected]b42a9f62011-06-08 20:36:041218 &channel_handle,
[email protected]b42a9f62011-06-08 20:36:041219 &gpu_info)) ||
[email protected]8bc550e2012-02-07 11:05:561220#if defined(OS_POSIX)
1221 channel_handle.socket.fd == -1 ||
1222#endif
[email protected]cd0bd792012-04-14 00:52:161223 channel_handle.name.empty()) {
[email protected]b42a9f62011-06-08 20:36:041224 // Otherwise cancel the connection.
[email protected]b42a9f62011-06-08 20:36:041225 return NULL;
1226 }
[email protected]6217d392010-03-25 22:08:351227
[email protected]e9ff79c2012-10-19 21:31:261228 GetContentClient()->SetGpuInfo(gpu_info);
[email protected]57cf97882013-06-26 20:39:011229
1230 // Cache some variables that are needed on the compositor thread for our
1231 // implementation of GpuChannelHostFactory.
1232 io_message_loop_proxy_ = ChildProcess::current()->io_message_loop_proxy();
1233 shutdown_event_ = ChildProcess::current()->GetShutDownEvent();
1234
[email protected]40d022582014-01-15 19:05:281235 gpu_channel_ = GpuChannelHost::Create(this, gpu_info, channel_handle);
[email protected]7951bfe2013-06-13 01:43:341236 return gpu_channel_.get();
[email protected]3bf4d532010-03-27 00:23:341237}
1238
[email protected]180ef242013-11-07 06:50:461239blink::WebMediaStreamCenter* RenderThreadImpl::CreateMediaStreamCenter(
1240 blink::WebMediaStreamCenterClient* client) {
[email protected]d9da9582013-01-31 04:59:051241#if defined(OS_ANDROID)
[email protected]ce585bf2013-03-14 16:25:161242 if (CommandLine::ForCurrentProcess()->HasSwitch(
1243 switches::kDisableWebRTC))
[email protected]d9da9582013-01-31 04:59:051244 return NULL;
1245#endif
1246
[email protected]d8cd8372012-03-09 10:49:511247#if defined(ENABLE_WEBRTC)
[email protected]68e5fee2013-02-18 10:04:221248 if (!media_stream_center_) {
1249 media_stream_center_ = GetContentClient()->renderer()
1250 ->OverrideCreateWebMediaStreamCenter(client);
1251 if (!media_stream_center_) {
[email protected]c1298742013-07-17 04:06:321252 scoped_ptr<MediaStreamCenter> media_stream_center(
1253 new MediaStreamCenter(client, GetMediaStreamDependencyFactory()));
1254 AddObserver(media_stream_center.get());
1255 media_stream_center_ = media_stream_center.release();
[email protected]68e5fee2013-02-18 10:04:221256 }
1257 }
[email protected]d8cd8372012-03-09 10:49:511258#endif
1259 return media_stream_center_;
1260}
1261
[email protected]6ee10bd2012-09-13 09:01:531262MediaStreamDependencyFactory*
1263RenderThreadImpl::GetMediaStreamDependencyFactory() {
[email protected]6ee10bd2012-09-13 09:01:531264 return media_stream_factory_.get();
1265}
1266
[email protected]f1a29a02011-10-06 23:08:441267GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
[email protected]fc72bb12013-06-02 21:13:461268 if (!gpu_channel_.get())
[email protected]6217d392010-03-25 22:08:351269 return NULL;
1270
[email protected]7951bfe2013-06-13 01:43:341271 if (gpu_channel_->IsLost())
[email protected]6217d392010-03-25 22:08:351272 return NULL;
1273
1274 return gpu_channel_.get();
1275}
1276
[email protected]f1a29a02011-10-06 23:08:441277void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:591278 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:271279 // The call below will cause a GetPlugins call with refresh=true, but at this
1280 // point we already know that the browser has refreshed its list, so disable
1281 // refresh temporarily to prevent each renderer process causing the list to be
1282 // regenerated.
[email protected]a70b4352012-07-31 22:34:531283 webkit_platform_support_->set_plugin_refresh_allowed(false);
[email protected]180ef242013-11-07 06:50:461284 blink::resetPluginCache(reload_pages);
[email protected]a70b4352012-07-31 22:34:531285 webkit_platform_support_->set_plugin_refresh_allowed(true);
[email protected]3b48dbc2012-01-06 16:34:171286
1287 FOR_EACH_OBSERVER(RenderProcessObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:271288}
[email protected]85c55dc2009-11-06 03:05:461289
[email protected]f1a29a02011-10-06 23:08:441290void RenderThreadImpl::OnNetworkStateChanged(bool online) {
[email protected]6eac57a2011-07-12 21:15:091291 EnsureWebKitInitialized();
1292 WebNetworkStateNotifier::setOnLine(online);
1293}
1294
[email protected]b69934e2011-10-29 02:51:521295void RenderThreadImpl::OnTempCrashWithData(const GURL& data) {
[email protected]e9ff79c2012-10-19 21:31:261296 GetContentClient()->SetActiveURL(data);
[email protected]b69934e2011-10-29 02:51:521297 CHECK(false);
1298}
1299
[email protected]7d08a9352013-10-15 08:24:561300void RenderThreadImpl::OnSetRendererProcessID(base::ProcessId process_id) {
1301 renderer_process_id_ = process_id;
1302}
1303
[email protected]b0988b22013-11-08 08:25:581304#if defined(OS_ANDROID)
[email protected]44363382013-03-12 07:22:471305void RenderThreadImpl::OnSetWebKitSharedTimersSuspended(bool suspend) {
[email protected]3b96b7b52014-01-13 18:36:071306 if (suspend_webkit_shared_timer_) {
1307 EnsureWebKitInitialized();
1308 if (suspend) {
1309 webkit_platform_support_->SuspendSharedTimer();
1310 } else {
1311 webkit_platform_support_->ResumeSharedTimer();
1312 }
1313 }
[email protected]44363382013-03-12 07:22:471314}
[email protected]b0988b22013-11-08 08:25:581315#endif
1316
1317#if defined(OS_MACOSX)
1318void RenderThreadImpl::OnUpdateScrollbarTheme(float initial_button_delay,
1319 float autoscroll_button_delay,
1320 bool jump_on_track_click,
1321 bool redraw) {
1322 blink::WebScrollbarTheme::updateScrollbars(initial_button_delay,
1323 autoscroll_button_delay,
1324 jump_on_track_click,
1325 redraw);
1326}
1327#endif
[email protected]44363382013-03-12 07:22:471328
[email protected]5071cb22013-07-10 02:19:061329void RenderThreadImpl::OnMemoryPressure(
1330 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
1331 base::allocator::ReleaseFreeMemory();
1332
1333 if (memory_pressure_level ==
1334 base::MemoryPressureListener::MEMORY_PRESSURE_CRITICAL) {
1335 // Trigger full v8 garbage collection on critical memory notification.
1336 v8::V8::LowMemoryNotification();
[email protected]3311c542013-07-24 12:24:031337 // Clear the image cache.
[email protected]180ef242013-11-07 06:50:461338 blink::WebImageCache::clear();
[email protected]8e3f8c82013-08-21 15:09:551339 // Purge Skia font cache, by setting it to 0 and then again to the previous
1340 // limit.
1341 size_t font_cache_limit = SkGraphics::SetFontCacheLimit(0);
1342 SkGraphics::SetFontCacheLimit(font_cache_limit);
[email protected]5071cb22013-07-10 02:19:061343 } else {
1344 // Otherwise trigger a couple of v8 GCs using IdleNotification.
1345 if (!v8::V8::IdleNotification())
1346 v8::V8::IdleNotification();
1347 }
1348}
1349
[email protected]c6a7b862010-08-20 22:19:381350scoped_refptr<base::MessageLoopProxy>
[email protected]f1a29a02011-10-06 23:08:441351RenderThreadImpl::GetFileThreadMessageLoopProxy() {
[email protected]dd32b1272013-05-04 14:17:111352 DCHECK(message_loop() == base::MessageLoop::current());
[email protected]59383c782013-04-17 16:43:271353 if (!file_thread_) {
[email protected]c6a7b862010-08-20 22:19:381354 file_thread_.reset(new base::Thread("Renderer::FILE"));
1355 file_thread_->Start();
1356 }
1357 return file_thread_->message_loop_proxy();
1358}
[email protected]e9ff79c2012-10-19 21:31:261359
[email protected]c1330c82013-06-06 02:23:251360scoped_refptr<base::MessageLoopProxy>
1361RenderThreadImpl::GetMediaThreadMessageLoopProxy() {
1362 DCHECK(message_loop() == base::MessageLoop::current());
1363 if (!media_thread_) {
1364 media_thread_.reset(new base::Thread("Media"));
1365 media_thread_->Start();
[email protected]6ca6a082013-09-22 20:10:051366
1367#if defined(OS_ANDROID)
1368 renderer_demuxer_ = new RendererDemuxerAndroid();
1369 AddFilter(renderer_demuxer_.get());
1370#endif
[email protected]c1330c82013-06-06 02:23:251371 }
1372 return media_thread_->message_loop_proxy();
1373}
1374
[email protected]107c0532013-02-06 02:34:091375void RenderThreadImpl::SetFlingCurveParameters(
1376 const std::vector<float>& new_touchpad,
1377 const std::vector<float>& new_touchscreen) {
1378 webkit_platform_support_->SetFlingCurveParameters(new_touchpad,
1379 new_touchscreen);
1380
1381}
1382
[email protected]180ef242013-11-07 06:50:461383void RenderThreadImpl::SampleGamepads(blink::WebGamepads* data) {
[email protected]ca998e5e2013-08-06 14:59:091384 if (!gamepad_shared_memory_reader_)
1385 gamepad_shared_memory_reader_.reset(new GamepadSharedMemoryReader);
1386 gamepad_shared_memory_reader_->SampleGamepads(*data);
1387}
1388
[email protected]7d08a9352013-10-15 08:24:561389base::ProcessId RenderThreadImpl::renderer_process_id() const {
1390 return renderer_process_id_;
1391}
1392
[email protected]b2db9272014-01-10 17:42:001393void RenderThreadImpl::WidgetCreated() {
1394 widget_count_++;
1395}
1396
1397void RenderThreadImpl::WidgetDestroyed() {
1398 widget_count_--;
1399}
1400
1401void RenderThreadImpl::WidgetHidden() {
1402 DCHECK_LT(hidden_widget_count_, widget_count_);
1403 hidden_widget_count_++;
1404
1405 if (widget_count_ && hidden_widget_count_ == widget_count_) {
1406#if !defined(SYSTEM_NATIVELY_SIGNALS_MEMORY_PRESSURE)
1407 // TODO(vollick): Remove this this heavy-handed approach once we're polling
1408 // the real system memory pressure.
1409 base::MemoryPressureListener::NotifyMemoryPressure(
1410 base::MemoryPressureListener::MEMORY_PRESSURE_MODERATE);
1411#endif
1412 if (GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
1413 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
1414 }
1415}
1416
1417void RenderThreadImpl::WidgetRestored() {
1418 DCHECK_GT(hidden_widget_count_, 0);
1419 hidden_widget_count_--;
1420
1421 if (!GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden()) {
1422 return;
1423 }
1424
1425 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
1426}
1427
[email protected]e9ff79c2012-10-19 21:31:261428} // namespace content