Upstream version 10.39.225.0
[platform/framework/web/crosswalk.git] / src / content / renderer / render_thread_impl.h
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.
4
5 #ifndef CONTENT_RENDERER_RENDER_THREAD_IMPL_H_
6 #define CONTENT_RENDERER_RENDER_THREAD_IMPL_H_
7
8 #include <set>
9 #include <string>
10 #include <vector>
11
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"
27
28 #if defined(OS_MACOSX)
29 #include "third_party/WebKit/public/web/mac/WebScrollbarTheme.h"
30 #endif
31
32 class GrContext;
33 class SkBitmap;
34 struct ViewMsg_New_Params;
35 struct WorkerProcessMsg_CreateWorker_Params;
36
37 namespace blink {
38 class WebGamepads;
39 class WebGraphicsContext3D;
40 class WebMediaStreamCenter;
41 class WebMediaStreamCenterClient;
42 }
43
44 namespace base {
45 class MessageLoopProxy;
46 class SingleThreadTaskRunner;
47 class Thread;
48 }
49
50 namespace cc {
51 class ContextProvider;
52 }
53
54 namespace IPC {
55 class ForwardingMessageFilter;
56 class MessageFilter;
57 }
58
59 namespace media {
60 class AudioHardwareConfig;
61 class GpuVideoAcceleratorFactories;
62 }
63
64 namespace v8 {
65 class Extension;
66 }
67
68 namespace webkit {
69 namespace gpu {
70 class ContextProviderWebContext;
71 class GrContextForWebGraphicsContext3D;
72 }
73 }
74
75 namespace content {
76
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 GpuChannelHost;
88 class IndexedDBDispatcher;
89 class InputEventFilter;
90 class InputHandlerManager;
91 class MediaStreamCenter;
92 class MemoryObserver;
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;
104
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).
110 //
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,
115                                         public ChildThread,
116                                         public GpuChannelHostFactory {
117  public:
118   static RenderThreadImpl* current();
119
120   RenderThreadImpl();
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;
125
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();
129
130   // Notify V8 that the date/time configuration of the system might have
131   // changed.
132   static void NotifyTimezoneChange();
133
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()
141       OVERRIDE;
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;
166 #if defined(OS_WIN)
167   virtual void PreCacheFont(const LOGFONT& log_font) OVERRIDE;
168   virtual void ReleaseCachedFonts() OVERRIDE;
169 #endif
170   virtual ServiceRegistry* GetServiceRegistry() OVERRIDE;
171
172   // Synchronously establish a channel to the GPU plugin if not previously
173   // established or if it has been lost (for example if the GPU plugin crashed).
174   // If there is a pending asynchronous request, it will be completed by the
175   // time this routine returns.
176   GpuChannelHost* EstablishGpuChannelSync(CauseForGpuLaunch);
177
178
179   // These methods modify how the next message is sent.  Normally, when sending
180   // a synchronous message that runs a nested message loop, we need to suspend
181   // callbacks into WebKit.  This involves disabling timers and deferring
182   // resource loads.  However, there are exceptions when we need to customize
183   // the behavior.
184   void DoNotSuspendWebKitSharedTimer();
185   void DoNotNotifyWebKitOfModalLoop();
186
187   // True if we are running layout tests. This currently disables forwarding
188   // various status messages to the console, skips network error pages, and
189   // short circuits size update and focus events.
190   bool layout_test_mode() const {
191     return layout_test_mode_;
192   }
193   void set_layout_test_mode(bool layout_test_mode) {
194     layout_test_mode_ = layout_test_mode;
195   }
196
197   RendererWebKitPlatformSupportImpl* webkit_platform_support() const {
198     DCHECK(webkit_platform_support_);
199     return webkit_platform_support_.get();
200   }
201
202   scoped_refptr<base::SingleThreadTaskRunner>
203   main_thread_compositor_task_runner() const {
204     return main_thread_compositor_task_runner_;
205   }
206
207   IPC::ForwardingMessageFilter* compositor_output_surface_filter() const {
208     return compositor_output_surface_filter_.get();
209   }
210
211   InputHandlerManager* input_handler_manager() const {
212     return input_handler_manager_.get();
213   }
214
215   // Will be NULL if threaded compositing has not been enabled.
216   scoped_refptr<base::MessageLoopProxy> compositor_message_loop_proxy() const {
217     return compositor_message_loop_proxy_;
218   }
219
220   bool is_gpu_rasterization_enabled() const {
221     return is_gpu_rasterization_enabled_;
222   }
223
224   bool is_gpu_rasterization_forced() const {
225     return is_gpu_rasterization_forced_;
226   }
227
228   bool is_impl_side_painting_enabled() const {
229     return is_impl_side_painting_enabled_;
230   }
231
232   bool is_lcd_text_enabled() const { return is_lcd_text_enabled_; }
233
234   bool is_distance_field_text_enabled() const {
235     return is_distance_field_text_enabled_;
236   }
237
238   bool is_zero_copy_enabled() const { return is_zero_copy_enabled_; }
239
240   bool is_one_copy_enabled() const { return is_one_copy_enabled_; }
241
242   AppCacheDispatcher* appcache_dispatcher() const {
243     return appcache_dispatcher_.get();
244   }
245
246   DomStorageDispatcher* dom_storage_dispatcher() const {
247     return dom_storage_dispatcher_.get();
248   }
249
250   EmbeddedWorkerDispatcher* embedded_worker_dispatcher() const {
251     return embedded_worker_dispatcher_.get();
252   }
253
254   AudioInputMessageFilter* audio_input_message_filter() {
255     return audio_input_message_filter_.get();
256   }
257
258   AudioMessageFilter* audio_message_filter() {
259     return audio_message_filter_.get();
260   }
261
262   MidiMessageFilter* midi_message_filter() {
263     return midi_message_filter_.get();
264   }
265
266 #if defined(OS_ANDROID)
267   RendererDemuxerAndroid* renderer_demuxer() {
268     return renderer_demuxer_.get();
269   }
270 #endif
271
272   // Creates the embedder implementation of WebMediaStreamCenter.
273   // The resulting object is owned by WebKit and deleted by WebKit at tear-down.
274   blink::WebMediaStreamCenter* CreateMediaStreamCenter(
275       blink::WebMediaStreamCenterClient* client);
276
277 #if defined(ENABLE_WEBRTC)
278   // Returns a factory used for creating RTC PeerConnection objects.
279   PeerConnectionDependencyFactory* GetPeerConnectionDependencyFactory();
280
281   PeerConnectionTracker* peer_connection_tracker() {
282     return peer_connection_tracker_.get();
283   }
284
285   // Current P2PSocketDispatcher. Set to NULL if P2P API is disabled.
286   P2PSocketDispatcher* p2p_socket_dispatcher() {
287     return p2p_socket_dispatcher_.get();
288   }
289 #endif
290
291   VideoCaptureImplManager* video_capture_impl_manager() const {
292     return vc_manager_.get();
293   }
294
295   // Get the GPU channel. Returns NULL if the channel is not established or
296   // has been lost.
297   GpuChannelHost* GetGpuChannel();
298
299   // Returns a MessageLoopProxy instance corresponding to the message loop
300   // of the thread on which file operations should be run. Must be called
301   // on the renderer's main thread.
302   scoped_refptr<base::MessageLoopProxy> GetFileThreadMessageLoopProxy();
303
304   // Returns a SingleThreadTaskRunner instance corresponding to the message loop
305   // of the thread on which media operations should be run. Must be called
306   // on the renderer's main thread.
307   scoped_refptr<base::SingleThreadTaskRunner> GetMediaThreadTaskRunner();
308
309   // Causes the idle handler to skip sending idle notifications
310   // on the two next scheduled calls, so idle notifications are
311   // not sent for at least one notification delay.
312   void PostponeIdleNotification();
313
314   scoped_refptr<media::GpuVideoAcceleratorFactories> GetGpuFactories();
315
316   scoped_refptr<webkit::gpu::ContextProviderWebContext>
317       SharedMainThreadContextProvider();
318
319   // AudioRendererMixerManager instance which manages renderer side mixer
320   // instances shared based on configured audio parameters.  Lazily created on
321   // first call.
322   AudioRendererMixerManager* GetAudioRendererMixerManager();
323
324   // AudioHardwareConfig contains audio hardware configuration for
325   // renderer side clients.  Creation requires a synchronous IPC call so it is
326   // lazily created on the first call.
327   media::AudioHardwareConfig* GetAudioHardwareConfig();
328
329 #if defined(OS_WIN)
330   void PreCacheFontCharacters(const LOGFONT& log_font,
331                               const base::string16& str);
332 #endif
333
334 #if defined(ENABLE_WEBRTC)
335   WebRTCIdentityService* get_webrtc_identity_service() {
336     return webrtc_identity_service_.get();
337   }
338 #endif
339
340   // For producing custom V8 histograms. Custom histograms are produced if all
341   // RenderViews share the same host, and the host is in the pre-specified set
342   // of hosts we want to produce custom diagrams for. The name for a custom
343   // diagram is the name of the corresponding generic diagram plus a
344   // host-specific suffix.
345   class CONTENT_EXPORT HistogramCustomizer {
346    public:
347     HistogramCustomizer();
348     ~HistogramCustomizer();
349
350     // Called when a top frame of a RenderView navigates. This function updates
351     // RenderThreadImpl's information about whether all RenderViews are
352     // displaying a page from the same host. |host| is the host where a
353     // RenderView navigated, and |view_count| is the number of RenderViews in
354     // this process.
355     void RenderViewNavigatedToHost(const std::string& host, size_t view_count);
356
357     // Used for customizing some histograms if all RenderViews share the same
358     // host. Returns the current custom histogram name to use for
359     // |histogram_name|, or |histogram_name| if it shouldn't be customized.
360     std::string ConvertToCustomHistogramName(const char* histogram_name) const;
361
362    private:
363     friend class RenderThreadImplUnittest;
364
365     // Used for updating the information on which is the common host which all
366     // RenderView's share (if any). If there is no common host, this function is
367     // called with an empty string.
368     void SetCommonHost(const std::string& host);
369
370     // The current common host of the RenderViews; empty string if there is no
371     // common host.
372     std::string common_host_;
373     // The corresponding suffix.
374     std::string common_host_histogram_suffix_;
375     // Set of histograms for which we want to produce a custom histogram if
376     // possible.
377     std::set<std::string> custom_histograms_;
378
379     DISALLOW_COPY_AND_ASSIGN(HistogramCustomizer);
380   };
381
382   HistogramCustomizer* histogram_customizer() {
383     return &histogram_customizer_;
384   }
385
386   // Retrieve current gamepad data.
387   void SampleGamepads(blink::WebGamepads* data);
388
389   // Called by a RenderWidget when it is created or destroyed. This
390   // allows the process to know when there are no visible widgets.
391   void WidgetCreated();
392   void WidgetDestroyed();
393   void WidgetHidden();
394   void WidgetRestored();
395
396   void AddEmbeddedWorkerRoute(int32 routing_id, IPC::Listener* listener);
397   void RemoveEmbeddedWorkerRoute(int32 routing_id);
398
399   void RegisterPendingRenderFrameConnect(int routing_id,
400                                          mojo::ScopedMessagePipeHandle handle);
401
402  private:
403   // ChildThread
404   virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE;
405
406   // GpuChannelHostFactory implementation:
407   virtual bool IsMainThread() OVERRIDE;
408   virtual base::MessageLoop* GetMainLoop() OVERRIDE;
409   virtual scoped_refptr<base::MessageLoopProxy> GetIOLoopProxy() OVERRIDE;
410   virtual scoped_ptr<base::SharedMemory> AllocateSharedMemory(
411       size_t size) OVERRIDE;
412   virtual CreateCommandBufferResult CreateViewCommandBuffer(
413       int32 surface_id,
414       const GPUCreateCommandBufferConfig& init_params,
415       int32 route_id) OVERRIDE;
416   virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer(
417       size_t width,
418       size_t height,
419       unsigned internalformat,
420       unsigned usage) OVERRIDE;
421
422   void Init();
423
424   void OnCreateNewFrame(int routing_id, int parent_routing_id);
425   void OnCreateNewFrameProxy(int routing_id,
426                              int parent_routing_id,
427                              int render_view_routing_id);
428   void OnSetZoomLevelForCurrentURL(const std::string& scheme,
429                                    const std::string& host,
430                                    double zoom_level);
431   void OnCreateNewView(const ViewMsg_New_Params& params);
432   void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
433 #if defined(ENABLE_PLUGINS)
434   void OnPurgePluginListCache(bool reload_pages);
435 #endif
436   void OnNetworkTypeChanged(net::NetworkChangeNotifier::ConnectionType type);
437   void OnGetAccessibilityTree();
438   void OnTempCrashWithData(const GURL& data);
439   void OnUpdateTimezone();
440   void OnMemoryPressure(
441       base::MemoryPressureListener::MemoryPressureLevel memory_pressure_level);
442 #if defined(OS_ANDROID)
443   void OnSetWebKitSharedTimersSuspended(bool suspend);
444 #endif
445 #if defined(OS_MACOSX)
446   void OnUpdateScrollbarTheme(float initial_button_delay,
447                               float autoscroll_button_delay,
448                               bool jump_on_track_click,
449                               blink::ScrollerStyle preferred_scroller_style,
450                               bool redraw);
451 #endif
452   void OnCreateNewSharedWorker(
453       const WorkerProcessMsg_CreateWorker_Params& params);
454
455   scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateOffscreenContext3d();
456
457   // These objects live solely on the render thread.
458   scoped_ptr<AppCacheDispatcher> appcache_dispatcher_;
459   scoped_ptr<DomStorageDispatcher> dom_storage_dispatcher_;
460   scoped_ptr<IndexedDBDispatcher> main_thread_indexed_db_dispatcher_;
461   scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_;
462   scoped_ptr<EmbeddedWorkerDispatcher> embedded_worker_dispatcher_;
463
464   // Used on the render thread and deleted by WebKit at shutdown.
465   blink::WebMediaStreamCenter* media_stream_center_;
466
467   // Used on the renderer and IPC threads.
468   scoped_refptr<DBMessageFilter> db_message_filter_;
469   scoped_refptr<AudioInputMessageFilter> audio_input_message_filter_;
470   scoped_refptr<AudioMessageFilter> audio_message_filter_;
471   scoped_refptr<MidiMessageFilter> midi_message_filter_;
472 #if defined(OS_ANDROID)
473   scoped_refptr<RendererDemuxerAndroid> renderer_demuxer_;
474 #endif
475   scoped_refptr<DevToolsAgentFilter> devtools_agent_message_filter_;
476
477 #if defined(ENABLE_WEBRTC)
478   scoped_ptr<PeerConnectionDependencyFactory> peer_connection_factory_;
479
480   // This is used to communicate to the browser process the status
481   // of all the peer connections created in the renderer.
482   scoped_ptr<PeerConnectionTracker> peer_connection_tracker_;
483
484   // Dispatches all P2P sockets.
485   scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_;
486 #endif
487
488   // Used on the render thread.
489   scoped_ptr<VideoCaptureImplManager> vc_manager_;
490
491   // Used for communicating registering AEC dump consumers with the browser and
492   // receving AEC dump file handles when AEC dump is enabled. An AEC dump is
493   // diagnostic audio data for WebRTC stored locally when enabled by the user in
494   // chrome://webrtc-internals.
495   scoped_refptr<AecDumpMessageFilter> aec_dump_message_filter_;
496
497   // The count of RenderWidgets running through this thread.
498   int widget_count_;
499
500   // The count of hidden RenderWidgets running through this thread.
501   int hidden_widget_count_;
502
503   // The current value of the idle notification timer delay.
504   int64 idle_notification_delay_in_ms_;
505
506   // The number of idle handler calls that skip sending idle notifications.
507   int idle_notifications_to_skip_;
508
509   bool suspend_webkit_shared_timer_;
510   bool notify_webkit_of_modal_loop_;
511   bool webkit_shared_timer_suspended_;
512
513   // The following flag is used to control layout test specific behavior.
514   bool layout_test_mode_;
515
516   // Timer that periodically calls IdleHandler.
517   base::RepeatingTimer<RenderThreadImpl> idle_timer_;
518
519   // The channel from the renderer process to the GPU process.
520   scoped_refptr<GpuChannelHost> gpu_channel_;
521
522   // Cache of variables that are needed on the compositor thread by
523   // GpuChannelHostFactory methods.
524   scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_;
525
526   // A lazily initiated thread on which file operations are run.
527   scoped_ptr<base::Thread> file_thread_;
528
529   // May be null if overridden by ContentRendererClient.
530   scoped_ptr<base::Thread> compositor_thread_;
531
532   // Thread for running multimedia operations (e.g., video decoding).
533   scoped_ptr<base::Thread> media_thread_;
534
535   // Will point to appropriate MessageLoopProxy after initialization,
536   // regardless of whether |compositor_thread_| is overriden.
537   scoped_refptr<base::MessageLoopProxy> compositor_message_loop_proxy_;
538
539   // May be null if unused by the |input_handler_manager_|.
540   scoped_refptr<InputEventFilter> input_event_filter_;
541   scoped_ptr<InputHandlerManager> input_handler_manager_;
542   scoped_refptr<IPC::ForwardingMessageFilter> compositor_output_surface_filter_;
543
544   scoped_refptr<webkit::gpu::ContextProviderWebContext>
545       shared_main_thread_contexts_;
546
547   ObserverList<RenderProcessObserver> observers_;
548
549   scoped_refptr<ContextProviderCommandBuffer> gpu_va_context_provider_;
550
551   scoped_ptr<AudioRendererMixerManager> audio_renderer_mixer_manager_;
552   scoped_ptr<media::AudioHardwareConfig> audio_hardware_config_;
553
554   HistogramCustomizer histogram_customizer_;
555
556   scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_;
557
558 #if defined(ENABLE_WEBRTC)
559   scoped_ptr<WebRTCIdentityService> webrtc_identity_service_;
560 #endif
561
562   // TODO(reveman): Allow AllocateGpuMemoryBuffer to be called from
563   // multiple threads. Current allocation mechanism for IOSurface
564   // backed GpuMemoryBuffers prevent this. crbug.com/325045
565   base::ThreadChecker allocate_gpu_memory_buffer_thread_checker_;
566
567   scoped_ptr<MemoryObserver> memory_observer_;
568
569   scoped_refptr<base::SingleThreadTaskRunner>
570       main_thread_compositor_task_runner_;
571
572   // Compositor settings
573   bool is_gpu_rasterization_enabled_;
574   bool is_gpu_rasterization_forced_;
575   bool is_impl_side_painting_enabled_;
576   bool is_lcd_text_enabled_;
577   bool is_distance_field_text_enabled_;
578   bool is_zero_copy_enabled_;
579   bool is_one_copy_enabled_;
580
581   std::map<int, mojo::MessagePipeHandle> pending_render_frame_connects_;
582
583   DISALLOW_COPY_AND_ASSIGN(RenderThreadImpl);
584 };
585
586 }  // namespace content
587
588 #endif  // CONTENT_RENDERER_RENDER_THREAD_IMPL_H_