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
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
|
// Copyright 2013 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/service_worker/embedded_worker_instance.h"
#include <utility>
#include "base/bind_helpers.h"
#include "base/macros.h"
#include "base/metrics/histogram_macros.h"
#include "base/threading/non_thread_safe.h"
#include "base/trace_event/trace_event.h"
#include "content/browser/devtools/service_worker_devtools_manager.h"
#include "content/browser/service_worker/embedded_worker_registry.h"
#include "content/browser/service_worker/service_worker_context_core.h"
#include "content/common/content_switches_internal.h"
#include "content/common/mojo/service_registry_impl.h"
#include "content/common/service_worker/embedded_worker_messages.h"
#include "content/common/service_worker/embedded_worker_setup.mojom.h"
#include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/browser_thread.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/common/child_process_host.h"
#include "ipc/ipc_message.h"
#include "url/gurl.h"
namespace content {
namespace {
// When a service worker version's failure count exceeds
// |kMaxSameProcessFailureCount|, the embedded worker is forced to start in a
// new process.
const int kMaxSameProcessFailureCount = 2;
void NotifyWorkerReadyForInspectionOnUI(int worker_process_id,
int worker_route_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ServiceWorkerDevToolsManager::GetInstance()->WorkerReadyForInspection(
worker_process_id, worker_route_id);
}
void NotifyWorkerDestroyedOnUI(int worker_process_id, int worker_route_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ServiceWorkerDevToolsManager::GetInstance()->WorkerDestroyed(
worker_process_id, worker_route_id);
}
void NotifyWorkerStopIgnoredOnUI(int worker_process_id, int worker_route_id) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
ServiceWorkerDevToolsManager::GetInstance()->WorkerStopIgnored(
worker_process_id, worker_route_id);
}
void RegisterToWorkerDevToolsManagerOnUI(
int process_id,
const ServiceWorkerContextCore* service_worker_context,
const base::WeakPtr<ServiceWorkerContextCore>& service_worker_context_weak,
int64_t service_worker_version_id,
const GURL& url,
const base::Callback<void(int worker_devtools_agent_route_id,
bool wait_for_debugger)>& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
int worker_devtools_agent_route_id = MSG_ROUTING_NONE;
bool wait_for_debugger = false;
if (RenderProcessHost* rph = RenderProcessHost::FromID(process_id)) {
// |rph| may be NULL in unit tests.
worker_devtools_agent_route_id = rph->GetNextRoutingID();
wait_for_debugger =
ServiceWorkerDevToolsManager::GetInstance()->WorkerCreated(
process_id,
worker_devtools_agent_route_id,
ServiceWorkerDevToolsManager::ServiceWorkerIdentifier(
service_worker_context,
service_worker_context_weak,
service_worker_version_id,
url));
}
BrowserThread::PostTask(
BrowserThread::IO,
FROM_HERE,
base::Bind(callback, worker_devtools_agent_route_id, wait_for_debugger));
}
void SetupMojoOnUIThread(
int process_id,
int thread_id,
mojo::InterfaceRequest<mojo::InterfaceProvider> services,
mojo::InterfacePtrInfo<mojo::InterfaceProvider> exposed_services) {
RenderProcessHost* rph = RenderProcessHost::FromID(process_id);
// |rph| or its ServiceRegistry may be NULL in unit tests.
if (!rph || !rph->GetServiceRegistry())
return;
EmbeddedWorkerSetupPtr setup;
rph->GetServiceRegistry()->ConnectToRemoteService(mojo::GetProxy(&setup));
setup->ExchangeInterfaceProviders(
thread_id, std::move(services),
mojo::MakeProxy(std::move(exposed_services)));
}
} // namespace
// Lives on IO thread, proxies notifications to DevToolsManager that lives on
// UI thread. Owned by EmbeddedWorkerInstance.
class EmbeddedWorkerInstance::DevToolsProxy : public base::NonThreadSafe {
public:
DevToolsProxy(int process_id, int agent_route_id)
: process_id_(process_id),
agent_route_id_(agent_route_id) {}
~DevToolsProxy() {
BrowserThread::PostTask(
BrowserThread::UI,
FROM_HERE,
base::Bind(NotifyWorkerDestroyedOnUI,
process_id_, agent_route_id_));
}
void NotifyWorkerReadyForInspection() {
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
base::Bind(NotifyWorkerReadyForInspectionOnUI,
process_id_, agent_route_id_));
}
void NotifyWorkerStopIgnored() {
DCHECK(CalledOnValidThread());
BrowserThread::PostTask(BrowserThread::UI,
FROM_HERE,
base::Bind(NotifyWorkerStopIgnoredOnUI,
process_id_, agent_route_id_));
}
int agent_route_id() const { return agent_route_id_; }
private:
const int process_id_;
const int agent_route_id_;
DISALLOW_COPY_AND_ASSIGN(DevToolsProxy);
};
// A handle for a worker process managed by ServiceWorkerProcessManager on the
// UI thread.
class EmbeddedWorkerInstance::WorkerProcessHandle {
public:
WorkerProcessHandle(const base::WeakPtr<ServiceWorkerContextCore>& context,
int embedded_worker_id,
int process_id)
: context_(context),
embedded_worker_id_(embedded_worker_id),
process_id_(process_id) {
DCHECK_NE(ChildProcessHost::kInvalidUniqueID, process_id_);
}
~WorkerProcessHandle() {
if (context_)
context_->process_manager()->ReleaseWorkerProcess(embedded_worker_id_);
}
int process_id() const { return process_id_; }
private:
base::WeakPtr<ServiceWorkerContextCore> context_;
const int embedded_worker_id_;
const int process_id_;
DISALLOW_COPY_AND_ASSIGN(WorkerProcessHandle);
};
// A task to allocate a worker process and to send a start worker message. This
// is created on EmbeddedWorkerInstance::Start(), owned by the instance and
// destroyed on EmbeddedWorkerInstance::OnScriptEvaluated().
// We can abort starting worker by destroying this task anytime during the
// sequence.
class EmbeddedWorkerInstance::StartTask {
public:
enum class ProcessAllocationState { NOT_ALLOCATED, ALLOCATING, ALLOCATED };
explicit StartTask(EmbeddedWorkerInstance* instance)
: instance_(instance),
state_(ProcessAllocationState::NOT_ALLOCATED),
weak_factory_(this) {}
~StartTask() {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (!instance_->context_)
return;
switch (state_) {
case ProcessAllocationState::NOT_ALLOCATED:
// Not necessary to release a process.
break;
case ProcessAllocationState::ALLOCATING:
// Abort half-baked process allocation on the UI thread.
instance_->context_->process_manager()->ReleaseWorkerProcess(
instance_->embedded_worker_id());
break;
case ProcessAllocationState::ALLOCATED:
// Otherwise, the process will be released by EmbeddedWorkerInstance.
break;
}
// Don't have to abort |start_callback_| here. The caller of
// EmbeddedWorkerInstance::Start(), that is, ServiceWorkerVersion does not
// expect it when the start worker sequence is canceled by Stop() because
// the callback, ServiceWorkerVersion::OnStartSentAndScriptEvaluated(),
// could drain valid start requests queued in the version. After the worker
// is stopped, the version attempts to restart the worker if there are
// requests in the queue. See ServiceWorkerVersion::OnStoppedInternal() for
// details.
// TODO(nhiroki): Reconsider this bizarre layering.
}
void Start(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
const StatusCallback& callback) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
TRACE_EVENT_ASYNC_BEGIN2("ServiceWorker",
"EmbeddedWorkerInstance::ProcessAllocate",
params.get(), "Scope", params->scope.spec(),
"Script URL", params->script_url.spec());
state_ = ProcessAllocationState::ALLOCATING;
start_callback_ = callback;
GURL scope(params->scope);
GURL script_url(params->script_url);
bool can_use_existing_process =
instance_->context_->GetVersionFailureCount(
params->service_worker_version_id) < kMaxSameProcessFailureCount;
instance_->context_->process_manager()->AllocateWorkerProcess(
instance_->embedded_worker_id_, scope, script_url,
can_use_existing_process,
base::Bind(&StartTask::OnProcessAllocated, weak_factory_.GetWeakPtr(),
base::Passed(¶ms)));
}
static void RunStartCallback(StartTask* task,
ServiceWorkerStatusCode status) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
StatusCallback callback = task->start_callback_;
task->start_callback_.Reset();
callback.Run(status);
// |task| may be destroyed.
}
private:
void OnProcessAllocated(
scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
ServiceWorkerStatusCode status,
int process_id,
bool is_new_process) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
TRACE_EVENT_ASYNC_END1("ServiceWorker",
"EmbeddedWorkerInstance::ProcessAllocate",
params.get(), "Status", status);
if (status != SERVICE_WORKER_OK) {
DCHECK_EQ(ChildProcessHost::kInvalidUniqueID, process_id);
StatusCallback callback = start_callback_;
start_callback_.Reset();
instance_->OnStartFailed(callback, status);
// |this| may be destroyed.
return;
}
// Notify the instance that a process is allocated.
state_ = ProcessAllocationState::ALLOCATED;
instance_->OnProcessAllocated(make_scoped_ptr(new WorkerProcessHandle(
instance_->context_, instance_->embedded_worker_id(), process_id)));
// Register the instance to DevToolsManager on UI thread.
const int64_t service_worker_version_id = params->service_worker_version_id;
GURL script_url(params->script_url);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(RegisterToWorkerDevToolsManagerOnUI, process_id,
instance_->context_.get(), instance_->context_,
service_worker_version_id, script_url,
base::Bind(&StartTask::OnRegisteredToDevToolsManager,
weak_factory_.GetWeakPtr(), base::Passed(¶ms),
is_new_process)));
}
void OnRegisteredToDevToolsManager(
scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
bool is_new_process,
int worker_devtools_agent_route_id,
bool wait_for_debugger) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
// Notify the instance that it is registered to the devtools manager.
instance_->OnRegisteredToDevToolsManager(
is_new_process, worker_devtools_agent_route_id, wait_for_debugger);
params->worker_devtools_agent_route_id = worker_devtools_agent_route_id;
params->wait_for_debugger = wait_for_debugger;
SendStartWorker(std::move(params));
}
void SendStartWorker(
scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
ServiceWorkerStatusCode status = instance_->registry_->SendStartWorker(
std::move(params), instance_->process_id());
if (status != SERVICE_WORKER_OK) {
StatusCallback callback = start_callback_;
start_callback_.Reset();
instance_->OnStartFailed(callback, status);
// |this| may be destroyed.
return;
}
instance_->OnStartWorkerMessageSent();
// |start_callback_| will be called via RunStartCallback() when the script
// is evaluated.
}
// |instance_| must outlive |this|.
EmbeddedWorkerInstance* instance_;
StatusCallback start_callback_;
ProcessAllocationState state_;
base::WeakPtrFactory<StartTask> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(StartTask);
};
EmbeddedWorkerInstance::~EmbeddedWorkerInstance() {
DCHECK(status_ == STOPPING || status_ == STOPPED) << status_;
devtools_proxy_.reset();
if (registry_->GetWorker(embedded_worker_id_))
registry_->RemoveWorker(process_id(), embedded_worker_id_);
process_handle_.reset();
}
void EmbeddedWorkerInstance::Start(int64_t service_worker_version_id,
const GURL& scope,
const GURL& script_url,
const StatusCallback& callback) {
if (!context_) {
callback.Run(SERVICE_WORKER_ERROR_ABORT);
// |this| may be destroyed by the callback.
return;
}
DCHECK(status_ == STOPPED);
// TODO(horo): If we will see crashes here, we have to find the root cause of
// the invalid version ID. Otherwise change CHECK to DCHECK.
CHECK_NE(service_worker_version_id, kInvalidServiceWorkerVersionId);
start_timing_ = base::TimeTicks::Now();
status_ = STARTING;
starting_phase_ = ALLOCATING_PROCESS;
network_accessed_for_script_ = false;
service_registry_.reset(new ServiceRegistryImpl());
FOR_EACH_OBSERVER(Listener, listener_list_, OnStarting());
scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params(
new EmbeddedWorkerMsg_StartWorker_Params());
params->embedded_worker_id = embedded_worker_id_;
params->service_worker_version_id = service_worker_version_id;
params->scope = scope;
params->script_url = script_url;
params->worker_devtools_agent_route_id = MSG_ROUTING_NONE;
params->wait_for_debugger = false;
params->v8_cache_options = GetV8CacheOptions();
inflight_start_task_.reset(new StartTask(this));
inflight_start_task_->Start(std::move(params), callback);
}
ServiceWorkerStatusCode EmbeddedWorkerInstance::Stop() {
DCHECK(status_ == STARTING || status_ == RUNNING) << status_;
// Abort an inflight start task.
inflight_start_task_.reset();
ServiceWorkerStatusCode status =
registry_->StopWorker(process_id(), embedded_worker_id_);
UMA_HISTOGRAM_ENUMERATION("ServiceWorker.SendStopWorker.Status", status,
SERVICE_WORKER_ERROR_MAX_VALUE);
// StopWorker could fail if we were starting up and don't have a process yet,
// or we can no longer communicate with the process. So just detach.
if (status != SERVICE_WORKER_OK) {
OnDetached();
return status;
}
status_ = STOPPING;
FOR_EACH_OBSERVER(Listener, listener_list_, OnStopping());
return status;
}
void EmbeddedWorkerInstance::StopIfIdle() {
if (devtools_attached_) {
if (devtools_proxy_)
devtools_proxy_->NotifyWorkerStopIgnored();
return;
}
Stop();
}
ServiceWorkerStatusCode EmbeddedWorkerInstance::SendMessage(
const IPC::Message& message) {
DCHECK_NE(kInvalidEmbeddedWorkerThreadId, thread_id_);
if (status_ != RUNNING && status_ != STARTING)
return SERVICE_WORKER_ERROR_IPC_FAILED;
return registry_->Send(process_id(),
new EmbeddedWorkerContextMsg_MessageToWorker(
thread_id_, embedded_worker_id_, message));
}
ServiceRegistry* EmbeddedWorkerInstance::GetServiceRegistry() {
DCHECK(status_ == STARTING || status_ == RUNNING) << status_;
return service_registry_.get();
}
EmbeddedWorkerInstance::EmbeddedWorkerInstance(
base::WeakPtr<ServiceWorkerContextCore> context,
int embedded_worker_id)
: context_(context),
registry_(context->embedded_worker_registry()),
embedded_worker_id_(embedded_worker_id),
status_(STOPPED),
starting_phase_(NOT_STARTING),
thread_id_(kInvalidEmbeddedWorkerThreadId),
devtools_attached_(false),
network_accessed_for_script_(false),
weak_factory_(this) {}
void EmbeddedWorkerInstance::OnProcessAllocated(
scoped_ptr<WorkerProcessHandle> handle) {
DCHECK_EQ(STARTING, status_);
DCHECK(!process_handle_);
process_handle_ = std::move(handle);
starting_phase_ = REGISTERING_TO_DEVTOOLS;
FOR_EACH_OBSERVER(Listener, listener_list_, OnProcessAllocated());
}
void EmbeddedWorkerInstance::OnRegisteredToDevToolsManager(
bool is_new_process,
int worker_devtools_agent_route_id,
bool wait_for_debugger) {
if (worker_devtools_agent_route_id != MSG_ROUTING_NONE) {
DCHECK(!devtools_proxy_);
devtools_proxy_.reset(
new DevToolsProxy(process_id(), worker_devtools_agent_route_id));
}
if (wait_for_debugger) {
// We don't measure the start time when wait_for_debugger flag is set. So we
// set the NULL time here.
start_timing_ = base::TimeTicks();
} else {
DCHECK(!start_timing_.is_null());
if (is_new_process) {
UMA_HISTOGRAM_TIMES("EmbeddedWorkerInstance.NewProcessAllocation",
base::TimeTicks::Now() - start_timing_);
} else {
UMA_HISTOGRAM_TIMES("EmbeddedWorkerInstance.ExistingProcessAllocation",
base::TimeTicks::Now() - start_timing_);
}
UMA_HISTOGRAM_BOOLEAN("EmbeddedWorkerInstance.ProcessCreated",
is_new_process);
// Reset |start_timing_| to measure the time excluding the process
// allocation time.
start_timing_ = base::TimeTicks::Now();
}
}
void EmbeddedWorkerInstance::OnStartWorkerMessageSent() {
starting_phase_ = SENT_START_WORKER;
FOR_EACH_OBSERVER(Listener, listener_list_, OnStartWorkerMessageSent());
}
void EmbeddedWorkerInstance::OnReadyForInspection() {
if (devtools_proxy_)
devtools_proxy_->NotifyWorkerReadyForInspection();
}
void EmbeddedWorkerInstance::OnScriptReadStarted() {
starting_phase_ = SCRIPT_READ_STARTED;
}
void EmbeddedWorkerInstance::OnScriptReadFinished() {
starting_phase_ = SCRIPT_READ_FINISHED;
}
void EmbeddedWorkerInstance::OnScriptLoaded() {
FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoaded());
starting_phase_ = SCRIPT_LOADED;
}
void EmbeddedWorkerInstance::OnThreadStarted(int thread_id) {
starting_phase_ = THREAD_STARTED;
if (!start_timing_.is_null()) {
if (network_accessed_for_script_) {
UMA_HISTOGRAM_TIMES("EmbeddedWorkerInstance.ScriptLoadWithNetworkAccess",
base::TimeTicks::Now() - start_timing_);
} else {
UMA_HISTOGRAM_TIMES(
"EmbeddedWorkerInstance.ScriptLoadWithoutNetworkAccess",
base::TimeTicks::Now() - start_timing_);
}
// Reset |start_timing_| to measure the time excluding the process
// allocation time and the script loading time.
start_timing_ = base::TimeTicks::Now();
}
thread_id_ = thread_id;
FOR_EACH_OBSERVER(Listener, listener_list_, OnThreadStarted());
mojo::InterfaceProviderPtr exposed_services;
service_registry_->Bind(GetProxy(&exposed_services));
mojo::InterfaceProviderPtr services;
mojo::InterfaceRequest<mojo::InterfaceProvider> services_request =
GetProxy(&services);
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(SetupMojoOnUIThread, process_id(), thread_id_,
base::Passed(&services_request),
base::Passed(exposed_services.PassInterface())));
service_registry_->BindRemoteServiceProvider(std::move(services));
}
void EmbeddedWorkerInstance::OnScriptLoadFailed() {
FOR_EACH_OBSERVER(Listener, listener_list_, OnScriptLoadFailed());
}
void EmbeddedWorkerInstance::OnScriptEvaluated(bool success) {
if (!inflight_start_task_)
return;
DCHECK_EQ(STARTING, status_);
starting_phase_ = SCRIPT_EVALUATED;
if (success && !start_timing_.is_null()) {
UMA_HISTOGRAM_TIMES("EmbeddedWorkerInstance.ScriptEvaluate",
base::TimeTicks::Now() - start_timing_);
}
base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr();
StartTask::RunStartCallback(
inflight_start_task_.get(),
success ? SERVICE_WORKER_OK
: SERVICE_WORKER_ERROR_SCRIPT_EVALUATE_FAILED);
// |this| may be destroyed by the callback.
}
void EmbeddedWorkerInstance::OnStarted() {
// Stop is requested before OnStarted is sent back from the worker.
if (status_ == STOPPING)
return;
DCHECK(status_ == STARTING);
status_ = RUNNING;
inflight_start_task_.reset();
FOR_EACH_OBSERVER(Listener, listener_list_, OnStarted());
}
void EmbeddedWorkerInstance::OnStopped() {
Status old_status = status_;
ReleaseProcess();
FOR_EACH_OBSERVER(Listener, listener_list_, OnStopped(old_status));
}
void EmbeddedWorkerInstance::OnDetached() {
Status old_status = status_;
ReleaseProcess();
FOR_EACH_OBSERVER(Listener, listener_list_, OnDetached(old_status));
}
void EmbeddedWorkerInstance::Detach() {
registry_->RemoveWorker(process_id(), embedded_worker_id_);
OnDetached();
}
base::WeakPtr<EmbeddedWorkerInstance> EmbeddedWorkerInstance::AsWeakPtr() {
return weak_factory_.GetWeakPtr();
}
bool EmbeddedWorkerInstance::OnMessageReceived(const IPC::Message& message) {
ListenerList::Iterator it(&listener_list_);
while (Listener* listener = it.GetNext()) {
if (listener->OnMessageReceived(message))
return true;
}
return false;
}
void EmbeddedWorkerInstance::OnReportException(
const base::string16& error_message,
int line_number,
int column_number,
const GURL& source_url) {
FOR_EACH_OBSERVER(
Listener,
listener_list_,
OnReportException(error_message, line_number, column_number, source_url));
}
void EmbeddedWorkerInstance::OnReportConsoleMessage(
int source_identifier,
int message_level,
const base::string16& message,
int line_number,
const GURL& source_url) {
FOR_EACH_OBSERVER(
Listener,
listener_list_,
OnReportConsoleMessage(
source_identifier, message_level, message, line_number, source_url));
}
int EmbeddedWorkerInstance::process_id() const {
if (process_handle_)
return process_handle_->process_id();
return ChildProcessHost::kInvalidUniqueID;
}
int EmbeddedWorkerInstance::worker_devtools_agent_route_id() const {
if (devtools_proxy_)
return devtools_proxy_->agent_route_id();
return MSG_ROUTING_NONE;
}
MessagePortMessageFilter* EmbeddedWorkerInstance::message_port_message_filter()
const {
return registry_->MessagePortMessageFilterForProcess(process_id());
}
void EmbeddedWorkerInstance::AddListener(Listener* listener) {
listener_list_.AddObserver(listener);
}
void EmbeddedWorkerInstance::RemoveListener(Listener* listener) {
listener_list_.RemoveObserver(listener);
}
void EmbeddedWorkerInstance::OnNetworkAccessedForScriptLoad() {
starting_phase_ = SCRIPT_DOWNLOADING;
network_accessed_for_script_ = true;
}
void EmbeddedWorkerInstance::ReleaseProcess() {
// Abort an inflight start task.
inflight_start_task_.reset();
devtools_proxy_.reset();
process_handle_.reset();
status_ = STOPPED;
thread_id_ = kInvalidEmbeddedWorkerThreadId;
service_registry_.reset();
}
void EmbeddedWorkerInstance::OnStartFailed(const StatusCallback& callback,
ServiceWorkerStatusCode status) {
Status old_status = status_;
ReleaseProcess();
base::WeakPtr<EmbeddedWorkerInstance> weak_this = weak_factory_.GetWeakPtr();
callback.Run(status);
if (weak_this && old_status != STOPPED)
FOR_EACH_OBSERVER(Listener, weak_this->listener_list_,
OnStopped(old_status));
}
// static
std::string EmbeddedWorkerInstance::StatusToString(Status status) {
switch (status) {
case STOPPED:
return "STOPPED";
case STARTING:
return "STARTING";
case RUNNING:
return "RUNNING";
case STOPPING:
return "STOPPING";
}
NOTREACHED() << status;
return std::string();
}
// static
std::string EmbeddedWorkerInstance::StartingPhaseToString(StartingPhase phase) {
switch (phase) {
case NOT_STARTING:
return "Not in STARTING status";
case ALLOCATING_PROCESS:
return "Allocating process";
case REGISTERING_TO_DEVTOOLS:
return "Registering to DevTools";
case SENT_START_WORKER:
return "Sent StartWorker message to renderer";
case SCRIPT_DOWNLOADING:
return "Script downloading";
case SCRIPT_LOADED:
return "Script loaded";
case SCRIPT_EVALUATED:
return "Script evaluated";
case THREAD_STARTED:
return "Thread started";
case SCRIPT_READ_STARTED:
return "Script read started";
case SCRIPT_READ_FINISHED:
return "Script read finished";
case STARTING_PHASE_MAX_VALUE:
NOTREACHED();
}
NOTREACHED() << phase;
return std::string();
}
} // namespace content
|