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

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 inconsistencies. 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 353 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 void AddNonWindowClient(ServiceWorkerProviderHost* host, 371 void AddNonWindowClient(ServiceWorkerProviderHost* host,
371 const ServiceWorkerClientQueryOptions& options, 372 const ServiceWorkerClientQueryOptions& options,
372 ServiceWorkerClients* clients) { 373 ServiceWorkerClients* clients) {
373 blink::WebServiceWorkerClientType host_client_type = host->client_type(); 374 blink::WebServiceWorkerClientType host_client_type = host->client_type();
374 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) 375 if (host_client_type == blink::WebServiceWorkerClientTypeWindow)
375 return; 376 return;
376 if (options.client_type != blink::WebServiceWorkerClientTypeAll && 377 if (options.client_type != blink::WebServiceWorkerClientTypeAll &&
377 options.client_type != host_client_type) 378 options.client_type != host_client_type)
378 return; 379 return;
379 380
380 ServiceWorkerClientInfo client_info( 381 ServiceWorkerClientInfo client_info(blink::WebPageVisibilityStateHidden,
381 blink::WebPageVisibilityStateHidden, 382 false, // is_focused
382 false, // is_focused 383 host->document_url(),
383 host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, host_client_type); 384 REQUEST_CONTEXT_FRAME_TYPE_NONE,
385 base::TimeTicks(), host_client_type);
384 client_info.client_uuid = host->client_uuid(); 386 client_info.client_uuid = host->client_uuid();
385 clients->push_back(client_info); 387 clients->push_back(client_info);
386 } 388 }
387 389
388 bool IsInstalled(ServiceWorkerVersion::Status status) { 390 bool IsInstalled(ServiceWorkerVersion::Status status) {
389 switch (status) { 391 switch (status) {
390 case ServiceWorkerVersion::NEW: 392 case ServiceWorkerVersion::NEW:
391 case ServiceWorkerVersion::INSTALLING: 393 case ServiceWorkerVersion::INSTALLING:
392 case ServiceWorkerVersion::REDUNDANT: 394 case ServiceWorkerVersion::REDUNDANT:
393 return false; 395 return false;
394 case ServiceWorkerVersion::INSTALLED: 396 case ServiceWorkerVersion::INSTALLED:
395 case ServiceWorkerVersion::ACTIVATING: 397 case ServiceWorkerVersion::ACTIVATING:
396 case ServiceWorkerVersion::ACTIVATED: 398 case ServiceWorkerVersion::ACTIVATED:
397 return true; 399 return true;
398 } 400 }
399 NOTREACHED() << "Unexpected status: " << status; 401 NOTREACHED() << "Unexpected status: " << status;
400 return false; 402 return false;
401 } 403 }
402 404
405 struct ServiceWorkerClientInfoSortMRU {
406 bool operator()(const ServiceWorkerClientInfo& a,
407 const ServiceWorkerClientInfo& b) const {
408 return a.last_focus_time > b.last_focus_time;
409 }
410 };
411
403 } // namespace 412 } // namespace
404 413
405 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; 414 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5;
406 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; 415 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5;
407 416
408 class ServiceWorkerVersion::Metrics { 417 class ServiceWorkerVersion::Metrics {
409 public: 418 public:
410 using EventType = ServiceWorkerMetrics::EventType; 419 using EventType = ServiceWorkerMetrics::EventType;
411 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {} 420 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {}
412 ~Metrics() { 421 ~Metrics() {
(...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after
1266 } 1275 }
1267 1276
1268 void ServiceWorkerVersion::OnGetClients( 1277 void ServiceWorkerVersion::OnGetClients(
1269 int request_id, 1278 int request_id,
1270 const ServiceWorkerClientQueryOptions& options) { 1279 const ServiceWorkerClientQueryOptions& options) {
1271 TRACE_EVENT_ASYNC_BEGIN2( 1280 TRACE_EVENT_ASYNC_BEGIN2(
1272 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, 1281 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id,
1273 "client_type", options.client_type, "include_uncontrolled", 1282 "client_type", options.client_type, "include_uncontrolled",
1274 options.include_uncontrolled); 1283 options.include_uncontrolled);
1275 1284
1285 ServiceWorkerClients clients;
1276 if (controllee_map_.empty() && !options.include_uncontrolled) { 1286 if (controllee_map_.empty() && !options.include_uncontrolled) {
1277 OnGetClientsFinished(request_id, std::vector<ServiceWorkerClientInfo>()); 1287 OnGetClientsFinished(request_id, &clients);
1278 return; 1288 return;
1279 } 1289 }
1280 1290
1281 // For Window clients we want to query the info on the UI thread first. 1291 // For Window clients we want to query the info on the UI thread first.
1282 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || 1292 if (options.client_type == blink::WebServiceWorkerClientTypeWindow ||
1283 options.client_type == blink::WebServiceWorkerClientTypeAll) { 1293 options.client_type == blink::WebServiceWorkerClientTypeAll) {
1284 GetWindowClients(request_id, options); 1294 GetWindowClients(request_id, options);
1285 return; 1295 return;
1286 } 1296 }
1287 1297
1288 ServiceWorkerClients clients;
1289 GetNonWindowClients(request_id, options, &clients); 1298 GetNonWindowClients(request_id, options, &clients);
1290 OnGetClientsFinished(request_id, clients); 1299 OnGetClientsFinished(request_id, &clients);
1291 } 1300 }
1292 1301
1293 void ServiceWorkerVersion::OnGetClientsFinished( 1302 void ServiceWorkerVersion::OnGetClientsFinished(int request_id,
1294 int request_id, 1303 ServiceWorkerClients* clients) {
1295 const ServiceWorkerClients& clients) {
1296 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1304 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1297 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", 1305 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients",
1298 request_id, "The number of clients", clients.size()); 1306 request_id, "The number of clients", clients->size());
1299 1307
1300 if (running_status() != RUNNING) 1308 if (running_status() != RUNNING)
1301 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());
1302 embedded_worker_->SendMessage( 1312 embedded_worker_->SendMessage(
1303 ServiceWorkerMsg_DidGetClients(request_id, clients)); 1313 ServiceWorkerMsg_DidGetClients(request_id, *clients));
1304 } 1314 }
1305 1315
1306 void ServiceWorkerVersion::OnActivateEventFinished( 1316 void ServiceWorkerVersion::OnActivateEventFinished(
1307 int request_id, 1317 int request_id,
1308 blink::WebServiceWorkerEventResult result) { 1318 blink::WebServiceWorkerEventResult result) {
1309 DCHECK(ACTIVATING == status() || 1319 DCHECK(ACTIVATING == status() ||
1310 REDUNDANT == status()) << status(); 1320 REDUNDANT == status()) << status();
1311 TRACE_EVENT0("ServiceWorker", 1321 TRACE_EVENT0("ServiceWorker",
1312 "ServiceWorkerVersion::OnActivateEventFinished"); 1322 "ServiceWorkerVersion::OnActivateEventFinished");
1313 1323
(...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after
1912 return clients_info; 1922 return clients_info;
1913 } 1923 }
1914 1924
1915 void ServiceWorkerVersion::DidGetWindowClients( 1925 void ServiceWorkerVersion::DidGetWindowClients(
1916 int request_id, 1926 int request_id,
1917 const ServiceWorkerClientQueryOptions& options, 1927 const ServiceWorkerClientQueryOptions& options,
1918 scoped_ptr<ServiceWorkerClients> clients) { 1928 scoped_ptr<ServiceWorkerClients> clients) {
1919 DCHECK_CURRENTLY_ON(BrowserThread::IO); 1929 DCHECK_CURRENTLY_ON(BrowserThread::IO);
1920 if (options.client_type == blink::WebServiceWorkerClientTypeAll) 1930 if (options.client_type == blink::WebServiceWorkerClientTypeAll)
1921 GetNonWindowClients(request_id, options, clients.get()); 1931 GetNonWindowClients(request_id, options, clients.get());
1922 OnGetClientsFinished(request_id, *clients); 1932 OnGetClientsFinished(request_id, clients.get());
1923 } 1933 }
1924 1934
1925 void ServiceWorkerVersion::GetNonWindowClients( 1935 void ServiceWorkerVersion::GetNonWindowClients(
1926 int request_id, 1936 int request_id,
1927 const ServiceWorkerClientQueryOptions& options, 1937 const ServiceWorkerClientQueryOptions& options,
1928 ServiceWorkerClients* clients) { 1938 ServiceWorkerClients* clients) {
1929 if (!options.include_uncontrolled) { 1939 if (!options.include_uncontrolled) {
1930 for (auto& controllee : controllee_map_) { 1940 for (auto& controllee : controllee_map_) {
1931 AddNonWindowClient(controllee.second, options, clients); 1941 AddNonWindowClient(controllee.second, options, clients);
1932 } 1942 }
(...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after
2300 base::string16()); 2310 base::string16());
2301 service_port_dispatcher_.reset(); 2311 service_port_dispatcher_.reset();
2302 } 2312 }
2303 2313
2304 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() { 2314 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() {
2305 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); 2315 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED);
2306 background_sync_dispatcher_.reset(); 2316 background_sync_dispatcher_.reset();
2307 } 2317 }
2308 2318
2309 } // namespace content 2319 } // namespace content
OLDNEW
« no previous file with comments | « content/browser/service_worker/service_worker_version.h ('k') | content/common/service_worker/service_worker_client_info.h » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698