Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 312 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 329 // The tuple contains process_id, frame_id, client_uuid. | 330 // The tuple contains process_id, frame_id, client_uuid. |
| 330 const std::vector<base::Tuple<int, int, std::string>>& clients_info, | 331 const std::vector<base::Tuple<int, int, std::string>>& clients_info, |
| 331 const GURL& script_url, | 332 const GURL& script_url, |
| 332 const GetClientsCallback& callback) { | 333 const GetClientsCallback& callback) { |
| 333 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); | 334 scoped_ptr<ServiceWorkerClients> clients(new ServiceWorkerClients); |
| 334 | 335 |
| 335 for (const auto& it : clients_info) { | 336 for (const auto& it : clients_info) { |
| 336 ServiceWorkerClientInfo info = | 337 ServiceWorkerClientInfo info = |
| 337 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(base::get<0>(it), | 338 ServiceWorkerProviderHost::GetWindowClientInfoOnUI(base::get<0>(it), |
| 338 base::get<1>(it)); | 339 base::get<1>(it)); |
| 339 | |
|
nhiroki
2015/08/19 07:42:33
Please keep this blank line for readability.
jeremyarcher
2015/08/19 07:50:57
Done.
| |
| 340 // If the request to the provider_host returned an empty | 340 // If the request to the provider_host returned an empty |
| 341 // ServiceWorkerClientInfo, that means that it wasn't possible to associate | 341 // ServiceWorkerClientInfo, that means that it wasn't possible to associate |
| 342 // it with a valid RenderFrameHost. It might be because the frame was killed | 342 // it with a valid RenderFrameHost. It might be because the frame was killed |
| 343 // or navigated in between. | 343 // or navigated in between. |
| 344 if (info.IsEmpty()) | 344 if (info.IsEmpty()) |
| 345 continue; | 345 continue; |
| 346 | 346 |
| 347 // We can get info for a frame that was navigating end ended up with a | 347 // We can get info for a frame that was navigating end ended up with a |
| 348 // different URL than expected. In such case, we should make sure to not | 348 // different URL than expected. In such case, we should make sure to not |
| 349 // expose cross-origin WindowClient. | 349 // expose cross-origin WindowClient. |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 370 void AddNonWindowClient(ServiceWorkerProviderHost* host, | 370 void AddNonWindowClient(ServiceWorkerProviderHost* host, |
| 371 const ServiceWorkerClientQueryOptions& options, | 371 const ServiceWorkerClientQueryOptions& options, |
| 372 ServiceWorkerClients* clients) { | 372 ServiceWorkerClients* clients) { |
| 373 blink::WebServiceWorkerClientType host_client_type = host->client_type(); | 373 blink::WebServiceWorkerClientType host_client_type = host->client_type(); |
| 374 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) | 374 if (host_client_type == blink::WebServiceWorkerClientTypeWindow) |
| 375 return; | 375 return; |
| 376 if (options.client_type != blink::WebServiceWorkerClientTypeAll && | 376 if (options.client_type != blink::WebServiceWorkerClientTypeAll && |
| 377 options.client_type != host_client_type) | 377 options.client_type != host_client_type) |
| 378 return; | 378 return; |
| 379 | 379 |
| 380 ServiceWorkerClientInfo client_info( | 380 ServiceWorkerClientInfo client_info(blink::WebPageVisibilityStateHidden, |
| 381 blink::WebPageVisibilityStateHidden, | 381 false, // is_focused |
| 382 false, // is_focused | 382 host->document_url(), |
| 383 host->document_url(), REQUEST_CONTEXT_FRAME_TYPE_NONE, host_client_type); | 383 REQUEST_CONTEXT_FRAME_TYPE_NONE, |
| 384 base::TimeTicks(), host_client_type); | |
| 384 client_info.client_uuid = host->client_uuid(); | 385 client_info.client_uuid = host->client_uuid(); |
| 385 clients->push_back(client_info); | 386 clients->push_back(client_info); |
| 386 } | 387 } |
| 387 | 388 |
| 388 bool IsInstalled(ServiceWorkerVersion::Status status) { | 389 bool IsInstalled(ServiceWorkerVersion::Status status) { |
| 389 switch (status) { | 390 switch (status) { |
| 390 case ServiceWorkerVersion::NEW: | 391 case ServiceWorkerVersion::NEW: |
| 391 case ServiceWorkerVersion::INSTALLING: | 392 case ServiceWorkerVersion::INSTALLING: |
| 392 case ServiceWorkerVersion::REDUNDANT: | 393 case ServiceWorkerVersion::REDUNDANT: |
| 393 return false; | 394 return false; |
| 394 case ServiceWorkerVersion::INSTALLED: | 395 case ServiceWorkerVersion::INSTALLED: |
| 395 case ServiceWorkerVersion::ACTIVATING: | 396 case ServiceWorkerVersion::ACTIVATING: |
| 396 case ServiceWorkerVersion::ACTIVATED: | 397 case ServiceWorkerVersion::ACTIVATED: |
| 397 return true; | 398 return true; |
| 398 } | 399 } |
| 399 NOTREACHED() << "Unexpected status: " << status; | 400 NOTREACHED() << "Unexpected status: " << status; |
| 400 return false; | 401 return false; |
| 401 } | 402 } |
| 402 | 403 |
| 404 struct ServiceWorkerClientInfoSortMRU { | |
| 405 bool operator()(const ServiceWorkerClientInfo& a, | |
| 406 const ServiceWorkerClientInfo& b) const { | |
| 407 return a.last_active_time > b.last_active_time; | |
| 408 } | |
| 409 }; | |
| 410 | |
| 403 } // namespace | 411 } // namespace |
| 404 | 412 |
| 405 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; | 413 const int ServiceWorkerVersion::kStartWorkerTimeoutMinutes = 5; |
| 406 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; | 414 const int ServiceWorkerVersion::kRequestTimeoutMinutes = 5; |
| 407 | 415 |
| 408 class ServiceWorkerVersion::Metrics { | 416 class ServiceWorkerVersion::Metrics { |
| 409 public: | 417 public: |
| 410 using EventType = ServiceWorkerMetrics::EventType; | 418 using EventType = ServiceWorkerMetrics::EventType; |
| 411 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {} | 419 explicit Metrics(ServiceWorkerVersion* owner) : owner_(owner) {} |
| 412 ~Metrics() { | 420 ~Metrics() { |
| (...skipping 853 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1266 } | 1274 } |
| 1267 | 1275 |
| 1268 void ServiceWorkerVersion::OnGetClients( | 1276 void ServiceWorkerVersion::OnGetClients( |
| 1269 int request_id, | 1277 int request_id, |
| 1270 const ServiceWorkerClientQueryOptions& options) { | 1278 const ServiceWorkerClientQueryOptions& options) { |
| 1271 TRACE_EVENT_ASYNC_BEGIN2( | 1279 TRACE_EVENT_ASYNC_BEGIN2( |
| 1272 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, | 1280 "ServiceWorker", "ServiceWorkerVersion::OnGetClients", request_id, |
| 1273 "client_type", options.client_type, "include_uncontrolled", | 1281 "client_type", options.client_type, "include_uncontrolled", |
| 1274 options.include_uncontrolled); | 1282 options.include_uncontrolled); |
| 1275 | 1283 |
| 1284 ServiceWorkerClients clients; | |
| 1276 if (controllee_map_.empty() && !options.include_uncontrolled) { | 1285 if (controllee_map_.empty() && !options.include_uncontrolled) { |
| 1277 OnGetClientsFinished(request_id, std::vector<ServiceWorkerClientInfo>()); | 1286 OnGetClientsFinished(request_id, &clients); |
| 1278 return; | 1287 return; |
| 1279 } | 1288 } |
| 1280 | 1289 |
| 1281 // For Window clients we want to query the info on the UI thread first. | 1290 // For Window clients we want to query the info on the UI thread first. |
| 1282 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || | 1291 if (options.client_type == blink::WebServiceWorkerClientTypeWindow || |
| 1283 options.client_type == blink::WebServiceWorkerClientTypeAll) { | 1292 options.client_type == blink::WebServiceWorkerClientTypeAll) { |
| 1284 GetWindowClients(request_id, options); | 1293 GetWindowClients(request_id, options); |
| 1285 return; | 1294 return; |
| 1286 } | 1295 } |
| 1287 | 1296 |
| 1288 ServiceWorkerClients clients; | |
| 1289 GetNonWindowClients(request_id, options, &clients); | 1297 GetNonWindowClients(request_id, options, &clients); |
| 1290 OnGetClientsFinished(request_id, clients); | 1298 OnGetClientsFinished(request_id, &clients); |
| 1291 } | 1299 } |
| 1292 | 1300 |
| 1293 void ServiceWorkerVersion::OnGetClientsFinished( | 1301 void ServiceWorkerVersion::OnGetClientsFinished(int request_id, |
| 1294 int request_id, | 1302 ServiceWorkerClients* clients) { |
| 1295 const ServiceWorkerClients& clients) { | |
| 1296 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1303 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1297 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", | 1304 TRACE_EVENT_ASYNC_END1("ServiceWorker", "ServiceWorkerVersion::OnGetClients", |
| 1298 request_id, "The number of clients", clients.size()); | 1305 request_id, "The number of clients", clients->size()); |
| 1299 | 1306 |
| 1300 if (running_status() != RUNNING) | 1307 if (running_status() != RUNNING) |
| 1301 return; | 1308 return; |
| 1309 // Sort clients so that the most recently active tab is in the front. | |
| 1310 std::sort(clients->begin(), clients->end(), ServiceWorkerClientInfoSortMRU()); | |
| 1302 embedded_worker_->SendMessage( | 1311 embedded_worker_->SendMessage( |
| 1303 ServiceWorkerMsg_DidGetClients(request_id, clients)); | 1312 ServiceWorkerMsg_DidGetClients(request_id, *clients)); |
| 1304 } | 1313 } |
| 1305 | 1314 |
| 1306 void ServiceWorkerVersion::OnActivateEventFinished( | 1315 void ServiceWorkerVersion::OnActivateEventFinished( |
| 1307 int request_id, | 1316 int request_id, |
| 1308 blink::WebServiceWorkerEventResult result) { | 1317 blink::WebServiceWorkerEventResult result) { |
| 1309 DCHECK(ACTIVATING == status() || | 1318 DCHECK(ACTIVATING == status() || |
| 1310 REDUNDANT == status()) << status(); | 1319 REDUNDANT == status()) << status(); |
| 1311 TRACE_EVENT0("ServiceWorker", | 1320 TRACE_EVENT0("ServiceWorker", |
| 1312 "ServiceWorkerVersion::OnActivateEventFinished"); | 1321 "ServiceWorkerVersion::OnActivateEventFinished"); |
| 1313 | 1322 |
| (...skipping 598 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1912 return clients_info; | 1921 return clients_info; |
| 1913 } | 1922 } |
| 1914 | 1923 |
| 1915 void ServiceWorkerVersion::DidGetWindowClients( | 1924 void ServiceWorkerVersion::DidGetWindowClients( |
| 1916 int request_id, | 1925 int request_id, |
| 1917 const ServiceWorkerClientQueryOptions& options, | 1926 const ServiceWorkerClientQueryOptions& options, |
| 1918 scoped_ptr<ServiceWorkerClients> clients) { | 1927 scoped_ptr<ServiceWorkerClients> clients) { |
| 1919 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 1928 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 1920 if (options.client_type == blink::WebServiceWorkerClientTypeAll) | 1929 if (options.client_type == blink::WebServiceWorkerClientTypeAll) |
| 1921 GetNonWindowClients(request_id, options, clients.get()); | 1930 GetNonWindowClients(request_id, options, clients.get()); |
| 1922 OnGetClientsFinished(request_id, *clients); | 1931 OnGetClientsFinished(request_id, clients.get()); |
| 1923 } | 1932 } |
| 1924 | 1933 |
| 1925 void ServiceWorkerVersion::GetNonWindowClients( | 1934 void ServiceWorkerVersion::GetNonWindowClients( |
| 1926 int request_id, | 1935 int request_id, |
| 1927 const ServiceWorkerClientQueryOptions& options, | 1936 const ServiceWorkerClientQueryOptions& options, |
| 1928 ServiceWorkerClients* clients) { | 1937 ServiceWorkerClients* clients) { |
| 1929 if (!options.include_uncontrolled) { | 1938 if (!options.include_uncontrolled) { |
| 1930 for (auto& controllee : controllee_map_) { | 1939 for (auto& controllee : controllee_map_) { |
| 1931 AddNonWindowClient(controllee.second, options, clients); | 1940 AddNonWindowClient(controllee.second, options, clients); |
| 1932 } | 1941 } |
| (...skipping 367 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2300 base::string16()); | 2309 base::string16()); |
| 2301 service_port_dispatcher_.reset(); | 2310 service_port_dispatcher_.reset(); |
| 2302 } | 2311 } |
| 2303 | 2312 |
| 2304 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() { | 2313 void ServiceWorkerVersion::OnBackgroundSyncDispatcherConnectionError() { |
| 2305 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); | 2314 RunIDMapCallbacks(&sync_requests_, SERVICE_WORKER_ERROR_FAILED); |
| 2306 background_sync_dispatcher_.reset(); | 2315 background_sync_dispatcher_.reset(); |
| 2307 } | 2316 } |
| 2308 | 2317 |
| 2309 } // namespace content | 2318 } // namespace content |
| OLD | NEW |