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 } | |
| 35 | |
| 36 StashedPortManager::StashedPort::~StashedPort() { | |
| 37 } | |
| 38 | |
| 39 StashedPortManager::StashedPortManager( | |
| 40 const scoped_refptr<ServiceWorkerContextWrapper>& service_worker_context) | |
| 41 : service_worker_context_(service_worker_context) { | |
| 42 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 43 } | |
| 44 | |
| 45 StashedPortManager::~StashedPortManager() { | |
| 46 } | |
| 47 | |
| 48 void StashedPortManager::Init() { | |
| 49 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 50 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 51 base::Bind(&StashedPortManager::InitOnIO, this)); | |
| 52 } | |
| 53 | |
| 54 void StashedPortManager::Shutdown() { | |
| 55 DCHECK_CURRENTLY_ON(BrowserThread::UI); | |
| 56 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, | |
| 57 base::Bind(&StashedPortManager::ShutdownOnIO, this)); | |
| 58 } | |
| 59 | |
| 60 void StashedPortManager::InitOnIO() { | |
| 61 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 62 // TODO(mek): Restore ports between service workers from storage. | |
| 63 } | |
| 64 | |
| 65 void StashedPortManager::ShutdownOnIO() { | |
| 66 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 67 for (ServiceWorkerVersion* worker : observed_service_workers_) { | |
| 68 worker->RemoveListener(this); | |
| 69 } | |
| 70 observed_service_workers_.clear(); | |
| 71 MessagePortService::GetInstance()->OnMessagePortDelegateClosing(this); | |
| 72 } | |
| 73 | |
| 74 void StashedPortManager::AddPort(ServiceWorkerVersion* service_worker, | |
| 75 int message_port_id, | |
| 76 const base::string16& name) { | |
| 77 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 78 DCHECK(ports_.find(message_port_id) == ports_.end()); | |
| 79 | |
| 80 StashedPort& port = ports_[message_port_id]; | |
| 81 port.service_worker_registration_id = service_worker->registration_id(); | |
| 82 port.service_worker_origin = service_worker->scope().GetOrigin(); | |
| 83 port.message_port_id = message_port_id; | |
| 84 port.name = name; | |
| 85 | |
| 86 // If service worker is currently running, get its current route id and | |
| 87 // start observing the worker. | |
| 88 if (service_worker->running_status() == ServiceWorkerVersion::RUNNING) { | |
| 89 MessagePortService::GetInstance()->GetMessagePortInfo( | |
| 90 message_port_id, nullptr, &port.route_id); | |
| 91 port.service_worker = service_worker; | |
| 92 if (observed_service_workers_.insert(service_worker).second) { | |
| 93 // Service Worker wasn't already being observed | |
| 94 service_worker->AddListener(this); | |
| 95 } | |
| 96 } else { | |
| 97 port.route_id = MSG_ROUTING_NONE; | |
| 98 port.service_worker = nullptr; | |
| 99 } | |
| 100 | |
| 101 MessagePortService::GetInstance()->UpdateMessagePort(message_port_id, this, | |
| 102 message_port_id); | |
| 103 | |
| 104 // If service worker is not currently running, all messages to this port | |
| 105 // should be held in the browser process. | |
| 106 if (port.route_id == MSG_ROUTING_NONE) | |
| 107 MessagePortService::GetInstance()->HoldMessages(message_port_id); | |
| 108 } | |
| 109 | |
| 110 void StashedPortManager::SendMessage( | |
| 111 int message_port_id, | |
| 112 const MessagePortMessage& message, | |
| 113 const std::vector<TransferredMessagePort>& sent_message_ports) { | |
| 114 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 115 DCHECK(ports_.find(message_port_id) != ports_.end()); | |
| 116 | |
| 117 const StashedPort& port = ports_[message_port_id]; | |
| 118 DCHECK(port.service_worker && port.route_id != MSG_ROUTING_NONE); | |
|
Avi (use Gerrit)
2015/05/20 03:02:10
break into a DCHECK and DCHECK_NE
| |
| 119 port.service_worker->embedded_worker() | |
| 120 ->message_port_message_filter() | |
| 121 ->SendMessage(port.route_id, message, sent_message_ports); | |
| 122 } | |
| 123 | |
| 124 void StashedPortManager::MessageWasHeld(int message_port_id) { | |
| 125 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 126 DCHECK(ports_.find(message_port_id) != ports_.end()); | |
| 127 | |
| 128 // Messages are queued in the browser process when the stashed port isn't | |
| 129 // currently available in any running renderer processes. Try to transfer | |
| 130 // the port to a (potentially newly started) service worker to enable | |
| 131 // message delivery. | |
| 132 const StashedPort& port = ports_[message_port_id]; | |
| 133 service_worker_context_->FindRegistrationForId( | |
| 134 port.service_worker_registration_id, port.service_worker_origin, | |
| 135 base::Bind(&StashedPortManager::TransferMessagePort, this, | |
| 136 message_port_id)); | |
| 137 } | |
| 138 | |
| 139 void StashedPortManager::SendMessagesAreQueued(int message_port_id) { | |
| 140 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 141 DCHECK(ports_.find(message_port_id) != ports_.end()); | |
| 142 // TODO(mek): Support transfering a stashed message port to a different | |
| 143 // process. | |
| 144 } | |
| 145 | |
| 146 void StashedPortManager::OnRunningStateChanged( | |
| 147 ServiceWorkerVersion* service_worker) { | |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 149 // Listener is only added to service workers whose running state is RUNNING, | |
| 150 // so running state here should only ever be able to be STOPPING or STOPPED. | |
| 151 DCHECK(service_worker->running_status() == ServiceWorkerVersion::STOPPING || | |
| 152 service_worker->running_status() == ServiceWorkerVersion::STOPPED); | |
| 153 | |
| 154 // Hold messages for all ports associated with the no longer running worker. | |
| 155 for (auto& port : ports_) { | |
| 156 if (port.second.service_worker != service_worker) | |
| 157 continue; | |
| 158 port.second.route_id = MSG_ROUTING_NONE; | |
| 159 port.second.service_worker = nullptr; | |
| 160 MessagePortService::GetInstance()->HoldMessages(port.first); | |
| 161 } | |
| 162 observed_service_workers_.erase(service_worker); | |
| 163 service_worker->RemoveListener(this); | |
| 164 } | |
| 165 | |
| 166 void StashedPortManager::TransferMessagePort( | |
| 167 int message_port_id, | |
| 168 ServiceWorkerStatusCode service_worker_status, | |
| 169 const scoped_refptr<ServiceWorkerRegistration>& | |
| 170 service_worker_registration) { | |
| 171 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 172 auto it = ports_.find(message_port_id); | |
| 173 if (it == ports_.end()) { | |
| 174 // Port no longer owned by StashedPortManager, no need to do anything else. | |
| 175 return; | |
| 176 } | |
| 177 const StashedPort& port = it->second; | |
| 178 | |
| 179 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 180 // TODO(mek): SW no longer exists, somehow handle this. | |
| 181 return; | |
| 182 } | |
| 183 | |
| 184 // TODO(mek): Figure out and spec correct logic for determining which version | |
| 185 // of a service worker a port should be associated with. | |
| 186 scoped_refptr<ServiceWorkerVersion> version = | |
| 187 service_worker_registration->active_version(); | |
| 188 if (!version) | |
| 189 version = service_worker_registration->waiting_version(); | |
| 190 if (!version) | |
| 191 version = service_worker_registration->installing_version(); | |
| 192 if (!version) { | |
| 193 // TODO(mek): Do something when no version is found. | |
| 194 return; | |
| 195 } | |
| 196 | |
| 197 std::vector<TransferredMessagePort> ports(1); | |
| 198 std::vector<base::string16> port_names(1); | |
| 199 ports[0].id = port.message_port_id; | |
| 200 port_names[0] = port.name; | |
| 201 version->SendStashedMessagePorts( | |
| 202 ports, port_names, base::Bind(&StashedPortManager::OnPortsTransferred, | |
| 203 this, version, ports)); | |
| 204 } | |
| 205 | |
| 206 void StashedPortManager::OnPortsTransferred( | |
| 207 const scoped_refptr<ServiceWorkerVersion>& service_worker, | |
| 208 const std::vector<TransferredMessagePort>& ports, | |
| 209 ServiceWorkerStatusCode service_worker_status, | |
| 210 const std::vector<int>& route_ids) { | |
| 211 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 212 if (service_worker_status != SERVICE_WORKER_OK) { | |
| 213 // TODO(mek): Handle failure. | |
| 214 return; | |
| 215 } | |
| 216 | |
| 217 if (service_worker->running_status() != ServiceWorkerVersion::RUNNING) { | |
| 218 // TODO(mek): Figure out what to do in this case. | |
| 219 return; | |
| 220 } | |
| 221 | |
| 222 // Port was transfered to a service worker, start observing the worker so | |
| 223 // messages can be held when the worker stops running. | |
| 224 if (observed_service_workers_.insert(service_worker.get()).second) { | |
| 225 // Service Worker wasn't already being observed | |
| 226 service_worker->AddListener(this); | |
| 227 } | |
| 228 | |
| 229 // Update ports with the new route ids and service worker version. | |
| 230 DCHECK(ports.size() == route_ids.size()); | |
|
Avi (use Gerrit)
2015/05/20 03:02:10
DCHECK_EQ
| |
| 231 for (size_t i = 0; i < ports.size(); ++i) { | |
| 232 DCHECK(ports_.find(ports[i].id) != ports_.end()); | |
| 233 StashedPort& port = ports_[ports[i].id]; | |
| 234 port.route_id = route_ids[i]; | |
| 235 port.service_worker = service_worker.get(); | |
| 236 } | |
| 237 } | |
| 238 | |
| 239 } // namespace content | |
| OLD | NEW |