Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(118)

Side by Side Diff: content/browser/service_worker/service_worker_version.cc

Issue 145033012: Make ServiceWorkerVersion::{Start,Stop}Worker take callback (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 6 years, 11 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
« no previous file with comments | « content/browser/service_worker/service_worker_version.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include "base/stl_util.h" 7 #include "base/stl_util.h"
8 #include "content/browser/service_worker/embedded_worker_instance.h" 8 #include "content/browser/service_worker/embedded_worker_instance.h"
9 #include "content/browser/service_worker/embedded_worker_registry.h" 9 #include "content/browser/service_worker/embedded_worker_registry.h"
10 #include "content/browser/service_worker/service_worker_context_core.h" 10 #include "content/browser/service_worker/service_worker_context_core.h"
11 #include "content/browser/service_worker/service_worker_registration.h" 11 #include "content/browser/service_worker/service_worker_registration.h"
12 #include "content/common/service_worker/service_worker_messages.h" 12 #include "content/common/service_worker/service_worker_messages.h"
13 13
14 namespace content { 14 namespace content {
15 15
16 namespace {
17
18 void RunSoon(const base::Closure& callback) {
19 base::MessageLoop::current()->PostTask(FROM_HERE, callback);
20 }
21
22 } // namespace
23
24 //-------------------------------------------------------------------
25 class ServiceWorkerVersion::WorkerObserverBase
26 : public EmbeddedWorkerInstance::Observer {
27 public:
28 virtual ~WorkerObserverBase() {
29 version_->embedded_worker_->RemoveObserver(this);
30 }
31
32 virtual void OnStarted() OVERRIDE { NOTREACHED(); }
33 virtual void OnStopped() OVERRIDE { NOTREACHED(); }
34 virtual void OnMessageReceived(const IPC::Message& message) OVERRIDE {
35 NOTREACHED();
36 }
37
38 protected:
39 explicit WorkerObserverBase(ServiceWorkerVersion* version)
40 : version_(version) {
41 version_->embedded_worker_->AddObserver(this);
42 }
43
44 ServiceWorkerVersion* version() { return version_; }
45
46 private:
47 ServiceWorkerVersion* version_;
48 };
49
50
51 // Observer class that is attached while the worker is starting.
52 class ServiceWorkerVersion::StartObserver : public WorkerObserverBase {
53 public:
54 typedef ServiceWorkerVersion::StatusCallback StatusCallback;
55
56 StartObserver(ServiceWorkerVersion* version, const StatusCallback& callback)
57 : WorkerObserverBase(version),
58 callback_(callback) {}
59 virtual ~StartObserver() {}
60
61 virtual void OnStarted() OVERRIDE {
62 Completed(SERVICE_WORKER_OK);
63 }
64
65 virtual void OnStopped() OVERRIDE {
66 Completed(SERVICE_WORKER_ERROR_START_WORKER_FAILED);
67 }
68
69 private:
70 void Completed(ServiceWorkerStatusCode status) {
71 StatusCallback callback = callback_;
72 version()->observer_.reset();
73 callback.Run(status);
74 }
75
76 StatusCallback callback_;
77 DISALLOW_COPY_AND_ASSIGN(StartObserver);
78 };
79
80 // Observer class that is attached while the worker is stopping.
81 class ServiceWorkerVersion::StopObserver : public WorkerObserverBase {
82 public:
83 typedef ServiceWorkerVersion::StatusCallback StatusCallback;
84 StopObserver(ServiceWorkerVersion* version, const StatusCallback& callback)
85 : WorkerObserverBase(version),
86 callback_(callback) {}
87 virtual ~StopObserver() {}
88
89 virtual void OnStopped() OVERRIDE {
90 StatusCallback callback = callback_;
91 version()->observer_.reset();
92 callback.Run(SERVICE_WORKER_OK);
93 }
94
95 virtual void OnMessageReceived(const IPC::Message& message) OVERRIDE {
96 // We just ignore messages, as we're stopping.
97 }
98
99 private:
100 StatusCallback callback_;
101 DISALLOW_COPY_AND_ASSIGN(StopObserver);
102 };
103
104 //-------------------------------------------------------------------
16 ServiceWorkerVersion::ServiceWorkerVersion( 105 ServiceWorkerVersion::ServiceWorkerVersion(
17 ServiceWorkerRegistration* registration, 106 ServiceWorkerRegistration* registration,
18 EmbeddedWorkerRegistry* worker_registry, 107 EmbeddedWorkerRegistry* worker_registry,
19 int64 version_id) 108 int64 version_id)
20 : version_id_(version_id), 109 : version_id_(version_id),
21 is_shutdown_(false), 110 is_shutdown_(false),
22 registration_(registration) { 111 registration_(registration) {
23 if (worker_registry) 112 if (worker_registry)
24 embedded_worker_ = worker_registry->CreateWorker(); 113 embedded_worker_ = worker_registry->CreateWorker();
25 } 114 }
26 115
27 ServiceWorkerVersion::~ServiceWorkerVersion() { DCHECK(is_shutdown_); } 116 ServiceWorkerVersion::~ServiceWorkerVersion() { DCHECK(is_shutdown_); }
28 117
29 void ServiceWorkerVersion::Shutdown() { 118 void ServiceWorkerVersion::Shutdown() {
30 is_shutdown_ = true; 119 is_shutdown_ = true;
31 registration_ = NULL; 120 registration_ = NULL;
32 embedded_worker_.reset(); 121 embedded_worker_.reset();
33 } 122 }
34 123
35 void ServiceWorkerVersion::StartWorker() { 124 void ServiceWorkerVersion::StartWorker(const StatusCallback& callback) {
36 DCHECK(!is_shutdown_); 125 DCHECK(!is_shutdown_);
37 DCHECK(registration_); 126 DCHECK(registration_);
38 embedded_worker_->Start(version_id_, registration_->script_url()); 127 DCHECK(!observer_);
128 if (status() == RUNNING) {
129 RunSoon(base::Bind(callback, SERVICE_WORKER_OK));
130 return;
131 }
132 observer_.reset(new StartObserver(this, callback));
133 const bool started = embedded_worker_->Start(
134 version_id_,
135 registration_->script_url());
136 if (!started) {
137 observer_.reset();
138 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED));
139 }
39 } 140 }
40 141
41 void ServiceWorkerVersion::StopWorker() { 142 void ServiceWorkerVersion::StopWorker(const StatusCallback& callback) {
42 DCHECK(!is_shutdown_); 143 DCHECK(!is_shutdown_);
43 embedded_worker_->Stop(); 144 DCHECK(!observer_);
145 if (status() == STOPPED) {
146 RunSoon(base::Bind(callback, SERVICE_WORKER_OK));
147 return;
148 }
149 observer_.reset(new StopObserver(this, callback));
150 const bool stopped = embedded_worker_->Stop();
151 if (!stopped) {
152 observer_.reset();
153 RunSoon(base::Bind(callback, SERVICE_WORKER_ERROR_FAILED));
154 }
44 } 155 }
45 156
46 bool ServiceWorkerVersion::DispatchFetchEvent( 157 bool ServiceWorkerVersion::DispatchFetchEvent(
47 const ServiceWorkerFetchRequest& request) { 158 const ServiceWorkerFetchRequest& request) {
48 if (embedded_worker_->status() != EmbeddedWorkerInstance::RUNNING) 159 if (status() != RUNNING)
49 return false; 160 return false;
50 return embedded_worker_->SendMessage( 161 return embedded_worker_->SendMessage(
51 ServiceWorkerMsg_FetchEvent(request)); 162 ServiceWorkerMsg_FetchEvent(request));
52 } 163 }
53 164
54 void ServiceWorkerVersion::OnAssociateProvider( 165 void ServiceWorkerVersion::AddProcessToWorker(int process_id) {
55 ServiceWorkerProviderHost* provider_host) {
56 DCHECK(!is_shutdown_); 166 DCHECK(!is_shutdown_);
57 embedded_worker_->AddProcessReference(provider_host->process_id()); 167 embedded_worker_->AddProcessReference(process_id);
58 } 168 }
59 169
60 void ServiceWorkerVersion::OnUnassociateProvider( 170 void ServiceWorkerVersion::RemoveProcessToWorker(int process_id) {
61 ServiceWorkerProviderHost* provider_host) { 171 embedded_worker_->ReleaseProcessReference(process_id);
62 embedded_worker_->ReleaseProcessReference(provider_host->process_id());
63 } 172 }
64 173
65 } // namespace content 174 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698