[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 1 | // Copyright (c) 2009 The Chromium Authors. All rights reserved. |
license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 4 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 5 | #include "chrome/renderer/render_thread.h" |
| 6 | |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 7 | #include <algorithm> |
[email protected] | 75e126b93 | 2009-09-28 19:38:49 | [diff] [blame] | 8 | #include <map> |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 9 | #include <vector> |
| 10 | |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 11 | #include "base/command_line.h" |
[email protected] | 94f9a0f68 | 2009-06-15 18:30:30 | [diff] [blame] | 12 | #include "base/lazy_instance.h" |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 13 | #include "base/logging.h" |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 14 | #include "base/nullable_string16.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 15 | #include "base/shared_memory.h" |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 16 | #include "base/stats_table.h" |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 17 | #include "base/string_util.h" |
[email protected] | 94f9a0f68 | 2009-06-15 18:30:30 | [diff] [blame] | 18 | #include "base/thread_local.h" |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame] | 19 | #include "chrome/common/appcache/appcache_dispatcher.h" |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 20 | #include "chrome/common/chrome_switches.h" |
[email protected] | 017022b | 2009-07-27 23:06:34 | [diff] [blame] | 21 | #include "chrome/common/db_message_filter.h" |
[email protected] | e09ba55 | 2009-02-05 03:26:29 | [diff] [blame] | 22 | #include "chrome/common/render_messages.h" |
[email protected] | 9b6f40e | 2009-06-11 15:54:26 | [diff] [blame] | 23 | #include "chrome/common/renderer_preferences.h" |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 24 | #include "chrome/common/url_constants.h" |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 25 | #include "chrome/plugin/npobject_util.h" |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 26 | // TODO(port) |
| 27 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 28 | #include "chrome/plugin/plugin_channel.h" |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 29 | #else |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 30 | #include "base/scoped_handle.h" |
| 31 | #include "chrome/plugin/plugin_channel_base.h" |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 32 | #endif |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame] | 33 | #include "chrome/renderer/devtools_agent_filter.h" |
[email protected] | ad1f9bd | 2009-07-30 20:23:15 | [diff] [blame] | 34 | #include "chrome/renderer/extension_groups.h" |
[email protected] | a40caa97 | 2009-04-08 18:35:34 | [diff] [blame] | 35 | #include "chrome/renderer/extensions/event_bindings.h" |
[email protected] | 309d7a28 | 2009-03-24 09:18:27 | [diff] [blame] | 36 | #include "chrome/renderer/extensions/extension_process_bindings.h" |
[email protected] | be77f0a | 2009-08-25 08:31:17 | [diff] [blame] | 37 | #include "chrome/renderer/extensions/js_only_v8_extensions.h" |
[email protected] | 0aa477bd | 2009-03-23 22:21:43 | [diff] [blame] | 38 | #include "chrome/renderer/extensions/renderer_extension_bindings.h" |
[email protected] | 3c8e370 | 2009-05-01 16:27:42 | [diff] [blame] | 39 | #include "chrome/renderer/external_extension.h" |
[email protected] | 0bc4655 | 2009-04-07 21:56:42 | [diff] [blame] | 40 | #include "chrome/renderer/loadtimes_extension_bindings.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 41 | #include "chrome/renderer/net/render_dns_master.h" |
| 42 | #include "chrome/renderer/render_process.h" |
| 43 | #include "chrome/renderer/render_view.h" |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 44 | #include "chrome/renderer/renderer_webkitclient_impl.h" |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 45 | #include "chrome/renderer/user_script_slave.h" |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 46 | #include "ipc/ipc_message.h" |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 47 | #include "webkit/api/public/WebCache.h" |
[email protected] | f5e4b9bf | 2009-10-08 00:59:59 | [diff] [blame^] | 48 | #include "webkit/api/public/WebColor.h" |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 49 | #include "webkit/api/public/WebKit.h" |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 50 | #include "webkit/api/public/WebStorageEventDispatcher.h" |
[email protected] | afdcf5c | 2009-05-10 20:30:41 | [diff] [blame] | 51 | #include "webkit/api/public/WebString.h" |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 52 | #include "webkit/extensions/v8/benchmarking_extension.h" |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 53 | #include "webkit/extensions/v8/gears_extension.h" |
| 54 | #include "webkit/extensions/v8/interval_extension.h" |
| 55 | #include "webkit/extensions/v8/playback_extension.h" |
[email protected] | e94afbb9 | 2009-10-01 00:25:41 | [diff] [blame] | 56 | #include "third_party/tcmalloc/tcmalloc/src/google/malloc_extension.h" |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 57 | |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 58 | #if defined(OS_WIN) |
| 59 | #include <windows.h> |
| 60 | #include <objbase.h> |
| 61 | #endif |
| 62 | |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 63 | using WebKit::WebCache; |
| 64 | using WebKit::WebString; |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 65 | using WebKit::WebStorageEventDispatcher; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 66 | |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 67 | namespace { |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 68 | static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */; |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 69 | static const double kInitialIdleHandlerDelayS = 1.0 /* seconds */; |
| 70 | |
[email protected] | 94f9a0f68 | 2009-06-15 18:30:30 | [diff] [blame] | 71 | static base::LazyInstance<base::ThreadLocalPointer<RenderThread> > lazy_tls( |
| 72 | base::LINKER_INITIALIZED); |
[email protected] | 1edc16b8 | 2009-04-07 17:45:54 | [diff] [blame] | 73 | |
[email protected] | 5fa1c54 | 2009-05-05 20:36:07 | [diff] [blame] | 74 | #if defined(OS_POSIX) |
| 75 | class SuicideOnChannelErrorFilter : public IPC::ChannelProxy::MessageFilter { |
| 76 | void OnChannelError() { |
| 77 | // On POSIX, at least, one can install an unload handler which loops |
| 78 | // forever and leave behind a renderer process which eats 100% CPU forever. |
| 79 | // |
| 80 | // This is because the terminate signals (ViewMsg_ShouldClose and the error |
| 81 | // from the IPC channel) are routed to the main message loop but never |
| 82 | // processed (because that message loop is stuck in V8). |
| 83 | // |
| 84 | // One could make the browser SIGKILL the renderers, but that leaves open a |
| 85 | // large window where a browser failure (or a user, manually terminating |
| 86 | // the browser because "it's stuck") will leave behind a process eating all |
| 87 | // the CPU. |
| 88 | // |
| 89 | // So, we install a filter on the channel so that we can process this event |
| 90 | // here and kill the process. |
| 91 | _exit(0); |
| 92 | } |
| 93 | }; |
| 94 | #endif |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 95 | } // namespace |
| 96 | |
| 97 | // When we run plugins in process, we actually run them on the render thread, |
| 98 | // which means that we need to make the render thread pump UI events. |
| 99 | RenderThread::RenderThread() { |
| 100 | Init(); |
| 101 | } |
| 102 | |
| 103 | RenderThread::RenderThread(const std::string& channel_name) |
| 104 | : ChildThread(channel_name) { |
| 105 | Init(); |
| 106 | } |
[email protected] | 5fa1c54 | 2009-05-05 20:36:07 | [diff] [blame] | 107 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 108 | void RenderThread::Init() { |
[email protected] | 94f9a0f68 | 2009-06-15 18:30:30 | [diff] [blame] | 109 | lazy_tls.Pointer()->Set(this); |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 110 | #if defined(OS_WIN) |
[email protected] | bdef78b5 | 2009-04-16 19:31:34 | [diff] [blame] | 111 | // If you are running plugins in this thread you need COM active but in |
| 112 | // the normal case you don't. |
| 113 | if (RenderProcess::InProcessPlugins()) |
| 114 | CoInitialize(0); |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 115 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 116 | |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 117 | plugin_refresh_allowed_ = true; |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 118 | cache_stats_task_pending_ = false; |
| 119 | widget_count_ = 0; |
| 120 | hidden_widget_count_ = 0; |
| 121 | idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS; |
| 122 | task_factory_.reset(new ScopedRunnableMethodFactory<RenderThread>(this)); |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 123 | |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 124 | visited_link_slave_.reset(new VisitedLinkSlave()); |
| 125 | user_script_slave_.reset(new UserScriptSlave()); |
| 126 | dns_master_.reset(new RenderDnsMaster()); |
| 127 | histogram_snapshots_.reset(new RendererHistogramSnapshots()); |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame] | 128 | appcache_dispatcher_.reset(new AppCacheDispatcher(this)); |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame] | 129 | devtools_agent_filter_ = new DevToolsAgentFilter(); |
| 130 | AddFilter(devtools_agent_filter_.get()); |
[email protected] | 017022b | 2009-07-27 23:06:34 | [diff] [blame] | 131 | db_message_filter_ = new DBMessageFilter(); |
| 132 | AddFilter(db_message_filter_.get()); |
[email protected] | 5fa1c54 | 2009-05-05 20:36:07 | [diff] [blame] | 133 | |
| 134 | #if defined(OS_POSIX) |
| 135 | suicide_on_channel_error_filter_ = new SuicideOnChannelErrorFilter; |
| 136 | AddFilter(suicide_on_channel_error_filter_.get()); |
| 137 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 138 | } |
| 139 | |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 140 | RenderThread::~RenderThread() { |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 141 | // Shutdown in reverse of the initialization order. |
[email protected] | a862471 | 2009-04-17 00:51:35 | [diff] [blame] | 142 | RemoveFilter(devtools_agent_filter_.get()); |
[email protected] | 017022b | 2009-07-27 23:06:34 | [diff] [blame] | 143 | RemoveFilter(db_message_filter_.get()); |
| 144 | db_message_filter_ = NULL; |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 145 | if (webkit_client_.get()) |
[email protected] | 9291ed1 | 2009-07-23 17:33:22 | [diff] [blame] | 146 | WebKit::shutdown(); |
[email protected] | 9291ed1 | 2009-07-23 17:33:22 | [diff] [blame] | 147 | |
[email protected] | 94f9a0f68 | 2009-06-15 18:30:30 | [diff] [blame] | 148 | lazy_tls.Pointer()->Set(NULL); |
[email protected] | 8fd8de9 | 2008-08-12 23:50:30 | [diff] [blame] | 149 | |
[email protected] | 8d86fce | 2009-02-26 23:37:55 | [diff] [blame] | 150 | // TODO(port) |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 151 | #if defined(OS_WIN) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 152 | // Clean up plugin channels before this thread goes away. |
| 153 | PluginChannelBase::CleanupChannels(); |
[email protected] | bdef78b5 | 2009-04-16 19:31:34 | [diff] [blame] | 154 | // Don't call COM if the renderer is in the sandbox. |
| 155 | if (RenderProcess::InProcessPlugins()) |
| 156 | CoUninitialize(); |
[email protected] | 2c62b56 | 2009-01-27 19:04:50 | [diff] [blame] | 157 | #endif |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 158 | } |
| 159 | |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 160 | RenderThread* RenderThread::current() { |
| 161 | return lazy_tls.Pointer()->Get(); |
| 162 | } |
| 163 | |
| 164 | void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 165 | channel()->AddFilter(filter); |
| 166 | } |
| 167 | |
| 168 | void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 169 | channel()->RemoveFilter(filter); |
| 170 | } |
| 171 | |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 172 | void RenderThread::WidgetHidden() { |
| 173 | DCHECK(hidden_widget_count_ < widget_count_); |
| 174 | hidden_widget_count_++ ; |
| 175 | if (widget_count_ && hidden_widget_count_ == widget_count_) { |
| 176 | // Reset the delay. |
| 177 | idle_notification_delay_in_s_ = kInitialIdleHandlerDelayS; |
| 178 | |
| 179 | // Schedule the IdleHandler to wakeup in a bit. |
| 180 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 181 | task_factory_->NewRunnableMethod(&RenderThread::IdleHandler), |
| 182 | static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000); |
| 183 | } |
| 184 | } |
| 185 | |
| 186 | void RenderThread::WidgetRestored() { |
| 187 | DCHECK(hidden_widget_count_ > 0); |
| 188 | hidden_widget_count_--; |
| 189 | |
| 190 | // Note: we may have a timer pending to call the IdleHandler (see the |
| 191 | // WidgetHidden() code). But we don't bother to cancel it as it is |
| 192 | // benign and won't do anything if the tab is un-hidden when it is |
| 193 | // called. |
| 194 | } |
| 195 | |
[email protected] | 42f1d782 | 2009-07-23 18:17:55 | [diff] [blame] | 196 | void RenderThread::Resolve(const char* name, size_t length) { |
| 197 | return dns_master_->Resolve(name, length); |
| 198 | } |
| 199 | |
| 200 | void RenderThread::SendHistograms(int sequence_number) { |
| 201 | return histogram_snapshots_->SendHistograms(sequence_number); |
| 202 | } |
| 203 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 204 | void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) { |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 205 | DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle"; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 206 | visited_link_slave_->Init(table); |
| 207 | } |
| 208 | |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 209 | void RenderThread::OnAddVisitedLinks( |
| 210 | const VisitedLinkSlave::Fingerprints& fingerprints) { |
| 211 | for (size_t i = 0; i < fingerprints.size(); ++i) |
| 212 | WebView::UpdateVisitedLinkState(fingerprints[i]); |
| 213 | } |
| 214 | |
| 215 | void RenderThread::OnResetVisitedLinks() { |
| 216 | WebView::ResetVisitedLinkState(); |
| 217 | } |
| 218 | |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 219 | void RenderThread::OnUpdateUserScripts( |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 220 | base::SharedMemoryHandle scripts) { |
[email protected] | 5fe733de | 2009-02-11 18:59:20 | [diff] [blame] | 221 | DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle"; |
[email protected] | 0938d3c | 2009-01-09 20:37:35 | [diff] [blame] | 222 | user_script_slave_->UpdateScripts(scripts); |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 223 | } |
| 224 | |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 225 | void RenderThread::OnSetExtensionFunctionNames( |
| 226 | const std::vector<std::string>& names) { |
[email protected] | a1a0df0 | 2009-04-09 08:18:04 | [diff] [blame] | 227 | ExtensionProcessBindings::SetFunctionNames(names); |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 228 | } |
| 229 | |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 230 | void RenderThread::OnPageActionsUpdated( |
| 231 | const std::string& extension_id, |
| 232 | const std::vector<std::string>& page_actions) { |
| 233 | ExtensionProcessBindings::SetPageActions(extension_id, page_actions); |
| 234 | } |
| 235 | |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 236 | void RenderThread::OnExtensionSetAPIPermissions( |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 237 | const std::string& extension_id, |
| 238 | const std::vector<std::string>& permissions) { |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 239 | ExtensionProcessBindings::SetAPIPermissions(extension_id, permissions); |
| 240 | } |
| 241 | |
| 242 | void RenderThread::OnExtensionSetHostPermissions( |
| 243 | const GURL& extension_url, const std::vector<URLPattern>& permissions) { |
| 244 | ExtensionProcessBindings::SetHostPermissions(extension_url, permissions); |
[email protected] | 3550635 | 2009-08-07 18:58:19 | [diff] [blame] | 245 | } |
| 246 | |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 247 | void RenderThread::OnDOMStorageEvent(const string16& key, |
| 248 | const NullableString16& old_value, const NullableString16& new_value, |
| 249 | const string16& origin, DOMStorageType dom_storage_type) { |
| 250 | if (!dom_storage_event_dispatcher_.get()) { |
| 251 | dom_storage_event_dispatcher_.reset(WebStorageEventDispatcher::create()); |
| 252 | } |
| 253 | dom_storage_event_dispatcher_->dispatchStorageEvent(key, old_value, new_value, |
| 254 | origin, dom_storage_type == DOM_STORAGE_LOCAL); |
| 255 | } |
| 256 | |
[email protected] | 75e126b93 | 2009-09-28 19:38:49 | [diff] [blame] | 257 | void RenderThread::OnExtensionSetL10nMessages( |
| 258 | const std::string& extension_id, |
| 259 | const std::map<std::string, std::string>& l10n_messages) { |
| 260 | ExtensionProcessBindings::SetL10nMessages(extension_id, l10n_messages); |
| 261 | } |
| 262 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 263 | void RenderThread::OnControlMessageReceived(const IPC::Message& msg) { |
[email protected] | 1edc16b8 | 2009-04-07 17:45:54 | [diff] [blame] | 264 | // App cache messages are handled by a delegate. |
[email protected] | f430b571 | 2009-08-21 21:46:31 | [diff] [blame] | 265 | if (appcache_dispatcher_->OnMessageReceived(msg)) |
[email protected] | 1edc16b8 | 2009-04-07 17:45:54 | [diff] [blame] | 266 | return; |
| 267 | |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 268 | IPC_BEGIN_MESSAGE_MAP(RenderThread, msg) |
| 269 | IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks) |
[email protected] | 3e90d4a | 2009-07-03 17:38:39 | [diff] [blame] | 270 | IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Add, OnAddVisitedLinks) |
| 271 | IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_Reset, OnResetVisitedLinks) |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 272 | IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID) |
[email protected] | b9ab10c | 2009-08-07 18:09:55 | [diff] [blame] | 273 | IPC_MESSAGE_HANDLER(ViewMsg_SetCSSColors, OnSetCSSColors) |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 274 | // TODO(port): removed from render_messages_internal.h; |
| 275 | // is there a new non-windows message I should add here? |
| 276 | IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView) |
| 277 | IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities) |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 278 | IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms, |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 279 | OnGetRendererHistograms) |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 280 | IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats, |
| 281 | OnGetCacheResourceStats) |
[email protected] | 2f2243e | 2009-05-26 02:27:02 | [diff] [blame] | 282 | IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_UpdatedScripts, |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 283 | OnUpdateUserScripts) |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 284 | // TODO(rafaelw): create an ExtensionDispatcher that handles extension |
| 285 | // messages seperates their handling from the RenderThread. |
[email protected] | dfcb62a | 2009-06-17 19:32:43 | [diff] [blame] | 286 | IPC_MESSAGE_HANDLER(ViewMsg_ExtensionMessageInvoke, |
| 287 | OnExtensionMessageInvoke) |
[email protected] | 703e807a | 2009-03-28 19:56:51 | [diff] [blame] | 288 | IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetFunctionNames, |
| 289 | OnSetExtensionFunctionNames) |
[email protected] | b547fd4 | 2009-04-23 23:16:27 | [diff] [blame] | 290 | IPC_MESSAGE_HANDLER(ViewMsg_PurgePluginListCache, |
| 291 | OnPurgePluginListCache) |
[email protected] | 4577622 | 2009-07-15 20:21:58 | [diff] [blame] | 292 | IPC_MESSAGE_HANDLER(ViewMsg_Extension_UpdatePageActions, |
| 293 | OnPageActionsUpdated) |
[email protected] | cccf9093 | 2009-08-23 17:56:25 | [diff] [blame] | 294 | IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetAPIPermissions, |
| 295 | OnExtensionSetAPIPermissions) |
| 296 | IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetHostPermissions, |
| 297 | OnExtensionSetHostPermissions) |
[email protected] | b7c7bcf | 2009-10-03 07:07:34 | [diff] [blame] | 298 | IPC_MESSAGE_HANDLER(ViewMsg_DOMStorageEvent, |
| 299 | OnDOMStorageEvent) |
[email protected] | 75e126b93 | 2009-09-28 19:38:49 | [diff] [blame] | 300 | IPC_MESSAGE_HANDLER(ViewMsg_Extension_SetL10nMessages, |
| 301 | OnExtensionSetL10nMessages) |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 302 | #if defined(IPC_MESSAGE_LOG_ENABLED) |
[email protected] | 12636df | 2009-09-28 22:32:21 | [diff] [blame] | 303 | IPC_MESSAGE_HANDLER(ViewMsg_SetIPCLoggingEnabled, |
[email protected] | d55aaa13 | 2009-09-28 21:08:04 | [diff] [blame] | 304 | OnSetIPCLoggingEnabled) |
| 305 | #endif |
[email protected] | 8930d47 | 2009-02-21 08:05:28 | [diff] [blame] | 306 | IPC_END_MESSAGE_MAP() |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 307 | } |
| 308 | |
| 309 | void RenderThread::OnSetNextPageID(int32 next_page_id) { |
| 310 | // This should only be called at process initialization time, so we shouldn't |
| 311 | // have to worry about thread-safety. |
| 312 | RenderView::SetNextPageID(next_page_id); |
| 313 | } |
| 314 | |
[email protected] | b9ab10c | 2009-08-07 18:09:55 | [diff] [blame] | 315 | // Called when to register CSS Color name->system color mappings. |
| 316 | // We update the colors one by one and then tell WebKit to refresh all render |
| 317 | // views. |
| 318 | void RenderThread::OnSetCSSColors( |
| 319 | const std::vector<CSSColors::CSSColorMapping>& colors) { |
[email protected] | f5e4b9bf | 2009-10-08 00:59:59 | [diff] [blame^] | 320 | EnsureWebKitInitialized(); |
[email protected] | b9ab10c | 2009-08-07 18:09:55 | [diff] [blame] | 321 | size_t num_colors = colors.size(); |
| 322 | scoped_array<WebKit::WebColorName> color_names( |
| 323 | new WebKit::WebColorName[num_colors]); |
| 324 | scoped_array<WebKit::WebColor> web_colors(new WebKit::WebColor[num_colors]); |
| 325 | size_t i = 0; |
| 326 | for (std::vector<CSSColors::CSSColorMapping>::const_iterator it = |
| 327 | colors.begin(); |
| 328 | it != colors.end(); |
| 329 | ++it, ++i) { |
| 330 | color_names[i] = it->first; |
| 331 | web_colors[i] = it->second; |
| 332 | } |
| 333 | WebKit::setNamedColors(color_names.get(), web_colors.get(), num_colors); |
| 334 | } |
| 335 | |
[email protected] | 18bcc3c | 2009-01-27 21:39:15 | [diff] [blame] | 336 | void RenderThread::OnCreateNewView(gfx::NativeViewId parent_hwnd, |
[email protected] | 80d96fa | 2009-06-10 22:34:51 | [diff] [blame] | 337 | const RendererPreferences& renderer_prefs, |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 338 | const WebPreferences& webkit_prefs, |
| 339 | int32 view_id) { |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 340 | EnsureWebKitInitialized(); |
[email protected] | be645db | 2009-02-06 20:36:33 | [diff] [blame] | 341 | // When bringing in render_view, also bring in webkit's glue and jsbindings. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 342 | RenderView::Create( |
[email protected] | 12636df | 2009-09-28 22:32:21 | [diff] [blame] | 343 | this, parent_hwnd, MSG_ROUTING_NONE, renderer_prefs, |
[email protected] | 80d96fa | 2009-06-10 22:34:51 | [diff] [blame] | 344 | webkit_prefs, new SharedRenderViewCounter(0), view_id); |
[email protected] | 7f874dec | 2009-02-06 01:48:27 | [diff] [blame] | 345 | } |
[email protected] | 4274e58 | 2009-01-27 22:09:56 | [diff] [blame] | 346 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 347 | void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity, |
| 348 | size_t max_dead_capacity, |
| 349 | size_t capacity) { |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 350 | EnsureWebKitInitialized(); |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 351 | WebCache::setCapacities( |
| 352 | min_dead_capacity, max_dead_capacity, capacity); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 353 | } |
| 354 | |
| 355 | void RenderThread::OnGetCacheResourceStats() { |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 356 | EnsureWebKitInitialized(); |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 357 | WebCache::ResourceTypeStats stats; |
| 358 | WebCache::getResourceTypeStats(&stats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 359 | Send(new ViewHostMsg_ResourceTypeStats(stats)); |
| 360 | } |
| 361 | |
[email protected] | c9a3ef8 | 2009-05-28 22:02:46 | [diff] [blame] | 362 | void RenderThread::OnGetRendererHistograms(int sequence_number) { |
| 363 | SendHistograms(sequence_number); |
[email protected] | 55e57d4 | 2009-02-25 06:10:17 | [diff] [blame] | 364 | } |
| 365 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 366 | void RenderThread::InformHostOfCacheStats() { |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 367 | EnsureWebKitInitialized(); |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 368 | WebCache::UsageStats stats; |
| 369 | WebCache::getUsageStats(&stats); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 370 | Send(new ViewHostMsg_UpdatedCacheStats(stats)); |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 371 | cache_stats_task_pending_ = false; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 372 | } |
| 373 | |
| 374 | void RenderThread::InformHostOfCacheStatsLater() { |
| 375 | // Rate limit informing the host of our cache stats. |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 376 | if (cache_stats_task_pending_) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 377 | return; |
| 378 | |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 379 | cache_stats_task_pending_ = true; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 380 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 381 | task_factory_->NewRunnableMethod( |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 382 | &RenderThread::InformHostOfCacheStats), |
| 383 | kCacheStatsDelayMS); |
| 384 | } |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 385 | |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 386 | void RenderThread::CloseIdleConnections() { |
| 387 | Send(new ViewHostMsg_CloseIdleConnections()); |
| 388 | } |
| 389 | |
| 390 | void RenderThread::SetCacheMode(bool enabled) { |
| 391 | Send(new ViewHostMsg_SetCacheMode(enabled)); |
| 392 | } |
| 393 | |
[email protected] | 3df0c20 | 2009-03-31 23:51:26 | [diff] [blame] | 394 | static void* CreateHistogram( |
| 395 | const char *name, int min, int max, size_t buckets) { |
[email protected] | 56a27b2 | 2009-05-21 00:44:37 | [diff] [blame] | 396 | Histogram* histogram = new Histogram(name, min, max, buckets); |
| 397 | if (histogram) { |
| 398 | histogram->SetFlags(kUmaTargetedHistogramFlag); |
| 399 | } |
| 400 | return histogram; |
[email protected] | 3df0c20 | 2009-03-31 23:51:26 | [diff] [blame] | 401 | } |
| 402 | |
| 403 | static void AddHistogramSample(void* hist, int sample) { |
| 404 | Histogram* histogram = static_cast<Histogram *>(hist); |
| 405 | histogram->Add(sample); |
| 406 | } |
| 407 | |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 408 | void RenderThread::EnsureWebKitInitialized() { |
| 409 | if (webkit_client_.get()) |
| 410 | return; |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 411 | |
| 412 | v8::V8::SetCounterFunction(StatsTable::FindLocation); |
[email protected] | 3df0c20 | 2009-03-31 23:51:26 | [diff] [blame] | 413 | v8::V8::SetCreateHistogramFunction(CreateHistogram); |
| 414 | v8::V8::SetAddHistogramSampleFunction(AddHistogramSample); |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 415 | |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 416 | webkit_client_.reset(new RendererWebKitClientImpl); |
| 417 | WebKit::initialize(webkit_client_.get()); |
[email protected] | 8881eca8 | 2009-03-12 18:20:44 | [diff] [blame] | 418 | |
[email protected] | 4ea00e8 | 2009-07-30 22:59:34 | [diff] [blame] | 419 | WebKit::enableV8SingleThreadMode(); |
| 420 | |
[email protected] | 60e44898 | 2009-05-06 04:21:16 | [diff] [blame] | 421 | // chrome: pages should not be accessible by normal content, and should |
[email protected] | 8881eca8 | 2009-03-12 18:20:44 | [diff] [blame] | 422 | // also be unable to script anything but themselves (to help limit the damage |
[email protected] | 60e44898 | 2009-05-06 04:21:16 | [diff] [blame] | 423 | // that a corrupt chrome: page could cause). |
[email protected] | 2c434b3 | 2009-03-19 06:27:47 | [diff] [blame] | 424 | WebString chrome_ui_scheme(ASCIIToUTF16(chrome::kChromeUIScheme)); |
[email protected] | 8881eca8 | 2009-03-12 18:20:44 | [diff] [blame] | 425 | WebKit::registerURLSchemeAsLocal(chrome_ui_scheme); |
| 426 | WebKit::registerURLSchemeAsNoAccess(chrome_ui_scheme); |
[email protected] | da00a288 | 2009-03-09 17:51:19 | [diff] [blame] | 427 | |
[email protected] | d53d849 | 2009-07-14 01:51:32 | [diff] [blame] | 428 | // print: pages should be not accessible by normal context. |
| 429 | WebString print_ui_scheme(ASCIIToUTF16(chrome::kPrintScheme)); |
| 430 | WebKit::registerURLSchemeAsLocal(print_ui_scheme); |
| 431 | WebKit::registerURLSchemeAsNoAccess(print_ui_scheme); |
| 432 | |
[email protected] | 6cf8b1a2 | 2009-05-26 23:36:00 | [diff] [blame] | 433 | #if defined(OS_WIN) |
| 434 | // We don't yet support Gears on non-Windows, so don't tell pages that we do. |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 435 | WebKit::registerExtension(extensions_v8::GearsExtension::Get()); |
[email protected] | 6cf8b1a2 | 2009-05-26 23:36:00 | [diff] [blame] | 436 | #endif |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 437 | WebKit::registerExtension(extensions_v8::IntervalExtension::Get()); |
[email protected] | c20210e6 | 2009-04-03 21:39:26 | [diff] [blame] | 438 | WebKit::registerExtension(extensions_v8::LoadTimesExtension::Get()); |
[email protected] | 3c8e370 | 2009-05-01 16:27:42 | [diff] [blame] | 439 | WebKit::registerExtension(extensions_v8::ExternalExtension::Get()); |
[email protected] | 309d7a28 | 2009-03-24 09:18:27 | [diff] [blame] | 440 | |
[email protected] | ad1f9bd | 2009-07-30 20:23:15 | [diff] [blame] | 441 | const WebKit::WebString kExtensionScheme = |
| 442 | WebKit::WebString::fromUTF8(chrome::kExtensionScheme); |
| 443 | |
| 444 | WebKit::registerExtension(ExtensionProcessBindings::Get(), kExtensionScheme); |
| 445 | |
| 446 | WebKit::registerExtension(BaseJsV8Extension::Get(), |
| 447 | EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 448 | WebKit::registerExtension(BaseJsV8Extension::Get(), kExtensionScheme); |
| 449 | WebKit::registerExtension(JsonSchemaJsV8Extension::Get(), |
| 450 | EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 451 | WebKit::registerExtension(JsonSchemaJsV8Extension::Get(), kExtensionScheme); |
| 452 | WebKit::registerExtension(EventBindings::Get(), |
| 453 | EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 454 | WebKit::registerExtension(EventBindings::Get(), kExtensionScheme); |
| 455 | WebKit::registerExtension(RendererExtensionBindings::Get(), |
| 456 | EXTENSION_GROUP_CONTENT_SCRIPTS); |
| 457 | WebKit::registerExtension(RendererExtensionBindings::Get(), kExtensionScheme); |
[email protected] | 5c1cea3f | 2009-08-31 17:15:47 | [diff] [blame] | 458 | WebKit::registerExtension(ExtensionApiTestV8Extension::Get(), |
| 459 | kExtensionScheme); |
| 460 | WebKit::registerExtension(ExtensionApiTestV8Extension::Get(), |
| 461 | EXTENSION_GROUP_CONTENT_SCRIPTS); |
[email protected] | a1a0df0 | 2009-04-09 08:18:04 | [diff] [blame] | 462 | |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 463 | const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
[email protected] | a1a0df0 | 2009-04-09 08:18:04 | [diff] [blame] | 464 | |
[email protected] | b07f2909 | 2009-06-05 07:33:21 | [diff] [blame] | 465 | if (command_line.HasSwitch(switches::kEnableBenchmarking)) |
| 466 | WebKit::registerExtension(extensions_v8::BenchmarkingExtension::Get()); |
| 467 | |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 468 | if (command_line.HasSwitch(switches::kPlaybackMode) || |
[email protected] | ca12c84 | 2009-04-14 22:20:18 | [diff] [blame] | 469 | command_line.HasSwitch(switches::kRecordMode) || |
| 470 | command_line.HasSwitch(switches::kNoJsRandomness)) { |
[email protected] | 06533c0b | 2009-03-05 21:39:11 | [diff] [blame] | 471 | WebKit::registerExtension(extensions_v8::PlaybackExtension::Get()); |
| 472 | } |
[email protected] | 2cb8233 | 2009-03-18 17:24:55 | [diff] [blame] | 473 | |
[email protected] | b08eebe | 2009-05-18 22:37:21 | [diff] [blame] | 474 | if (RenderProcess::current()->initialized_media_library()) |
| 475 | WebKit::enableMediaPlayer(); |
[email protected] | 90a3fbb1 | 2009-02-28 01:13:47 | [diff] [blame] | 476 | } |
[email protected] | 75e5a87 | 2009-04-02 23:56:11 | [diff] [blame] | 477 | |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 478 | void RenderThread::IdleHandler() { |
| 479 | // It is possible that the timer was set while the widgets were idle, |
| 480 | // but that they are no longer idle. If so, just return. |
| 481 | if (!widget_count_ || hidden_widget_count_ < widget_count_) |
| 482 | return; |
| 483 | |
[email protected] | 0500118 | 2009-09-15 23:34:22 | [diff] [blame] | 484 | #if defined(OS_WIN) |
[email protected] | e94afbb9 | 2009-10-01 00:25:41 | [diff] [blame] | 485 | MallocExtension::instance()->ReleaseFreeMemory(); |
[email protected] | 0500118 | 2009-09-15 23:34:22 | [diff] [blame] | 486 | #endif |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 487 | |
[email protected] | f5e4b9bf | 2009-10-08 00:59:59 | [diff] [blame^] | 488 | LOG(INFO) << "RenderThread calling v8 IdleNotification for " << this; |
| 489 | v8::V8::IdleNotification(); |
[email protected] | 0500118 | 2009-09-15 23:34:22 | [diff] [blame] | 490 | |
| 491 | // Schedule next invocation. |
| 492 | // Dampen the delay using the algorithm: |
| 493 | // delay = delay + 1 / (delay + 2) |
| 494 | // Using floor(delay) has a dampening effect such as: |
| 495 | // 1s, 1, 1, 2, 2, 2, 2, 3, 3, ... |
| 496 | // Note that idle_notification_delay_in_s_ would be reset to |
| 497 | // kInitialIdleHandlerDelayS in RenderThread::WidgetHidden. |
| 498 | idle_notification_delay_in_s_ += |
| 499 | 1.0 / (idle_notification_delay_in_s_ + 2.0); |
| 500 | |
| 501 | // Schedule the next timer. |
| 502 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 503 | task_factory_->NewRunnableMethod(&RenderThread::IdleHandler), |
| 504 | static_cast<int64>(floor(idle_notification_delay_in_s_)) * 1000); |
[email protected] | bee16aab | 2009-08-26 15:55:03 | [diff] [blame] | 505 | } |
| 506 | |
[email protected] | dfcb62a | 2009-06-17 19:32:43 | [diff] [blame] | 507 | void RenderThread::OnExtensionMessageInvoke(const std::string& function_name, |
| 508 | const ListValue& args) { |
[email protected] | 7120f13 | 2009-07-20 21:05:37 | [diff] [blame] | 509 | RendererExtensionBindings::Invoke(function_name, args, NULL); |
[email protected] | b68d5ed | 2009-04-16 02:41:28 | [diff] [blame] | 510 | } |
[email protected] | b547fd4 | 2009-04-23 23:16:27 | [diff] [blame] | 511 | |
[email protected] | b78e168b | 2009-09-21 22:05:45 | [diff] [blame] | 512 | void RenderThread::OnPurgePluginListCache(bool reload_pages) { |
[email protected] | f5e4b9bf | 2009-10-08 00:59:59 | [diff] [blame^] | 513 | EnsureWebKitInitialized(); |
[email protected] | b547fd4 | 2009-04-23 23:16:27 | [diff] [blame] | 514 | // The call below will cause a GetPlugins call with refresh=true, but at this |
| 515 | // point we already know that the browser has refreshed its list, so disable |
| 516 | // refresh temporarily to prevent each renderer process causing the list to be |
| 517 | // regenerated. |
| 518 | plugin_refresh_allowed_ = false; |
[email protected] | b78e168b | 2009-09-21 22:05:45 | [diff] [blame] | 519 | WebKit::resetPluginCache(reload_pages); |
[email protected] | b547fd4 | 2009-04-23 23:16:27 | [diff] [blame] | 520 | plugin_refresh_allowed_ = true; |
| 521 | } |