| 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 21 matching lines...) Expand all Loading... |
| 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 |
| 70 enum RunningStatus { | |
| 71 STOPPED = EmbeddedWorkerInstance::STOPPED, | |
| 72 STARTING = EmbeddedWorkerInstance::STARTING, | |
| 73 RUNNING = EmbeddedWorkerInstance::RUNNING, | |
| 74 STOPPING = EmbeddedWorkerInstance::STOPPING, | |
| 75 }; | |
| 76 | |
| 77 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) | 71 // Current version status; some of the status (e.g. INSTALLED and ACTIVATED) |
| 78 // should be persisted unlike running status. | 72 // should be persisted unlike running status. |
| 79 enum Status { | 73 enum Status { |
| 80 NEW, // The version is just created. | 74 NEW, // The version is just created. |
| 81 INSTALLING, // Install event is dispatched and being handled. | 75 INSTALLING, // Install event is dispatched and being handled. |
| 82 INSTALLED, // Install event is finished and is ready to be activated. | 76 INSTALLED, // Install event is finished and is ready to be activated. |
| 83 ACTIVATING, // Activate event is dispatched and being handled. | 77 ACTIVATING, // Activate event is dispatched and being handled. |
| 84 ACTIVATED, // Activation is finished and can run as activated. | 78 ACTIVATED, // Activation is finished and can run as activated. |
| 85 REDUNDANT, // The version is no longer running as activated, due to | 79 REDUNDANT, // The version is no longer running as activated, due to |
| 86 // unregistration or replace. | 80 // unregistration or replace. |
| (...skipping 38 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 125 | 119 |
| 126 ServiceWorkerVersion(ServiceWorkerRegistration* registration, | 120 ServiceWorkerVersion(ServiceWorkerRegistration* registration, |
| 127 const GURL& script_url, | 121 const GURL& script_url, |
| 128 int64_t version_id, | 122 int64_t version_id, |
| 129 base::WeakPtr<ServiceWorkerContextCore> context); | 123 base::WeakPtr<ServiceWorkerContextCore> context); |
| 130 | 124 |
| 131 int64_t version_id() const { return version_id_; } | 125 int64_t version_id() const { return version_id_; } |
| 132 int64_t registration_id() const { return registration_id_; } | 126 int64_t registration_id() const { return registration_id_; } |
| 133 const GURL& script_url() const { return script_url_; } | 127 const GURL& script_url() const { return script_url_; } |
| 134 const GURL& scope() const { return scope_; } | 128 const GURL& scope() const { return scope_; } |
| 135 RunningStatus running_status() const { | 129 EmbeddedWorkerStatus running_status() const { |
| 136 return static_cast<RunningStatus>(embedded_worker_->status()); | 130 return embedded_worker_->status(); |
| 137 } | 131 } |
| 138 ServiceWorkerVersionInfo GetInfo(); | 132 ServiceWorkerVersionInfo GetInfo(); |
| 139 Status status() const { return status_; } | 133 Status status() const { return status_; } |
| 140 bool has_fetch_handler() const { return has_fetch_handler_; } | 134 bool has_fetch_handler() const { return has_fetch_handler_; } |
| 141 void set_has_fetch_handler(bool has_fetch_handler) { | 135 void set_has_fetch_handler(bool has_fetch_handler) { |
| 142 has_fetch_handler_ = has_fetch_handler; | 136 has_fetch_handler_ = has_fetch_handler; |
| 143 } | 137 } |
| 144 | 138 |
| 145 const std::vector<GURL>& foreign_fetch_scopes() const { | 139 const std::vector<GURL>& foreign_fetch_scopes() const { |
| 146 return foreign_fetch_scopes_; | 140 return foreign_fetch_scopes_; |
| (...skipping 141 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 288 void SetStartWorkerStatusCode(ServiceWorkerStatusCode status); | 282 void SetStartWorkerStatusCode(ServiceWorkerStatusCode status); |
| 289 | 283 |
| 290 // Sets this version's status to REDUNDANT and deletes its resources. | 284 // Sets this version's status to REDUNDANT and deletes its resources. |
| 291 // The version must not have controllees. | 285 // The version must not have controllees. |
| 292 void Doom(); | 286 void Doom(); |
| 293 bool is_redundant() const { return status_ == REDUNDANT; } | 287 bool is_redundant() const { return status_ == REDUNDANT; } |
| 294 | 288 |
| 295 bool skip_waiting() const { return skip_waiting_; } | 289 bool skip_waiting() const { return skip_waiting_; } |
| 296 void set_skip_waiting(bool skip_waiting) { skip_waiting_ = skip_waiting; } | 290 void set_skip_waiting(bool skip_waiting) { skip_waiting_ = skip_waiting; } |
| 297 | 291 |
| 292 bool skip_recording_startup_time() const { |
| 293 return skip_recording_startup_time_; |
| 294 } |
| 295 |
| 298 bool force_bypass_cache_for_scripts() const { | 296 bool force_bypass_cache_for_scripts() const { |
| 299 return force_bypass_cache_for_scripts_; | 297 return force_bypass_cache_for_scripts_; |
| 300 } | 298 } |
| 301 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { | 299 void set_force_bypass_cache_for_scripts(bool force_bypass_cache_for_scripts) { |
| 302 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; | 300 force_bypass_cache_for_scripts_ = force_bypass_cache_for_scripts; |
| 303 } | 301 } |
| 304 | 302 |
| 305 bool pause_after_download() const { return pause_after_download_; } | 303 bool pause_after_download() const { return pause_after_download_; } |
| 306 void set_pause_after_download(bool pause_after_download) { | 304 void set_pause_after_download(bool pause_after_download) { |
| 307 pause_after_download_ = pause_after_download; | 305 pause_after_download_ = pause_after_download; |
| (...skipping 163 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 471 // Timeout for the worker to stop. | 469 // Timeout for the worker to stop. |
| 472 static const int kStopWorkerTimeoutSeconds; | 470 static const int kStopWorkerTimeoutSeconds; |
| 473 | 471 |
| 474 ~ServiceWorkerVersion() override; | 472 ~ServiceWorkerVersion() override; |
| 475 | 473 |
| 476 // EmbeddedWorkerInstance::Listener overrides: | 474 // EmbeddedWorkerInstance::Listener overrides: |
| 477 void OnThreadStarted() override; | 475 void OnThreadStarted() override; |
| 478 void OnStarting() override; | 476 void OnStarting() override; |
| 479 void OnStarted() override; | 477 void OnStarted() override; |
| 480 void OnStopping() override; | 478 void OnStopping() override; |
| 481 void OnStopped(EmbeddedWorkerInstance::Status old_status) override; | 479 void OnStopped(EmbeddedWorkerStatus old_status) override; |
| 482 void OnDetached(EmbeddedWorkerInstance::Status old_status) override; | 480 void OnDetached(EmbeddedWorkerStatus old_status) override; |
| 483 void OnScriptLoaded() override; | 481 void OnScriptLoaded() override; |
| 484 void OnScriptLoadFailed() override; | 482 void OnScriptLoadFailed() override; |
| 485 void OnReportException(const base::string16& error_message, | 483 void OnReportException(const base::string16& error_message, |
| 486 int line_number, | 484 int line_number, |
| 487 int column_number, | 485 int column_number, |
| 488 const GURL& source_url) override; | 486 const GURL& source_url) override; |
| 489 void OnReportConsoleMessage(int source_identifier, | 487 void OnReportConsoleMessage(int source_identifier, |
| 490 int message_level, | 488 int message_level, |
| 491 const base::string16& message, | 489 const base::string16& message, |
| 492 int line_number, | 490 int line_number, |
| (...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 586 ServiceWorkerStatusCode default_code); | 584 ServiceWorkerStatusCode default_code); |
| 587 | 585 |
| 588 // Sets |stale_time_| if this worker is stale, causing an update to eventually | 586 // Sets |stale_time_| if this worker is stale, causing an update to eventually |
| 589 // occur once the worker stops or is running too long. | 587 // occur once the worker stops or is running too long. |
| 590 void MarkIfStale(); | 588 void MarkIfStale(); |
| 591 | 589 |
| 592 void FoundRegistrationForUpdate( | 590 void FoundRegistrationForUpdate( |
| 593 ServiceWorkerStatusCode status, | 591 ServiceWorkerStatusCode status, |
| 594 const scoped_refptr<ServiceWorkerRegistration>& registration); | 592 const scoped_refptr<ServiceWorkerRegistration>& registration); |
| 595 | 593 |
| 596 void OnStoppedInternal(EmbeddedWorkerInstance::Status old_status); | 594 void OnStoppedInternal(EmbeddedWorkerStatus old_status); |
| 597 | 595 |
| 598 // Called when the remote side of a connection to a mojo service is lost. | 596 // Called when the remote side of a connection to a mojo service is lost. |
| 599 void OnMojoConnectionError(const char* service_name); | 597 void OnMojoConnectionError(const char* service_name); |
| 600 | 598 |
| 601 // Called at the beginning of each Dispatch*Event function: records | 599 // Called at the beginning of each Dispatch*Event function: records |
| 602 // the time elapsed since idle (generally the time since the previous | 600 // the time elapsed since idle (generally the time since the previous |
| 603 // event ended). | 601 // event ended). |
| 604 void OnBeginEvent(); | 602 void OnBeginEvent(); |
| 605 | 603 |
| 606 const int64_t version_id_; | 604 const int64_t version_id_; |
| (...skipping 73 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 680 bool stop_when_devtools_detached_ = false; | 678 bool stop_when_devtools_detached_ = false; |
| 681 | 679 |
| 682 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 680 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 683 | 681 |
| 684 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 682 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
| 685 }; | 683 }; |
| 686 | 684 |
| 687 template <typename Interface> | 685 template <typename Interface> |
| 688 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( | 686 base::WeakPtr<Interface> ServiceWorkerVersion::GetMojoServiceForRequest( |
| 689 int request_id) { | 687 int request_id) { |
| 690 DCHECK_EQ(RUNNING, running_status()); | 688 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); |
| 691 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 689 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 692 DCHECK(request) << "Invalid request id"; | 690 DCHECK(request) << "Invalid request id"; |
| 693 DCHECK(!request->mojo_service) | 691 DCHECK(!request->mojo_service) |
| 694 << "Request is already associated with a mojo service"; | 692 << "Request is already associated with a mojo service"; |
| 695 | 693 |
| 696 MojoServiceWrapper<Interface>* service = | 694 MojoServiceWrapper<Interface>* service = |
| 697 static_cast<MojoServiceWrapper<Interface>*>( | 695 static_cast<MojoServiceWrapper<Interface>*>( |
| 698 mojo_services_.get(Interface::Name_)); | 696 mojo_services_.get(Interface::Name_)); |
| 699 if (!service) { | 697 if (!service) { |
| 700 mojo::InterfacePtr<Interface> interface; | 698 mojo::InterfacePtr<Interface> interface; |
| 701 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( | 699 embedded_worker_->GetServiceRegistry()->ConnectToRemoteService( |
| 702 mojo::GetProxy(&interface)); | 700 mojo::GetProxy(&interface)); |
| 703 interface.set_connection_error_handler( | 701 interface.set_connection_error_handler( |
| 704 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, | 702 base::Bind(&ServiceWorkerVersion::OnMojoConnectionError, |
| 705 weak_factory_.GetWeakPtr(), Interface::Name_)); | 703 weak_factory_.GetWeakPtr(), Interface::Name_)); |
| 706 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); | 704 service = new MojoServiceWrapper<Interface>(this, std::move(interface)); |
| 707 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); | 705 mojo_services_.add(Interface::Name_, base::WrapUnique(service)); |
| 708 } | 706 } |
| 709 request->mojo_service = Interface::Name_; | 707 request->mojo_service = Interface::Name_; |
| 710 return service->GetWeakPtr(); | 708 return service->GetWeakPtr(); |
| 711 } | 709 } |
| 712 | 710 |
| 713 template <typename ResponseMessage, typename ResponseCallbackType> | 711 template <typename ResponseMessage, typename ResponseCallbackType> |
| 714 void ServiceWorkerVersion::DispatchEvent(int request_id, | 712 void ServiceWorkerVersion::DispatchEvent(int request_id, |
| 715 const IPC::Message& message, | 713 const IPC::Message& message, |
| 716 const ResponseCallbackType& callback) { | 714 const ResponseCallbackType& callback) { |
| 717 DCHECK_EQ(RUNNING, running_status()); | 715 DCHECK_EQ(EmbeddedWorkerStatus::RUNNING, running_status()); |
| 718 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); | 716 PendingRequest<StatusCallback>* request = custom_requests_.Lookup(request_id); |
| 719 DCHECK(request) << "Invalid request id"; | 717 DCHECK(request) << "Invalid request id"; |
| 720 DCHECK(!request->listener) << "Request already dispatched an IPC event"; | 718 DCHECK(!request->listener) << "Request already dispatched an IPC event"; |
| 721 | 719 |
| 722 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); | 720 ServiceWorkerStatusCode status = embedded_worker_->SendMessage(message); |
| 723 if (status != SERVICE_WORKER_OK) { | 721 if (status != SERVICE_WORKER_OK) { |
| 724 base::ThreadTaskRunnerHandle::Get()->PostTask( | 722 base::ThreadTaskRunnerHandle::Get()->PostTask( |
| 725 FROM_HERE, base::Bind(request->callback, status)); | 723 FROM_HERE, base::Bind(request->callback, status)); |
| 726 custom_requests_.Remove(request_id); | 724 custom_requests_.Remove(request_id); |
| 727 } else { | 725 } else { |
| (...skipping 30 matching lines...) Expand all Loading... |
| 758 | 756 |
| 759 // At this point |this| can have been deleted, so don't do anything other | 757 // At this point |this| can have been deleted, so don't do anything other |
| 760 // than returning. | 758 // than returning. |
| 761 | 759 |
| 762 return true; | 760 return true; |
| 763 } | 761 } |
| 764 | 762 |
| 765 } // namespace content | 763 } // namespace content |
| 766 | 764 |
| 767 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 765 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |