diff options
author | mek <mek@chromium.org> | 2015-07-17 15:45:07 -0700 |
---|---|---|
committer | Commit bot <commit-bot@chromium.org> | 2015-07-17 22:45:39 +0000 |
commit | ef0b94c53140a46f65c8233c7833b0bacd789792 (patch) | |
tree | 1919dee72a4194c9cb0ed7b7b3203fc0d478a99c /content/browser/service_worker | |
parent | 673add86a33e1cd0c753030f9ff5faa5b11a39bc (diff) | |
download | chromium_src-ef0b94c53140a46f65c8233c7833b0bacd789792.zip chromium_src-ef0b94c53140a46f65c8233c7833b0bacd789792.tar.gz chromium_src-ef0b94c53140a46f65c8233c7833b0bacd789792.tar.bz2 |
Update navigator.services API to use the new services.onconnect event [2/3].
This is part of a series of patches to change the service side API from
a global oncrossoriginconnect event to a new navigator.services.onconnect
event.
This patch builds on https://codereview.chromium.org/1221503003 to setup a
mojo service in the worker thread in the renderer which is used to
dispatch the new event to blink.
[1/3] https://codereview.chromium.org/1210633002
Adds new event to blink.
[2/3] This patch
[3/3] https://codereview.chromium.org/1205783004
Removes old event from blink and adds some tests specific to
the new event.
BUG=426458
Review URL: https://codereview.chromium.org/1210643002
Cr-Commit-Position: refs/heads/master@{#339347}
Diffstat (limited to 'content/browser/service_worker')
-rw-r--r-- | content/browser/service_worker/service_worker_version.cc | 88 | ||||
-rw-r--r-- | content/browser/service_worker/service_worker_version.h | 37 |
2 files changed, 82 insertions, 43 deletions
diff --git a/content/browser/service_worker/service_worker_version.cc b/content/browser/service_worker/service_worker_version.cc index f225a9b..4b24f21 100644 --- a/content/browser/service_worker/service_worker_version.cc +++ b/content/browser/service_worker/service_worker_version.cc @@ -42,6 +42,8 @@ #include "content/public/common/content_client.h" #include "content/public/common/content_switches.h" #include "content/public/common/result_codes.h" +#include "mojo/common/common_type_converters.h" +#include "mojo/common/url_type_converters.h" #include "net/http/http_response_headers.h" #include "net/http/http_response_info.h" @@ -165,10 +167,11 @@ void RunErrorMessageCallback( callback.Run(status); } -void RunErrorCrossOriginConnectCallback( - const ServiceWorkerVersion::CrossOriginConnectCallback& callback, +void RunErrorServicePortConnectCallback( + const ServiceWorkerVersion::ServicePortConnectCallback& callback, ServiceWorkerStatusCode status) { - callback.Run(status, false /* accept_connection */); + callback.Run(status, false /* accept_connection */, base::string16(), + base::string16()); } void RunErrorSendStashedPortsCallback( @@ -894,14 +897,17 @@ void ServiceWorkerVersion::DispatchGeofencingEvent( } } -void ServiceWorkerVersion::DispatchCrossOriginConnectEvent( - const CrossOriginConnectCallback& callback, - const NavigatorConnectClient& client) { +void ServiceWorkerVersion::DispatchServicePortConnectEvent( + const ServicePortConnectCallback& callback, + const GURL& target_url, + const GURL& origin, + int port_id) { DCHECK_EQ(ACTIVATED, status()) << status(); if (!base::CommandLine::ForCurrentProcess()->HasSwitch( switches::kEnableExperimentalWebPlatformFeatures)) { - callback.Run(SERVICE_WORKER_ERROR_ABORT, false); + callback.Run(SERVICE_WORKER_ERROR_ABORT, false, base::string16(), + base::string16()); return; } @@ -909,20 +915,26 @@ void ServiceWorkerVersion::DispatchCrossOriginConnectEvent( // Schedule calling this method after starting the worker. StartWorker( base::Bind(&RunTaskAfterStartWorker, weak_factory_.GetWeakPtr(), - base::Bind(&RunErrorCrossOriginConnectCallback, callback), - base::Bind(&self::DispatchCrossOriginConnectEvent, - weak_factory_.GetWeakPtr(), callback, client))); + base::Bind(&RunErrorServicePortConnectCallback, callback), + base::Bind(&self::DispatchServicePortConnectEvent, + weak_factory_.GetWeakPtr(), callback, target_url, + origin, port_id))); return; } - int request_id = AddRequest(callback, &cross_origin_connect_requests_, - REQUEST_CROSS_ORIGIN_CONNECT); - ServiceWorkerStatusCode status = embedded_worker_->SendMessage( - ServiceWorkerMsg_CrossOriginConnectEvent(request_id, client)); - if (status != SERVICE_WORKER_OK) { - cross_origin_connect_requests_.Remove(request_id); - RunSoon(base::Bind(callback, status, false)); + int request_id = AddRequest(callback, &service_port_connect_requests_, + REQUEST_SERVICE_PORT_CONNECT); + if (!service_port_dispatcher_) { + embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( + mojo::GetProxy(&service_port_dispatcher_)); + service_port_dispatcher_.set_connection_error_handler(base::Bind( + &ServiceWorkerVersion::OnServicePortDispatcherConnectionError, + weak_factory_.GetWeakPtr())); } + service_port_dispatcher_->Connect( + mojo::String::From(target_url), mojo::String::From(origin), port_id, + base::Bind(&ServiceWorkerVersion::OnServicePortConnectEventFinished, + weak_factory_.GetWeakPtr(), request_id)); } void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( @@ -1203,8 +1215,6 @@ bool ServiceWorkerVersion::OnMessageReceived(const IPC::Message& message) { OnPushEventFinished) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GeofencingEventFinished, OnGeofencingEventFinished) - IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_CrossOriginConnectEventFinished, - OnCrossOriginConnectEventFinished) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenWindow, OnOpenWindow) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SetCachedMetadata, @@ -1462,22 +1472,26 @@ void ServiceWorkerVersion::OnGeofencingEventFinished(int request_id) { RemoveCallbackAndStopIfRedundant(&geofencing_requests_, request_id); } -void ServiceWorkerVersion::OnCrossOriginConnectEventFinished( +void ServiceWorkerVersion::OnServicePortConnectEventFinished( int request_id, - bool accept_connection) { + ServicePortConnectResult result, + const mojo::String& name, + const mojo::String& data) { TRACE_EVENT1("ServiceWorker", - "ServiceWorkerVersion::OnCrossOriginConnectEventFinished", + "ServiceWorkerVersion::OnServicePortConnectEventFinished", "Request id", request_id); - PendingRequest<CrossOriginConnectCallback>* request = - cross_origin_connect_requests_.Lookup(request_id); + PendingRequest<ServicePortConnectCallback>* request = + service_port_connect_requests_.Lookup(request_id); if (!request) { NOTREACHED() << "Got unexpected message: " << request_id; return; } scoped_refptr<ServiceWorkerVersion> protect(this); - request->callback.Run(SERVICE_WORKER_OK, accept_connection); - RemoveCallbackAndStopIfRedundant(&cross_origin_connect_requests_, request_id); + request->callback.Run(SERVICE_WORKER_OK, + result == SERVICE_PORT_CONNECT_RESULT_ACCEPT, + name.To<base::string16>(), data.To<base::string16>()); + RemoveCallbackAndStopIfRedundant(&service_port_connect_requests_, request_id); } void ServiceWorkerVersion::OnOpenWindow(int request_id, GURL url) { @@ -1980,7 +1994,7 @@ bool ServiceWorkerVersion::HasInflightRequests() const { !fetch_requests_.IsEmpty() || !sync_requests_.IsEmpty() || !notification_click_requests_.IsEmpty() || !push_requests_.IsEmpty() || !geofencing_requests_.IsEmpty() || - !cross_origin_connect_requests_.IsEmpty() || + !service_port_connect_requests_.IsEmpty() || !streaming_url_request_jobs_.empty(); } @@ -2071,10 +2085,11 @@ bool ServiceWorkerVersion::OnRequestTimeout(const RequestInfo& info) { case REQUEST_GEOFENCING: return RunIDMapCallback(&geofencing_requests_, info.id, SERVICE_WORKER_ERROR_TIMEOUT); - case REQUEST_CROSS_ORIGIN_CONNECT: - return RunIDMapCallback(&cross_origin_connect_requests_, info.id, + case REQUEST_SERVICE_PORT_CONNECT: + return RunIDMapCallback(&service_port_connect_requests_, info.id, SERVICE_WORKER_ERROR_TIMEOUT, - false /* accept_connection */); + false /* accept_connection */, base::string16(), + base::string16()); } NOTREACHED() << "Got unexpected request type: " << info.type; return false; @@ -2191,8 +2206,10 @@ void ServiceWorkerVersion::OnStoppedInternal( RunIDMapCallbacks(¬ification_click_requests_, SERVICE_WORKER_ERROR_FAILED); RunIDMapCallbacks(&push_requests_, SERVICE_WORKER_ERROR_FAILED); RunIDMapCallbacks(&geofencing_requests_, SERVICE_WORKER_ERROR_FAILED); - RunIDMapCallbacks(&cross_origin_connect_requests_, - SERVICE_WORKER_ERROR_FAILED, false); + + // Close all mojo services. This will also fire and clear all callbacks + // for messages that are still outstanding for those services. + OnServicePortDispatcherConnectionError(); streaming_url_request_jobs_.clear(); @@ -2202,4 +2219,11 @@ void ServiceWorkerVersion::OnStoppedInternal( StartWorkerInternal(); } +void ServiceWorkerVersion::OnServicePortDispatcherConnectionError() { + RunIDMapCallbacks(&service_port_connect_requests_, + SERVICE_WORKER_ERROR_FAILED, false, base::string16(), + base::string16()); + service_port_dispatcher_.reset(); +} + } // namespace content diff --git a/content/browser/service_worker/service_worker_version.h b/content/browser/service_worker/service_worker_version.h index deb22aa..47114dc 100644 --- a/content/browser/service_worker/service_worker_version.h +++ b/content/browser/service_worker/service_worker_version.h @@ -22,8 +22,10 @@ #include "content/browser/service_worker/embedded_worker_instance.h" #include "content/browser/service_worker/service_worker_script_cache_map.h" #include "content/common/content_export.h" +#include "content/common/service_port_service.mojom.h" #include "content/common/service_worker/service_worker_status_code.h" #include "content/common/service_worker/service_worker_types.h" +#include "content/public/common/service_registry.h" #include "third_party/WebKit/public/platform/WebGeofencingEventType.h" #include "third_party/WebKit/public/platform/WebServiceWorkerEventResult.h" @@ -69,8 +71,10 @@ class CONTENT_EXPORT ServiceWorkerVersion ServiceWorkerFetchEventResult, const ServiceWorkerResponse&)> FetchCallback; typedef base::Callback<void(ServiceWorkerStatusCode, - bool /* accept_connction */)> - CrossOriginConnectCallback; + bool /* accept_connction */, + const base::string16& /* name */, + const base::string16& /* data */)> + ServicePortConnectCallback; typedef base::Callback<void(ServiceWorkerStatusCode, const std::vector<int>&)> SendStashedPortsCallback; @@ -237,13 +241,15 @@ class CONTENT_EXPORT ServiceWorkerVersion const std::string& region_id, const blink::WebCircularGeofencingRegion& region); - // Sends a cross origin connect event to the associated embedded worker and + // Sends a ServicePort connect event to the associated embedded worker and // asynchronously calls |callback| with the response from the worker. // // This must be called when the status() is ACTIVATED. - void DispatchCrossOriginConnectEvent( - const CrossOriginConnectCallback& callback, - const NavigatorConnectClient& client); + void DispatchServicePortConnectEvent( + const ServicePortConnectCallback& callback, + const GURL& target_url, + const GURL& origin, + int port_id); // Sends a cross origin message event to the associated embedded worker and // asynchronously calls |callback| when the message was sent (or failed to @@ -364,7 +370,7 @@ class CONTENT_EXPORT ServiceWorkerVersion REQUEST_NOTIFICATION_CLICK, REQUEST_PUSH, REQUEST_GEOFENCING, - REQUEST_CROSS_ORIGIN_CONNECT + REQUEST_SERVICE_PORT_CONNECT }; struct RequestInfo { @@ -438,8 +444,10 @@ class CONTENT_EXPORT ServiceWorkerVersion void OnPushEventFinished(int request_id, blink::WebServiceWorkerEventResult result); void OnGeofencingEventFinished(int request_id); - void OnCrossOriginConnectEventFinished(int request_id, - bool accept_connection); + void OnServicePortConnectEventFinished(int request_id, + ServicePortConnectResult result, + const mojo::String& name, + const mojo::String& data); void OnOpenWindow(int request_id, GURL url); void DidOpenWindow(int request_id, int render_process_id, @@ -533,6 +541,11 @@ class CONTENT_EXPORT ServiceWorkerVersion void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); + // Called when the connection to a ServicePortDispatcher drops or fails. + // Calls callbacks for any outstanding requests to the dispatcher as well + // as cleans up the dispatcher. + void OnServicePortDispatcherConnectionError(); + const int64 version_id_; const int64 registration_id_; const GURL script_url_; @@ -554,8 +567,10 @@ class CONTENT_EXPORT ServiceWorkerVersion notification_click_requests_; IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> push_requests_; IDMap<PendingRequest<StatusCallback>, IDMapOwnPointer> geofencing_requests_; - IDMap<PendingRequest<CrossOriginConnectCallback>, IDMapOwnPointer> - cross_origin_connect_requests_; + IDMap<PendingRequest<ServicePortConnectCallback>, IDMapOwnPointer> + service_port_connect_requests_; + + ServicePortDispatcherPtr service_port_dispatcher_; std::set<const ServiceWorkerURLRequestJob*> streaming_url_request_jobs_; |