blob: 83754f7a431f6ceab28db6fed11f3e80efff6a33 [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
initial.commit09911bf2008-07-26 23:55:295#include "chrome/renderer/render_thread.h"
6
[email protected]da00a2882009-03-09 17:51:197#include <algorithm>
[email protected]61a9b2d82010-02-26 00:31:088#include <limits>
[email protected]75e126b932009-09-28 19:38:499#include <map>
[email protected]da00a2882009-03-09 17:51:1910#include <vector>
11
[email protected]06533c0b2009-03-05 21:39:1112#include "base/command_line.h"
[email protected]c53165ee2010-08-16 21:40:0713#include "base/field_trial.h"
[email protected]94f9a0f682009-06-15 18:30:3014#include "base/lazy_instance.h"
[email protected]bee16aab2009-08-26 15:55:0315#include "base/logging.h"
[email protected]b7c7bcf2009-10-03 07:07:3416#include "base/nullable_string16.h"
[email protected]d41041092009-10-08 06:56:5717#include "base/process_util.h"
initial.commit09911bf2008-07-26 23:55:2918#include "base/shared_memory.h"
[email protected]da00a2882009-03-09 17:51:1919#include "base/stats_table.h"
[email protected]b7c7bcf2009-10-03 07:07:3420#include "base/string_util.h"
[email protected]46f36a492010-07-28 19:36:4121#include "base/task.h"
[email protected]94f9a0f682009-06-15 18:30:3022#include "base/thread_local.h"
[email protected]a872ea1f2010-08-11 04:45:3323#include "base/trace_event.h"
[email protected]be1ce6a72010-08-03 14:35:2224#include "base/utf_string_conversions.h"
[email protected]7a4de7a62010-08-17 18:38:2425#include "base/values.h"
[email protected]f430b5712009-08-21 21:46:3126#include "chrome/common/appcache/appcache_dispatcher.h"
[email protected]c8865962009-12-16 07:47:3927#include "chrome/common/child_process_logging.h"
[email protected]06533c0b2009-03-05 21:39:1128#include "chrome/common/chrome_switches.h"
[email protected]017022b2009-07-27 23:06:3429#include "chrome/common/db_message_filter.h"
[email protected]e13ad79b2010-07-22 21:36:5030#include "chrome/common/dom_storage_common.h"
31#include "chrome/common/extensions/extension_extent.h"
[email protected]c1f50aa2010-02-18 03:46:5732#include "chrome/common/plugin_messages.h"
[email protected]e09ba552009-02-05 03:26:2933#include "chrome/common/render_messages.h"
[email protected]939856a2010-08-24 20:29:0234#include "chrome/common/render_messages_params.h"
[email protected]9b6f40e2009-06-11 15:54:2635#include "chrome/common/renderer_preferences.h"
[email protected]90a3fbb12009-02-28 01:13:4736#include "chrome/common/url_constants.h"
[email protected]2b437e232010-04-02 01:30:0837#include "chrome/common/web_database_observer_impl.h"
[email protected]8930d472009-02-21 08:05:2838#include "chrome/plugin/npobject_util.h"
[email protected]2c62b562009-01-27 19:04:5039// TODO(port)
40#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:2941#include "chrome/plugin/plugin_channel.h"
[email protected]2c62b562009-01-27 19:04:5042#else
[email protected]2c62b562009-01-27 19:04:5043#include "base/scoped_handle.h"
44#include "chrome/plugin/plugin_channel_base.h"
[email protected]2c62b562009-01-27 19:04:5045#endif
[email protected]a9602de2010-03-18 23:43:1146#include "chrome/renderer/automation/dom_automation_v8_extension.h"
[email protected]dd9241932010-02-24 19:23:1347#include "chrome/renderer/cookie_message_filter.h"
[email protected]a8624712009-04-17 00:51:3548#include "chrome/renderer/devtools_agent_filter.h"
[email protected]ad1f9bd2009-07-30 20:23:1549#include "chrome/renderer/extension_groups.h"
[email protected]2747dd842010-08-27 14:22:1250#include "chrome/renderer/extensions/chrome_app_bindings.h"
[email protected]a40caa972009-04-08 18:35:3451#include "chrome/renderer/extensions/event_bindings.h"
[email protected]309d7a282009-03-24 09:18:2752#include "chrome/renderer/extensions/extension_process_bindings.h"
[email protected]be77f0a2009-08-25 08:31:1753#include "chrome/renderer/extensions/js_only_v8_extensions.h"
[email protected]0aa477bd2009-03-23 22:21:4354#include "chrome/renderer/extensions/renderer_extension_bindings.h"
[email protected]3c8e3702009-05-01 16:27:4255#include "chrome/renderer/external_extension.h"
[email protected]e13ad79b2010-07-22 21:36:5056#include "chrome/renderer/gpu_channel_host.h"
[email protected]ee68378a2010-08-10 01:05:4157#include "chrome/renderer/gpu_video_service_host.h"
[email protected]70c19a932010-05-14 12:59:1158#include "chrome/renderer/indexed_db_dispatcher.h"
[email protected]0bc46552009-04-07 21:56:4259#include "chrome/renderer/loadtimes_extension_bindings.h"
[email protected]3530cd92010-06-27 06:22:0160#include "chrome/renderer/net/renderer_net_predictor.h"
[email protected]c1f50aa2010-02-18 03:46:5761#include "chrome/renderer/plugin_channel_host.h"
[email protected]00c39612010-03-06 02:53:2862#include "chrome/renderer/render_process_impl.h"
initial.commit09911bf2008-07-26 23:55:2963#include "chrome/renderer/render_view.h"
[email protected]40bd6582009-12-04 23:49:5164#include "chrome/renderer/render_view_visitor.h"
[email protected]e13ad79b2010-07-22 21:36:5065#include "chrome/renderer/renderer_histogram_snapshots.h"
[email protected]7ef8f602010-07-30 11:09:4766#include "chrome/renderer/renderer_webidbfactory_impl.h"
[email protected]8d86fce2009-02-26 23:37:5567#include "chrome/renderer/renderer_webkitclient_impl.h"
[email protected]85c55dc2009-11-06 03:05:4668#include "chrome/renderer/spellchecker/spellcheck.h"
[email protected]0938d3c2009-01-09 20:37:3569#include "chrome/renderer/user_script_slave.h"
[email protected]46f36a492010-07-28 19:36:4170#include "ipc/ipc_channel_handle.h"
[email protected]d55aaa132009-09-28 21:08:0471#include "ipc/ipc_message.h"
[email protected]cb6037d2009-11-16 22:55:1772#include "ipc/ipc_platform_file.h"
[email protected]9d797f32010-04-23 07:17:5473#include "net/base/net_util.h"
[email protected]e33cba42010-08-18 23:37:0374#include "third_party/sqlite/sqlite3.h"
[email protected]1b1f3eb2009-12-01 13:48:0475#include "third_party/tcmalloc/chromium/src/google/malloc_extension.h"
[email protected]418ed5ab2009-11-12 01:14:4976#include "third_party/WebKit/WebKit/chromium/public/WebCache.h"
77#include "third_party/WebKit/WebKit/chromium/public/WebColor.h"
78#include "third_party/WebKit/WebKit/chromium/public/WebCrossOriginPreflightResultCache.h"
79#include "third_party/WebKit/WebKit/chromium/public/WebDatabase.h"
80#include "third_party/WebKit/WebKit/chromium/public/WebFontCache.h"
[email protected]40bd6582009-12-04 23:49:5181#include "third_party/WebKit/WebKit/chromium/public/WebFrame.h"
[email protected]418ed5ab2009-11-12 01:14:4982#include "third_party/WebKit/WebKit/chromium/public/WebKit.h"
83#include "third_party/WebKit/WebKit/chromium/public/WebRuntimeFeatures.h"
84#include "third_party/WebKit/WebKit/chromium/public/WebScriptController.h"
85#include "third_party/WebKit/WebKit/chromium/public/WebSecurityPolicy.h"
86#include "third_party/WebKit/WebKit/chromium/public/WebStorageEventDispatcher.h"
87#include "third_party/WebKit/WebKit/chromium/public/WebString.h"
[email protected]4d51d5bf2010-07-26 18:48:2688#include "third_party/WebKit/WebKit/chromium/public/WebView.h"
[email protected]b07f29092009-06-05 07:33:2189#include "webkit/extensions/v8/benchmarking_extension.h"
[email protected]06533c0b2009-03-05 21:39:1190#include "webkit/extensions/v8/gears_extension.h"
[email protected]06533c0b2009-03-05 21:39:1191#include "webkit/extensions/v8/playback_extension.h"
[email protected]e0d92f282010-04-03 06:04:2792#include "v8/include/v8.h"
[email protected]2c62b562009-01-27 19:04:5093
[email protected]da00a2882009-03-09 17:51:1994#if defined(OS_WIN)
95#include <windows.h>
96#include <objbase.h>
97#endif
98
[email protected]fe819f52009-12-15 07:58:1199#if defined(OS_MACOSX)
100#include "chrome/app/breakpad_mac.h"
101#endif
102
[email protected]6217d392010-03-25 22:08:35103#if defined(OS_POSIX)
104#include "ipc/ipc_channel_posix.h"
105#endif
106
[email protected]2c434b32009-03-19 06:27:47107using WebKit::WebCache;
[email protected]fede6ca12009-10-08 18:24:26108using WebKit::WebCrossOriginPreflightResultCache;
109using WebKit::WebFontCache;
[email protected]f85f0702010-01-30 09:31:01110using WebKit::WebFrame;
[email protected]adf00bc2009-11-02 18:35:00111using WebKit::WebRuntimeFeatures;
[email protected]204758c2009-10-22 03:56:30112using WebKit::WebSecurityPolicy;
[email protected]98d7127b2009-10-23 18:26:51113using WebKit::WebScriptController;
[email protected]2c434b32009-03-19 06:27:47114using WebKit::WebString;
[email protected]b7c7bcf2009-10-03 07:07:34115using WebKit::WebStorageEventDispatcher;
[email protected]50ae00ef2009-10-19 05:11:03116using WebKit::WebView;
initial.commit09911bf2008-07-26 23:55:29117
[email protected]42f1d7822009-07-23 18:17:55118namespace {
[email protected]2c434b32009-03-19 06:27:47119static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
[email protected]bee16aab2009-08-26 15:55:03120static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */;
[email protected]71d6d852009-12-07 22:12:36121static const double kInitialExtensionIdleHandlerDelayS = 5.0 /* seconds */;
122static const int64 kMaxExtensionIdleHandlerDelayS = 5*60 /* seconds */;
[email protected]bee16aab2009-08-26 15:55:03123
[email protected]c53165ee2010-08-16 21:40:07124static const int kPrelauchGpuPercentage = 5;
125static const int kPrelauchGpuProcessDelayMS = 10000;
126
[email protected]f3ede412010-06-21 22:52:16127// Keep the global RenderThread in a TLS slot so it is impossible to access
128// incorrectly from the wrong thread.
[email protected]94f9a0f682009-06-15 18:30:30129static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls(
130 base::LINKER_INITIALIZED);
[email protected]1edc16b82009-04-07 17:45:54131
[email protected]5fa1c542009-05-05 20:36:07132#if defined(OS_POSIX)
133class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter {
134 void OnChannelError() {
135 // On POSIX, at least, one can install an unload handler which loops
136 // forever and leave behind a renderer process which eats 100% CPU forever.
137 //
138 // This is because the terminate signals (ViewMsg_ShouldClose and the error
139 // from the IPC channel) are routed to the main message loop but never
140 // processed (because that message loop is stuck in V8).
141 //
142 // One could make the browser SIGKILL the renderers, but that leaves open a
143 // large window where a browser failure (or a user, manually terminating
144 // the browser because "it's stuck") will leave behind a process eating all
145 // the CPU.
146 //
147 // So, we install a filter on the channel so that we can process this event
148 // here and kill the process.
[email protected]fe819f52009-12-15 07:58:11149
150#if defined(OS_MACOSX)
151 // TODO(viettrungluu): crbug.com/28547: The following is needed, as a
152 // stopgap, to avoid leaking due to not releasing Breakpad properly.
153 // TODO(viettrungluu): Investigate why this is being called.
154 if (IsCrashReporterEnabled()) {
155 LOG(INFO) << "Cleaning up Breakpad.";
156 DestructCrashReporter();
157 } else {
158 LOG(INFO) << "Breakpad not enabled; no clean-up needed.";
159 }
160#endif // OS_MACOSX
161
[email protected]5fa1c542009-05-05 20:36:07162 _exit(0);
163 }
164};
165#endif
[email protected]40bd6582009-12-04 23:49:51166
[email protected]f85f0702010-01-30 09:31:01167class RenderViewContentSettingsSetter : public RenderViewVisitor {
168 public:
[email protected]0314ae02010-04-08 09:18:29169 RenderViewContentSettingsSetter(const GURL& url,
[email protected]f85f0702010-01-30 09:31:01170 const ContentSettings& content_settings)
[email protected]0314ae02010-04-08 09:18:29171 : url_(url),
[email protected]f85f0702010-01-30 09:31:01172 content_settings_(content_settings) {
173 }
174
175 virtual bool Visit(RenderView* render_view) {
[email protected]0314ae02010-04-08 09:18:29176 if (GURL(render_view->webview()->mainFrame()->url()) == url_)
[email protected]433819d2010-01-30 20:20:01177 render_view->SetContentSettings(content_settings_);
[email protected]f85f0702010-01-30 09:31:01178 return true;
179 }
180
181 private:
[email protected]0314ae02010-04-08 09:18:29182 GURL url_;
[email protected]f85f0702010-01-30 09:31:01183 ContentSettings content_settings_;
184
185 DISALLOW_COPY_AND_ASSIGN(RenderViewContentSettingsSetter);
186};
187
[email protected]40bd6582009-12-04 23:49:51188class RenderViewZoomer : public RenderViewVisitor {
189 public:
[email protected]9d797f32010-04-23 07:17:54190 RenderViewZoomer(const GURL& url, int zoom_level)
191 : zoom_level_(zoom_level) {
192 host_ = net::GetHostOrSpecFromURL(url);
[email protected]40bd6582009-12-04 23:49:51193 }
194
195 virtual bool Visit(RenderView* render_view) {
196 WebView* webview = render_view->webview(); // Guaranteed non-NULL.
[email protected]9d797f32010-04-23 07:17:54197 if (net::GetHostOrSpecFromURL(GURL(webview->mainFrame()->url())) == host_)
[email protected]40bd6582009-12-04 23:49:51198 webview->setZoomLevel(false, zoom_level_);
199 return true;
200 }
201
202 private:
203 std::string host_;
204 int zoom_level_;
205
206 DISALLOW_COPY_AND_ASSIGN(RenderViewZoomer);
207};
[email protected]42f1d7822009-07-23 18:17:55208} // namespace
209
[email protected]e13ad79b2010-07-22 21:36:50210// Contains extension-related data that the renderer needs to know about.
211// TODO(mpcomplete): this doesn't feel like it belongs here. Find a better
212// place.
213struct RenderThread::ExtensionInfo {
214 std::string extension_id;
215 ExtensionExtent web_extent;
216 ExtensionExtent browse_extent;
217};
218
[email protected]42f1d7822009-07-23 18:17:55219// When we run plugins in process, we actually run them on the render thread,
220// which means that we need to make the render thread pump UI events.
221RenderThread::RenderThread() {
222 Init();
223}
224
225RenderThread::RenderThread(const std::string& channel_name)
226 : ChildThread(channel_name) {
227 Init();
228}
[email protected]5fa1c542009-05-05 20:36:07229
initial.commit09911bf2008-07-26 23:55:29230void RenderThread::Init() {
[email protected]a872ea1f2010-08-11 04:45:33231 TRACE_EVENT_BEGIN("RenderThread::Init", 0, "");
232
[email protected]94f9a0f682009-06-15 18:30:30233 lazy_tls.Pointer()->Set(this);
[email protected]2c62b562009-01-27 19:04:50234#if defined(OS_WIN)
[email protected]bdef78b52009-04-16 19:31:34235 // If you are running plugins in this thread you need COM active but in
236 // the normal case you don't.
[email protected]00c39612010-03-06 02:53:28237 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34238 CoInitialize(0);
[email protected]2c62b562009-01-27 19:04:50239#endif
initial.commit09911bf2008-07-26 23:55:29240
[email protected]71d6d852009-12-07 22:12:36241 std::string type_str = CommandLine::ForCurrentProcess()->GetSwitchValueASCII(
242 switches::kProcessType);
[email protected]31f87132010-04-21 23:36:21243 // In single process the single process is all there is.
244 is_extension_process_ = type_str == switches::kExtensionProcess ||
245 CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess);
[email protected]b2a74ca2010-03-12 17:57:09246 is_incognito_process_ = false;
[email protected]80fc08c52010-03-09 07:43:50247 suspend_webkit_shared_timer_ = true;
248 notify_webkit_of_modal_loop_ = true;
[email protected]42f1d7822009-07-23 18:17:55249 plugin_refresh_allowed_ = true;
[email protected]bee16aab2009-08-26 15:55:03250 cache_stats_task_pending_ = false;
251 widget_count_ = 0;
252 hidden_widget_count_ = 0;
[email protected]71d6d852009-12-07 22:12:36253 idle_notification_delay_in_s_ = is_extension_process_ ?
254 kInitialExtensionIdleHandlerDelayS : kInitialIdleHandlerDelayS;
[email protected]bee16aab2009-08-26 15:55:03255 task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this));
[email protected]8d86fce2009-02-26 23:37:55256
[email protected]8d86fce2009-02-26 23:37:55257 visited_link_slave_.reset(new VisitedLinkSlave());
[email protected]ee9080642010-04-07 00:23:06258 user_script_slave_.reset(new UserScriptSlave());
[email protected]74be069e82010-06-25 00:12:49259 renderer_net_predictor_.reset(new RendererNetPredictor());
[email protected]8d86fce2009-02-26 23:37:55260 histogram_snapshots_.reset(new RendererHistogramSnapshots());
[email protected]f430b5712009-08-21 21:46:31261 appcache_dispatcher_.reset(new AppCacheDispatcher(this));
[email protected]70c19a932010-05-14 12:59:11262 indexed_db_dispatcher_.reset(new IndexedDBDispatcher());
[email protected]dd9241932010-02-24 19:23:13263 spellchecker_.reset(new SpellCheck());
264
[email protected]a8624712009-04-17 00:51:35265 devtools_agent_filter_ = new DevToolsAgentFilter();
266 AddFilter(devtools_agent_filter_.get());
[email protected]dd9241932010-02-24 19:23:13267
[email protected]017022b2009-07-27 23:06:34268 db_message_filter_ = new DBMessageFilter();
269 AddFilter(db_message_filter_.get());
[email protected]dd9241932010-02-24 19:23:13270
271 cookie_message_filter_ = new CookieMessageFilter();
272 AddFilter(cookie_message_filter_.get());
[email protected]5fa1c542009-05-05 20:36:07273
274#if defined(OS_POSIX)
275 suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter;
276 AddFilter(suicide_on_channel_error_filter_.get());
277#endif
[email protected]515c7162010-07-12 23:46:17278
279 // Establish a channel to the GPU process asynchronously if requested. If the
280 // channel is established in time, EstablishGpuChannelSync will not block when
[email protected]c53165ee2010-08-16 21:40:07281 // it is later called. Delays by a fixed period of time to avoid loading the
282 // GPU immediately in an attempt to not slow startup time.
283 scoped_refptr<FieldTrial> prelaunch_trial(
284 new FieldTrial("PrelaunchGpuProcessExperiment", 100));
285 int prelaunch_group = prelaunch_trial->AppendGroup("prelaunch_gpu_process",
286 kPrelauchGpuPercentage);
287 if (prelaunch_group == prelaunch_trial->group() ||
288 CommandLine::ForCurrentProcess()->HasSwitch(
289 switches::kPrelaunchGpuProcess)) {
290 message_loop()->PostDelayedTask(FROM_HERE,
291 task_factory_->NewRunnableMethod(
292 &RenderThread::EstablishGpuChannel),
293 kPrelauchGpuProcessDelayMS);
[email protected]515c7162010-07-12 23:46:17294 }
[email protected]ee68378a2010-08-10 01:05:41295
296 GpuVideoServiceHost::get()->OnRendererThreadInit(MessageLoop::current());
[email protected]a872ea1f2010-08-11 04:45:33297
298 TRACE_EVENT_END("RenderThread::Init", 0, "");
initial.commit09911bf2008-07-26 23:55:29299}
300
[email protected]42f1d7822009-07-23 18:17:55301RenderThread::~RenderThread() {
[email protected]12cbfda32010-01-30 01:04:25302 // Wait for all databases to be closed.
[email protected]2b437e232010-04-02 01:30:08303 if (web_database_observer_impl_.get())
304 web_database_observer_impl_->WaitForAllDatabasesToClose();
[email protected]12cbfda32010-01-30 01:04:25305
[email protected]8d86fce2009-02-26 23:37:55306 // Shutdown in reverse of the initialization order.
[email protected]017022b2009-07-27 23:06:34307 RemoveFilter(db_message_filter_.get());
308 db_message_filter_ = NULL;
[email protected]2b437e232010-04-02 01:30:08309 RemoveFilter(devtools_agent_filter_.get());
[email protected]12cbfda32010-01-30 01:04:25310
[email protected]c6a7b862010-08-20 22:19:38311 // Shutdown the file thread if it's running.
312 if (file_thread_.get())
313 file_thread_->Stop();
314
[email protected]42f1d7822009-07-23 18:17:55315 if (webkit_client_.get())
[email protected]9291ed12009-07-23 17:33:22316 WebKit::shutdown();
[email protected]9291ed12009-07-23 17:33:22317
[email protected]94f9a0f682009-06-15 18:30:30318 lazy_tls.Pointer()->Set(NULL);
[email protected]8fd8de92008-08-12 23:50:30319
[email protected]8d86fce2009-02-26 23:37:55320 // TODO(port)
[email protected]2c62b562009-01-27 19:04:50321#if defined(OS_WIN)
initial.commit09911bf2008-07-26 23:55:29322 // Clean up plugin channels before this thread goes away.
323 PluginChannelBase::CleanupChannels();
[email protected]bdef78b52009-04-16 19:31:34324 // Don't call COM if the renderer is in the sandbox.
[email protected]00c39612010-03-06 02:53:28325 if (RenderProcessImpl::InProcessPlugins())
[email protected]bdef78b52009-04-16 19:31:34326 CoUninitialize();
[email protected]2c62b562009-01-27 19:04:50327#endif
initial.commit09911bf2008-07-26 23:55:29328}
329
[email protected]42f1d7822009-07-23 18:17:55330RenderThread* RenderThread::current() {
331 return lazy_tls.Pointer()->Get();
332}
333
[email protected]c1f50aa2010-02-18 03:46:57334int32 RenderThread::RoutingIDForCurrentContext() {
335 int32 routing_id = MSG_ROUTING_CONTROL;
336 if (v8::Context::InContext()) {
[email protected]d5ddf9a2010-03-04 00:48:29337 WebFrame* frame = WebFrame::frameForCurrentContext();
338 if (frame) {
339 RenderView* view = RenderView::FromWebView(frame->view());
340 if (view)
341 routing_id = view->routing_id();
342 }
[email protected]c1f50aa2010-02-18 03:46:57343 } else {
344 DLOG(WARNING) << "Not called within a script context!";
345 }
346 return routing_id;
347}
348
349bool RenderThread::Send(IPC::Message* msg) {
[email protected]80fc08c52010-03-09 07:43:50350 // Certain synchronous messages can result in an app-modal cookie prompt.
351 // This could cause a complete hang of Chrome if a windowed plug-in is trying
352 // to communicate with the renderer thread since the browser's UI thread
353 // could be stuck (within a Windows API call) trying to synchronously
354 // communicate with the plug-in. The remedy is to pump messages on this
355 // thread while the cookie prompt is showing. This creates an opportunity
356 // for re-entrancy into WebKit, so we need to take care to disable callbacks,
357 // timers, and pending network loads that could trigger such callbacks.
[email protected]c1f50aa2010-02-18 03:46:57358
[email protected]80fc08c52010-03-09 07:43:50359 bool pumping_events = false, may_show_cookie_prompt = false;
360 if (msg->is_sync()) {
361 if (msg->is_caller_pumping_messages()) {
362 pumping_events = true;
363 } else {
364 switch (msg->type()) {
365 case ViewHostMsg_GetCookies::ID:
366 case ViewHostMsg_GetRawCookies::ID:
367 case ViewHostMsg_DOMStorageSetItem::ID:
368 case ViewHostMsg_SyncLoad::ID:
[email protected]0a1a45432010-03-31 08:09:45369 case ViewHostMsg_AllowDatabase::ID:
[email protected]80fc08c52010-03-09 07:43:50370 may_show_cookie_prompt = true;
371 pumping_events = true;
372 break;
373 }
374 }
[email protected]c1f50aa2010-02-18 03:46:57375 }
376
[email protected]80fc08c52010-03-09 07:43:50377 bool suspend_webkit_shared_timer = true; // default value
378 std::swap(suspend_webkit_shared_timer, suspend_webkit_shared_timer_);
[email protected]c1f50aa2010-02-18 03:46:57379
[email protected]80fc08c52010-03-09 07:43:50380 bool notify_webkit_of_modal_loop = true; // default value
381 std::swap(notify_webkit_of_modal_loop, notify_webkit_of_modal_loop_);
382
383 gfx::NativeViewId host_window = 0;
[email protected]c1f50aa2010-02-18 03:46:57384
385 if (pumping_events) {
[email protected]80fc08c52010-03-09 07:43:50386 // See ViewMsg_SignalCookiePromptEvent.
387 if (may_show_cookie_prompt) {
388 static_cast<IPC::SyncMessage*>(msg)->set_pump_messages_event(
389 cookie_message_filter_->pump_messages_event());
390 }
391
392 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57393 webkit_client_->SuspendSharedTimer();
394
[email protected]39065d012010-07-09 11:22:46395 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57396 WebView::willEnterModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57397
[email protected]80fc08c52010-03-09 07:43:50398 RenderWidget* widget =
399 static_cast<RenderWidget*>(ResolveRoute(msg->routing_id()));
400 if (widget) {
401 host_window = widget->host_window();
[email protected]c1f50aa2010-02-18 03:46:57402 PluginChannelHost::Broadcast(
403 new PluginMsg_SignalModalDialogEvent(host_window));
404 }
405 }
406
407 bool rv = ChildThread::Send(msg);
408
409 if (pumping_events) {
410 if (host_window) {
411 PluginChannelHost::Broadcast(
412 new PluginMsg_ResetModalDialogEvent(host_window));
413 }
414
[email protected]39065d012010-07-09 11:22:46415 if (notify_webkit_of_modal_loop)
[email protected]c1f50aa2010-02-18 03:46:57416 WebView::didExitModalLoop();
[email protected]c1f50aa2010-02-18 03:46:57417
[email protected]80fc08c52010-03-09 07:43:50418 if (suspend_webkit_shared_timer)
[email protected]c1f50aa2010-02-18 03:46:57419 webkit_client_->ResumeSharedTimer();
[email protected]80fc08c52010-03-09 07:43:50420
421 // We may end up nesting calls to Send, so we defer the reset until we
422 // return to the top-most message loop.
423 if (may_show_cookie_prompt &&
424 cookie_message_filter_->pump_messages_event()->IsSignaled()) {
425 MessageLoop::current()->PostNonNestableTask(FROM_HERE,
426 NewRunnableMethod(cookie_message_filter_.get(),
427 &CookieMessageFilter::ResetPumpMessagesEvent));
428 }
[email protected]c1f50aa2010-02-18 03:46:57429 }
430
431 return rv;
432}
433
434void RenderThread::AddRoute(int32 routing_id,
435 IPC::Channel::Listener* listener) {
436 widget_count_++;
437 return ChildThread::AddRoute(routing_id, listener);
438}
439
440void RenderThread::RemoveRoute(int32 routing_id) {
441 widget_count_--;
442 return ChildThread::RemoveRoute(routing_id);
443}
444
[email protected]42f1d7822009-07-23 18:17:55445void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
446 channel()->AddFilter(filter);
447}
448
449void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
450 channel()->RemoveFilter(filter);
451}
452
[email protected]bee16aab2009-08-26 15:55:03453void RenderThread::WidgetHidden() {
454 DCHECK(hidden_widget_count_ < widget_count_);
[email protected]4a3dab22009-11-11 17:36:50455 hidden_widget_count_++;
[email protected]b8f41a192010-04-19 18:25:04456 if (!is_extension_process_ &&
[email protected]71d6d852009-12-07 22:12:36457 widget_count_ && hidden_widget_count_ == widget_count_)
458 ScheduleIdleHandler(kInitialIdleHandlerDelayS);
[email protected]bee16aab2009-08-26 15:55:03459}
460
461void RenderThread::WidgetRestored() {
[email protected]61a9b2d82010-02-26 00:31:08462 DCHECK_GT(hidden_widget_count_, 0);
[email protected]bee16aab2009-08-26 15:55:03463 hidden_widget_count_--;
[email protected]b8f41a192010-04-19 18:25:04464 if (!is_extension_process_)
[email protected]71d6d852009-12-07 22:12:36465 idle_timer_.Stop();
[email protected]bee16aab2009-08-26 15:55:03466}
467
[email protected]c1f50aa2010-02-18 03:46:57468void RenderThread::DoNotSuspendWebKitSharedTimer() {
[email protected]80fc08c52010-03-09 07:43:50469 suspend_webkit_shared_timer_ = false;
[email protected]c1f50aa2010-02-18 03:46:57470}
471
472void RenderThread::DoNotNotifyWebKitOfModalLoop() {
[email protected]80fc08c52010-03-09 07:43:50473 notify_webkit_of_modal_loop_ = false;
[email protected]c1f50aa2010-02-18 03:46:57474}
475
[email protected]42f1d7822009-07-23 18:17:55476void RenderThread::Resolve(const char* name, size_t length) {
[email protected]74be069e82010-06-25 00:12:49477 return renderer_net_predictor_->Resolve(name, length);
[email protected]42f1d7822009-07-23 18:17:55478}
479
480void RenderThread::SendHistograms(int sequence_number) {
481 return histogram_snapshots_->SendHistograms(sequence_number);
482}
483
[email protected]176aa482008-11-14 03:25:15484void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
[email protected]5fe733de2009-02-11 18:59:20485 DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
initial.commit09911bf2008-07-26 23:55:29486 visited_link_slave_->Init(table);
487}
488
[email protected]3e90d4a2009-07-03 17:38:39489void RenderThread::OnAddVisitedLinks(
490 const VisitedLinkSlave::Fingerprints& fingerprints) {
491 for (size_t i = 0; i < fingerprints.size(); ++i)
[email protected]50ae00ef2009-10-19 05:11:03492 WebView::updateVisitedLinkState(fingerprints[i]);
[email protected]3e90d4a2009-07-03 17:38:39493}
494
495void RenderThread::OnResetVisitedLinks() {
[email protected]50ae00ef2009-10-19 05:11:03496 WebView::resetVisitedLinkState();
[email protected]3e90d4a2009-07-03 17:38:39497}
498
[email protected]0314ae02010-04-08 09:18:29499void RenderThread::OnSetContentSettingsForCurrentURL(
500 const GURL& url,
[email protected]f85f0702010-01-30 09:31:01501 const ContentSettings& content_settings) {
[email protected]0314ae02010-04-08 09:18:29502 RenderViewContentSettingsSetter setter(url, content_settings);
[email protected]f85f0702010-01-30 09:31:01503 RenderView::ForEach(&setter);
504}
505
[email protected]9d797f32010-04-23 07:17:54506void RenderThread::OnSetZoomLevelForCurrentURL(const GURL& url,
507 int zoom_level) {
508 RenderViewZoomer zoomer(url, zoom_level);
[email protected]40bd6582009-12-04 23:49:51509 RenderView::ForEach(&zoomer);
510}
511
[email protected]b2a74ca2010-03-12 17:57:09512void RenderThread::OnUpdateUserScripts(base::SharedMemoryHandle scripts) {
[email protected]5fe733de2009-02-11 18:59:20513 DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
[email protected]ee9080642010-04-07 00:23:06514 user_script_slave_->UpdateScripts(scripts);
[email protected]c8865962009-12-16 07:47:39515 UpdateActiveExtensions();
[email protected]1e0f70402008-10-16 23:57:47516}
517
[email protected]703e807a2009-03-28 19:56:51518void RenderThread::OnSetExtensionFunctionNames(
519 const std::vector<std::string>& names) {
[email protected]a1a0df02009-04-09 08:18:04520 ExtensionProcessBindings::SetFunctionNames(names);
[email protected]703e807a2009-03-28 19:56:51521}
522
[email protected]3a8eecb2010-04-22 23:56:30523void RenderThread::OnExtensionExtentsUpdated(
524 const ViewMsg_ExtensionExtentsUpdated_Params& params) {
525 extension_extents_.resize(params.extension_apps.size());
526 for (size_t i = 0; i < params.extension_apps.size(); ++i) {
[email protected]4fdbc1492010-07-01 01:20:59527 extension_extents_[i].extension_id = params.extension_apps[i].extension_id;
528 extension_extents_[i].web_extent = params.extension_apps[i].web_extent;
529 extension_extents_[i].browse_extent =
530 params.extension_apps[i].browse_extent;
[email protected]3a8eecb2010-04-22 23:56:30531 }
532}
533
[email protected]45776222009-07-15 20:21:58534void RenderThread::OnPageActionsUpdated(
535 const std::string& extension_id,
536 const std::vector<std::string>& page_actions) {
537 ExtensionProcessBindings::SetPageActions(extension_id, page_actions);
538}
539
[email protected]cccf90932009-08-23 17:56:25540void RenderThread::OnExtensionSetAPIPermissions(
[email protected]35506352009-08-07 18:58:19541 const std::string& extension_id,
542 const std::vector<std::string>& permissions) {
[email protected]cccf90932009-08-23 17:56:25543 ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions);
[email protected]71d6d852009-12-07 22:12:36544
545 // This is called when starting a new extension page, so start the idle
546 // handler ticking.
[email protected]71d6d852009-12-07 22:12:36547 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]c8865962009-12-16 07:47:39548
549 UpdateActiveExtensions();
[email protected]cccf90932009-08-23 17:56:25550}
551
552void RenderThread::OnExtensionSetHostPermissions(
553 const GURL& extension_url, const std::vector<URLPattern>& permissions) {
554 ExtensionProcessBindings::SetHostPermissions(extension_url, permissions);
[email protected]35506352009-08-07 18:58:19555}
556
[email protected]db7331a2010-02-25 22:10:50557void RenderThread::OnExtensionSetIncognitoEnabled(
558 const std::string& extension_id, bool enabled) {
559 ExtensionProcessBindings::SetIncognitoEnabled(extension_id, enabled);
560}
561
[email protected]c61cc652009-11-04 05:44:40562void RenderThread::OnDOMStorageEvent(
563 const ViewMsg_DOMStorageEvent_Params& params) {
564 if (!dom_storage_event_dispatcher_.get())
[email protected]b7c7bcf2009-10-03 07:07:34565 dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create());
[email protected]c61cc652009-11-04 05:44:40566 dom_storage_event_dispatcher_->dispatchStorageEvent(params.key_,
[email protected]ee2be5b32009-11-05 09:13:12567 params.old_value_, params.new_value_, params.origin_, params.url_,
[email protected]c61cc652009-11-04 05:44:40568 params.storage_type_ == DOM_STORAGE_LOCAL);
[email protected]b7c7bcf2009-10-03 07:07:34569}
570
[email protected]8930d472009-02-21 08:05:28571void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
[email protected]70c19a932010-05-14 12:59:11572 // Some messages are handled by delegates.
[email protected]f430b5712009-08-21 21:46:31573 if (appcache_dispatcher_->OnMessageReceived(msg))
[email protected]1edc16b82009-04-07 17:45:54574 return;
[email protected]70c19a932010-05-14 12:59:11575 if (indexed_db_dispatcher_->OnMessageReceived(msg))
576 return;
[email protected]1edc16b82009-04-07 17:45:54577
[email protected]8930d472009-02-21 08:05:28578 IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
579 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
[email protected]3e90d4a2009-07-03 17:38:39580 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks)
581 IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks)
[email protected]0314ae02010-04-08 09:18:29582 IPC_MESSAGE_HANDLER(ViewMsg_SetContentSettingsForCurrentURL,
583 OnSetContentSettingsForCurrentURL)
[email protected]9d797f32010-04-23 07:17:54584 IPC_MESSAGE_HANDLER(ViewMsg_SetZoomLevelForCurrentURL,
585 OnSetZoomLevelForCurrentURL)
[email protected]b2a74ca2010-03-12 17:57:09586 IPC_MESSAGE_HANDLER(ViewMsg_SetIsIncognitoProcess, OnSetIsIncognitoProcess)
[email protected]8930d472009-02-21 08:05:28587 IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
[email protected]b9ab10c2009-08-07 18:09:55588 IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors)
[email protected]8930d472009-02-21 08:05:28589 // TODO(port): removed from render_messages_internal.h;
590 // is there a new non-windows message I should add here?
591 IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
592 IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
[email protected]55e57d42009-02-25 06:10:17593 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
[email protected]c9a3ef82009-05-28 22:02:46594 OnGetRendererHistograms)
[email protected]d41041092009-10-08 06:56:57595#if defined(USE_TCMALLOC)
596 IPC_MESSAGE_HANDLER(ViewMsg_GetRendererTcmalloc,
597 OnGetRendererTcmalloc)
598#endif
[email protected]38b48a82009-11-11 01:51:32599 IPC_MESSAGE_HANDLER(ViewMsg_GetV8HeapStats, OnGetV8HeapStats)
[email protected]8930d472009-02-21 08:05:28600 IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
601 OnGetCacheResourceStats)
[email protected]2f2243e2009-05-26 02:27:02602 IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts,
[email protected]8930d472009-02-21 08:05:28603 OnUpdateUserScripts)
[email protected]b68d5ed2009-04-16 02:41:28604 // TODO(rafaelw): create an ExtensionDispatcher that handles extension
605 // messages seperates their handling from the RenderThread.
[email protected]dfcb62a2009-06-17 19:32:43606 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke,
607 OnExtensionMessageInvoke)
[email protected]703e807a2009-03-28 19:56:51608 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames,
609 OnSetExtensionFunctionNames)
[email protected]3a8eecb2010-04-22 23:56:30610 IPC_MESSAGE_HANDLER(ViewMsg_ExtensionExtentsUpdated,
611 OnExtensionExtentsUpdated)
[email protected]fede6ca12009-10-08 18:24:26612 IPC_MESSAGE_HANDLER(ViewMsg_PurgeMemory, OnPurgeMemory)
[email protected]b547fd42009-04-23 23:16:27613 IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache,
614 OnPurgePluginListCache)
[email protected]45776222009-07-15 20:21:58615 IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions,
616 OnPageActionsUpdated)
[email protected]cccf90932009-08-23 17:56:25617 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions,
618 OnExtensionSetAPIPermissions)
619 IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions,
620 OnExtensionSetHostPermissions)
[email protected]db7331a2010-02-25 22:10:50621 IPC_MESSAGE_HANDLER(ViewMsg_Extension_ExtensionSetIncognitoEnabled,
622 OnExtensionSetIncognitoEnabled)
[email protected]b7c7bcf2009-10-03 07:07:34623 IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent,
624 OnDOMStorageEvent)
[email protected]d55aaa132009-09-28 21:08:04625#if defined(IPC_MESSAGE_LOG_ENABLED)
[email protected]12636df2009-09-28 22:32:21626 IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled,
[email protected]d55aaa132009-09-28 21:08:04627 OnSetIPCLoggingEnabled)
628#endif
[email protected]85c55dc2009-11-06 03:05:46629 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_Init,
630 OnInitSpellChecker)
631 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_WordAdded,
632 OnSpellCheckWordAdded)
633 IPC_MESSAGE_HANDLER(ViewMsg_SpellChecker_EnableAutoSpellCorrect,
634 OnSpellCheckEnableAutoSpellCorrect)
[email protected]6217d392010-03-25 22:08:35635 IPC_MESSAGE_HANDLER(ViewMsg_GpuChannelEstablished, OnGpuChannelEstablished)
[email protected]8930d472009-02-21 08:05:28636 IPC_END_MESSAGE_MAP()
initial.commit09911bf2008-07-26 23:55:29637}
638
639void RenderThread::OnSetNextPageID(int32 next_page_id) {
640 // This should only be called at process initialization time, so we shouldn't
641 // have to worry about thread-safety.
642 RenderView::SetNextPageID(next_page_id);
643}
644
[email protected]b9ab10c2009-08-07 18:09:55645// Called when to register CSS Color name->system color mappings.
646// We update the colors one by one and then tell WebKit to refresh all render
647// views.
648void RenderThread::OnSetCSSColors(
649 const std::vector<CSSColors::CSSColorMapping>& colors) {
[email protected]f5e4b9bf2009-10-08 00:59:59650 EnsureWebKitInitialized();
[email protected]b9ab10c2009-08-07 18:09:55651 size_t num_colors = colors.size();
652 scoped_array<WebKit::WebColorName> color_names(
653 new WebKit::WebColorName[num_colors]);
654 scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]);
655 size_t i = 0;
656 for (std::vector<CSSColors::CSSColorMapping>::const_iterator it =
657 colors.begin();
658 it != colors.end();
659 ++it, ++i) {
660 color_names[i] = it->first;
661 web_colors[i] = it->second;
662 }
663 WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors);
664}
665
[email protected]4e6419c2010-01-15 04:50:34666void RenderThread::OnCreateNewView(const ViewMsg_New_Params& params) {
[email protected]90a3fbb12009-02-28 01:13:47667 EnsureWebKitInitialized();
[email protected]be645db2009-02-06 20:36:33668 // When bringing in render_view, also bring in webkit's glue and jsbindings.
initial.commit09911bf2008-07-26 23:55:29669 RenderView::Create(
[email protected]8ab04652010-06-12 02:47:26670 this,
671 params.parent_window,
672 MSG_ROUTING_NONE,
673 params.renderer_preferences,
674 params.web_preferences,
675 new SharedRenderViewCounter(0),
676 params.view_id,
677 params.session_storage_namespace_id,
678 params.frame_name);
[email protected]7f874dec2009-02-06 01:48:27679}
[email protected]4274e582009-01-27 22:09:56680
initial.commit09911bf2008-07-26 23:55:29681void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
682 size_t max_dead_capacity,
683 size_t capacity) {
[email protected]90a3fbb12009-02-28 01:13:47684 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47685 WebCache::setCapacities(
686 min_dead_capacity, max_dead_capacity, capacity);
initial.commit09911bf2008-07-26 23:55:29687}
688
689void RenderThread::OnGetCacheResourceStats() {
[email protected]90a3fbb12009-02-28 01:13:47690 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47691 WebCache::ResourceTypeStats stats;
692 WebCache::getResourceTypeStats(&stats);
initial.commit09911bf2008-07-26 23:55:29693 Send(new ViewHostMsg_ResourceTypeStats(stats));
694}
695
[email protected]c9a3ef82009-05-28 22:02:46696void RenderThread::OnGetRendererHistograms(int sequence_number) {
697 SendHistograms(sequence_number);
[email protected]55e57d42009-02-25 06:10:17698}
699
[email protected]d41041092009-10-08 06:56:57700#if defined(USE_TCMALLOC)
701void RenderThread::OnGetRendererTcmalloc() {
702 std::string result;
703 char buffer[1024 * 32];
[email protected]a4dc33f2009-10-20 15:09:55704 base::ProcessId pid = base::GetCurrentProcId();
[email protected]d41041092009-10-08 06:56:57705 MallocExtension::instance()->GetStats(buffer, sizeof(buffer));
706 result.append(buffer);
707 Send(new ViewHostMsg_RendererTcmalloc(pid, result));
708}
709#endif
710
[email protected]38b48a82009-11-11 01:51:32711void RenderThread::OnGetV8HeapStats() {
712 v8::HeapStatistics heap_stats;
713 v8::V8::GetHeapStatistics(&heap_stats);
714 Send(new ViewHostMsg_V8HeapStats(heap_stats.total_heap_size(),
715 heap_stats.used_heap_size()));
716}
717
initial.commit09911bf2008-07-26 23:55:29718void RenderThread::InformHostOfCacheStats() {
[email protected]90a3fbb12009-02-28 01:13:47719 EnsureWebKitInitialized();
[email protected]2c434b32009-03-19 06:27:47720 WebCache::UsageStats stats;
721 WebCache::getUsageStats(&stats);
initial.commit09911bf2008-07-26 23:55:29722 Send(new ViewHostMsg_UpdatedCacheStats(stats));
[email protected]bee16aab2009-08-26 15:55:03723 cache_stats_task_pending_ = false;
initial.commit09911bf2008-07-26 23:55:29724}
725
726void RenderThread::InformHostOfCacheStatsLater() {
727 // Rate limit informing the host of our cache stats.
[email protected]bee16aab2009-08-26 15:55:03728 if (cache_stats_task_pending_)
initial.commit09911bf2008-07-26 23:55:29729 return;
730
[email protected]bee16aab2009-08-26 15:55:03731 cache_stats_task_pending_ = true;
initial.commit09911bf2008-07-26 23:55:29732 MessageLoop::current()->PostDelayedTask(FROM_HERE,
[email protected]bee16aab2009-08-26 15:55:03733 task_factory_->NewRunnableMethod(
initial.commit09911bf2008-07-26 23:55:29734 &RenderThread::InformHostOfCacheStats),
735 kCacheStatsDelayMS);
736}
[email protected]90a3fbb12009-02-28 01:13:47737
[email protected]c40acc32010-01-14 01:02:53738void RenderThread::CloseCurrentConnections() {
739 Send(new ViewHostMsg_CloseCurrentConnections());
[email protected]b07f29092009-06-05 07:33:21740}
741
742void RenderThread::SetCacheMode(bool enabled) {
743 Send(new ViewHostMsg_SetCacheMode(enabled));
744}
745
[email protected]c5d79342010-06-05 01:27:34746void RenderThread::ClearCache() {
747 int rv;
748 Send(new ViewHostMsg_ClearCache(&rv));
749}
750
[email protected]12893c32010-08-19 17:30:54751void RenderThread::EnableSpdy(bool enable) {
752 Send(new ViewHostMsg_EnableSpdy(enable));
753}
754
[email protected]c8865962009-12-16 07:47:39755void RenderThread::UpdateActiveExtensions() {
756 // In single-process mode, the browser process reports the active extensions.
757 if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kSingleProcess))
758 return;
759
760 std::set<std::string> active_extensions;
761 user_script_slave_->GetActiveExtensions(&active_extensions);
762 ExtensionProcessBindings::GetActiveExtensions(&active_extensions);
763 child_process_logging::SetActiveExtensions(active_extensions);
764}
765
[email protected]6217d392010-03-25 22:08:35766void RenderThread::EstablishGpuChannel() {
767 if (gpu_channel_.get()) {
[email protected]1082b1d2010-03-30 00:31:22768 // Do nothing if we already have a GPU channel or are already
769 // establishing one.
770 if (gpu_channel_->state() == GpuChannelHost::UNCONNECTED ||
771 gpu_channel_->state() == GpuChannelHost::CONNECTED)
[email protected]6217d392010-03-25 22:08:35772 return;
773
774 // Recreate the channel if it has been lost.
775 if (gpu_channel_->state() == GpuChannelHost::LOST)
776 gpu_channel_ = NULL;
777 }
778
779 if (!gpu_channel_.get())
780 gpu_channel_ = new GpuChannelHost;
781
782 // Ask the browser for the channel name.
783 Send(new ViewHostMsg_EstablishGpuChannel());
784}
785
[email protected]3bf4d532010-03-27 00:23:34786GpuChannelHost* RenderThread::EstablishGpuChannelSync() {
[email protected]f9a2b2fe2010-07-15 21:13:23787 EstablishGpuChannel();
788 Send(new ViewHostMsg_SynchronizeGpu());
789 return GetGpuChannel();
[email protected]3bf4d532010-03-27 00:23:34790}
791
[email protected]6217d392010-03-25 22:08:35792GpuChannelHost* RenderThread::GetGpuChannel() {
793 if (!gpu_channel_.get())
794 return NULL;
795
796 if (gpu_channel_->state() != GpuChannelHost::CONNECTED)
797 return NULL;
798
799 return gpu_channel_.get();
800}
801
[email protected]3df0c202009-03-31 23:51:26802static void* CreateHistogram(
803 const char *name, int min, int max, size_t buckets) {
[email protected]e8829a192009-12-06 00:09:37804 if (min <= 0)
805 min = 1;
[email protected]2753b392009-12-28 06:59:52806 scoped_refptr<Histogram> histogram = Histogram::FactoryGet(
807 name, min, max, buckets, Histogram::kUmaTargetedHistogramFlag);
[email protected]e8829a192009-12-06 00:09:37808 // We'll end up leaking these histograms, unless there is some code hiding in
809 // there to do the dec-ref.
[email protected]2753b392009-12-28 06:59:52810 // TODO(jar): Handle reference counting in webkit glue.
[email protected]e8829a192009-12-06 00:09:37811 histogram->AddRef();
812 return histogram.get();
[email protected]3df0c202009-03-31 23:51:26813}
814
815static void AddHistogramSample(void* hist, int sample) {
816 Histogram* histogram = static_cast<Histogram *>(hist);
817 histogram->Add(sample);
818}
819
[email protected]90a3fbb12009-02-28 01:13:47820void RenderThread::EnsureWebKitInitialized() {
821 if (webkit_client_.get())
822 return;
[email protected]da00a2882009-03-09 17:51:19823
[email protected]71d6d852009-12-07 22:12:36824 // For extensions, we want to ensure we call the IdleHandler every so often,
825 // even if the extension keeps up activity.
[email protected]b8f41a192010-04-19 18:25:04826 if (is_extension_process_) {
[email protected]71d6d852009-12-07 22:12:36827 forced_idle_timer_.Start(
828 base::TimeDelta::FromSeconds(kMaxExtensionIdleHandlerDelayS),
829 this, &RenderThread::IdleHandler);
830 }
831
[email protected]da00a2882009-03-09 17:51:19832 v8::V8::SetCounterFunction(StatsTable::FindLocation);
[email protected]3df0c202009-03-31 23:51:26833 v8::V8::SetCreateHistogramFunction(CreateHistogram);
834 v8::V8::SetAddHistogramSampleFunction(AddHistogramSample);
[email protected]da00a2882009-03-09 17:51:19835
[email protected]90a3fbb12009-02-28 01:13:47836 webkit_client_.reset(new RendererWebKitClientImpl);
837 WebKit::initialize(webkit_client_.get());
[email protected]8881eca82009-03-12 18:20:44838
[email protected]98d7127b2009-10-23 18:26:51839 WebScriptController::enableV8SingleThreadMode();
[email protected]4ea00e82009-07-30 22:59:34840
[email protected]60e448982009-05-06 04:21:16841 // chrome: pages should not be accessible by normal content, and should
[email protected]8881eca82009-03-12 18:20:44842 // also be unable to script anything but themselves (to help limit the damage
[email protected]60e448982009-05-06 04:21:16843 // that a corrupt chrome: page could cause).
[email protected]2c434b32009-03-19 06:27:47844 WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme));
[email protected]204758c2009-10-22 03:56:30845 WebSecurityPolicy::registerURLSchemeAsLocal(chrome_ui_scheme);
846 WebSecurityPolicy::registerURLSchemeAsNoAccess(chrome_ui_scheme);
[email protected]da00a2882009-03-09 17:51:19847
[email protected]b4e75c12010-05-18 18:28:48848 // chrome-extension: resources shouldn't trigger insecure content warnings.
[email protected]37e5a9a2010-03-13 09:42:39849 WebString extension_scheme(ASCIIToUTF16(chrome::kExtensionScheme));
850 WebSecurityPolicy::registerURLSchemeAsSecure(extension_scheme);
851
[email protected]6cf8b1a22009-05-26 23:36:00852#if defined(OS_WIN)
853 // We don't yet support Gears on non-Windows, so don't tell pages that we do.
[email protected]98d7127b2009-10-23 18:26:51854 WebScriptController::registerExtension(extensions_v8::GearsExtension::Get());
[email protected]6cf8b1a22009-05-26 23:36:00855#endif
[email protected]98d7127b2009-10-23 18:26:51856 WebScriptController::registerExtension(
[email protected]98d7127b2009-10-23 18:26:51857 extensions_v8::LoadTimesExtension::Get());
858 WebScriptController::registerExtension(
[email protected]2747dd842010-08-27 14:22:12859 extensions_v8::ChromeAppExtension::Get());
860 WebScriptController::registerExtension(
[email protected]98d7127b2009-10-23 18:26:51861 extensions_v8::ExternalExtension::Get());
[email protected]309d7a282009-03-24 09:18:27862
[email protected]ad1f9bd2009-07-30 20:23:15863 const WebKit::WebString kExtensionScheme =
864 WebKit::WebString::fromUTF8(chrome::kExtensionScheme);
865
[email protected]98d7127b2009-10-23 18:26:51866 WebScriptController::registerExtension(
867 ExtensionProcessBindings::Get(), kExtensionScheme);
[email protected]ad1f9bd2009-07-30 20:23:15868
[email protected]98d7127b2009-10-23 18:26:51869 WebScriptController::registerExtension(
870 BaseJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
871 WebScriptController::registerExtension(
872 BaseJsV8Extension::Get(), kExtensionScheme);
873 WebScriptController::registerExtension(
874 JsonSchemaJsV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
875 WebScriptController::registerExtension(JsonSchemaJsV8Extension::Get(),
876 kExtensionScheme);
877 WebScriptController::registerExtension(
878 EventBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
879 WebScriptController::registerExtension(EventBindings::Get(),
880 kExtensionScheme);
881 WebScriptController::registerExtension(
882 RendererExtensionBindings::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
883 WebScriptController::registerExtension(
884 RendererExtensionBindings::Get(), kExtensionScheme);
885 WebScriptController::registerExtension(
886 ExtensionApiTestV8Extension::Get(), kExtensionScheme);
887 WebScriptController::registerExtension(
888 ExtensionApiTestV8Extension::Get(), EXTENSION_GROUP_CONTENT_SCRIPTS);
[email protected]a1a0df02009-04-09 08:18:04889
[email protected]2b437e232010-04-02 01:30:08890 web_database_observer_impl_.reset(new WebDatabaseObserverImpl(this));
891 WebKit::WebDatabase::setObserver(web_database_observer_impl_.get());
[email protected]e2b2d4a2009-10-24 03:32:59892
[email protected]06533c0b2009-03-05 21:39:11893 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
[email protected]a1a0df02009-04-09 08:18:04894
[email protected]98d7127b2009-10-23 18:26:51895 if (command_line.HasSwitch(switches::kEnableBenchmarking)) {
896 WebScriptController::registerExtension(
897 extensions_v8::BenchmarkingExtension::Get());
898 }
[email protected]b07f29092009-06-05 07:33:21899
[email protected]06533c0b2009-03-05 21:39:11900 if (command_line.HasSwitch(switches::kPlaybackMode) ||
[email protected]ca12c842009-04-14 22:20:18901 command_line.HasSwitch(switches::kRecordMode) ||
902 command_line.HasSwitch(switches::kNoJsRandomness)) {
[email protected]98d7127b2009-10-23 18:26:51903 WebScriptController::registerExtension(
904 extensions_v8::PlaybackExtension::Get());
[email protected]06533c0b2009-03-05 21:39:11905 }
[email protected]2cb82332009-03-18 17:24:55906
[email protected]a9602de2010-03-18 23:43:11907 if (command_line.HasSwitch(switches::kDomAutomationController)) {
908 WebScriptController::registerExtension(DomAutomationV8Extension::Get());
909 }
910
[email protected]b1b13212009-10-30 06:42:01911 WebRuntimeFeatures::enableMediaPlayer(
[email protected]00c39612010-03-06 02:53:28912 RenderProcess::current()->HasInitializedMediaLibrary());
[email protected]3553c6cc2009-10-20 05:51:55913
[email protected]b1b13212009-10-30 06:42:01914 WebRuntimeFeatures::enableSockets(
[email protected]cdf24782009-11-05 09:32:47915 !command_line.HasSwitch(switches::kDisableWebSockets));
[email protected]0afb9932009-10-24 00:37:58916
[email protected]b1b13212009-10-30 06:42:01917 WebRuntimeFeatures::enableDatabase(
[email protected]90a7ad522009-11-20 22:42:53918 !command_line.HasSwitch(switches::kDisableDatabases));
[email protected]bfa69d492009-10-31 03:27:19919
[email protected]13bc0d822009-11-07 01:17:14920 WebRuntimeFeatures::enableApplicationCache(
[email protected]425c270642010-01-26 20:42:37921 !command_line.HasSwitch(switches::kDisableApplicationCache));
[email protected]13bc0d822009-11-07 01:17:14922
[email protected]bfa69d492009-10-31 03:27:19923 WebRuntimeFeatures::enableNotifications(
[email protected]380f1862009-11-14 02:48:01924 !command_line.HasSwitch(switches::kDisableDesktopNotifications));
[email protected]7ee66e62009-11-05 09:16:30925
926 WebRuntimeFeatures::enableLocalStorage(
[email protected]5cf18362009-11-06 18:45:01927 !command_line.HasSwitch(switches::kDisableLocalStorage));
[email protected]7ee66e62009-11-05 09:16:30928 WebRuntimeFeatures::enableSessionStorage(
[email protected]03567802010-02-24 11:48:53929 !command_line.HasSwitch(switches::kDisableSessionStorage));
[email protected]2236bd1f2010-01-20 15:17:08930
[email protected]582fab732010-02-02 02:50:28931 WebRuntimeFeatures::enableIndexedDatabase(
932 command_line.HasSwitch(switches::kEnableIndexedDatabase));
933
[email protected]2236bd1f2010-01-20 15:17:08934 WebRuntimeFeatures::enableGeolocation(
[email protected]d0569f42010-04-01 10:48:18935 !command_line.HasSwitch(switches::kDisableGeolocation));
[email protected]7a716c82010-03-03 10:33:44936
937 WebRuntimeFeatures::enableWebGL(
938 command_line.HasSwitch(switches::kEnableExperimentalWebGL));
[email protected]ccbe04e2010-03-17 17:58:43939
940 WebRuntimeFeatures::enablePushState(true);
[email protected]f2e3d6c2010-04-30 14:23:04941
942 WebRuntimeFeatures::enableTouch(
943 command_line.HasSwitch(switches::kEnableTouch));
[email protected]66249e22010-07-22 09:55:33944
[email protected]1f501972010-08-09 05:40:02945 WebRuntimeFeatures::enableDeviceMotion(
946 command_line.HasSwitch(switches::kEnableDeviceMotion));
947
[email protected]66249e22010-07-22 09:55:33948 WebRuntimeFeatures::enableDeviceOrientation(
949 command_line.HasSwitch(switches::kEnableDeviceOrientation));
[email protected]c8647272010-07-30 21:52:57950
951 WebRuntimeFeatures::enableSpeechInput(
952 command_line.HasSwitch(switches::kEnableSpeechInput));
[email protected]90a3fbb12009-02-28 01:13:47953}
[email protected]75e5a872009-04-02 23:56:11954
[email protected]bee16aab2009-08-26 15:55:03955void RenderThread::IdleHandler() {
[email protected]61a9b2d82010-02-26 00:31:08956#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]e94afbb92009-10-01 00:25:41957 MallocExtension::instance()->ReleaseFreeMemory();
[email protected]05001182009-09-15 23:34:22958#endif
[email protected]bee16aab2009-08-26 15:55:03959
[email protected]f5e4b9bf2009-10-08 00:59:59960 v8::V8::IdleNotification();
[email protected]05001182009-09-15 23:34:22961
962 // Schedule next invocation.
963 // Dampen the delay using the algorithm:
964 // delay = delay + 1 / (delay + 2)
965 // Using floor(delay) has a dampening effect such as:
966 // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ...
967 // Note that idle_notification_delay_in_s_ would be reset to
968 // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden.
[email protected]71d6d852009-12-07 22:12:36969 ScheduleIdleHandler(idle_notification_delay_in_s_ +
970 1.0 / (idle_notification_delay_in_s_ + 2.0));
[email protected]b8f41a192010-04-19 18:25:04971 if (is_extension_process_) {
[email protected]71d6d852009-12-07 22:12:36972 // Dampen the forced delay as well if the extension stays idle for long
973 // periods of time.
974 int64 forced_delay_s =
975 std::max(static_cast<int64>(idle_notification_delay_in_s_),
976 kMaxExtensionIdleHandlerDelayS);
977 forced_idle_timer_.Stop();
978 forced_idle_timer_.Start(
979 base::TimeDelta::FromSeconds(forced_delay_s),
980 this, &RenderThread::IdleHandler);
981 }
982}
[email protected]05001182009-09-15 23:34:22983
[email protected]71d6d852009-12-07 22:12:36984void RenderThread::ScheduleIdleHandler(double initial_delay_s) {
985 idle_notification_delay_in_s_ = initial_delay_s;
986 idle_timer_.Stop();
987 idle_timer_.Start(
988 base::TimeDelta::FromSeconds(static_cast<int64>(initial_delay_s)),
989 this, &RenderThread::IdleHandler);
[email protected]bee16aab2009-08-26 15:55:03990}
991
[email protected]dfcb62a2009-06-17 19:32:43992void RenderThread::OnExtensionMessageInvoke(const std::string& function_name,
[email protected]d7259472010-03-24 08:40:49993 const ListValue& args,
[email protected]a807bbe2010-04-14 10:51:19994 bool requires_incognito_access,
995 const GURL& event_url) {
996 RendererExtensionBindings::Invoke(
997 function_name, args, NULL, requires_incognito_access, event_url);
[email protected]71d6d852009-12-07 22:12:36998
999 // Reset the idle handler each time there's any activity like event or message
1000 // dispatch, for which Invoke is the chokepoint.
[email protected]b8f41a192010-04-19 18:25:041001 if (is_extension_process_)
[email protected]71d6d852009-12-07 22:12:361002 ScheduleIdleHandler(kInitialExtensionIdleHandlerDelayS);
[email protected]b68d5ed2009-04-16 02:41:281003}
[email protected]b547fd42009-04-23 23:16:271004
[email protected]fede6ca12009-10-08 18:24:261005void RenderThread::OnPurgeMemory() {
[email protected]85c55dc2009-11-06 03:05:461006 spellchecker_.reset(new SpellCheck());
[email protected]85c55dc2009-11-06 03:05:461007
[email protected]fede6ca12009-10-08 18:24:261008 EnsureWebKitInitialized();
1009
1010 // Clear the object cache (as much as possible; some live objects cannot be
1011 // freed).
1012 WebCache::clear();
1013
1014 // Clear the font/glyph cache.
1015 WebFontCache::clear();
1016
1017 // Clear the Cross-Origin Preflight cache.
1018 WebCrossOriginPreflightResultCache::clear();
1019
[email protected]adf00bc2009-11-02 18:35:001020 // Release all freeable memory from the SQLite process-global page cache (a
1021 // low-level object which backs the Connection-specific page caches).
[email protected]4a3dab22009-11-11 17:36:501022 while (sqlite3_release_memory(std::numeric_limits<int>::max()) > 0) {
1023 }
[email protected]adf00bc2009-11-02 18:35:001024
[email protected]fede6ca12009-10-08 18:24:261025 // Repeatedly call the V8 idle notification until it returns true ("nothing
1026 // more to free"). Note that it makes more sense to do this than to implement
1027 // a new "delete everything" pass because object references make it difficult
1028 // to free everything possible in just one pass.
[email protected]4a3dab22009-11-11 17:36:501029 while (!v8::V8::IdleNotification()) {
1030 }
[email protected]fede6ca12009-10-08 18:24:261031
[email protected]61a9b2d82010-02-26 00:31:081032#if (defined(OS_WIN) || defined(OS_LINUX)) && defined(USE_TCMALLOC)
[email protected]fede6ca12009-10-08 18:24:261033 // Tell tcmalloc to release any free pages it's still holding.
1034 MallocExtension::instance()->ReleaseFreeMemory();
1035#endif
1036}
1037
[email protected]b78e168b2009-09-21 22:05:451038void RenderThread::OnPurgePluginListCache(bool reload_pages) {
[email protected]f5e4b9bf2009-10-08 00:59:591039 EnsureWebKitInitialized();
[email protected]b547fd42009-04-23 23:16:271040 // The call below will cause a GetPlugins call with refresh=true, but at this
1041 // point we already know that the browser has refreshed its list, so disable
1042 // refresh temporarily to prevent each renderer process causing the list to be
1043 // regenerated.
1044 plugin_refresh_allowed_ = false;
[email protected]b78e168b2009-09-21 22:05:451045 WebKit::resetPluginCache(reload_pages);
[email protected]b547fd42009-04-23 23:16:271046 plugin_refresh_allowed_ = true;
1047}
[email protected]85c55dc2009-11-06 03:05:461048
[email protected]85c55dc2009-11-06 03:05:461049void RenderThread::OnInitSpellChecker(
[email protected]cb6037d2009-11-16 22:55:171050 IPC::PlatformFileForTransit bdict_file,
[email protected]85c55dc2009-11-06 03:05:461051 const std::vector<std::string>& custom_words,
1052 const std::string& language,
1053 bool auto_spell_correct) {
[email protected]cb6037d2009-11-16 22:55:171054 spellchecker_->Init(IPC::PlatformFileForTransitToPlatformFile(bdict_file),
1055 custom_words, language);
[email protected]85c55dc2009-11-06 03:05:461056 spellchecker_->EnableAutoSpellCorrect(auto_spell_correct);
1057}
1058
1059void RenderThread::OnSpellCheckWordAdded(const std::string& word) {
1060 spellchecker_->WordAdded(word);
1061}
1062
1063void RenderThread::OnSpellCheckEnableAutoSpellCorrect(bool enable) {
1064 spellchecker_->EnableAutoSpellCorrect(enable);
1065}
[email protected]b2a74ca2010-03-12 17:57:091066
1067void RenderThread::OnSetIsIncognitoProcess(bool is_incognito_process) {
1068 is_incognito_process_ = is_incognito_process;
1069}
[email protected]6217d392010-03-25 22:08:351070
1071void RenderThread::OnGpuChannelEstablished(
1072 const IPC::ChannelHandle& channel_handle) {
1073#if defined(OS_POSIX)
1074 // If we received a ChannelHandle, register it now.
1075 if (channel_handle.socket.fd >= 0)
1076 IPC::AddChannelSocket(channel_handle.name, channel_handle.socket.fd);
1077#endif
1078
1079 if (channel_handle.name.size() != 0) {
1080 // Connect to the GPU process if a channel name was received.
1081 gpu_channel_->Connect(channel_handle.name);
1082 } else {
1083 // Otherwise cancel the connection.
1084 gpu_channel_ = NULL;
1085 }
1086}
[email protected]3a8eecb2010-04-22 23:56:301087
[email protected]4fdbc1492010-07-01 01:20:591088std::string RenderThread::GetExtensionIdByURL(const GURL& url) {
[email protected]3a8eecb2010-04-22 23:56:301089 if (url.SchemeIs(chrome::kExtensionScheme))
1090 return url.host();
1091
1092 for (size_t i = 0; i < extension_extents_.size(); ++i) {
1093 if (extension_extents_[i].web_extent.ContainsURL(url))
1094 return extension_extents_[i].extension_id;
1095 }
1096
1097 return std::string();
1098}
[email protected]4fdbc1492010-07-01 01:20:591099
1100std::string RenderThread::GetExtensionIdByBrowseExtent(const GURL& url) {
1101 for (size_t i = 0; i < extension_extents_.size(); ++i) {
1102 if (extension_extents_[i].browse_extent.ContainsURL(url))
1103 return extension_extents_[i].extension_id;
1104 }
1105
1106 return std::string();
1107}
[email protected]c6a7b862010-08-20 22:19:381108
1109scoped_refptr<base::MessageLoopProxy>
1110RenderThread::GetFileThreadMessageLoopProxy() {
1111 DCHECK(message_loop() == MessageLoop::current());
1112 if (!file_thread_.get()) {
1113 file_thread_.reset(new base::Thread("Renderer::FILE"));
1114 file_thread_->Start();
1115 }
1116 return file_thread_->message_loop_proxy();
1117}