blob: 0dcd7d2a8dad638f00569fbba1913bc1ca4ca808 [file] [log] [blame]
[email protected]f85f0702010-01-30 09:31:011// Copyright (c) 2010 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]1d97d2e2008-12-18 23:39:025#ifndef CHROME_RENDERER_RENDER_THREAD_H_
6#define CHROME_RENDERER_RENDER_THREAD_H_
initial.commit09911bf2008-07-26 23:55:297
[email protected]c8865962009-12-16 07:47:398#include <set>
[email protected]b68d5ed2009-04-16 02:41:289#include <string>
[email protected]1bc83062009-02-06 00:16:3710#include <vector>
11
initial.commit09911bf2008-07-26 23:55:2912#include "base/shared_memory.h"
[email protected]85c55dc2009-11-06 03:05:4613#include "base/string16.h"
initial.commit09911bf2008-07-26 23:55:2914#include "base/task.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]8930d472009-02-21 08:05:2818#include "chrome/common/child_thread.h"
[email protected]b9ab10c2009-08-07 18:09:5519#include "chrome/common/css_colors.h"
[email protected]4e6419c2010-01-15 04:50:3420#include "chrome/common/dom_storage_common.h"
[email protected]3a8eecb2010-04-22 23:56:3021#include "chrome/common/extensions/extension_extent.h"
[email protected]6217d392010-03-25 22:08:3522#include "chrome/renderer/gpu_channel_host.h"
[email protected]55e57d42009-02-25 06:10:1723#include "chrome/renderer/renderer_histogram_snapshots.h"
[email protected]3e90d4a2009-07-03 17:38:3924#include "chrome/renderer/visitedlink_slave.h"
[email protected]5c7293a2010-03-17 06:40:5725#include "gfx/native_widget_types.h"
[email protected]6217d392010-03-25 22:08:3526#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1727#include "ipc/ipc_platform_file.h"
initial.commit09911bf2008-07-26 23:55:2928
[email protected]1edc16b82009-04-07 17:45:5429class AppCacheDispatcher;
[email protected]dd9241932010-02-24 19:23:1330class CookieMessageFilter;
[email protected]017022b2009-07-27 23:06:3431class DBMessageFilter;
[email protected]a8624712009-04-17 00:51:3532class DevToolsAgentFilter;
[email protected]39008c02009-02-11 23:59:2533class FilePath;
[email protected]70c19a932010-05-14 12:59:1134class IndexedDBDispatcher;
[email protected]dfcb62a2009-06-17 19:32:4335class ListValue;
[email protected]b7c7bcf2009-10-03 07:07:3436class NullableString16;
[email protected]39008c02009-02-11 23:59:2537class RenderDnsMaster;
[email protected]55e57d42009-02-25 06:10:1738class RendererHistogram;
[email protected]8d86fce2009-02-26 23:37:5539class RendererWebKitClientImpl;
[email protected]85c55dc2009-11-06 03:05:4640class SpellCheck;
[email protected]39008c02009-02-11 23:59:2541class SkBitmap;
[email protected]4d395d092009-02-11 21:40:4042class UserScriptSlave;
[email protected]cccf90932009-08-23 17:56:2543class URLPattern;
[email protected]2b437e232010-04-02 01:30:0844class WebDatabaseObserverImpl;
[email protected]cccf90932009-08-23 17:56:2545
[email protected]f85f0702010-01-30 09:31:0146struct ContentSettings;
[email protected]9b6f40e2009-06-11 15:54:2647struct RendererPreferences;
[email protected]c61cc652009-11-04 05:44:4048struct ViewMsg_DOMStorageEvent_Params;
[email protected]942690b132010-05-11 06:42:1449struct ViewMsg_ExtensionExtentsUpdated_Params;
[email protected]4e6419c2010-01-15 04:50:3450struct ViewMsg_New_Params;
[email protected]39008c02009-02-11 23:59:2551struct WebPreferences;
initial.commit09911bf2008-07-26 23:55:2952
[email protected]b7c7bcf2009-10-03 07:07:3453namespace WebKit {
54class WebStorageEventDispatcher;
55}
56
[email protected]81a34412009-01-05 19:17:2457// The RenderThreadBase is the minimal interface that a RenderView/Widget
58// expects from a render thread. The interface basically abstracts a way to send
59// and receive messages.
[email protected]00c39612010-03-06 02:53:2860//
[email protected]f3ede412010-06-21 22:52:1661// TODO(brettw): This has two different and opposing usage patterns which
62// make it confusing.
63//
64// In the first mode, callers call RenderThread::current() to get the one and
65// only global RenderThread (bug 10837: this should be renamed get()). Then
66// they access it. Since RenderThread is a concrete class, this can be NULL
67// during unit tests. Callers need to NULL check this every time. Some callers
68// don't happen to get called during unit tests and don't do the NULL checks,
69// which is also confusing since it's not clear if you need to or not.
70//
71// In the second mode, the abstract base class RenderThreadBase is passed to
72// RenderView and RenderWidget. Normally, this points to
73// RenderThread::current() so it's quite confusing which accessing mode should
74// be used. However, during unit testing, this class is replaced with a mock
75// to support testing functions, and is guaranteed non-NULL.
76//
77// It might be nice not to have the ::current() call and put all of the
78// functions on the abstract class so they can be mocked. However, there are
79// some standalone functions like in ChromiumBridge that are not associated
80// with a view that need to access the current thread to send messages to the
81// browser process. These need the ::current() paradigm. So instead, we should
82// probably remove the render_thread_ parameter to RenderView/Widget in
83// preference to just getting the global singleton. We can make it easier to
84// understand by moving everything to the abstract interface and saying that
85// there should never be a NULL RenderThread::current(). Tests would be
86// responsible for setting up the mock one.
[email protected]8930d472009-02-21 08:05:2887class RenderThreadBase {
[email protected]8085dbc82008-09-26 22:53:4488 public:
89 virtual ~RenderThreadBase() {}
90
[email protected]8930d472009-02-21 08:05:2891 virtual bool Send(IPC::Message* msg) = 0;
92
[email protected]8085dbc82008-09-26 22:53:4493 // Called to add or remove a listener for a particular message routing ID.
94 // These methods normally get delegated to a MessageRouter.
95 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener) = 0;
96 virtual void RemoveRoute(int32 routing_id) = 0;
[email protected]81a34412009-01-05 19:17:2497
98 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
99 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
[email protected]bee16aab2009-08-26 15:55:03100
101 // Called by a RenderWidget when it is hidden or restored.
102 virtual void WidgetHidden() = 0;
103 virtual void WidgetRestored() = 0;
[email protected]b8f41a192010-04-19 18:25:04104
105 // True if this process should be treated as an extension process.
106 virtual bool IsExtensionProcess() const = 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]c1f50aa2010-02-18 03:46:57148 // These methods modify how the next message is sent. Normally, when sending
149 // a synchronous message that runs a nested message loop, we need to suspend
150 // callbacks into WebKit. This involves disabling timers and deferring
151 // resource loads. However, there are exceptions when we need to customize
152 // the behavior.
153 void DoNotSuspendWebKitSharedTimer();
154 void DoNotNotifyWebKitOfModalLoop();
155
[email protected]8d86fce2009-02-26 23:37:55156 VisitedLinkSlave* visited_link_slave() const {
157 return visited_link_slave_.get();
158 }
initial.commit09911bf2008-07-26 23:55:29159
[email protected]8d86fce2009-02-26 23:37:55160 UserScriptSlave* user_script_slave() const {
161 return user_script_slave_.get();
162 }
[email protected]1e0f70402008-10-16 23:57:47163
[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]85c55dc2009-11-06 03:05:46172 SpellCheck* spellchecker() const {
173 return spellchecker_.get();
174 }
[email protected]85c55dc2009-11-06 03:05:46175
[email protected]b547fd42009-04-23 23:16:27176 bool plugin_refresh_allowed() const { return plugin_refresh_allowed_; }
177
[email protected]b8f41a192010-04-19 18:25:04178 virtual bool IsExtensionProcess() const { return is_extension_process_; }
[email protected]71d6d852009-12-07 22:12:36179
[email protected]b2a74ca2010-03-12 17:57:09180 bool is_incognito_process() const { return is_incognito_process_; }
181
initial.commit09911bf2008-07-26 23:55:29182 // Do DNS prefetch resolution of a hostname.
183 void Resolve(const char* name, size_t length);
184
[email protected]55e57d42009-02-25 06:10:17185 // Send all the Histogram data to browser.
[email protected]c9a3ef82009-05-28 22:02:46186 void SendHistograms(int sequence_number);
[email protected]55e57d42009-02-25 06:10:17187
initial.commit09911bf2008-07-26 23:55:29188 // Invokes InformHostOfCacheStats after a short delay. Used to move this
189 // bookkeeping operation off the critical latency path.
190 void InformHostOfCacheStatsLater();
191
[email protected]c40acc32010-01-14 01:02:53192 // Sends a message to the browser to close all connections.
193 void CloseCurrentConnections();
[email protected]b07f29092009-06-05 07:33:21194
195 // Sends a message to the browser to enable or disable the disk cache.
196 void SetCacheMode(bool enabled);
197
[email protected]c5d79342010-06-05 01:27:34198 // Sends a message to the browser to clear the disk cache.
199 void ClearCache();
200
[email protected]c8865962009-12-16 07:47:39201 // Update the list of active extensions that will be reported when we crash.
202 void UpdateActiveExtensions();
203
[email protected]6217d392010-03-25 22:08:35204 // Asynchronously establish a channel to the GPU plugin if not previously
205 // established or if it has been lost (for example if the GPU plugin crashed).
206 // Use GetGpuChannel() to determine when the channel is ready for use.
207 void EstablishGpuChannel();
208
[email protected]3bf4d532010-03-27 00:23:34209 // Synchronously establish a channel to the GPU plugin if not previously
210 // established or if it has been lost (for example if the GPU plugin crashed).
211 // If there is a pending asynchronous request, it will be completed by the
212 // time this routine returns.
213 GpuChannelHost* EstablishGpuChannelSync();
214
[email protected]6217d392010-03-25 22:08:35215 // Get the GPU channel. Returns NULL if the channel is not established or
216 // has been lost.
217 GpuChannelHost* GetGpuChannel();
218
[email protected]3a8eecb2010-04-22 23:56:30219 // Returns the extension ID that the given URL is a part of, or empty if
220 // none. This includes web URLs that are part of an extension's web extent.
221 // TODO(mpcomplete): this doesn't feel like it belongs here. Find a better
222 // place.
223 std::string GetExtensionIdForURL(const GURL& url);
224
[email protected]8930d472009-02-21 08:05:28225 private:
[email protected]3a8eecb2010-04-22 23:56:30226 // Contains extension-related data that the renderer needs to know about.
227 // TODO(mpcomplete): this doesn't feel like it belongs here. Find a better
228 // place.
229 struct ExtensionInfo {
230 std::string extension_id;
231 ExtensionExtent web_extent;
232 };
233
[email protected]8930d472009-02-21 08:05:28234 virtual void OnControlMessageReceived(const IPC::Message& msg);
initial.commit09911bf2008-07-26 23:55:29235
[email protected]42f1d7822009-07-23 18:17:55236 void Init();
initial.commit09911bf2008-07-26 23:55:29237
[email protected]176aa482008-11-14 03:25:15238 void OnUpdateVisitedLinks(base::SharedMemoryHandle table);
[email protected]3e90d4a2009-07-03 17:38:39239 void OnAddVisitedLinks(const VisitedLinkSlave::Fingerprints& fingerprints);
240 void OnResetVisitedLinks();
[email protected]9d797f32010-04-23 07:17:54241 void OnSetZoomLevelForCurrentURL(const GURL& url, int zoom_level);
[email protected]0314ae02010-04-08 09:18:29242 void OnSetContentSettingsForCurrentURL(
243 const GURL& url, const ContentSettings& content_settings);
[email protected]b2a74ca2010-03-12 17:57:09244 void OnUpdateUserScripts(base::SharedMemoryHandle table);
[email protected]703e807a2009-03-28 19:56:51245 void OnSetExtensionFunctionNames(const std::vector<std::string>& names);
[email protected]3a8eecb2010-04-22 23:56:30246 void OnExtensionExtentsUpdated(
247 const ViewMsg_ExtensionExtentsUpdated_Params& params);
[email protected]45776222009-07-15 20:21:58248 void OnPageActionsUpdated(const std::string& extension_id,
[email protected]b7c7bcf2009-10-03 07:07:34249 const std::vector<std::string>& page_actions);
[email protected]c61cc652009-11-04 05:44:40250 void OnDOMStorageEvent(const ViewMsg_DOMStorageEvent_Params& params);
[email protected]75e126b932009-09-28 19:38:49251 void OnExtensionSetAPIPermissions(
252 const std::string& extension_id,
253 const std::vector<std::string>& permissions);
254 void OnExtensionSetHostPermissions(
255 const GURL& extension_url,
256 const std::vector<URLPattern>& permissions);
[email protected]db7331a2010-02-25 22:10:50257 void OnExtensionSetIncognitoEnabled(
258 const std::string& extension_id,
259 bool enabled);
initial.commit09911bf2008-07-26 23:55:29260 void OnSetNextPageID(int32 next_page_id);
[email protected]b2a74ca2010-03-12 17:57:09261 void OnSetIsIncognitoProcess(bool is_incognito_process);
[email protected]b9ab10c2009-08-07 18:09:55262 void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors);
[email protected]4e6419c2010-01-15 04:50:34263 void OnCreateNewView(const ViewMsg_New_Params& params);
initial.commit09911bf2008-07-26 23:55:29264 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
265 void OnSetCacheCapacities(size_t min_dead_capacity,
266 size_t max_dead_capacity,
267 size_t capacity);
268 void OnGetCacheResourceStats();
269
[email protected]55e57d42009-02-25 06:10:17270 // Send all histograms to browser.
[email protected]c9a3ef82009-05-28 22:02:46271 void OnGetRendererHistograms(int sequence_number);
[email protected]55e57d42009-02-25 06:10:17272
[email protected]d41041092009-10-08 06:56:57273 // Send tcmalloc info to browser.
274 void OnGetRendererTcmalloc();
[email protected]38b48a82009-11-11 01:51:32275 void OnGetV8HeapStats();
[email protected]d41041092009-10-08 06:56:57276
[email protected]dfcb62a2009-06-17 19:32:43277 void OnExtensionMessageInvoke(const std::string& function_name,
[email protected]d7259472010-03-24 08:40:49278 const ListValue& args,
[email protected]a807bbe2010-04-14 10:51:19279 bool requires_incognito_access,
280 const GURL& event_url);
[email protected]fede6ca12009-10-08 18:24:26281 void OnPurgeMemory();
[email protected]b78e168b2009-09-21 22:05:45282 void OnPurgePluginListCache(bool reload_pages);
[email protected]75e5a872009-04-02 23:56:11283
[email protected]cb6037d2009-11-16 22:55:17284 void OnInitSpellChecker(IPC::PlatformFileForTransit bdict_file,
[email protected]85c55dc2009-11-06 03:05:46285 const std::vector<std::string>& custom_words,
286 const std::string& language,
287 bool auto_spell_correct);
288 void OnSpellCheckWordAdded(const std::string& word);
289 void OnSpellCheckEnableAutoSpellCorrect(bool enable);
[email protected]85c55dc2009-11-06 03:05:46290
[email protected]6217d392010-03-25 22:08:35291 void OnGpuChannelEstablished(const IPC::ChannelHandle& channel_handle);
292
[email protected]b3df5a42010-05-11 14:31:09293 void OnGetAccessibilityTree();
294
initial.commit09911bf2008-07-26 23:55:29295 // Gather usage statistics from the in-memory cache and inform our host.
296 // These functions should be call periodically so that the host can make
297 // decisions about how to allocation resources using current information.
298 void InformHostOfCacheStats();
299
[email protected]90a3fbb12009-02-28 01:13:47300 // We initialize WebKit as late as possible.
301 void EnsureWebKitInitialized();
302
[email protected]bee16aab2009-08-26 15:55:03303 // A task we invoke periodically to assist with idle cleanup.
304 void IdleHandler();
305
[email protected]71d6d852009-12-07 22:12:36306 // Schedule a call to IdleHandler with the given initial delay.
307 void ScheduleIdleHandler(double initial_delay_s);
308
initial.commit09911bf2008-07-26 23:55:29309 // These objects live solely on the render thread.
[email protected]bee16aab2009-08-26 15:55:03310 scoped_ptr<ScopedRunnableMethodFactory<RenderThread> > task_factory_;
[email protected]42f1d7822009-07-23 18:17:55311 scoped_ptr<VisitedLinkSlave> visited_link_slave_;
312 scoped_ptr<UserScriptSlave> user_script_slave_;
313 scoped_ptr<RenderDnsMaster> dns_master_;
[email protected]f430b5712009-08-21 21:46:31314 scoped_ptr<AppCacheDispatcher> appcache_dispatcher_;
[email protected]70c19a932010-05-14 12:59:11315 scoped_ptr<IndexedDBDispatcher> indexed_db_dispatcher_;
[email protected]9291ed12009-07-23 17:33:22316 scoped_refptr<DevToolsAgentFilter> devtools_agent_filter_;
[email protected]42f1d7822009-07-23 18:17:55317 scoped_ptr<RendererHistogramSnapshots> histogram_snapshots_;
318 scoped_ptr<RendererWebKitClientImpl> webkit_client_;
[email protected]b7c7bcf2009-10-03 07:07:34319 scoped_ptr<WebKit::WebStorageEventDispatcher> dom_storage_event_dispatcher_;
[email protected]2b437e232010-04-02 01:30:08320 scoped_ptr<WebDatabaseObserverImpl> web_database_observer_impl_;
[email protected]85c55dc2009-11-06 03:05:46321 scoped_ptr<SpellCheck> spellchecker_;
[email protected]9291ed12009-07-23 17:33:22322
[email protected]e2b2d4a2009-10-24 03:32:59323 // Used on the renderer and IPC threads.
[email protected]017022b2009-07-27 23:06:34324 scoped_refptr<DBMessageFilter> db_message_filter_;
[email protected]dd9241932010-02-24 19:23:13325 scoped_refptr<CookieMessageFilter> cookie_message_filter_;
[email protected]017022b2009-07-27 23:06:34326
[email protected]5fa1c542009-05-05 20:36:07327#if defined(OS_POSIX)
328 scoped_refptr<IPC::ChannelProxy::MessageFilter>
329 suicide_on_channel_error_filter_;
330#endif
331
[email protected]b547fd42009-04-23 23:16:27332 // If true, then a GetPlugins call is allowed to rescan the disk.
333 bool plugin_refresh_allowed_;
334
[email protected]bee16aab2009-08-26 15:55:03335 // Is there a pending task for doing CacheStats.
336 bool cache_stats_task_pending_;
337
338 // The count of RenderWidgets running through this thread.
339 int widget_count_;
340
341 // The count of hidden RenderWidgets running through this thread.
342 int hidden_widget_count_;
343
344 // The current value of the idle notification timer delay.
345 double idle_notification_delay_in_s_;
346
[email protected]71d6d852009-12-07 22:12:36347 // True if this renderer is running extensions.
348 bool is_extension_process_;
349
[email protected]b2a74ca2010-03-12 17:57:09350 // True if this renderer is incognito.
351 bool is_incognito_process_;
352
[email protected]80fc08c52010-03-09 07:43:50353 bool suspend_webkit_shared_timer_;
354 bool notify_webkit_of_modal_loop_;
[email protected]c1f50aa2010-02-18 03:46:57355 bool did_notify_webkit_of_modal_loop_;
356
[email protected]71d6d852009-12-07 22:12:36357 // Timer that periodically calls IdleHandler.
358 base::RepeatingTimer<RenderThread> idle_timer_;
359
360 // Same as above, but on a longer timer and will run even if the process is
361 // not idle, to ensure that IdleHandle gets called eventually.
362 base::RepeatingTimer<RenderThread> forced_idle_timer_;
363
[email protected]6217d392010-03-25 22:08:35364 // The channel from the renderer process to the GPU process.
365 scoped_refptr<GpuChannelHost> gpu_channel_;
366
[email protected]3a8eecb2010-04-22 23:56:30367 // A list of extension web extents, which tells us which URLs belong to an
368 // installed app.
369 std::vector<ExtensionInfo> extension_extents_;
370
[email protected]1bc83062009-02-06 00:16:37371 DISALLOW_COPY_AND_ASSIGN(RenderThread);
initial.commit09911bf2008-07-26 23:55:29372};
373
[email protected]1d97d2e2008-12-18 23:39:02374#endif // CHROME_RENDERER_RENDER_THREAD_H_