Commit 41d50fb4 authored by Han Leon's avatar Han Leon Committed by Commit Bot

[ServiceWorker] Implement ServiceWorkerHost.GetClient()

This CL mojofies the 2 legacy IPCs:
  ServiceWorkerHostMsg_GetClient
  ServiceWorkerMsg_DidGetClient

BUG=772793

Change-Id: I3a069713c1ed0068883214c6ba583eca758ed323
Reviewed-on: https://chromium-review.googlesource.com/880486
Commit-Queue: Han Leon <leon.han@intel.com>
Reviewed-by: default avatarKinuko Yasuda <kinuko@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarMakoto Shimazu <shimazu@chromium.org>
Cr-Commit-Position: refs/heads/master@{#532309}
parent 1c6d493b
...@@ -253,7 +253,7 @@ void ShowPaymentHandlerWindowOnUI( ...@@ -253,7 +253,7 @@ void ShowPaymentHandlerWindowOnUI(
void DidGetClients( void DidGetClients(
blink::mojom::ServiceWorkerHost::GetClientsCallback callback, blink::mojom::ServiceWorkerHost::GetClientsCallback callback,
std::unique_ptr<std::vector<blink::mojom::ServiceWorkerClientInfoPtr>> std::unique_ptr<service_worker_client_utils::ServiceWorkerClientPtrs>
clients) { clients) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
std::move(callback).Run(std::move(*clients)); std::move(callback).Run(std::move(*clients));
...@@ -1054,7 +1054,6 @@ void ServiceWorkerVersion::OnReportConsoleMessage(int source_identifier, ...@@ -1054,7 +1054,6 @@ void ServiceWorkerVersion::OnReportConsoleMessage(int source_identifier,
bool ServiceWorkerVersion::OnMessageReceived(const IPC::Message& message) { bool ServiceWorkerVersion::OnMessageReceived(const IPC::Message& message) {
bool handled = true; bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message) IPC_BEGIN_MESSAGE_MAP(ServiceWorkerVersion, message)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_GetClient, OnGetClient)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenNewTab, OnOpenNewTab) IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenNewTab, OnOpenNewTab)
IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenPaymentHandlerWindow, IPC_MESSAGE_HANDLER(ServiceWorkerHostMsg_OpenPaymentHandlerWindow,
OnOpenPaymentHandlerWindow) OnOpenPaymentHandlerWindow)
...@@ -1137,6 +1136,24 @@ void ServiceWorkerVersion::GetClients( ...@@ -1137,6 +1136,24 @@ void ServiceWorkerVersion::GetClients(
base::BindOnce(&DidGetClients, std::move(callback))); base::BindOnce(&DidGetClients, std::move(callback)));
} }
void ServiceWorkerVersion::GetClient(const std::string& client_uuid,
GetClientCallback callback) {
if (!context_) {
// The promise will be resolved to 'undefined'.
std::move(callback).Run(blink::mojom::ServiceWorkerClientInfo::New());
return;
}
ServiceWorkerProviderHost* provider_host =
context_->GetProviderHostByClientID(client_uuid);
if (!provider_host ||
provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) {
// The promise will be resolved to 'undefined'.
std::move(callback).Run(blink::mojom::ServiceWorkerClientInfo::New());
return;
}
service_worker_client_utils::GetClient(provider_host, std::move(callback));
}
void ServiceWorkerVersion::OnSetCachedMetadataFinished(int64_t callback_id, void ServiceWorkerVersion::OnSetCachedMetadataFinished(int64_t callback_id,
size_t size, size_t size,
int result) { int result) {
...@@ -1156,46 +1173,6 @@ void ServiceWorkerVersion::OnClearCachedMetadataFinished(int64_t callback_id, ...@@ -1156,46 +1173,6 @@ void ServiceWorkerVersion::OnClearCachedMetadataFinished(int64_t callback_id,
listener.OnCachedMetadataUpdated(this, 0); listener.OnCachedMetadataUpdated(this, 0);
} }
void ServiceWorkerVersion::OnGetClient(int request_id,
const std::string& client_uuid) {
if (!context_)
return;
TRACE_EVENT_ASYNC_BEGIN1("ServiceWorker", "ServiceWorkerVersion::OnGetClient",
request_id, "client_uuid", client_uuid);
ServiceWorkerProviderHost* provider_host =
context_->GetProviderHostByClientID(client_uuid);
if (!provider_host ||
provider_host->document_url().GetOrigin() != script_url_.GetOrigin()) {
// The promise will be resolved to 'undefined'.
OnGetClientFinished(request_id,
blink::mojom::ServiceWorkerClientInfo::New());
return;
}
service_worker_client_utils::GetClient(
provider_host, base::BindOnce(&ServiceWorkerVersion::OnGetClientFinished,
weak_factory_.GetWeakPtr(), request_id));
}
void ServiceWorkerVersion::OnGetClientFinished(
int request_id,
blink::mojom::ServiceWorkerClientInfoPtr client_info) {
DCHECK_CURRENTLY_ON(BrowserThread::IO);
TRACE_EVENT_ASYNC_END1(
"ServiceWorker", "ServiceWorkerVersion::OnGetClient", request_id,
"client_type",
ServiceWorkerUtils::ClientTypeToString(client_info->client_type));
// When Clients.get() is called on the script evaluation phase, the running
// status can be STARTING here.
if (running_status() != EmbeddedWorkerStatus::STARTING &&
running_status() != EmbeddedWorkerStatus::RUNNING) {
return;
}
embedded_worker_->SendIpcMessage(
ServiceWorkerMsg_DidGetClient(request_id, *client_info));
}
void ServiceWorkerVersion::OnSimpleEventFinished( void ServiceWorkerVersion::OnSimpleEventFinished(
int request_id, int request_id,
blink::mojom::ServiceWorkerEventStatus status, blink::mojom::ServiceWorkerEventStatus status,
......
...@@ -630,6 +630,8 @@ class CONTENT_EXPORT ServiceWorkerVersion ...@@ -630,6 +630,8 @@ class CONTENT_EXPORT ServiceWorkerVersion
void ClaimClients(ClaimClientsCallback callback) override; void ClaimClients(ClaimClientsCallback callback) override;
void GetClients(blink::mojom::ServiceWorkerClientQueryOptionsPtr options, void GetClients(blink::mojom::ServiceWorkerClientQueryOptionsPtr options,
GetClientsCallback callback) override; GetClientsCallback callback) override;
void GetClient(const std::string& client_uuid,
GetClientCallback callback) override;
void OnSetCachedMetadataFinished(int64_t callback_id, void OnSetCachedMetadataFinished(int64_t callback_id,
size_t size, size_t size,
...@@ -638,9 +640,6 @@ class CONTENT_EXPORT ServiceWorkerVersion ...@@ -638,9 +640,6 @@ class CONTENT_EXPORT ServiceWorkerVersion
// Message handlers. // Message handlers.
// This corresponds to the spec's get(id) steps.
void OnGetClient(int request_id, const std::string& client_uuid);
// Currently used for Clients.openWindow() only. // Currently used for Clients.openWindow() only.
void OnOpenNewTab(int request_id, const GURL& url); void OnOpenNewTab(int request_id, const GURL& url);
...@@ -692,10 +691,6 @@ class CONTENT_EXPORT ServiceWorkerVersion ...@@ -692,10 +691,6 @@ class CONTENT_EXPORT ServiceWorkerVersion
blink::mojom::ServiceWorkerEventStatus status, blink::mojom::ServiceWorkerEventStatus status,
base::Time dispatch_event_time); base::Time dispatch_event_time);
void OnGetClientFinished(
int request_id,
blink::mojom::ServiceWorkerClientInfoPtr client_info);
// The timeout timer periodically calls OnTimeoutTimer, which stops the worker // The timeout timer periodically calls OnTimeoutTimer, which stops the worker
// if it is excessively idle or unresponsive to ping. // if it is excessively idle or unresponsive to ping.
void StartTimeoutTimer(); void StartTimeoutTimer();
......
...@@ -115,11 +115,6 @@ IPC_MESSAGE_CONTROL5( ...@@ -115,11 +115,6 @@ IPC_MESSAGE_CONTROL5(
IPC_MESSAGE_CONTROL1(ServiceWorkerHostMsg_TerminateWorker, IPC_MESSAGE_CONTROL1(ServiceWorkerHostMsg_TerminateWorker,
int /* handle_id */) int /* handle_id */)
// Asks the browser to retrieve client of the sender ServiceWorker.
IPC_MESSAGE_ROUTED2(ServiceWorkerHostMsg_GetClient,
int /* request_id */,
std::string /* client_uuid */)
// Sends MessageEvent to a client (renderer->browser). // Sends MessageEvent to a client (renderer->browser).
IPC_MESSAGE_ROUTED3( IPC_MESSAGE_ROUTED3(
ServiceWorkerHostMsg_PostMessageToClient, ServiceWorkerHostMsg_PostMessageToClient,
...@@ -170,11 +165,6 @@ IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerStateChanged, ...@@ -170,11 +165,6 @@ IPC_MESSAGE_CONTROL3(ServiceWorkerMsg_ServiceWorkerStateChanged,
IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_DidSkipWaiting, IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_DidSkipWaiting,
int /* request_id */) int /* request_id */)
// Sent via EmbeddedWorker as a response of GetClient.
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_DidGetClient,
int /* request_id */,
blink::mojom::ServiceWorkerClientInfo)
// Sent via EmbeddedWorker as a response of OpenWindow. // Sent via EmbeddedWorker as a response of OpenWindow.
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_OpenWindowResponse, IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_OpenWindowResponse,
int /* request_id */, int /* request_id */,
......
...@@ -450,6 +450,17 @@ void DidClaimClients( ...@@ -450,6 +450,17 @@ void DidClaimClients(
callbacks->OnSuccess(); callbacks->OnSuccess();
} }
void DidGetClient(
std::unique_ptr<blink::WebServiceWorkerClientCallbacks> callbacks,
blink::mojom::ServiceWorkerClientInfoPtr client) {
std::unique_ptr<blink::WebServiceWorkerClientInfo> web_client;
if (!client->client_uuid.empty()) {
web_client = std::make_unique<blink::WebServiceWorkerClientInfo>(
ToWebServiceWorkerClientInfo(std::move(client)));
}
callbacks->OnSuccess(std::move(web_client));
}
} // namespace } // namespace
// Holding data that needs to be bound to the worker context on the // Holding data that needs to be bound to the worker context on the
...@@ -757,7 +768,6 @@ void ServiceWorkerContextClient::OnMessageReceived( ...@@ -757,7 +768,6 @@ void ServiceWorkerContextClient::OnMessageReceived(
CHECK_EQ(embedded_worker_id_, embedded_worker_id); CHECK_EQ(embedded_worker_id_, embedded_worker_id);
bool handled = true; bool handled = true;
IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message) IPC_BEGIN_MESSAGE_MAP(ServiceWorkerContextClient, message)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_DidGetClient, OnDidGetClient)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowResponse,
OnOpenWindowResponse) OnOpenWindowResponse)
IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError, IPC_MESSAGE_HANDLER(ServiceWorkerMsg_OpenWindowError,
...@@ -778,9 +788,9 @@ void ServiceWorkerContextClient::GetClient( ...@@ -778,9 +788,9 @@ void ServiceWorkerContextClient::GetClient(
const blink::WebString& id, const blink::WebString& id,
std::unique_ptr<blink::WebServiceWorkerClientCallbacks> callbacks) { std::unique_ptr<blink::WebServiceWorkerClientCallbacks> callbacks) {
DCHECK(callbacks); DCHECK(callbacks);
int request_id = context_->client_callbacks.Add(std::move(callbacks)); (*context_->service_worker_host)
Send(new ServiceWorkerHostMsg_GetClient(GetRoutingID(), request_id, ->GetClient(id.Utf8(), WrapCallbackThreadSafe(base::BindOnce(
id.Utf8())); &DidGetClient, std::move(callbacks))));
} }
void ServiceWorkerContextClient::GetClients( void ServiceWorkerContextClient::GetClients(
...@@ -1693,25 +1703,6 @@ void ServiceWorkerContextClient::DispatchPushEvent( ...@@ -1693,25 +1703,6 @@ void ServiceWorkerContextClient::DispatchPushEvent(
proxy_->DispatchPushEvent(request_id, data); proxy_->DispatchPushEvent(request_id, data);
} }
void ServiceWorkerContextClient::OnDidGetClient(
int request_id,
const blink::mojom::ServiceWorkerClientInfo& client) {
TRACE_EVENT0("ServiceWorker", "ServiceWorkerContextClient::OnDidGetClient");
blink::WebServiceWorkerClientCallbacks* callbacks =
context_->client_callbacks.Lookup(request_id);
if (!callbacks) {
NOTREACHED() << "Got stray response: " << request_id;
return;
}
std::unique_ptr<blink::WebServiceWorkerClientInfo> web_client;
if (!client.client_uuid.empty()) {
web_client.reset(new blink::WebServiceWorkerClientInfo(
ToWebServiceWorkerClientInfo(client)));
}
callbacks->OnSuccess(std::move(web_client));
context_->client_callbacks.Remove(request_id);
}
void ServiceWorkerContextClient::OnOpenWindowResponse( void ServiceWorkerContextClient::OnOpenWindowResponse(
int request_id, int request_id,
const blink::mojom::ServiceWorkerClientInfo& client) { const blink::mojom::ServiceWorkerClientInfo& client) {
......
...@@ -359,8 +359,6 @@ class CONTENT_EXPORT ServiceWorkerContextClient ...@@ -359,8 +359,6 @@ class CONTENT_EXPORT ServiceWorkerContextClient
const std::string& notification_id, const std::string& notification_id,
const PlatformNotificationData& notification_data); const PlatformNotificationData& notification_data);
void OnDidGetClient(int request_id,
const blink::mojom::ServiceWorkerClientInfo& client);
void OnOpenWindowResponse( void OnOpenWindowResponse(
int request_id, int request_id,
const blink::mojom::ServiceWorkerClientInfo& client); const blink::mojom::ServiceWorkerClientInfo& client);
......
...@@ -25,8 +25,11 @@ interface ServiceWorkerHost { ...@@ -25,8 +25,11 @@ interface ServiceWorkerHost {
GetClients(ServiceWorkerClientQueryOptions options) GetClients(ServiceWorkerClientQueryOptions options)
=> (array<ServiceWorkerClientInfo> clients); => (array<ServiceWorkerClientInfo> clients);
// Corresponds to Clients#get(id). Gets information of the service worker
// client with |client_uuid|.
GetClient(string client_uuid) => (ServiceWorkerClientInfo client);
// TODO(leonhsl): Impl all methods. // TODO(leonhsl): Impl all methods.
// GetClient();
// OpenNewTab(); // OpenNewTab();
// OpenNewPopup(); // OpenNewPopup();
// PostMessageToClient(); // PostMessageToClient();
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment