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 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| 7 | 7 |
| 8 #include <stdint.h> | 8 #include <stdint.h> |
| 9 | 9 |
| 10 #include <functional> | 10 #include <functional> |
| 11 #include <map> | 11 #include <map> |
| 12 #include <memory> | 12 #include <memory> |
| 13 #include <queue> | 13 #include <queue> |
| 14 #include <set> | 14 #include <set> |
| 15 #include <string> | 15 #include <string> |
| 16 #include <vector> | 16 #include <vector> |
| 17 | 17 |
| 18 #include "base/callback.h" | 18 #include "base/callback.h" |
| 19 #include "base/containers/scoped_ptr_hash_map.h" | 19 #include "base/containers/scoped_ptr_hash_map.h" |
| 20 #include "base/gtest_prod_util.h" | 20 #include "base/gtest_prod_util.h" |
| 21 #include "base/id_map.h" | 21 #include "base/id_map.h" |
| 22 #include "base/macros.h" | 22 #include "base/macros.h" |
| 23 #include "base/memory/ptr_util.h" | 23 #include "base/memory/ptr_util.h" |
| 24 #include "base/memory/ref_counted.h" | 24 #include "base/memory/ref_counted.h" |
| 25 #include "base/observer_list.h" | 25 #include "base/observer_list.h" |
| 26 #include "base/threading/thread_task_runner_handle.h" | 26 #include "base/threading/thread_task_runner_handle.h" |
| 27 #include "base/timer/timer.h" | 27 #include "base/timer/timer.h" |
| 28 #include "content/browser/service_worker/embedded_worker_instance.h" | 28 #include "content/browser/service_worker/embedded_worker_instance.h" |
| 29 #include "content/browser/service_worker/embedded_worker_status.h" | |
| 29 #include "content/browser/service_worker/service_worker_metrics.h" | 30 #include "content/browser/service_worker/service_worker_metrics.h" |
| 30 #include "content/browser/service_worker/service_worker_script_cache_map.h" | 31 #include "content/browser/service_worker/service_worker_script_cache_map.h" |
| 31 #include "content/common/content_export.h" | 32 #include "content/common/content_export.h" |
| 32 #include "content/common/service_worker/service_worker_status_code.h" | 33 #include "content/common/service_worker/service_worker_status_code.h" |
| 33 #include "content/common/service_worker/service_worker_types.h" | 34 #include "content/common/service_worker/service_worker_types.h" |
| 34 #include "content/public/common/service_registry.h" | 35 #include "content/public/common/service_registry.h" |
| 35 #include "ipc/ipc_message.h" | 36 #include "ipc/ipc_message.h" |
| 36 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" | 37 #include "third_party/WebKit/public/platform/modules/serviceworker/WebServiceWor kerEventResult.h" |
| 37 #include "url/gurl.h" | 38 #include "url/gurl.h" |
| 38 #include "url/origin.h" | 39 #include "url/origin.h" |
| (...skipping 20 matching lines...) Expand all Loading... | |
| 59 // This class corresponds to a specific version of a ServiceWorker | 60 // This class corresponds to a specific version of a ServiceWorker |
| 60 // script for a given pattern. When a script is upgraded, there may be | 61 // script for a given pattern. When a script is upgraded, there may be |
| 61 // more than one ServiceWorkerVersion "running" at a time, but only | 62 // more than one ServiceWorkerVersion "running" at a time, but only |
| 62 // one of them is activated. This class connects the actual script with a | 63 // one of them is activated. This class connects the actual script with a |
| 63 // running worker. | 64 // running worker. |
| 64 class CONTENT_EXPORT ServiceWorkerVersion | 65 class CONTENT_EXPORT ServiceWorkerVersion |
| 65 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), | 66 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), |
| 66 public EmbeddedWorkerInstance::Listener { | 67 public EmbeddedWorkerInstance::Listener { |
| 67 public: | 68 public: |
| 68 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 69 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
| 69 | 70 typedef EmbeddedWorkerStatus RunningStatus; |
|
falken
2016/06/07 03:46:12
I'm not sure this typedef is an improvement. Can w
horo
2016/06/07 05:58:12
Done.
| |
| 70 enum RunningStatus { | |
| 71 STOPPED = EmbeddedWorkerInstance::STOPPED, | |
| 72 STARTING = EmbeddedWorkerInstance::STARTING, | |
| 73 RUNNING = EmbeddedWorkerInstance::RUNNING, | |
| 74 STOPPING = EmbeddedWorkerInstance::STOPPING, | |
| 75 }; | |
| 76 | 71 |
| 77 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) | 72 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) |
| 78 // should be persisted unlike running status. | 73 // should be persisted unlike running status. |
| 79 enum Status { | 74 enum Status { |
| 80 NEW, // The version is just created. | 75 NEW, // The version is just created. |
| 81 INSTALLING, // Install event is dispatched and being handled. | 76 INSTALLING, // Install event is dispatched and being handled. |
| 82 INSTALLED, // Install event is finished and is ready to be activated. | 77 INSTALLED, // Install event is finished and is ready to be activated. |
| 83 ACTIVATING, // Activate event is dispatched and being handled. | 78 ACTIVATING, // Activate event is dispatched and being handled. |
| 84 ACTIVATED, // Activation is finished and can run as activated. | 79 ACTIVATED, // Activation is finished and can run as activated. |
| 85 REDUNDANT, // The version is no longer running as activated, due to | 80 REDUNDANT, // The version is no longer running as activated, due to |
| (...skipping 39 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 125 | 120 |
| 126 ServiceWorkerVersion(ServiceWorkerRegistration* registration, | 121 ServiceWorkerVersion(ServiceWorkerRegistration* registration, |
| 127 const GURL& script_url, | 122 const GURL& script_url, |
| 128 int64_t version_id, | 123 int64_t version_id, |
| 129 base::WeakPtr<ServiceWorkerContextCore> context); | 124 base::WeakPtr<ServiceWorkerContextCore> context); |
| 130 | 125 |
| 131 int64_t version_id() const { return version_id_; } | 126 int64_t version_id() const { return version_id_; } |
| 132 int64_t registration_id() const { return registration_id_; } | 127 int64_t registration_id() const { return registration_id_; } |
| 133 const GURL& script_url() const { return script_url_; } | 128 const GURL& script_url() const { return script_url_; } |
| 134 const GURL& scope() const { return scope_; } | 129 const GURL& scope() const { return scope_; } |
| 135 RunningStatus running_status() const { | 130 RunningStatus running_status() const { return embedded_worker_->status(); } |
| 136 return static_cast<RunningStatus>(embedded_worker_->status()); | |
| 137 } | |
| 138 ServiceWorkerVersionInfo GetInfo(); | 131 ServiceWorkerVersionInfo GetInfo(); |
| 139 Status status() const { return status_; } | 132 Status status() const { return status_; } |
| 140 bool has_fetch_handler() const { return has_fetch_handler_; } | 133 bool has_fetch_handler() const { return has_fetch_handler_; } |
| 141 void set_has_fetch_handler(bool has_fetch_handler) { | 134 void set_has_fetch_handler(bool has_fetch_handler) { |
| 142 has_fetch_handler_ = has_fetch_handler; | 135 has_fetch_handler_ = has_fetch_handler; |
| 143 } | 136 } |
| 144 | 137 |
| 145 const std::vector<GURL>& foreign_fetch_scopes() const { | 138 const std::vector<GURL>& foreign_fetch_scopes() const { |
| 146 return foreign_fetch_scopes_; | 139 return foreign_fetch_scopes_; |
| 147 } | 140 } |
| (...skipping 140 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 288 void SetStartWorkerStatusCode(ServiceWorkerStatusCode status); | 281 void SetStartWorkerStatusCode(ServiceWorkerStatusCode status); |
| 289 | 282 |
| 290 // Sets this version's status to REDUNDANT and deletes its resources. | 283 // Sets this version's status to REDUNDANT and deletes its resources. |
| 291 // The version must not have controllees. | 284 // The version must not have controllees. |
| 292 void Doom(); | 285 void Doom(); |
| 293 bool is_redundant() const { return status_ == REDUNDANT; } | 286 bool is_redundant() const { return status_ == REDUNDANT; } |
| 294 | 287 |
| 295 bool skip_waiting() const { return skip_waiting_; } | 288 bool skip_waiting() const { return skip_waiting_; } |
| 296 void set_skip_waiting(bool skip_waiting) { skip_waiting_ = skip_waiting; } | 289 void set_skip_waiting(bool skip_waiting) { skip_waiting_ = skip_waiting; } |
| 297 | 290 |
| 291 bool skip_recording_startup_time() const { | |
| 292 return skip_recording_startup_time_; | |
| 293 } | |
| 294 | |
| 298 bool force_bypass_cache_for_scripts() const { | 295 bool force_bypass_cache_for_scripts() const { |
| 299 return force_bypass_cache_for_scripts_; | 296 return force_bypass_cache_for_scripts_; |
| 300 } | 297 } |
| 301 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { | 298 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { |
| 302 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; | 299 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; |
| 303 } | 300 } |
| 304 | 301 |
| 305 bool pause_after_download() const { return pause_after_download_; } | 302 bool pause_after_download() const { return pause_after_download_; } |
| 306 void set_pause_after_download(bool pause_after_download) { | 303 void set_pause_after_download(bool pause_after_download) { |
| 307 pause_after_download_ = pause_after_download; | 304 pause_after_download_ = pause_after_download; |
| (...skipping 372 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 680 bool stop_when_devtools_detached_ = false; | 677 bool stop_when_devtools_detached_ = false; |
| 681 | 678 |
| 682 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 679 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 683 | 680 |
| 684 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 681 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
| 685 }; | 682 }; |
| 686 | 683 |
| 687 template <typename Interface> | 684 template <typename Interface> |
| 688 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( | 685 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( |
| 689 int request_id) { | 686 int request_id) { |
| 690 DCHECK_EQ(RUNNING, running_status()); | 687 DCHECK_EQ(RunningStatus::RUNNING, running_status()); |
| 691 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 688 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 692 DCHECK(request) << "Invalid request id"; | 689 DCHECK(request) << "Invalid request id"; |
| 693 DCHECK(!request->mojo_service) | 690 DCHECK(!request->mojo_service) |
| 694 << "Request is already associated with a mojo service"; | 691 << "Request is already associated with a mojo service"; |
| 695 | 692 |
| 696 MojoServiceWrapper<Interface>* service = | 693 MojoServiceWrapper<Interface>* service = |
| 697 static_cast<MojoServiceWrapper<Interface>*>( | 694 static_cast<MojoServiceWrapper<Interface>*>( |
| 698 mojo_services_.get(Interface::Name_)); | 695 mojo_services_.get(Interface::Name_)); |
| 699 if (!service) { | 696 if (!service) { |
| 700 mojo::InterfacePtr<Interface> interface; | 697 mojo::InterfacePtr<Interface> interface; |
| 701 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( | 698 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( |
| 702 mojo::GetProxy(&interface)); | 699 mojo::GetProxy(&interface)); |
| 703 interface.set_connection_error_handler( | 700 interface.set_connection_error_handler( |
| 704 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, | 701 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, |
| 705 weak_factory_.GetWeakPtr(), Interface::Name_)); | 702 weak_factory_.GetWeakPtr(), Interface::Name_)); |
| 706 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); | 703 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); |
| 707 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); | 704 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); |
| 708 } | 705 } |
| 709 request->mojo_service = Interface::Name_; | 706 request->mojo_service = Interface::Name_; |
| 710 return service->GetWeakPtr(); | 707 return service->GetWeakPtr(); |
| 711 } | 708 } |
| 712 | 709 |
| 713 template <typename ResponseMessage, typename ResponseCallbackType> | 710 template <typename ResponseMessage, typename ResponseCallbackType> |
| 714 void ServiceWorkerVersion::DispatchEvent(int request_id, | 711 void ServiceWorkerVersion::DispatchEvent(int request_id, |
| 715 const IPC::Message& message, | 712 const IPC::Message& message, |
| 716 const ResponseCallbackType& callback) { | 713 const ResponseCallbackType& callback) { |
| 717 DCHECK_EQ(RUNNING, running_status()); | 714 DCHECK_EQ(RunningStatus::RUNNING, running_status()); |
| 718 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 715 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 719 DCHECK(request) << "Invalid request id"; | 716 DCHECK(request) << "Invalid request id"; |
| 720 DCHECK(!request->listener) << "Request already dispatched an IPC event"; | 717 DCHECK(!request->listener) << "Request already dispatched an IPC event"; |
| 721 | 718 |
| 722 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); | 719 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); |
| 723 if (status != SERVICE_WORKER_OK) { | 720 if (status != SERVICE_WORKER_OK) { |
| 724 base::ThreadTaskRunnerHandle::Get()->PostTask( | 721 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 725 FROM_HERE, base::Bind(request->callback, status)); | 722 FROM_HERE, base::Bind(request->callback, status)); |
| 726 custom_requests_.Remove(request_id); | 723 custom_requests_.Remove(request_id); |
| 727 } else { | 724 } else { |
| (...skipping 30 matching lines...) Expand all Loading... | |
| 758 | 755 |
| 759 // At this point |this| can have been deleted, so don't do anything other | 756 // At this point |this| can have been deleted, so don't do anything other |
| 760 // than returning. | 757 // than returning. |
| 761 | 758 |
| 762 return true; | 759 return true; |
| 763 } | 760 } |
| 764 | 761 |
| 765 } // namespace content | 762 } // namespace content |
| 766 | 763 |
| 767 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 764 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |