blob: 5aa1dc6f7d1ff6925e09c0356008ccb663807dbc [file] [log] [blame]
license.botbf09a502008-08-24 00:55:551// 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.commit09911bf2008-07-26 23:55:294
5#include <windows.h>
6#include <algorithm>
7
8#include "chrome/renderer/render_thread.h"
9
initial.commit09911bf2008-07-26 23:55:2910#include "base/shared_memory.h"
[email protected]a9f4d902008-09-15 23:43:4211#include "chrome/common/chrome_plugin_lib.h"
initial.commit09911bf2008-07-26 23:55:2912#include "chrome/common/ipc_logging.h"
[email protected]173de1b2008-08-15 18:36:4613#include "chrome/common/notification_service.h"
initial.commit09911bf2008-07-26 23:55:2914#include "chrome/plugin/plugin_channel.h"
15#include "chrome/renderer/net/render_dns_master.h"
[email protected]1e0f70402008-10-16 23:57:4716#include "chrome/renderer/greasemonkey_slave.h"
initial.commit09911bf2008-07-26 23:55:2917#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]1d97d2e2008-12-18 23:39:0222RenderThread* g_render_thread;
23
initial.commit09911bf2008-07-26 23:55:2924static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
25
26// V8 needs a 1MB stack size.
27static const size_t kStackSize = 1024 * 1024;
28
initial.commit09911bf2008-07-26 23:55:2929//-----------------------------------------------------------------------------
30// Methods below are only called on the owner's thread:
31
initial.commit09911bf2008-07-26 23:55:2932RenderThread::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]1e0f70402008-10-16 23:57:4737 greasemonkey_slave_(NULL),
initial.commit09911bf2008-07-26 23:55:2938 render_dns_master_(NULL),
39 in_send_(0) {
40 DCHECK(owner_loop_);
[email protected]ab820df2008-08-26 05:55:1041 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.commit09911bf2008-07-26 23:55:2948}
49
50RenderThread::~RenderThread() {
51 Stop();
52}
53
54void RenderThread::OnChannelError() {
[email protected]295039bd2008-08-15 04:32:5755 owner_loop_->PostTask(FROM_HERE, new MessageLoop::QuitTask());
initial.commit09911bf2008-07-26 23:55:2956}
57
58bool RenderThread::Send(IPC::Message* msg) {
59 in_send_++;
60 bool rv = channel_->Send(msg);
61 in_send_--;
62 return rv;
63}
64
65void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
66 channel_->AddFilter(filter);
67}
68
69void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
70 channel_->RemoveFilter(filter);
71}
72
73void RenderThread::Resolve(const char* name, size_t length) {
[email protected]81a34412009-01-05 19:17:2474 return render_dns_master_->Resolve(name, length);
75}
initial.commit09911bf2008-07-26 23:55:2976
77void 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
85void RenderThread::RemoveRoute(int32 routing_id) {
86 DCHECK(MessageLoop::current() == message_loop());
87
88 router_.RemoveRoute(routing_id);
89}
90
91void RenderThread::Init() {
[email protected]1d97d2e2008-12-18 23:39:0292 DCHECK(!g_render_thread);
93 g_render_thread = this;
initial.commit09911bf2008-07-26 23:55:2994
[email protected]173de1b2008-08-15 18:36:4695 notification_service_.reset(new NotificationService);
96
initial.commit09911bf2008-07-26 23:55:2997 cache_stats_factory_.reset(
98 new ScopedRunnableMethodFactory<RenderThread>(this));
99
100 channel_.reset(new IPC::SyncChannel(channel_name_,
[email protected]d65cab7a2008-08-12 01:25:41101 IPC::Channel::MODE_CLIENT, this, NULL, owner_loop_, true,
102 RenderProcess::GetShutDownEvent()));
initial.commit09911bf2008-07-26 23:55:29103
initial.commit09911bf2008-07-26 23:55:29104 // The renderer thread should wind-up COM.
105 CoInitialize(0);
106
initial.commit09911bf2008-07-26 23:55:29107 visited_link_slave_ = new VisitedLinkSlave();
[email protected]1e0f70402008-10-16 23:57:47108 greasemonkey_slave_ = new GreasemonkeySlave();
initial.commit09911bf2008-07-26 23:55:29109
110 render_dns_master_.reset(new RenderDnsMaster());
111
112#ifdef IPC_MESSAGE_LOG_ENABLED
113 IPC::Logging::current()->SetIPCSender(this);
114#endif
115}
116
117void RenderThread::CleanUp() {
[email protected]1d97d2e2008-12-18 23:39:02118 DCHECK(g_render_thread == this);
119 g_render_thread = NULL;
initial.commit09911bf2008-07-26 23:55:29120
[email protected]8fd8de92008-08-12 23:50:30121 // 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.commit09911bf2008-07-26 23:55:29125 // 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]173de1b2008-08-15 18:36:46132 notification_service_.reset();
133
initial.commit09911bf2008-07-26 23:55:29134 delete visited_link_slave_;
135 visited_link_slave_ = NULL;
136
[email protected]1e0f70402008-10-16 23:57:47137 delete greasemonkey_slave_;
138 greasemonkey_slave_ = NULL;
139
initial.commit09911bf2008-07-26 23:55:29140 CoUninitialize();
141}
142
[email protected]176aa482008-11-14 03:25:15143void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
initial.commit09911bf2008-07-26 23:55:29144 DCHECK(table) << "Bad table handle";
145 visited_link_slave_->Init(table);
146}
147
[email protected]176aa482008-11-14 03:25:15148void RenderThread::OnUpdateGreasemonkeyScripts(
149 base::SharedMemoryHandle scripts) {
[email protected]1e0f70402008-10-16 23:57:47150 DCHECK(scripts) << "Bad scripts handle";
[email protected]176aa482008-11-14 03:25:15151 greasemonkey_slave_->UpdateScripts(scripts);
[email protected]1e0f70402008-10-16 23:57:47152}
153
initial.commit09911bf2008-07-26 23:55:29154void 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]a9f4d902008-09-15 23:43:42167 IPC_MESSAGE_HANDLER(ViewMsg_PluginMessage, OnPluginMessage)
[email protected]1e0f70402008-10-16 23:57:47168 IPC_MESSAGE_HANDLER(ViewMsg_Greasemonkey_NewScripts,
169 OnUpdateGreasemonkeyScripts)
initial.commit09911bf2008-07-26 23:55:29170 // 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]690a99c2009-01-06 16:48:45178void RenderThread::OnPluginMessage(const FilePath& plugin_path,
[email protected]a9f4d902008-09-15 23:43:42179 const std::vector<uint8>& data) {
180 CHECK(ChromePluginLib::IsPluginThread());
[email protected]690a99c2009-01-06 16:48:45181 ChromePluginLib *chrome_plugin = ChromePluginLib::Find(plugin_path);
[email protected]a9f4d902008-09-15 23:43:42182 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.commit09911bf2008-07-26 23:55:29189void 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
195void 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.commit09911bf2008-07-26 23:55:29201 RenderView::Create(
[email protected]81a34412009-01-05 19:17:24202 this, parent_hwnd, modal_dialog_event, MSG_ROUTING_NONE, webkit_prefs,
[email protected]0aa55312008-10-17 21:53:08203 new SharedRenderViewCounter(0), view_id);
initial.commit09911bf2008-07-26 23:55:29204}
205
206void 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
212void RenderThread::OnGetCacheResourceStats() {
213 CacheManager::ResourceTypeStats stats;
214 CacheManager::GetResourceTypeStats(&stats);
215 Send(new ViewHostMsg_ResourceTypeStats(stats));
216}
217
218void RenderThread::InformHostOfCacheStats() {
219 CacheManager::UsageStats stats;
220 CacheManager::GetUsageStats(&stats);
221 Send(new ViewHostMsg_UpdatedCacheStats(stats));
222}
223
224void 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}