blob: da651ef0f9c9bc5d15ace0a75cc777b6dbf62186 [file] [log] [blame]
[email protected]1d4dbde2011-04-01 20:40:351// Copyright (c) 2011 The Chromium Authors. All rights reserved.
license.botbf09a502008-08-24 00:55:552// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
initial.commit09911bf2008-07-26 23:55:294
[email protected]10e6ab572011-04-14 23:42:005#ifndef CONTENT_RENDERER_RENDER_THREAD_H_
6#define CONTENT_RENDERER_RENDER_THREAD_H_
[email protected]32b76ef2010-07-26 23:08:247#pragma once
initial.commit09911bf2008-07-26 23:55:298
[email protected]6779aa12011-03-29 17:32:249#include <set>
[email protected]b68d5ed2009-04-16 02:41:2810#include <string>
[email protected]1bc83062009-02-06 00:16:3711#include <vector>
12
[email protected]1223d6ef2011-03-28 16:47:5013#include "base/observer_list.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/shared_memory.h"
[email protected]71d6d852009-12-07 22:12:3615#include "base/time.h"
16#include "base/timer.h"
[email protected]037fce02009-01-22 01:42:1517#include "build/build_config.h"
[email protected]7ef40ffe12011-03-08 05:05:2818#include "content/common/child_thread.h"
[email protected]8d128d62011-09-13 22:11:5719#include "content/common/content_export.h"
[email protected]778574e2011-03-21 22:03:5020#include "content/common/css_colors.h"
[email protected]202b54ff2011-04-22 21:36:3821#include "content/common/gpu/gpu_process_launch_causes.h"
[email protected]a83d42292010-08-17 22:51:1022#include "ipc/ipc_channel_proxy.h"
[email protected]08397d52011-02-05 01:53:3823#include "ui/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2924
[email protected]1edc16b82009-04-07 17:45:5425class AppCacheDispatcher;
[email protected]f7eb0a392011-07-12 10:19:5126class AudioInputMessageFilter;
27class AudioMessageFilter;
[email protected]017022b2009-07-27 23:06:3428class DBMessageFilter;
[email protected]82622452011-07-22 09:57:2029class DevToolsAgentFilter;
[email protected]39008c02009-02-11 23:59:2530class FilePath;
[email protected]e13ad79b2010-07-22 21:36:5031class GpuChannelHost;
[email protected]70c19a932010-05-14 12:59:1132class IndexedDBDispatcher;
[email protected]55e57d42009-02-25 06:10:1733class RendererHistogram;
[email protected]e13ad79b2010-07-22 21:36:5034class RendererHistogramSnapshots;
[email protected]1223d6ef2011-03-28 16:47:5035class RenderProcessObserver;
[email protected]74be069e82010-06-25 00:12:4936class RendererNetPredictor;
[email protected]8d6cba42011-09-02 10:05:1937class RendererWebKitPlatformSupportImpl;
[email protected]39008c02009-02-11 23:59:2538class SkBitmap;
[email protected]80b161a2011-06-27 17:42:1139class VideoCaptureImplManager;
[email protected]2b437e232010-04-02 01:30:0840class WebDatabaseObserverImpl;
[email protected]cccf90932009-08-23 17:56:2541
[email protected]9b6f40e2009-06-11 15:54:2642struct RendererPreferences;
[email protected]56879f932010-12-13 21:05:3743struct DOMStorageMsg_Event_Params;
[email protected]d6d8f712011-03-10 22:54:4344struct GPUInfo;
[email protected]4e6419c2010-01-15 04:50:3445struct ViewMsg_New_Params;
[email protected]39008c02009-02-11 23:59:2546struct WebPreferences;
initial.commit09911bf2008-07-26 23:55:2947
[email protected]c6a7b862010-08-20 22:19:3848namespace base {
49class MessageLoopProxy;
50class Thread;
51}
52
[email protected]46f36a492010-07-28 19:36:4153namespace IPC {
54struct ChannelHandle;
55}
56
[email protected]b7c7bcf2009-10-03 07:07:3457namespace WebKit {
58class WebStorageEventDispatcher;
59}
60
[email protected]af7eb3fb2010-09-23 21:31:0661namespace v8 {
62class Extension;
63}
64
[email protected]81a34412009-01-05 19:17:2465// The RenderThreadBase is the minimal interface that a RenderView/Widget
66// expects from a render thread. The interface basically abstracts a way to send
67// and receive messages.
[email protected]00c39612010-03-06 02:53:2868//
[email protected]f3ede412010-06-21 22:52:1669// TODO(brettw): This has two different and opposing usage patterns which
70// make it confusing.
71//
72// In the first mode, callers call RenderThread::current() to get the one and
73// only global RenderThread (bug 10837: this should be renamed get()). Then
74// they access it. Since RenderThread is a concrete class, this can be NULL
75// during unit tests. Callers need to NULL check this every time. Some callers
76// don't happen to get called during unit tests and don't do the NULL checks,
77// which is also confusing since it's not clear if you need to or not.
78//
79// In the second mode, the abstract base class RenderThreadBase is passed to
80// RenderView and RenderWidget. Normally, this points to
81// RenderThread::current() so it's quite confusing which accessing mode should
82// be used. However, during unit testing, this class is replaced with a mock
83// to support testing functions, and is guaranteed non-NULL.
84//
85// It might be nice not to have the ::current() call and put all of the
86// functions on the abstract class so they can be mocked. However, there are
87// some standalone functions like in ChromiumBridge that are not associated
88// with a view that need to access the current thread to send messages to the
89// browser process. These need the ::current() paradigm. So instead, we should
90// probably remove the render_thread_ parameter to RenderView/Widget in
91// preference to just getting the global singleton. We can make it easier to
92// understand by moving everything to the abstract interface and saying that
93// there should never be a NULL RenderThread::current(). Tests would be
94// responsible for setting up the mock one.
[email protected]8d128d62011-09-13 22:11:5795class CONTENT_EXPORT RenderThreadBase {
[email protected]8085dbc82008-09-26 22:53:4496 public:
97 virtual ~RenderThreadBase() {}
98
[email protected]8930d472009-02-21 08:05:2899 virtual bool Send(IPC::Message* msg) = 0;
100
[email protected]8085dbc82008-09-26 22:53:44101 // Called to add or remove a listener for a particular message routing ID.
102 // These methods normally get delegated to a MessageRouter.
103 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener) = 0;
104 virtual void RemoveRoute(int32 routing_id) = 0;
[email protected]81a34412009-01-05 19:17:24105
106 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
107 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
[email protected]bee16aab2009-08-26 15:55:03108
109 // Called by a RenderWidget when it is hidden or restored.
110 virtual void WidgetHidden() = 0;
111 virtual void WidgetRestored() = 0;
[email protected]8085dbc82008-09-26 22:53:44112};
113
initial.commit09911bf2008-07-26 23:55:29114// The RenderThread class represents a background thread where RenderView
115// instances live. The RenderThread supports an API that is used by its
116// consumer to talk indirectly to the RenderViews and supporting objects.
117// Likewise, it provides an API for the RenderViews to talk back to the main
[email protected]57c6a652009-05-04 07:58:34118// process (i.e., their corresponding TabContents).
initial.commit09911bf2008-07-26 23:55:29119//
120// Most of the communication occurs in the form of IPC messages. They are
121// routed to the RenderThread according to the routing IDs of the messages.
122// The routing IDs correspond to RenderView instances.
[email protected]8d128d62011-09-13 22:11:57123class CONTENT_EXPORT RenderThread : public RenderThreadBase,
124 public ChildThread {
initial.commit09911bf2008-07-26 23:55:29125 public:
[email protected]8930d472009-02-21 08:05:28126 // Grabs the IPC channel name from the command line.
127 RenderThread();
128 // Constructor that's used when running in single process mode.
[email protected]11f4857282009-11-13 19:56:17129 explicit RenderThread(const std::string& channel_name);
[email protected]8085dbc82008-09-26 22:53:44130 virtual ~RenderThread();
initial.commit09911bf2008-07-26 23:55:29131
[email protected]8930d472009-02-21 08:05:28132 // Returns the one render thread for this process. Note that this should only
133 // be accessed when running on the render thread itself
[email protected]00c39612010-03-06 02:53:28134 //
135 // TODO(brettw) this should be on the abstract base class instead of here,
136 // and return the base class' interface instead. Currently this causes
137 // problems with testing. See the comment above RenderThreadBase above.
[email protected]8930d472009-02-21 08:05:28138 static RenderThread* current();
initial.commit09911bf2008-07-26 23:55:29139
[email protected]c1f50aa2010-02-18 03:46:57140 // Returns the routing ID of the RenderWidget containing the current script
141 // execution context (corresponding to WebFrame::frameForCurrentContext).
142 static int32 RoutingIDForCurrentContext();
143
[email protected]45776222009-07-15 20:21:58144 // Overridden from RenderThreadBase.
[email protected]c1f50aa2010-02-18 03:46:57145 virtual bool Send(IPC::Message* msg);
146 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
147 virtual void RemoveRoute(int32 routing_id);
[email protected]81a34412009-01-05 19:17:24148 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
149 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter);
[email protected]bee16aab2009-08-26 15:55:03150 virtual void WidgetHidden();
151 virtual void WidgetRestored();
152
[email protected]1223d6ef2011-03-28 16:47:50153 void AddObserver(RenderProcessObserver* observer);
154 void RemoveObserver(RenderProcessObserver* observer);
155
[email protected]c1f50aa2010-02-18 03:46:57156 // These methods modify how the next message is sent. Normally, when sending
157 // a synchronous message that runs a nested message loop, we need to suspend
158 // callbacks into WebKit. This involves disabling timers and deferring
159 // resource loads. However, there are exceptions when we need to customize
160 // the behavior.
161 void DoNotSuspendWebKitSharedTimer();
162 void DoNotNotifyWebKitOfModalLoop();
163
[email protected]f430b5712009-08-21 21:46:31164 AppCacheDispatcher* appcache_dispatcher() const {
165 return appcache_dispatcher_.get();
166 }
167
[email protected]70c19a932010-05-14 12:59:11168 IndexedDBDispatcher* indexed_db_dispatcher() const {
169 return indexed_db_dispatcher_.get();
170 }
171
[email protected]f7eb0a392011-07-12 10:19:51172 AudioInputMessageFilter* audio_input_message_filter() {
173 return audio_input_message_filter_.get();
174 }
175
176 AudioMessageFilter* audio_message_filter() {
177 return audio_message_filter_.get();
178 }
179
[email protected]80b161a2011-06-27 17:42:11180 VideoCaptureImplManager* video_capture_impl_manager() const {
181 return vc_manager_.get();
182 }
183
[email protected]b547fd42009-04-23 23:16:27184 bool plugin_refresh_allowed() const { return plugin_refresh_allowed_; }
185
[email protected]6779aa12011-03-29 17:32:24186 double idle_notification_delay_in_s() const {
187 return idle_notification_delay_in_s_;
188 }
189 void set_idle_notification_delay_in_s(double idle_notification_delay_in_s) {
[email protected]d07495f2011-03-29 17:41:28190 idle_notification_delay_in_s_ = idle_notification_delay_in_s;
[email protected]6779aa12011-03-29 17:32:24191 }
192
[email protected]c40acc32010-01-14 01:02:53193 // Sends a message to the browser to close all connections.
194 void CloseCurrentConnections();
[email protected]b07f29092009-06-05 07:33:21195
196 // Sends a message to the browser to enable or disable the disk cache.
197 void SetCacheMode(bool enabled);
198
[email protected]c5d79342010-06-05 01:27:34199 // Sends a message to the browser to clear the disk cache.
[email protected]0e34852a2011-02-15 23:14:44200 // |preserve_ssl_host_info| is a flag indicating if the cache should purge
201 // entries related to cached SSL information.
202 void ClearCache(bool preserve_ssl_host_info);
[email protected]c5d79342010-06-05 01:27:34203
[email protected]8990e4712011-03-28 20:50:55204 // Sends a message to the browser to clear thed host cache.
205 void ClearHostResolverCache();
206
207 // Sends a message to the browser to clear the predictor cache.
208 void ClearPredictorCache();
209
[email protected]12893c32010-08-19 17:30:54210 // Sends a message to the browser to enable/disable spdy.
211 void EnableSpdy(bool enable);
212
[email protected]3bf4d532010-03-27 00:23:34213 // Synchronously establish a channel to the GPU plugin if not previously
214 // established or if it has been lost (for example if the GPU plugin crashed).
215 // If there is a pending asynchronous request, it will be completed by the
216 // time this routine returns.
[email protected]7f3a2cf2011-04-06 00:10:50217 GpuChannelHost* EstablishGpuChannelSync(content::CauseForGpuLaunch);
[email protected]3bf4d532010-03-27 00:23:34218
[email protected]6217d392010-03-25 22:08:35219 // Get the GPU channel. Returns NULL if the channel is not established or
220 // has been lost.
221 GpuChannelHost* GetGpuChannel();
222
[email protected]c6a7b862010-08-20 22:19:38223 // Returns a MessageLoopProxy instance corresponding to the message loop
224 // of the thread on which file operations should be run. Must be called
225 // on the renderer's main thread.
226 scoped_refptr<base::MessageLoopProxy> GetFileThreadMessageLoopProxy();
227
[email protected]6779aa12011-03-29 17:32:24228 // Schedule a call to IdleHandler with the given initial delay.
229 void ScheduleIdleHandler(double initial_delay_s);
230
231 // A task we invoke periodically to assist with idle cleanup.
232 void IdleHandler();
233
234 // Registers the given V8 extension with WebKit.
235 void RegisterExtension(v8::Extension* extension);
236
[email protected]c6e27b92011-04-30 17:03:43237 // Returns true iff the extension is registered.
238 bool IsRegisteredExtension(const std::string& v8_extension_name) const;
239
[email protected]d1b8fccc2011-08-03 01:20:13240 // We initialize WebKit as late as possible.
241 void EnsureWebKitInitialized();
242
[email protected]8930d472009-02-21 08:05:28243 private:
[email protected]a95986a82010-12-24 06:19:28244 virtual bool OnControlMessageReceived(const IPC::Message& msg);
initial.commit09911bf2008-07-26 23:55:29245
[email protected]42f1d7822009-07-23 18:17:55246 void Init();
initial.commit09911bf2008-07-26 23:55:29247
[email protected]b75b8292010-10-01 07:28:25248 void OnSetZoomLevelForCurrentURL(const GURL& url, double zoom_level);
[email protected]56879f932010-12-13 21:05:37249 void OnDOMStorageEvent(const DOMStorageMsg_Event_Params& params);
initial.commit09911bf2008-07-26 23:55:29250 void OnSetNextPageID(int32 next_page_id);
[email protected]b9ab10c2009-08-07 18:09:55251 void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors);
[email protected]4e6419c2010-01-15 04:50:34252 void OnCreateNewView(const ViewMsg_New_Params& params);
initial.commit09911bf2008-07-26 23:55:29253 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
[email protected]b78e168b2009-09-21 22:05:45254 void OnPurgePluginListCache(bool reload_pages);
[email protected]6eac57a2011-07-12 21:15:09255 void OnNetworkStateChanged(bool online);
[email protected]b3df5a42010-05-11 14:31:09256 void OnGetAccessibilityTree();
257
initial.commit09911bf2008-07-26 23:55:29258 // These objects live solely on the render thread.
[email protected]bee16aab2009-08-26 15:55:03259 scoped_ptr<ScopedRunnableMethodFactory<RenderThread> > task_factory_;
[email protected]f430b5712009-08-21 21:46:31260 scoped_ptr<AppCacheDispatcher> appcache_dispatcher_;
[email protected]70c19a932010-05-14 12:59:11261 scoped_ptr<IndexedDBDispatcher> indexed_db_dispatcher_;
[email protected]8d6cba42011-09-02 10:05:19262 scoped_ptr<RendererWebKitPlatformSupportImpl> webkit_platform_support_;
[email protected]b7c7bcf2009-10-03 07:07:34263 scoped_ptr<WebKit::WebStorageEventDispatcher> dom_storage_event_dispatcher_;
[email protected]9291ed12009-07-23 17:33:22264
[email protected]e2b2d4a2009-10-24 03:32:59265 // Used on the renderer and IPC threads.
[email protected]017022b2009-07-27 23:06:34266 scoped_refptr<DBMessageFilter> db_message_filter_;
[email protected]f7eb0a392011-07-12 10:19:51267 scoped_refptr<AudioInputMessageFilter> audio_input_message_filter_;
268 scoped_refptr<AudioMessageFilter> audio_message_filter_;
[email protected]82622452011-07-22 09:57:20269 scoped_refptr<DevToolsAgentFilter> devtools_agent_message_filter_;
[email protected]017022b2009-07-27 23:06:34270
[email protected]80b161a2011-06-27 17:42:11271 // Used on multiple threads.
272 scoped_refptr<VideoCaptureImplManager> vc_manager_;
273
[email protected]1d4dbde2011-04-01 20:40:35274 // Used on multiple script execution context threads.
275 scoped_ptr<WebDatabaseObserverImpl> web_database_observer_impl_;
276
[email protected]b547fd42009-04-23 23:16:27277 // If true, then a GetPlugins call is allowed to rescan the disk.
278 bool plugin_refresh_allowed_;
279
[email protected]bee16aab2009-08-26 15:55:03280 // The count of RenderWidgets running through this thread.
281 int widget_count_;
282
283 // The count of hidden RenderWidgets running through this thread.
284 int hidden_widget_count_;
285
286 // The current value of the idle notification timer delay.
287 double idle_notification_delay_in_s_;
288
[email protected]80fc08c52010-03-09 07:43:50289 bool suspend_webkit_shared_timer_;
290 bool notify_webkit_of_modal_loop_;
[email protected]c1f50aa2010-02-18 03:46:57291
[email protected]71d6d852009-12-07 22:12:36292 // Timer that periodically calls IdleHandler.
293 base::RepeatingTimer<RenderThread> idle_timer_;
294
[email protected]6217d392010-03-25 22:08:35295 // The channel from the renderer process to the GPU process.
296 scoped_refptr<GpuChannelHost> gpu_channel_;
297
[email protected]c6a7b862010-08-20 22:19:38298 // A lazily initiated thread on which file operations are run.
299 scoped_ptr<base::Thread> file_thread_;
300
[email protected]6779aa12011-03-29 17:32:24301 // Map of registered v8 extensions. The key is the extension name.
302 std::set<std::string> v8_extensions_;
[email protected]2a521c52011-01-26 18:45:21303
[email protected]1223d6ef2011-03-28 16:47:50304 ObserverList<RenderProcessObserver> observers_;
305
[email protected]1bc83062009-02-06 00:16:37306 DISALLOW_COPY_AND_ASSIGN(RenderThread);
initial.commit09911bf2008-07-26 23:55:29307};
308
[email protected]10e6ab572011-04-14 23:42:00309#endif // CONTENT_RENDERER_RENDER_THREAD_H_