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 WebGamepadListener; 40 class WebGraphicsContext3D; 41 class WebMediaStreamCenter; 42 class WebMediaStreamCenterClient; 43 } 44 45 namespace base { 46 class MessageLoopProxy; 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 GamepadSharedMemoryReader; 88 class GpuChannelHost; 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; 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 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); 176 177 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 182 // the behavior. 183 void DoNotSuspendWebKitSharedTimer(); 184 void DoNotNotifyWebKitOfModalLoop(); 185 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_; 191 } 192 void set_layout_test_mode(bool layout_test_mode) { 193 layout_test_mode_ = layout_test_mode; 194 } 195 196 RendererWebKitPlatformSupportImpl* webkit_platform_support() const { 197 DCHECK(webkit_platform_support_); 198 return webkit_platform_support_.get(); 199 } 200 201 IPC::ForwardingMessageFilter* compositor_output_surface_filter() const { 202 return compositor_output_surface_filter_.get(); 203 } 204 205 InputHandlerManager* input_handler_manager() const { 206 return input_handler_manager_.get(); 207 } 208 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_; 212 } 213 214 bool is_gpu_rasterization_enabled() const { 215 return is_gpu_rasterization_enabled_; 216 } 217 218 bool is_gpu_rasterization_forced() const { 219 return is_gpu_rasterization_forced_; 220 } 221 222 bool is_impl_side_painting_enabled() const { 223 return is_impl_side_painting_enabled_; 224 } 225 226 bool is_low_res_tiling_enabled() const { return is_low_res_tiling_enabled_; } 227 228 bool is_lcd_text_enabled() const { return is_lcd_text_enabled_; } 229 230 bool is_distance_field_text_enabled() const { 231 return is_distance_field_text_enabled_; 232 } 233 234 bool is_zero_copy_enabled() const { return is_zero_copy_enabled_; } 235 236 bool is_one_copy_enabled() const { return is_one_copy_enabled_; } 237 238 AppCacheDispatcher* appcache_dispatcher() const { 239 return appcache_dispatcher_.get(); 240 } 241 242 DomStorageDispatcher* dom_storage_dispatcher() const { 243 return dom_storage_dispatcher_.get(); 244 } 245 246 EmbeddedWorkerDispatcher* embedded_worker_dispatcher() const { 247 return embedded_worker_dispatcher_.get(); 248 } 249 250 AudioInputMessageFilter* audio_input_message_filter() { 251 return audio_input_message_filter_.get(); 252 } 253 254 AudioMessageFilter* audio_message_filter() { 255 return audio_message_filter_.get(); 256 } 257 258 MidiMessageFilter* midi_message_filter() { 259 return midi_message_filter_.get(); 260 } 261 262 #if defined(OS_ANDROID) 263 RendererDemuxerAndroid* renderer_demuxer() { 264 return renderer_demuxer_.get(); 265 } 266 #endif 267 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); 272 273 // Returns a factory used for creating RTC PeerConnection objects. 274 PeerConnectionDependencyFactory* GetPeerConnectionDependencyFactory(); 275 276 PeerConnectionTracker* peer_connection_tracker() { 277 return peer_connection_tracker_.get(); 278 } 279 280 // Current P2PSocketDispatcher. Set to NULL if P2P API is disabled. 281 P2PSocketDispatcher* p2p_socket_dispatcher() { 282 return p2p_socket_dispatcher_.get(); 283 } 284 285 VideoCaptureImplManager* video_capture_impl_manager() const { 286 return vc_manager_.get(); 287 } 288 289 GamepadSharedMemoryReader* gamepad_shared_memory_reader() const { 290 return gamepad_shared_memory_reader_.get(); 291 } 292 293 // Get the GPU channel. Returns NULL if the channel is not established or 294 // has been lost. 295 GpuChannelHost* GetGpuChannel(); 296 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(); 301 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(); 306 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(); 311 312 scoped_refptr<media::GpuVideoAcceleratorFactories> GetGpuFactories(); 313 314 scoped_refptr<webkit::gpu::ContextProviderWebContext> 315 SharedMainThreadContextProvider(); 316 317 // AudioRendererMixerManager instance which manages renderer side mixer 318 // instances shared based on configured audio parameters. Lazily created on 319 // first call. 320 AudioRendererMixerManager* GetAudioRendererMixerManager(); 321 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(); 326 327 #if defined(OS_WIN) 328 void PreCacheFontCharacters(const LOGFONT& log_font, 329 const base::string16& str); 330 #endif 331 332 #if defined(ENABLE_WEBRTC) 333 WebRTCIdentityService* get_webrtc_identity_service() { 334 return webrtc_identity_service_.get(); 335 } 336 #endif 337 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 { 344 public: 345 HistogramCustomizer(); 346 ~HistogramCustomizer(); 347 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 352 // this process. 353 void RenderViewNavigatedToHost(const std::string& host, size_t view_count); 354 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; 359 360 private: 361 friend class RenderThreadImplUnittest; 362 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); 367 368 // The current common host of the RenderViews; empty string if there is no 369 // common host. 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 374 // possible. 375 std::set<std::string> custom_histograms_; 376 377 DISALLOW_COPY_AND_ASSIGN(HistogramCustomizer); 378 }; 379 380 HistogramCustomizer* histogram_customizer() { 381 return &histogram_customizer_; 382 } 383 384 void SetFlingCurveParameters(const std::vector<float>& new_touchpad, 385 const std::vector<float>& new_touchscreen); 386 387 // Retrieve current gamepad data. 388 void SampleGamepads(blink::WebGamepads* data); 389 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); 393 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(); 398 void WidgetHidden(); 399 void WidgetRestored(); 400 401 void AddEmbeddedWorkerRoute(int32 routing_id, IPC::Listener* listener); 402 void RemoveEmbeddedWorkerRoute(int32 routing_id); 403 404 private: 405 // ChildThread 406 virtual bool OnControlMessageReceived(const IPC::Message& msg) OVERRIDE; 407 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( 415 int32 surface_id, 416 const GPUCreateCommandBufferConfig& init_params, 417 int32 route_id) OVERRIDE; 418 virtual void CreateImage( 419 gfx::PluginWindowHandle window, 420 int32 image_id, 421 const CreateImageCallback& callback) OVERRIDE; 422 virtual void DeleteImage(int32 image_id, int32 sync_point) OVERRIDE; 423 virtual scoped_ptr<gfx::GpuMemoryBuffer> AllocateGpuMemoryBuffer( 424 size_t width, 425 size_t height, 426 unsigned internalformat, 427 unsigned usage) OVERRIDE; 428 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; 435 436 void Init(); 437 438 void OnSetZoomLevelForCurrentURL(const std::string& scheme, 439 const std::string& host, 440 double zoom_level); 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); 452 #endif 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, 458 bool redraw); 459 #endif 460 void OnCreateNewSharedWorker( 461 const WorkerProcessMsg_CreateWorker_Params& params); 462 463 void IdleHandlerInForegroundTab(); 464 465 scoped_ptr<WebGraphicsContext3DCommandBufferImpl> CreateOffscreenContext3d(); 466 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_; 473 474 // Used on the render thread and deleted by WebKit at shutdown. 475 blink::WebMediaStreamCenter* media_stream_center_; 476 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_; 484 #endif 485 scoped_refptr<DevToolsAgentFilter> devtools_agent_message_filter_; 486 487 scoped_ptr<PeerConnectionDependencyFactory> peer_connection_factory_; 488 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_; 492 493 // Dispatches all P2P sockets. 494 scoped_refptr<P2PSocketDispatcher> p2p_socket_dispatcher_; 495 496 // Used on the render thread. 497 scoped_ptr<VideoCaptureImplManager> vc_manager_; 498 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_; 504 505 // The count of RenderWidgets running through this thread. 506 int widget_count_; 507 508 // The count of hidden RenderWidgets running through this thread. 509 int hidden_widget_count_; 510 511 // The current value of the idle notification timer delay. 512 int64 idle_notification_delay_in_ms_; 513 514 // The number of idle handler calls that skip sending idle notifications. 515 int idle_notifications_to_skip_; 516 517 bool suspend_webkit_shared_timer_; 518 bool notify_webkit_of_modal_loop_; 519 bool webkit_shared_timer_suspended_; 520 521 // The following flag is used to control layout test specific behavior. 522 bool layout_test_mode_; 523 524 // Timer that periodically calls IdleHandler. 525 base::RepeatingTimer<RenderThreadImpl> idle_timer_; 526 527 // The channel from the renderer process to the GPU process. 528 scoped_refptr<GpuChannelHost> gpu_channel_; 529 530 // Cache of variables that are needed on the compositor thread by 531 // GpuChannelHostFactory methods. 532 scoped_refptr<base::MessageLoopProxy> io_message_loop_proxy_; 533 534 // A lazily initiated thread on which file operations are run. 535 scoped_ptr<base::Thread> file_thread_; 536 537 // May be null if overridden by ContentRendererClient. 538 scoped_ptr<base::Thread> compositor_thread_; 539 540 // Thread for running multimedia operations (e.g., video decoding). 541 scoped_ptr<base::Thread> media_thread_; 542 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_; 546 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_; 551 552 scoped_refptr<ContextProviderCommandBuffer> shared_main_thread_contexts_; 553 554 ObserverList<RenderProcessObserver> observers_; 555 556 scoped_refptr<ContextProviderCommandBuffer> gpu_va_context_provider_; 557 558 scoped_ptr<AudioRendererMixerManager> audio_renderer_mixer_manager_; 559 scoped_ptr<media::AudioHardwareConfig> audio_hardware_config_; 560 561 HistogramCustomizer histogram_customizer_; 562 563 scoped_ptr<base::MemoryPressureListener> memory_pressure_listener_; 564 565 scoped_ptr<WebRTCIdentityService> webrtc_identity_service_; 566 567 scoped_ptr<GamepadSharedMemoryReader> gamepad_shared_memory_reader_; 568 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_; 573 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_; 583 584 DISALLOW_COPY_AND_ASSIGN(RenderThreadImpl); 585 }; 586 587 } // namespace content 588 589 #endif // CONTENT_RENDERER_RENDER_THREAD_IMPL_H_ 590