#ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
#define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
#include <map>
#include "base/basictypes.h"
#include "base/callback_forward.h"
#include "base/gtest_prod_util.h"
#include "base/logging.h"
#include "base/memory/ref_counted.h"
#include "base/observer_list.h"
#include "content/common/content_export.h"
#include "content/common/service_worker/service_worker_status_code.h"
class GURL;
namespace IPC {
class Message;
}
namespace content {
class EmbeddedWorkerRegistry;
struct ServiceWorkerFetchRequest;
class CONTENT_EXPORT EmbeddedWorkerInstance {
public:
enum Status {
STOPPED,
STARTING,
RUNNING,
STOPPING,
};
class Observer {
public:
virtual ~Observer() {}
virtual void OnStarted() = 0;
virtual void OnStopped() = 0;
virtual void OnMessageReceived(int request_id,
const IPC::Message& message) = 0;
};
~EmbeddedWorkerInstance();
ServiceWorkerStatusCode Start(int64 service_worker_version_id,
const GURL& script_url);
ServiceWorkerStatusCode Stop();
ServiceWorkerStatusCode SendMessage(
int request_id, const IPC::Message& message);
void AddProcessReference(int process_id);
void ReleaseProcessReference(int process_id);
int embedded_worker_id() const { return embedded_worker_id_; }
Status status() const { return status_; }
int process_id() const { return process_id_; }
int thread_id() const { return thread_id_; }
void AddObserver(Observer* observer);
void RemoveObserver(Observer* observer);
private:
friend class EmbeddedWorkerRegistry;
FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
typedef std::map<int, int> ProcessRefMap;
EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry,
int embedded_worker_id);
void OnStarted(int thread_id);
void OnStopped();
void OnMessageReceived(int request_id, const IPC::Message& message);
bool ChooseProcess();
scoped_refptr<EmbeddedWorkerRegistry> registry_;
const int embedded_worker_id_;
Status status_;
int process_id_;
int thread_id_;
ProcessRefMap process_refs_;
ObserverList<Observer> observer_list_;
DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
};
}
#endif