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