blob: af0eefe6d05b5a0c3df53ef3ef21817fe4bd25de [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]6217d392010-03-25 22:08:3521#include "chrome/renderer/gpu_channel_host.h"
[email protected]55e57d42009-02-25 06:10:1722#include "chrome/renderer/renderer_histogram_snapshots.h"
[email protected]3e90d4a2009-07-03 17:38:3923#include "chrome/renderer/visitedlink_slave.h"
[email protected]5c7293a2010-03-17 06:40:5724#include "gfx/native_widget_types.h"
[email protected]6217d392010-03-25 22:08:3525#include "ipc/ipc_channel_handle.h"
[email protected]cb6037d2009-11-16 22:55:1726#include "ipc/ipc_platform_file.h"
initial.commit09911bf2008-07-26 23:55:2927
[email protected]1edc16b82009-04-07 17:45:5428class AppCacheDispatcher;
[email protected]dd9241932010-02-24 19:23:1329class CookieMessageFilter;
[email protected]017022b2009-07-27 23:06:3430class DBMessageFilter;
[email protected]a8624712009-04-17 00:51:3531class DevToolsAgentFilter;
[email protected]39008c02009-02-11 23:59:2532class FilePath;
[email protected]dfcb62a2009-06-17 19:32:4333class ListValue;
[email protected]b7c7bcf2009-10-03 07:07:3434class NullableString16;
[email protected]39008c02009-02-11 23:59:2535class RenderDnsMaster;
[email protected]55e57d42009-02-25 06:10:1736class RendererHistogram;
[email protected]8d86fce2009-02-26 23:37:5537class RendererWebKitClientImpl;
[email protected]85c55dc2009-11-06 03:05:4638class SpellCheck;
[email protected]39008c02009-02-11 23:59:2539class SkBitmap;
[email protected]4d395d092009-02-11 21:40:4040class UserScriptSlave;
[email protected]cccf90932009-08-23 17:56:2541class URLPattern;
[email protected]2b437e232010-04-02 01:30:0842class WebDatabaseObserverImpl;
[email protected]cccf90932009-08-23 17:56:2543
[email protected]f85f0702010-01-30 09:31:0144struct ContentSettings;
[email protected]9b6f40e2009-06-11 15:54:2645struct RendererPreferences;
[email protected]c61cc652009-11-04 05:44:4046struct ViewMsg_DOMStorageEvent_Params;
[email protected]4e6419c2010-01-15 04:50:3447struct ViewMsg_New_Params;
[email protected]39008c02009-02-11 23:59:2548struct WebPreferences;
initial.commit09911bf2008-07-26 23:55:2949
[email protected]b7c7bcf2009-10-03 07:07:3450namespace WebKit {
51class WebStorageEventDispatcher;
52}
53
[email protected]81a34412009-01-05 19:17:2454// The RenderThreadBase is the minimal interface that a RenderView/Widget
55// expects from a render thread. The interface basically abstracts a way to send
56// and receive messages.
[email protected]00c39612010-03-06 02:53:2857//
58// TODO(brettw) this should be refactored like RenderProcess/RenderProcessImpl:
59// This class should be named RenderThread and the implementation below should
60// be RenderThreadImpl. The ::current() getter on the impl should then be moved
61// here so we can provide another implementation of RenderThread for tests
62// without having to check for NULL all the time.
[email protected]8930d472009-02-21 08:05:2863class RenderThreadBase {
[email protected]8085dbc82008-09-26 22:53:4464 public:
65 virtual ~RenderThreadBase() {}
66
[email protected]8930d472009-02-21 08:05:2867 virtual bool Send(IPC::Message* msg) = 0;
68
[email protected]8085dbc82008-09-26 22:53:4469 // Called to add or remove a listener for a particular message routing ID.
70 // These methods normally get delegated to a MessageRouter.
71 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener) = 0;
72 virtual void RemoveRoute(int32 routing_id) = 0;
[email protected]81a34412009-01-05 19:17:2473
74 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
75 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) = 0;
[email protected]bee16aab2009-08-26 15:55:0376
77 // Called by a RenderWidget when it is hidden or restored.
78 virtual void WidgetHidden() = 0;
79 virtual void WidgetRestored() = 0;
[email protected]8085dbc82008-09-26 22:53:4480};
81
initial.commit09911bf2008-07-26 23:55:2982// The RenderThread class represents a background thread where RenderView
83// instances live. The RenderThread supports an API that is used by its
84// consumer to talk indirectly to the RenderViews and supporting objects.
85// Likewise, it provides an API for the RenderViews to talk back to the main
[email protected]57c6a652009-05-04 07:58:3486// process (i.e., their corresponding TabContents).
initial.commit09911bf2008-07-26 23:55:2987//
88// Most of the communication occurs in the form of IPC messages. They are
89// routed to the RenderThread according to the routing IDs of the messages.
90// The routing IDs correspond to RenderView instances.
[email protected]8930d472009-02-21 08:05:2891class RenderThread : public RenderThreadBase,
92 public ChildThread {
initial.commit09911bf2008-07-26 23:55:2993 public:
[email protected]8930d472009-02-21 08:05:2894 // Grabs the IPC channel name from the command line.
95 RenderThread();
96 // Constructor that's used when running in single process mode.
[email protected]11f4857282009-11-13 19:56:1797 explicit RenderThread(const std::string& channel_name);
[email protected]8085dbc82008-09-26 22:53:4498 virtual ~RenderThread();
initial.commit09911bf2008-07-26 23:55:2999
[email protected]8930d472009-02-21 08:05:28100 // Returns the one render thread for this process. Note that this should only
101 // be accessed when running on the render thread itself
[email protected]00c39612010-03-06 02:53:28102 //
103 // TODO(brettw) this should be on the abstract base class instead of here,
104 // and return the base class' interface instead. Currently this causes
105 // problems with testing. See the comment above RenderThreadBase above.
[email protected]8930d472009-02-21 08:05:28106 static RenderThread* current();
initial.commit09911bf2008-07-26 23:55:29107
[email protected]c1f50aa2010-02-18 03:46:57108 // Returns the routing ID of the RenderWidget containing the current script
109 // execution context (corresponding to WebFrame::frameForCurrentContext).
110 static int32 RoutingIDForCurrentContext();
111
[email protected]45776222009-07-15 20:21:58112 // Overridden from RenderThreadBase.
[email protected]c1f50aa2010-02-18 03:46:57113 virtual bool Send(IPC::Message* msg);
114 virtual void AddRoute(int32 routing_id, IPC::Channel::Listener* listener);
115 virtual void RemoveRoute(int32 routing_id);
[email protected]81a34412009-01-05 19:17:24116 virtual void AddFilter(IPC::ChannelProxy::MessageFilter* filter);
117 virtual void RemoveFilter(IPC::ChannelProxy::MessageFilter* filter);
[email protected]bee16aab2009-08-26 15:55:03118 virtual void WidgetHidden();
119 virtual void WidgetRestored();
120
[email protected]c1f50aa2010-02-18 03:46:57121 // These methods modify how the next message is sent. Normally, when sending
122 // a synchronous message that runs a nested message loop, we need to suspend
123 // callbacks into WebKit. This involves disabling timers and deferring
124 // resource loads. However, there are exceptions when we need to customize
125 // the behavior.
126 void DoNotSuspendWebKitSharedTimer();
127 void DoNotNotifyWebKitOfModalLoop();
128
[email protected]8d86fce2009-02-26 23:37:55129 VisitedLinkSlave* visited_link_slave() const {
130 return visited_link_slave_.get();
131 }
initial.commit09911bf2008-07-26 23:55:29132
[email protected]8d86fce2009-02-26 23:37:55133 UserScriptSlave* user_script_slave() const {
134 return user_script_slave_.get();
135 }
[email protected]1e0f70402008-10-16 23:57:47136
[email protected]f430b5712009-08-21 21:46:31137 AppCacheDispatcher* appcache_dispatcher() const {
138 return appcache_dispatcher_.get();
139 }
140
[email protected]85c55dc2009-11-06 03:05:46141 SpellCheck* spellchecker() const {
142 return spellchecker_.get();
143 }
[email protected]85c55dc2009-11-06 03:05:46144
[email protected]b547fd42009-04-23 23:16:27145 bool plugin_refresh_allowed() const { return plugin_refresh_allowed_; }
146
[email protected]71d6d852009-12-07 22:12:36147 bool is_extension_process() const { return is_extension_process_; }
148
[email protected]b2a74ca2010-03-12 17:57:09149 bool is_incognito_process() const { return is_incognito_process_; }
150
initial.commit09911bf2008-07-26 23:55:29151 // Do DNS prefetch resolution of a hostname.
152 void Resolve(const char* name, size_t length);
153
[email protected]55e57d42009-02-25 06:10:17154 // Send all the Histogram data to browser.
[email protected]c9a3ef82009-05-28 22:02:46155 void SendHistograms(int sequence_number);
[email protected]55e57d42009-02-25 06:10:17156
initial.commit09911bf2008-07-26 23:55:29157 // Invokes InformHostOfCacheStats after a short delay. Used to move this
158 // bookkeeping operation off the critical latency path.
159 void InformHostOfCacheStatsLater();
160
[email protected]c40acc32010-01-14 01:02:53161 // Sends a message to the browser to close all connections.
162 void CloseCurrentConnections();
[email protected]b07f29092009-06-05 07:33:21163
164 // Sends a message to the browser to enable or disable the disk cache.
165 void SetCacheMode(bool enabled);
166
[email protected]c8865962009-12-16 07:47:39167 // Update the list of active extensions that will be reported when we crash.
168 void UpdateActiveExtensions();
169
[email protected]6217d392010-03-25 22:08:35170 // Asynchronously establish a channel to the GPU plugin if not previously
171 // established or if it has been lost (for example if the GPU plugin crashed).
172 // Use GetGpuChannel() to determine when the channel is ready for use.
173 void EstablishGpuChannel();
174
[email protected]3bf4d532010-03-27 00:23:34175 // Synchronously establish a channel to the GPU plugin if not previously
176 // established or if it has been lost (for example if the GPU plugin crashed).
177 // If there is a pending asynchronous request, it will be completed by the
178 // time this routine returns.
179 GpuChannelHost* EstablishGpuChannelSync();
180
[email protected]6217d392010-03-25 22:08:35181 // Get the GPU channel. Returns NULL if the channel is not established or
182 // has been lost.
183 GpuChannelHost* GetGpuChannel();
184
[email protected]8930d472009-02-21 08:05:28185 private:
186 virtual void OnControlMessageReceived(const IPC::Message& msg);
initial.commit09911bf2008-07-26 23:55:29187
[email protected]42f1d7822009-07-23 18:17:55188 void Init();
initial.commit09911bf2008-07-26 23:55:29189
[email protected]176aa482008-11-14 03:25:15190 void OnUpdateVisitedLinks(base::SharedMemoryHandle table);
[email protected]3e90d4a2009-07-03 17:38:39191 void OnAddVisitedLinks(const VisitedLinkSlave::Fingerprints& fingerprints);
192 void OnResetVisitedLinks();
[email protected]40bd6582009-12-04 23:49:51193 void OnSetZoomLevelForCurrentHost(const std::string& host, int zoom_level);
[email protected]f85f0702010-01-30 09:31:01194 void OnSetContentSettingsForCurrentHost(
195 const std::string& host, const ContentSettings& content_settings);
[email protected]b2a74ca2010-03-12 17:57:09196 void OnUpdateUserScripts(base::SharedMemoryHandle table);
[email protected]703e807a2009-03-28 19:56:51197 void OnSetExtensionFunctionNames(const std::vector<std::string>& names);
[email protected]45776222009-07-15 20:21:58198 void OnPageActionsUpdated(const std::string& extension_id,
[email protected]b7c7bcf2009-10-03 07:07:34199 const std::vector<std::string>& page_actions);
[email protected]c61cc652009-11-04 05:44:40200 void OnDOMStorageEvent(const ViewMsg_DOMStorageEvent_Params& params);
[email protected]75e126b932009-09-28 19:38:49201 void OnExtensionSetAPIPermissions(
202 const std::string& extension_id,
203 const std::vector<std::string>& permissions);
204 void OnExtensionSetHostPermissions(
205 const GURL& extension_url,
206 const std::vector<URLPattern>& permissions);
[email protected]db7331a2010-02-25 22:10:50207 void OnExtensionSetIncognitoEnabled(
208 const std::string& extension_id,
209 bool enabled);
initial.commit09911bf2008-07-26 23:55:29210 void OnSetNextPageID(int32 next_page_id);
[email protected]b2a74ca2010-03-12 17:57:09211 void OnSetIsIncognitoProcess(bool is_incognito_process);
[email protected]b9ab10c2009-08-07 18:09:55212 void OnSetCSSColors(const std::vector<CSSColors::CSSColorMapping>& colors);
[email protected]4e6419c2010-01-15 04:50:34213 void OnCreateNewView(const ViewMsg_New_Params& params);
initial.commit09911bf2008-07-26 23:55:29214 void OnTransferBitmap(const SkBitmap& bitmap, int resource_id);
215 void OnSetCacheCapacities(size_t min_dead_capacity,
216 size_t max_dead_capacity,
217 size_t capacity);
218 void OnGetCacheResourceStats();
219
[email protected]55e57d42009-02-25 06:10:17220 // Send all histograms to browser.
[email protected]c9a3ef82009-05-28 22:02:46221 void OnGetRendererHistograms(int sequence_number);
[email protected]55e57d42009-02-25 06:10:17222
[email protected]d41041092009-10-08 06:56:57223 // Send tcmalloc info to browser.
224 void OnGetRendererTcmalloc();
[email protected]38b48a82009-11-11 01:51:32225 void OnGetV8HeapStats();
[email protected]d41041092009-10-08 06:56:57226
[email protected]dfcb62a2009-06-17 19:32:43227 void OnExtensionMessageInvoke(const std::string& function_name,
[email protected]d7259472010-03-24 08:40:49228 const ListValue& args,
229 bool requires_incognito_access);
[email protected]fede6ca12009-10-08 18:24:26230 void OnPurgeMemory();
[email protected]b78e168b2009-09-21 22:05:45231 void OnPurgePluginListCache(bool reload_pages);
[email protected]75e5a872009-04-02 23:56:11232
[email protected]cb6037d2009-11-16 22:55:17233 void OnInitSpellChecker(IPC::PlatformFileForTransit bdict_file,
[email protected]85c55dc2009-11-06 03:05:46234 const std::vector<std::string>& custom_words,
235 const std::string& language,
236 bool auto_spell_correct);
237 void OnSpellCheckWordAdded(const std::string& word);
238 void OnSpellCheckEnableAutoSpellCorrect(bool enable);
[email protected]85c55dc2009-11-06 03:05:46239
[email protected]6217d392010-03-25 22:08:35240 void OnGpuChannelEstablished(const IPC::ChannelHandle& channel_handle);
241
initial.commit09911bf2008-07-26 23:55:29242 // Gather usage statistics from the in-memory cache and inform our host.
243 // These functions should be call periodically so that the host can make
244 // decisions about how to allocation resources using current information.
245 void InformHostOfCacheStats();
246
[email protected]90a3fbb12009-02-28 01:13:47247 // We initialize WebKit as late as possible.
248 void EnsureWebKitInitialized();
249
[email protected]bee16aab2009-08-26 15:55:03250 // A task we invoke periodically to assist with idle cleanup.
251 void IdleHandler();
252
[email protected]71d6d852009-12-07 22:12:36253 // Schedule a call to IdleHandler with the given initial delay.
254 void ScheduleIdleHandler(double initial_delay_s);
255
initial.commit09911bf2008-07-26 23:55:29256 // These objects live solely on the render thread.
[email protected]bee16aab2009-08-26 15:55:03257 scoped_ptr<ScopedRunnableMethodFactory<RenderThread> > task_factory_;
[email protected]42f1d7822009-07-23 18:17:55258 scoped_ptr<VisitedLinkSlave> visited_link_slave_;
259 scoped_ptr<UserScriptSlave> user_script_slave_;
260 scoped_ptr<RenderDnsMaster> dns_master_;
[email protected]f430b5712009-08-21 21:46:31261 scoped_ptr<AppCacheDispatcher> appcache_dispatcher_;
[email protected]9291ed12009-07-23 17:33:22262 scoped_refptr<DevToolsAgentFilter> devtools_agent_filter_;
[email protected]42f1d7822009-07-23 18:17:55263 scoped_ptr<RendererHistogramSnapshots> histogram_snapshots_;
264 scoped_ptr<RendererWebKitClientImpl> webkit_client_;
[email protected]b7c7bcf2009-10-03 07:07:34265 scoped_ptr<WebKit::WebStorageEventDispatcher> dom_storage_event_dispatcher_;
[email protected]2b437e232010-04-02 01:30:08266 scoped_ptr<WebDatabaseObserverImpl> web_database_observer_impl_;
[email protected]85c55dc2009-11-06 03:05:46267 scoped_ptr<SpellCheck> spellchecker_;
[email protected]9291ed12009-07-23 17:33:22268
[email protected]e2b2d4a2009-10-24 03:32:59269 // Used on the renderer and IPC threads.
[email protected]017022b2009-07-27 23:06:34270 scoped_refptr<DBMessageFilter> db_message_filter_;
[email protected]dd9241932010-02-24 19:23:13271 scoped_refptr<CookieMessageFilter> cookie_message_filter_;
[email protected]017022b2009-07-27 23:06:34272
[email protected]5fa1c542009-05-05 20:36:07273#if defined(OS_POSIX)
274 scoped_refptr<IPC::ChannelProxy::MessageFilter>
275 suicide_on_channel_error_filter_;
276#endif
277
[email protected]b547fd42009-04-23 23:16:27278 // If true, then a GetPlugins call is allowed to rescan the disk.
279 bool plugin_refresh_allowed_;
280
[email protected]bee16aab2009-08-26 15:55:03281 // Is there a pending task for doing CacheStats.
282 bool cache_stats_task_pending_;
283
284 // The count of RenderWidgets running through this thread.
285 int widget_count_;
286
287 // The count of hidden RenderWidgets running through this thread.
288 int hidden_widget_count_;
289
290 // The current value of the idle notification timer delay.
291 double idle_notification_delay_in_s_;
292
[email protected]71d6d852009-12-07 22:12:36293 // True if this renderer is running extensions.
294 bool is_extension_process_;
295
[email protected]b2a74ca2010-03-12 17:57:09296 // True if this renderer is incognito.
297 bool is_incognito_process_;
298
[email protected]80fc08c52010-03-09 07:43:50299 bool suspend_webkit_shared_timer_;
300 bool notify_webkit_of_modal_loop_;
[email protected]c1f50aa2010-02-18 03:46:57301 bool did_notify_webkit_of_modal_loop_;
302
[email protected]71d6d852009-12-07 22:12:36303 // Timer that periodically calls IdleHandler.
304 base::RepeatingTimer<RenderThread> idle_timer_;
305
306 // Same as above, but on a longer timer and will run even if the process is
307 // not idle, to ensure that IdleHandle gets called eventually.
308 base::RepeatingTimer<RenderThread> forced_idle_timer_;
309
[email protected]6217d392010-03-25 22:08:35310 // The channel from the renderer process to the GPU process.
311 scoped_refptr<GpuChannelHost> gpu_channel_;
312
[email protected]1bc83062009-02-06 00:16:37313 DISALLOW_COPY_AND_ASSIGN(RenderThread);
initial.commit09911bf2008-07-26 23:55:29314};
315
[email protected]1d97d2e2008-12-18 23:39:02316#endif // CHROME_RENDERER_RENDER_THREAD_H_