// Copyright 2013 The Chromium Authors. All rights reserved. // Use of this source code is governed by a BSD-style license that can be // found in the LICENSE file. #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_ #include #include "base/basictypes.h" #include "base/callback.h" #include "base/gtest_prod_util.h" #include "base/id_map.h" #include "base/memory/ref_counted.h" #include "base/memory/scoped_ptr.h" #include "content/browser/service_worker/embedded_worker_instance.h" #include "content/common/content_export.h" #include "content/common/service_worker/service_worker_status_code.h" #include "content/common/service_worker/service_worker_types.h" class GURL; namespace content { class EmbeddedWorkerRegistry; class ServiceWorkerRegistration; // This class corresponds to a specific version of a ServiceWorker // script for a given pattern. When a script is upgraded, there may be // more than one ServiceWorkerVersion "running" at a time, but only // one of them is active. This class connects the actual script with a // running worker. // // is_shutdown_ detects the live-ness of the object itself. If the object is // shut down, then it is in the process of being deleted from memory. // This happens when a version is replaced as well as at browser shutdown. class CONTENT_EXPORT ServiceWorkerVersion : NON_EXPORTED_BASE(public base::RefCounted), public EmbeddedWorkerInstance::Observer { public: typedef base::Callback StatusCallback; typedef base::Callback MessageCallback; typedef base::Callback FetchCallback; enum RunningStatus { STOPPED = EmbeddedWorkerInstance::STOPPED, STARTING = EmbeddedWorkerInstance::STARTING, RUNNING = EmbeddedWorkerInstance::RUNNING, STOPPING = EmbeddedWorkerInstance::STOPPING, }; // Current version status; some of the status (e.g. INSTALLED and ACTIVE) // should be persisted unlike running status. Note that this class doesn't // change status on its own, consumers of this class should explicitly set // a new status by calling set_status(). enum Status { NEW, // The version is just created. INSTALLING, // Install event is dispatched and being handled. INSTALLED, // Install event is finished and is ready to be activated. ACTIVATING, // Activate event is dispatched and being handled. ACTIVE, // Activation is finished and can run as active. DEACTIVATED, // The version is no longer running as active, due to // unregistration or replace. (TODO(kinuko): we may need // different states for different termination sequences) }; ServiceWorkerVersion( ServiceWorkerRegistration* registration, EmbeddedWorkerRegistry* worker_registry, int64 version_id); int64 version_id() const { return version_id_; } void Shutdown(); bool is_shutdown() const { return is_shutdown_; } RunningStatus running_status() const { return static_cast(embedded_worker_->status()); } Status status() const { return status_; } void set_status(Status status) { status_ = status; } // Starts an embedded worker for this version. // This returns OK (success) if the worker is already running. void StartWorker(const StatusCallback& callback); // Starts an embedded worker for this version. // This returns OK (success) if the worker is already stopped. void StopWorker(const StatusCallback& callback); // Sends an IPC message to the worker. // If the worker is not running this first tries to start it by // calling StartWorker internally. // |callback| can be null if the sender does not need to know if the // message is successfully sent or not. // (If the sender expects the receiver to respond please use // SendMessageAndRegisterCallback instead) void SendMessage(const IPC::Message& message, const StatusCallback& callback); // Sends an IPC message to the worker and registers |callback| to // be notified when a response message is received. // The |callback| will be also fired with an error code if the worker // is unexpectedly (being) stopped. // If the worker is not running this first tries to start it by // calling StartWorker internally. void SendMessageAndRegisterCallback(const IPC::Message& message, const MessageCallback& callback); // Sends install event to the associated embedded worker and asynchronously // calls |callback| when it errors out or it gets response from the worker // to notify install completion. // |active_version_embedded_worker_id| must be a valid positive ID // if there's an active (previous) version running. // // This must be called when the status() is NEW. Calling this in other // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. void DispatchInstallEvent(int active_version_embedded_worker_id, const StatusCallback& callback); // Sends fetch event to the associated embedded worker and calls // |callback| with the response from the worker. // // This must be called when the status() is ACTIVE. Calling this in other // statuses will result in an error SERVICE_WORKER_ERROR_FAILED. void DispatchFetchEvent(const ServiceWorkerFetchRequest& request, const FetchCallback& callback); // These are expected to be called when a renderer process host for the // same-origin as for this ServiceWorkerVersion is created. The added // processes are used to run an in-renderer embedded worker. void AddProcessToWorker(int process_id); void RemoveProcessToWorker(int process_id); EmbeddedWorkerInstance* embedded_worker() { return embedded_worker_.get(); } // EmbeddedWorkerInstance::Observer overrides: virtual void OnStarted() OVERRIDE; virtual void OnStopped() OVERRIDE; virtual void OnMessageReceived(int request_id, const IPC::Message& message) OVERRIDE; private: typedef ServiceWorkerVersion self; friend class base::RefCounted; virtual ~ServiceWorkerVersion(); const int64 version_id_; Status status_; bool is_shutdown_; scoped_refptr registration_; scoped_ptr embedded_worker_; // Pending callbacks. std::vector start_callbacks_; std::vector stop_callbacks_; IDMap message_callbacks_; base::WeakPtrFactory weak_factory_; DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion); }; } // namespace content #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_