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