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

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

Issue 1110103003: Initial implementation of stashed message ports, content side (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 5 years, 7 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
OLDNEW
(Empty)
1 // Copyright 2015 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.
4
5 #include "content/browser/service_worker/stashed_port_manager.h"
6
7 #include "content/browser/message_port_message_filter.h"
8 #include "content/browser/message_port_service.h"
9 #include "content/browser/service_worker/service_worker_context_wrapper.h"
10 #include "content/public/browser/browser_thread.h"
11
12 namespace content {
13
14 struct StashedPortManager::StashedPort {
scheib 2015/05/06 22:19:46 This can be wrapped with anon namespace I think, i
Marijn Kruisselbrink 2015/05/12 06:57:33 I don't think it can (and it makes more sense as a
15 StashedPort();
16 ~StashedPort();
17
18 int64 service_worker_registration_id;
19 GURL service_worker_origin;
20
21 int message_port_id;
22 base::string16 name;
23
24 // The |route_id| to the port in the process this port currently lives in, or
25 // MSG_ROUTING_NONE if the port isn't currently associated with any running
26 // ServiceWorkerVersion.
27 int route_id;
28 // The running ServiceWorkerVersion this port is currently associated with.
29 // Set to null if the port does not currently exist in a running worker.
30 ServiceWorkerVersion* service_worker;
31 };
32
33 StashedPortManager::StashedPort::StashedPort() {
34 }
35 StashedPortManager::StashedPort::~StashedPort() {
36 }
37
38 StashedPortManager::StashedPortManager(
39 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context)
40 : service_worker_context_(service_worker_context) {
41 DCHECK_CURRENTLY_ON(BrowserThread::UI);
42 }
43
44 StashedPortManager::~StashedPortManager() {
45 }
46
47 void StashedPortManager::Init() {
48 DCHECK_CURRENTLY_ON(BrowserThread::UI);
49 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
50 base::Bind(&StashedPortManager::InitOnIO, this));
51 }
52
53 void StashedPortManager::Shutdown() {
54 DCHECK_CURRENTLY_ON(BrowserThread::UI);
55 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
56 base::Bind(&StashedPortManager::ShutdownOnIO, this));
57 }
58
59 void StashedPortManager::InitOnIO() {
60 DCHECK_CURRENTLY_ON(BrowserThread::IO);
61 // TODO(mek): Restore ports between service workers from storage.
62 }
63
64 void StashedPortManager::ShutdownOnIO() {
65 DCHECK_CURRENTLY_ON(BrowserThread::IO);
66 for (ServiceWorkerVersion* worker : observing_service_workers_) {
67 worker->RemoveListener(this);
68 }
69 observing_service_workers_.clear();
70 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(this);
71 }
72
73 void StashedPortManager::AddPort(ServiceWorkerVersion* service_worker,
74 int message_port_id,
75 const base::string16& name) {
76 DCHECK_CURRENTLY_ON(BrowserThread::IO);
77 DCHECK(ports_.find(message_port_id) == ports_.end());
78
79 StashedPort& port = ports_[message_port_id];
80 port.service_worker_registration_id = service_worker->registration_id();
81 port.service_worker_origin = service_worker->scope().GetOrigin();
82 port.message_port_id = message_port_id;
83 port.name = name;
84
85 // If service worker is currently running, get its current route id and
86 // start observing the worker.
87 if (service_worker->running_status() == ServiceWorkerVersion::RUNNING) {
88 MessagePortService::GetInstance()->GetMessagePortInfo(
89 message_port_id, nullptr, &port.route_id);
90 port.service_worker = service_worker;
91 if (observing_service_workers_.insert(service_worker).second) {
92 // Service Worker wasn't already being observed
93 service_worker->AddListener(this);
94 }
95 } else {
96 port.route_id = MSG_ROUTING_NONE;
97 port.service_worker = nullptr;
98 }
99
100 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id, this,
101 message_port_id);
102
103 // If service worker is not currently running, all messages to this port
104 // should be held in the browser process.
105 if (port.route_id == MSG_ROUTING_NONE)
106 MessagePortService::GetInstance()->HoldMessages(message_port_id);
107 }
108
109 void StashedPortManager::SendMessage(
110 int message_port_id,
111 const MessagePortMessage& message,
112 const std::vector<TransferredMessagePort>& sent_message_ports) {
113 DCHECK_CURRENTLY_ON(BrowserThread::IO);
114 DCHECK(ports_.find(message_port_id) != ports_.end());
115
116 const StashedPort& port = ports_[message_port_id];
117 DCHECK(port.service_worker && port.route_id != MSG_ROUTING_NONE);
118 port.service_worker->embedded_worker()
119 ->message_port_message_filter()
120 ->SendMessage(port.route_id, message, sent_message_ports);
121 }
122
123 void StashedPortManager::MessageWasHeld(int message_port_id) {
124 DCHECK_CURRENTLY_ON(BrowserThread::IO);
125 DCHECK(ports_.find(message_port_id) != ports_.end());
126
127 const StashedPort& port = ports_[message_port_id];
128 service_worker_context_->FindRegistrationForId(
129 port.service_worker_registration_id, port.service_worker_origin,
130 base::Bind(&StashedPortManager::TransferMessagePort, this,
scheib 2015/05/06 22:19:46 On first read at least it's not obvious why it's t
Marijn Kruisselbrink 2015/05/12 06:57:33 I tried to explain this a bit better
131 message_port_id));
132 }
133
134 void StashedPortManager::SendMessagesAreQueued(int message_port_id) {
135 DCHECK_CURRENTLY_ON(BrowserThread::IO);
136 DCHECK(ports_.find(message_port_id) != ports_.end());
137 // TODO(mek): Support transfering a stashed message port to a different
138 // process.
139 }
140
141 void StashedPortManager::OnRunningStateChanged(
142 ServiceWorkerVersion* service_worker) {
143 DCHECK_CURRENTLY_ON(BrowserThread::IO);
144 // Listener is only added to service workers whose running state is RUNNING,
145 // so running state here should only ever be able to be STOPPING or STOPPED.
146 DCHECK(service_worker->running_status() == ServiceWorkerVersion::STOPPING ||
147 service_worker->running_status() == ServiceWorkerVersion::STOPPED);
148
149 // Hold messages for all ports associated with the no longer running worker.
150 for (auto& port : ports_) {
151 if (port.second.service_worker != service_worker)
152 continue;
153 port.second.route_id = MSG_ROUTING_NONE;
154 port.second.service_worker = nullptr;
155 MessagePortService::GetInstance()->HoldMessages(port.first);
156 }
157 observing_service_workers_.erase(service_worker);
158 service_worker->RemoveListener(this);
159 }
160
161 void StashedPortManager::TransferMessagePort(
162 int message_port_id,
163 ServiceWorkerStatusCode service_worker_status,
164 const scoped_refptr<ServiceWorkerRegistration>&
165 service_worker_registration) {
166 DCHECK_CURRENTLY_ON(BrowserThread::IO);
167 auto it = ports_.find(message_port_id);
168 if (it == ports_.end()) {
169 // Port no longer owned by StashedPortManager, no need to do anything else.
170 return;
171 }
172 const StashedPort& port = it->second;
173
174 if (service_worker_status != SERVICE_WORKER_OK) {
175 // TODO(mek): SW no longer exists, somehow handle this.
176 return;
177 }
178
179 // TODO(mek): Figure out and spec correct logic for determining which version
180 // of a service worker a port should be associated with.
181 scoped_refptr<ServiceWorkerVersion> version =
182 service_worker_registration->active_version();
183 if (!version)
184 version = service_worker_registration->waiting_version();
185 if (!version)
186 version = service_worker_registration->installing_version();
187 if (!version) {
188 // TODO(mek): Do something when no version is found.
189 return;
190 }
191
192 std::vector<TransferredMessagePort> ports(1);
193 std::vector<base::string16> port_names(1);
194 ports[0].id = port.message_port_id;
195 port_names[0] = port.name;
196 version->SendStashedMessagePorts(
197 ports, port_names,
198 base::Bind(&StashedPortManager::TransferredPorts, this, version, ports));
199 }
200
201 void StashedPortManager::TransferredPorts(
scheib 2015/05/06 22:19:45 OnTransferredPorts?
Marijn Kruisselbrink 2015/05/12 06:57:33 Done.
202 const scoped_refptr<ServiceWorkerVersion>& service_worker,
203 const std::vector<TransferredMessagePort>& ports,
204 ServiceWorkerStatusCode service_worker_status,
205 const std::vector<int>& route_ids) {
206 DCHECK_CURRENTLY_ON(BrowserThread::IO);
207 if (service_worker_status != SERVICE_WORKER_OK) {
208 // TODO(mek): Handle failure.
209 return;
210 }
211
212 if (service_worker->running_status() != ServiceWorkerVersion::RUNNING) {
213 // TODO(mek): Figure out what to do in this case.
214 return;
215 }
216
217 // Port was transfered to a service worker, start observing the worker so
218 // messages can be held when the worker stops running.
219 if (observing_service_workers_.insert(service_worker.get()).second) {
220 // Service Worker wasn't already being observed
221 service_worker->AddListener(this);
222 }
223
224 // Update ports with the new route ids and service worker version.
225 DCHECK(ports.size() == route_ids.size());
226 for (size_t i = 0; i < ports.size(); ++i) {
227 DCHECK(ports_.find(ports[i].id) != ports_.end());
228 StashedPort& port = ports_[ports[i].id];
229 port.route_id = route_ids[i];
230 port.service_worker = service_worker.get();
231 }
232 }
233
234 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698