blob: 13e32e613c5b117860e7ba09788079364726c100 [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>
dcheng07945f632015-12-26 07:59:3210#include <utility>
[email protected]da00a2882009-03-09 17:51:1911#include <vector>
12
[email protected]237a14852012-04-28 02:56:3813#include "base/allocator/allocator_extension.h"
[email protected]06533c0b2009-03-05 21:39:1114#include "base/command_line.h"
creis9f7248b2016-01-27 20:27:3915#include "base/debug/crash_logging.h"
[email protected]94f9a0f682009-06-15 18:30:3016#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0317#include "base/logging.h"
avi1023d012015-12-25 02:39:1418#include "base/macros.h"
reveman1af05cb32015-03-17 23:18:1619#include "base/memory/discardable_memory_allocator.h"
dchengcedca5612016-04-09 01:40:1520#include "base/memory/ptr_util.h"
[email protected]8bac37b2013-07-18 19:13:5621#include "base/memory/shared_memory.h"
[email protected]835d7c82010-10-14 04:38:3822#include "base/metrics/field_trial.h"
[email protected]6cf19311f2011-04-14 23:06:0223#include "base/metrics/histogram.h"
[email protected]433df472012-03-07 20:33:3924#include "base/path_service.h"
fdoray2df4a9e2016-07-18 23:47:1625#include "base/run_loop.h"
[email protected]21aa99682013-06-11 07:17:0126#include "base/strings/string16.h"
[email protected]1e1d1e12014-01-17 16:14:2927#include "base/strings/string_number_conversions.h"
hablichd6a4f122015-10-28 11:34:4928#include "base/strings/string_split.h"
[email protected]3dda5852014-02-19 06:42:1429#include "base/strings/string_tokenizer.h"
ccameron6728bae32015-01-09 20:18:0630#include "base/strings/sys_string_conversions.h"
[email protected]74ebfb12013-06-07 20:48:0031#include "base/strings/utf_string_conversions.h"
reveman34b7a1522015-03-23 20:27:4732#include "base/threading/simple_thread.h"
[email protected]1357c322010-12-30 22:18:5633#include "base/threading/thread_local.h"
[email protected]d293572a2013-05-23 18:50:4534#include "base/threading/thread_restrictions.h"
gab30f26df2016-05-11 19:37:5535#include "base/threading/thread_task_runner_handle.h"
primiano9e38d552015-01-28 04:18:0136#include "base/trace_event/trace_event.h"
[email protected]7a4de7a62010-08-17 18:38:2437#include "base/values.h"
avi1023d012015-12-25 02:39:1438#include "build/build_config.h"
jbroman0d302162015-09-05 05:46:2339#include "cc/base/histograms.h"
[email protected]d72493152014-01-08 17:37:4540#include "cc/base/switches.h"
[email protected]38564622014-08-19 02:47:1841#include "cc/blink/web_external_bitmap_impl.h"
42#include "cc/blink/web_layer_impl.h"
ericrk9151705c2016-07-26 19:53:1643#include "cc/output/buffer_to_texture_target_map.h"
jbroman6ccbc7d472016-07-27 04:45:4144#include "cc/output/copy_output_request.h"
danakj83066a32016-06-21 02:34:4945#include "cc/output/output_surface.h"
46#include "cc/output/vulkan_in_process_context_provider.h"
danakj920156852015-05-18 20:22:2947#include "cc/raster/task_graph_runner.h"
vollick1050cc62015-12-03 07:04:5448#include "cc/trees/layer_tree_host_common.h"
loysoa6edaaff2015-05-25 03:26:4449#include "cc/trees/layer_tree_settings.h"
bashi77f19b32016-07-19 11:19:4350#include "components/memory_coordinator/child/child_memory_coordinator_impl.h"
[email protected]a8cb3b73b2013-08-12 05:50:5051#include "content/child/appcache/appcache_dispatcher.h"
52#include "content/child/appcache/appcache_frontend_impl.h"
dmurph1fb98482016-03-30 21:14:2653#include "content/child/blob_storage/blob_message_filter.h"
revemancb5a66af2014-10-25 00:34:3954#include "content/child/child_discardable_shared_memory_manager.h"
reveman22dd9292014-10-13 20:52:0555#include "content/child/child_gpu_memory_buffer_manager.h"
[email protected]10208ea2013-06-06 20:08:0356#include "content/child/child_histogram_message_filter.h"
alexclarke0bc36e8d32014-12-17 17:29:2057#include "content/child/child_resource_message_filter.h"
jbauman2f5c1942014-12-06 03:28:2458#include "content/child/child_shared_bitmap_manager.h"
[email protected]fdeac262014-07-10 12:52:5159#include "content/child/content_child_helpers.h"
[email protected]89c36f82013-07-17 06:23:2760#include "content/child/db_message_filter.h"
[email protected]c7199a6e2013-06-04 12:56:0161#include "content/child/indexed_db/indexed_db_dispatcher.h"
62#include "content/child/indexed_db/indexed_db_message_filter.h"
[email protected]10208ea2013-06-06 20:08:0363#include "content/child/resource_dispatcher.h"
alexclarke0bc36e8d32014-12-17 17:29:2064#include "content/child/resource_scheduling_filter.h"
[email protected]643255da2013-06-12 20:55:1365#include "content/child/runtime_features.h"
[email protected]57cf97882013-06-26 20:39:0166#include "content/child/thread_safe_sender.h"
[email protected]c7199a6e2013-06-04 12:56:0167#include "content/child/web_database_observer_impl.h"
darin815c4cb2016-08-08 21:05:3168#include "content/child/websocket_message_filter.h"
kinukoef647412015-12-23 06:10:4369#include "content/child/worker_thread_registry.h"
[email protected]bdae9812011-10-15 00:33:0370#include "content/common/child_process_messages.h"
[email protected]89c36f82013-07-17 06:23:2771#include "content/common/content_constants_internal.h"
[email protected]5f2aa722013-08-07 16:59:4172#include "content/common/dom_storage/dom_storage_messages.h"
[email protected]82307f6b2014-08-07 03:30:1273#include "content/common/frame_messages.h"
raymesbba82b32016-07-19 00:41:3874#include "content/common/frame_owner_properties.h"
[email protected]c29b7ff2013-03-06 03:51:0475#include "content/common/gpu/client/context_provider_command_buffer.h"
avia7c3f51c2015-09-16 00:30:3176#include "content/common/render_process_messages.h"
[email protected]94dc971d2011-03-05 19:08:3277#include "content/common/resource_messages.h"
mek27c9d742015-07-16 18:30:1878#include "content/common/service_worker/embedded_worker_setup.mojom.h"
[email protected]778574e2011-03-21 22:03:5079#include "content/common/view_messages.h"
[email protected]0ec90d522014-03-12 16:28:1980#include "content/common/worker_messages.h"
[email protected]a458504b2012-07-23 19:57:0681#include "content/public/common/content_constants.h"
[email protected]433df472012-03-07 20:33:3982#include "content/public/common/content_paths.h"
[email protected]c08950d22011-10-13 22:20:2983#include "content/public/common/content_switches.h"
[email protected]daf82f82011-10-31 22:35:3184#include "content/public/common/renderer_preferences.h"
[email protected]58436a12012-03-21 17:10:2685#include "content/public/common/url_constants.h"
[email protected]d344114c2011-10-01 01:24:3486#include "content/public/renderer/content_renderer_client.h"
tyoshino832a58a2016-04-18 08:14:0887#include "content/public/renderer/render_thread_observer.h"
[email protected]64ffa0442011-10-03 22:08:3688#include "content/public/renderer/render_view_visitor.h"
fsamuel6c1dfeb2014-12-18 19:21:3389#include "content/renderer/browser_plugin/browser_plugin_manager.h"
jsbell279efb42015-03-31 17:02:4690#include "content/renderer/cache_storage/cache_storage_dispatcher.h"
91#include "content/renderer/cache_storage/cache_storage_message_filter.h"
prashant.nfad657e2016-06-01 07:52:1792#include "content/renderer/categorized_worker_pool.h"
[email protected]70019152012-12-19 11:44:1993#include "content/renderer/devtools/devtools_agent_filter.h"
alph666f80f2014-12-08 15:52:1194#include "content/renderer/devtools/v8_sampling_profiler.h"
[email protected]1910fe82012-05-10 00:04:1095#include "content/renderer/dom_storage/dom_storage_dispatcher.h"
96#include "content/renderer/dom_storage/webstoragearea_impl.h"
97#include "content/renderer/dom_storage/webstoragenamespace_impl.h"
simonhonga7e3ac42014-11-11 20:50:2298#include "content/renderer/gpu/compositor_external_begin_frame_source.h"
99#include "content/renderer/gpu/compositor_forwarding_message_filter.h"
[email protected]1842fe22012-08-13 23:24:35100#include "content/renderer/gpu/compositor_output_surface.h"
danakj83066a32016-06-21 02:34:49101#include "content/renderer/gpu/frame_swap_message_queue.h"
[email protected]7a72d452013-12-13 10:01:13102#include "content/renderer/input/input_event_filter.h"
103#include "content/renderer/input/input_handler_manager.h"
jdduke691dd572014-12-02 20:47:52104#include "content/renderer/input/main_thread_input_event_filter.h"
[email protected]da9f30a2014-06-18 19:39:04105#include "content/renderer/media/aec_dump_message_filter.h"
[email protected]f7eb0a392011-07-12 10:19:51106#include "content/renderer/media/audio_input_message_filter.h"
107#include "content/renderer/media/audio_message_filter.h"
[email protected]3958e972012-07-17 00:25:41108#include "content/renderer/media/audio_renderer_mixer_manager.h"
[email protected]d8cd8372012-03-09 10:49:51109#include "content/renderer/media/media_stream_center.h"
[email protected]a9875152013-06-22 04:03:03110#include "content/renderer/media/midi_message_filter.h"
xhwang194acae2014-11-12 22:46:33111#include "content/renderer/media/render_media_client.h"
[email protected]2329bfc72014-02-28 02:03:21112#include "content/renderer/media/renderer_gpu_video_accelerator_factories.h"
[email protected]80b161a2011-06-27 17:42:11113#include "content/renderer/media/video_capture_impl_manager.h"
[email protected]ba164c912011-06-14 22:07:34114#include "content/renderer/media/video_capture_message_filter.h"
[email protected]d7ff5fb2014-05-29 19:50:25115#include "content/renderer/net_info_helper.h"
[email protected]4761cf12012-09-12 10:37:55116#include "content/renderer/p2p/socket_dispatcher.h"
[email protected]82307f6b2014-08-07 03:30:12117#include "content/renderer/render_frame_proxy.h"
[email protected]8704f89b2011-04-15 00:30:05118#include "content/renderer/render_process_impl.h"
[email protected]310ebd6302011-10-10 19:06:28119#include "content/renderer/render_view_impl.h"
tfarina556a7232014-10-05 01:02:09120#include "content/renderer/renderer_blink_platform_impl.h"
jdduke94ae1f32015-02-05 06:27:13121#include "content/renderer/scheduler/resource_dispatch_throttler.h"
[email protected]7670d4332013-11-29 04:35:00122#include "content/renderer/service_worker/embedded_worker_dispatcher.h"
mek27c9d742015-07-16 18:30:18123#include "content/renderer/service_worker/service_worker_context_client.h"
kinuko5af4ffe2015-06-09 03:38:46124#include "content/renderer/service_worker/service_worker_context_message_filter.h"
[email protected]0ec90d522014-03-12 16:28:19125#include "content/renderer/shared_worker/embedded_shared_worker_stub.h"
chunyang.daibe874c52014-11-14 06:45:05126#include "gin/public/debug.h"
dongseong.hwang72183b02014-12-08 10:41:55127#include "gpu/GLES2/gl2extchromium.h"
danakj6f4e1e22016-04-20 03:27:34128#include "gpu/command_buffer/client/shared_memory_limits.h"
danakj870925d42016-05-03 20:07:38129#include "gpu/ipc/client/command_buffer_proxy_impl.h"
penghuang346a46f92016-03-31 21:37:52130#include "gpu/ipc/client/gpu_channel_host.h"
[email protected]46f36a492010-07-28 19:36:41131#include "ipc/ipc_channel_handle.h"
amistryd4aa70d2016-06-23 07:52:37132#include "ipc/ipc_channel_mojo.h"
[email protected]cb6037d2009-11-16 22:55:17133#include "ipc/ipc_platform_file.h"
[email protected]433df472012-03-07 20:33:39134#include "media/base/media.h"
servolk8b3b39b2015-03-03 19:08:18135#include "media/renderers/gpu_video_accelerator_factories.h"
[email protected]f68c1462014-04-24 18:17:53136#include "mojo/common/common_type_converters.h"
rockot85dce0862015-11-13 01:33:59137#include "mojo/public/cpp/bindings/strong_binding.h"
[email protected]620161e2011-03-07 18:05:26138#include "net/base/net_errors.h"
eroman9ab64842015-07-21 05:07:52139#include "net/base/port_util.h"
hablichd6a4f122015-10-28 11:34:49140#include "net/base/registry_controlled_domains/registry_controlled_domain.h"
tfarina7a4a7fd2016-01-20 14:23:44141#include "net/base/url_util.h"
rockotcef38272016-07-15 22:47:47142#include "services/shell/public/cpp/interface_provider.h"
143#include "services/shell/public/cpp/interface_registry.h"
[email protected]b48c53ad2014-02-05 21:59:18144#include "skia/ext/event_tracer_impl.h"
ssid59c969162015-07-28 13:02:58145#include "skia/ext/skia_memory_dump_provider.h"
reed6e5a72222015-08-06 20:37:16146#include "third_party/WebKit/public/platform/WebImageGenerator.h"
[email protected]89c36f82013-07-17 06:23:27147#include "third_party/WebKit/public/platform/WebString.h"
skyostil457b0a12014-09-09 10:12:07148#include "third_party/WebKit/public/platform/WebThread.h"
skyostil529caa292016-08-10 17:44:51149#include "third_party/WebKit/public/platform/scheduler/child/compositor_worker_scheduler.h"
150#include "third_party/WebKit/public/platform/scheduler/child/webthread_impl_for_worker_scheduler.h"
151#include "third_party/WebKit/public/platform/scheduler/renderer/renderer_scheduler.h"
hong.zhenga35a480d2014-12-25 17:11:09152#include "third_party/WebKit/public/web/WebCache.h"
[email protected]2255a9332013-06-17 05:12:31153#include "third_party/WebKit/public/web/WebDatabase.h"
154#include "third_party/WebKit/public/web/WebDocument.h"
155#include "third_party/WebKit/public/web/WebFrame.h"
156#include "third_party/WebKit/public/web/WebKit.h"
bashi5e843ac2016-06-17 07:44:35157#include "third_party/WebKit/public/web/WebMemoryCoordinator.h"
[email protected]2255a9332013-06-17 05:12:31158#include "third_party/WebKit/public/web/WebNetworkStateNotifier.h"
[email protected]2255a9332013-06-17 05:12:31159#include "third_party/WebKit/public/web/WebRuntimeFeatures.h"
160#include "third_party/WebKit/public/web/WebScriptController.h"
161#include "third_party/WebKit/public/web/WebSecurityPolicy.h"
[email protected]2255a9332013-06-17 05:12:31162#include "third_party/WebKit/public/web/WebView.h"
jshin0a9aa632015-04-29 18:35:29163#include "third_party/icu/source/i18n/unicode/timezone.h"
[email protected]ddbb53342014-01-06 10:59:47164#include "third_party/skia/include/core/SkGraphics.h"
[email protected]c49201a2012-05-24 11:04:57165#include "ui/base/layout.h"
[email protected]18ad6772011-09-20 21:51:32166#include "ui/base/ui_base_switches.h"
[email protected]2c62b562009-01-27 19:04:50167
[email protected]33b02992014-03-01 01:06:29168#if defined(OS_ANDROID)
169#include <cpu-features.h>
boliubee541f42015-11-05 00:52:53170#include "content/renderer/android/synchronous_compositor_filter.h"
danakj83066a32016-06-21 02:34:49171#include "content/renderer/android/synchronous_compositor_output_surface.h"
[email protected]33b02992014-03-01 01:06:29172#include "content/renderer/media/android/renderer_demuxer_android.h"
boliue81d16132016-04-26 00:54:41173#include "content/renderer/media/android/stream_texture_factory.h"
dalecurtis88af3932016-02-20 00:12:20174#include "media/base/android/media_codec_util.h"
[email protected]33b02992014-03-01 01:06:29175#endif
176
177#if defined(OS_MACOSX)
ccamerona7644752014-12-30 01:16:31178#include "base/mac/mac_util.h"
rsesek1efb3c32015-09-29 15:39:50179#include "content/renderer/theme_helper_mac.h"
[email protected]33b02992014-03-01 01:06:29180#include "content/renderer/webscrollbarbehavior_impl_mac.h"
181#endif
182
183#if defined(OS_POSIX)
184#include "ipc/ipc_channel_posix.h"
185#endif
186
[email protected]da00a2882009-03-09 17:51:19187#if defined(OS_WIN)
188#include <windows.h>
189#include <objbase.h>
190#endif
191
[email protected]22fe91d2014-08-12 17:07:12192#if defined(ENABLE_WEBRTC)
193#include "content/renderer/media/peer_connection_tracker.h"
194#include "content/renderer/media/rtc_peer_connection_handler.h"
195#include "content/renderer/media/webrtc/peer_connection_dependency_factory.h"
[email protected]22fe91d2014-08-12 17:07:12196#endif
197
chunyang.daibe874c52014-11-14 06:45:05198#ifdef ENABLE_VTUNE_JIT_INTERFACE
199#include "v8/src/third_party/vtune/v8-vtune.h"
200#endif
201
sadrul943e3b32016-08-04 18:22:59202#if defined(USE_AURA)
fsamuel6c6da0232015-11-26 05:13:46203#include "content/public/common/mojo_shell_connection.h"
danakj83066a32016-06-21 02:34:49204#include "content/renderer/mus/render_widget_mus_connection.h"
fsamuel2545ecc2015-12-05 00:44:46205#include "content/renderer/mus/render_widget_window_tree_client_factory.h"
ben6b0453d12016-07-02 04:27:19206#include "services/ui/common/gpu_service.h"
fsamuel6c6da0232015-11-26 05:13:46207#endif
208
ochangdd89a1e2016-06-08 16:39:01209#if defined(ENABLE_IPC_FUZZER)
210#include "content/common/external_ipc_dumper.h"
211#endif
212
[email protected]d293572a2013-05-23 18:50:45213using base::ThreadRestrictions;
[email protected]180ef242013-11-07 06:50:46214using blink::WebDocument;
215using blink::WebFrame;
216using blink::WebNetworkStateNotifier;
217using blink::WebRuntimeFeatures;
218using blink::WebScriptController;
219using blink::WebSecurityPolicy;
220using blink::WebString;
221using blink::WebView;
skyostil529caa292016-08-10 17:44:51222using blink::scheduler::WebThreadImplForWorkerScheduler;
[email protected]e9ff79c2012-10-19 21:31:26223
224namespace content {
initial.commit09911bf2008-07-26 23:55:29225
[email protected]42f1d7822009-07-23 18:17:55226namespace {
[email protected]da9ccfb2012-01-28 00:34:40227
avi1023d012015-12-25 02:39:14228const int64_t kInitialIdleHandlerDelayMs = 1000;
229const int64_t kLongIdleHandlerDelayMs = 30 * 1000;
[email protected]bee16aab2009-08-26 15:55:03230
jdduke94ae1f32015-02-05 06:27:13231#if defined(OS_ANDROID)
232// On Android, resource messages can each take ~1.5ms to dispatch on the browser
233// IO thread. Limiting the message rate to 3/frame at 60hz ensures that the
234// induced work takes but a fraction (~1/4) of the overall frame budget.
235const int kMaxResourceRequestsPerFlushWhenThrottled = 3;
236#else
237const int kMaxResourceRequestsPerFlushWhenThrottled = 8;
238#endif
239const double kThrottledResourceRequestFlushPeriodS = 1. / 60.;
240
[email protected]dd2c1022014-07-22 23:13:57241// Maximum allocation size allowed for image scaling filters that
242// require pre-scaling. Skia will fallback to a filter that doesn't
243// require pre-scaling if the default filter would require an
244// allocation that exceeds this limit.
245const size_t kImageCacheSingleAllocationByteLimit = 64 * 1024 * 1024;
246
danakj83066a32016-06-21 02:34:49247// Unique identifier for each output surface created.
248uint32_t g_next_output_surface_id = 1;
249
[email protected]f1a29a02011-10-06 23:08:44250// Keep the global RenderThreadImpl in a TLS slot so it is impossible to access
[email protected]f3ede412010-06-21 22:52:16251// incorrectly from the wrong thread.
[email protected]da9ccfb2012-01-28 00:34:40252base::LazyInstance<base::ThreadLocalPointer<RenderThreadImpl> >
[email protected]6de0fd1d2011-11-15 13:31:49253 lazy_tls = LAZY_INSTANCE_INITIALIZER;
[email protected]1edc16b82009-04-07 17:45:54254
hong.zhengb28b5332016-05-11 02:33:39255// v8::MemoryPressureLevel should correspond to base::MemoryPressureListener.
256static_assert(static_cast<v8::MemoryPressureLevel>(
257 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) ==
258 v8::MemoryPressureLevel::kNone, "none level not align");
259static_assert(static_cast<v8::MemoryPressureLevel>(
260 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) ==
261 v8::MemoryPressureLevel::kModerate, "moderate level not align");
262static_assert(static_cast<v8::MemoryPressureLevel>(
263 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) ==
264 v8::MemoryPressureLevel::kCritical, "critical level not align");
265
hong.zheng2e296f822016-06-29 02:47:44266// WebMemoryPressureLevel should correspond to base::MemoryPressureListener.
267static_assert(static_cast<blink::WebMemoryPressureLevel>(
268 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_NONE) ==
269 blink::WebMemoryPressureLevelNone,
270 "blink::WebMemoryPressureLevelNone not align");
271static_assert(static_cast<blink::WebMemoryPressureLevel>(
272 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_MODERATE) ==
273 blink::WebMemoryPressureLevelModerate,
274 "blink::WebMemoryPressureLevelModerate not align");
275static_assert(static_cast<blink::WebMemoryPressureLevel>(
276 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) ==
277 blink::WebMemoryPressureLevelCritical,
278 "blink::WebMemoryPressureLevelCritical not align");
279
sadrulff12c002016-01-14 20:52:20280class WebThreadForCompositor : public WebThreadImplForWorkerScheduler {
281 public:
282 explicit WebThreadForCompositor(base::Thread::Options options)
283 : WebThreadImplForWorkerScheduler("Compositor", options) {
284 Init();
285 }
286 ~WebThreadForCompositor() override {}
287
288 private:
289 // WebThreadImplForWorkerScheduler:
skyostil529caa292016-08-10 17:44:51290 std::unique_ptr<blink::scheduler::WorkerScheduler> CreateWorkerScheduler()
291 override {
292 return base::WrapUnique(
293 new blink::scheduler::CompositorWorkerScheduler(thread()));
sadrulff12c002016-01-14 20:52:20294 }
295
296 DISALLOW_COPY_AND_ASSIGN(WebThreadForCompositor);
297};
298
[email protected]b2d98762012-09-03 17:04:06299void* CreateHistogram(
[email protected]d1b8fccc2011-08-03 01:20:13300 const char *name, int min, int max, size_t buckets) {
301 if (min <= 0)
302 min = 1;
[email protected]b2d98762012-09-03 17:04:06303 std::string histogram_name;
304 RenderThreadImpl* render_thread_impl = RenderThreadImpl::current();
305 if (render_thread_impl) { // Can be null in tests.
306 histogram_name = render_thread_impl->
307 histogram_customizer()->ConvertToCustomHistogramName(name);
308 } else {
309 histogram_name = std::string(name);
310 }
[email protected]de415552013-01-23 04:12:17311 base::HistogramBase* histogram = base::Histogram::FactoryGet(
[email protected]b2d98762012-09-03 17:04:06312 histogram_name, min, max, buckets,
313 base::Histogram::kUmaTargetedHistogramFlag);
[email protected]d1b8fccc2011-08-03 01:20:13314 return histogram;
315}
316
[email protected]b2d98762012-09-03 17:04:06317void AddHistogramSample(void* hist, int sample) {
[email protected]d1b8fccc2011-08-03 01:20:13318 base::Histogram* histogram = static_cast<base::Histogram*>(hist);
319 histogram->Add(sample);
320}
321
dchengcedca5612016-04-09 01:40:15322std::unique_ptr<cc::SharedBitmap> AllocateSharedBitmapFunction(
jbauman9015c8b2014-12-11 00:49:37323 const gfx::Size& size) {
jam8a021512a2015-02-03 18:16:08324 return ChildThreadImpl::current()->shared_bitmap_manager()->
325 AllocateSharedBitmap(size);
[email protected]2541d1a2013-07-10 07:33:27326}
327
[email protected]8f13def2014-02-04 00:51:36328void EnableBlinkPlatformLogChannels(const std::string& channels) {
[email protected]6bd867b2013-07-24 22:10:20329 if (channels.empty())
330 return;
331 base::StringTokenizer t(channels, ", ");
332 while (t.GetNext())
[email protected]180ef242013-11-07 06:50:46333 blink::enableLogChannel(t.token().c_str());
[email protected]6bd867b2013-07-24 22:10:20334}
335
[email protected]df9accd2014-03-13 18:18:30336void NotifyTimezoneChangeOnThisThread() {
337 v8::Isolate* isolate = v8::Isolate::GetCurrent();
338 if (!isolate)
339 return;
340 v8::Date::DateTimeConfigurationChangeNotification(isolate);
341}
342
ben76f52b242016-06-18 05:42:48343class FrameFactoryImpl : public mojom::FrameFactory {
rockotf8fdd9b2015-12-16 22:22:35344 public:
ben76f52b242016-06-18 05:42:48345 explicit FrameFactoryImpl(mojom::FrameFactoryRequest request)
dcheng07945f632015-12-26 07:59:32346 : routing_id_highmark_(-1), binding_(this, std::move(request)) {}
rockotf8fdd9b2015-12-16 22:22:35347
ben76f52b242016-06-18 05:42:48348 private:
349 // mojom::FrameFactory:
350 void CreateFrame(int32_t frame_routing_id,
351 mojom::FrameRequest frame_request,
352 mojom::FrameHostPtr frame_host) override {
rockotf8fdd9b2015-12-16 22:22:35353 // TODO(morrita): This is for investigating http://crbug.com/415059 and
354 // should be removed once it is fixed.
355 CHECK_LT(routing_id_highmark_, frame_routing_id);
356 routing_id_highmark_ = frame_routing_id;
357
358 RenderFrameImpl* frame = RenderFrameImpl::FromRoutingID(frame_routing_id);
359 // We can receive a GetServiceProviderForFrame message for a frame not yet
360 // created due to a race between the message and a ViewMsg_New IPC that
361 // triggers creation of the RenderFrame we want.
362 if (!frame) {
ben76f52b242016-06-18 05:42:48363 RenderThreadImpl::current()->RegisterPendingFrameCreate(
364 frame_routing_id, std::move(frame_request), std::move(frame_host));
rockotf8fdd9b2015-12-16 22:22:35365 return;
366 }
367
ben76f52b242016-06-18 05:42:48368 frame->Bind(std::move(frame_request), std::move(frame_host));
rockotf8fdd9b2015-12-16 22:22:35369 }
370
371 private:
372 int32_t routing_id_highmark_;
ben76f52b242016-06-18 05:42:48373 mojo::StrongBinding<mojom::FrameFactory> binding_;
rockotf8fdd9b2015-12-16 22:22:35374};
375
ben76f52b242016-06-18 05:42:48376void CreateFrameFactory(mojom::FrameFactoryRequest request) {
377 new FrameFactoryImpl(std::move(request));
rockotf8fdd9b2015-12-16 22:22:35378}
379
mek27c9d742015-07-16 18:30:18380void SetupEmbeddedWorkerOnWorkerThread(
ben13be6082016-06-29 15:08:58381 shell::mojom::InterfaceProviderRequest request,
382 shell::mojom::InterfaceProviderPtrInfo remote_interfaces) {
mek27c9d742015-07-16 18:30:18383 ServiceWorkerContextClient* client =
384 ServiceWorkerContextClient::ThreadSpecificInstance();
385 // It is possible for client to be null if for some reason the worker died
386 // before this call made it to the worker thread. In that case just do
387 // nothing and let mojo close the connection.
388 if (!client)
389 return;
ben13be6082016-06-29 15:08:58390 client->BindInterfaceProviders(std::move(request),
391 mojo::MakeProxy(std::move(remote_interfaces)));
mek27c9d742015-07-16 18:30:18392}
393
leon.hanbe782c42016-03-22 05:05:22394class EmbeddedWorkerSetupImpl : public mojom::EmbeddedWorkerSetup {
mek27c9d742015-07-16 18:30:18395 public:
396 explicit EmbeddedWorkerSetupImpl(
leon.hanbe782c42016-03-22 05:05:22397 mojo::InterfaceRequest<mojom::EmbeddedWorkerSetup> request)
dcheng07945f632015-12-26 07:59:32398 : binding_(this, std::move(request)) {}
mek27c9d742015-07-16 18:30:18399
ben95787952016-02-10 07:46:20400 void ExchangeInterfaceProviders(
mek27c9d742015-07-16 18:30:18401 int32_t thread_id,
ben13be6082016-06-29 15:08:58402 shell::mojom::InterfaceProviderRequest request,
403 shell::mojom::InterfaceProviderPtr remote_interfaces) override {
kinukoef647412015-12-23 06:10:43404 WorkerThreadRegistry::Instance()->GetTaskRunnerFor(thread_id)->PostTask(
mek27c9d742015-07-16 18:30:18405 FROM_HERE,
ben13be6082016-06-29 15:08:58406 base::Bind(&SetupEmbeddedWorkerOnWorkerThread, base::Passed(&request),
407 base::Passed(remote_interfaces.PassInterface())));
mek27c9d742015-07-16 18:30:18408 }
409
410 private:
leon.hanbe782c42016-03-22 05:05:22411 mojo::StrongBinding<mojom::EmbeddedWorkerSetup> binding_;
mek27c9d742015-07-16 18:30:18412};
413
414void CreateEmbeddedWorkerSetup(
leon.hanbe782c42016-03-22 05:05:22415 mojo::InterfaceRequest<mojom::EmbeddedWorkerSetup> request) {
dcheng07945f632015-12-26 07:59:32416 new EmbeddedWorkerSetupImpl(std::move(request));
mek27c9d742015-07-16 18:30:18417}
418
danakjc3983552016-05-03 00:04:35419scoped_refptr<ContextProviderCommandBuffer> CreateOffscreenContext(
420 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host,
danakj0dd9e1e2016-05-11 22:15:09421 const gpu::SharedMemoryLimits& limits,
danakj9a04adc2016-05-16 22:45:07422 bool support_locking,
sunnypsd8ce1c22016-05-10 18:02:40423 command_buffer_metrics::ContextType type,
424 int32_t stream_id,
425 gpu::GpuStreamPriority stream_priority) {
danakje8ec797e2016-04-19 04:08:43426 DCHECK(gpu_channel_host);
427 // This is used to create a few different offscreen contexts:
428 // - The shared main thread context (offscreen) used by blink for canvas.
429 // - The worker context (offscreen) used for GPU raster and video decoding.
430 // This is for an offscreen context, so the default framebuffer doesn't need
431 // alpha, depth, stencil, antialiasing.
432 gpu::gles2::ContextCreationAttribHelper attributes;
433 attributes.alpha_size = -1;
434 attributes.depth_size = 0;
435 attributes.stencil_size = 0;
436 attributes.samples = 0;
437 attributes.sample_buffers = 0;
438 attributes.bind_generates_resource = false;
439 attributes.lose_context_when_out_of_memory = true;
sunnypsd8ce1c22016-05-10 18:02:40440 const bool automatic_flushes = false;
danakjc3983552016-05-03 00:04:35441 return make_scoped_refptr(new ContextProviderCommandBuffer(
sunnypsd8ce1c22016-05-10 18:02:40442 std::move(gpu_channel_host), stream_id, stream_priority,
443 gpu::kNullSurfaceHandle,
danakjba37f812016-05-03 03:58:03444 GURL("chrome://gpu/RenderThreadImpl::CreateOffscreenContext"),
pimand488e8b42016-06-30 19:06:59445 automatic_flushes, support_locking, limits, attributes, nullptr, type));
danakje8ec797e2016-04-19 04:08:43446}
447
penghuange1d86512016-07-08 18:15:00448bool IsRunningInMash() {
449 const base::CommandLine* cmdline = base::CommandLine::ForCurrentProcess();
450 return cmdline->HasSwitch(switches::kIsRunningInMash);
451}
452
[email protected]b2d98762012-09-03 17:04:06453} // namespace
454
[email protected]fdeac262014-07-10 12:52:51455// For measuring memory usage after each task. Behind a command line flag.
456class MemoryObserver : public base::MessageLoop::TaskObserver {
457 public:
458 MemoryObserver() {}
dcheng6d18e402014-10-21 12:32:52459 ~MemoryObserver() override {}
[email protected]fdeac262014-07-10 12:52:51460
dcheng6d18e402014-10-21 12:32:52461 void WillProcessTask(const base::PendingTask& pending_task) override {}
[email protected]fdeac262014-07-10 12:52:51462
dcheng6d18e402014-10-21 12:32:52463 void DidProcessTask(const base::PendingTask& pending_task) override {
asvitkinec0fb8022014-08-26 04:39:35464 LOCAL_HISTOGRAM_MEMORY_KB("Memory.RendererUsed", GetMemoryUsageKB());
[email protected]fdeac262014-07-10 12:52:51465 }
466
467 private:
468 DISALLOW_COPY_AND_ASSIGN(MemoryObserver);
469};
470
[email protected]b2d98762012-09-03 17:04:06471RenderThreadImpl::HistogramCustomizer::HistogramCustomizer() {
472 custom_histograms_.insert("V8.MemoryExternalFragmentationTotal");
473 custom_histograms_.insert("V8.MemoryHeapSampleTotalCommitted");
474 custom_histograms_.insert("V8.MemoryHeapSampleTotalUsed");
hablich4598ddc2015-11-07 18:37:28475 custom_histograms_.insert("V8.MemoryHeapUsed");
476 custom_histograms_.insert("V8.MemoryHeapCommitted");
[email protected]b2d98762012-09-03 17:04:06477}
478
479RenderThreadImpl::HistogramCustomizer::~HistogramCustomizer() {}
480
481void RenderThreadImpl::HistogramCustomizer::RenderViewNavigatedToHost(
482 const std::string& host, size_t view_count) {
avi83883c82014-12-23 00:08:49483 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
484 switches::kDisableHistogramCustomizer)) {
[email protected]9baee832012-12-10 11:07:15485 return;
486 }
[email protected]b2d98762012-09-03 17:04:06487 // Check if all RenderViews are displaying a page from the same host. If there
488 // is only one RenderView, the common host is this view's host. If there are
489 // many, check if this one shares the common host of the other
490 // RenderViews. It's ok to not detect some cases where the RenderViews share a
491 // common host. This information is only used for producing custom histograms.
492 if (view_count == 1)
493 SetCommonHost(host);
494 else if (host != common_host_)
495 SetCommonHost(std::string());
496}
497
498std::string RenderThreadImpl::HistogramCustomizer::ConvertToCustomHistogramName(
499 const char* histogram_name) const {
500 std::string name(histogram_name);
501 if (!common_host_histogram_suffix_.empty() &&
502 custom_histograms_.find(name) != custom_histograms_.end())
503 name += common_host_histogram_suffix_;
504 return name;
505}
506
507void RenderThreadImpl::HistogramCustomizer::SetCommonHost(
508 const std::string& host) {
509 if (host != common_host_) {
510 common_host_ = host;
511 common_host_histogram_suffix_ = HostToCustomHistogramSuffix(host);
[email protected]4b5340282014-07-08 11:37:34512 blink::mainThreadIsolate()->SetCreateHistogramFunction(CreateHistogram);
[email protected]b2d98762012-09-03 17:04:06513 }
514}
515
hablichd6a4f122015-10-28 11:34:49516std::string RenderThreadImpl::HistogramCustomizer::HostToCustomHistogramSuffix(
517 const std::string& host) {
518 if (host == "mail.google.com")
519 return ".gmail";
520 if (host == "docs.google.com" || host == "drive.google.com")
521 return ".docs";
522 if (host == "plus.google.com")
523 return ".plus";
524 if (host == "inbox.google.com")
525 return ".inbox";
hpayerdd4def052016-02-06 19:06:10526 if (host == "calendar.google.com")
527 return ".calendar";
hablichd6a4f122015-10-28 11:34:49528 if (host == "www.youtube.com")
529 return ".youtube";
530 if (IsAlexaTop10NonGoogleSite(host))
531 return ".top10";
532
533 return std::string();
534}
535
536bool RenderThreadImpl::HistogramCustomizer::IsAlexaTop10NonGoogleSite(
537 const std::string& host) {
538 // The Top10 sites have different TLD and/or subdomains depending on the
539 // localization.
540 if (host == "sina.com.cn")
541 return true;
542
543 std::string sanitized_host =
544 net::registry_controlled_domains::GetDomainAndRegistry(
545 host, net::registry_controlled_domains::INCLUDE_PRIVATE_REGISTRIES);
546
547 if (sanitized_host == "facebook.com")
548 return true;
549 if (sanitized_host == "baidu.com")
550 return true;
551 if (sanitized_host == "qq.com")
552 return true;
553 if (sanitized_host == "twitter.com")
554 return true;
555 if (sanitized_host == "taobao.com")
556 return true;
557 if (sanitized_host == "live.com")
558 return true;
559
560 if (!sanitized_host.empty()) {
561 std::vector<base::StringPiece> host_tokens = base::SplitStringPiece(
562 sanitized_host, ".", base::TRIM_WHITESPACE, base::SPLIT_WANT_NONEMPTY);
563
564 if (host_tokens.size() >= 2) {
565 if ((host_tokens[0] == "yahoo") || (host_tokens[0] == "amazon") ||
566 (host_tokens[0] == "wikipedia")) {
567 return true;
568 }
569 }
570 }
571 return false;
572}
573
leon.han2c0f9f12015-07-11 02:01:19574// static
575RenderThreadImpl* RenderThreadImpl::Create(
576 const InProcessChildThreadParams& params) {
skyostil529caa292016-08-10 17:44:51577 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler =
578 blink::scheduler::RendererScheduler::Create();
jam75c44222016-03-23 05:34:24579 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter;
580 return new RenderThreadImpl(
581 params, std::move(renderer_scheduler), test_task_counter);
leon.han2c0f9f12015-07-11 02:01:19582}
583
584// static
585RenderThreadImpl* RenderThreadImpl::Create(
dchengcedca5612016-04-09 01:40:15586 std::unique_ptr<base::MessageLoop> main_message_loop,
skyostil529caa292016-08-10 17:44:51587 std::unique_ptr<blink::scheduler::RendererScheduler> renderer_scheduler) {
dcheng07945f632015-12-26 07:59:32588 return new RenderThreadImpl(std::move(main_message_loop),
589 std::move(renderer_scheduler));
leon.han2c0f9f12015-07-11 02:01:19590}
591
[email protected]f1a29a02011-10-06 23:08:44592RenderThreadImpl* RenderThreadImpl::current() {
[email protected]526476902011-10-06 20:34:06593 return lazy_tls.Pointer()->Get();
594}
595
leon.han2c0f9f12015-07-11 02:01:19596RenderThreadImpl::RenderThreadImpl(
597 const InProcessChildThreadParams& params,
skyostil529caa292016-08-10 17:44:51598 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler,
jam75c44222016-03-23 05:34:24599 scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue)
morritac6238ab2015-03-18 01:48:29600 : ChildThreadImpl(Options::Builder()
prashant.nfad657e2016-06-01 07:52:17601 .InBrowserProcess(params)
602 .UseMojoChannel(true)
rockotcef38272016-07-15 22:47:47603 .AutoStartMojoShellConnection(false)
ben5be0b9132016-08-03 00:17:18604 .ConnectToBrowser(true)
prashant.nfad657e2016-06-01 07:52:17605 .Build()),
dcheng07945f632015-12-26 07:59:32606 renderer_scheduler_(std::move(scheduler)),
prashant.nfad657e2016-06-01 07:52:17607 categorized_worker_pool_(new CategorizedWorkerPool()) {
jam75c44222016-03-23 05:34:24608 Init(resource_task_queue);
morritac6238ab2015-03-18 01:48:29609}
610
[email protected]42f1d7822009-07-23 18:17:55611// When we run plugins in process, we actually run them on the render thread,
612// which means that we need to make the render thread pump UI events.
haraken53f081d2014-11-11 01:03:40613RenderThreadImpl::RenderThreadImpl(
dchengcedca5612016-04-09 01:40:15614 std::unique_ptr<base::MessageLoop> main_message_loop,
skyostil529caa292016-08-10 17:44:51615 std::unique_ptr<blink::scheduler::RendererScheduler> scheduler)
rockotcef38272016-07-15 22:47:47616 : ChildThreadImpl(Options::Builder()
617 .UseMojoChannel(true)
618 .AutoStartMojoShellConnection(false)
ben5be0b9132016-08-03 00:17:18619 .ConnectToBrowser(true)
rockotcef38272016-07-15 22:47:47620 .Build()),
dcheng07945f632015-12-26 07:59:32621 renderer_scheduler_(std::move(scheduler)),
622 main_message_loop_(std::move(main_message_loop)),
prashant.nfad657e2016-06-01 07:52:17623 categorized_worker_pool_(new CategorizedWorkerPool()) {
jam75c44222016-03-23 05:34:24624 scoped_refptr<base::SingleThreadTaskRunner> test_task_counter;
625 Init(test_task_counter);
haraken53f081d2014-11-11 01:03:40626}
627
jam75c44222016-03-23 05:34:24628void RenderThreadImpl::Init(
629 scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue) {
fdorayf6d86242015-10-08 16:49:53630 TRACE_EVENT0("startup", "RenderThreadImpl::Init");
[email protected]a872ea1f2010-08-11 04:45:33631
ssidb2e3ece2015-02-09 16:02:20632 base::trace_event::TraceLog::GetInstance()->SetThreadSortIndex(
[email protected]91a2aea2013-07-08 23:14:39633 base::PlatformThread::CurrentId(),
634 kTraceEventRendererMainThreadSortIndex);
635
haibinluc643d33c2016-06-03 02:22:34636#if defined(USE_EXTERNAL_POPUP_MENU)
sievers9dff72052015-11-16 18:35:57637 // On Mac and Android Java UI, the select popups are rendered by the browser.
[email protected]180ef242013-11-07 06:50:46638 blink::WebView::setUseExternalPopupMenus(true);
[email protected]53c607c2011-03-21 23:19:04639#endif
640
[email protected]94f9a0f682009-06-15 18:30:30641 lazy_tls.Pointer()->Set(this);
[email protected]b3e83de2012-02-07 03:33:28642
[email protected]06c694d2012-02-01 22:26:16643 // Register this object as the main thread.
644 ChildProcess::current()->set_main_thread(this);
645
sadrul943e3b32016-08-04 18:22:59646#if defined(USE_AURA)
sadrul62c31132016-08-11 04:13:17647 if (IsRunningInMash()) {
sadrul9e0bb1d2016-08-04 20:18:39648 gpu_service_ =
649 ui::GpuService::Initialize(GetMojoShellConnection()->GetConnector());
sadrul62c31132016-08-11 04:13:17650 }
penghuange1d86512016-07-08 18:15:00651#endif
652
jam75c44222016-03-23 05:34:24653 InitializeWebKit(resource_task_queue);
654
[email protected]31f87132010-04-21 23:36:21655 // In single process the single process is all there is.
[email protected]26e82322014-01-20 14:18:22656 webkit_shared_timer_suspended_ = false;
[email protected]bee16aab2009-08-26 15:55:03657 widget_count_ = 0;
658 hidden_widget_count_ = 0;
[email protected]6593ae12011-11-14 12:09:44659 idle_notification_delay_in_ms_ = kInitialIdleHandlerDelayMs;
[email protected]1784b2f2011-11-24 10:53:48660 idle_notifications_to_skip_ = 0;
[email protected]8d86fce2009-02-26 23:37:55661
[email protected]d5b2fdf2013-06-05 09:36:55662 appcache_dispatcher_.reset(
[email protected]a8cb3b73b2013-08-12 05:50:50663 new AppCacheDispatcher(Get(), new AppCacheFrontendImpl()));
[email protected]1910fe82012-05-10 00:04:10664 dom_storage_dispatcher_.reset(new DomStorageDispatcher());
[email protected]1e8d91e2013-06-27 00:12:43665 main_thread_indexed_db_dispatcher_.reset(new IndexedDBDispatcher(
666 thread_safe_sender()));
jsbellabadb9b2015-03-23 21:03:44667 main_thread_cache_storage_dispatcher_.reset(
jsbell279efb42015-03-31 17:02:46668 new CacheStorageDispatcher(thread_safe_sender()));
[email protected]7670d4332013-11-29 04:35:00669 embedded_worker_dispatcher_.reset(new EmbeddedWorkerDispatcher());
[email protected]dd9241932010-02-24 19:23:13670
jdduke94ae1f32015-02-05 06:27:13671 // Note: This may reorder messages from the ResourceDispatcher with respect to
672 // other subsystems.
673 resource_dispatch_throttler_.reset(new ResourceDispatchThrottler(
674 static_cast<RenderThread*>(this), renderer_scheduler_.get(),
675 base::TimeDelta::FromSecondsD(kThrottledResourceRequestFlushPeriodS),
676 kMaxResourceRequestsPerFlushWhenThrottled));
677 resource_dispatcher()->set_message_sender(resource_dispatch_throttler_.get());
678
penghuange1d86512016-07-08 18:15:00679 media_stream_center_ = nullptr;
[email protected]d8cd8372012-03-09 10:49:51680
thestig529ad8a2016-07-08 20:30:12681 blob_message_filter_ = new BlobMessageFilter(GetFileThreadTaskRunner());
dmurph1fb98482016-03-30 21:14:26682 AddFilter(blob_message_filter_.get());
[email protected]017022b2009-07-27 23:06:34683 db_message_filter_ = new DBMessageFilter();
684 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13685
[email protected]45048072014-01-14 13:51:29686 vc_manager_.reset(new VideoCaptureImplManager());
[email protected]c9c43a02013-12-17 08:59:54687 AddFilter(vc_manager_->video_capture_message_filter());
688
fsamuel6c1dfeb2014-12-18 19:21:33689 browser_plugin_manager_.reset(new BrowserPluginManager());
690 AddObserver(browser_plugin_manager_.get());
691
[email protected]4761cf12012-09-12 10:37:55692#if defined(ENABLE_WEBRTC)
[email protected]af089972013-01-10 04:04:40693 peer_connection_tracker_.reset(new PeerConnectionTracker());
694 AddObserver(peer_connection_tracker_.get());
695
thestig529ad8a2016-07-08 20:30:12696 p2p_socket_dispatcher_ = new P2PSocketDispatcher(GetIOTaskRunner().get());
[email protected]fc72bb12013-06-02 21:13:46697 AddFilter(p2p_socket_dispatcher_.get());
[email protected]921480f62013-07-20 03:42:57698
ivocadd54f0d2015-12-18 23:17:05699 peer_connection_factory_.reset(
700 new PeerConnectionDependencyFactory(p2p_socket_dispatcher_.get()));
701
skyostil2d3b5bd2015-05-27 15:40:59702 aec_dump_message_filter_ = new AecDumpMessageFilter(
thestig529ad8a2016-07-08 20:30:12703 GetIOTaskRunner(), message_loop()->task_runner());
ivocadd54f0d2015-12-18 23:17:05704
[email protected]da9f30a2014-06-18 19:39:04705 AddFilter(aec_dump_message_filter_.get());
706
[email protected]4761cf12012-09-12 10:37:55707#endif // defined(ENABLE_WEBRTC)
[email protected]e25f4d72011-06-08 20:58:46708
thestig529ad8a2016-07-08 20:30:12709 audio_input_message_filter_ = new AudioInputMessageFilter(GetIOTaskRunner());
[email protected]f7eb0a392011-07-12 10:19:51710 AddFilter(audio_input_message_filter_.get());
711
thestig529ad8a2016-07-08 20:30:12712 audio_message_filter_ = new AudioMessageFilter(GetIOTaskRunner());
[email protected]f7eb0a392011-07-12 10:19:51713 AddFilter(audio_message_filter_.get());
714
thestig529ad8a2016-07-08 20:30:12715 midi_message_filter_ = new MidiMessageFilter(GetIOTaskRunner());
[email protected]a9875152013-06-22 04:03:03716 AddFilter(midi_message_filter_.get());
717
[email protected]ce944c62013-11-19 16:34:36718 AddFilter((new IndexedDBMessageFilter(thread_safe_sender()))->GetFilter());
[email protected]31bfae72011-12-16 02:04:38719
jsbellabadb9b2015-03-23 21:03:44720 AddFilter((new CacheStorageMessageFilter(thread_safe_sender()))->GetFilter());
721
kinuko5af4ffe2015-06-09 03:38:46722 AddFilter((new ServiceWorkerContextMessageFilter())->GetFilter());
[email protected]ddbb53342014-01-06 10:59:47723
sadrul943e3b32016-08-04 18:22:59724#if defined(USE_AURA)
rockotcef38272016-07-15 22:47:47725 if (base::CommandLine::ForCurrentProcess()->HasSwitch(
726 switches::kUseMusInRenderer)) {
727 CreateRenderWidgetWindowTreeClientFactory(GetMojoShellConnection());
728 }
729#endif
730
731 // Must be called before RenderThreadStarted() below.
732 StartMojoShellConnection();
733
[email protected]e9ff79c2012-10-19 21:31:26734 GetContentClient()->renderer()->RenderThreadStarted();
[email protected]6779aa12011-03-29 17:32:24735
[email protected]b48c53ad2014-02-05 21:59:18736 InitSkiaEventTracer();
ssid59c969162015-07-28 13:02:58737 base::trace_event::MemoryDumpManager::GetInstance()->RegisterDumpProvider(
primiano186d6bfe2015-10-30 13:21:40738 skia::SkiaMemoryDumpProvider::GetInstance(), "Skia", nullptr);
[email protected]b48c53ad2014-02-05 21:59:18739
avi83883c82014-12-23 00:08:49740 const base::CommandLine& command_line =
741 *base::CommandLine::ForCurrentProcess();
[email protected]e54ab492012-06-12 19:40:01742
ochangdd89a1e2016-06-08 16:39:01743#if defined(ENABLE_IPC_FUZZER)
744 if (command_line.HasSwitch(switches::kIpcDumpDirectory)) {
745 base::FilePath dump_directory =
746 command_line.GetSwitchValuePath(switches::kIpcDumpDirectory);
747 IPC::ChannelProxy::OutgoingMessageFilter* filter =
748 LoadExternalIPCDumper(dump_directory);
749 GetChannel()->set_outgoing_message_filter(filter);
750 }
751#endif
752
jbroman0d302162015-09-05 05:46:23753 cc::SetClientNameForMetrics("Renderer");
loysoa6edaaff2015-05-25 03:26:44754
loyso65c93c602015-08-11 05:15:57755 is_threaded_animation_enabled_ =
756 !command_line.HasSwitch(cc::switches::kDisableThreadedAnimation);
757
reveman91a0a872014-11-04 03:40:32758 is_zero_copy_enabled_ = command_line.HasSwitch(switches::kEnableZeroCopy);
ericrk1d17f752015-10-20 03:03:07759 is_partial_raster_enabled_ =
dongseong.hwang23db47f2016-03-08 07:50:02760 !command_line.HasSwitch(switches::kDisablePartialRaster);
ccameronc7fcd132015-11-03 20:14:31761 is_gpu_memory_buffer_compositor_resources_enabled_ = command_line.HasSwitch(
762 switches::kEnableGpuMemoryBufferCompositorResources);
[email protected]a23530d2014-03-11 06:04:14763
jame1f453c2016-03-21 15:51:34764#if defined(OS_MACOSX)
thakis114a1ef2016-04-26 16:02:59765 base::ScopedCFTypeRef<CFStringRef> key(
766 base::SysUTF8ToCFStringRef("NSScrollViewRubberbanding"));
767 Boolean key_exists = false;
768 Boolean value = CFPreferencesGetAppBooleanValue(
769 key, kCFPreferencesCurrentApplication, &key_exists);
770 is_elastic_overscroll_enabled_ = !key_exists || value;
ccamerona7644752014-12-30 01:16:31771#else
772 is_elastic_overscroll_enabled_ = false;
773#endif
774
danakje6eb185c2015-02-27 23:52:05775 std::string image_texture_target_string =
dcastagna7f2f7192015-06-16 18:44:35776 command_line.GetSwitchValueASCII(switches::kContentImageTextureTarget);
ericrk9151705c2016-07-26 19:53:16777 buffer_to_texture_target_map_ =
778 cc::StringToBufferToTextureTargetMap(image_texture_target_string);
reveman44b807f12014-11-26 02:44:50779
[email protected]a23530d2014-03-11 06:04:14780 if (command_line.HasSwitch(switches::kDisableLCDText)) {
781 is_lcd_text_enabled_ = false;
782 } else if (command_line.HasSwitch(switches::kEnableLCDText)) {
783 is_lcd_text_enabled_ = true;
784 } else {
785#if defined(OS_ANDROID)
786 is_lcd_text_enabled_ = false;
787#else
788 is_lcd_text_enabled_ = true;
789#endif
790 }
791
[email protected]b8d82c22014-03-31 20:12:46792 is_gpu_rasterization_enabled_ =
793 command_line.HasSwitch(switches::kEnableGpuRasterization);
794 is_gpu_rasterization_forced_ =
795 command_line.HasSwitch(switches::kForceGpuRasterization);
sunnypsd8ce1c22016-05-10 18:02:40796 is_async_worker_context_enabled_ =
797 command_line.HasSwitch(switches::kEnableGpuAsyncWorkerContext);
[email protected]a23530d2014-03-11 06:04:14798
senorblancob60ba952015-01-27 19:12:36799 if (command_line.HasSwitch(switches::kGpuRasterizationMSAASampleCount)) {
800 std::string string_value = command_line.GetSwitchValueASCII(
801 switches::kGpuRasterizationMSAASampleCount);
802 bool parsed_msaa_sample_count =
803 base::StringToInt(string_value, &gpu_rasterization_msaa_sample_count_);
804 DCHECK(parsed_msaa_sample_count) << string_value;
805 DCHECK_GE(gpu_rasterization_msaa_sample_count_, 0);
806 } else {
senorblanco2a5b0e12015-08-14 21:55:37807 gpu_rasterization_msaa_sample_count_ = -1;
senorblancob60ba952015-01-27 19:12:36808 }
809
[email protected]b242b142014-05-07 14:48:49810 if (command_line.HasSwitch(switches::kDisableDistanceFieldText)) {
811 is_distance_field_text_enabled_ = false;
812 } else if (command_line.HasSwitch(switches::kEnableDistanceFieldText)) {
813 is_distance_field_text_enabled_ = true;
814 } else {
815 is_distance_field_text_enabled_ = false;
816 }
817
[email protected]433df472012-03-07 20:33:39818 // Note that under Linux, the media library will normally already have
819 // been initialized by the Zygote before this instance became a Renderer.
chcunninghamfd11b3c2015-06-09 02:09:42820 media::InitializeMediaLibrary();
[email protected]433df472012-03-07 20:33:39821
dalecurtis88af3932016-02-20 00:12:20822#if defined(OS_ANDROID)
823 if (!command_line.HasSwitch(switches::kDisableAcceleratedVideoDecode) &&
824 media::MediaCodecUtil::IsMediaCodecAvailable()) {
825 media::EnablePlatformDecoderSupport();
826 }
827#endif
828
[email protected]5071cb22013-07-10 02:19:06829 memory_pressure_listener_.reset(new base::MemoryPressureListener(
hong.zhengb28b5332016-05-11 02:33:39830 base::Bind(&RenderThreadImpl::OnMemoryPressure, base::Unretained(this)),
831 base::Bind(&RenderThreadImpl::OnSyncMemoryPressure,
832 base::Unretained(this))));
[email protected]5071cb22013-07-10 02:19:06833
bashi77f19b32016-07-19 11:19:43834 if (memory_coordinator::IsEnabled()) {
835 // TODO(bashi): Revisit how to manage the lifetime of
836 // ChildMemoryCoordinatorImpl.
837 // https://codereview.chromium.org/2094583002/#msg52
838 memory_coordinator::mojom::MemoryCoordinatorHandlePtr parent_coordinator;
839 GetRemoteInterfaces()->GetInterface(mojo::GetProxy(&parent_coordinator));
840 memory_coordinator_.reset(
841 new memory_coordinator::ChildMemoryCoordinatorImpl(
842 std::move(parent_coordinator)));
843 }
844
danakj16275d4c2015-06-11 19:23:51845 int num_raster_threads = 0;
846 std::string string_value =
847 command_line.GetSwitchValueASCII(switches::kNumRasterThreads);
848 bool parsed_num_raster_threads =
849 base::StringToInt(string_value, &num_raster_threads);
850 DCHECK(parsed_num_raster_threads) << string_value;
851 DCHECK_GT(num_raster_threads, 0);
vmiura78b69282015-02-14 00:01:17852
vmpstr6c0b5112016-04-25 22:12:14853 // TODO(vmpstr): If the flag sticks, we should clean it up and always have
854 // image decode tasks.
vmpstr9dd810a2016-01-19 21:16:10855 are_image_decode_tasks_enabled_ = true;
halliwell4b3deee2014-12-19 19:57:49856
prashant.nfad657e2016-06-01 07:52:17857 categorized_worker_pool_->Start(num_raster_threads);
[email protected]1e1d1e12014-01-17 16:14:29858
boliu9760e212015-06-23 22:49:06859 // TODO(boliu): In single process, browser main loop should set up the
860 // discardable memory manager, and should skip this if kSingleProcess.
861 // See crbug.com/503724.
862 base::DiscardableMemoryAllocator::SetInstance(
863 ChildThreadImpl::discardable_shared_memory_manager());
revemancb5a66af2014-10-25 00:34:39864
bend1dd50f52016-06-26 22:10:48865 GetContentClient()->renderer()->ExposeInterfacesToBrowser(
866 GetInterfaceRegistry());
rockot229fb28e2016-06-16 04:46:16867
bend1dd50f52016-06-26 22:10:48868 GetInterfaceRegistry()->AddInterface(base::Bind(CreateFrameFactory));
869 GetInterfaceRegistry()->AddInterface(base::Bind(CreateEmbeddedWorkerSetup));
fsamuel6c6da0232015-11-26 05:13:46870
bend1dd50f52016-06-26 22:10:48871 GetRemoteInterfaces()->GetInterface(
jamc912ca32016-02-24 20:17:31872 mojo::GetProxy(&storage_partition_service_));
hajimehoshi69093272016-05-13 08:30:58873
874 is_renderer_suspended_ = false;
initial.commit09911bf2008-07-26 23:55:29875}
876
[email protected]f1a29a02011-10-06 23:08:44877RenderThreadImpl::~RenderThreadImpl() {
[email protected]ce79d8512013-04-22 22:44:41878}
879
880void RenderThreadImpl::Shutdown() {
[email protected]1223d6ef2011-03-28 16:47:50881 FOR_EACH_OBSERVER(
tyoshino832a58a2016-04-18 08:14:08882 RenderThreadObserver, observers_, OnRenderProcessShutdown());
[email protected]1223d6ef2011-03-28 16:47:50883
[email protected]fdeac262014-07-10 12:52:51884 if (memory_observer_) {
885 message_loop()->RemoveTaskObserver(memory_observer_.get());
886 memory_observer_.reset();
887 }
888
[email protected]12cbfda32010-01-30 01:04:25889 // Wait for all databases to be closed.
tfarina556a7232014-10-05 01:02:09890 if (blink_platform_impl_) {
michaelnb15028542015-10-29 03:33:24891 // Crash the process if they fail to close after a generous amount of time.
892 bool all_closed = blink_platform_impl_->web_database_observer_impl()
893 ->WaitForAllDatabasesToClose(base::TimeDelta::FromSeconds(60));
894 CHECK(all_closed);
[email protected]1406f892013-11-12 19:38:19895 }
[email protected]12cbfda32010-01-30 01:04:25896
[email protected]8d86fce2009-02-26 23:37:55897 // Shutdown in reverse of the initialization order.
[email protected]fc72bb12013-06-02 21:13:46898 if (devtools_agent_message_filter_.get()) {
[email protected]b146d6d2012-09-11 10:20:05899 RemoveFilter(devtools_agent_message_filter_.get());
penghuange1d86512016-07-08 18:15:00900 devtools_agent_message_filter_ = nullptr;
[email protected]b146d6d2012-09-11 10:20:05901 }
[email protected]82622452011-07-22 09:57:20902
[email protected]f7eb0a392011-07-12 10:19:51903 RemoveFilter(audio_input_message_filter_.get());
penghuange1d86512016-07-08 18:15:00904 audio_input_message_filter_ = nullptr;
[email protected]f7eb0a392011-07-12 10:19:51905
[email protected]45048072014-01-14 13:51:29906#if defined(ENABLE_WEBRTC)
[email protected]51c7985e2014-05-23 22:00:08907 RTCPeerConnectionHandler::DestructAllHandlers();
tommi6c33f252014-11-24 12:29:51908 // |peer_connection_factory_| cannot be deleted until after the main message
909 // loop has been destroyed. This is because there may be pending tasks that
910 // hold on to objects produced by the PC factory that depend on threads owned
911 // by the PC factory. Once those tasks have been freed, the factory can be
912 // deleted.
[email protected]45048072014-01-14 13:51:29913#endif
[email protected]80b161a2011-06-27 17:42:11914 RemoveFilter(vc_manager_->video_capture_message_filter());
[email protected]45048072014-01-14 13:51:29915 vc_manager_.reset();
[email protected]80b161a2011-06-27 17:42:11916
[email protected]017022b2009-07-27 23:06:34917 RemoveFilter(db_message_filter_.get());
penghuange1d86512016-07-08 18:15:00918 db_message_filter_ = nullptr;
[email protected]12cbfda32010-01-30 01:04:25919
[email protected]c6a7b862010-08-20 22:19:38920 // Shutdown the file thread if it's running.
[email protected]59383c782013-04-17 16:43:27921 if (file_thread_)
[email protected]c6a7b862010-08-20 22:19:38922 file_thread_->Stop();
923
simonhonga7e3ac42014-11-11 20:50:22924 if (compositor_message_filter_.get()) {
925 RemoveFilter(compositor_message_filter_.get());
penghuange1d86512016-07-08 18:15:00926 compositor_message_filter_ = nullptr;
[email protected]23d2b7e2012-08-14 00:34:09927 }
[email protected]1842fe22012-08-13 23:24:35928
boliubee541f42015-11-05 00:52:53929#if defined(OS_ANDROID)
930 if (sync_compositor_message_filter_) {
931 RemoveFilter(sync_compositor_message_filter_.get());
932 sync_compositor_message_filter_ = nullptr;
933 }
siva.gunturi5d4feb052015-11-15 16:15:31934 stream_texture_factory_ = nullptr;
boliubee541f42015-11-05 00:52:53935#endif
936
[email protected]b4ef7742014-03-19 06:12:56937 media_thread_.reset();
dalecurtis3ba0480e2014-09-09 20:21:30938
sadrulff12c002016-01-14 20:52:20939 blink_platform_impl_->SetCompositorThread(nullptr);
vollick1050cc62015-12-03 07:04:54940
dcastagna7f45dada2015-10-19 20:17:35941 compositor_thread_.reset();
942
dalecurtis3ba0480e2014-09-09 20:21:30943 // AudioMessageFilter may be accessed on |media_thread_|, so shutdown after.
944 RemoveFilter(audio_message_filter_.get());
penghuange1d86512016-07-08 18:15:00945 audio_message_filter_ = nullptr;
dalecurtis3ba0480e2014-09-09 20:21:30946
prashant.nfad657e2016-06-01 07:52:17947 categorized_worker_pool_->Shutdown();
reveman34b7a1522015-03-23 20:27:47948
jdduke691dd572014-12-02 20:47:52949 main_input_callback_.Cancel();
[email protected]b0255682013-06-07 21:24:08950 input_handler_manager_.reset();
[email protected]cadac622013-06-11 16:46:36951 if (input_event_filter_.get()) {
[email protected]b0255682013-06-07 21:24:08952 RemoveFilter(input_event_filter_.get());
penghuange1d86512016-07-08 18:15:00953 input_event_filter_ = nullptr;
[email protected]a9fb30aa2011-10-06 06:58:46954 }
955
[email protected]51f82b02014-05-27 00:02:57956 // RemoveEmbeddedWorkerRoute may be called while deleting
957 // EmbeddedWorkerDispatcher. So it must be deleted before deleting
958 // RenderThreadImpl.
959 embedded_worker_dispatcher_.reset();
960
[email protected]c5739b9e2013-11-07 06:30:22961 // Ramp down IDB before we ramp down WebKit (and V8), since IDB classes might
962 // hold pointers to V8 objects (e.g., via pending requests).
963 main_thread_indexed_db_dispatcher_.reset();
964
penghuange1d86512016-07-08 18:15:00965 main_thread_compositor_task_runner_ = nullptr;
skyostil457b0a12014-09-09 10:12:07966
dcastagna09bd6b32016-02-01 21:54:28967 gpu_factories_.clear();
dcastagna7f45dada2015-10-19 20:17:35968
vmiuraeeb91772015-04-15 01:00:51969 // Context providers must be released prior to destroying the GPU channel.
revemand180dfc32015-09-24 00:19:43970 shared_worker_context_provider_ = nullptr;
piman765e5282015-05-08 03:43:01971 shared_main_thread_contexts_ = nullptr;
vmiuraeeb91772015-04-15 01:00:51972
haraken53f081d2014-11-11 01:03:40973 if (gpu_channel_.get())
974 gpu_channel_->DestroyChannel();
[email protected]8fd8de92008-08-12 23:50:30975
reveman413413fb2016-01-14 00:43:07976 ChildThreadImpl::Shutdown();
977
fdoray2df4a9e2016-07-18 23:47:16978 // Shut down the message loop (if provided when the RenderThreadImpl was
979 // constructed) and the renderer scheduler before shutting down Blink. This
980 // prevents a scenario where a pending task in the message loop accesses Blink
981 // objects after Blink shuts down.
skyostila37c2a72016-06-29 17:30:07982 renderer_scheduler_->SetRAILModeObserver(nullptr);
rmcilroy168f3dc2015-03-17 13:09:15983 renderer_scheduler_->Shutdown();
haraken534ab852016-01-29 03:52:56984 if (main_message_loop_)
fdoray2df4a9e2016-07-18 23:47:16985 base::RunLoop().RunUntilIdle();
haraken534ab852016-01-29 03:52:56986
rickyzf2c93062015-09-15 09:10:57987 if (blink_platform_impl_) {
988 blink_platform_impl_->Shutdown();
haraken534ab852016-01-29 03:52:56989 // This must be at the very end of the shutdown sequence.
990 // blink::shutdown() must be called after all strong references from
991 // Chromium to Blink are cleared.
haraken53f081d2014-11-11 01:03:40992 blink::shutdown();
rickyzf2c93062015-09-15 09:10:57993 }
haraken53f081d2014-11-11 01:03:40994
haraken534ab852016-01-29 03:52:56995 // Delay shutting down DiscardableSharedMemoryManager until blink::shutdown
996 // is complete, because blink::shutdown destructs Blink Resources and they
997 // may try to unlock their underlying discardable memory.
998 ChildThreadImpl::ShutdownDiscardableSharedMemoryManager();
999
1000 // The message loop must be cleared after shutting down
1001 // the DiscardableSharedMemoryManager, which needs to send messages
1002 // to the browser process.
1003 main_message_loop_.reset();
1004
penghuange1d86512016-07-08 18:15:001005 lazy_tls.Pointer()->Set(nullptr);
initial.commit09911bf2008-07-26 23:55:291006}
1007
[email protected]f1a29a02011-10-06 23:08:441008bool RenderThreadImpl::Send(IPC::Message* msg) {
changwan6ed4d432016-05-19 22:03:541009 // There are cases where we want to pump asynchronous messages while waiting
1010 // synchronously for the replies to the message to be sent here. However, this
1011 // may create an opportunity for re-entrancy into WebKit and other subsystems,
1012 // so we need to take care to disable callbacks, timers, and pending network
1013 // loads that could trigger such callbacks.
[email protected]38b592902011-04-16 02:08:421014 bool pumping_events = false;
[email protected]80fc08c52010-03-09 07:43:501015 if (msg->is_sync()) {
1016 if (msg->is_caller_pumping_messages()) {
1017 pumping_events = true;
[email protected]80fc08c52010-03-09 07:43:501018 }
[email protected]c1f50aa2010-02-18 03:46:571019 }
1020
[email protected]c1f50aa2010-02-18 03:46:571021 if (pumping_events) {
jdduked73ae5412015-05-11 20:07:161022 renderer_scheduler_->SuspendTimerQueue();
changwan6ed4d432016-05-19 22:03:541023 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:571024 }
1025
jam8a021512a2015-02-03 18:16:081026 bool rv = ChildThreadImpl::Send(msg);
[email protected]c1f50aa2010-02-18 03:46:571027
1028 if (pumping_events) {
changwan6ed4d432016-05-19 22:03:541029 WebView::didExitModalLoop();
jdduked73ae5412015-05-11 20:07:161030 renderer_scheduler_->ResumeTimerQueue();
[email protected]c1f50aa2010-02-18 03:46:571031 }
1032
1033 return rv;
1034}
1035
[email protected]f1a29a02011-10-06 23:08:441036IPC::SyncChannel* RenderThreadImpl::GetChannel() {
[email protected]526476902011-10-06 20:34:061037 return channel();
1038}
1039
[email protected]f1a29a02011-10-06 23:08:441040std::string RenderThreadImpl::GetLocale() {
[email protected]526476902011-10-06 20:34:061041 // The browser process should have passed the locale to the renderer via the
[email protected]dfd53652012-10-25 00:20:021042 // --lang command line flag.
avi83883c82014-12-23 00:08:491043 const base::CommandLine& parsed_command_line =
1044 *base::CommandLine::ForCurrentProcess();
[email protected]526476902011-10-06 20:34:061045 const std::string& lang =
1046 parsed_command_line.GetSwitchValueASCII(switches::kLang);
[email protected]dfd53652012-10-25 00:20:021047 DCHECK(!lang.empty());
[email protected]526476902011-10-06 20:34:061048 return lang;
1049}
1050
[email protected]07bb6332012-01-21 01:07:571051IPC::SyncMessageFilter* RenderThreadImpl::GetSyncMessageFilter() {
1052 return sync_message_filter();
1053}
1054
skyostil12262cf2015-05-21 14:49:311055scoped_refptr<base::SingleThreadTaskRunner>
thestig529ad8a2016-07-08 20:30:121056RenderThreadImpl::GetIOTaskRunner() {
skyostil12262cf2015-05-21 14:49:311057 return ChildProcess::current()->io_task_runner();
[email protected]96191d52012-05-17 01:37:111058}
1059
avi1023d012015-12-25 02:39:141060void RenderThreadImpl::AddRoute(int32_t routing_id, IPC::Listener* listener) {
jam8a021512a2015-02-03 18:16:081061 ChildThreadImpl::GetRouter()->AddRoute(routing_id, listener);
ben76f52b242016-06-18 05:42:481062 auto it = pending_frame_creates_.find(routing_id);
1063 if (it == pending_frame_creates_.end())
rockotf8fdd9b2015-12-16 22:22:351064 return;
1065
1066 RenderFrameImpl* frame = RenderFrameImpl::FromRoutingID(routing_id);
1067 if (!frame)
1068 return;
1069
ben76f52b242016-06-18 05:42:481070 scoped_refptr<PendingFrameCreate> create(it->second);
1071 frame->Bind(it->second->TakeFrameRequest(), it->second->TakeFrameHost());
1072 pending_frame_creates_.erase(it);
[email protected]c1f50aa2010-02-18 03:46:571073}
1074
avi1023d012015-12-25 02:39:141075void RenderThreadImpl::RemoveRoute(int32_t routing_id) {
jam8a021512a2015-02-03 18:16:081076 ChildThreadImpl::GetRouter()->RemoveRoute(routing_id);
[email protected]c1f50aa2010-02-18 03:46:571077}
[email protected]0ec90d522014-03-12 16:28:191078
avi1023d012015-12-25 02:39:141079void RenderThreadImpl::AddEmbeddedWorkerRoute(int32_t routing_id,
[email protected]a620b132014-04-30 22:39:171080 IPC::Listener* listener) {
[email protected]48e52e42014-03-20 06:58:071081 AddRoute(routing_id, listener);
1082 if (devtools_agent_message_filter_.get()) {
[email protected]a620b132014-04-30 22:39:171083 devtools_agent_message_filter_->AddEmbeddedWorkerRouteOnMainThread(
[email protected]48e52e42014-03-20 06:58:071084 routing_id);
1085 }
1086}
1087
avi1023d012015-12-25 02:39:141088void RenderThreadImpl::RemoveEmbeddedWorkerRoute(int32_t routing_id) {
[email protected]48e52e42014-03-20 06:58:071089 RemoveRoute(routing_id);
1090 if (devtools_agent_message_filter_.get()) {
[email protected]a620b132014-04-30 22:39:171091 devtools_agent_message_filter_->RemoveEmbeddedWorkerRouteOnMainThread(
[email protected]48e52e42014-03-20 06:58:071092 routing_id);
1093 }
1094}
1095
ben76f52b242016-06-18 05:42:481096void RenderThreadImpl::RegisterPendingFrameCreate(
rockotf8fdd9b2015-12-16 22:22:351097 int routing_id,
ben76f52b242016-06-18 05:42:481098 mojom::FrameRequest frame_request,
1099 mojom::FrameHostPtr frame_host) {
1100 std::pair<PendingFrameCreateMap::iterator, bool> result =
1101 pending_frame_creates_.insert(std::make_pair(
rockotf8fdd9b2015-12-16 22:22:351102 routing_id,
ben76f52b242016-06-18 05:42:481103 make_scoped_refptr(new PendingFrameCreate(
1104 routing_id, std::move(frame_request), std::move(frame_host)))));
rockotf8fdd9b2015-12-16 22:22:351105 CHECK(result.second) << "Inserting a duplicate item.";
1106}
1107
leon.han06e55662016-03-26 17:19:421108mojom::StoragePartitionService* RenderThreadImpl::GetStoragePartitionService() {
jamc912ca32016-02-24 20:17:311109 return storage_partition_service_.get();
1110}
1111
[email protected]77fc9b92011-10-15 16:20:371112int RenderThreadImpl::GenerateRoutingID() {
1113 int routing_id = MSG_ROUTING_NONE;
1114 Send(new ViewHostMsg_GenerateRoutingID(&routing_id));
1115 return routing_id;
1116}
1117
[email protected]74122042014-04-25 00:07:301118void RenderThreadImpl::AddFilter(IPC::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:551119 channel()->AddFilter(filter);
1120}
1121
[email protected]74122042014-04-25 00:07:301122void RenderThreadImpl::RemoveFilter(IPC::MessageFilter* filter) {
[email protected]42f1d7822009-07-23 18:17:551123 channel()->RemoveFilter(filter);
1124}
1125
tyoshino832a58a2016-04-18 08:14:081126void RenderThreadImpl::AddObserver(RenderThreadObserver* observer) {
[email protected]526476902011-10-06 20:34:061127 observers_.AddObserver(observer);
1128}
1129
tyoshino832a58a2016-04-18 08:14:081130void RenderThreadImpl::RemoveObserver(RenderThreadObserver* observer) {
[email protected]526476902011-10-06 20:34:061131 observers_.RemoveObserver(observer);
1132}
1133
[email protected]359dfa32011-10-12 01:10:151134void RenderThreadImpl::SetResourceDispatcherDelegate(
[email protected]e9ff79c2012-10-19 21:31:261135 ResourceDispatcherDelegate* delegate) {
[email protected]359dfa32011-10-12 01:10:151136 resource_dispatcher()->set_delegate(delegate);
1137}
1138
vollick1050cc62015-12-03 07:04:541139void RenderThreadImpl::InitializeCompositorThread() {
boliu66024c62016-04-20 04:00:411140 base::Thread::Options options;
vollick1050cc62015-12-03 07:04:541141#if defined(OS_ANDROID)
boliu66024c62016-04-20 04:00:411142 options.priority = base::ThreadPriority::DISPLAY;
vollick1050cc62015-12-03 07:04:541143#endif
boliu66024c62016-04-20 04:00:411144 compositor_thread_.reset(new WebThreadForCompositor(options));
1145 blink_platform_impl_->SetCompositorThread(compositor_thread_.get());
1146 compositor_task_runner_ = compositor_thread_->GetTaskRunner();
1147 compositor_task_runner_->PostTask(
1148 FROM_HERE,
1149 base::Bind(base::IgnoreResult(&ThreadRestrictions::SetIOAllowed), false));
vollick1050cc62015-12-03 07:04:541150
boliude5b75b2016-03-11 07:02:141151 SynchronousInputHandlerProxyClient* synchronous_input_handler_proxy_client =
1152 nullptr;
vollick1050cc62015-12-03 07:04:541153#if defined(OS_ANDROID)
boliu66024c62016-04-20 04:00:411154 if (GetContentClient()->UsingSynchronousCompositing()) {
vollick1050cc62015-12-03 07:04:541155 sync_compositor_message_filter_ =
1156 new SynchronousCompositorFilter(compositor_task_runner_);
1157 AddFilter(sync_compositor_message_filter_.get());
boliude5b75b2016-03-11 07:02:141158 synchronous_input_handler_proxy_client =
1159 sync_compositor_message_filter_.get();
vollick1050cc62015-12-03 07:04:541160 }
1161#endif
boliub2768db2016-06-03 23:35:161162 scoped_refptr<InputEventFilter> compositor_input_event_filter(
1163 new InputEventFilter(main_input_callback_.callback(),
1164 main_thread_compositor_task_runner_,
1165 compositor_task_runner_));
1166 InputHandlerManagerClient* input_handler_manager_client =
1167 compositor_input_event_filter.get();
1168 input_event_filter_ = compositor_input_event_filter;
vollick1050cc62015-12-03 07:04:541169 input_handler_manager_.reset(new InputHandlerManager(
1170 compositor_task_runner_, input_handler_manager_client,
boliude5b75b2016-03-11 07:02:141171 synchronous_input_handler_proxy_client, renderer_scheduler_.get()));
vollick1050cc62015-12-03 07:04:541172}
1173
jam75c44222016-03-23 05:34:241174void RenderThreadImpl::InitializeWebKit(
1175 scoped_refptr<base::SingleThreadTaskRunner>& resource_task_queue) {
1176 DCHECK(!blink_platform_impl_);
[email protected]d1b8fccc2011-08-03 01:20:131177
avi83883c82014-12-23 00:08:491178 const base::CommandLine& command_line =
1179 *base::CommandLine::ForCurrentProcess();
chunyang.daibe874c52014-11-14 06:45:051180
1181#ifdef ENABLE_VTUNE_JIT_INTERFACE
1182 if (command_line.HasSwitch(switches::kEnableVtune))
1183 gin::Debug::SetJitCodeEventHandler(vTune::GetVtuneCodeEventHandler());
1184#endif
1185
rmcilroy4073ae12015-01-08 13:08:101186 SetRuntimeFeaturesDefaultsAndUpdateFromArgs(command_line);
1187
sammcbebeb602016-04-06 06:42:551188 blink_platform_impl_.reset(new RendererBlinkPlatformImpl(
1189 renderer_scheduler_.get(),
bend1dd50f52016-06-26 22:10:481190 GetRemoteInterfaces()->GetWeakPtr()));
tfarina556a7232014-10-05 01:02:091191 blink::initialize(blink_platform_impl_.get());
[email protected]d1b8fccc2011-08-03 01:20:131192
[email protected]4b5340282014-07-08 11:37:341193 v8::Isolate* isolate = blink::mainThreadIsolate();
[email protected]4b5340282014-07-08 11:37:341194 isolate->SetCreateHistogramFunction(CreateHistogram);
1195 isolate->SetAddHistogramSampleFunction(AddHistogramSample);
skyostila37c2a72016-06-29 17:30:071196 renderer_scheduler_->SetRAILModeObserver(this);
[email protected]4b5340282014-07-08 11:37:341197
rmcilroy321f924d2014-11-06 00:56:001198 main_thread_compositor_task_runner_ =
danakj6e3bf8012014-12-16 18:27:531199 renderer_scheduler_->CompositorTaskRunner();
skyostilc30aa402014-10-10 13:49:091200
jdduke691dd572014-12-02 20:47:521201 main_input_callback_.Reset(
1202 base::Bind(base::IgnoreResult(&RenderThreadImpl::OnMessageReceived),
1203 base::Unretained(this)));
1204
jam75c44222016-03-23 05:34:241205 scoped_refptr<base::SingleThreadTaskRunner> resource_task_queue2;
1206 if (resource_task_queue) {
1207 resource_task_queue2 = resource_task_queue;
1208 } else {
1209 resource_task_queue2 = renderer_scheduler_->LoadingTaskRunner();
1210 }
1211 // Add a filter that forces resource messages to be dispatched via a
1212 // particular task runner.
1213 scoped_refptr<ResourceSchedulingFilter> filter(
1214 new ResourceSchedulingFilter(
1215 resource_task_queue2, resource_dispatcher()));
1216 channel()->AddFilter(filter.get());
1217 resource_dispatcher()->SetResourceSchedulingFilter(filter);
1218
1219 // The ChildResourceMessageFilter and the ResourceDispatcher need to use the
1220 // same queue to ensure tasks are executed in the expected order.
1221 child_resource_message_filter()->SetMainThreadTaskRunner(
1222 resource_task_queue2);
1223 resource_dispatcher()->SetMainThreadTaskRunner(resource_task_queue2);
1224
darin815c4cb2016-08-08 21:05:311225 websocket_message_filter()->SetLoadingTaskRunner(
1226 renderer_scheduler_->LoadingTaskRunner());
1227
khushalsagara5b15d72016-01-07 06:15:401228 if (!command_line.HasSwitch(switches::kDisableThreadedCompositing) &&
1229 !command_line.HasSwitch(switches::kUseRemoteCompositing))
vollick1050cc62015-12-03 07:04:541230 InitializeCompositorThread();
[email protected]a9fb30aa2011-10-06 06:58:461231
jdduke691dd572014-12-02 20:47:521232 if (!input_event_filter_.get()) {
1233 // Always provide an input event filter implementation to ensure consistent
1234 // input event scheduling and prioritization.
1235 // TODO(jdduke): Merge InputEventFilter, InputHandlerManager and
1236 // MainThreadInputEventFilter, crbug.com/436057.
1237 input_event_filter_ = new MainThreadInputEventFilter(
1238 main_input_callback_.callback(), main_thread_compositor_task_runner_);
1239 }
1240 AddFilter(input_event_filter_.get());
1241
skyostil2d3b5bd2015-05-27 15:40:591242 scoped_refptr<base::SingleThreadTaskRunner> compositor_impl_side_task_runner;
vollick1050cc62015-12-03 07:04:541243 if (compositor_task_runner_)
skyostil2d3b5bd2015-05-27 15:40:591244 compositor_impl_side_task_runner = compositor_task_runner_;
[email protected]7f1f63f2013-03-07 06:07:291245 else
skyostil2d3b5bd2015-05-27 15:40:591246 compositor_impl_side_task_runner = base::ThreadTaskRunnerHandle::Get();
[email protected]1842fe22012-08-13 23:24:351247
simonhonga7e3ac42014-11-11 20:50:221248 compositor_message_filter_ = new CompositorForwardingMessageFilter(
skyostil2d3b5bd2015-05-27 15:40:591249 compositor_impl_side_task_runner.get());
simonhonga7e3ac42014-11-11 20:50:221250 AddFilter(compositor_message_filter_.get());
[email protected]1842fe22012-08-13 23:24:351251
[email protected]58436a12012-03-21 17:10:261252 RenderThreadImpl::RegisterSchemes();
1253
[email protected]8f13def2014-02-04 00:51:361254 EnableBlinkPlatformLogChannels(
1255 command_line.GetSwitchValueASCII(switches::kBlinkPlatformLogChannels));
[email protected]d1b8fccc2011-08-03 01:20:131256
xhwang194acae2014-11-12 22:46:331257 RenderMediaClient::Initialize();
1258
[email protected]b146d6d2012-09-11 10:20:051259 devtools_agent_message_filter_ = new DevToolsAgentFilter();
1260 AddFilter(devtools_agent_message_filter_.get());
1261
alph666f80f2014-12-08 15:52:111262 v8_sampling_profiler_.reset(new V8SamplingProfiler());
1263
ulan4a385192015-11-11 10:59:181264 if (GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden()) {
[email protected]1784b2f2011-11-24 10:53:481265 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
ulan4a385192015-11-11 10:59:181266 } else {
1267 // If we do not track widget visibility, then assume conservatively that
1268 // the isolate is in background. This reduces memory usage.
1269 isolate->IsolateInBackgroundNotification();
1270 }
[email protected]2541d1a2013-07-10 07:33:271271
jdduke73220f02015-09-04 17:03:511272 renderer_scheduler_->SetTimerQueueSuspensionWhenBackgroundedEnabled(
1273 GetContentClient()
1274 ->renderer()
1275 ->AllowTimerSuspensionWhenProcessBackgrounded());
1276
jbauman9015c8b2014-12-11 00:49:371277 cc_blink::SetSharedBitmapAllocationFunction(AllocateSharedBitmapFunction);
[email protected]d72493152014-01-08 17:37:451278
reedccf98c52014-10-03 16:40:381279 SkGraphics::SetResourceCacheSingleAllocationByteLimit(
[email protected]dd2c1022014-07-22 23:13:571280 kImageCacheSingleAllocationByteLimit);
1281
reed6e5a72222015-08-06 20:37:161282 // Hook up blink's codecs so skia can call them
1283 SkGraphics::SetImageGeneratorFromEncodedFactory(
1284 blink::WebImageGenerator::create);
1285
[email protected]fdeac262014-07-10 12:52:511286 if (command_line.HasSwitch(switches::kMemoryMetrics)) {
1287 memory_observer_.reset(new MemoryObserver());
1288 message_loop()->AddTaskObserver(memory_observer_.get());
1289 }
paritosh.in7e30c902015-04-15 17:04:071290
1291 if (command_line.HasSwitch(switches::kExplicitlyAllowedPorts)) {
1292 std::string allowed_ports =
1293 command_line.GetSwitchValueASCII(switches::kExplicitlyAllowedPorts);
1294 net::SetExplicitlyAllowedPorts(allowed_ports);
1295 }
[email protected]d1b8fccc2011-08-03 01:20:131296}
1297
[email protected]58436a12012-03-21 17:10:261298void RenderThreadImpl::RegisterSchemes() {
mkwst8e94fb32015-05-20 05:05:141299 // chrome:
1300 WebString chrome_scheme(base::ASCIIToUTF16(kChromeUIScheme));
1301 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(chrome_scheme);
1302 WebSecurityPolicy::registerURLSchemeAsNotAllowingJavascriptURLs(
1303 chrome_scheme);
1304 WebSecurityPolicy::registerURLSchemeAsSecure(chrome_scheme);
1305 WebSecurityPolicy::registerURLSchemeAsCORSEnabled(chrome_scheme);
1306
1307 // chrome-devtools:
1308 WebString devtools_scheme(base::ASCIIToUTF16(kChromeDevToolsScheme));
1309 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(devtools_scheme);
meacerce6b66032016-06-02 20:56:051310
1311 // view-source:
1312 WebString view_source_scheme(base::ASCIIToUTF16(kViewSourceScheme));
1313 WebSecurityPolicy::registerURLSchemeAsDisplayIsolated(view_source_scheme);
[email protected]58436a12012-03-21 17:10:261314}
1315
[email protected]df9accd2014-03-13 18:18:301316void RenderThreadImpl::NotifyTimezoneChange() {
1317 NotifyTimezoneChangeOnThisThread();
1318 RenderThread::Get()->PostTaskToAllWebWorkers(
1319 base::Bind(&NotifyTimezoneChangeOnThisThread));
1320}
1321
[email protected]e6e30ac2014-01-13 21:24:391322void RenderThreadImpl::RecordAction(const base::UserMetricsAction& action) {
[email protected]97880c82013-12-04 07:09:211323 Send(new ViewHostMsg_UserMetricsRecordAction(action.str_));
1324}
1325
1326void RenderThreadImpl::RecordComputedAction(const std::string& action) {
[email protected]526476902011-10-06 20:34:061327 Send(new ViewHostMsg_UserMetricsRecordAction(action));
1328}
1329
dchengcedca5612016-04-09 01:40:151330std::unique_ptr<base::SharedMemory>
1331RenderThreadImpl::HostAllocateSharedMemoryBuffer(size_t size) {
erikchen4e01cbe2016-05-25 22:00:051332 return ChildThreadImpl::AllocateSharedMemory(size, thread_safe_sender(),
1333 nullptr);
[email protected]00614a82011-10-07 22:39:311334}
1335
jbauman2f5c1942014-12-06 03:28:241336cc::SharedBitmapManager* RenderThreadImpl::GetSharedBitmapManager() {
1337 return shared_bitmap_manager();
1338}
1339
[email protected]f1a29a02011-10-06 23:08:441340void RenderThreadImpl::RegisterExtension(v8::Extension* extension) {
[email protected]526476902011-10-06 20:34:061341 WebScriptController::registerExtension(extension);
[email protected]526476902011-10-06 20:34:061342}
1343
avi1023d012015-12-25 02:39:141344void RenderThreadImpl::ScheduleIdleHandler(int64_t initial_delay_ms) {
[email protected]6593ae12011-11-14 12:09:441345 idle_notification_delay_in_ms_ = initial_delay_ms;
[email protected]526476902011-10-06 20:34:061346 idle_timer_.Stop();
1347 idle_timer_.Start(FROM_HERE,
[email protected]6593ae12011-11-14 12:09:441348 base::TimeDelta::FromMilliseconds(initial_delay_ms),
[email protected]f1a29a02011-10-06 23:08:441349 this, &RenderThreadImpl::IdleHandler);
[email protected]526476902011-10-06 20:34:061350}
1351
[email protected]f1a29a02011-10-06 23:08:441352void RenderThreadImpl::IdleHandler() {
[email protected]1784b2f2011-11-24 10:53:481353 bool run_in_foreground_tab = (widget_count_ > hidden_widget_count_) &&
[email protected]e9ff79c2012-10-19 21:31:261354 GetContentClient()->renderer()->
[email protected]1784b2f2011-11-24 10:53:481355 RunIdleHandlerWhenWidgetsHidden();
1356 if (run_in_foreground_tab) {
jochen5a32aaf2014-09-26 20:37:431357 if (idle_notifications_to_skip_ > 0) {
1358 --idle_notifications_to_skip_;
1359 } else {
ssid0603ca9f2015-06-09 16:48:081360 ReleaseFreeMemory();
jochen5a32aaf2014-09-26 20:37:431361 }
1362 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
[email protected]1784b2f2011-11-24 10:53:481363 return;
1364 }
[email protected]237a14852012-04-28 02:56:381365
ssid0603ca9f2015-06-09 16:48:081366 ReleaseFreeMemory();
[email protected]526476902011-10-06 20:34:061367
[email protected]26e82322014-01-20 14:18:221368 // Continue the idle timer if the webkit shared timer is not suspended or
1369 // something is left to do.
1370 bool continue_timer = !webkit_shared_timer_suspended_;
1371
sullivancd45a3e2014-09-19 14:39:181372 // Schedule next invocation. When the tab is originally hidden, an invocation
1373 // is scheduled for kInitialIdleHandlerDelayMs in
1374 // RenderThreadImpl::WidgetHidden in order to race to a minimal heap.
1375 // After that, idle calls can be much less frequent, so run at a maximum of
1376 // once every kLongIdleHandlerDelayMs.
[email protected]6593ae12011-11-14 12:09:441377 // Dampen the delay using the algorithm (if delay is in seconds):
[email protected]526476902011-10-06 20:34:061378 // delay = delay + 1 / (delay + 2)
1379 // Using floor(delay) has a dampening effect such as:
sullivancd45a3e2014-09-19 14:39:181380 // 30s, 30, 30, 31, 31, 31, 31, 32, 32, ...
[email protected]6593ae12011-11-14 12:09:441381 // If the delay is in milliseconds, the above formula is equivalent to:
1382 // delay_ms / 1000 = delay_ms / 1000 + 1 / (delay_ms / 1000 + 2)
1383 // which is equivalent to
1384 // delay_ms = delay_ms + 1000*1000 / (delay_ms + 2000).
[email protected]26e82322014-01-20 14:18:221385 if (continue_timer) {
sullivancd45a3e2014-09-19 14:39:181386 ScheduleIdleHandler(
1387 std::max(kLongIdleHandlerDelayMs,
1388 idle_notification_delay_in_ms_ +
1389 1000000 / (idle_notification_delay_in_ms_ + 2000)));
[email protected]26e82322014-01-20 14:18:221390
1391 } else {
1392 idle_timer_.Stop();
1393 }
[email protected]526476902011-10-06 20:34:061394
tyoshino832a58a2016-04-18 08:14:081395 FOR_EACH_OBSERVER(RenderThreadObserver, observers_, IdleNotification());
[email protected]526476902011-10-06 20:34:061396}
1397
avi1023d012015-12-25 02:39:141398int64_t RenderThreadImpl::GetIdleNotificationDelayInMs() const {
[email protected]6593ae12011-11-14 12:09:441399 return idle_notification_delay_in_ms_;
[email protected]526476902011-10-06 20:34:061400}
1401
[email protected]6593ae12011-11-14 12:09:441402void RenderThreadImpl::SetIdleNotificationDelayInMs(
avi1023d012015-12-25 02:39:141403 int64_t idle_notification_delay_in_ms) {
[email protected]6593ae12011-11-14 12:09:441404 idle_notification_delay_in_ms_ = idle_notification_delay_in_ms;
[email protected]4a7d6392011-09-19 20:55:081405}
1406
[email protected]a1a7ff32012-07-19 14:03:511407void RenderThreadImpl::UpdateHistograms(int sequence_number) {
1408 child_histogram_message_filter()->SendHistograms(sequence_number);
1409}
1410
[email protected]5b18406362013-06-18 18:46:431411int RenderThreadImpl::PostTaskToAllWebWorkers(const base::Closure& closure) {
kinukoef647412015-12-23 06:10:431412 return WorkerThreadRegistry::Instance()->PostTaskToAllThreads(closure);
[email protected]a9bd323d2013-06-17 20:27:561413}
1414
[email protected]b02f5902012-12-19 07:33:001415bool RenderThreadImpl::ResolveProxy(const GURL& url, std::string* proxy_list) {
1416 bool result = false;
1417 Send(new ViewHostMsg_ResolveProxy(url, &result, proxy_list));
1418 return result;
1419}
1420
[email protected]1784b2f2011-11-24 10:53:481421void RenderThreadImpl::PostponeIdleNotification() {
1422 idle_notifications_to_skip_ = 2;
1423}
1424
dcastagna7f45dada2015-10-19 20:17:351425media::GpuVideoAcceleratorFactories* RenderThreadImpl::GetGpuFactories() {
[email protected]3bb8bb32013-07-11 13:13:031426 DCHECK(IsMainThread());
1427
dcastagna09bd6b32016-02-01 21:54:281428 if (!gpu_factories_.empty()) {
1429 scoped_refptr<ContextProviderCommandBuffer> shared_context_provider =
1430 gpu_factories_.back()->ContextProviderMainThread();
1431 if (shared_context_provider) {
1432 cc::ContextProvider::ScopedContextLock lock(
1433 shared_context_provider.get());
1434 if (lock.ContextGL()->GetGraphicsResetStatusKHR() == GL_NO_ERROR) {
1435 return gpu_factories_.back();
1436 } else {
1437 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner =
1438 GetMediaThreadTaskRunner();
1439 media_task_runner->PostTask(
1440 FROM_HERE,
1441 base::Bind(
1442 base::IgnoreResult(
1443 &RendererGpuVideoAcceleratorFactories::CheckContextLost),
1444 base::Unretained(gpu_factories_.back())));
1445 }
1446 }
1447 }
dcastagna7f45dada2015-10-19 20:17:351448
tobiasjsca238b3b2015-06-24 22:53:541449 const base::CommandLine* cmd_line = base::CommandLine::ForCurrentProcess();
1450
danakj0b4b94e32016-05-10 22:33:011451 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host =
sadrul6d310fa2016-08-04 02:12:161452 EstablishGpuChannelSync();
danakj0b4b94e32016-05-10 22:33:011453 if (!gpu_channel_host)
1454 return nullptr;
danakj0dd9e1e2016-05-11 22:15:091455 // This context is only used to create textures and mailbox them, so
1456 // use lower limits than the default.
1457 gpu::SharedMemoryLimits limits = gpu::SharedMemoryLimits::ForMailboxContext();
danakj9a04adc2016-05-16 22:45:071458 bool support_locking = true;
danakj0b4b94e32016-05-10 22:33:011459 scoped_refptr<ContextProviderCommandBuffer> media_context_provider =
danakj9a04adc2016-05-16 22:45:071460 CreateOffscreenContext(gpu_channel_host, limits, support_locking,
danakj0dd9e1e2016-05-11 22:15:091461 command_buffer_metrics::RENDER_WORKER_CONTEXT,
1462 gpu::GPU_STREAM_DEFAULT,
1463 gpu::GpuStreamPriority::NORMAL);
danakj0b4b94e32016-05-10 22:33:011464 if (!media_context_provider->BindToCurrentThread())
1465 return nullptr;
danakj0b4b94e32016-05-10 22:33:011466
acolwellb4034942014-08-28 15:42:431467 scoped_refptr<base::SingleThreadTaskRunner> media_task_runner =
1468 GetMediaThreadTaskRunner();
danakj0b4b94e32016-05-10 22:33:011469 const bool enable_video_accelerator =
1470 !cmd_line->HasSwitch(switches::kDisableAcceleratedVideoDecode);
1471 const bool enable_gpu_memory_buffer_video_frames =
dcastagna43c3a86a2016-02-02 21:16:381472#if defined(OS_MACOSX) || defined(OS_LINUX)
dcastagnaa82ed96a2016-05-12 02:52:471473 !cmd_line->HasSwitch(switches::kDisableGpuMemoryBufferVideoFrames) &&
1474 !cmd_line->HasSwitch(switches::kDisableGpuCompositing) &&
1475 !gpu_channel_host->gpu_info().software_rendering;
dcastagnab65e6072015-09-05 07:18:421476#else
danakj0b4b94e32016-05-10 22:33:011477 cmd_line->HasSwitch(switches::kEnableGpuMemoryBufferVideoFrames);
dcastagnab65e6072015-09-05 07:18:421478#endif
dcastagna5077d6d2016-01-27 00:07:051479
danakj0b4b94e32016-05-10 22:33:011480 gpu_factories_.push_back(RendererGpuVideoAcceleratorFactories::Create(
1481 std::move(gpu_channel_host), base::ThreadTaskRunnerHandle::Get(),
1482 media_task_runner, std::move(media_context_provider),
ericrk9151705c2016-07-26 19:53:161483 enable_gpu_memory_buffer_video_frames, buffer_to_texture_target_map_,
danakj0b4b94e32016-05-10 22:33:011484 enable_video_accelerator));
1485 return gpu_factories_.back();
[email protected]3bb8bb32013-07-11 13:13:031486}
1487
danakj615aac22016-04-29 21:28:161488scoped_refptr<ContextProviderCommandBuffer>
[email protected]f10dc472013-09-27 03:31:591489RenderThreadImpl::SharedMainThreadContextProvider() {
[email protected]e06e1122013-03-15 17:12:381490 DCHECK(IsMainThread());
danakje8ec797e2016-04-19 04:08:431491 if (shared_main_thread_contexts_ &&
1492 shared_main_thread_contexts_->ContextGL()->GetGraphicsResetStatusKHR() ==
1493 GL_NO_ERROR)
1494 return shared_main_thread_contexts_;
1495
sadrul6d310fa2016-08-04 02:12:161496 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(
1497 EstablishGpuChannelSync());
danakje8ec797e2016-04-19 04:08:431498 if (!gpu_channel_host) {
1499 shared_main_thread_contexts_ = nullptr;
1500 return nullptr;
[email protected]c29b7ff2013-03-06 03:51:041501 }
danakje8ec797e2016-04-19 04:08:431502
danakj9a04adc2016-05-16 22:45:071503 bool support_locking = false;
danakjc3983552016-05-03 00:04:351504 shared_main_thread_contexts_ = CreateOffscreenContext(
danakj9a04adc2016-05-16 22:45:071505 std::move(gpu_channel_host), gpu::SharedMemoryLimits(), support_locking,
sunnypsd8ce1c22016-05-10 18:02:401506 command_buffer_metrics::RENDERER_MAINTHREAD_CONTEXT,
1507 gpu::GPU_STREAM_DEFAULT, gpu::GpuStreamPriority::NORMAL);
danakje8ec797e2016-04-19 04:08:431508 if (!shared_main_thread_contexts_->BindToCurrentThread())
1509 shared_main_thread_contexts_ = nullptr;
[email protected]f10dc472013-09-27 03:31:591510 return shared_main_thread_contexts_;
[email protected]c29b7ff2013-03-06 03:51:041511}
1512
siva.gunturi5d4feb052015-11-15 16:15:311513#if defined(OS_ANDROID)
boliuf8753bf62016-02-11 20:09:421514
siva.gunturi5d4feb052015-11-15 16:15:311515scoped_refptr<StreamTextureFactory> RenderThreadImpl::GetStreamTexureFactory() {
1516 DCHECK(IsMainThread());
boliu30f1b262016-04-19 00:12:331517 if (!stream_texture_factory_.get() ||
1518 stream_texture_factory_->ContextGL()->GetGraphicsResetStatusKHR() !=
1519 GL_NO_ERROR) {
piman9fc22f32016-05-02 22:21:221520 scoped_refptr<ContextProviderCommandBuffer> shared_context_provider =
1521 SharedMainThreadContextProvider();
1522 if (!shared_context_provider) {
penghuange1d86512016-07-08 18:15:001523 stream_texture_factory_ = nullptr;
1524 return nullptr;
siva.gunturi5d4feb052015-11-15 16:15:311525 }
piman9fc22f32016-05-02 22:21:221526 DCHECK(shared_context_provider->GetCommandBufferProxy());
1527 DCHECK(shared_context_provider->GetCommandBufferProxy()->channel());
1528 stream_texture_factory_ =
1529 StreamTextureFactory::Create(std::move(shared_context_provider));
siva.gunturi5d4feb052015-11-15 16:15:311530 }
1531 return stream_texture_factory_;
1532}
boliuf8753bf62016-02-11 20:09:421533
1534bool RenderThreadImpl::EnableStreamTextureCopy() {
boliu30f1b262016-04-19 00:12:331535 return sync_compositor_message_filter_.get();
boliuf8753bf62016-02-11 20:09:421536}
1537
siva.gunturi5d4feb052015-11-15 16:15:311538#endif
1539
[email protected]e9ff79c2012-10-19 21:31:261540AudioRendererMixerManager* RenderThreadImpl::GetAudioRendererMixerManager() {
[email protected]59383c782013-04-17 16:43:271541 if (!audio_renderer_mixer_manager_) {
olka7a4679392016-05-27 15:32:581542 audio_renderer_mixer_manager_ = AudioRendererMixerManager::Create();
[email protected]3958e972012-07-17 00:25:411543 }
1544
1545 return audio_renderer_mixer_manager_.get();
1546}
1547
[email protected]73429ca2014-03-06 06:07:471548base::WaitableEvent* RenderThreadImpl::GetShutdownEvent() {
1549 return ChildProcess::current()->GetShutDownEvent();
1550}
1551
[email protected]4a7d6392011-09-19 20:55:081552#if defined(OS_WIN)
[email protected]feb32372012-11-21 01:14:561553void RenderThreadImpl::PreCacheFontCharacters(const LOGFONT& log_font,
[email protected]fcf75d42013-12-03 20:11:261554 const base::string16& str) {
avia7c3f51c2015-09-16 00:30:311555 Send(new RenderProcessHostMsg_PreCacheFontCharacters(log_font, str));
[email protected]feb32372012-11-21 01:14:561556}
1557
[email protected]4a7d6392011-09-19 20:55:081558#endif // OS_WIN
1559
danakj6e3bf8012014-12-16 18:27:531560bool RenderThreadImpl::IsGpuRasterizationForced() {
1561 return is_gpu_rasterization_forced_;
1562}
1563
1564bool RenderThreadImpl::IsGpuRasterizationEnabled() {
1565 return is_gpu_rasterization_enabled_;
1566}
1567
sunnypsd8ce1c22016-05-10 18:02:401568bool RenderThreadImpl::IsAsyncWorkerContextEnabled() {
1569 return is_async_worker_context_enabled_;
1570}
1571
senorblancob60ba952015-01-27 19:12:361572int RenderThreadImpl::GetGpuRasterizationMSAASampleCount() {
1573 return gpu_rasterization_msaa_sample_count_;
1574}
1575
danakj6e3bf8012014-12-16 18:27:531576bool RenderThreadImpl::IsLcdTextEnabled() {
1577 return is_lcd_text_enabled_;
1578}
1579
1580bool RenderThreadImpl::IsDistanceFieldTextEnabled() {
1581 return is_distance_field_text_enabled_;
1582}
1583
1584bool RenderThreadImpl::IsZeroCopyEnabled() {
1585 return is_zero_copy_enabled_;
1586}
1587
ericrk1d17f752015-10-20 03:03:071588bool RenderThreadImpl::IsPartialRasterEnabled() {
1589 return is_partial_raster_enabled_;
jbroman5f7f71932015-08-18 16:24:461590}
1591
ccameronc7fcd132015-11-03 20:14:311592bool RenderThreadImpl::IsGpuMemoryBufferCompositorResourcesEnabled() {
1593 return is_gpu_memory_buffer_compositor_resources_enabled_;
1594}
1595
ccamerona7644752014-12-30 01:16:311596bool RenderThreadImpl::IsElasticOverscrollEnabled() {
1597 return is_elastic_overscroll_enabled_;
1598}
1599
ericrk9151705c2016-07-26 19:53:161600const cc::BufferToTextureTargetMap&
1601RenderThreadImpl::GetBufferToTextureTargetMap() {
1602 return buffer_to_texture_target_map_;
danakj6e3bf8012014-12-16 18:27:531603}
vmiura9084b342015-02-03 22:19:571604
danakj6e3bf8012014-12-16 18:27:531605scoped_refptr<base::SingleThreadTaskRunner>
1606RenderThreadImpl::GetCompositorMainThreadTaskRunner() {
1607 return main_thread_compositor_task_runner_;
1608}
1609
1610scoped_refptr<base::SingleThreadTaskRunner>
1611RenderThreadImpl::GetCompositorImplThreadTaskRunner() {
skyostil2d3b5bd2015-05-27 15:40:591612 return compositor_task_runner_;
danakj6e3bf8012014-12-16 18:27:531613}
1614
1615gpu::GpuMemoryBufferManager* RenderThreadImpl::GetGpuMemoryBufferManager() {
1616 return gpu_memory_buffer_manager();
1617}
1618
skyostil529caa292016-08-10 17:44:511619blink::scheduler::RendererScheduler* RenderThreadImpl::GetRendererScheduler() {
danakj6e3bf8012014-12-16 18:27:531620 return renderer_scheduler_.get();
1621}
1622
dchengcedca5612016-04-09 01:40:151623std::unique_ptr<cc::BeginFrameSource>
danakj6e3bf8012014-12-16 18:27:531624RenderThreadImpl::CreateExternalBeginFrameSource(int routing_id) {
dchengcedca5612016-04-09 01:40:151625 return base::WrapUnique(new CompositorExternalBeginFrameSource(
danakj6e3bf8012014-12-16 18:27:531626 compositor_message_filter_.get(), sync_message_filter(), routing_id));
1627}
1628
nyquist16febb42016-02-12 00:15:041629cc::ImageSerializationProcessor*
1630RenderThreadImpl::GetImageSerializationProcessor() {
1631 return GetContentClient()->renderer()->GetImageSerializationProcessor();
1632}
1633
reveman34b7a1522015-03-23 20:27:471634cc::TaskGraphRunner* RenderThreadImpl::GetTaskGraphRunner() {
prashant.nfad657e2016-06-01 07:52:171635 return categorized_worker_pool_->GetTaskGraphRunner();
reveman34b7a1522015-03-23 20:27:471636}
1637
vmpstrdfd22862015-09-25 17:42:411638bool RenderThreadImpl::AreImageDecodeTasksEnabled() {
1639 return are_image_decode_tasks_enabled_;
reveman34b7a1522015-03-23 20:27:471640}
1641
loyso65c93c602015-08-11 05:15:571642bool RenderThreadImpl::IsThreadedAnimationEnabled() {
1643 return is_threaded_animation_enabled_;
1644}
1645
skyostila37c2a72016-06-29 17:30:071646void RenderThreadImpl::OnRAILModeChanged(v8::RAILMode rail_mode) {
1647 blink::mainThreadIsolate()->SetRAILMode(rail_mode);
1648 blink::setRAILModeOnWorkerThreadIsolates(rail_mode);
1649}
1650
[email protected]b3e83de2012-02-07 03:33:281651bool RenderThreadImpl::IsMainThread() {
1652 return !!current();
1653}
1654
skyostild4452912015-05-14 14:41:531655scoped_refptr<base::SingleThreadTaskRunner>
1656RenderThreadImpl::GetIOThreadTaskRunner() {
1657 return io_thread_task_runner_;
[email protected]b3e83de2012-02-07 03:33:281658}
1659
dchengcedca5612016-04-09 01:40:151660std::unique_ptr<base::SharedMemory> RenderThreadImpl::AllocateSharedMemory(
[email protected]3f892832013-01-11 03:23:591661 size_t size) {
piman08f75532015-10-05 18:58:011662 return HostAllocateSharedMemoryBuffer(size);
[email protected]b3e83de2012-02-07 03:33:281663}
1664
torne88b66452016-05-03 13:22:141665void RenderThreadImpl::OnChannelError() {
1666 // In single-process mode, the renderer can't be restarted after shutdown.
1667 // So, if we get a channel error, crash the whole process right now to get a
1668 // more informative stack, since we will otherwise just crash later when we
1669 // try to restart it.
1670 CHECK(!base::CommandLine::ForCurrentProcess()->HasSwitch(
1671 switches::kSingleProcess));
1672 ChildThreadImpl::OnChannelError();
1673}
1674
[email protected]f1a29a02011-10-06 23:08:441675bool RenderThreadImpl::OnControlMessageReceived(const IPC::Message& msg) {
tyoshino832a58a2016-04-18 08:14:081676 base::ObserverListBase<RenderThreadObserver>::Iterator it(&observers_);
1677 RenderThreadObserver* observer;
penghuange1d86512016-07-08 18:15:001678 while ((observer = it.GetNext()) != nullptr) {
[email protected]1223d6ef2011-03-28 16:47:501679 if (observer->OnControlMessageReceived(msg))
1680 return true;
1681 }
1682
[email protected]70c19a932010-05-14 12:59:111683 // Some messages are handled by delegates.
[email protected]1910fe82012-05-10 00:04:101684 if (appcache_dispatcher_->OnMessageReceived(msg) ||
[email protected]7670d4332013-11-29 04:35:001685 dom_storage_dispatcher_->OnMessageReceived(msg) ||
1686 embedded_worker_dispatcher_->OnMessageReceived(msg)) {
[email protected]a95986a82010-12-24 06:19:281687 return true;
[email protected]1910fe82012-05-10 00:04:101688 }
[email protected]1edc16b82009-04-07 17:45:541689
[email protected]a95986a82010-12-24 06:19:281690 bool handled = true;
[email protected]f1a29a02011-10-06 23:08:441691 IPC_BEGIN_MESSAGE_MAP(RenderThreadImpl, msg)
[email protected]82307f6b2014-08-07 03:30:121692 IPC_MESSAGE_HANDLER(FrameMsg_NewFrame, OnCreateNewFrame)
1693 IPC_MESSAGE_HANDLER(FrameMsg_NewFrameProxy, OnCreateNewFrameProxy)
[email protected]8930d472009-02-21 08:05:281694 // TODO(port): removed from render_messages_internal.h;
1695 // is there a new non-windows message I should add here?
1696 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
jkarlin300a1d22015-09-18 19:32:521697 IPC_MESSAGE_HANDLER(ViewMsg_NetworkConnectionChanged,
1698 OnNetworkConnectionChanged)
[email protected]0ec90d522014-03-12 16:28:191699 IPC_MESSAGE_HANDLER(WorkerProcessMsg_CreateWorker, OnCreateNewSharedWorker)
[email protected]df9accd2014-03-13 18:18:301700 IPC_MESSAGE_HANDLER(ViewMsg_TimezoneChange, OnUpdateTimezone)
[email protected]b0988b22013-11-08 08:25:581701#if defined(OS_ANDROID)
[email protected]44363382013-03-12 07:22:471702 IPC_MESSAGE_HANDLER(ViewMsg_SetWebKitSharedTimersSuspended,
1703 OnSetWebKitSharedTimersSuspended)
[email protected]b0988b22013-11-08 08:25:581704#endif
1705#if defined(OS_MACOSX)
1706 IPC_MESSAGE_HANDLER(ViewMsg_UpdateScrollbarTheme, OnUpdateScrollbarTheme)
rsesek1efb3c32015-09-29 15:39:501707 IPC_MESSAGE_HANDLER(ViewMsg_SystemColorsChanged, OnSystemColorsChanged)
[email protected]b0988b22013-11-08 08:25:581708#endif
thestigc4cac8f2014-09-04 21:17:501709#if defined(ENABLE_PLUGINS)
1710 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, OnPurgePluginListCache)
1711#endif
[email protected]a95986a82010-12-24 06:19:281712 IPC_MESSAGE_UNHANDLED(handled = false)
[email protected]8930d472009-02-21 08:05:281713 IPC_END_MESSAGE_MAP()
[email protected]a95986a82010-12-24 06:19:281714 return handled;
initial.commit09911bf2008-07-26 23:55:291715}
1716
jdduke73220f02015-09-04 17:03:511717void RenderThreadImpl::OnProcessBackgrounded(bool backgrounded) {
1718 ChildThreadImpl::OnProcessBackgrounded(backgrounded);
1719
hajimehoshi69093272016-05-13 08:30:581720 if (backgrounded) {
jdduke73220f02015-09-04 17:03:511721 renderer_scheduler_->OnRendererBackgrounded();
hajimehoshi69093272016-05-13 08:30:581722 } else {
jdduke73220f02015-09-04 17:03:511723 renderer_scheduler_->OnRendererForegrounded();
hajimehoshi69093272016-05-13 08:30:581724 is_renderer_suspended_ = false;
1725 }
1726}
1727
1728void RenderThreadImpl::OnProcessPurgeAndSuspend() {
1729 ChildThreadImpl::OnProcessPurgeAndSuspend();
1730 if (is_renderer_suspended_)
1731 return;
1732 // TODO(hajimehoshi): Implement purging e.g. cache (crbug/607077)
1733 is_renderer_suspended_ = true;
1734 renderer_scheduler_->SuspendRenderer();
jdduke73220f02015-09-04 17:03:511735}
1736
alexmos9f8705a2015-05-06 19:58:591737void RenderThreadImpl::OnCreateNewFrame(FrameMsg_NewFrame_Params params) {
kenrba7199832015-01-22 23:44:591738 CompositorDependencies* compositor_deps = this;
alexmos9f8705a2015-05-06 19:58:591739 RenderFrameImpl::CreateFrame(
alexmosa181efc2015-09-03 00:39:041740 params.routing_id, params.proxy_routing_id, params.opener_routing_id,
1741 params.parent_routing_id, params.previous_sibling_routing_id,
lazyboy70605c32015-11-03 01:27:311742 params.replication_state, compositor_deps, params.widget_params,
raymes31457802016-07-20 06:08:091743 params.frame_owner_properties);
[email protected]82307f6b2014-08-07 03:30:121744}
1745
alexmosbc7eafa2014-12-06 01:38:091746void RenderThreadImpl::OnCreateNewFrameProxy(
1747 int routing_id,
alexmosbc7eafa2014-12-06 01:38:091748 int render_view_routing_id,
alexmosa181efc2015-09-03 00:39:041749 int opener_routing_id,
1750 int parent_routing_id,
alexmosbc7eafa2014-12-06 01:38:091751 const FrameReplicationState& replicated_state) {
creis9f7248b2016-01-27 20:27:391752 // Debug cases of https://crbug.com/575245.
1753 base::debug::SetCrashKeyValue("newproxy_proxy_id",
1754 base::IntToString(routing_id));
1755 base::debug::SetCrashKeyValue("newproxy_view_id",
1756 base::IntToString(render_view_routing_id));
1757 base::debug::SetCrashKeyValue("newproxy_opener_id",
1758 base::IntToString(opener_routing_id));
1759 base::debug::SetCrashKeyValue("newproxy_parent_id",
1760 base::IntToString(parent_routing_id));
alexmosa181efc2015-09-03 00:39:041761 RenderFrameProxy::CreateFrameProxy(routing_id, render_view_routing_id,
1762 opener_routing_id, parent_routing_id,
1763 replicated_state);
[email protected]82307f6b2014-08-07 03:30:121764}
1765
[email protected]f1a29a02011-10-06 23:08:441766void RenderThreadImpl::OnCreateNewView(const ViewMsg_New_Params& params) {
danakj6e3bf8012014-12-16 18:27:531767 CompositorDependencies* compositor_deps = this;
[email protected]be645db2009-02-06 20:36:331768 // When bringing in render_view, also bring in webkit's glue and jsbindings.
dcheng35d31c112015-07-22 00:17:361769 RenderViewImpl::Create(compositor_deps, params, false);
[email protected]7f874dec2009-02-06 01:48:271770}
[email protected]4274e582009-01-27 22:09:561771
sadrul6d310fa2016-08-04 02:12:161772scoped_refptr<gpu::GpuChannelHost> RenderThreadImpl::EstablishGpuChannelSync() {
[email protected]d13f35d2012-05-18 02:28:151773 TRACE_EVENT0("gpu", "RenderThreadImpl::EstablishGpuChannelSync");
1774
piman9fc22f32016-05-02 22:21:221775 if (gpu_channel_) {
[email protected]1082b1d2010-03-30 00:31:221776 // Do nothing if we already have a GPU channel or are already
1777 // establishing one.
[email protected]7951bfe2013-06-13 01:43:341778 if (!gpu_channel_->IsLost())
piman9fc22f32016-05-02 22:21:221779 return gpu_channel_;
[email protected]6217d392010-03-25 22:08:351780
1781 // Recreate the channel if it has been lost.
piman765e5282015-05-08 03:43:011782 gpu_channel_->DestroyChannel();
penghuange1d86512016-07-08 18:15:001783 gpu_channel_ = nullptr;
[email protected]6217d392010-03-25 22:08:351784 }
1785
penghuange1d86512016-07-08 18:15:001786 if (!IsRunningInMash()) {
1787 int client_id = 0;
1788 IPC::ChannelHandle channel_handle;
1789 gpu::GPUInfo gpu_info;
1790 // Ask the browser for the channel name.
1791 if (!Send(new ChildProcessHostMsg_EstablishGpuChannel(
sadrul6d310fa2016-08-04 02:12:161792 &client_id, &channel_handle, &gpu_info)) ||
amistrycef5cfa2016-07-21 00:09:321793 !channel_handle.mojo_handle.is_valid()) {
penghuange1d86512016-07-08 18:15:001794 // Otherwise cancel the connection.
1795 return nullptr;
1796 }
1797 GetContentClient()->SetGpuInfo(gpu_info);
1798
1799 // Cache some variables that are needed on the compositor thread for our
1800 // implementation of GpuChannelHostFactory.
1801 io_thread_task_runner_ = ChildProcess::current()->io_task_runner();
1802
1803 gpu_channel_ =
1804 gpu::GpuChannelHost::Create(this, client_id, gpu_info, channel_handle,
1805 ChildProcess::current()->GetShutDownEvent(),
1806 gpu_memory_buffer_manager());
1807 } else {
sadrul943e3b32016-08-04 18:22:591808#if defined(USE_AURA)
sadrul9e0bb1d2016-08-04 20:18:391809 gpu_channel_ = gpu_service_->EstablishGpuChannelSync();
penghuange1d86512016-07-08 18:15:001810#else
1811 NOTREACHED();
1812#endif
[email protected]b42a9f62011-06-08 20:36:041813 }
piman9fc22f32016-05-02 22:21:221814 return gpu_channel_;
[email protected]3bf4d532010-03-27 00:23:341815}
1816
danakj83066a32016-06-21 02:34:491817std::unique_ptr<cc::OutputSurface>
1818RenderThreadImpl::CreateCompositorOutputSurface(
1819 bool use_software,
1820 int routing_id,
1821 scoped_refptr<FrameSwapMessageQueue> frame_swap_message_queue,
1822 const GURL& url) {
1823 const base::CommandLine& command_line =
1824 *base::CommandLine::ForCurrentProcess();
1825 if (command_line.HasSwitch(switches::kDisableGpuCompositing))
1826 use_software = true;
1827
sadrul943e3b32016-08-04 18:22:591828#if defined(USE_AURA)
rockotcef38272016-07-15 22:47:471829 if (GetMojoShellConnection() && !use_software &&
danakj83066a32016-06-21 02:34:491830 command_line.HasSwitch(switches::kUseMusInRenderer)) {
1831 RenderWidgetMusConnection* connection =
1832 RenderWidgetMusConnection::GetOrCreate(routing_id);
sadrul62c31132016-08-11 04:13:171833 return connection->CreateOutputSurface(gpu_service_.get());
danakj83066a32016-06-21 02:34:491834 }
1835#endif
1836
1837 uint32_t output_surface_id = g_next_output_surface_id++;
1838
1839 if (command_line.HasSwitch(switches::kEnableVulkan)) {
1840 scoped_refptr<cc::VulkanContextProvider> vulkan_context_provider =
1841 cc::VulkanInProcessContextProvider::Create();
1842 if (vulkan_context_provider) {
1843 DCHECK(!layout_test_mode());
1844 return base::WrapUnique(new CompositorOutputSurface(
danakja40dd4482016-06-28 01:14:101845 routing_id, output_surface_id, std::move(vulkan_context_provider),
1846 std::move(frame_swap_message_queue)));
danakj83066a32016-06-21 02:34:491847 }
1848 }
1849
1850 // Create a gpu process channel and verify we want to use GPU compositing
1851 // before creating any context providers.
1852 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host;
1853 if (!use_software) {
sadrul6d310fa2016-08-04 02:12:161854 gpu_channel_host = EstablishGpuChannelSync();
danakj83066a32016-06-21 02:34:491855 if (!gpu_channel_host) {
1856 // Cause the compositor to wait and try again.
1857 return nullptr;
1858 }
1859 // We may get a valid channel, but with a software renderer. In that case,
1860 // disable GPU compositing.
1861 if (gpu_channel_host->gpu_info().software_rendering)
1862 use_software = true;
1863 }
1864
1865 if (use_software) {
1866 DCHECK(!layout_test_mode());
danakja40dd4482016-06-28 01:14:101867 return base::WrapUnique(new CompositorOutputSurface(
1868 routing_id, output_surface_id, nullptr, nullptr,
1869 std::move(frame_swap_message_queue)));
danakj83066a32016-06-21 02:34:491870 }
1871
1872 scoped_refptr<ContextProviderCommandBuffer> worker_context_provider =
1873 SharedCompositorWorkerContextProvider();
1874 if (!worker_context_provider) {
1875 // Cause the compositor to wait and try again.
1876 return nullptr;
1877 }
1878
1879 // The renderer compositor context doesn't do a lot of stuff, so we don't
1880 // expect it to need a lot of space for commands or transfer. Raster and
1881 // uploads happen on the worker context instead.
1882 gpu::SharedMemoryLimits limits = gpu::SharedMemoryLimits::ForMailboxContext();
1883
1884 // This is for an offscreen context for the compositor. So the default
1885 // framebuffer doesn't need alpha, depth, stencil, antialiasing.
1886 gpu::gles2::ContextCreationAttribHelper attributes;
1887 attributes.alpha_size = -1;
1888 attributes.depth_size = 0;
1889 attributes.stencil_size = 0;
1890 attributes.samples = 0;
1891 attributes.sample_buffers = 0;
1892 attributes.bind_generates_resource = false;
1893 attributes.lose_context_when_out_of_memory = true;
1894
1895 constexpr bool automatic_flushes = false;
1896 constexpr bool support_locking = false;
1897
1898 // The compositor context shares resources with the worker context unless
1899 // the worker is async.
1900 ContextProviderCommandBuffer* share_context = worker_context_provider.get();
1901 if (IsAsyncWorkerContextEnabled())
1902 share_context = nullptr;
1903
1904 scoped_refptr<ContextProviderCommandBuffer> context_provider(
1905 new ContextProviderCommandBuffer(
danakja40dd4482016-06-28 01:14:101906 gpu_channel_host, gpu::GPU_STREAM_DEFAULT,
danakj83066a32016-06-21 02:34:491907 gpu::GpuStreamPriority::NORMAL, gpu::kNullSurfaceHandle, url,
pimand488e8b42016-06-30 19:06:591908 automatic_flushes, support_locking, limits, attributes, share_context,
danakj83066a32016-06-21 02:34:491909 command_buffer_metrics::RENDER_COMPOSITOR_CONTEXT));
1910
danakj83066a32016-06-21 02:34:491911 if (layout_test_deps_) {
1912 return layout_test_deps_->CreateOutputSurface(
jbroman6ccbc7d472016-07-27 04:45:411913 routing_id, std::move(gpu_channel_host), std::move(context_provider),
danakja40dd4482016-06-28 01:14:101914 std::move(worker_context_provider), this);
danakj83066a32016-06-21 02:34:491915 }
1916
1917#if defined(OS_ANDROID)
1918 if (sync_compositor_message_filter_) {
1919 return base::WrapUnique(new SynchronousCompositorOutputSurface(
danakja40dd4482016-06-28 01:14:101920 std::move(context_provider), std::move(worker_context_provider),
1921 routing_id, output_surface_id, sync_compositor_message_filter_.get(),
1922 std::move(frame_swap_message_queue)));
danakj83066a32016-06-21 02:34:491923 }
1924#endif
1925
1926 return base::WrapUnique(new CompositorOutputSurface(
1927 routing_id, output_surface_id, std::move(context_provider),
danakja40dd4482016-06-28 01:14:101928 std::move(worker_context_provider), std::move(frame_swap_message_queue)));
danakj83066a32016-06-21 02:34:491929}
1930
jbroman6ccbc7d472016-07-27 04:45:411931std::unique_ptr<cc::SwapPromise>
1932RenderThreadImpl::RequestCopyOfOutputForLayoutTest(
1933 int32_t routing_id,
1934 std::unique_ptr<cc::CopyOutputRequest> request) {
1935 DCHECK(layout_test_deps_);
1936 return layout_test_deps_->RequestCopyOfOutput(routing_id, std::move(request));
1937}
1938
[email protected]180ef242013-11-07 06:50:461939blink::WebMediaStreamCenter* RenderThreadImpl::CreateMediaStreamCenter(
1940 blink::WebMediaStreamCenterClient* client) {
[email protected]d8cd8372012-03-09 10:49:511941#if defined(ENABLE_WEBRTC)
[email protected]68e5fee2013-02-18 10:04:221942 if (!media_stream_center_) {
1943 media_stream_center_ = GetContentClient()->renderer()
1944 ->OverrideCreateWebMediaStreamCenter(client);
1945 if (!media_stream_center_) {
dchengcedca5612016-04-09 01:40:151946 std::unique_ptr<MediaStreamCenter> media_stream_center(
[email protected]0107d8a2014-05-16 10:20:341947 new MediaStreamCenter(client, GetPeerConnectionDependencyFactory()));
[email protected]c1298742013-07-17 04:06:321948 media_stream_center_ = media_stream_center.release();
[email protected]68e5fee2013-02-18 10:04:221949 }
1950 }
[email protected]d8cd8372012-03-09 10:49:511951#endif
1952 return media_stream_center_;
1953}
1954
[email protected]22fe91d2014-08-12 17:07:121955#if defined(ENABLE_WEBRTC)
[email protected]0107d8a2014-05-16 10:20:341956PeerConnectionDependencyFactory*
1957RenderThreadImpl::GetPeerConnectionDependencyFactory() {
[email protected]83e0a482014-05-22 18:07:181958 return peer_connection_factory_.get();
[email protected]6ee10bd2012-09-13 09:01:531959}
[email protected]22fe91d2014-08-12 17:07:121960#endif
[email protected]6ee10bd2012-09-13 09:01:531961
rockot1587e332016-07-27 17:44:141962mojom::RenderFrameMessageFilter*
1963RenderThreadImpl::render_frame_message_filter() {
1964 if (!render_frame_message_filter_)
1965 GetChannel()->GetRemoteAssociatedInterface(&render_frame_message_filter_);
1966 return render_frame_message_filter_.get();
1967}
1968
penghuang346a46f92016-03-31 21:37:521969gpu::GpuChannelHost* RenderThreadImpl::GetGpuChannel() {
piman9fc22f32016-05-02 22:21:221970 if (!gpu_channel_)
danakj3873e852016-05-05 00:46:071971 return nullptr;
[email protected]7951bfe2013-06-13 01:43:341972 if (gpu_channel_->IsLost())
danakj3873e852016-05-05 00:46:071973 return nullptr;
[email protected]6217d392010-03-25 22:08:351974 return gpu_channel_.get();
1975}
1976
thestigc4cac8f2014-09-04 21:17:501977#if defined(ENABLE_PLUGINS)
[email protected]f1a29a02011-10-06 23:08:441978void RenderThreadImpl::OnPurgePluginListCache(bool reload_pages) {
[email protected]b547fd42009-04-23 23:16:271979 // The call below will cause a GetPlugins call with refresh=true, but at this
1980 // point we already know that the browser has refreshed its list, so disable
1981 // refresh temporarily to prevent each renderer process causing the list to be
1982 // regenerated.
tfarina556a7232014-10-05 01:02:091983 blink_platform_impl_->set_plugin_refresh_allowed(false);
[email protected]180ef242013-11-07 06:50:461984 blink::resetPluginCache(reload_pages);
tfarina556a7232014-10-05 01:02:091985 blink_platform_impl_->set_plugin_refresh_allowed(true);
[email protected]3b48dbc2012-01-06 16:34:171986
tyoshino832a58a2016-04-18 08:14:081987 FOR_EACH_OBSERVER(RenderThreadObserver, observers_, PluginListChanged());
[email protected]b547fd42009-04-23 23:16:271988}
thestigc4cac8f2014-09-04 21:17:501989#endif
[email protected]85c55dc2009-11-06 03:05:461990
jkarlin300a1d22015-09-18 19:32:521991void RenderThreadImpl::OnNetworkConnectionChanged(
1992 net::NetworkChangeNotifier::ConnectionType type,
1993 double max_bandwidth_mbps) {
[email protected]8c2a34b2014-06-10 16:27:271994 bool online = type != net::NetworkChangeNotifier::CONNECTION_NONE;
[email protected]6eac57a2011-07-12 21:15:091995 WebNetworkStateNotifier::setOnLine(online);
[email protected]d7ff5fb2014-05-29 19:50:251996 FOR_EACH_OBSERVER(
tyoshino832a58a2016-04-18 08:14:081997 RenderThreadObserver, observers_, NetworkStateChanged(online));
jkarlin300a1d22015-09-18 19:32:521998 WebNetworkStateNotifier::setWebConnection(
1999 NetConnectionTypeToWebConnectionType(type), max_bandwidth_mbps);
[email protected]6eac57a2011-07-12 21:15:092000}
2001
jshin0a9aa632015-04-29 18:35:292002void RenderThreadImpl::OnUpdateTimezone(const std::string& zone_id) {
jochen22b6f9a2014-11-26 11:30:562003 if (!blink_platform_impl_)
2004 return;
jshin0a9aa632015-04-29 18:35:292005 if (!zone_id.empty()) {
2006 icu::TimeZone *new_zone = icu::TimeZone::createTimeZone(
2007 icu::UnicodeString::fromUTF8(zone_id));
2008 icu::TimeZone::adoptDefault(new_zone);
2009 VLOG(1) << "ICU default timezone is set to " << zone_id;
2010 }
[email protected]df9accd2014-03-13 18:18:302011 NotifyTimezoneChange();
2012}
2013
[email protected]b0988b22013-11-08 08:25:582014#if defined(OS_ANDROID)
[email protected]44363382013-03-12 07:22:472015void RenderThreadImpl::OnSetWebKitSharedTimersSuspended(bool suspend) {
jdduked73ae5412015-05-11 20:07:162016 if (suspend) {
jdduked73ae5412015-05-11 20:07:162017 renderer_scheduler_->SuspendTimerQueue();
2018 } else {
jdduked73ae5412015-05-11 20:07:162019 renderer_scheduler_->ResumeTimerQueue();
[email protected]3b96b7b52014-01-13 18:36:072020 }
jdduked73ae5412015-05-11 20:07:162021 webkit_shared_timer_suspended_ = suspend;
[email protected]44363382013-03-12 07:22:472022}
[email protected]b0988b22013-11-08 08:25:582023#endif
2024
2025#if defined(OS_MACOSX)
[email protected]291857a2014-01-23 17:49:112026void RenderThreadImpl::OnUpdateScrollbarTheme(
kerrnelc39eeb72015-06-25 21:11:482027 const ViewMsg_UpdateScrollbarTheme_Params& params) {
[email protected]33b02992014-03-01 01:06:292028 static_cast<WebScrollbarBehaviorImpl*>(
tfarina556a7232014-10-05 01:02:092029 blink_platform_impl_->scrollbarBehavior())
kerrnelc39eeb72015-06-25 21:11:482030 ->set_jump_on_track_click(params.jump_on_track_click);
2031
kerrnela84d95e2015-07-31 17:34:212032 blink::WebScrollbarTheme::updateScrollbarsWithNSDefaults(
kerrnelc39eeb72015-06-25 21:11:482033 params.initial_button_delay, params.autoscroll_button_delay,
kerrnela84d95e2015-07-31 17:34:212034 params.preferred_scroller_style, params.redraw,
bokanb9c004262016-02-17 23:22:102035 params.button_placement);
[email protected]b0988b22013-11-08 08:25:582036}
rsesek1efb3c32015-09-29 15:39:502037
2038void RenderThreadImpl::OnSystemColorsChanged(
2039 int aqua_color_variant,
2040 const std::string& highlight_text_color,
2041 const std::string& highlight_color) {
2042 SystemColorsDidChange(aqua_color_variant, highlight_text_color,
2043 highlight_color);
2044}
[email protected]b0988b22013-11-08 08:25:582045#endif
[email protected]44363382013-03-12 07:22:472046
[email protected]0ec90d522014-03-12 16:28:192047void RenderThreadImpl::OnCreateNewSharedWorker(
2048 const WorkerProcessMsg_CreateWorker_Params& params) {
2049 // EmbeddedSharedWorkerStub will self-destruct.
mkwstcfa98932016-03-09 13:06:192050 new EmbeddedSharedWorkerStub(
2051 params.url, params.name, params.content_security_policy,
2052 params.security_policy_type, params.creation_address_space,
2053 params.pause_on_start, params.route_id);
[email protected]0ec90d522014-03-12 16:28:192054}
2055
[email protected]5071cb22013-07-10 02:19:062056void RenderThreadImpl::OnMemoryPressure(
2057 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
hong.zhengb28b5332016-05-11 02:33:392058 TRACE_EVENT0("memory","RenderThreadImpl::OnMemoryPressure");
ssiddda2ba022015-06-18 21:12:392059 ReleaseFreeMemory();
[email protected]5071cb22013-07-10 02:19:062060
rmcilroyab967c972015-02-17 23:15:002061 // Do not call into blink if it is not initialized.
rmcilroy7fbb3bd52015-02-17 19:02:142062 if (blink_platform_impl_) {
bashi5e843ac2016-06-17 07:44:352063 blink::WebMemoryCoordinator::onMemoryPressure(
bashic577bfc2016-01-08 03:42:352064 static_cast<blink::WebMemoryPressureLevel>(memory_pressure_level));
rmcilroyab967c972015-02-17 23:15:002065
rmcilroyab967c972015-02-17 23:15:002066 if (memory_pressure_level ==
2067 base::MemoryPressureListener::MEMORY_PRESSURE_LEVEL_CRITICAL) {
rmcilroyab967c972015-02-17 23:15:002068 // Purge Skia font cache, by setting it to 0 and then again to the
2069 // previous limit.
2070 size_t font_cache_limit = SkGraphics::SetFontCacheLimit(0);
2071 SkGraphics::SetFontCacheLimit(font_cache_limit);
2072 }
rmcilroy7fbb3bd52015-02-17 19:02:142073 }
[email protected]5071cb22013-07-10 02:19:062074}
2075
skyostil2d3b5bd2015-05-27 15:40:592076scoped_refptr<base::SingleThreadTaskRunner>
thestig529ad8a2016-07-08 20:30:122077RenderThreadImpl::GetFileThreadTaskRunner() {
fdoraye94d8642016-07-07 19:19:012078 DCHECK(message_loop()->task_runner()->BelongsToCurrentThread());
[email protected]59383c782013-04-17 16:43:272079 if (!file_thread_) {
[email protected]c6a7b862010-08-20 22:19:382080 file_thread_.reset(new base::Thread("Renderer::FILE"));
2081 file_thread_->Start();
2082 }
skyostil2d3b5bd2015-05-27 15:40:592083 return file_thread_->task_runner();
[email protected]c6a7b862010-08-20 22:19:382084}
[email protected]e9ff79c2012-10-19 21:31:262085
acolwellb4034942014-08-28 15:42:432086scoped_refptr<base::SingleThreadTaskRunner>
2087RenderThreadImpl::GetMediaThreadTaskRunner() {
fdoraye94d8642016-07-07 19:19:012088 DCHECK(message_loop()->task_runner()->BelongsToCurrentThread());
[email protected]c1330c82013-06-06 02:23:252089 if (!media_thread_) {
2090 media_thread_.reset(new base::Thread("Media"));
2091 media_thread_->Start();
[email protected]6ca6a082013-09-22 20:10:052092
2093#if defined(OS_ANDROID)
2094 renderer_demuxer_ = new RendererDemuxerAndroid();
2095 AddFilter(renderer_demuxer_.get());
2096#endif
[email protected]c1330c82013-06-06 02:23:252097 }
skyostil2d3b5bd2015-05-27 15:40:592098 return media_thread_->task_runner();
[email protected]c1330c82013-06-06 02:23:252099}
2100
dcastagna4517a182015-08-05 19:51:032101base::TaskRunner* RenderThreadImpl::GetWorkerTaskRunner() {
prashant.nfad657e2016-06-01 07:52:172102 return categorized_worker_pool_.get();
dcastagnab880e8f2015-06-30 20:16:062103}
2104
revemand180dfc32015-09-24 00:19:432105scoped_refptr<ContextProviderCommandBuffer>
danakj0b4b94e32016-05-10 22:33:012106RenderThreadImpl::SharedCompositorWorkerContextProvider() {
revemand180dfc32015-09-24 00:19:432107 DCHECK(IsMainThread());
2108 // Try to reuse existing shared worker context provider.
revemand180dfc32015-09-24 00:19:432109 if (shared_worker_context_provider_) {
2110 // Note: If context is lost, delete reference after releasing the lock.
boliu11afa7e2016-04-18 18:04:142111 cc::ContextProvider::ScopedContextLock lock(
2112 shared_worker_context_provider_.get());
revemand180dfc32015-09-24 00:19:432113 if (shared_worker_context_provider_->ContextGL()
danakje8ec797e2016-04-19 04:08:432114 ->GetGraphicsResetStatusKHR() == GL_NO_ERROR)
2115 return shared_worker_context_provider_;
revemand180dfc32015-09-24 00:19:432116 }
danakje8ec797e2016-04-19 04:08:432117
sadrul6d310fa2016-08-04 02:12:162118 scoped_refptr<gpu::GpuChannelHost> gpu_channel_host(
2119 EstablishGpuChannelSync());
danakje8ec797e2016-04-19 04:08:432120 if (!gpu_channel_host) {
2121 shared_worker_context_provider_ = nullptr;
2122 return shared_worker_context_provider_;
revemand180dfc32015-09-24 00:19:432123 }
danakje8ec797e2016-04-19 04:08:432124
sunnypsd8ce1c22016-05-10 18:02:402125 int32_t stream_id = gpu::GPU_STREAM_DEFAULT;
2126 gpu::GpuStreamPriority stream_priority = gpu::GpuStreamPriority::NORMAL;
2127 if (is_async_worker_context_enabled_) {
2128 stream_id = gpu_channel_host->GenerateStreamID();
2129 stream_priority = gpu::GpuStreamPriority::LOW;
2130 }
2131
danakj9a04adc2016-05-16 22:45:072132 bool support_locking = true;
danakj0dd9e1e2016-05-11 22:15:092133 shared_worker_context_provider_ = CreateOffscreenContext(
danakj9a04adc2016-05-16 22:45:072134 std::move(gpu_channel_host), gpu::SharedMemoryLimits(), support_locking,
danakj0dd9e1e2016-05-11 22:15:092135 command_buffer_metrics::RENDER_WORKER_CONTEXT, stream_id,
2136 stream_priority);
danakje8ec797e2016-04-19 04:08:432137 if (!shared_worker_context_provider_->BindToCurrentThread())
2138 shared_worker_context_provider_ = nullptr;
revemand180dfc32015-09-24 00:19:432139 return shared_worker_context_provider_;
2140}
2141
[email protected]180ef242013-11-07 06:50:462142void RenderThreadImpl::SampleGamepads(blink::WebGamepads* data) {
tfarina556a7232014-10-05 01:02:092143 blink_platform_impl_->sampleGamepads(*data);
[email protected]0ff736562014-05-09 09:09:472144}
2145
rmcilroyaa296052015-04-14 15:35:272146bool RenderThreadImpl::RendererIsHidden() const {
2147 return widget_count_ > 0 && hidden_widget_count_ == widget_count_;
2148}
2149
[email protected]b2db9272014-01-10 17:42:002150void RenderThreadImpl::WidgetCreated() {
rmcilroyaa296052015-04-14 15:35:272151 bool renderer_was_hidden = RendererIsHidden();
[email protected]b2db9272014-01-10 17:42:002152 widget_count_++;
rmcilroyaa296052015-04-14 15:35:272153 if (renderer_was_hidden)
2154 OnRendererVisible();
[email protected]b2db9272014-01-10 17:42:002155}
2156
2157void RenderThreadImpl::WidgetDestroyed() {
rmcilroyaa296052015-04-14 15:35:272158 // TODO(rmcilroy): Remove the restriction that destroyed widgets must be
2159 // unhidden before WidgetDestroyed is called.
2160 DCHECK_GT(widget_count_, 0);
2161 DCHECK_GT(widget_count_, hidden_widget_count_);
[email protected]b2db9272014-01-10 17:42:002162 widget_count_--;
rmcilroyaa296052015-04-14 15:35:272163 if (RendererIsHidden())
2164 OnRendererHidden();
[email protected]b2db9272014-01-10 17:42:002165}
2166
2167void RenderThreadImpl::WidgetHidden() {
2168 DCHECK_LT(hidden_widget_count_, widget_count_);
2169 hidden_widget_count_++;
rmcilroyaa296052015-04-14 15:35:272170 if (RendererIsHidden())
2171 OnRendererHidden();
[email protected]b2db9272014-01-10 17:42:002172}
2173
2174void RenderThreadImpl::WidgetRestored() {
rmcilroyaa296052015-04-14 15:35:272175 bool renderer_was_hidden = RendererIsHidden();
[email protected]b2db9272014-01-10 17:42:002176 DCHECK_GT(hidden_widget_count_, 0);
2177 hidden_widget_count_--;
rmcilroyaa296052015-04-14 15:35:272178 if (renderer_was_hidden)
2179 OnRendererVisible();
2180}
[email protected]b2db9272014-01-10 17:42:002181
rmcilroyaa296052015-04-14 15:35:272182void RenderThreadImpl::OnRendererHidden() {
ulan432cd6e42016-03-01 15:59:142183 blink::mainThreadIsolate()->IsolateInBackgroundNotification();
rmcilroyaa296052015-04-14 15:35:272184 // TODO(rmcilroy): Remove IdleHandler and replace it with an IdleTask
2185 // scheduled by the RendererScheduler - http://crbug.com/469210.
ulan4a385192015-11-11 10:59:182186 if (!GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
2187 return;
2188 ScheduleIdleHandler(kInitialIdleHandlerDelayMs);
rmcilroyaa296052015-04-14 15:35:272189}
2190
2191void RenderThreadImpl::OnRendererVisible() {
ulan432cd6e42016-03-01 15:59:142192 blink::mainThreadIsolate()->IsolateInForegroundNotification();
rmcilroyaa296052015-04-14 15:35:272193 if (!GetContentClient()->renderer()->RunIdleHandlerWhenWidgetsHidden())
[email protected]b2db9272014-01-10 17:42:002194 return;
[email protected]b2db9272014-01-10 17:42:002195 ScheduleIdleHandler(kLongIdleHandlerDelayMs);
2196}
2197
ssid0603ca9f2015-06-09 16:48:082198void RenderThreadImpl::ReleaseFreeMemory() {
2199 base::allocator::ReleaseFreeMemory();
2200 discardable_shared_memory_manager()->ReleaseFreeMemory();
2201
2202 if (blink_platform_impl_)
2203 blink::decommitFreeableMemory();
2204}
2205
ben76f52b242016-06-18 05:42:482206RenderThreadImpl::PendingFrameCreate::PendingFrameCreate(
rockotf8fdd9b2015-12-16 22:22:352207 int routing_id,
ben76f52b242016-06-18 05:42:482208 mojom::FrameRequest frame_request,
2209 mojom::FrameHostPtr frame_host)
rockotf8fdd9b2015-12-16 22:22:352210 : routing_id_(routing_id),
ben76f52b242016-06-18 05:42:482211 frame_request_(std::move(frame_request)),
2212 frame_host_(std::move(frame_host)) {
2213 // The RenderFrame may be deleted before the CreateFrame message is received.
2214 // In that case, the RenderFrameHost should cancel the create, which is
2215 // detected by setting an error handler on |frame_host_|.
2216 frame_host_.set_connection_error_handler(base::Bind(
2217 &RenderThreadImpl::PendingFrameCreate::OnConnectionError,
rockotf8fdd9b2015-12-16 22:22:352218 base::Unretained(this)));
2219}
2220
ben76f52b242016-06-18 05:42:482221RenderThreadImpl::PendingFrameCreate::~PendingFrameCreate() {
rockotf8fdd9b2015-12-16 22:22:352222}
2223
ben76f52b242016-06-18 05:42:482224void RenderThreadImpl::PendingFrameCreate::OnConnectionError() {
rockotf8fdd9b2015-12-16 22:22:352225 size_t erased =
ben76f52b242016-06-18 05:42:482226 RenderThreadImpl::current()->pending_frame_creates_.erase(routing_id_);
rockotf8fdd9b2015-12-16 22:22:352227 DCHECK_EQ(1u, erased);
2228}
2229
hong.zhengb28b5332016-05-11 02:33:392230void RenderThreadImpl::OnSyncMemoryPressure(
2231 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level) {
2232 if (!blink::mainThreadIsolate())
2233 return;
2234
2235 v8::MemoryPressureLevel v8_memory_pressure_level =
2236 static_cast<v8::MemoryPressureLevel>(memory_pressure_level);
2237
2238 // In order to reduce performance impact, translate critical level to
2239 // moderate level for foregroud renderer.
2240 if (!RendererIsHidden() &&
2241 v8_memory_pressure_level == v8::MemoryPressureLevel::kCritical)
2242 v8_memory_pressure_level = v8::MemoryPressureLevel::kModerate;
2243
2244 blink::mainThreadIsolate()->MemoryPressureNotification(
2245 v8_memory_pressure_level);
2246 blink::MemoryPressureNotificationToWorkerThreadIsolates(
2247 v8_memory_pressure_level);
2248}
2249
[email protected]e9ff79c2012-10-19 21:31:262250} // namespace content