#ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
#define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_VERSION_H_
+#include <vector>
+
#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"
namespace content {
class EmbeddedWorkerRegistry;
-class ServiceWorkerProviderHost;
class ServiceWorkerRegistration;
struct ServiceWorkerFetchRequest;
// 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<ServiceWorkerVersion>) {
+ : NON_EXPORTED_BASE(public base::RefCounted<ServiceWorkerVersion>),
+ public EmbeddedWorkerInstance::Observer {
public:
typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
+ typedef base::Callback<void(ServiceWorkerStatusCode,
+ const IPC::Message& message)> MessageCallback;
enum Status {
STOPPED = EmbeddedWorkerInstance::STOPPED,
}
// Starts an embedded worker for this version.
- // It is not valid to call this while there's other inflight start or
- // stop process running.
// This returns OK (success) if the worker is already running.
void StartWorker(const StatusCallback& callback);
// Starts an embedded worker for this version.
- // It is not valid to call this while there's other inflight start or
- // stop process running.
// 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.
+ void DispatchInstallEvent(int active_version_embedded_worker_id,
+ const StatusCallback& callback);
+
// Sends fetch event to the associated embedded worker.
// This immediately returns false if the worker is not running
// or sending a message to the child process fails.
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<ServiceWorkerVersion>;
- // Embedded worker observer classes.
- class WorkerObserverBase;
- class StartObserver;
- class StopObserver;
-
- ~ServiceWorkerVersion();
+ virtual ~ServiceWorkerVersion();
const int64 version_id_;
bool is_shutdown_;
scoped_refptr<ServiceWorkerRegistration> registration_;
-
scoped_ptr<EmbeddedWorkerInstance> embedded_worker_;
- scoped_ptr<EmbeddedWorkerInstance::Observer> observer_;
+
+ // Pending callbacks.
+ std::vector<StatusCallback> start_callbacks_;
+ std::vector<StatusCallback> stop_callbacks_;
+
+ IDMap<MessageCallback, IDMapOwnPointer> message_callbacks_;
+
+ base::WeakPtrFactory<ServiceWorkerVersion> weak_factory_;
DISALLOW_COPY_AND_ASSIGN(ServiceWorkerVersion);
};