Index: content/browser/service_worker/service_worker_client_utils.cc |
diff --git a/content/browser/service_worker/service_worker_client_utils.cc b/content/browser/service_worker/service_worker_client_utils.cc |
index 2888bd48de6fa6f1f0374f70b58940686f576798..6f467d718472ef29fffa25b7398595a15fcd3bd1 100644 |
--- a/content/browser/service_worker/service_worker_client_utils.cc |
+++ b/content/browser/service_worker/service_worker_client_utils.cc |
@@ -102,6 +102,7 @@ class OpenURLObserver : public WebContentsObserver { |
ServiceWorkerClientInfo GetWindowClientInfoOnUI( |
int render_process_id, |
int render_frame_id, |
+ base::TimeTicks create_time, |
const std::string& client_uuid) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
RenderFrameHostImpl* render_frame_host = |
@@ -117,12 +118,13 @@ ServiceWorkerClientInfo GetWindowClientInfoOnUI( |
render_frame_host->IsFocused(), render_frame_host->GetLastCommittedURL(), |
render_frame_host->GetParent() ? REQUEST_CONTEXT_FRAME_TYPE_NESTED |
: REQUEST_CONTEXT_FRAME_TYPE_TOP_LEVEL, |
- render_frame_host->frame_tree_node()->last_focus_time(), |
+ render_frame_host->frame_tree_node()->last_focus_time(), create_time, |
blink::kWebServiceWorkerClientTypeWindow); |
} |
ServiceWorkerClientInfo FocusOnUI(int render_process_id, |
int render_frame_id, |
+ base::TimeTicks create_time, |
const std::string& client_uuid) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
RenderFrameHostImpl* render_frame_host = |
@@ -146,7 +148,7 @@ ServiceWorkerClientInfo FocusOnUI(int render_process_id, |
web_contents->Activate(); |
return GetWindowClientInfoOnUI(render_process_id, render_frame_id, |
- client_uuid); |
+ create_time, client_uuid); |
} |
// This is only called for main frame navigations in OpenWindowOnUI(). |
@@ -274,7 +276,8 @@ void DidNavigate(const base::WeakPtr<ServiceWorkerContextCore>& context, |
BrowserThread::PostTaskAndReplyWithResult( |
BrowserThread::UI, FROM_HERE, |
base::Bind(&GetWindowClientInfoOnUI, provider_host->process_id(), |
- provider_host->route_id(), provider_host->client_uuid()), |
+ provider_host->route_id(), provider_host->create_time(), |
+ provider_host->client_uuid()), |
base::Bind(callback, SERVICE_WORKER_OK)); |
return; |
} |
@@ -286,11 +289,13 @@ void DidNavigate(const base::WeakPtr<ServiceWorkerContextCore>& context, |
void AddWindowClient( |
ServiceWorkerProviderHost* host, |
- std::vector<std::tuple<int, int, std::string>>* client_info) { |
+ std::vector<std::tuple<int, int, base::TimeTicks, std::string>>* |
+ client_info) { |
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
if (host->client_type() != blink::kWebServiceWorkerClientTypeWindow) |
return; |
client_info->push_back(std::make_tuple(host->process_id(), host->frame_id(), |
+ host->create_time(), |
host->client_uuid())); |
} |
@@ -309,13 +314,14 @@ void AddNonWindowClient(ServiceWorkerProviderHost* host, |
host->client_uuid(), blink::kWebPageVisibilityStateHidden, |
false, // is_focused |
host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, base::TimeTicks(), |
- host_client_type); |
+ host->create_time(), host_client_type); |
clients->push_back(client_info); |
} |
void OnGetWindowClientsOnUI( |
// The tuple contains process_id, frame_id, client_uuid. |
- const std::vector<std::tuple<int, int, std::string>>& clients_info, |
+ const std::vector<std::tuple<int, int, base::TimeTicks, std::string>>& |
+ clients_info, |
const GURL& script_url, |
const GetWindowClientsCallback& callback) { |
DCHECK_CURRENTLY_ON(BrowserThread::UI); |
@@ -323,7 +329,7 @@ void OnGetWindowClientsOnUI( |
std::unique_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); |
shimazu
2017/05/25 08:56:26
Could you use base::MakeUnique<ServiceWorkerClient
xiaofengzhang
2017/05/26 04:01:28
Done.
|
for (const auto& it : clients_info) { |
ServiceWorkerClientInfo info = GetWindowClientInfoOnUI( |
- std::get<0>(it), std::get<1>(it), std::get<2>(it)); |
+ std::get<0>(it), std::get<1>(it), std::get<2>(it), std::get<3>(it)); |
// If the request to the provider_host returned an empty |
// ServiceWorkerClientInfo, that means that it wasn't possible to associate |
@@ -345,6 +351,13 @@ void OnGetWindowClientsOnUI( |
base::Bind(callback, base::Passed(&clients))); |
} |
+struct ServiceWorkerClientTypeIsWindow { |
+ bool operator()(const ServiceWorkerClientInfo& a, |
+ const ServiceWorkerClientInfo& b) const { |
+ return a.client_type == blink::kWebServiceWorkerClientTypeWindow; |
+ } |
+}; |
xiaofengzhang
2017/05/26 04:01:28
Sorry, this part is not needed
shimazu
2017/05/26 08:10:07
Acknowledged.
|
+ |
struct ServiceWorkerClientInfoSortMRU { |
bool operator()(const ServiceWorkerClientInfo& a, |
const ServiceWorkerClientInfo& b) const { |
@@ -352,12 +365,41 @@ struct ServiceWorkerClientInfoSortMRU { |
} |
}; |
+struct ServiceWorkerClientInfoSortCreateTime { |
+ bool operator()(const ServiceWorkerClientInfo& a, |
+ const ServiceWorkerClientInfo& b) const { |
+ return a.create_time < b.create_time; |
+ } |
+}; |
+ |
void DidGetClients(const ClientsCallback& callback, |
ServiceWorkerClients* clients) { |
shimazu
2017/05/25 08:56:26
Could you change the type to std::unique_ptr<Servi
xiaofengzhang
2017/05/26 04:01:27
Done.
|
DCHECK_CURRENTLY_ON(BrowserThread::IO); |
- // Sort clients so that the most recently active tab is in the front. |
- std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); |
+ // Window clients are always placed before worker clients. |
+ auto window_clients_bound = std::stable_partition( |
+ clients->begin(), clients->end(), [](ServiceWorkerClientInfo info) { |
shimazu
2017/05/25 08:56:26
I feel it's more simple to implement operator< in
xiaofengzhang
2017/05/26 04:01:27
Thanks shimazu. please see the cl#2.
The reasons I
shimazu
2017/05/26 08:10:07
It looks good, thanks!
|
+ return info.client_type == blink::kWebServiceWorkerClientTypeWindow; |
+ }); |
+ |
+ // Window clients that have ever been focused are placed first. |
+ auto focused_bound = std::stable_partition( |
+ clients->begin(), window_clients_bound, [](ServiceWorkerClientInfo info) { |
+ return info.last_focus_time > base::TimeTicks(); |
+ }); |
+ |
+ // Sort focused window clients so that the most recently active tab is in the |
+ // front. |
+ std::sort(clients->begin(), focused_bound, ServiceWorkerClientInfoSortMRU()); |
+ |
+ // Sort non-focused window clients so that the earliest created tab is in the |
+ // front. |
+ std::sort(focused_bound, clients->end(), |
+ ServiceWorkerClientInfoSortCreateTime()); |
+ |
+ // Sort worker clients so that the earliest created one is in the front. |
+ std::sort(window_clients_bound, clients->end(), |
+ ServiceWorkerClientInfoSortCreateTime()); |
callback.Run(clients); |
} |
@@ -395,10 +437,11 @@ void GetWindowClients(const base::WeakPtr<ServiceWorkerVersion>& controller, |
DCHECK(options.client_type == blink::kWebServiceWorkerClientTypeWindow || |
options.client_type == blink::kWebServiceWorkerClientTypeAll); |
- std::vector<std::tuple<int, int, std::string>> clients_info; |
+ std::vector<std::tuple<int, int, base::TimeTicks, std::string>> clients_info; |
if (!options.include_uncontrolled) { |
- for (auto& controllee : controller->controllee_map()) |
+ for (auto& controllee : controller->controllee_map()) { |
AddWindowClient(controllee.second, &clients_info); |
+ } |
} else if (controller->context()) { |
GURL origin = controller->script_url().GetOrigin(); |
for (auto it = controller->context()->GetClientProviderHostIterator(origin); |
@@ -430,7 +473,8 @@ void FocusWindowClient(ServiceWorkerProviderHost* provider_host, |
BrowserThread::PostTaskAndReplyWithResult( |
BrowserThread::UI, FROM_HERE, |
base::Bind(&FocusOnUI, provider_host->process_id(), |
- provider_host->frame_id(), provider_host->client_uuid()), |
+ provider_host->frame_id(), provider_host->create_time(), |
+ provider_host->client_uuid()), |
callback); |
} |
@@ -476,7 +520,8 @@ void GetClient(ServiceWorkerProviderHost* provider_host, |
BrowserThread::PostTaskAndReplyWithResult( |
BrowserThread::UI, FROM_HERE, |
base::Bind(&GetWindowClientInfoOnUI, provider_host->process_id(), |
- provider_host->route_id(), provider_host->client_uuid()), |
+ provider_host->route_id(), provider_host->create_time(), |
+ provider_host->client_uuid()), |
callback); |
return; |
} |
@@ -485,7 +530,8 @@ void GetClient(ServiceWorkerProviderHost* provider_host, |
provider_host->client_uuid(), blink::kWebPageVisibilityStateHidden, |
false, // is_focused |
provider_host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, |
- base::TimeTicks(), provider_host->client_type()); |
+ base::TimeTicks(), provider_host->create_time(), |
+ provider_host->client_type()); |
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE, |
base::Bind(callback, client_info)); |
} |