| 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 <vector> | 8 #include <vector> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| (...skipping 13 matching lines...) Expand all Loading... |
| 24 class EmbeddedWorkerRegistry; | 24 class EmbeddedWorkerRegistry; |
| 25 class ServiceWorkerRegistration; | 25 class ServiceWorkerRegistration; |
| 26 struct ServiceWorkerFetchRequest; | 26 struct ServiceWorkerFetchRequest; |
| 27 struct ServiceWorkerFetchResponse; | 27 struct ServiceWorkerFetchResponse; |
| 28 | 28 |
| 29 // This class corresponds to a specific version of a ServiceWorker | 29 // This class corresponds to a specific version of a ServiceWorker |
| 30 // script for a given pattern. When a script is upgraded, there may be | 30 // script for a given pattern. When a script is upgraded, there may be |
| 31 // more than one ServiceWorkerVersion "running" at a time, but only | 31 // more than one ServiceWorkerVersion "running" at a time, but only |
| 32 // one of them is active. This class connects the actual script with a | 32 // one of them is active. This class connects the actual script with a |
| 33 // running worker. | 33 // running worker. |
| 34 // Instances of this class are in one of two install states: | |
| 35 // - Pending: The script is in the process of being installed. There | |
| 36 // may be another active script running. | |
| 37 // - Active: The script is the only worker handling requests for the | |
| 38 // registration's pattern. | |
| 39 // | 34 // |
| 40 // In addition, a version has a running state (this is a rough | 35 // And finally, is_shutdown_ detects the live-ness of the object |
| 41 // sketch). Since a service worker can be stopped and started at any | |
| 42 // time, it will transition among these states multiple times during | |
| 43 // its lifetime. | |
| 44 // - Stopped: The script is not running | |
| 45 // - Starting: A request to fire an event against the version has been | |
| 46 // queued, but the worker is not yet | |
| 47 // loaded/initialized/etc. | |
| 48 // - Started: The worker is ready to receive events | |
| 49 // - Stopping: The worker is returning to the stopped state. | |
| 50 // | |
| 51 // The worker can "run" in both the Pending and the Active | |
| 52 // install states above. During the Pending state, the worker is only | |
| 53 // started in order to fire the 'install' and 'activate' | |
| 54 // events. During the Active state, it can receive other events such | |
| 55 // as 'fetch'. | |
| 56 // | |
| 57 // And finally, is_shutdown_ is detects the live-ness of the object | |
| 58 // itself. If the object is shut down, then it is in the process of | 36 // itself. If the object is shut down, then it is in the process of |
| 59 // being deleted from memory. This happens when a version is replaced | 37 // being deleted from memory. This happens when a version is replaced |
| 60 // as well as at browser shutdown. | 38 // as well as at browser shutdown. |
| 61 class CONTENT_EXPORT ServiceWorkerVersion | 39 class CONTENT_EXPORT ServiceWorkerVersion |
| 62 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), | 40 : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>), |
| 63 public EmbeddedWorkerInstance::Observer { | 41 public EmbeddedWorkerInstance::Observer { |
| 64 public: | 42 public: |
| 65 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 43 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
| 66 typedef base::Callback<void(ServiceWorkerStatusCode, | 44 typedef base::Callback<void(ServiceWorkerStatusCode, |
| 67 const IPC::Message& message)> MessageCallback; | 45 const IPC::Message& message)> MessageCallback; |
| 68 typedef base::Callback<void(ServiceWorkerStatusCode, | 46 typedef base::Callback<void(ServiceWorkerStatusCode, |
| 69 const ServiceWorkerFetchResponse& response)> | 47 const ServiceWorkerFetchResponse& response)> |
| 70 FetchCallback; | 48 FetchCallback; |
| 71 | 49 |
| 72 enum Status { | 50 enum RunningStatus { |
| 73 STOPPED = EmbeddedWorkerInstance::STOPPED, | 51 STOPPED = EmbeddedWorkerInstance::STOPPED, |
| 74 STARTING = EmbeddedWorkerInstance::STARTING, | 52 STARTING = EmbeddedWorkerInstance::STARTING, |
| 75 RUNNING = EmbeddedWorkerInstance::RUNNING, | 53 RUNNING = EmbeddedWorkerInstance::RUNNING, |
| 76 STOPPING = EmbeddedWorkerInstance::STOPPING, | 54 STOPPING = EmbeddedWorkerInstance::STOPPING, |
| 77 }; | 55 }; |
| 78 | 56 |
| 57 // Current version status; some of the status (e.g. INSTALLED and ACTIVE) |
| 58 // should be persisted unlike running status. Note that this class doesn't |
| 59 // change status on its own, consumers of this class should explicitly set |
| 60 // a new status by calling set_status(). |
| 61 enum Status { |
| 62 NEW, // The version is just created. |
| 63 INSTALLING, // Install event is dispatched and being handled. |
| 64 INSTALLED, // Install event is finished and is ready to be activated. |
| 65 ACTIVATING, // Activate event is dispatched and being handled. |
| 66 ACTIVE, // Activation is finished and can run as active. |
| 67 DEACTIVATED, // The version is no longer running as active, due to |
| 68 // unregistration or replace. (TODO(kinuko): we may need |
| 69 // different states for different termination sequences) |
| 70 }; |
| 71 |
| 79 ServiceWorkerVersion( | 72 ServiceWorkerVersion( |
| 80 ServiceWorkerRegistration* registration, | 73 ServiceWorkerRegistration* registration, |
| 81 EmbeddedWorkerRegistry* worker_registry, | 74 EmbeddedWorkerRegistry* worker_registry, |
| 82 int64 version_id); | 75 int64 version_id); |
| 83 | 76 |
| 84 int64 version_id() const { return version_id_; } | 77 int64 version_id() const { return version_id_; } |
| 85 | 78 |
| 86 void Shutdown(); | 79 void Shutdown(); |
| 87 bool is_shutdown() const { return is_shutdown_; } | 80 bool is_shutdown() const { return is_shutdown_; } |
| 88 | 81 |
| 89 Status status() const { | 82 RunningStatus running_status() const { |
| 90 return static_cast<Status>(embedded_worker_->status()); | 83 return static_cast<RunningStatus>(embedded_worker_->status()); |
| 91 } | 84 } |
| 92 | 85 |
| 86 Status status() const { return status_; } |
| 87 void set_status(Status status) { status_ = status; } |
| 88 |
| 93 // Starts an embedded worker for this version. | 89 // Starts an embedded worker for this version. |
| 94 // This returns OK (success) if the worker is already running. | 90 // This returns OK (success) if the worker is already running. |
| 95 void StartWorker(const StatusCallback& callback); | 91 void StartWorker(const StatusCallback& callback); |
| 96 | 92 |
| 97 // Starts an embedded worker for this version. | 93 // Starts an embedded worker for this version. |
| 98 // This returns OK (success) if the worker is already stopped. | 94 // This returns OK (success) if the worker is already stopped. |
| 99 void StopWorker(const StatusCallback& callback); | 95 void StopWorker(const StatusCallback& callback); |
| 100 | 96 |
| 101 // Sends an IPC message to the worker. | 97 // Sends an IPC message to the worker. |
| 102 // If the worker is not running this first tries to start it by | 98 // If the worker is not running this first tries to start it by |
| (...skipping 11 matching lines...) Expand all Loading... |
| 114 // If the worker is not running this first tries to start it by | 110 // If the worker is not running this first tries to start it by |
| 115 // calling StartWorker internally. | 111 // calling StartWorker internally. |
| 116 void SendMessageAndRegisterCallback(const IPC::Message& message, | 112 void SendMessageAndRegisterCallback(const IPC::Message& message, |
| 117 const MessageCallback& callback); | 113 const MessageCallback& callback); |
| 118 | 114 |
| 119 // Sends install event to the associated embedded worker and asynchronously | 115 // Sends install event to the associated embedded worker and asynchronously |
| 120 // calls |callback| when it errors out or it gets response from the worker | 116 // calls |callback| when it errors out or it gets response from the worker |
| 121 // to notify install completion. | 117 // to notify install completion. |
| 122 // |active_version_embedded_worker_id| must be a valid positive ID | 118 // |active_version_embedded_worker_id| must be a valid positive ID |
| 123 // if there's an active (previous) version running. | 119 // if there's an active (previous) version running. |
| 120 // |
| 121 // This must be called when the status() is NEW. Calling this in other |
| 122 // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. |
| 124 void DispatchInstallEvent(int active_version_embedded_worker_id, | 123 void DispatchInstallEvent(int active_version_embedded_worker_id, |
| 125 const StatusCallback& callback); | 124 const StatusCallback& callback); |
| 126 | 125 |
| 127 // Sends fetch event to the associated embedded worker and calls | 126 // Sends fetch event to the associated embedded worker and calls |
| 128 // |callback| with the response from the worker. | 127 // |callback| with the response from the worker. |
| 128 // |
| 129 // This must be called when the status() is ACTIVE. Calling this in other |
| 130 // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. |
| 129 void DispatchFetchEvent(const ServiceWorkerFetchRequest& request, | 131 void DispatchFetchEvent(const ServiceWorkerFetchRequest& request, |
| 130 const FetchCallback& callback); | 132 const FetchCallback& callback); |
| 131 | 133 |
| 132 // These are expected to be called when a renderer process host for the | 134 // These are expected to be called when a renderer process host for the |
| 133 // same-origin as for this ServiceWorkerVersion is created. The added | 135 // same-origin as for this ServiceWorkerVersion is created. The added |
| 134 // processes are used to run an in-renderer embedded worker. | 136 // processes are used to run an in-renderer embedded worker. |
| 135 void AddProcessToWorker(int process_id); | 137 void AddProcessToWorker(int process_id); |
| 136 void RemoveProcessToWorker(int process_id); | 138 void RemoveProcessToWorker(int process_id); |
| 137 | 139 |
| 138 EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); } | 140 EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); } |
| 139 | 141 |
| 140 // EmbeddedWorkerInstance::Observer overrides: | 142 // EmbeddedWorkerInstance::Observer overrides: |
| 141 virtual void OnStarted() OVERRIDE; | 143 virtual void OnStarted() OVERRIDE; |
| 142 virtual void OnStopped() OVERRIDE; | 144 virtual void OnStopped() OVERRIDE; |
| 143 virtual void OnMessageReceived(int request_id, | 145 virtual void OnMessageReceived(int request_id, |
| 144 const IPC::Message& message) OVERRIDE; | 146 const IPC::Message& message) OVERRIDE; |
| 145 | 147 |
| 146 private: | 148 private: |
| 147 typedef ServiceWorkerVersion self; | 149 typedef ServiceWorkerVersion self; |
| 148 friend class base::RefCounted<ServiceWorkerVersion>; | 150 friend class base::RefCounted<ServiceWorkerVersion>; |
| 149 | 151 |
| 150 virtual ~ServiceWorkerVersion(); | 152 virtual ~ServiceWorkerVersion(); |
| 151 | 153 |
| 152 const int64 version_id_; | 154 const int64 version_id_; |
| 153 | 155 |
| 156 Status status_; |
| 157 |
| 154 bool is_shutdown_; | 158 bool is_shutdown_; |
| 155 scoped_refptr<ServiceWorkerRegistration> registration_; | 159 scoped_refptr<ServiceWorkerRegistration> registration_; |
| 156 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; | 160 scoped_ptr<EmbeddedWorkerInstance> embedded_worker_; |
| 157 | 161 |
| 158 // Pending callbacks. | 162 // Pending callbacks. |
| 159 std::vector<StatusCallback> start_callbacks_; | 163 std::vector<StatusCallback> start_callbacks_; |
| 160 std::vector<StatusCallback> stop_callbacks_; | 164 std::vector<StatusCallback> stop_callbacks_; |
| 161 | 165 |
| 162 IDMap<MessageCallback, IDMapOwnPointer> message_callbacks_; | 166 IDMap<MessageCallback, IDMapOwnPointer> message_callbacks_; |
| 163 | 167 |
| 164 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; | 168 base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_; |
| 165 | 169 |
| 166 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); | 170 DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); |
| 167 }; | 171 }; |
| 168 | 172 |
| 169 } // namespace content | 173 } // namespace content |
| 170 | 174 |
| 171 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ | 175 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ |
| OLD | NEW |