1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
5 #ifndef CONTENT_RENDERER_RENDER_THREAD_IMPL_H_
6 #define CONTENT_RENDERER_RENDER_THREAD_IMPL_H_
12 #include "base/memory/memory_pressure_listener.h"
13 #include "base/metrics/user_metrics_action.h"
14 #include "base/observer_list.h"
15 #include "base/strings/string16.h"
16 #include "base/threading/thread_checker.h"
17 #include "base/timer/timer.h"
18 #include "build/build_config.h"
19 #include "content/child/child_thread.h"
20 #include "content/common/content_export.h"
21 #include "content/common/gpu/client/gpu_channel_host.h"
22 #include "content/common/gpu/gpu_result_codes.h"
23 #include "content/public/renderer/render_thread.h"
24 #include "net/base/network_change_notifier.h"
25 #include "third_party/WebKit/public/platform/WebConnectionType.h"
26 #include "ui/gfx/native_widget_types.h"
28 #if defined(OS_MACOSX)
29 #include "third_party/WebKit/public/web/mac/WebScrollbarTheme.h"
34 struct ViewMsg_New_Params;
35 struct WorkerProcessMsg_CreateWorker_Params;
39 class WebGamepadListener;
40 class WebGraphicsContext3D;
41 class WebMediaStreamCenter;
42 class WebMediaStreamCenterClient;
46 class MessageLoopProxy;
51 class ContextProvider;
55 class ForwardingMessageFilter;
60 class AudioHardwareConfig;
61 class GpuVideoAcceleratorFactories;
70 class ContextProviderWebContext;
71 class GrContextForWebGraphicsContext3D;
77 class AppCacheDispatcher;
78 class AecDumpMessageFilter;
79 class AudioInputMessageFilter;
80 class AudioMessageFilter;
81 class AudioRendererMixerManager;
82 class ContextProviderCommandBuffer;
83 class DBMessageFilter;
84 class DevToolsAgentFilter;
85 class DomStorageDispatcher;
86 class EmbeddedWorkerDispatcher;
87 class GamepadSharedMemoryReader;
89 class IndexedDBDispatcher;
90 class InputEventFilter;
91 class InputHandlerManager;
92 class MediaStreamCenter;
93 class PeerConnectionDependencyFactory;
94 class MidiMessageFilter;
95 class NetInfoDispatcher;
96 class P2PSocketDispatcher;
97 class PeerConnectionTracker;
98 class RendererDemuxerAndroid;
99 class RendererWebKitPlatformSupportImpl;
100 class RenderProcessObserver;
101 class VideoCaptureImplManager;
102 class WebGraphicsContext3DCommandBufferImpl;
103 class WebRTCIdentityService;
105 // The RenderThreadImpl class represents a background thread where RenderView
106 // instances live. The RenderThread supports an API that is used by its
107 // consumer to talk indirectly to the RenderViews and supporting objects.
108 // Likewise, it provides an API for the RenderViews to talk back to the main
109 // process (i.e., their corresponding WebContentsImpl).
111 // Most of the communication occurs in the form of IPC messages. They are
112 // routed to the RenderThread according to the routing IDs of the messages.
113 // The routing IDs correspond to RenderView instances.
114 class CONTENT_EXPORT RenderThreadImpl : public RenderThread,
116 public GpuChannelHostFactory {
118 static RenderThreadImpl* current();
121 // Constructor that's used when running in single process mode.
122 explicit RenderThreadImpl(const std::string& channel_name);
123 virtual ~RenderThreadImpl();
124 virtual void Shutdown() OVERRIDE;
126 // When initializing WebKit, ensure that any schemes needed for the content
127 // module are registered properly. Static to allow sharing with tests.
128 static void RegisterSchemes();
130 // Notify V8 that the date/time configuration of the system might have
132 static void NotifyTimezoneChange();
134 // RenderThread implementation:
135 virtual bool Send(IPC::Message* msg) OVERRIDE;
136 virtual base::MessageLoop* GetMessageLoop() OVERRIDE;
137 virtual IPC::SyncChannel* GetChannel() OVERRIDE;
138 virtual std::string GetLocale() OVERRIDE;
139 virtual IPC::SyncMessageFilter* GetSyncMessageFilter() OVERRIDE;
140 virtual scoped_refptr<base::MessageLoopProxy> GetIOMessageLoopProxy()
142 virtual void AddRoute(int32 routing_id, IPC::Listener* listener) OVERRIDE;
143 virtual void RemoveRoute(int32 routing_id) OVERRIDE;
144 virtual int GenerateRoutingID() OVERRIDE;
145 virtual void AddFilter(IPC::MessageFilter* filter) OVERRIDE;
146 virtual void RemoveFilter(IPC::MessageFilter* filter) OVERRIDE;
147 virtual void AddObserver(RenderProcessObserver* observer) OVERRIDE;
148 virtual void RemoveObserver(RenderProcessObserver* observer) OVERRIDE;
149 virtual void SetResourceDispatcherDelegate(
150 ResourceDispatcherDelegate* delegate) OVERRIDE;
151 virtual void EnsureWebKitInitialized() OVERRIDE;
152 virtual void RecordAction(const base::UserMetricsAction& action) OVERRIDE;
153 virtual void RecordComputedAction(const std::string& action) OVERRIDE;
154 virtual scoped_ptr<base::SharedMemory> HostAllocateSharedMemoryBuffer(
155 size_t buffer_size) OVERRIDE;
156 virtual void RegisterExtension(v8::Extension* extension) OVERRIDE;
157 virtual void ScheduleIdleHandler(int64 initial_delay_ms) OVERRIDE;
158 virtual void IdleHandler() OVERRIDE;
159 virtual int64 GetIdleNotificationDelayInMs() const OVERRIDE;
160 virtual void SetIdleNotificationDelayInMs(
161 int64 idle_notification_delay_in_ms) OVERRIDE;
162 virtual void UpdateHistograms(int sequence_number) OVERRIDE;
163 virtual int PostTaskToAllWebWorkers(const base::Closure& closure) OVERRIDE;
164 virtual bool ResolveProxy(const GURL& url, std::string* proxy_list) OVERRIDE;
165 virtual base::WaitableEvent* GetShutdownEvent() OVERRIDE;
167 virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
168 virtual void ReleaseCachedFonts() OVERRIDE;
171 // Synchronously establish a channel to the GPU plugin if not previously
172 // established or if it has been lost (for example if the GPU plugin crashed).
173 // If there is a pending asynchronous request, it will be completed by the
174 // time this routine returns.
175 GpuChannelHost* EstablishGpuChannelSync(CauseForGpuLaunch);
178 // These methods modify how the next message is sent. Normally, when sending
179 // a synchronous message that runs a nested message loop, we need to suspend
180 // callbacks into WebKit. This involves disabling timers and deferring
181 // resource loads. However, there are exceptions when we need to customize
183 void DoNotSuspendWebKitSharedTimer();
184 void DoNotNotifyWebKitOfModalLoop();
186 // True if we are running layout tests. This currently disables forwarding
187 // various status messages to the console, skips network error pages, and
188 // short circuits size update and focus events.
189 bool layout_test_mode() const {
190 return layout_test_mode_;
192 void set_layout_test_mode(bool layout_test_mode) {
193 layout_test_mode_ = layout_test_mode;
196 RendererWebKitPlatformSupportImpl* webkit_platform_support() const {
197 DCHECK(webkit_platform_support_);
198 return webkit_platform_support_.get();
201 IPC::ForwardingMessageFilter* compositor_output_surface_filter() const {
202 return compositor_output_surface_filter_.get();
205 InputHandlerManager* input_handler_manager() const {
206 return input_handler_manager_.get();
209 // Will be NULL if threaded compositing has not been enabled.
210 scoped_refptr<base::MessageLoopProxy> compositor_message_loop_proxy() const {
211 return compositor_message_loop_proxy_;
214 bool is_gpu_rasterization_enabled() const {
215 return is_gpu_rasterization_enabled_;
218 bool is_gpu_rasterization_forced() const {
219 return is_gpu_rasterization_forced_;
222 bool is_impl_side_painting_enabled() const {
223 return is_impl_side_painting_enabled_;
226 bool is_low_res_tiling_enabled() const { return is_low_res_tiling_enabled_; }
228 bool is_lcd_text_enabled() const { return is_lcd_text_enabled_; }
230 bool is_distance_field_text_enabled() const {
231 return is_distance_field_text_enabled_;
234 bool is_zero_copy_enabled() const { return is_zero_copy_enabled_; }
236 bool is_one_copy_enabled() const { return is_one_copy_enabled_; }
238 AppCacheDispatcher* appcache_dispatcher() const {
239 return appcache_dispatcher_.get();
242 DomStorageDispatcher* dom_storage_dispatcher() const {
243 return dom_storage_dispatcher_.get();
246 EmbeddedWorkerDispatcher* embedded_worker_dispatcher() const {
247 return embedded_worker_dispatcher_.get();
250 AudioInputMessageFilter* audio_input_message_filter() {
251 return audio_input_message_filter_.get();
254 AudioMessageFilter* audio_message_filter() {
255 return audio_message_filter_.get();
258 MidiMessageFilter* midi_message_filter() {
259 return midi_message_filter_.get();
262 #if defined(OS_ANDROID)
263 RendererDemuxerAndroid* renderer_demuxer() {
264 return renderer_demuxer_.get();
268 // Creates the embedder implementation of WebMediaStreamCenter.
269 // The resulting object is owned by WebKit and deleted by WebKit at tear-down.
270 blink::WebMediaStreamCenter* CreateMediaStreamCenter(
271 blink::WebMediaStreamCenterClient* client);
273 // Returns a factory used for creating RTC PeerConnection objects.
274 PeerConnectionDependencyFactory* GetPeerConnectionDependencyFactory();
276 PeerConnectionTracker* peer_connection_tracker() {
277 return peer_connection_tracker_.get();
280 // Current P2PSocketDispatcher. Set to NULL if P2P API is disabled.
281 P2PSocketDispatcher* p2p_socket_dispatcher() {
282 return p2p_socket_dispatcher_.get();
285 VideoCaptureImplManager* video_capture_impl_manager() const {
286 return vc_manager_.get();
289 GamepadSharedMemoryReader* gamepad_shared_memory_reader() const {
290 return gamepad_shared_memory_reader_.get();
293 // Get the GPU channel. Returns NULL if the channel is not established or
295 GpuChannelHost* GetGpuChannel();
297 // Returns a MessageLoopProxy instance corresponding to the message loop
298 // of the thread on which file operations should be run. Must be called
299 // on the renderer's main thread.
300 scoped_refptr<base::MessageLoopProxy> GetFileThreadMessageLoopProxy();
302 // Returns a MessageLoopProxy instance corresponding to the message loop
303 // of the thread on which media operations should be run. Must be called
304 // on the renderer's main thread.
305 scoped_refptr<base::MessageLoopProxy> GetMediaThreadMessageLoopProxy();
307 // Causes the idle handler to skip sending idle notifications
308 // on the two next scheduled calls, so idle notifications are
309 // not sent for at least one notification delay.
310 void PostponeIdleNotification();
312 scoped_refptr<media::GpuVideoAcceleratorFactories> GetGpuFactories();
314 scoped_refptr<webkit::gpu::ContextProviderWebContext>
315 SharedMainThreadContextProvider();
317 // AudioRendererMixerManager instance which manages renderer side mixer
318 // instances shared based on configured audio parameters. Lazily created on
320 AudioRendererMixerManager* GetAudioRendererMixerManager();
322 // AudioHardwareConfig contains audio hardware configuration for
323 // renderer side clients. Creation requires a synchronous IPC call so it is
324 // lazily created on the first call.
325 media::AudioHardwareConfig* GetAudioHardwareConfig();
328 void PreCacheFontCharacters(const LOGFONT& log_font,
329 const base::string16& str);
332 #if defined(ENABLE_WEBRTC)
333 WebRTCIdentityService* get_webrtc_identity_service() {
334 return webrtc_identity_service_.get();
338 // For producing custom V8 histograms. Custom histograms are produced if all
339 // RenderViews share the same host, and the host is in the pre-specified set
340 // of hosts we want to produce custom diagrams for. The name for a custom
341 // diagram is the name of the corresponding generic diagram plus a
342 // host-specific suffix.
343 class CONTENT_EXPORT HistogramCustomizer {
345 HistogramCustomizer();
346 ~HistogramCustomizer();
348 // Called when a top frame of a RenderView navigates. This function updates
349 // RenderThreadImpl's information about whether all RenderViews are
350 // displaying a page from the same host. |host| is the host where a
351 // RenderView navigated, and |view_count| is the number of RenderViews in
353 void RenderViewNavigatedToHost(const std::string& host, size_t view_count);
355 // Used for customizing some histograms if all RenderViews share the same
356 // host. Returns the current custom histogram name to use for
357 // |histogram_name|, or |histogram_name| if it shouldn't be customized.
358 std::string ConvertToCustomHistogramName(const char* histogram_name) const;
361 friend class RenderThreadImplUnittest;
363 // Used for updating the information on which is the common host which all
364 // RenderView's share (if any). If there is no common host, this function is
365 // called with an empty string.
366 void SetCommonHost(const std::string& host);
368 // The current common host of the RenderViews; empty string if there is no
370 std::string common_host_;
371 // The corresponding suffix.
372 std::string common_host_histogram_suffix_;
373 // Set of histograms for which we want to produce a custom histogram if
375 std::set<std::string> custom_histograms_;
377 DISALLOW_COPY_AND_ASSIGN(HistogramCustomizer);
380 HistogramCustomizer* histogram_customizer() {
381 return &histogram_customizer_;
384 void SetFlingCurveParameters(const std::vector<float>& new_touchpad,
385 const std::vector<float>& new_touchscreen);
387 // Retrieve current gamepad data.
388 void SampleGamepads(blink::WebGamepads* data);
390 // Set a listener for gamepad connected/disconnected events.
391 // A non-null listener must be set first before calling SampleGamepads.
392 void SetGamepadListener(blink::WebGamepadListener* listener);
394 // Called by a RenderWidget when it is created or destroyed. This
395 // allows the process to know when there are no visible widgets.
396 void WidgetCreated();
397 void WidgetDestroyed();
399 void WidgetRestored();
401 void AddEmbeddedWorkerRoute(int32 routing_id, IPC::Listener* listener);
402 void RemoveEmbeddedWorkerRoute(int32 routing_id);
406 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
408 // GpuChannelHostFactory implementation:
409 virtual bool IsMainThread() OVERRIDE;
410 virtual base::MessageLoop* GetMainLoop() OVERRIDE;
411 virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() OVERRIDE;
412 virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(
413 size_t size) OVERRIDE;
414 virtual CreateCommandBufferResult CreateViewCommandBuffer(
416 const GPUCreateCommandBufferConfig& init_params,
417 int32 route_id) OVERRIDE;
418 virtual void CreateImage(
419 gfx::PluginWindowHandle window,
421 const CreateImageCallback& callback) OVERRIDE;
422 virtual void DeleteImage(int32 image_id, int32 sync_point) OVERRIDE;
423 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
426 unsigned internalformat,
427 unsigned usage) OVERRIDE;
429 // mojo::ServiceProvider implementation:
430 virtual void ConnectToService(
431 const mojo::String& service_url,
432 const mojo::String& service_name,
433 mojo::ScopedMessagePipeHandle message_pipe,
434 const mojo::String& requestor_url) OVERRIDE;
438 void OnSetZoomLevelForCurrentURL(const std::string& scheme,
439 const std::string& host,
441 void OnCreateNewView(const ViewMsg_New_Params& params);
442 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
443 void OnPurgePluginListCache(bool reload_pages);
444 void OnNetworkTypeChanged(net::NetworkChangeNotifier::ConnectionType type);
445 void OnGetAccessibilityTree();
446 void OnTempCrashWithData(const GURL& data);
447 void OnUpdateTimezone();
448 void OnMemoryPressure(
449 base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
450 #if defined(OS_ANDROID)
451 void OnSetWebKitSharedTimersSuspended(bool suspend);
453 #if defined(OS_MACOSX)
454 void OnUpdateScrollbarTheme(float initial_button_delay,
455 float autoscroll_button_delay,
456 bool jump_on_track_click,
457 blink::ScrollerStyle preferred_scroller_style,
460 void OnCreateNewSharedWorker(
461 const WorkerProcessMsg_CreateWorker_Params& params);
463 void IdleHandlerInForegroundTab();
465 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateOffscreenContext3d();
467 // These objects live solely on the render thread.
468 scoped_ptr<AppCacheDispatcher> appcache_dispatcher_;
469 scoped_ptr<DomStorageDispatcher> dom_storage_dispatcher_;
470 scoped_ptr<IndexedDBDispatcher> main_thread_indexed_db_dispatcher_;
471 scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_;
472 scoped_ptr<EmbeddedWorkerDispatcher> embedded_worker_dispatcher_;
474 // Used on the render thread and deleted by WebKit at shutdown.
475 blink::WebMediaStreamCenter* media_stream_center_;
477 // Used on the renderer and IPC threads.
478 scoped_refptr<DBMessageFilter> db_message_filter_;
479 scoped_refptr<AudioInputMessageFilter> audio_input_message_filter_;
480 scoped_refptr<AudioMessageFilter> audio_message_filter_;
481 scoped_refptr<MidiMessageFilter> midi_message_filter_;
482 #if defined(OS_ANDROID)
483 scoped_refptr<RendererDemuxerAndroid> renderer_demuxer_;
485 scoped_refptr<DevToolsAgentFilter> devtools_agent_message_filter_;
487 scoped_ptr<PeerConnectionDependencyFactory> peer_connection_factory_;
489 // This is used to communicate to the browser process the status
490 // of all the peer connections created in the renderer.
491 scoped_ptr<PeerConnectionTracker> peer_connection_tracker_;
493 // Dispatches all P2P sockets.
494 scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
496 // Used on the render thread.
497 scoped_ptr<VideoCaptureImplManager> vc_manager_;
499 // Used for communicating registering AEC dump consumers with the browser and
500 // receving AEC dump file handles when AEC dump is enabled. An AEC dump is
501 // diagnostic audio data for WebRTC stored locally when enabled by the user in
502 // chrome://webrtc-internals.
503 scoped_refptr<AecDumpMessageFilter> aec_dump_message_filter_;
505 // The count of RenderWidgets running through this thread.
508 // The count of hidden RenderWidgets running through this thread.
509 int hidden_widget_count_;
511 // The current value of the idle notification timer delay.
512 int64 idle_notification_delay_in_ms_;
514 // The number of idle handler calls that skip sending idle notifications.
515 int idle_notifications_to_skip_;
517 bool suspend_webkit_shared_timer_;
518 bool notify_webkit_of_modal_loop_;
519 bool webkit_shared_timer_suspended_;
521 // The following flag is used to control layout test specific behavior.
522 bool layout_test_mode_;
524 // Timer that periodically calls IdleHandler.
525 base::RepeatingTimer<RenderThreadImpl> idle_timer_;
527 // The channel from the renderer process to the GPU process.
528 scoped_refptr<GpuChannelHost> gpu_channel_;
530 // Cache of variables that are needed on the compositor thread by
531 // GpuChannelHostFactory methods.
532 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
534 // A lazily initiated thread on which file operations are run.
535 scoped_ptr<base::Thread> file_thread_;
537 // May be null if overridden by ContentRendererClient.
538 scoped_ptr<base::Thread> compositor_thread_;
540 // Thread for running multimedia operations (e.g., video decoding).
541 scoped_ptr<base::Thread> media_thread_;
543 // Will point to appropriate MessageLoopProxy after initialization,
544 // regardless of whether |compositor_thread_| is overriden.
545 scoped_refptr<base::MessageLoopProxy> compositor_message_loop_proxy_;
547 // May be null if unused by the |input_handler_manager_|.
548 scoped_refptr<InputEventFilter> input_event_filter_;
549 scoped_ptr<InputHandlerManager> input_handler_manager_;
550 scoped_refptr<IPC::ForwardingMessageFilter> compositor_output_surface_filter_;
552 scoped_refptr<ContextProviderCommandBuffer> shared_main_thread_contexts_;
554 ObserverList<RenderProcessObserver> observers_;
556 scoped_refptr<ContextProviderCommandBuffer> gpu_va_context_provider_;
558 scoped_ptr<AudioRendererMixerManager> audio_renderer_mixer_manager_;
559 scoped_ptr<media::AudioHardwareConfig> audio_hardware_config_;
561 HistogramCustomizer histogram_customizer_;
563 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
565 scoped_ptr<WebRTCIdentityService> webrtc_identity_service_;
567 scoped_ptr<GamepadSharedMemoryReader> gamepad_shared_memory_reader_;
569 // TODO(reveman): Allow AllocateGpuMemoryBuffer to be called from
570 // multiple threads. Current allocation mechanism for IOSurface
571 // backed GpuMemoryBuffers prevent this. crbug.com/325045
572 base::ThreadChecker allocate_gpu_memory_buffer_thread_checker_;
574 // Compositor settings
575 bool is_gpu_rasterization_enabled_;
576 bool is_gpu_rasterization_forced_;
577 bool is_impl_side_painting_enabled_;
578 bool is_low_res_tiling_enabled_;
579 bool is_lcd_text_enabled_;
580 bool is_distance_field_text_enabled_;
581 bool is_zero_copy_enabled_;
582 bool is_one_copy_enabled_;
584 DISALLOW_COPY_AND_ASSIGN(RenderThreadImpl);
587 } // namespace content
589 #endif // CONTENT_RENDERER_RENDER_THREAD_IMPL_H_