Chromium Code Reviews| Index: content/browser/navigator_connect/navigator_connect_context_impl.cc |
| diff --git a/content/browser/navigator_connect/navigator_connect_context_impl.cc b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
| index ca4aadd18f9bfaf7e0c487964b1e0a41f2f6d3bd..316a4961b5527c61a7f354b2eae8bedae23bc915 100644 |
| --- a/content/browser/navigator_connect/navigator_connect_context_impl.cc |
| +++ b/content/browser/navigator_connect/navigator_connect_context_impl.cc |
| @@ -11,6 +11,7 @@ |
| #include "content/public/browser/browser_thread.h" |
| #include "content/public/browser/navigator_connect_service_factory.h" |
| #include "content/public/common/navigator_connect_client.h" |
| +#include "mojo/common/url_type_converters.h" |
| namespace content { |
| @@ -137,8 +138,7 @@ void NavigatorConnectContextImpl::GotServiceWorkerRegistration( |
| if (status != SERVICE_WORKER_OK) { |
| // No service worker found, reject connection attempt. |
| - OnConnectResult(callback, client_port_id, service_port_id, registration, |
| - status, false, base::string16(), base::string16()); |
| + OnConnectError(callback, client_port_id, service_port_id, status); |
| return; |
| } |
| @@ -150,10 +150,36 @@ void NavigatorConnectContextImpl::GotServiceWorkerRegistration( |
| service_port.service_worker_registration_origin = |
| registration->pattern().GetOrigin(); |
| - active_version->DispatchServicePortConnectEvent( |
| + active_version->RunAfterStartWorker( |
| + base::Bind(&NavigatorConnectContextImpl::OnConnectError, this, callback, |
| + client_port_id, service_port_id), |
| + base::Bind(&NavigatorConnectContextImpl::DispatchConnectEvent, this, |
| + callback, client_port_id, service_port_id, registration, |
| + make_scoped_refptr(active_version))); |
| +} |
| + |
| +void NavigatorConnectContextImpl::DispatchConnectEvent( |
| + const ConnectCallback& callback, |
| + int client_port_id, |
| + int service_port_id, |
| + const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| + const scoped_refptr<ServiceWorkerVersion>& worker) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + DCHECK(ports_.find(client_port_id) != ports_.end()); |
| + DCHECK(ports_.find(service_port_id) != ports_.end()); |
|
nhiroki
2016/01/05 09:33:24
nit: ContainsKey() in stl_util.h would be more rea
Marijn Kruisselbrink
2016/01/05 23:23:16
Done
|
| + |
| + const Port& service_port = ports_[service_port_id]; |
| + int request_id = worker->StartRequest( |
| + base::Bind(&NavigatorConnectContextImpl::OnConnectError, this, callback, |
| + client_port_id, service_port_id)); |
| + base::WeakPtr<ServicePortDispatcher> dispatcher = |
| + worker->GetMojoServiceForRequest<ServicePortDispatcher>(request_id); |
| + dispatcher->Connect( |
| + mojo::String::From(service_port.target_url), |
| + mojo::String::From(service_port.client_origin), service_port_id, |
| base::Bind(&NavigatorConnectContextImpl::OnConnectResult, this, callback, |
| - client_port_id, service_port_id, registration), |
| - service_port.target_url, service_port.client_origin, service_port_id); |
| + client_port_id, service_port_id, service_worker_registration, |
| + worker, request_id)); |
| } |
| void NavigatorConnectContextImpl::ServicePortServiceDestroyed( |
| @@ -169,26 +195,42 @@ void NavigatorConnectContextImpl::ServicePortServiceDestroyed( |
| } |
| } |
| +void NavigatorConnectContextImpl::OnConnectError( |
| + const ConnectCallback& callback, |
| + int client_port_id, |
| + int service_port_id, |
| + ServiceWorkerStatusCode status) { |
| + DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| + // Destroy ports since connection failed. |
| + ports_.erase(service_port_id); |
| + ports_.erase(client_port_id); |
| + callback.Run(MSG_ROUTING_NONE, false); |
| +} |
| + |
| void NavigatorConnectContextImpl::OnConnectResult( |
| const ConnectCallback& callback, |
| int client_port_id, |
| int service_port_id, |
| const scoped_refptr<ServiceWorkerRegistration>& service_worker_registration, |
| - ServiceWorkerStatusCode status, |
| - bool accept_connection, |
| - const base::string16& name, |
| - const base::string16& data) { |
| + const scoped_refptr<ServiceWorkerVersion>& worker, |
| + int request_id, |
| + ServicePortConnectResult result, |
| + const mojo::String& name, |
| + const mojo::String& data) { |
| DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| - if (accept_connection) { |
| - // TODO(mek): Might have to do something else if the client connection got |
| - // severed while the service side connection was being set up. |
| - callback.Run(client_port_id, true); |
| - } else { |
| - // Destroy ports since connection failed. |
| - ports_.erase(service_port_id); |
| - ports_.erase(client_port_id); |
| - callback.Run(MSG_ROUTING_NONE, false); |
| + |
| + if (!worker->FinishRequest(request_id)) |
| + return; |
| + |
| + if (result != SERVICE_PORT_CONNECT_RESULT_ACCEPT) { |
| + OnConnectError(callback, client_port_id, service_port_id, |
| + SERVICE_WORKER_ERROR_FAILED); |
| + return; |
| } |
| + |
| + // TODO(mek): Might have to do something else if the client connection got |
| + // severed while the service side connection was being set up. |
| + callback.Run(client_port_id, true); |
| } |
| void NavigatorConnectContextImpl::DeliverMessage( |