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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
|
// Copyright 2014 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 "content/browser/shared_worker/shared_worker_host.h"
#include "base/metrics/histogram.h"
#include "content/browser/devtools/shared_worker_devtools_manager.h"
#include "content/browser/message_port_message_filter.h"
#include "content/browser/message_port_service.h"
#include "content/browser/shared_worker/shared_worker_instance.h"
#include "content/browser/shared_worker/shared_worker_message_filter.h"
#include "content/browser/shared_worker/shared_worker_service_impl.h"
#include "content/browser/shared_worker/worker_document_set.h"
#include "content/common/view_messages.h"
#include "content/common/worker_messages.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/content_browser_client.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/content_client.h"
namespace content {
namespace {
void NotifyWorkerReadyForInspection(int worker_process_id,
int worker_route_id) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(NotifyWorkerReadyForInspection,
worker_process_id,
worker_route_id));
return;
}
SharedWorkerDevToolsManager::GetInstance()->WorkerReadyForInspection(
worker_process_id, worker_route_id);
}
void NotifyWorkerDestroyed(int worker_process_id, int worker_route_id) {
if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(NotifyWorkerDestroyed, worker_process_id, worker_route_id));
return;
}
SharedWorkerDevToolsManager::GetInstance()->WorkerDestroyed(
worker_process_id, worker_route_id);
}
} // namespace
SharedWorkerHost::SharedWorkerHost(SharedWorkerInstance* instance,
SharedWorkerMessageFilter* filter,
int worker_route_id)
: instance_(instance),
worker_document_set_(new WorkerDocumentSet()),
container_render_filter_(filter),
worker_process_id_(filter->render_process_id()),
worker_route_id_(worker_route_id),
termination_message_sent_(false),
closed_(false),
creation_time_(base::TimeTicks::Now()),
weak_factory_(this) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
}
SharedWorkerHost::~SharedWorkerHost() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
UMA_HISTOGRAM_LONG_TIMES("SharedWorker.TimeToDeleted",
base::TimeTicks::Now() - creation_time_);
if (!closed_ && !termination_message_sent_)
NotifyWorkerDestroyed(worker_process_id_, worker_route_id_);
SharedWorkerServiceImpl::GetInstance()->NotifyWorkerDestroyed(
worker_process_id_, worker_route_id_);
}
bool SharedWorkerHost::Send(IPC::Message* message) {
if (!container_render_filter_) {
delete message;
return false;
}
return container_render_filter_->Send(message);
}
void SharedWorkerHost::Start(bool pause_on_start) {
WorkerProcessMsg_CreateWorker_Params params;
params.url = instance_->url();
params.name = instance_->name();
params.content_security_policy = instance_->content_security_policy();
params.security_policy_type = instance_->security_policy_type();
params.pause_on_start = pause_on_start;
params.route_id = worker_route_id_;
Send(new WorkerProcessMsg_CreateWorker(params));
for (FilterList::const_iterator i = filters_.begin(); i != filters_.end();
++i) {
i->filter()->Send(new ViewMsg_WorkerCreated(i->route_id()));
}
}
bool SharedWorkerHost::FilterMessage(const IPC::Message& message,
SharedWorkerMessageFilter* filter) {
if (!IsAvailable() || !HasFilter(filter, message.routing_id()))
return false;
RelayMessage(message, filter);
return true;
}
void SharedWorkerHost::FilterShutdown(SharedWorkerMessageFilter* filter) {
if (!instance_)
return;
RemoveFilters(filter);
worker_document_set_->RemoveAll(filter);
if (worker_document_set_->IsEmpty()) {
// This worker has no more associated documents - shut it down.
TerminateWorker();
}
}
void SharedWorkerHost::DocumentDetached(SharedWorkerMessageFilter* filter,
unsigned long long document_id) {
if (!instance_)
return;
// Walk all instances and remove the document from their document set.
worker_document_set_->Remove(filter, document_id);
if (worker_document_set_->IsEmpty()) {
// This worker has no more associated documents - shut it down.
TerminateWorker();
}
}
void SharedWorkerHost::WorkerContextClosed() {
if (!instance_)
return;
// Set the closed flag - this will stop any further messages from
// being sent to the worker (messages can still be sent from the worker,
// for exception reporting, etc).
closed_ = true;
if (!termination_message_sent_)
NotifyWorkerDestroyed(worker_process_id_, worker_route_id_);
}
void SharedWorkerHost::WorkerContextDestroyed() {
if (!instance_)
return;
instance_.reset();
worker_document_set_ = NULL;
}
void SharedWorkerHost::WorkerReadyForInspection() {
NotifyWorkerReadyForInspection(worker_process_id_, worker_route_id_);
}
void SharedWorkerHost::WorkerScriptLoaded() {
UMA_HISTOGRAM_TIMES("SharedWorker.TimeToScriptLoaded",
base::TimeTicks::Now() - creation_time_);
}
void SharedWorkerHost::WorkerScriptLoadFailed() {
UMA_HISTOGRAM_TIMES("SharedWorker.TimeToScriptLoadFailed",
base::TimeTicks::Now() - creation_time_);
if (!instance_)
return;
for (FilterList::const_iterator i = filters_.begin(); i != filters_.end();
++i) {
i->filter()->Send(new ViewMsg_WorkerScriptLoadFailed(i->route_id()));
}
}
void SharedWorkerHost::WorkerConnected(int message_port_id) {
if (!instance_)
return;
for (FilterList::const_iterator i = filters_.begin(); i != filters_.end();
++i) {
if (i->message_port_id() != message_port_id)
continue;
i->filter()->Send(new ViewMsg_WorkerConnected(i->route_id()));
return;
}
}
void SharedWorkerHost::AllowDatabase(const GURL& url,
const base::string16& name,
const base::string16& display_name,
bool* result) {
if (!instance_)
return;
*result = GetContentClient()->browser()->AllowWorkerDatabase(
url,
name,
display_name,
instance_->resource_context(),
GetRenderFrameIDsForWorker());
}
void SharedWorkerHost::AllowFileSystem(const GURL& url,
scoped_ptr<IPC::Message> reply_msg) {
if (!instance_)
return;
GetContentClient()->browser()->AllowWorkerFileSystem(
url,
instance_->resource_context(),
GetRenderFrameIDsForWorker(),
base::Bind(&SharedWorkerHost::AllowFileSystemResponse,
weak_factory_.GetWeakPtr(),
base::Passed(&reply_msg)));
}
void SharedWorkerHost::AllowFileSystemResponse(
scoped_ptr<IPC::Message> reply_msg,
bool allowed) {
WorkerProcessHostMsg_RequestFileSystemAccessSync::WriteReplyParams(
reply_msg.get(),
allowed);
Send(reply_msg.release());
}
void SharedWorkerHost::AllowIndexedDB(const GURL& url,
const base::string16& name,
bool* result) {
if (!instance_)
return;
*result = GetContentClient()->browser()->AllowWorkerIndexedDB(
url, name, instance_->resource_context(), GetRenderFrameIDsForWorker());
}
void SharedWorkerHost::RelayMessage(
const IPC::Message& message,
SharedWorkerMessageFilter* incoming_filter) {
if (!instance_)
return;
if (message.type() == WorkerMsg_Connect::ID) {
// Crack the SharedWorker Connect message to setup routing for the port.
WorkerMsg_Connect::Param param;
if (!WorkerMsg_Connect::Read(&message, ¶m))
return;
int sent_message_port_id = base::get<0>(param);
int new_routing_id = base::get<1>(param);
DCHECK(container_render_filter_);
new_routing_id = container_render_filter_->GetNextRoutingID();
MessagePortService::GetInstance()->UpdateMessagePort(
sent_message_port_id,
container_render_filter_->message_port_message_filter(),
new_routing_id);
SetMessagePortID(
incoming_filter, message.routing_id(), sent_message_port_id);
// Resend the message with the new routing id.
Send(new WorkerMsg_Connect(
worker_route_id_, sent_message_port_id, new_routing_id));
// Send any queued messages for the sent port.
MessagePortService::GetInstance()->SendQueuedMessagesIfPossible(
sent_message_port_id);
} else {
IPC::Message* new_message = new IPC::Message(message);
new_message->set_routing_id(worker_route_id_);
Send(new_message);
return;
}
}
void SharedWorkerHost::TerminateWorker() {
termination_message_sent_ = true;
if (!closed_)
NotifyWorkerDestroyed(worker_process_id_, worker_route_id_);
Send(new WorkerMsg_TerminateWorkerContext(worker_route_id_));
}
std::vector<std::pair<int, int> >
SharedWorkerHost::GetRenderFrameIDsForWorker() {
std::vector<std::pair<int, int> > result;
if (!instance_)
return result;
const WorkerDocumentSet::DocumentInfoSet& documents =
worker_document_set_->documents();
for (WorkerDocumentSet::DocumentInfoSet::const_iterator doc =
documents.begin();
doc != documents.end();
++doc) {
result.push_back(
std::make_pair(doc->render_process_id(), doc->render_frame_id()));
}
return result;
}
bool SharedWorkerHost::IsAvailable() const {
return instance_ && !termination_message_sent_ && !closed_;
}
void SharedWorkerHost::AddFilter(SharedWorkerMessageFilter* filter,
int route_id) {
CHECK(filter);
if (!HasFilter(filter, route_id)) {
FilterInfo info(filter, route_id);
filters_.push_back(info);
}
}
void SharedWorkerHost::RemoveFilters(SharedWorkerMessageFilter* filter) {
for (FilterList::iterator i = filters_.begin(); i != filters_.end();) {
if (i->filter() == filter)
i = filters_.erase(i);
else
++i;
}
}
bool SharedWorkerHost::HasFilter(SharedWorkerMessageFilter* filter,
int route_id) const {
for (FilterList::const_iterator i = filters_.begin(); i != filters_.end();
++i) {
if (i->filter() == filter && i->route_id() == route_id)
return true;
}
return false;
}
void SharedWorkerHost::SetMessagePortID(SharedWorkerMessageFilter* filter,
int route_id,
int message_port_id) {
for (FilterList::iterator i = filters_.begin(); i != filters_.end(); ++i) {
if (i->filter() == filter && i->route_id() == route_id) {
i->set_message_port_id(message_port_id);
return;
}
}
}
} // namespace content
|