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/observer_list.h"
13 #include "base/timer.h"
14 #include "build/build_config.h"
15 #include "content/common/child_process.h"
16 #include "content/common/child_thread.h"
17 #include "content/common/content_export.h"
18 #include "content/common/gpu/client/gpu_channel_host.h"
19 #include "content/common/gpu/gpu_process_launch_causes.h"
20 #include "content/public/renderer/render_thread.h"
21 #include "ipc/ipc_channel_proxy.h"
22 #include "ui/gfx/native_widget_types.h"
25 struct ViewMsg_New_Params
;
28 class WebMediaStreamCenter
;
29 class WebMediaStreamCenterClient
;
33 class MessageLoopProxy
;
38 class ScopedCOMInitializer
;
44 class ForwardingMessageFilter
;
53 class AppCacheDispatcher
;
54 class AudioInputMessageFilter
;
55 class AudioMessageFilter
;
56 class AudioRendererMixerManager
;
57 class CompositorThread
;
58 class DBMessageFilter
;
59 class DevToolsAgentFilter
;
60 class DomStorageDispatcher
;
62 class IndexedDBDispatcher
;
63 class MediaStreamCenter
;
64 class MediaStreamDependencyFactory
;
65 class P2PSocketDispatcher
;
66 class RendererWebKitPlatformSupportImpl
;
67 class RenderProcessObserver
;
68 class VideoCaptureImplManager
;
69 class WebDatabaseObserverImpl
;
70 class WebGraphicsContext3DCommandBufferImpl
;
72 // The RenderThreadImpl class represents a background thread where RenderView
73 // instances live. The RenderThread supports an API that is used by its
74 // consumer to talk indirectly to the RenderViews and supporting objects.
75 // Likewise, it provides an API for the RenderViews to talk back to the main
76 // process (i.e., their corresponding WebContentsImpl).
78 // Most of the communication occurs in the form of IPC messages. They are
79 // routed to the RenderThread according to the routing IDs of the messages.
80 // The routing IDs correspond to RenderView instances.
81 class CONTENT_EXPORT RenderThreadImpl
: public RenderThread
,
83 public GpuChannelHostFactory
{
85 static RenderThreadImpl
* current();
88 // Constructor that's used when running in single process mode.
89 explicit RenderThreadImpl(const std::string
& channel_name
);
90 virtual ~RenderThreadImpl();
92 // When initializing WebKit, ensure that any schemes needed for the content
93 // module are registered properly. Static to allow sharing with tests.
94 static void RegisterSchemes();
96 // RenderThread implementation:
97 virtual bool Send(IPC::Message
* msg
) OVERRIDE
;
98 virtual MessageLoop
* GetMessageLoop() OVERRIDE
;
99 virtual IPC::SyncChannel
* GetChannel() OVERRIDE
;
100 virtual std::string
GetLocale() OVERRIDE
;
101 virtual IPC::SyncMessageFilter
* GetSyncMessageFilter() OVERRIDE
;
102 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOMessageLoopProxy()
104 virtual void AddRoute(int32 routing_id
, IPC::Listener
* listener
) OVERRIDE
;
105 virtual void RemoveRoute(int32 routing_id
) OVERRIDE
;
106 virtual int GenerateRoutingID() OVERRIDE
;
107 virtual void AddFilter(IPC::ChannelProxy::MessageFilter
* filter
) OVERRIDE
;
108 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter
* filter
) OVERRIDE
;
109 virtual void SetOutgoingMessageFilter(
110 IPC::ChannelProxy::OutgoingMessageFilter
* filter
) OVERRIDE
;
111 virtual void AddObserver(RenderProcessObserver
* observer
) OVERRIDE
;
112 virtual void RemoveObserver(RenderProcessObserver
* observer
) OVERRIDE
;
113 virtual void SetResourceDispatcherDelegate(
114 ResourceDispatcherDelegate
* delegate
) OVERRIDE
;
115 virtual void WidgetHidden() OVERRIDE
;
116 virtual void WidgetRestored() OVERRIDE
;
117 virtual void EnsureWebKitInitialized() OVERRIDE
;
118 virtual void RecordUserMetrics(const std::string
& action
) OVERRIDE
;
119 virtual base::SharedMemoryHandle
HostAllocateSharedMemoryBuffer(
120 uint32 buffer_size
) OVERRIDE
;
121 virtual void RegisterExtension(v8::Extension
* extension
) OVERRIDE
;
122 virtual void ScheduleIdleHandler(int64 initial_delay_ms
) OVERRIDE
;
123 virtual void IdleHandler() OVERRIDE
;
124 virtual int64
GetIdleNotificationDelayInMs() const OVERRIDE
;
125 virtual void SetIdleNotificationDelayInMs(
126 int64 idle_notification_delay_in_ms
) OVERRIDE
;
127 virtual void ToggleWebKitSharedTimer(bool suspend
) OVERRIDE
;
128 virtual void UpdateHistograms(int sequence_number
) OVERRIDE
;
130 virtual void PreCacheFont(const LOGFONT
& log_font
) OVERRIDE
;
131 virtual void ReleaseCachedFonts() OVERRIDE
;
135 virtual bool IsWebFrameValid(WebKit::WebFrame
* frame
) OVERRIDE
;
137 // GpuChannelHostFactory implementation:
138 virtual bool IsMainThread() OVERRIDE
;
139 virtual bool IsIOThread() OVERRIDE
;
140 virtual MessageLoop
* GetMainLoop() OVERRIDE
;
141 virtual scoped_refptr
<base::MessageLoopProxy
> GetIOLoopProxy() OVERRIDE
;
142 virtual base::WaitableEvent
* GetShutDownEvent() OVERRIDE
;
143 virtual scoped_ptr
<base::SharedMemory
> AllocateSharedMemory(
144 uint32 size
) OVERRIDE
;
145 virtual int32
CreateViewCommandBuffer(
147 const GPUCreateCommandBufferConfig
& init_params
) OVERRIDE
;
148 virtual void CreateImage(
149 gfx::PluginWindowHandle window
,
151 const CreateImageCallback
& callback
) OVERRIDE
;
152 virtual void DeleteImage(int32 image_id
, int32 sync_point
) OVERRIDE
;
154 // Synchronously establish a channel to the GPU plugin if not previously
155 // established or if it has been lost (for example if the GPU plugin crashed).
156 // If there is a pending asynchronous request, it will be completed by the
157 // time this routine returns.
158 virtual GpuChannelHost
* EstablishGpuChannelSync(CauseForGpuLaunch
) OVERRIDE
;
161 // These methods modify how the next message is sent. Normally, when sending
162 // a synchronous message that runs a nested message loop, we need to suspend
163 // callbacks into WebKit. This involves disabling timers and deferring
164 // resource loads. However, there are exceptions when we need to customize
166 void DoNotSuspendWebKitSharedTimer();
167 void DoNotNotifyWebKitOfModalLoop();
169 IPC::ForwardingMessageFilter
* compositor_output_surface_filter() const {
170 return compositor_output_surface_filter_
.get();
173 // Will be NULL if threaded compositing has not been enabled.
174 CompositorThread
* compositor_thread() const {
175 return compositor_thread_
.get();
178 AppCacheDispatcher
* appcache_dispatcher() const {
179 return appcache_dispatcher_
.get();
182 DomStorageDispatcher
* dom_storage_dispatcher() const {
183 return dom_storage_dispatcher_
.get();
186 AudioInputMessageFilter
* audio_input_message_filter() {
187 return audio_input_message_filter_
.get();
190 AudioMessageFilter
* audio_message_filter() {
191 return audio_message_filter_
.get();
196 // Creates the embedder implementation of WebMediaStreamCenter.
197 // The resulting object is owned by WebKit and deleted by WebKit at tear-down.
198 WebKit::WebMediaStreamCenter
* CreateMediaStreamCenter(
199 WebKit::WebMediaStreamCenterClient
* client
);
201 // Returns a factory used for creating RTC PeerConnection objects.
202 MediaStreamDependencyFactory
* GetMediaStreamDependencyFactory();
204 // Current P2PSocketDispatcher. Set to NULL if P2P API is disabled.
205 P2PSocketDispatcher
* p2p_socket_dispatcher() {
206 return p2p_socket_dispatcher_
.get();
209 VideoCaptureImplManager
* video_capture_impl_manager() const {
210 return vc_manager_
.get();
213 // Get the GPU channel. Returns NULL if the channel is not established or
215 GpuChannelHost
* GetGpuChannel();
217 // Returns a MessageLoopProxy instance corresponding to the message loop
218 // of the thread on which file operations should be run. Must be called
219 // on the renderer's main thread.
220 scoped_refptr
<base::MessageLoopProxy
> GetFileThreadMessageLoopProxy();
222 // Causes the idle handler to skip sending idle notifications
223 // on the two next scheduled calls, so idle notifications are
224 // not sent for at least one notification delay.
225 void PostponeIdleNotification();
227 // Returns a graphics context shared among all
228 // RendererGpuVideoDecoderFactories, or NULL on error. Context remains owned
229 // by this class and must be null-tested before each use to detect context
230 // loss. The returned context is only valid on the compositor thread when
231 // threaded compositing is enabled.
232 WebGraphicsContext3DCommandBufferImpl
* GetGpuVDAContext3D();
234 // Handle loss of the shared GpuVDAContext3D context above.
235 static void OnGpuVDAContextLoss();
237 // AudioRendererMixerManager instance which manages renderer side mixer
238 // instances shared based on configured audio parameters. Lazily created on
240 AudioRendererMixerManager
* GetAudioRendererMixerManager();
242 // For producing custom V8 histograms. Custom histograms are produced if all
243 // RenderViews share the same host, and the host is in the pre-specified set
244 // of hosts we want to produce custom diagrams for. The name for a custom
245 // diagram is the name of the corresponding generic diagram plus a
246 // host-specific suffix.
247 class CONTENT_EXPORT HistogramCustomizer
{
249 HistogramCustomizer();
250 ~HistogramCustomizer();
252 // Called when a top frame of a RenderView navigates. This function updates
253 // RenderThreadImpl's information about whether all RenderViews are
254 // displaying a page from the same host. |host| is the host where a
255 // RenderView navigated, and |view_count| is the number of RenderViews in
257 void RenderViewNavigatedToHost(const std::string
& host
, size_t view_count
);
259 // Used for customizing some histograms if all RenderViews share the same
260 // host. Returns the current custom histogram name to use for
261 // |histogram_name|, or |histogram_name| if it shouldn't be customized.
262 std::string
ConvertToCustomHistogramName(const char* histogram_name
) const;
265 friend class RenderThreadImplUnittest
;
267 // Used for updating the information on which is the common host which all
268 // RenderView's share (if any). If there is no common host, this function is
269 // called with an empty string.
270 void SetCommonHost(const std::string
& host
);
272 // The current common host of the RenderViews; empty string if there is no
274 std::string common_host_
;
275 // The corresponding suffix.
276 std::string common_host_histogram_suffix_
;
277 // Set of histograms for which we want to produce a custom histogram if
279 std::set
<std::string
> custom_histograms_
;
281 DISALLOW_COPY_AND_ASSIGN(HistogramCustomizer
);
284 HistogramCustomizer
* histogram_customizer() {
285 return &histogram_customizer_
;
289 virtual bool OnControlMessageReceived(const IPC::Message
& msg
) OVERRIDE
;
293 void OnSetZoomLevelForCurrentURL(const std::string
& host
, double zoom_level
);
294 void OnCreateNewView(const ViewMsg_New_Params
& params
);
295 void OnTransferBitmap(const SkBitmap
& bitmap
, int resource_id
);
296 void OnPurgePluginListCache(bool reload_pages
);
297 void OnNetworkStateChanged(bool online
);
298 void OnGetAccessibilityTree();
299 void OnTempCrashWithData(const GURL
& data
);
301 void IdleHandlerInForegroundTab();
303 // These objects live solely on the render thread.
304 scoped_ptr
<AppCacheDispatcher
> appcache_dispatcher_
;
305 scoped_ptr
<DomStorageDispatcher
> dom_storage_dispatcher_
;
306 scoped_ptr
<IndexedDBDispatcher
> main_thread_indexed_db_dispatcher_
;
307 scoped_ptr
<RendererWebKitPlatformSupportImpl
> webkit_platform_support_
;
309 // Used on the render thread and deleted by WebKit at shutdown.
310 MediaStreamCenter
* media_stream_center_
;
312 // Used on the renderer and IPC threads.
313 scoped_refptr
<DBMessageFilter
> db_message_filter_
;
314 scoped_refptr
<AudioInputMessageFilter
> audio_input_message_filter_
;
315 scoped_refptr
<AudioMessageFilter
> audio_message_filter_
;
316 scoped_refptr
<DevToolsAgentFilter
> devtools_agent_message_filter_
;
318 scoped_ptr
<MediaStreamDependencyFactory
> media_stream_factory_
;
320 // Dispatches all P2P sockets.
321 scoped_refptr
<P2PSocketDispatcher
> p2p_socket_dispatcher_
;
323 // Used on multiple threads.
324 scoped_refptr
<VideoCaptureImplManager
> vc_manager_
;
326 // Used on multiple script execution context threads.
327 scoped_ptr
<WebDatabaseObserverImpl
> web_database_observer_impl_
;
330 // Initialize COM when using plugins outside the sandbox.
331 scoped_ptr
<base::win::ScopedCOMInitializer
> initialize_com_
;
334 // The count of RenderWidgets running through this thread.
337 // The count of hidden RenderWidgets running through this thread.
338 int hidden_widget_count_
;
340 // The current value of the idle notification timer delay.
341 int64 idle_notification_delay_in_ms_
;
343 // The number of idle handler calls that skip sending idle notifications.
344 int idle_notifications_to_skip_
;
346 bool suspend_webkit_shared_timer_
;
347 bool notify_webkit_of_modal_loop_
;
349 // Timer that periodically calls IdleHandler.
350 base::RepeatingTimer
<RenderThreadImpl
> idle_timer_
;
352 // The channel from the renderer process to the GPU process.
353 scoped_refptr
<GpuChannelHost
> gpu_channel_
;
355 // A lazily initiated thread on which file operations are run.
356 scoped_ptr
<base::Thread
> file_thread_
;
358 bool compositor_initialized_
;
359 scoped_ptr
<CompositorThread
> compositor_thread_
;
360 scoped_refptr
<IPC::ForwardingMessageFilter
> compositor_output_surface_filter_
;
362 ObserverList
<RenderProcessObserver
> observers_
;
364 class GpuVDAContextLostCallback
;
365 scoped_ptr
<GpuVDAContextLostCallback
> context_lost_cb_
;
366 scoped_ptr
<WebGraphicsContext3DCommandBufferImpl
> gpu_vda_context3d_
;
368 scoped_ptr
<AudioRendererMixerManager
> audio_renderer_mixer_manager_
;
370 HistogramCustomizer histogram_customizer_
;
372 DISALLOW_COPY_AND_ASSIGN(RenderThreadImpl
);
375 } // namespace content
377 #endif // CONTENT_RENDERER_RENDER_THREAD_IMPL_H_