Android: Store language .pak files in res/raw rather than assets
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_instance.h
blob309c2ddf7a33969099f9723ccf525ffe16a4bea5
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 base::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 bool is_new_process);
166 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
167 const StatusCallback& callback,
168 int process_id,
169 bool is_new_process,
170 ServiceWorkerStatusCode status);
171 // Called back after ProcessAllocated() passes control to the UI thread to
172 // register to WorkerDevToolsManager.
173 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
174 const StatusCallback& callback,
175 bool is_new_process,
176 int worker_devtools_agent_route_id,
177 bool wait_for_debugger);
179 // Called back from Registry when the worker instance has ack'ed that
180 // it is ready for inspection.
181 void OnReadyForInspection();
183 // Called back from Registry when the worker instance has ack'ed that
184 // it finished loading the script and has started a worker thread.
185 void OnScriptLoaded(int thread_id);
187 // Called back from Registry when the worker instance has ack'ed that
188 // it failed to load the script.
189 void OnScriptLoadFailed();
191 // Called back from Registry when the worker instance has ack'ed that
192 // it finished evaluating the script. This is called before OnStarted.
193 void OnScriptEvaluated(bool success);
195 // Called back from Registry when the worker instance has ack'ed that its
196 // WorkerGlobalScope has actually started and evaluated the script. This is
197 // called after OnScriptEvaluated.
198 // This will change the internal status from STARTING to RUNNING.
199 void OnStarted();
201 void OnPausedAfterDownload();
203 // Called back from Registry when the worker instance has ack'ed that
204 // its WorkerGlobalScope is actually stopped in the child process.
205 // This will change the internal status from STARTING or RUNNING to
206 // STOPPED.
207 void OnStopped();
209 // Called back from Registry when the worker instance sends message
210 // to the browser (i.e. EmbeddedWorker observers).
211 // Returns false if the message is not handled.
212 bool OnMessageReceived(const IPC::Message& message);
214 // Called back from Registry when the worker instance reports the exception.
215 void OnReportException(const base::string16& error_message,
216 int line_number,
217 int column_number,
218 const GURL& source_url);
220 // Called back from Registry when the worker instance reports to the console.
221 void OnReportConsoleMessage(int source_identifier,
222 int message_level,
223 const base::string16& message,
224 int line_number,
225 const GURL& source_url);
227 base::WeakPtr<ServiceWorkerContextCore> context_;
228 scoped_refptr<EmbeddedWorkerRegistry> registry_;
229 const int embedded_worker_id_;
230 Status status_;
231 StartingPhase starting_phase_;
233 // Current running information. -1 indicates the worker is not running.
234 int process_id_;
235 int thread_id_;
237 // Whether devtools is attached or not.
238 bool devtools_attached_;
240 // True if the script load request accessed the network. If the script was
241 // served from HTTPCache or ServiceWorkerDatabase this value is false.
242 bool network_accessed_for_script_;
244 StatusCallback start_callback_;
245 ListenerList listener_list_;
246 scoped_ptr<DevToolsProxy> devtools_proxy_;
248 base::TimeTicks start_timing_;
250 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
252 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
255 } // namespace content
257 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_