Supervised user whitelists: Cleanup
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_instance.h
blob04ce412ad2295c290320f8b32ebfd6f4bb3072f8
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 <vector>
11 #include "base/basictypes.h"
12 #include "base/callback.h"
13 #include "base/callback_forward.h"
14 #include "base/gtest_prod_util.h"
15 #include "base/logging.h"
16 #include "base/memory/ref_counted.h"
17 #include "base/memory/weak_ptr.h"
18 #include "base/observer_list.h"
19 #include "base/strings/string16.h"
20 #include "base/time/time.h"
21 #include "content/common/content_export.h"
22 #include "content/common/service_worker/service_worker_status_code.h"
23 #include "url/gurl.h"
25 // Windows headers will redefine SendMessage.
26 #ifdef SendMessage
27 #undef SendMessage
28 #endif
30 struct EmbeddedWorkerMsg_StartWorker_Params;
32 namespace IPC {
33 class Message;
36 namespace content {
38 class EmbeddedWorkerRegistry;
39 class MessagePortMessageFilter;
40 class ServiceWorkerContextCore;
41 struct ServiceWorkerFetchRequest;
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 STARTING_PHASE_MAX_VALUE,
69 class Listener {
70 public:
71 virtual ~Listener() {}
72 virtual void OnScriptLoaded() {}
73 virtual void OnStarting() {}
74 virtual void OnStarted() {}
75 virtual void OnStopping() {}
76 virtual void OnStopped(Status old_status) {}
77 virtual void OnPausedAfterDownload() {}
78 virtual void OnReportException(const base::string16& error_message,
79 int line_number,
80 int column_number,
81 const GURL& source_url) {}
82 virtual void OnReportConsoleMessage(int source_identifier,
83 int message_level,
84 const base::string16& message,
85 int line_number,
86 const GURL& source_url) {}
87 // These should return false if the message is not handled by this
88 // listener. (TODO(kinuko): consider using IPC::Listener interface)
89 // TODO(kinuko): Deprecate OnReplyReceived.
90 virtual bool OnMessageReceived(const IPC::Message& message) = 0;
93 ~EmbeddedWorkerInstance();
95 // Starts the worker. It is invalid to call this when the worker is not in
96 // STOPPED status. |callback| is invoked after the worker script has been
97 // started and evaluated, or when an error occurs.
98 void Start(int64 service_worker_version_id,
99 const GURL& scope,
100 const GURL& script_url,
101 bool pause_after_download,
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 void ResumeAfterDownload();
122 int embedded_worker_id() const { return embedded_worker_id_; }
123 Status status() const { return status_; }
124 StartingPhase starting_phase() const {
125 DCHECK_EQ(STARTING, status());
126 return starting_phase_;
128 int process_id() const { return process_id_; }
129 int thread_id() const { return thread_id_; }
130 int worker_devtools_agent_route_id() const;
131 MessagePortMessageFilter* message_port_message_filter() const;
133 void AddListener(Listener* listener);
134 void RemoveListener(Listener* listener);
136 void set_devtools_attached(bool attached) { devtools_attached_ = attached; }
137 bool devtools_attached() const { return devtools_attached_; }
139 // Called when the script load request accessed the network.
140 void OnNetworkAccessedForScriptLoad();
142 static std::string StatusToString(Status status);
143 static std::string StartingPhaseToString(StartingPhase phase);
145 private:
146 typedef ObserverList<Listener> ListenerList;
147 class DevToolsProxy;
148 friend class EmbeddedWorkerRegistry;
149 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
151 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
152 // This instance holds a ref of |registry|.
153 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context,
154 int embedded_worker_id);
156 // Called back from ServiceWorkerProcessManager after Start() passes control
157 // to the UI thread to acquire a reference to the process.
158 static void RunProcessAllocated(
159 base::WeakPtr<EmbeddedWorkerInstance> instance,
160 base::WeakPtr<ServiceWorkerContextCore> context,
161 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
162 const EmbeddedWorkerInstance::StatusCallback& callback,
163 ServiceWorkerStatusCode status,
164 int process_id);
165 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
166 const StatusCallback& callback,
167 int process_id,
168 ServiceWorkerStatusCode status);
169 // Called back after ProcessAllocated() passes control to the UI thread to
170 // register to WorkerDevToolsManager.
171 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
172 const StatusCallback& callback,
173 int worker_devtools_agent_route_id,
174 bool wait_for_debugger);
176 // Called back from Registry when the worker instance has ack'ed that
177 // it is ready for inspection.
178 void OnReadyForInspection();
180 // Called back from Registry when the worker instance has ack'ed that
181 // it finished loading the script and has started a worker thread.
182 void OnScriptLoaded(int thread_id);
184 // Called back from Registry when the worker instance has ack'ed that
185 // it failed to load the script.
186 void OnScriptLoadFailed();
188 // Called back from Registry when the worker instance has ack'ed that
189 // it finished evaluating the script. This is called before OnStarted.
190 void OnScriptEvaluated(bool success);
192 // Called back from Registry when the worker instance has ack'ed that its
193 // WorkerGlobalScope has actually started and evaluated the script. This is
194 // called after OnScriptEvaluated.
195 // This will change the internal status from STARTING to RUNNING.
196 void OnStarted();
198 void OnPausedAfterDownload();
200 // Called back from Registry when the worker instance has ack'ed that
201 // its WorkerGlobalScope is actually stopped in the child process.
202 // This will change the internal status from STARTING or RUNNING to
203 // STOPPED.
204 void OnStopped();
206 // Called back from Registry when the worker instance sends message
207 // to the browser (i.e. EmbeddedWorker observers).
208 // Returns false if the message is not handled.
209 bool OnMessageReceived(const IPC::Message& message);
211 // Called back from Registry when the worker instance reports the exception.
212 void OnReportException(const base::string16& error_message,
213 int line_number,
214 int column_number,
215 const GURL& source_url);
217 // Called back from Registry when the worker instance reports to the console.
218 void OnReportConsoleMessage(int source_identifier,
219 int message_level,
220 const base::string16& message,
221 int line_number,
222 const GURL& source_url);
224 base::WeakPtr<ServiceWorkerContextCore> context_;
225 scoped_refptr<EmbeddedWorkerRegistry> registry_;
226 const int embedded_worker_id_;
227 Status status_;
228 StartingPhase starting_phase_;
230 // Current running information. -1 indicates the worker is not running.
231 int process_id_;
232 int thread_id_;
234 // Whether devtools is attached or not.
235 bool devtools_attached_;
237 // True if the script load request accessed the network. If the script was
238 // served from HTTPCache or ServiceWorkerDatabase this value is false.
239 bool network_accessed_for_script_;
241 StatusCallback start_callback_;
242 ListenerList listener_list_;
243 scoped_ptr<DevToolsProxy> devtools_proxy_;
245 base::TimeTicks start_timing_;
247 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
249 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
252 } // namespace content
254 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_