Chromium Code Reviews| OLD | NEW |
|---|---|
| (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 { | |
| 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 } | |
|
kinuko
2015/05/19 09:01:45
nit: please have one empty line between methods
Marijn Kruisselbrink
2015/05/19 17:40:18
Done.
| |
| 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 // Messages are queued in the browser process when the stashed port isn't | |
| 128 // currently available in any running renderer processes. Try to transfer | |
| 129 // the port to a (potentially newly started) service worker to enable | |
| 130 // message delivery. | |
| 131 const StashedPort& port = ports_[message_port_id]; | |
| 132 service_worker_context_->FindRegistrationForId( | |
| 133 port.service_worker_registration_id, port.service_worker_origin, | |
| 134 base::Bind(&StashedPortManager::TransferMessagePort, this, | |
| 135 message_port_id)); | |
| 136 } | |
| 137 | |
| 138 void StashedPortManager::SendMessagesAreQueued(int message_port_id) { | |
| 139 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 140 DCHECK(ports_.find(message_port_id) != ports_.end()); | |
| 141 // TODO(mek): Support transfering a stashed message port to a different | |
| 142 // process. | |
| 143 } | |
| 144 | |
| 145 void StashedPortManager::OnRunningStateChanged( | |
| 146 ServiceWorkerVersion* service_worker) { | |
| 147 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 148 // Listener is only added to service workers whose running state is RUNNING, | |
| 149 // so running state here should only ever be able to be STOPPING or STOPPED. | |
| 150 DCHECK(service_worker->running_status() == ServiceWorkerVersion::STOPPING || | |
| 151 service_worker->running_status() == ServiceWorkerVersion::STOPPED); | |
| 152 | |
| 153 // Hold messages for all ports associated with the no longer running worker. | |
| 154 for (auto& port : ports_) { | |
| 155 if (port.second.service_worker != service_worker) | |
| 156 continue; | |
| 157 port.second.route_id = MSG_ROUTING_NONE; | |
| 158 port.second.service_worker = nullptr; | |
| 159 MessagePortService::GetInstance()->HoldMessages(port.first); | |
| 160 } | |
| 161 observing_service_workers_.erase(service_worker); | |
|
kinuko
2015/05/15 06:59:18
Is it guaranteed that OnRunningStateChanged is alw
Marijn Kruisselbrink
2015/05/18 22:19:36
Actually I'm not sure. I misread a comment in ~Ser
kinuko
2015/05/19 09:01:45
My guess is it's not guaranteed, but falken@ would
Marijn Kruisselbrink
2015/05/19 17:40:19
That's also my conclusion, yes.
| |
| 162 service_worker->RemoveListener(this); | |
| 163 } | |
| 164 | |
| 165 void StashedPortManager::TransferMessagePort( | |
| 166 int message_port_id, | |
| 167 ServiceWorkerStatusCode service_worker_status, | |
| 168 const scoped_refptr<ServiceWorkerRegistration>& | |
| 169 service_worker_registration) { | |
| 170 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 171 auto it = ports_.find(message_port_id); | |
| 172 if (it == ports_.end()) { | |
| 173 // Port no longer owned by StashedPortManager, no need to do anything else. | |
| 174 return; | |
| 175 } | |
| 176 const StashedPort& port = it->second; | |
| 177 | |
| 178 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 179 // TODO(mek): SW no longer exists, somehow handle this. | |
| 180 return; | |
| 181 } | |
| 182 | |
| 183 // TODO(mek): Figure out and spec correct logic for determining which version | |
| 184 // of a service worker a port should be associated with. | |
| 185 scoped_refptr<ServiceWorkerVersion> version = | |
| 186 service_worker_registration->active_version(); | |
| 187 if (!version) | |
| 188 version = service_worker_registration->waiting_version(); | |
| 189 if (!version) | |
| 190 version = service_worker_registration->installing_version(); | |
| 191 if (!version) { | |
| 192 // TODO(mek): Do something when no version is found. | |
| 193 return; | |
| 194 } | |
| 195 | |
| 196 std::vector<TransferredMessagePort> ports(1); | |
| 197 std::vector<base::string16> port_names(1); | |
| 198 ports[0].id = port.message_port_id; | |
| 199 port_names[0] = port.name; | |
| 200 version->SendStashedMessagePorts( | |
| 201 ports, port_names, base::Bind(&StashedPortManager::OnPortsTransferred, | |
| 202 this, version, ports)); | |
| 203 } | |
| 204 | |
| 205 void StashedPortManager::OnPortsTransferred( | |
| 206 const scoped_refptr<ServiceWorkerVersion>& service_worker, | |
| 207 const std::vector<TransferredMessagePort>& ports, | |
| 208 ServiceWorkerStatusCode service_worker_status, | |
| 209 const std::vector<int>& route_ids) { | |
| 210 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 211 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 212 // TODO(mek): Handle failure. | |
| 213 return; | |
| 214 } | |
| 215 | |
| 216 if (service_worker->running_status() != ServiceWorkerVersion::RUNNING) { | |
| 217 // TODO(mek): Figure out what to do in this case. | |
| 218 return; | |
| 219 } | |
| 220 | |
| 221 // Port was transfered to a service worker, start observing the worker so | |
| 222 // messages can be held when the worker stops running. | |
| 223 if (observing_service_workers_.insert(service_worker.get()).second) { | |
| 224 // Service Worker wasn't already being observed | |
| 225 service_worker->AddListener(this); | |
| 226 } | |
| 227 | |
| 228 // Update ports with the new route ids and service worker version. | |
| 229 DCHECK(ports.size() == route_ids.size()); | |
| 230 for (size_t i = 0; i < ports.size(); ++i) { | |
| 231 DCHECK(ports_.find(ports[i].id) != ports_.end()); | |
| 232 StashedPort& port = ports_[ports[i].id]; | |
| 233 port.route_id = route_ids[i]; | |
| 234 port.service_worker = service_worker.get(); | |
| 235 } | |
| 236 } | |
| 237 | |
| 238 } // namespace content | |
| OLD | NEW |