Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(35)

Side by Side Diff: content/browser/service_worker/service_worker_version.cc

Issue 1285373002: Ensure that Service Worker clients are always returned in MRU order (1) (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Fix typos and style. Created 5 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright 2013 The Chromium Authors. All rights reserved. 1 // Copyright 2013 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "content/browser/service_worker/service_worker_version.h" 5 #include "content/browser/service_worker/service_worker_version.h"
6 6
7 #include <algorithm>
7 #include <map> 8 #include <map>
8 #include <string> 9 #include <string>
9 10
10 #include "base/command_line.h" 11 #include "base/command_line.h"
11 #include "base/guid.h" 12 #include "base/guid.h"
12 #include "base/location.h" 13 #include "base/location.h"
13 #include "base/memory/ref_counted.h" 14 #include "base/memory/ref_counted.h"
14 #include "base/metrics/histogram_macros.h" 15 #include "base/metrics/histogram_macros.h"
15 #include "base/single_thread_task_runner.h" 16 #include "base/single_thread_task_runner.h"
16 #include "base/stl_util.h" 17 #include "base/stl_util.h"
(...skipping 354 matching lines...) Expand 10 before | Expand all | Expand 10 after
371 void AddNonWindowClient(ServiceWorkerProviderHost* host, 372 void AddNonWindowClient(ServiceWorkerProviderHost* host,
372 const ServiceWorkerClientQueryOptions& options, 373 const ServiceWorkerClientQueryOptions& options,
373 ServiceWorkerClients* clients) { 374 ServiceWorkerClients* clients) {
374 blink::WebServiceWorkerClientType host_client_type = host->client_type(); 375 blink::WebServiceWorkerClientType host_client_type = host->client_type();
375 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) 376 if (host_client_type == blink::WebServiceWorkerClientTypeWindow)
376 return; 377 return;
377 if (options.client_type != blink::WebServiceWorkerClientTypeAll && 378 if (options.client_type != blink::WebServiceWorkerClientTypeAll &&
378 options.client_type != host_client_type) 379 options.client_type != host_client_type)
379 return; 380 return;
380 381
381 ServiceWorkerClientInfo client_info( 382 ServiceWorkerClientInfo client_info(blink::WebPageVisibilityStateHidden,
382 blink::WebPageVisibilityStateHidden, 383 false, // is_focused
383 false, // is_focused 384 host->document_url(),
384 host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, host_client_type); 385 REQUEST_CONTEXT_FRAME_TYPE_NONE,
386 base::TimeTicks(), host_client_type);
385 client_info.client_uuid = host->client_uuid(); 387 client_info.client_uuid = host->client_uuid();
386 clients->push_back(client_info); 388 clients->push_back(client_info);
387 } 389 }
388 390
389 bool IsInstalled(ServiceWorkerVersion::Status status) { 391 bool IsInstalled(ServiceWorkerVersion::Status status) {
390 switch (status) { 392 switch (status) {
391 case ServiceWorkerVersion::NEW: 393 case ServiceWorkerVersion::NEW:
392 case ServiceWorkerVersion::INSTALLING: 394 case ServiceWorkerVersion::INSTALLING:
393 case ServiceWorkerVersion::REDUNDANT: 395 case ServiceWorkerVersion::REDUNDANT:
394 return false; 396 return false;
(...skipping 869 matching lines...) Expand 10 before | Expand all | Expand 10 after
1264 } 1266 }
1265 1267
1266 void ServiceWorkerVersion::OnGetClients( 1268 void ServiceWorkerVersion::OnGetClients(
1267 int request_id, 1269 int request_id,
1268 const ServiceWorkerClientQueryOptions& options) { 1270 const ServiceWorkerClientQueryOptions& options) {
1269 TRACE_EVENT_ASYNC_BEGIN2( 1271 TRACE_EVENT_ASYNC_BEGIN2(
1270 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, 1272 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id,
1271 "client_type", options.client_type, "include_uncontrolled", 1273 "client_type", options.client_type, "include_uncontrolled",
1272 options.include_uncontrolled); 1274 options.include_uncontrolled);
1273 1275
1276 ServiceWorkerClients clients;
1274 if (controllee_map_.empty() && !options.include_uncontrolled) { 1277 if (controllee_map_.empty() && !options.include_uncontrolled) {
1275 OnGetClientsFinished(request_id, std::vector<ServiceWorkerClientInfo>()); 1278 OnGetClientsFinished(request_id, &clients);
1276 return; 1279 return;
1277 } 1280 }
1278 1281
1279 // For Window clients we want to query the info on the UI thread first. 1282 // For Window clients we want to query the info on the UI thread first.
1280 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || 1283 if (options.client_type == blink::WebServiceWorkerClientTypeWindow ||
1281 options.client_type == blink::WebServiceWorkerClientTypeAll) { 1284 options.client_type == blink::WebServiceWorkerClientTypeAll) {
1282 GetWindowClients(request_id, options); 1285 GetWindowClients(request_id, options);
1283 return; 1286 return;
1284 } 1287 }
1285 1288
1286 ServiceWorkerClients clients;
1287 GetNonWindowClients(request_id, options, &clients); 1289 GetNonWindowClients(request_id, options, &clients);
1288 OnGetClientsFinished(request_id, clients); 1290 OnGetClientsFinished(request_id, &clients);
1289 } 1291 }
1290 1292
1291 void ServiceWorkerVersion::OnGetClientsFinished( 1293 namespace {
1292 int request_id, 1294 struct ServiceWorkerClientInfoSortMRU {
1293 const ServiceWorkerClients& clients) { 1295 bool operator()(const ServiceWorkerClientInfo& a,
1296 const ServiceWorkerClientInfo& b) const {
1297 return a.last_active_time > b.last_active_time;
1298 }
1299 };
1300 }
nhiroki 2015/08/17 06:43:22 We already have an unnamed namespace starting at l
jeremyarcher 2015/08/17 06:51:57 Ah, whoops. Added.
1301
1302 void ServiceWorkerVersion::OnGetClientsFinished(int request_id,
1303 ServiceWorkerClients* clients) {
1294 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1304 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1295 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", 1305 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients",
1296 request_id, "The number of clients", clients.size()); 1306 request_id, "The number of clients", clients->size());
1297 1307
1298 if (running_status() != RUNNING) 1308 if (running_status() != RUNNING)
1299 return; 1309 return;
1310 // Sort clients so that the most recently active tab is in the front.
1311 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU());
1300 embedded_worker_->SendMessage( 1312 embedded_worker_->SendMessage(
1301 ServiceWorkerMsg_DidGetClients(request_id, clients)); 1313 ServiceWorkerMsg_DidGetClients(request_id, *clients));
1302 } 1314 }
1303 1315
1304 void ServiceWorkerVersion::OnActivateEventFinished( 1316 void ServiceWorkerVersion::OnActivateEventFinished(
1305 int request_id, 1317 int request_id,
1306 blink::WebServiceWorkerEventResult result) { 1318 blink::WebServiceWorkerEventResult result) {
1307 DCHECK(ACTIVATING == status() || 1319 DCHECK(ACTIVATING == status() ||
1308 REDUNDANT == status()) << status(); 1320 REDUNDANT == status()) << status();
1309 TRACE_EVENT0("ServiceWorker", 1321 TRACE_EVENT0("ServiceWorker",
1310 "ServiceWorkerVersion::OnActivateEventFinished"); 1322 "ServiceWorkerVersion::OnActivateEventFinished");
1311 1323
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
1910 return clients_info; 1922 return clients_info;
1911 } 1923 }
1912 1924
1913 void ServiceWorkerVersion::DidGetWindowClients( 1925 void ServiceWorkerVersion::DidGetWindowClients(
1914 int request_id, 1926 int request_id,
1915 const ServiceWorkerClientQueryOptions& options, 1927 const ServiceWorkerClientQueryOptions& options,
1916 scoped_ptr<ServiceWorkerClients> clients) { 1928 scoped_ptr<ServiceWorkerClients> clients) {
1917 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1929 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1918 if (options.client_type == blink::WebServiceWorkerClientTypeAll) 1930 if (options.client_type == blink::WebServiceWorkerClientTypeAll)
1919 GetNonWindowClients(request_id, options, clients.get()); 1931 GetNonWindowClients(request_id, options, clients.get());
1920 OnGetClientsFinished(request_id, *clients); 1932 OnGetClientsFinished(request_id, clients.get());
1921 } 1933 }
1922 1934
1923 void ServiceWorkerVersion::GetNonWindowClients( 1935 void ServiceWorkerVersion::GetNonWindowClients(
1924 int request_id, 1936 int request_id,
1925 const ServiceWorkerClientQueryOptions& options, 1937 const ServiceWorkerClientQueryOptions& options,
1926 ServiceWorkerClients* clients) { 1938 ServiceWorkerClients* clients) {
1927 if (!options.include_uncontrolled) { 1939 if (!options.include_uncontrolled) {
1928 for (auto& controllee : controllee_map_) { 1940 for (auto& controllee : controllee_map_) {
1929 AddNonWindowClient(controllee.second, options, clients); 1941 AddNonWindowClient(controllee.second, options, clients);
1930 } 1942 }
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
2298 base::string16()); 2310 base::string16());
2299 service_port_dispatcher_.reset(); 2311 service_port_dispatcher_.reset();
2300 } 2312 }
2301 2313
2302 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() { 2314 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() {
2303 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); 2315 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED);
2304 background_sync_dispatcher_.reset(); 2316 background_sync_dispatcher_.reset();
2305 } 2317 }
2306 2318
2307 } // namespace content 2319 } // namespace content
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698