Commit aa91264d authored by mlamouri's avatar mlamouri Committed by Commit bot

[ServiceWorker] Update SWProviderHost::Focus() to return a Client.

Instead of returning a boolean to say whether the focus suceeded, this
is now returning an updated WindowClient which will contain the new
focus state.

This is a three sided CL:
Part 1: https://codereview.chromium.org/897493002
Part 2: <this>
Part 3: https://codereview.chromium.org/868233006

BUG=447212

Review URL: https://codereview.chromium.org/894853007

Cr-Commit-Position: refs/heads/master@{#315018}
parent be51cdcb
...@@ -5,6 +5,8 @@ ...@@ -5,6 +5,8 @@
#include "content/browser/service_worker/service_worker_provider_host.h" #include "content/browser/service_worker/service_worker_provider_host.h"
#include "base/stl_util.h" #include "base/stl_util.h"
#include "content/browser/frame_host/frame_tree.h"
#include "content/browser/frame_host/frame_tree_node.h"
#include "content/browser/frame_host/render_frame_host_impl.h" #include "content/browser/frame_host/render_frame_host_impl.h"
#include "content/browser/message_port_message_filter.h" #include "content/browser/message_port_message_filter.h"
#include "content/browser/service_worker/service_worker_context_core.h" #include "content/browser/service_worker/service_worker_context_core.h"
...@@ -15,35 +17,19 @@ ...@@ -15,35 +17,19 @@
#include "content/browser/service_worker/service_worker_registration_handle.h" #include "content/browser/service_worker/service_worker_registration_handle.h"
#include "content/browser/service_worker/service_worker_utils.h" #include "content/browser/service_worker/service_worker_utils.h"
#include "content/browser/service_worker/service_worker_version.h" #include "content/browser/service_worker/service_worker_version.h"
#include "content/browser/web_contents/web_contents_impl.h"
#include "content/common/resource_request_body.h" #include "content/common/resource_request_body.h"
#include "content/common/service_worker/service_worker_messages.h" #include "content/common/service_worker/service_worker_messages.h"
#include "content/common/service_worker/service_worker_types.h" #include "content/common/service_worker/service_worker_types.h"
#include "content/public/browser/render_frame_host.h" #include "content/public/browser/render_frame_host.h"
#include "content/public/browser/render_widget_host_view.h"
#include "content/public/browser/web_contents.h" #include "content/public/browser/web_contents.h"
#include "content/public/browser/web_contents_delegate.h"
#include "content/public/common/child_process_host.h" #include "content/public/common/child_process_host.h"
namespace content { namespace content {
namespace { namespace {
void FocusOnUIThread(int render_process_id,
int render_frame_id,
const ServiceWorkerProviderHost::FocusCallback& callback) {
WebContents* web_contents = WebContents::FromRenderFrameHost(
RenderFrameHost::FromID(render_process_id, render_frame_id));
bool result = false;
if (web_contents && web_contents->GetDelegate()) {
result = true;
web_contents->GetDelegate()->ActivateContents(web_contents);
}
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(callback, result));
}
void GetClientInfoOnUIThread( void GetClientInfoOnUIThread(
int render_process_id, int render_process_id,
int render_frame_id, int render_frame_id,
...@@ -70,6 +56,35 @@ void GetClientInfoOnUIThread( ...@@ -70,6 +56,35 @@ void GetClientInfoOnUIThread(
base::Bind(callback, client_info)); base::Bind(callback, client_info));
} }
void FocusOnUIThread(
int render_process_id,
int render_frame_id,
const ServiceWorkerProviderHost::GetClientInfoCallback& callback) {
RenderFrameHostImpl* render_frame_host =
RenderFrameHostImpl::FromID(render_process_id, render_frame_id);
WebContentsImpl* web_contents = static_cast<WebContentsImpl*>(
WebContents::FromRenderFrameHost(render_frame_host));
if (!render_frame_host || !web_contents) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(callback, ServiceWorkerClientInfo()));
return;
}
FrameTreeNode* frame_tree_node = render_frame_host->frame_tree_node();
// Focus the frame in the frame tree node, in case it has changed.
frame_tree_node->frame_tree()->SetFocusedFrame(frame_tree_node);
// Focus the frame's view to make sure the frame is now considered as focused.
render_frame_host->GetView()->Focus();
// Move the web contents to the foreground.
web_contents->Activate();
GetClientInfoOnUIThread(render_process_id, render_frame_id, callback);
}
} // anonymous namespace } // anonymous namespace
ServiceWorkerProviderHost::ServiceWorkerProviderHost( ServiceWorkerProviderHost::ServiceWorkerProviderHost(
...@@ -285,7 +300,7 @@ void ServiceWorkerProviderHost::PostMessage( ...@@ -285,7 +300,7 @@ void ServiceWorkerProviderHost::PostMessage(
new_routing_ids)); new_routing_ids));
} }
void ServiceWorkerProviderHost::Focus(const FocusCallback& callback) { void ServiceWorkerProviderHost::Focus(const GetClientInfoCallback& callback) {
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, BrowserThread::UI, FROM_HERE,
base::Bind(&FocusOnUIThread, base::Bind(&FocusOnUIThread,
......
...@@ -45,7 +45,6 @@ class CONTENT_EXPORT ServiceWorkerProviderHost ...@@ -45,7 +45,6 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
: public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener), : public NON_EXPORTED_BASE(ServiceWorkerRegistration::Listener),
public base::SupportsWeakPtr<ServiceWorkerProviderHost> { public base::SupportsWeakPtr<ServiceWorkerProviderHost> {
public: public:
using FocusCallback = base::Callback<void(bool)>;
using GetClientInfoCallback = using GetClientInfoCallback =
base::Callback<void(const ServiceWorkerClientInfo&)>; base::Callback<void(const ServiceWorkerClientInfo&)>;
...@@ -143,9 +142,8 @@ class CONTENT_EXPORT ServiceWorkerProviderHost ...@@ -143,9 +142,8 @@ class CONTENT_EXPORT ServiceWorkerProviderHost
// Activates the WebContents associated with // Activates the WebContents associated with
// { render_process_id_, render_frame_id_ }. // { render_process_id_, render_frame_id_ }.
// Runs the |callback| with the result in parameter describing whether the // Runs the |callback| with the updated ServiceWorkerClientInfo in parameter.
// focusing action was successful. void Focus(const GetClientInfoCallback& callback);
void Focus(const FocusCallback& callback);
// Asks the renderer to send back the document information. // Asks the renderer to send back the document information.
void GetClientInfo(const GetClientInfoCallback& callback) const; void GetClientInfo(const GetClientInfoCallback& callback) const;
......
...@@ -1247,7 +1247,7 @@ void ServiceWorkerVersion::OnPostMessageToDocument( ...@@ -1247,7 +1247,7 @@ void ServiceWorkerVersion::OnPostMessageToDocument(
void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) { void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) {
TRACE_EVENT2("ServiceWorker", TRACE_EVENT2("ServiceWorker",
"ServiceWorkerVersion::OnFocusDocument", "ServiceWorkerVersion::OnFocusClient",
"Request id", request_id, "Request id", request_id,
"Client id", client_id); "Client id", client_id);
ServiceWorkerProviderHost* provider_host = ServiceWorkerProviderHost* provider_host =
...@@ -1260,17 +1260,24 @@ void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) { ...@@ -1260,17 +1260,24 @@ void ServiceWorkerVersion::OnFocusClient(int request_id, int client_id) {
provider_host->Focus( provider_host->Focus(
base::Bind(&ServiceWorkerVersion::OnFocusClientFinished, base::Bind(&ServiceWorkerVersion::OnFocusClientFinished,
weak_factory_.GetWeakPtr(), weak_factory_.GetWeakPtr(),
request_id)); request_id,
client_id));
} }
void ServiceWorkerVersion::OnFocusClientFinished(int request_id, bool result) { void ServiceWorkerVersion::OnFocusClientFinished(
int request_id,
int cliend_id,
const ServiceWorkerClientInfo& client) {
DCHECK_CURRENTLY_ON(BrowserThread::IO); DCHECK_CURRENTLY_ON(BrowserThread::IO);
if (running_status() != RUNNING) if (running_status() != RUNNING)
return; return;
ServiceWorkerClientInfo client_info(client);
client_info.client_id = cliend_id;
embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse( embedded_worker_->SendMessage(ServiceWorkerMsg_FocusClientResponse(
request_id, result)); request_id, client_info));
} }
void ServiceWorkerVersion::OnSkipWaiting(int request_id) { void ServiceWorkerVersion::OnSkipWaiting(int request_id) {
......
...@@ -373,7 +373,9 @@ class CONTENT_EXPORT ServiceWorkerVersion ...@@ -373,7 +373,9 @@ class CONTENT_EXPORT ServiceWorkerVersion
void OnSkipWaiting(int request_id); void OnSkipWaiting(int request_id);
void OnClaimClients(int request_id); void OnClaimClients(int request_id);
void OnFocusClientFinished(int request_id, bool result); void OnFocusClientFinished(int request_id,
int client_id,
const ServiceWorkerClientInfo& client);
void DidSkipWaiting(int request_id); void DidSkipWaiting(int request_id);
void DidClaimClients(int request_id, ServiceWorkerStatusCode status); void DidClaimClients(int request_id, ServiceWorkerStatusCode status);
......
...@@ -461,7 +461,7 @@ IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_OpenWindowError, ...@@ -461,7 +461,7 @@ IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_OpenWindowError,
// Sent via EmbeddedWorker as a response of FocusClient. // Sent via EmbeddedWorker as a response of FocusClient.
IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_FocusClientResponse, IPC_MESSAGE_CONTROL2(ServiceWorkerMsg_FocusClientResponse,
int /* request_id */, int /* request_id */,
bool /* result */) content::ServiceWorkerClientInfo /* client */)
// Sent via EmbeddedWorker at successful completion of CacheStorage operations. // Sent via EmbeddedWorker at successful completion of CacheStorage operations.
IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_CacheStorageHasSuccess, IPC_MESSAGE_CONTROL1(ServiceWorkerMsg_CacheStorageHasSuccess,
......
...@@ -398,7 +398,7 @@ void EmbeddedWorkerContextClient::postMessageToCrossOriginClient( ...@@ -398,7 +398,7 @@ void EmbeddedWorkerContextClient::postMessageToCrossOriginClient(
} }
void EmbeddedWorkerContextClient::focus( void EmbeddedWorkerContextClient::focus(
int client_id, blink::WebServiceWorkerClientFocusCallback* callback) { int client_id, blink::WebServiceWorkerClientCallbacks* callback) {
DCHECK(script_context_); DCHECK(script_context_);
script_context_->FocusClient(client_id, callback); script_context_->FocusClient(client_id, callback);
} }
......
...@@ -118,7 +118,7 @@ class EmbeddedWorkerContextClient ...@@ -118,7 +118,7 @@ class EmbeddedWorkerContextClient
const blink::WebString& message, const blink::WebString& message,
blink::WebMessagePortChannelArray* channels); blink::WebMessagePortChannelArray* channels);
virtual void focus(int client_id, virtual void focus(int client_id,
blink::WebServiceWorkerClientFocusCallback*); blink::WebServiceWorkerClientCallbacks*);
virtual void skipWaiting( virtual void skipWaiting(
blink::WebServiceWorkerSkipWaitingCallbacks* callbacks); blink::WebServiceWorkerSkipWaitingCallbacks* callbacks);
virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks* callbacks); virtual void claim(blink::WebServiceWorkerClientsClaimCallbacks* callbacks);
......
...@@ -264,9 +264,9 @@ void ServiceWorkerScriptContext::PostCrossOriginMessageToClient( ...@@ -264,9 +264,9 @@ void ServiceWorkerScriptContext::PostCrossOriginMessageToClient(
} }
void ServiceWorkerScriptContext::FocusClient( void ServiceWorkerScriptContext::FocusClient(
int client_id, blink::WebServiceWorkerClientFocusCallback* callback) { int client_id, blink::WebServiceWorkerClientCallbacks* callback) {
DCHECK(callback); DCHECK(callback);
int request_id = pending_focus_client_callbacks_.Add(callback); int request_id = pending_client_callbacks_.Add(callback);
Send(new ServiceWorkerHostMsg_FocusClient( Send(new ServiceWorkerHostMsg_FocusClient(
GetRoutingID(), request_id, client_id)); GetRoutingID(), request_id, client_id));
} }
...@@ -500,18 +500,31 @@ void ServiceWorkerScriptContext::OnOpenWindowError(int request_id) { ...@@ -500,18 +500,31 @@ void ServiceWorkerScriptContext::OnOpenWindowError(int request_id) {
pending_client_callbacks_.Remove(request_id); pending_client_callbacks_.Remove(request_id);
} }
void ServiceWorkerScriptContext::OnFocusClientResponse(int request_id, void ServiceWorkerScriptContext::OnFocusClientResponse(
bool result) { int request_id, const ServiceWorkerClientInfo& client) {
TRACE_EVENT0("ServiceWorker", TRACE_EVENT0("ServiceWorker",
"ServiceWorkerScriptContext::OnFocusClientResponse"); "ServiceWorkerScriptContext::OnFocusClientResponse");
blink::WebServiceWorkerClientFocusCallback* callback = blink::WebServiceWorkerClientCallbacks* callback =
pending_focus_client_callbacks_.Lookup(request_id); pending_client_callbacks_.Lookup(request_id);
if (!callback) { if (!callback) {
NOTREACHED() << "Got stray response: " << request_id; NOTREACHED() << "Got stray response: " << request_id;
return; return;
} }
callback->onSuccess(&result); if (!client.IsEmpty()) {
pending_focus_client_callbacks_.Remove(request_id); DCHECK(client.IsValid());
scoped_ptr<blink::WebServiceWorkerClientInfo> web_client (
new blink::WebServiceWorkerClientInfo(
ToWebServiceWorkerClientInfo(client)));
callback->onSuccess(web_client.release());
} else {
scoped_ptr<blink::WebServiceWorkerError> error(
new blink::WebServiceWorkerError(
blink::WebServiceWorkerError::ErrorTypeNotFound,
"The WindowClient was not found."));
callback->onError(error.release());
}
pending_client_callbacks_.Remove(request_id);
} }
void ServiceWorkerScriptContext::OnDidSkipWaiting(int request_id) { void ServiceWorkerScriptContext::OnDidSkipWaiting(int request_id) {
......
...@@ -19,7 +19,6 @@ ...@@ -19,7 +19,6 @@
#include "content/renderer/service_worker/service_worker_cache_storage_dispatcher.h" #include "content/renderer/service_worker/service_worker_cache_storage_dispatcher.h"
#include "third_party/WebKit/public/platform/WebGeofencingEventType.h" #include "third_party/WebKit/public/platform/WebGeofencingEventType.h"
#include "third_party/WebKit/public/platform/WebMessagePortChannel.h" #include "third_party/WebKit/public/platform/WebMessagePortChannel.h"
#include "third_party/WebKit/public/platform/WebServiceWorkerClientFocusCallback.h"
#include "third_party/WebKit/public/platform/WebServiceWorkerClientsClaimCallbacks.h" #include "third_party/WebKit/public/platform/WebServiceWorkerClientsClaimCallbacks.h"
#include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h" #include "third_party/WebKit/public/platform/WebServiceWorkerClientsInfo.h"
#include "third_party/WebKit/public/platform/WebServiceWorkerError.h" #include "third_party/WebKit/public/platform/WebServiceWorkerError.h"
...@@ -86,7 +85,7 @@ class ServiceWorkerScriptContext { ...@@ -86,7 +85,7 @@ class ServiceWorkerScriptContext {
const base::string16& message, const base::string16& message,
scoped_ptr<blink::WebMessagePortChannelArray> channels); scoped_ptr<blink::WebMessagePortChannelArray> channels);
void FocusClient(int client_id, void FocusClient(int client_id,
blink::WebServiceWorkerClientFocusCallback* callback); blink::WebServiceWorkerClientCallbacks* callback);
void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks* callbacks); void SkipWaiting(blink::WebServiceWorkerSkipWaitingCallbacks* callbacks);
void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks* callbacks); void ClaimClients(blink::WebServiceWorkerClientsClaimCallbacks* callbacks);
...@@ -108,8 +107,6 @@ class ServiceWorkerScriptContext { ...@@ -108,8 +107,6 @@ class ServiceWorkerScriptContext {
ClaimClientsCallbacksMap; ClaimClientsCallbacksMap;
typedef IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer> typedef IDMap<blink::WebServiceWorkerClientCallbacks, IDMapOwnPointer>
ClientCallbacksMap; ClientCallbacksMap;
typedef IDMap<blink::WebServiceWorkerClientFocusCallback, IDMapOwnPointer>
FocusClientCallbacksMap;
typedef IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer> typedef IDMap<blink::WebServiceWorkerSkipWaitingCallbacks, IDMapOwnPointer>
SkipWaitingCallbacksMap; SkipWaitingCallbacksMap;
...@@ -141,7 +138,8 @@ class ServiceWorkerScriptContext { ...@@ -141,7 +138,8 @@ class ServiceWorkerScriptContext {
void OnOpenWindowResponse(int request_id, void OnOpenWindowResponse(int request_id,
const ServiceWorkerClientInfo& client); const ServiceWorkerClientInfo& client);
void OnOpenWindowError(int request_id); void OnOpenWindowError(int request_id);
void OnFocusClientResponse(int request_id, bool result); void OnFocusClientResponse(int request_id,
const ServiceWorkerClientInfo& client);
void OnDidSkipWaiting(int request_id); void OnDidSkipWaiting(int request_id);
void OnDidClaimClients(int request_id); void OnDidClaimClients(int request_id);
void OnClaimClientsError(int request_id, void OnClaimClientsError(int request_id,
...@@ -164,12 +162,9 @@ class ServiceWorkerScriptContext { ...@@ -164,12 +162,9 @@ class ServiceWorkerScriptContext {
// Pending callbacks for GetClientDocuments(). // Pending callbacks for GetClientDocuments().
ClientsCallbacksMap pending_clients_callbacks_; ClientsCallbacksMap pending_clients_callbacks_;
// Pending callbacks for OpenWindow(). // Pending callbacks for OpenWindow() and FocusClient().
ClientCallbacksMap pending_client_callbacks_; ClientCallbacksMap pending_client_callbacks_;
// Pending callbacks for FocusClient().
FocusClientCallbacksMap pending_focus_client_callbacks_;
// Pending callbacks for SkipWaiting(). // Pending callbacks for SkipWaiting().
SkipWaitingCallbacksMap pending_skip_waiting_callbacks_; SkipWaitingCallbacksMap pending_skip_waiting_callbacks_;
......
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