license.bot | bf09a50 | 2008-08-24 00:55:55 | [diff] [blame] | 1 | // Copyright (c) 2006-2008 The Chromium Authors. All rights reserved. |
| 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 | |
| 5 | #include <windows.h> |
| 6 | #include <algorithm> |
| 7 | |
| 8 | #include "chrome/renderer/render_thread.h" |
| 9 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 10 | #include "base/shared_memory.h" |
[email protected] | a9f4d90 | 2008-09-15 23:43:42 | [diff] [blame] | 11 | #include "chrome/common/chrome_plugin_lib.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 12 | #include "chrome/common/ipc_logging.h" |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 13 | #include "chrome/common/notification_service.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 14 | #include "chrome/plugin/plugin_channel.h" |
| 15 | #include "chrome/renderer/net/render_dns_master.h" |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 16 | #include "chrome/renderer/greasemonkey_slave.h" |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 17 | #include "chrome/renderer/render_process.h" |
| 18 | #include "chrome/renderer/render_view.h" |
| 19 | #include "chrome/renderer/visitedlink_slave.h" |
| 20 | #include "webkit/glue/cache_manager.h" |
| 21 | |
[email protected] | 1d97d2e | 2008-12-18 23:39:02 | [diff] [blame] | 22 | RenderThread* g_render_thread; |
| 23 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 24 | static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */; |
| 25 | |
| 26 | // V8 needs a 1MB stack size. |
| 27 | static const size_t kStackSize = 1024 * 1024; |
| 28 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 29 | //----------------------------------------------------------------------------- |
| 30 | // Methods below are only called on the owner's thread: |
| 31 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 32 | RenderThread::RenderThread(const std::wstring& channel_name) |
| 33 | : Thread("Chrome_RenderThread"), |
| 34 | channel_name_(channel_name), |
| 35 | owner_loop_(MessageLoop::current()), |
| 36 | visited_link_slave_(NULL), |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 37 | greasemonkey_slave_(NULL), |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 38 | render_dns_master_(NULL), |
| 39 | in_send_(0) { |
| 40 | DCHECK(owner_loop_); |
[email protected] | ab820df | 2008-08-26 05:55:10 | [diff] [blame] | 41 | base::Thread::Options options; |
| 42 | options.stack_size = kStackSize; |
| 43 | // When we run plugins in process, we actually run them on the render thread, |
| 44 | // which means that we need to make the render thread pump UI events. |
| 45 | if (RenderProcess::ShouldLoadPluginsInProcess()) |
| 46 | options.message_loop_type = MessageLoop::TYPE_UI; |
| 47 | StartWithOptions(options); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 48 | } |
| 49 | |
| 50 | RenderThread::~RenderThread() { |
| 51 | Stop(); |
| 52 | } |
| 53 | |
| 54 | void RenderThread::OnChannelError() { |
[email protected] | 295039bd | 2008-08-15 04:32:57 | [diff] [blame] | 55 | owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask()); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 56 | } |
| 57 | |
| 58 | bool RenderThread::Send(IPC::Message* msg) { |
| 59 | in_send_++; |
| 60 | bool rv = channel_->Send(msg); |
| 61 | in_send_--; |
| 62 | return rv; |
| 63 | } |
| 64 | |
| 65 | void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 66 | channel_->AddFilter(filter); |
| 67 | } |
| 68 | |
| 69 | void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) { |
| 70 | channel_->RemoveFilter(filter); |
| 71 | } |
| 72 | |
| 73 | void RenderThread::Resolve(const char* name, size_t length) { |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 74 | return render_dns_master_->Resolve(name, length); |
| 75 | } |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 76 | |
| 77 | void RenderThread::AddRoute(int32 routing_id, |
| 78 | IPC::Channel::Listener* listener) { |
| 79 | DCHECK(MessageLoop::current() == message_loop()); |
| 80 | |
| 81 | // This corresponds to the AddRoute call done in CreateView. |
| 82 | router_.AddRoute(routing_id, listener); |
| 83 | } |
| 84 | |
| 85 | void RenderThread::RemoveRoute(int32 routing_id) { |
| 86 | DCHECK(MessageLoop::current() == message_loop()); |
| 87 | |
| 88 | router_.RemoveRoute(routing_id); |
| 89 | } |
| 90 | |
| 91 | void RenderThread::Init() { |
[email protected] | 1d97d2e | 2008-12-18 23:39:02 | [diff] [blame] | 92 | DCHECK(!g_render_thread); |
| 93 | g_render_thread = this; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 94 | |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 95 | notification_service_.reset(new NotificationService); |
| 96 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 97 | cache_stats_factory_.reset( |
| 98 | new ScopedRunnableMethodFactory<RenderThread>(this)); |
| 99 | |
| 100 | channel_.reset(new IPC::SyncChannel(channel_name_, |
[email protected] | d65cab7a | 2008-08-12 01:25:41 | [diff] [blame] | 101 | IPC::Channel::MODE_CLIENT, this, NULL, owner_loop_, true, |
| 102 | RenderProcess::GetShutDownEvent())); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 103 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 104 | // The renderer thread should wind-up COM. |
| 105 | CoInitialize(0); |
| 106 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 107 | visited_link_slave_ = new VisitedLinkSlave(); |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 108 | greasemonkey_slave_ = new GreasemonkeySlave(); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 109 | |
| 110 | render_dns_master_.reset(new RenderDnsMaster()); |
| 111 | |
| 112 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 113 | IPC::Logging::current()->SetIPCSender(this); |
| 114 | #endif |
| 115 | } |
| 116 | |
| 117 | void RenderThread::CleanUp() { |
[email protected] | 1d97d2e | 2008-12-18 23:39:02 | [diff] [blame] | 118 | DCHECK(g_render_thread == this); |
| 119 | g_render_thread = NULL; |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 120 | |
[email protected] | 8fd8de9 | 2008-08-12 23:50:30 | [diff] [blame] | 121 | // Need to destruct the SyncChannel to the browser before we go away because |
| 122 | // it caches a pointer to this thread. |
| 123 | channel_.reset(); |
| 124 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 125 | // Clean up plugin channels before this thread goes away. |
| 126 | PluginChannelBase::CleanupChannels(); |
| 127 | |
| 128 | #ifdef IPC_MESSAGE_LOG_ENABLED |
| 129 | IPC::Logging::current()->SetIPCSender(NULL); |
| 130 | #endif |
| 131 | |
[email protected] | 173de1b | 2008-08-15 18:36:46 | [diff] [blame] | 132 | notification_service_.reset(); |
| 133 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 134 | delete visited_link_slave_; |
| 135 | visited_link_slave_ = NULL; |
| 136 | |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 137 | delete greasemonkey_slave_; |
| 138 | greasemonkey_slave_ = NULL; |
| 139 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 140 | CoUninitialize(); |
| 141 | } |
| 142 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 143 | void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) { |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 144 | DCHECK(table) << "Bad table handle"; |
| 145 | visited_link_slave_->Init(table); |
| 146 | } |
| 147 | |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 148 | void RenderThread::OnUpdateGreasemonkeyScripts( |
| 149 | base::SharedMemoryHandle scripts) { |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 150 | DCHECK(scripts) << "Bad scripts handle"; |
[email protected] | 176aa48 | 2008-11-14 03:25:15 | [diff] [blame] | 151 | greasemonkey_slave_->UpdateScripts(scripts); |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 152 | } |
| 153 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 154 | void RenderThread::OnMessageReceived(const IPC::Message& msg) { |
| 155 | // NOTE: We could subclass router_ to intercept OnControlMessageReceived, but |
| 156 | // it seems simpler to just process any control messages that we care about |
| 157 | // up-front and then send the rest of the messages onto router_. |
| 158 | |
| 159 | if (msg.routing_id() == MSG_ROUTING_CONTROL) { |
| 160 | IPC_BEGIN_MESSAGE_MAP(RenderThread, msg) |
| 161 | IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks) |
| 162 | IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID) |
| 163 | IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView) |
| 164 | IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities) |
| 165 | IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats, |
| 166 | OnGetCacheResourceStats) |
[email protected] | a9f4d90 | 2008-09-15 23:43:42 | [diff] [blame] | 167 | IPC_MESSAGE_HANDLER(ViewMsg_PluginMessage, OnPluginMessage) |
[email protected] | 1e0f7040 | 2008-10-16 23:57:47 | [diff] [blame] | 168 | IPC_MESSAGE_HANDLER(ViewMsg_Greasemonkey_NewScripts, |
| 169 | OnUpdateGreasemonkeyScripts) |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 170 | // send the rest to the router |
| 171 | IPC_MESSAGE_UNHANDLED(router_.OnMessageReceived(msg)) |
| 172 | IPC_END_MESSAGE_MAP() |
| 173 | } else { |
| 174 | router_.OnMessageReceived(msg); |
| 175 | } |
| 176 | } |
| 177 | |
[email protected] | 690a99c | 2009-01-06 16:48:45 | [diff] [blame^] | 178 | void RenderThread::OnPluginMessage(const FilePath& plugin_path, |
[email protected] | a9f4d90 | 2008-09-15 23:43:42 | [diff] [blame] | 179 | const std::vector<uint8>& data) { |
| 180 | CHECK(ChromePluginLib::IsPluginThread()); |
[email protected] | 690a99c | 2009-01-06 16:48:45 | [diff] [blame^] | 181 | ChromePluginLib *chrome_plugin = ChromePluginLib::Find(plugin_path); |
[email protected] | a9f4d90 | 2008-09-15 23:43:42 | [diff] [blame] | 182 | if (chrome_plugin) { |
| 183 | void *data_ptr = const_cast<void*>(reinterpret_cast<const void*>(&data[0])); |
| 184 | uint32 data_len = static_cast<uint32>(data.size()); |
| 185 | chrome_plugin->functions().on_message(data_ptr, data_len); |
| 186 | } |
| 187 | } |
| 188 | |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 189 | void RenderThread::OnSetNextPageID(int32 next_page_id) { |
| 190 | // This should only be called at process initialization time, so we shouldn't |
| 191 | // have to worry about thread-safety. |
| 192 | RenderView::SetNextPageID(next_page_id); |
| 193 | } |
| 194 | |
| 195 | void RenderThread::OnCreateNewView(HWND parent_hwnd, |
| 196 | HANDLE modal_dialog_event, |
| 197 | const WebPreferences& webkit_prefs, |
| 198 | int32 view_id) { |
| 199 | // TODO(darin): once we have a RenderThread per RenderView, this will need to |
| 200 | // change to assert that we are not creating more than one view. |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 201 | RenderView::Create( |
[email protected] | 81a3441 | 2009-01-05 19:17:24 | [diff] [blame] | 202 | this, parent_hwnd, modal_dialog_event, MSG_ROUTING_NONE, webkit_prefs, |
[email protected] | 0aa5531 | 2008-10-17 21:53:08 | [diff] [blame] | 203 | new SharedRenderViewCounter(0), view_id); |
initial.commit | 09911bf | 2008-07-26 23:55:29 | [diff] [blame] | 204 | } |
| 205 | |
| 206 | void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity, |
| 207 | size_t max_dead_capacity, |
| 208 | size_t capacity) { |
| 209 | CacheManager::SetCapacities(min_dead_capacity, max_dead_capacity, capacity); |
| 210 | } |
| 211 | |
| 212 | void RenderThread::OnGetCacheResourceStats() { |
| 213 | CacheManager::ResourceTypeStats stats; |
| 214 | CacheManager::GetResourceTypeStats(&stats); |
| 215 | Send(new ViewHostMsg_ResourceTypeStats(stats)); |
| 216 | } |
| 217 | |
| 218 | void RenderThread::InformHostOfCacheStats() { |
| 219 | CacheManager::UsageStats stats; |
| 220 | CacheManager::GetUsageStats(&stats); |
| 221 | Send(new ViewHostMsg_UpdatedCacheStats(stats)); |
| 222 | } |
| 223 | |
| 224 | void RenderThread::InformHostOfCacheStatsLater() { |
| 225 | // Rate limit informing the host of our cache stats. |
| 226 | if (!cache_stats_factory_->empty()) |
| 227 | return; |
| 228 | |
| 229 | MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 230 | cache_stats_factory_->NewRunnableMethod( |
| 231 | &RenderThread::InformHostOfCacheStats), |
| 232 | kCacheStatsDelayMS); |
| 233 | } |