From eb113ebb7a772b5e7af45bedebe070fe23762b07 Mon Sep 17 00:00:00 2001 From: kinuko Date: Thu, 12 Mar 2015 22:13:09 -0700 Subject: [PATCH] Use UUID for ServiceWorker Client identifier (2/3, chromium) We used to use ServiceWorkerVersion-local identifier for clients but this patch changes the ID to global UUID one. Since it changes some security assumption I also added code to check the origin, so that ServiceWorker can only talk to the clients that are in the same origin. Spec issue: https://github.com/slightlyoff/ServiceWorker/issues/634 More context: https://github.com/slightlyoff/ServiceWorker/issues/609 To be landed in 3 patches: 1/3: blink-side patch https://crrev.com/990583002/ 2/3: THIS 3/3: blink-side cleanup BUG=464668 Review URL: https://codereview.chromium.org/988063004 Cr-Commit-Position: refs/heads/master@{#320451} --- .../service_worker/service_worker_context_core.cc | 18 ++++- .../service_worker/service_worker_context_core.h | 13 +++ .../service_worker/service_worker_provider_host.cc | 5 +- .../service_worker/service_worker_provider_host.h | 9 ++- .../service_worker/service_worker_version.cc | 94 ++++++++++++---------- .../service_worker/service_worker_version.h | 15 ++-- .../service_worker/service_worker_client_info.cc | 8 +- .../service_worker/service_worker_client_info.h | 8 +- .../service_worker/service_worker_messages.h | 10 +-- .../embedded_worker_context_client.cc | 11 +-- .../embedded_worker_context_client.h | 4 +- .../service_worker_script_context.cc | 28 ++++--- .../service_worker/service_worker_script_context.h | 6 +- 13 files changed, 135 insertions(+), 94 deletions(-) diff --git a/content/browser/service_worker/service_worker_context_core.cc b/content/browser/service_worker/service_worker_context_core.cc index c2e9553f1539..60991352a81c 100644 --- a/content/browser/service_worker/service_worker_context_core.cc +++ b/content/browser/service_worker/service_worker_context_core.cc @@ -116,6 +116,7 @@ ServiceWorkerContextCore::ServiceWorkerContextCore( ServiceWorkerContextWrapper* wrapper) : wrapper_(wrapper), providers_(new ProcessToProviderMap), + provider_by_uuid_(new ProviderByClientUUIDMap), cache_manager_(ServiceWorkerCacheStorageManager::Create( path, cache_task_runner.get(), @@ -141,7 +142,9 @@ ServiceWorkerContextCore::ServiceWorkerContextCore( ServiceWorkerContextWrapper* wrapper) : wrapper_(wrapper), providers_(old_context->providers_.release()), - cache_manager_(old_context->cache_manager_.release()), + provider_by_uuid_(old_context->provider_by_uuid_.release()), + cache_manager_(ServiceWorkerCacheStorageManager::Create( + old_context->cache_manager())), next_handle_id_(old_context->next_handle_id_), next_registration_handle_id_(old_context->next_registration_handle_id_), observer_list_(old_context->observer_list_), @@ -201,6 +204,19 @@ ServiceWorkerContextCore::GetProviderHostIterator() { return make_scoped_ptr(new ProviderHostIterator(providers_.get())); } +void ServiceWorkerContextCore::RegisterClientIDForProviderHost( + const std::string& client_uuid, + ServiceWorkerProviderHost* provider_host) { + DCHECK(!ContainsKey(*provider_by_uuid_, client_uuid)); + (*provider_by_uuid_)[client_uuid] = provider_host; +} + +void ServiceWorkerContextCore::UnregisterClientIDForProviderHost( + const std::string& client_uuid) { + DCHECK(ContainsKey(*provider_by_uuid_, client_uuid)); + provider_by_uuid_->erase(client_uuid); +} + void ServiceWorkerContextCore::RegisterServiceWorker( const GURL& pattern, const GURL& script_url, diff --git a/content/browser/service_worker/service_worker_context_core.h b/content/browser/service_worker/service_worker_context_core.h index f357e1672ac8..a38e8141abda 100644 --- a/content/browser/service_worker/service_worker_context_core.h +++ b/content/browser/service_worker/service_worker_context_core.h @@ -68,6 +68,9 @@ class CONTENT_EXPORT ServiceWorkerContextCore typedef IDMap ProviderMap; typedef IDMap ProcessToProviderMap; + using ProviderByClientUUIDMap = + std::map; + // Directory for ServiceWorkerStorage and ServiceWorkerCacheManager. static const base::FilePath::CharType kServiceWorkerDirectory[]; @@ -146,6 +149,15 @@ class CONTENT_EXPORT ServiceWorkerContextCore void RemoveAllProviderHostsForProcess(int process_id); scoped_ptr GetProviderHostIterator(); + // Maintains a map from Client UUID to ProviderHost. + // (Note: instead of maintaining 2 maps we might be able to uniformly use + // UUID instead of process_id+provider_id elsewhere. For now I'm leaving + // these as provider_id is deeply wired everywhere) + void RegisterClientIDForProviderHost( + const std::string& client_uuid, + ServiceWorkerProviderHost* provider_host); + void UnregisterClientIDForProviderHost(const std::string& client_uuid); + // A child process of |source_process_id| may be used to run the created // worker for initial installation. // Non-null |provider_host| must be given if this is called from a document. @@ -231,6 +243,7 @@ class CONTENT_EXPORT ServiceWorkerContextCore // Bind() to hold a reference to |wrapper_| until |this| is fully destroyed. ServiceWorkerContextWrapper* wrapper_; scoped_ptr providers_; + scoped_ptr provider_by_uuid_; scoped_ptr storage_; scoped_ptr cache_manager_; scoped_refptr embedded_worker_registry_; diff --git a/content/browser/service_worker/service_worker_provider_host.cc b/content/browser/service_worker/service_worker_provider_host.cc index 8b215b094f60..3054878f8ccb 100644 --- a/content/browser/service_worker/service_worker_provider_host.cc +++ b/content/browser/service_worker/service_worker_provider_host.cc @@ -4,6 +4,7 @@ #include "content/browser/service_worker/service_worker_provider_host.h" +#include "base/guid.h" #include "base/stl_util.h" #include "content/browser/frame_host/frame_tree.h" #include "content/browser/frame_host/frame_tree_node.h" @@ -74,7 +75,8 @@ ServiceWorkerProviderHost::ServiceWorkerProviderHost( ServiceWorkerProviderType provider_type, base::WeakPtr context, ServiceWorkerDispatcherHost* dispatcher_host) - : render_process_id_(render_process_id), + : client_uuid_(base::GenerateGUID()), + render_process_id_(render_process_id), render_frame_id_(render_frame_id), render_thread_id_(kDocumentMainThreadId), provider_id_(provider_id), @@ -89,6 +91,7 @@ ServiceWorkerProviderHost::ServiceWorkerProviderHost( // Actual thread id is set when the service worker context gets started. render_thread_id_ = kInvalidEmbeddedWorkerThreadId; } + context_->RegisterClientIDForProviderHost(client_uuid_, this); } ServiceWorkerProviderHost::~ServiceWorkerProviderHost() { diff --git a/content/browser/service_worker/service_worker_provider_host.h b/content/browser/service_worker/service_worker_provider_host.h index 758e1ab7910b..eb678632ff91 100644 --- a/content/browser/service_worker/service_worker_provider_host.h +++ b/content/browser/service_worker/service_worker_provider_host.h @@ -34,10 +34,11 @@ class ServiceWorkerRequestHandler; class ServiceWorkerVersion; // This class is the browser-process representation of a service worker -// provider. There is a provider per document and the lifetime of this -// object is tied to the lifetime of its document in the renderer process. +// provider. There is a provider per document or a worker and the lifetime +// of this object is tied to the lifetime of its document or the worker +// in the renderer process. // This class holds service worker state that is scoped to an individual -// document. +// document or a worker. // // Note this class can also host a running service worker, in which // case it will observe resource loads made directly by the service worker. @@ -63,6 +64,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost ServiceWorkerDispatcherHost* dispatcher_host); virtual ~ServiceWorkerProviderHost(); + const std::string& client_uuid() const { return client_uuid_; } int process_id() const { return render_process_id_; } int provider_id() const { return provider_id_; } int frame_id() const { return render_frame_id_; } @@ -253,6 +255,7 @@ class CONTENT_EXPORT ServiceWorkerProviderHost bool IsReadyToSendMessages() const; void Send(IPC::Message* message) const; + std::string client_uuid_; int render_process_id_; int render_frame_id_; int render_thread_id_; diff --git a/content/browser/service_worker/service_worker_version.cc b/content/browser/service_worker/service_worker_version.cc index 53f05f35587f..53ece274c991 100644 --- a/content/browser/service_worker/service_worker_version.cc +++ b/content/browser/service_worker/service_worker_version.cc @@ -260,8 +260,8 @@ base::TimeDelta GetTickDuration(const base::TimeTicks& time) { } void OnGetClientsFromUI( - // The tuple contains process_id, frame_id, client_id. - const std::vector>& clients_info, + // The tuple contains process_id, frame_id, client_uuid. + const std::vector>& clients_info, const GURL& script_url, const GetClientsCallback& callback) { std::vector clients; @@ -283,7 +283,7 @@ void OnGetClientsFromUI( if (info.url.GetOrigin() != script_url.GetOrigin()) return; - info.client_id = get<2>(it); + info.client_uuid = get<2>(it); clients.push_back(info); } @@ -727,21 +727,19 @@ void ServiceWorkerVersion::DispatchCrossOriginMessageEvent( void ServiceWorkerVersion::AddControllee( ServiceWorkerProviderHost* provider_host) { - DCHECK(!ContainsKey(controllee_map_, provider_host)); - int controllee_id = controllee_by_id_.Add(provider_host); - // IDMap<>'s last index is kInvalidServiceWorkerClientId. - CHECK(controllee_id != kInvalidServiceWorkerClientId); - controllee_map_[provider_host] = controllee_id; + const std::string& uuid = provider_host->client_uuid(); + CHECK(!provider_host->client_uuid().empty()); + DCHECK(!ContainsKey(controllee_map_, uuid)); + controllee_map_[uuid] = provider_host; // Keep the worker alive a bit longer right after a new controllee is added. RestartTick(&idle_time_); } void ServiceWorkerVersion::RemoveControllee( ServiceWorkerProviderHost* provider_host) { - ControlleeMap::iterator found = controllee_map_.find(provider_host); - DCHECK(found != controllee_map_.end()); - controllee_by_id_.Remove(found->second); - controllee_map_.erase(found); + const std::string& uuid = provider_host->client_uuid(); + DCHECK(ContainsKey(controllee_map_, uuid)); + controllee_map_.erase(uuid); if (HasControllee()) return; FOR_EACH_OBSERVER(Listener, listeners_, OnNoControllees(this)); @@ -948,8 +946,8 @@ bool ServiceWorkerVersion::OnMessageReceived(const IPC::Message& message) { OnSetCachedMetadata) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_ClearCachedMetadata, OnClearCachedMetadata) - IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToDocument, - OnPostMessageToDocument) + IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_PostMessageToClient, + OnPostMessageToClient) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_FocusClient, OnFocusClient) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_SkipWaiting, @@ -1000,7 +998,7 @@ void ServiceWorkerVersion::OnGetClients( int request_id, const ServiceWorkerClientQueryOptions& /* options */) { // TODO(kinuko): Handle ClientQueryOptions. (crbug.com/455241, 460415 etc) - if (controllee_by_id_.IsEmpty()) { + if (controllee_map_.empty()) { if (running_status() == RUNNING) { embedded_worker_->SendMessage( ServiceWorkerMsg_DidGetClients(request_id, @@ -1012,14 +1010,13 @@ void ServiceWorkerVersion::OnGetClients( TRACE_EVENT0("ServiceWorker", "ServiceWorkerVersion::OnGetClients"); - std::vector> clients_info; - for (ControlleeByIDMap::iterator it(&controllee_by_id_); !it.IsAtEnd(); - it.Advance()) { - int process_id = it.GetCurrentValue()->process_id(); - int frame_id = it.GetCurrentValue()->frame_id(); - int client_id = it.GetCurrentKey(); + std::vector> clients_info; + for (auto& controllee : controllee_map_) { + int process_id = controllee.second->process_id(); + int frame_id = controllee.second->frame_id(); + const std::string& client_uuid = controllee.first; - clients_info.push_back(MakeTuple(process_id, frame_id, client_id)); + clients_info.push_back(MakeTuple(process_id, frame_id, client_uuid)); } BrowserThread::PostTask( @@ -1240,27 +1237,27 @@ void ServiceWorkerVersion::DidOpenWindow(int request_id, } for (const auto& it : controllee_map_) { - const ServiceWorkerProviderHost* provider_host = it.first; + const ServiceWorkerProviderHost* provider_host = it.second; if (provider_host->process_id() != render_process_id || provider_host->frame_id() != render_frame_id) { continue; } - // it.second is the client_id associated with the provider_host. + // it.second is the client_uuid associated with the provider_host. provider_host->GetClientInfo( base::Bind(&ServiceWorkerVersion::OnOpenWindowFinished, - weak_factory_.GetWeakPtr(), request_id, it.second)); + weak_factory_.GetWeakPtr(), request_id, it.first)); return; } // If here, it means that no provider_host was found, in which case, the // renderer should still be informed that the window was opened. - OnOpenWindowFinished(request_id, 0, ServiceWorkerClientInfo()); + OnOpenWindowFinished(request_id, std::string(), ServiceWorkerClientInfo()); } void ServiceWorkerVersion::OnOpenWindowFinished( int request_id, - int client_id, + const std::string& client_uuid, const ServiceWorkerClientInfo& client_info) { DCHECK_CURRENTLY_ON(BrowserThread::IO); @@ -1273,7 +1270,7 @@ void ServiceWorkerVersion::OnOpenWindowFinished( // controlled but the action still succeeded. The renderer process is // expecting an empty client in such case. if (!client.IsEmpty()) - client.client_id = client_id; + client.client_uuid = client_uuid; embedded_worker_->SendMessage(ServiceWorkerMsg_OpenWindowResponse( request_id, client)); @@ -1316,44 +1313,53 @@ void ServiceWorkerVersion::OnClearCachedMetadataFinished(int64 callback_id, FOR_EACH_OBSERVER(Listener, listeners_, OnCachedMetadataUpdated(this)); } -void ServiceWorkerVersion::OnPostMessageToDocument( - int client_id, +void ServiceWorkerVersion::OnPostMessageToClient( + const std::string& client_uuid, const base::string16& message, const std::vector& sent_message_ports) { TRACE_EVENT1("ServiceWorker", "ServiceWorkerVersion::OnPostMessageToDocument", - "Client id", client_id); - ServiceWorkerProviderHost* provider_host = - controllee_by_id_.Lookup(client_id); - if (!provider_host) { + "Client id", client_uuid); + auto it = controllee_map_.find(client_uuid); + if (it == controllee_map_.end()) { // The client may already have been closed, just ignore. return; } - provider_host->PostMessage(message, sent_message_ports); + if (it->second->document_url().GetOrigin() != script_url_.GetOrigin()) { + // The client does not belong to the same origin as this ServiceWorker, + // possibly due to timing issue or bad message. + return; + } + it->second->PostMessage(message, sent_message_ports); } -void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) { +void ServiceWorkerVersion::OnFocusClient(int request_id, + const std::string& client_uuid) { TRACE_EVENT2("ServiceWorker", "ServiceWorkerVersion::OnFocusClient", "Request id", request_id, - "Client id", client_id); - ServiceWorkerProviderHost* provider_host = - controllee_by_id_.Lookup(client_id); - if (!provider_host) { + "Client id", client_uuid); + auto it = controllee_map_.find(client_uuid); + if (it == controllee_map_.end()) { // The client may already have been closed, just ignore. return; } + if (it->second->document_url().GetOrigin() != script_url_.GetOrigin()) { + // The client does not belong to the same origin as this ServiceWorker, + // possibly due to timing issue or bad message. + return; + } - provider_host->Focus( + it->second->Focus( base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, weak_factory_.GetWeakPtr(), request_id, - client_id)); + client_uuid)); } void ServiceWorkerVersion::OnFocusClientFinished( int request_id, - int cliend_id, + const std::string& cliend_uuid, const ServiceWorkerClientInfo& client) { DCHECK_CURRENTLY_ON(BrowserThread::IO); @@ -1361,7 +1367,7 @@ void ServiceWorkerVersion::OnFocusClientFinished( return; ServiceWorkerClientInfo client_info(client); - client_info.client_id = cliend_id; + client_info.client_uuid = cliend_uuid; embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( request_id, client_info)); diff --git a/content/browser/service_worker/service_worker_version.h b/content/browser/service_worker/service_worker_version.h index 8af56858eb40..225278486a38 100644 --- a/content/browser/service_worker/service_worker_version.h +++ b/content/browser/service_worker/service_worker_version.h @@ -312,8 +312,6 @@ class CONTENT_EXPORT ServiceWorkerVersion friend class ServiceWorkerVersionBrowserTest; typedef ServiceWorkerVersion self; - typedef std::map ControlleeMap; - typedef IDMap ControlleeByIDMap; enum PingState { NOT_PINGING, PINGING, PING_TIMED_OUT }; @@ -371,7 +369,7 @@ class CONTENT_EXPORT ServiceWorkerVersion int render_process_id, int render_frame_id); void OnOpenWindowFinished(int request_id, - int client_id, + const std::string& client_uuid, const ServiceWorkerClientInfo& client_info); void OnSetCachedMetadata(const GURL& url, const std::vector& data); @@ -379,17 +377,17 @@ class CONTENT_EXPORT ServiceWorkerVersion void OnClearCachedMetadata(const GURL& url); void OnClearCachedMetadataFinished(int64 callback_id, int result); - void OnPostMessageToDocument( - int client_id, + void OnPostMessageToClient( + const std::string& client_uuid, const base::string16& message, const std::vector& sent_message_ports); - void OnFocusClient(int request_id, int client_id); + void OnFocusClient(int request_id, const std::string& client_uuid); void OnSkipWaiting(int request_id); void OnClaimClients(int request_id); void OnPongFromWorker(); void OnFocusClientFinished(int request_id, - int client_id, + const std::string& client_uuid, const ServiceWorkerClientInfo& client); void DidSkipWaiting(int request_id); @@ -449,8 +447,7 @@ class CONTENT_EXPORT ServiceWorkerVersion std::set streaming_url_request_jobs_; - ControlleeMap controllee_map_; - ControlleeByIDMap controllee_by_id_; + std::map controllee_map_; // Will be null while shutting down. base::WeakPtr context_; ObserverList listeners_; diff --git a/content/common/service_worker/service_worker_client_info.cc b/content/common/service_worker/service_worker_client_info.cc index e36af87fa6dc..3d9d554af4f4 100644 --- a/content/common/service_worker/service_worker_client_info.cc +++ b/content/common/service_worker/service_worker_client_info.cc @@ -10,8 +10,7 @@ namespace content { ServiceWorkerClientInfo::ServiceWorkerClientInfo() - : client_id(kInvalidServiceWorkerClientId), - page_visibility_state(blink::WebPageVisibilityStateLast), + : page_visibility_state(blink::WebPageVisibilityStateLast), is_focused(false), frame_type(REQUEST_CONTEXT_FRAME_TYPE_LAST), client_type(blink::WebServiceWorkerClientTypeLast) { @@ -23,8 +22,7 @@ ServiceWorkerClientInfo::ServiceWorkerClientInfo( const GURL& url, RequestContextFrameType frame_type, blink::WebServiceWorkerClientType client_type) - : client_id(kInvalidServiceWorkerClientId), - page_visibility_state(page_visibility_state), + : page_visibility_state(page_visibility_state), is_focused(is_focused), url(url), frame_type(frame_type), @@ -40,7 +38,7 @@ bool ServiceWorkerClientInfo::IsEmpty() const { } bool ServiceWorkerClientInfo::IsValid() const { - return !IsEmpty() && client_id != kInvalidServiceWorkerClientId; + return !IsEmpty() && !client_uuid.empty(); } } // namespace content diff --git a/content/common/service_worker/service_worker_client_info.h b/content/common/service_worker/service_worker_client_info.h index 7a299509d5de..e21aa1552f95 100644 --- a/content/common/service_worker/service_worker_client_info.h +++ b/content/common/service_worker/service_worker_client_info.h @@ -15,8 +15,8 @@ namespace content { // This class holds the information related to a service worker window client. // It is the content/ equivalent of Blink's WebServiceWorkerClientInfo. // An instance can be created empty or can be filed with the expected -// properties. Except for the client_id, it is preferred to use the constructor -// to fill the properties. +// properties. Except for the client_uuid, it is preferred to use the +// constructor to fill the properties. struct ServiceWorkerClientInfo { ServiceWorkerClientInfo(); ServiceWorkerClientInfo(blink::WebPageVisibilityState page_visibility_state, @@ -29,10 +29,10 @@ struct ServiceWorkerClientInfo { bool IsEmpty() const; // Returns whether the instance is valid. A valid instance is not empty and - // has a valid client_id. + // has a valid client_uuid. bool IsValid() const; - int client_id; + std::string client_uuid; blink::WebPageVisibilityState page_visibility_state; bool is_focused; GURL url; diff --git a/content/common/service_worker/service_worker_messages.h b/content/common/service_worker/service_worker_messages.h index 984ae312e4d0..77b6bf950f4f 100644 --- a/content/common/service_worker/service_worker_messages.h +++ b/content/common/service_worker/service_worker_messages.h @@ -111,7 +111,7 @@ IPC_STRUCT_TRAITS_BEGIN(content::ServiceWorkerVersionAttributes) IPC_STRUCT_TRAITS_END() IPC_STRUCT_TRAITS_BEGIN(content::ServiceWorkerClientInfo) - IPC_STRUCT_TRAITS_MEMBER(client_id) + IPC_STRUCT_TRAITS_MEMBER(client_uuid) IPC_STRUCT_TRAITS_MEMBER(page_visibility_state) IPC_STRUCT_TRAITS_MEMBER(is_focused) IPC_STRUCT_TRAITS_MEMBER(url) @@ -243,10 +243,10 @@ IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_GetClients, int /* request_id */, content::ServiceWorkerClientQueryOptions) -// Sends a 'message' event to a client document (renderer->browser). +// Sends a 'message' event to a client (renderer->browser). IPC_MESSAGE_ROUTED3( - ServiceWorkerHostMsg_PostMessageToDocument, - int /* client_id */, + ServiceWorkerHostMsg_PostMessageToClient, + std::string /* uuid */, base::string16 /* message */, std::vector /* sent_message_ports */) @@ -268,7 +268,7 @@ IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_OpenWindow, // Ask the browser to focus a client (renderer->browser). IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_FocusClient, int /* request_id */, - int /* client_id */) + std::string /* uuid */) // Asks the browser to force this worker to become activated. IPC_MESSAGE_ROUTED1(ServiceWorkerHostMsg_SkipWaiting, diff --git a/content/renderer/service_worker/embedded_worker_context_client.cc b/content/renderer/service_worker/embedded_worker_context_client.cc index e99e9edf3e8f..e2c50d84f61d 100644 --- a/content/renderer/service_worker/embedded_worker_context_client.cc +++ b/content/renderer/service_worker/embedded_worker_context_client.cc @@ -390,12 +390,12 @@ EmbeddedWorkerContextClient::createServiceWorkerProvider() { } void EmbeddedWorkerContextClient::postMessageToClient( - int client_id, + const blink::WebString& uuid, const blink::WebString& message, blink::WebMessagePortChannelArray* channels) { DCHECK(script_context_); - script_context_->PostMessageToDocument(client_id, message, - make_scoped_ptr(channels)); + script_context_->PostMessageToClient( + uuid, message, make_scoped_ptr(channels)); } void EmbeddedWorkerContextClient::postMessageToCrossOriginClient( @@ -408,9 +408,10 @@ void EmbeddedWorkerContextClient::postMessageToCrossOriginClient( } void EmbeddedWorkerContextClient::focus( - int client_id, blink::WebServiceWorkerClientCallbacks* callback) { + const blink::WebString& uuid, + blink::WebServiceWorkerClientCallbacks* callback) { DCHECK(script_context_); - script_context_->FocusClient(client_id, callback); + script_context_->FocusClient(uuid, callback); } void EmbeddedWorkerContextClient::skipWaiting( diff --git a/content/renderer/service_worker/embedded_worker_context_client.h b/content/renderer/service_worker/embedded_worker_context_client.h index 166bcecd3119..d12268de92b4 100644 --- a/content/renderer/service_worker/embedded_worker_context_client.h +++ b/content/renderer/service_worker/embedded_worker_context_client.h @@ -115,14 +115,14 @@ class EmbeddedWorkerContextClient virtual blink::WebServiceWorkerProvider* createServiceWorkerProvider(); virtual void postMessageToClient( - int client_id, + const blink::WebString& uuid, const blink::WebString& message, blink::WebMessagePortChannelArray* channels); virtual void postMessageToCrossOriginClient( const blink::WebCrossOriginServiceWorkerClient& client, const blink::WebString& message, blink::WebMessagePortChannelArray* channels); - virtual void focus(int client_id, + virtual void focus(const blink::WebString& uuid, blink::WebServiceWorkerClientCallbacks*); virtual void skipWaiting( blink::WebServiceWorkerSkipWaitingCallbacks* callbacks); diff --git a/content/renderer/service_worker/service_worker_script_context.cc b/content/renderer/service_worker/service_worker_script_context.cc index 7bd700c11d86..a3b296e93208 100644 --- a/content/renderer/service_worker/service_worker_script_context.cc +++ b/content/renderer/service_worker/service_worker_script_context.cc @@ -6,6 +6,7 @@ #include "base/logging.h" #include "base/metrics/histogram.h" +#include "base/strings/utf_string_conversions.h" #include "base/trace_event/trace_event.h" #include "content/child/notifications/notification_data_conversions.h" #include "content/child/service_worker/web_service_worker_registration_impl.h" @@ -30,14 +31,14 @@ namespace content { namespace { -void SendPostMessageToDocumentOnMainThread( +void SendPostMessageToClientOnMainThread( ThreadSafeSender* sender, int routing_id, - int client_id, + const std::string& uuid, const base::string16& message, scoped_ptr channels) { - sender->Send(new ServiceWorkerHostMsg_PostMessageToDocument( - routing_id, client_id, message, + sender->Send(new ServiceWorkerHostMsg_PostMessageToClient( + routing_id, uuid, message, WebMessagePortChannelImpl::ExtractMessagePortIDs(channels.release()))); } @@ -81,7 +82,7 @@ ToWebServiceWorkerClientInfo(const ServiceWorkerClientInfo& client_info) { blink::WebServiceWorkerClientInfo web_client_info; - web_client_info.clientID = client_info.client_id; + web_client_info.uuid = base::UTF8ToUTF16(client_info.client_uuid); web_client_info.pageVisibilityState = client_info.page_visibility_state; web_client_info.isFocused = client_info.is_focused; web_client_info.url = client_info.url; @@ -253,8 +254,8 @@ void ServiceWorkerScriptContext::ClearCachedMetadata(const GURL& url) { Send(new ServiceWorkerHostMsg_ClearCachedMetadata(GetRoutingID(), url)); } -void ServiceWorkerScriptContext::PostMessageToDocument( - int client_id, +void ServiceWorkerScriptContext::PostMessageToClient( + const base::string16& uuid, const base::string16& message, scoped_ptr channels) { // This may send channels for MessagePorts, and all internal book-keeping @@ -263,9 +264,11 @@ void ServiceWorkerScriptContext::PostMessageToDocument( // to overtake those messages. embedded_context_->main_thread_task_runner()->PostTask( FROM_HERE, - base::Bind(&SendPostMessageToDocumentOnMainThread, + base::Bind(&SendPostMessageToClientOnMainThread, make_scoped_refptr(embedded_context_->thread_safe_sender()), - GetRoutingID(), client_id, message, base::Passed(&channels))); + GetRoutingID(), + base::UTF16ToUTF8(uuid), + message, base::Passed(&channels))); } void ServiceWorkerScriptContext::PostCrossOriginMessageToClient( @@ -284,11 +287,12 @@ void ServiceWorkerScriptContext::PostCrossOriginMessageToClient( } void ServiceWorkerScriptContext::FocusClient( - int client_id, blink::WebServiceWorkerClientCallbacks* callback) { + const base::string16& uuid, + blink::WebServiceWorkerClientCallbacks* callback) { DCHECK(callback); int request_id = pending_client_callbacks_.Add(callback); - Send(new ServiceWorkerHostMsg_FocusClient( - GetRoutingID(), request_id, client_id)); + Send(new ServiceWorkerHostMsg_FocusClient(GetRoutingID(), request_id, + base::UTF16ToUTF8(uuid))); } void ServiceWorkerScriptContext::ClaimClients( diff --git a/content/renderer/service_worker/service_worker_script_context.h b/content/renderer/service_worker/service_worker_script_context.h index c220dd145499..d179834b8cd4 100644 --- a/content/renderer/service_worker/service_worker_script_context.h +++ b/content/renderer/service_worker/service_worker_script_context.h @@ -80,15 +80,15 @@ class ServiceWorkerScriptContext { blink::WebServiceWorkerClientCallbacks* callbacks); void SetCachedMetadata(const GURL& url, const char* data, size_t size); void ClearCachedMetadata(const GURL& url); - void PostMessageToDocument( - int client_id, + void PostMessageToClient( + const base::string16& uuid, const base::string16& message, scoped_ptr channels); void PostCrossOriginMessageToClient( const blink::WebCrossOriginServiceWorkerClient& client, const base::string16& message, scoped_ptr channels); - void FocusClient(int client_id, + void FocusClient(const base::string16& uuid, blink::WebServiceWorkerClientCallbacks* callback); void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks* callbacks); void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks* callbacks); -- 2.11.4.GIT