blob: 704e727c9c69b5c2070aaf3bc31c3cc46230b01c [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]778574e2011-03-21 22:03:5019#include "content/common/css_colors.h"
[email protected]202b54ff2011-04-22 21:36:3820#include "content/common/gpu/gpu_process_launch_causes.h"
[email protected]a83d42292010-08-17 22:51:1021#include "ipc/ipc_channel_proxy.h"
[email protected]08397d52011-02-05 01:53:3822#include "ui/gfx/native_widget_types.h"
initial.commit09911bf2008-07-26 23:55:2923
[email protected]1edc16b82009-04-07 17:45:5424class AppCacheDispatcher;
[email protected]017022b2009-07-27 23:06:3425class DBMessageFilter;
[email protected]39008c02009-02-11 23:59:2526class FilePath;
[email protected]e13ad79b2010-07-22 21:36:5027class GpuChannelHost;
[email protected]70c19a932010-05-14 12:59:1128class IndexedDBDispatcher;
[email protected]55e57d42009-02-25 06:10:1729class RendererHistogram;
[email protected]e13ad79b2010-07-22 21:36:5030class RendererHistogramSnapshots;
[email protected]1223d6ef2011-03-28 16:47:5031class RenderProcessObserver;
[email protected]74be069e82010-06-25 00:12:4932class RendererNetPredictor;
[email protected]8d86fce2009-02-26 23:37:5533class RendererWebKitClientImpl;
[email protected]39008c02009-02-11 23:59:2534class SkBitmap;
[email protected]2b437e232010-04-02 01:30:0835class WebDatabaseObserverImpl;
[email protected]cccf90932009-08-23 17:56:2536
[email protected]9b6f40e2009-06-11 15:54:2637struct RendererPreferences;
[email protected]56879f932010-12-13 21:05:3738struct DOMStorageMsg_Event_Params;
[email protected]d6d8f712011-03-10 22:54:4339struct GPUInfo;
[email protected]4e6419c2010-01-15 04:50:3440struct ViewMsg_New_Params;
[email protected]39008c02009-02-11 23:59:2541struct WebPreferences;
initial.commit09911bf2008-07-26 23:55:2942
[email protected]c6a7b862010-08-20 22:19:3843namespace base {
44class MessageLoopProxy;
45class Thread;
46}
47
[email protected]46f36a492010-07-28 19:36:4148namespace IPC {
49struct ChannelHandle;
50}
51
[email protected]b7c7bcf2009-10-03 07:07:3452namespace WebKit {
53class WebStorageEventDispatcher;
54}
55
[email protected]af7eb3fb2010-09-23 21:31:0656namespace v8 {
57class Extension;
58}
59
[email protected]81a34412009-01-05 19:17:2460// The RenderThreadBase is the minimal interface that a RenderView/Widget
61// expects from a render thread. The interface basically abstracts a way to send
62// and receive messages.
[email protected]00c39612010-03-06 02:53:2863//
[email protected]f3ede412010-06-21 22:52:1664// TODO(brettw): This has two different and opposing usage patterns which
65// make it confusing.
66//
67// In the first mode, callers call RenderThread::current() to get the one and
68// only global RenderThread (bug 10837: this should be renamed get()). Then
69// they access it. Since RenderThread is a concrete class, this can be NULL
70// during unit tests. Callers need to NULL check this every time. Some callers
71// don't happen to get called during unit tests and don't do the NULL checks,
72// which is also confusing since it's not clear if you need to or not.
73//
74// In the second mode, the abstract base class RenderThreadBase is passed to
75// RenderView and RenderWidget. Normally, this points to
76// RenderThread::current() so it's quite confusing which accessing mode should
77// be used. However, during unit testing, this class is replaced with a mock
78// to support testing functions, and is guaranteed non-NULL.
79//
80// It might be nice not to have the ::current() call and put all of the
81// functions on the abstract class so they can be mocked. However, there are
82// some standalone functions like in ChromiumBridge that are not associated
83// with a view that need to access the current thread to send messages to the
84// browser process. These need the ::current() paradigm. So instead, we should
85// probably remove the render_thread_ parameter to RenderView/Widget in
86// preference to just getting the global singleton. We can make it easier to
87// understand by moving everything to the abstract interface and saying that
88// there should never be a NULL RenderThread::current(). Tests would be
89// responsible for setting up the mock one.
[email protected]8930d472009-02-21 08:05:2890class RenderThreadBase {
[email protected]8085dbc82008-09-26 22:53:4491 public:
92 virtual ~RenderThreadBase() {}
93
[email protected]8930d472009-02-21 08:05:2894 virtual bool Send(IPC::Message* msg) = 0;
95
[email protected]8085dbc82008-09-26 22:53:4496 // Called to add or remove a listener for a particular message routing ID.
97 // These methods normally get delegated to a MessageRouter.
98 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener) = 0;
99 virtual void RemoveRoute(int32 routing_id) = 0;
[email protected]81a34412009-01-05 19:17:24100
101 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
102 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
[email protected]bee16aab2009-08-26 15:55:03103
104 // Called by a RenderWidget when it is hidden or restored.
105 virtual void WidgetHidden() = 0;
106 virtual void WidgetRestored() = 0;
[email protected]8085dbc82008-09-26 22:53:44107};
108
initial.commit09911bf2008-07-26 23:55:29109// The RenderThread class represents a background thread where RenderView
110// instances live. The RenderThread supports an API that is used by its
111// consumer to talk indirectly to the RenderViews and supporting objects.
112// Likewise, it provides an API for the RenderViews to talk back to the main
[email protected]57c6a652009-05-04 07:58:34113// process (i.e., their corresponding TabContents).
initial.commit09911bf2008-07-26 23:55:29114//
115// Most of the communication occurs in the form of IPC messages. They are
116// routed to the RenderThread according to the routing IDs of the messages.
117// The routing IDs correspond to RenderView instances.
[email protected]8930d472009-02-21 08:05:28118class RenderThread : public RenderThreadBase,
119 public ChildThread {
initial.commit09911bf2008-07-26 23:55:29120 public:
[email protected]8930d472009-02-21 08:05:28121 // Grabs the IPC channel name from the command line.
122 RenderThread();
123 // Constructor that's used when running in single process mode.
[email protected]11f4857282009-11-13 19:56:17124 explicit RenderThread(const std::string& channel_name);
[email protected]8085dbc82008-09-26 22:53:44125 virtual ~RenderThread();
initial.commit09911bf2008-07-26 23:55:29126
[email protected]8930d472009-02-21 08:05:28127 // Returns the one render thread for this process. Note that this should only
128 // be accessed when running on the render thread itself
[email protected]00c39612010-03-06 02:53:28129 //
130 // TODO(brettw) this should be on the abstract base class instead of here,
131 // and return the base class' interface instead. Currently this causes
132 // problems with testing. See the comment above RenderThreadBase above.
[email protected]8930d472009-02-21 08:05:28133 static RenderThread* current();
initial.commit09911bf2008-07-26 23:55:29134
[email protected]c1f50aa2010-02-18 03:46:57135 // Returns the routing ID of the RenderWidget containing the current script
136 // execution context (corresponding to WebFrame::frameForCurrentContext).
137 static int32 RoutingIDForCurrentContext();
138
[email protected]45776222009-07-15 20:21:58139 // Overridden from RenderThreadBase.
[email protected]c1f50aa2010-02-18 03:46:57140 virtual bool Send(IPC::Message* msg);
141 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
142 virtual void RemoveRoute(int32 routing_id);
[email protected]81a34412009-01-05 19:17:24143 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
144 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter);
[email protected]bee16aab2009-08-26 15:55:03145 virtual void WidgetHidden();
146 virtual void WidgetRestored();
147
[email protected]1223d6ef2011-03-28 16:47:50148 void AddObserver(RenderProcessObserver* observer);
149 void RemoveObserver(RenderProcessObserver* observer);
150
[email protected]c1f50aa2010-02-18 03:46:57151 // These methods modify how the next message is sent. Normally, when sending
152 // a synchronous message that runs a nested message loop, we need to suspend
153 // callbacks into WebKit. This involves disabling timers and deferring
154 // resource loads. However, there are exceptions when we need to customize
155 // the behavior.
156 void DoNotSuspendWebKitSharedTimer();
157 void DoNotNotifyWebKitOfModalLoop();
158
[email protected]f430b5712009-08-21 21:46:31159 AppCacheDispatcher* appcache_dispatcher() const {
160 return appcache_dispatcher_.get();
161 }
162
[email protected]70c19a932010-05-14 12:59:11163 IndexedDBDispatcher* indexed_db_dispatcher() const {
164 return indexed_db_dispatcher_.get();
165 }
166
[email protected]b547fd42009-04-23 23:16:27167 bool plugin_refresh_allowed() const { return plugin_refresh_allowed_; }
168
[email protected]6779aa12011-03-29 17:32:24169 double idle_notification_delay_in_s() const {
170 return idle_notification_delay_in_s_;
171 }
172 void set_idle_notification_delay_in_s(double idle_notification_delay_in_s) {
[email protected]d07495f2011-03-29 17:41:28173 idle_notification_delay_in_s_ = idle_notification_delay_in_s;
[email protected]6779aa12011-03-29 17:32:24174 }
175
[email protected]c40acc32010-01-14 01:02:53176 // Sends a message to the browser to close all connections.
177 void CloseCurrentConnections();
[email protected]b07f29092009-06-05 07:33:21178
179 // Sends a message to the browser to enable or disable the disk cache.
180 void SetCacheMode(bool enabled);
181
[email protected]c5d79342010-06-05 01:27:34182 // Sends a message to the browser to clear the disk cache.
[email protected]0e34852a2011-02-15 23:14:44183 // |preserve_ssl_host_info| is a flag indicating if the cache should purge
184 // entries related to cached SSL information.
185 void ClearCache(bool preserve_ssl_host_info);
[email protected]c5d79342010-06-05 01:27:34186
[email protected]8990e4712011-03-28 20:50:55187 // Sends a message to the browser to clear thed host cache.
188 void ClearHostResolverCache();
189
190 // Sends a message to the browser to clear the predictor cache.
191 void ClearPredictorCache();
192
[email protected]12893c32010-08-19 17:30:54193 // Sends a message to the browser to enable/disable spdy.
194 void EnableSpdy(bool enable);
195
[email protected]6217d392010-03-25 22:08:35196 // Asynchronously establish a channel to the GPU plugin if not previously
197 // established or if it has been lost (for example if the GPU plugin crashed).
198 // Use GetGpuChannel() to determine when the channel is ready for use.
[email protected]7f3a2cf2011-04-06 00:10:50199 void EstablishGpuChannel(content::CauseForGpuLaunch);
[email protected]6217d392010-03-25 22:08:35200
[email protected]3bf4d532010-03-27 00:23:34201 // Synchronously establish a channel to the GPU plugin if not previously
202 // established or if it has been lost (for example if the GPU plugin crashed).
203 // If there is a pending asynchronous request, it will be completed by the
204 // time this routine returns.
[email protected]7f3a2cf2011-04-06 00:10:50205 GpuChannelHost* EstablishGpuChannelSync(content::CauseForGpuLaunch);
[email protected]3bf4d532010-03-27 00:23:34206
[email protected]6217d392010-03-25 22:08:35207 // Get the GPU channel. Returns NULL if the channel is not established or
208 // has been lost.
209 GpuChannelHost* GetGpuChannel();
210
[email protected]c6a7b862010-08-20 22:19:38211 // Returns a MessageLoopProxy instance corresponding to the message loop
212 // of the thread on which file operations should be run. Must be called
213 // on the renderer's main thread.
214 scoped_refptr<base::MessageLoopProxy> GetFileThreadMessageLoopProxy();
215
[email protected]af7eb3fb2010-09-23 21:31:06216 // This function is called for every registered V8 extension each time a new
217 // script context is created. Returns true if the given V8 extension is
218 // allowed to run on the given URL and extension group.
219 bool AllowScriptExtension(const std::string& v8_extension_name,
220 const GURL& url,
221 int extension_group);
222
[email protected]08f42e72011-02-16 07:34:40223 // Hack for http://crbug.com/71735.
224 // TODO(jamesr): remove once http://crbug.com/72007 is fixed.
225 RendererWebKitClientImpl* GetWebKitClientImpl() const {
226 return webkit_client_.get();
227 }
228
[email protected]6779aa12011-03-29 17:32:24229 // Schedule a call to IdleHandler with the given initial delay.
230 void ScheduleIdleHandler(double initial_delay_s);
231
232 // A task we invoke periodically to assist with idle cleanup.
233 void IdleHandler();
234
235 // Registers the given V8 extension with WebKit.
236 void RegisterExtension(v8::Extension* extension);
237
[email protected]8930d472009-02-21 08:05:28238 private:
[email protected]a95986a82010-12-24 06:19:28239 virtual bool OnControlMessageReceived(const IPC::Message& msg);
initial.commit09911bf2008-07-26 23:55:29240
[email protected]42f1d7822009-07-23 18:17:55241 void Init();
initial.commit09911bf2008-07-26 23:55:29242
[email protected]b75b8292010-10-01 07:28:25243 void OnSetZoomLevelForCurrentURL(const GURL& url, double zoom_level);
[email protected]56879f932010-12-13 21:05:37244 void OnDOMStorageEvent(const DOMStorageMsg_Event_Params& params);
initial.commit09911bf2008-07-26 23:55:29245 void OnSetNextPageID(int32 next_page_id);
[email protected]b9ab10c2009-08-07 18:09:55246 void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors);
[email protected]4e6419c2010-01-15 04:50:34247 void OnCreateNewView(const ViewMsg_New_Params& params);
initial.commit09911bf2008-07-26 23:55:29248 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
[email protected]b78e168b2009-09-21 22:05:45249 void OnPurgePluginListCache(bool reload_pages);
[email protected]4bce24e2010-09-07 20:45:01250 void OnGpuChannelEstablished(const IPC::ChannelHandle& channel_handle,
[email protected]0100b7a2011-02-24 22:54:50251 base::ProcessHandle renderer_process_for_gpu,
[email protected]4bce24e2010-09-07 20:45:01252 const GPUInfo& gpu_info);
[email protected]b3df5a42010-05-11 14:31:09253 void OnGetAccessibilityTree();
254
[email protected]90a3fbb12009-02-28 01:13:47255 // We initialize WebKit as late as possible.
256 void EnsureWebKitInitialized();
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]42f1d7822009-07-23 18:17:55262 scoped_ptr<RendererWebKitClientImpl> webkit_client_;
[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_;
267
[email protected]1d4dbde2011-04-01 20:40:35268 // Used on multiple script execution context threads.
269 scoped_ptr<WebDatabaseObserverImpl> web_database_observer_impl_;
270
[email protected]b547fd42009-04-23 23:16:27271 // If true, then a GetPlugins call is allowed to rescan the disk.
272 bool plugin_refresh_allowed_;
273
[email protected]bee16aab2009-08-26 15:55:03274 // The count of RenderWidgets running through this thread.
275 int widget_count_;
276
277 // The count of hidden RenderWidgets running through this thread.
278 int hidden_widget_count_;
279
280 // The current value of the idle notification timer delay.
281 double idle_notification_delay_in_s_;
282
[email protected]80fc08c52010-03-09 07:43:50283 bool suspend_webkit_shared_timer_;
284 bool notify_webkit_of_modal_loop_;
[email protected]c1f50aa2010-02-18 03:46:57285
[email protected]71d6d852009-12-07 22:12:36286 // Timer that periodically calls IdleHandler.
287 base::RepeatingTimer<RenderThread> idle_timer_;
288
[email protected]6217d392010-03-25 22:08:35289 // The channel from the renderer process to the GPU process.
290 scoped_refptr<GpuChannelHost> gpu_channel_;
291
[email protected]c6a7b862010-08-20 22:19:38292 // A lazily initiated thread on which file operations are run.
293 scoped_ptr<base::Thread> file_thread_;
294
[email protected]6779aa12011-03-29 17:32:24295 // Map of registered v8 extensions. The key is the extension name.
296 std::set<std::string> v8_extensions_;
[email protected]2a521c52011-01-26 18:45:21297
[email protected]1223d6ef2011-03-28 16:47:50298 ObserverList<RenderProcessObserver> observers_;
299
[email protected]1bc83062009-02-06 00:16:37300 DISALLOW_COPY_AND_ASSIGN(RenderThread);
initial.commit09911bf2008-07-26 23:55:29301};
302
[email protected]10e6ab572011-04-14 23:42:00303#endif // CONTENT_RENDERER_RENDER_THREAD_H_