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