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
3 // found in the LICENSE file.
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_
10 #include "base/basictypes.h"
11 #include "base/callback_forward.h"
12 #include "base/gtest_prod_util.h"
13 #include "base/logging.h"
14 #include "base/memory/ref_counted.h"
15 #include "base/observer_list.h"
16 #include "content/common/content_export.h"
17 #include "content/common/service_worker/service_worker_status_code.h"
27 class EmbeddedWorkerRegistry;
28 struct ServiceWorkerFetchRequest;
30 // This gives an interface to control one EmbeddedWorker instance, which
31 // may be 'in-waiting' or running in one of the child processes added by
32 // AddProcessReference().
33 class CONTENT_EXPORT EmbeddedWorkerInstance {
44 virtual ~Observer() {}
45 virtual void OnStarted() = 0;
46 virtual void OnStopped() = 0;
47 virtual void OnMessageReceived(int request_id,
48 const IPC::Message& message) = 0;
51 ~EmbeddedWorkerInstance();
53 // Starts the worker. It is invalid to call this when the worker is
54 // not in STOPPED status.
55 ServiceWorkerStatusCode Start(int64 service_worker_version_id,
56 const GURL& script_url);
58 // Stops the worker. It is invalid to call this when the worker is
59 // not in STARTING or RUNNING status.
60 // This returns false if stopping a worker fails immediately, e.g. when
61 // IPC couldn't be sent to the worker.
62 ServiceWorkerStatusCode Stop();
64 // Sends |message| to the embedded worker running in the child process.
65 // It is invalid to call this while the worker is not in RUNNING status.
66 // |request_id| can be optionally used to establish 2-way request-response
67 // messaging (e.g. the receiver can send back a response using the same
69 ServiceWorkerStatusCode SendMessage(
70 int request_id, const IPC::Message& message);
72 // Add or remove |process_id| to the internal process set where this
73 // worker can be started.
74 void AddProcessReference(int process_id);
75 void ReleaseProcessReference(int process_id);
77 int embedded_worker_id() const { return embedded_worker_id_; }
78 Status status() const { return status_; }
79 int process_id() const { return process_id_; }
80 int thread_id() const { return thread_id_; }
82 void AddObserver(Observer* observer);
83 void RemoveObserver(Observer* observer);
86 friend class EmbeddedWorkerRegistry;
87 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
89 typedef std::map<int, int> ProcessRefMap;
91 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
92 // This instance holds a ref of |registry|.
93 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry,
94 int embedded_worker_id);
96 // Called back from Registry when the worker instance has ack'ed that
97 // its WorkerGlobalScope is actually started on |thread_id| in the
99 // This will change the internal status from STARTING to RUNNING.
100 void OnStarted(int thread_id);
102 // Called back from Registry when the worker instance has ack'ed that
103 // its WorkerGlobalScope is actually stopped in the child process.
104 // This will change the internal status from STARTING or RUNNING to
108 // Called back from Registry when the worker instance sends message
109 // to the browser (i.e. EmbeddedWorker observers).
110 void OnMessageReceived(int request_id, const IPC::Message& message);
112 // Chooses a process to start this worker and populate process_id_.
113 // Returns false when no process is available.
114 bool ChooseProcess();
116 scoped_refptr<EmbeddedWorkerRegistry> registry_;
117 const int embedded_worker_id_;
120 // Current running information. -1 indicates the worker is not running.
124 ProcessRefMap process_refs_;
125 ObserverList<Observer> observer_list_;
127 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
130 } // namespace content
132 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_