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