Fix some interleaving issues in EmbeddedWorkerInstance.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_instance.h
blob7bece691cd392a4ebf2b5129611e9a6db8f55a70
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_
8 #include <map>
9 #include <string>
10 #include <vector>
12 #include "base/basictypes.h"
13 #include "base/callback.h"
14 #include "base/callback_forward.h"
15 #include "base/gtest_prod_util.h"
16 #include "base/logging.h"
17 #include "base/memory/ref_counted.h"
18 #include "base/memory/weak_ptr.h"
19 #include "base/observer_list.h"
20 #include "base/strings/string16.h"
21 #include "base/time/time.h"
22 #include "content/common/content_export.h"
23 #include "content/common/service_worker/service_worker_status_code.h"
24 #include "url/gurl.h"
26 // Windows headers will redefine SendMessage.
27 #ifdef SendMessage
28 #undef SendMessage
29 #endif
31 struct EmbeddedWorkerMsg_StartWorker_Params;
33 namespace IPC {
34 class Message;
37 namespace content {
39 class EmbeddedWorkerRegistry;
40 class MessagePortMessageFilter;
41 class ServiceRegistry;
42 class ServiceRegistryImpl;
43 class ServiceWorkerContextCore;
44 struct ServiceWorkerFetchRequest;
46 // This gives an interface to control one EmbeddedWorker instance, which
47 // may be 'in-waiting' or running in one of the child processes added by
48 // AddProcessReference().
49 class CONTENT_EXPORT EmbeddedWorkerInstance {
50 public:
51 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
52 enum Status {
53 STOPPED,
54 STARTING,
55 RUNNING,
56 STOPPING,
59 // This enum is used in UMA histograms, so don't change the order or remove
60 // entries.
61 enum StartingPhase {
62 NOT_STARTING,
63 ALLOCATING_PROCESS,
64 REGISTERING_TO_DEVTOOLS,
65 SENT_START_WORKER,
66 SCRIPT_DOWNLOADING,
67 SCRIPT_LOADED,
68 SCRIPT_EVALUATED,
69 STARTING_PHASE_MAX_VALUE,
72 class Listener {
73 public:
74 virtual ~Listener() {}
75 virtual void OnScriptLoaded() {}
76 virtual void OnStarting() {}
77 virtual void OnStarted() {}
78 virtual void OnStopping() {}
79 // Received ACK from renderer that the worker context terminated.
80 virtual void OnStopped(Status old_status) {}
81 // The browser-side IPC endpoint for communication with the worker died.
82 virtual void OnDetached(Status old_status) {}
83 virtual void OnReportException(const base::string16& error_message,
84 int line_number,
85 int column_number,
86 const GURL& source_url) {}
87 virtual void OnReportConsoleMessage(int source_identifier,
88 int message_level,
89 const base::string16& message,
90 int line_number,
91 const GURL& source_url) {}
92 // Returns false if the message is not handled by this listener.
93 virtual bool OnMessageReceived(const IPC::Message& message) = 0;
96 ~EmbeddedWorkerInstance();
98 // Starts the worker. It is invalid to call this when the worker is not in
99 // STOPPED status. |callback| is invoked after the worker script has been
100 // started and evaluated, or when an error occurs.
101 void Start(int64 service_worker_version_id,
102 const GURL& scope,
103 const GURL& script_url,
104 const StatusCallback& callback);
106 // Stops the worker. It is invalid to call this when the worker is
107 // not in STARTING or RUNNING status.
108 // This returns false if stopping a worker fails immediately, e.g. when
109 // IPC couldn't be sent to the worker.
110 ServiceWorkerStatusCode Stop();
112 // Stops the worker if the worker is not being debugged (i.e. devtools is
113 // not attached). This method is called by a stop-worker timer to kill
114 // idle workers.
115 void StopIfIdle();
117 // Sends |message| to the embedded worker running in the child process.
118 // It is invalid to call this while the worker is not in STARTING or RUNNING
119 // status.
120 ServiceWorkerStatusCode SendMessage(const IPC::Message& message);
122 // Returns the ServiceRegistry for this worker. It is invalid to call this
123 // when the worker is not in STARTING or RUNNING status.
124 ServiceRegistry* GetServiceRegistry();
126 int embedded_worker_id() const { return embedded_worker_id_; }
127 Status status() const { return status_; }
128 StartingPhase starting_phase() const {
129 DCHECK_EQ(STARTING, status());
130 return starting_phase_;
132 int process_id() const { return process_id_; }
133 int thread_id() const { return thread_id_; }
134 int worker_devtools_agent_route_id() const;
135 MessagePortMessageFilter* message_port_message_filter() const;
137 void AddListener(Listener* listener);
138 void RemoveListener(Listener* listener);
140 void set_devtools_attached(bool attached) { devtools_attached_ = attached; }
141 bool devtools_attached() const { return devtools_attached_; }
143 // Called when the script load request accessed the network.
144 void OnNetworkAccessedForScriptLoad();
146 static std::string StatusToString(Status status);
147 static std::string StartingPhaseToString(StartingPhase phase);
149 private:
150 typedef base::ObserverList<Listener> ListenerList;
151 class DevToolsProxy;
152 friend class EmbeddedWorkerRegistry;
153 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
154 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, DetachDuringStart);
155 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StopDuringStart);
157 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
158 // This instance holds a ref of |registry|.
159 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context,
160 int embedded_worker_id);
162 // Called back from ServiceWorkerProcessManager after Start() passes control
163 // to the UI thread to acquire a reference to the process.
164 static void RunProcessAllocated(
165 base::WeakPtr<EmbeddedWorkerInstance> instance,
166 base::WeakPtr<ServiceWorkerContextCore> context,
167 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
168 const EmbeddedWorkerInstance::StatusCallback& callback,
169 ServiceWorkerStatusCode status,
170 int process_id,
171 bool is_new_process);
172 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
173 const StatusCallback& callback,
174 int process_id,
175 bool is_new_process,
176 ServiceWorkerStatusCode status);
177 // Called back after ProcessAllocated() passes control to the UI thread to
178 // register to WorkerDevToolsManager.
179 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
180 const StatusCallback& callback,
181 bool is_new_process,
182 int worker_devtools_agent_route_id,
183 bool wait_for_debugger);
185 // Called back from Registry when the worker instance has ack'ed that
186 // it is ready for inspection.
187 void OnReadyForInspection();
189 // Called back from Registry when the worker instance has ack'ed that
190 // it finished loading the script and has started a worker thread.
191 void OnScriptLoaded(int thread_id);
193 // Called back from Registry when the worker instance has ack'ed that
194 // it failed to load the script.
195 void OnScriptLoadFailed();
197 // Called back from Registry when the worker instance has ack'ed that
198 // it finished evaluating the script. This is called before OnStarted.
199 void OnScriptEvaluated(bool success);
201 // Called back from Registry when the worker instance has ack'ed that its
202 // WorkerGlobalScope has actually started and evaluated the script. This is
203 // called after OnScriptEvaluated.
204 // This will change the internal status from STARTING to RUNNING.
205 void OnStarted();
207 // Called back from Registry when the worker instance has ack'ed that
208 // its WorkerGlobalScope is actually stopped in the child process.
209 // This will change the internal status from STARTING or RUNNING to
210 // STOPPED.
211 void OnStopped();
212 // Called when ServiceWorkerDispatcherHost for the worker died while it was
213 // running.
214 void OnDetached();
216 // Called back from Registry when the worker instance sends message
217 // to the browser (i.e. EmbeddedWorker observers).
218 // Returns false if the message is not handled.
219 bool OnMessageReceived(const IPC::Message& message);
221 // Called back from Registry when the worker instance reports the exception.
222 void OnReportException(const base::string16& error_message,
223 int line_number,
224 int column_number,
225 const GURL& source_url);
227 // Called back from Registry when the worker instance reports to the console.
228 void OnReportConsoleMessage(int source_identifier,
229 int message_level,
230 const base::string16& message,
231 int line_number,
232 const GURL& source_url);
234 void ReleaseProcess();
236 base::WeakPtr<ServiceWorkerContextCore> context_;
237 scoped_refptr<EmbeddedWorkerRegistry> registry_;
238 const int embedded_worker_id_;
239 Status status_;
240 StartingPhase starting_phase_;
242 // Current running information. -1 indicates the worker is not running.
243 int process_id_;
244 int thread_id_;
245 scoped_ptr<ServiceRegistryImpl> service_registry_;
247 // Whether devtools is attached or not.
248 bool devtools_attached_;
250 // True if the script load request accessed the network. If the script was
251 // served from HTTPCache or ServiceWorkerDatabase this value is false.
252 bool network_accessed_for_script_;
254 StatusCallback start_callback_;
255 ListenerList listener_list_;
256 scoped_ptr<DevToolsProxy> devtools_proxy_;
258 base::TimeTicks start_timing_;
260 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
262 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
265 } // namespace content
267 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_