1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
|
// Copyright (c) 2006-2008 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "build/build_config.h"
#if defined(OS_WIN)
#include <windows.h>
#include <objbase.h>
#endif
#include <algorithm>
#include "chrome/renderer/render_thread.h"
#include "base/command_line.h"
#include "base/shared_memory.h"
#include "chrome/common/chrome_plugin_lib.h"
#include "chrome/common/chrome_switches.h"
#include "chrome/common/render_messages.h"
#include "chrome/common/notification_service.h"
#include "chrome/common/url_constants.h"
#include "chrome/plugin/npobject_util.h"
// TODO(port)
#if defined(OS_WIN)
#include "chrome/plugin/plugin_channel.h"
#else
#include <vector>
#include "base/scoped_handle.h"
#include "chrome/plugin/plugin_channel_base.h"
#include "webkit/glue/weburlrequest.h"
#endif
#include "chrome/renderer/net/render_dns_master.h"
#include "chrome/renderer/render_process.h"
#include "chrome/renderer/render_view.h"
#include "chrome/renderer/renderer_webkitclient_impl.h"
#include "chrome/renderer/user_script_slave.h"
#include "chrome/renderer/visitedlink_slave.h"
#include "webkit/extensions/v8/gears_extension.h"
#include "webkit/extensions/v8/interval_extension.h"
#include "webkit/extensions/v8/playback_extension.h"
#include "webkit/glue/cache_manager.h"
#include "WebKit.h"
#include "WebString.h"
static const unsigned int kCacheStatsDelayMS = 2000 /* milliseconds */;
//-----------------------------------------------------------------------------
// Methods below are only called on the owner's thread:
// When we run plugins in process, we actually run them on the render thread,
// which means that we need to make the render thread pump UI events.
RenderThread::RenderThread()
: ChildThread(
base::Thread::Options(RenderProcess::InProcessPlugins() ?
MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
}
RenderThread::RenderThread(const std::wstring& channel_name)
: ChildThread(
base::Thread::Options(RenderProcess::InProcessPlugins() ?
MessageLoop::TYPE_UI : MessageLoop::TYPE_DEFAULT, kV8StackSize)) {
SetChannelName(channel_name);
}
RenderThread::~RenderThread() {
}
RenderThread* RenderThread::current() {
DCHECK(!IsPluginProcess());
return static_cast<RenderThread*>(ChildThread::current());
}
void RenderThread::AddFilter(IPC::ChannelProxy::MessageFilter* filter) {
channel()->AddFilter(filter);
}
void RenderThread::RemoveFilter(IPC::ChannelProxy::MessageFilter* filter) {
channel()->RemoveFilter(filter);
}
void RenderThread::Resolve(const char* name, size_t length) {
return dns_master_->Resolve(name, length);
}
void RenderThread::SendHistograms() {
return histogram_snapshots_->SendHistograms();
}
void RenderThread::Init() {
// TODO(darin): Why do we need COM here? This is probably bogus.
#if defined(OS_WIN)
// The renderer thread should wind-up COM.
CoInitialize(0);
#endif
ChildThread::Init();
notification_service_.reset(new NotificationService);
cache_stats_factory_.reset(
new ScopedRunnableMethodFactory<RenderThread>(this));
visited_link_slave_.reset(new VisitedLinkSlave());
user_script_slave_.reset(new UserScriptSlave());
dns_master_.reset(new RenderDnsMaster());
histogram_snapshots_.reset(new RendererHistogramSnapshots());
}
void RenderThread::CleanUp() {
// Shutdown in reverse of the initialization order.
histogram_snapshots_.reset();
dns_master_.reset();
user_script_slave_.reset();
visited_link_slave_.reset();
if (webkit_client_.get()) {
WebKit::shutdown();
webkit_client_.reset();
}
notification_service_.reset();
ChildThread::CleanUp();
// TODO(port)
#if defined(OS_WIN)
// Clean up plugin channels before this thread goes away.
PluginChannelBase::CleanupChannels();
#endif
#if defined(OS_WIN)
CoUninitialize();
#endif
}
void RenderThread::OnUpdateVisitedLinks(base::SharedMemoryHandle table) {
DCHECK(base::SharedMemory::IsHandleValid(table)) << "Bad table handle";
visited_link_slave_->Init(table);
}
void RenderThread::OnUpdateUserScripts(
base::SharedMemoryHandle scripts) {
DCHECK(base::SharedMemory::IsHandleValid(scripts)) << "Bad scripts handle";
user_script_slave_->UpdateScripts(scripts);
}
void RenderThread::OnControlMessageReceived(const IPC::Message& msg) {
IPC_BEGIN_MESSAGE_MAP(RenderThread, msg)
IPC_MESSAGE_HANDLER(ViewMsg_VisitedLink_NewTable, OnUpdateVisitedLinks)
IPC_MESSAGE_HANDLER(ViewMsg_SetNextPageID, OnSetNextPageID)
// TODO(port): removed from render_messages_internal.h;
// is there a new non-windows message I should add here?
IPC_MESSAGE_HANDLER(ViewMsg_New, OnCreateNewView)
IPC_MESSAGE_HANDLER(ViewMsg_SetCacheCapacities, OnSetCacheCapacities)
IPC_MESSAGE_HANDLER(ViewMsg_GetRendererHistograms,
OnGetRendererHistograms)
IPC_MESSAGE_HANDLER(ViewMsg_GetCacheResourceStats,
OnGetCacheResourceStats)
IPC_MESSAGE_HANDLER(ViewMsg_PluginMessage, OnPluginMessage)
IPC_MESSAGE_HANDLER(ViewMsg_UserScripts_NewScripts,
OnUpdateUserScripts)
IPC_END_MESSAGE_MAP()
}
void RenderThread::OnPluginMessage(const FilePath& plugin_path,
const std::vector<uint8>& data) {
if (!ChromePluginLib::IsInitialized()) {
return;
}
CHECK(ChromePluginLib::IsPluginThread());
ChromePluginLib *chrome_plugin = ChromePluginLib::Find(plugin_path);
if (chrome_plugin) {
void *data_ptr = const_cast<void*>(reinterpret_cast<const void*>(&data[0]));
uint32 data_len = static_cast<uint32>(data.size());
chrome_plugin->functions().on_message(data_ptr, data_len);
}
}
void RenderThread::OnSetNextPageID(int32 next_page_id) {
// This should only be called at process initialization time, so we shouldn't
// have to worry about thread-safety.
// TODO(port)
#if !defined(OS_LINUX)
RenderView::SetNextPageID(next_page_id);
#endif
}
void RenderThread::OnCreateNewView(gfx::NativeViewId parent_hwnd,
ModalDialogEvent modal_dialog_event,
const WebPreferences& webkit_prefs,
int32 view_id) {
EnsureWebKitInitialized();
// When bringing in render_view, also bring in webkit's glue and jsbindings.
base::WaitableEvent* waitable_event = new base::WaitableEvent(
#if defined(OS_WIN)
modal_dialog_event.event);
#else
true, false);
#endif
// TODO(darin): once we have a RenderThread per RenderView, this will need to
// change to assert that we are not creating more than one view.
RenderView::Create(
this, parent_hwnd, waitable_event, MSG_ROUTING_NONE, webkit_prefs,
new SharedRenderViewCounter(0), view_id);
}
void RenderThread::OnSetCacheCapacities(size_t min_dead_capacity,
size_t max_dead_capacity,
size_t capacity) {
EnsureWebKitInitialized();
CacheManager::SetCapacities(min_dead_capacity, max_dead_capacity, capacity);
}
void RenderThread::OnGetCacheResourceStats() {
EnsureWebKitInitialized();
CacheManager::ResourceTypeStats stats;
CacheManager::GetResourceTypeStats(&stats);
Send(new ViewHostMsg_ResourceTypeStats(stats));
}
void RenderThread::OnGetRendererHistograms() {
SendHistograms();
}
void RenderThread::InformHostOfCacheStats() {
EnsureWebKitInitialized();
CacheManager::UsageStats stats;
CacheManager::GetUsageStats(&stats);
Send(new ViewHostMsg_UpdatedCacheStats(stats));
}
void RenderThread::InformHostOfCacheStatsLater() {
// Rate limit informing the host of our cache stats.
if (!cache_stats_factory_->empty())
return;
MessageLoop::current()->PostDelayedTask(FROM_HERE,
cache_stats_factory_->NewRunnableMethod(
&RenderThread::InformHostOfCacheStats),
kCacheStatsDelayMS);
}
void RenderThread::EnsureWebKitInitialized() {
if (webkit_client_.get())
return;
webkit_client_.reset(new RendererWebKitClientImpl);
WebKit::initialize(webkit_client_.get());
WebKit::registerURLSchemeAsLocal(ASCIIToUTF16(chrome::kChromeUIScheme));
WebKit::registerExtension(extensions_v8::GearsExtension::Get());
WebKit::registerExtension(extensions_v8::IntervalExtension::Get());
const CommandLine& command_line = *CommandLine::ForCurrentProcess();
if (command_line.HasSwitch(switches::kPlaybackMode) ||
command_line.HasSwitch(switches::kRecordMode)) {
WebKit::registerExtension(extensions_v8::PlaybackExtension::Get());
}
}
|