Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / content / browser / service_worker / embedded_worker_instance.h
blob9abde460397710cf4f1bf1d802260d5b793b97de
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_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 "content/common/content_export.h"
20 #include "content/common/service_worker/service_worker_status_code.h"
21 #include "url/gurl.h"
23 struct EmbeddedWorkerMsg_StartWorker_Params;
25 namespace IPC {
26 class Message;
29 namespace content {
31 class EmbeddedWorkerRegistry;
32 class ServiceWorkerContextCore;
33 struct ServiceWorkerFetchRequest;
35 // This gives an interface to control one EmbeddedWorker instance, which
36 // may be 'in-waiting' or running in one of the child processes added by
37 // AddProcessReference().
38 class CONTENT_EXPORT EmbeddedWorkerInstance {
39 public:
40 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback;
41 enum Status {
42 STOPPED,
43 STARTING,
44 RUNNING,
45 STOPPING,
48 class Listener {
49 public:
50 virtual ~Listener() {}
51 virtual void OnStarted() {}
52 virtual void OnStopped() {}
53 virtual void OnPausedAfterDownload() {}
54 virtual void OnReportException(const base::string16& error_message,
55 int line_number,
56 int column_number,
57 const GURL& source_url) {}
58 virtual void OnReportConsoleMessage(int source_identifier,
59 int message_level,
60 const base::string16& message,
61 int line_number,
62 const GURL& source_url) {}
63 // These should return false if the message is not handled by this
64 // listener. (TODO(kinuko): consider using IPC::Listener interface)
65 // TODO(kinuko): Deprecate OnReplyReceived.
66 virtual bool OnMessageReceived(const IPC::Message& message) = 0;
69 ~EmbeddedWorkerInstance();
71 // Starts the worker. It is invalid to call this when the worker is not in
72 // STOPPED status. |callback| is invoked when the worker's process is created
73 // if necessary and the IPC to evaluate the worker's script is sent.
74 // Observer::OnStarted() is run when the worker is actually started.
75 void Start(int64 service_worker_version_id,
76 const GURL& scope,
77 const GURL& script_url,
78 bool pause_after_download,
79 const std::vector<int>& possible_process_ids,
80 const StatusCallback& callback);
82 // Stops the worker. It is invalid to call this when the worker is
83 // not in STARTING or RUNNING status.
84 // This returns false if stopping a worker fails immediately, e.g. when
85 // IPC couldn't be sent to the worker.
86 ServiceWorkerStatusCode Stop();
88 // Sends |message| to the embedded worker running in the child process.
89 // It is invalid to call this while the worker is not in RUNNING status.
90 ServiceWorkerStatusCode SendMessage(const IPC::Message& message);
92 void ResumeAfterDownload();
94 // Add or remove |process_id| to the internal process set where this
95 // worker can be started.
96 void AddProcessReference(int process_id);
97 void ReleaseProcessReference(int process_id);
98 bool HasProcessToRun() const { return !process_refs_.empty(); }
100 int embedded_worker_id() const { return embedded_worker_id_; }
101 Status status() const { return status_; }
102 int process_id() const { return process_id_; }
103 int thread_id() const { return thread_id_; }
104 int worker_devtools_agent_route_id() const {
105 return worker_devtools_agent_route_id_;
108 void AddListener(Listener* listener);
109 void RemoveListener(Listener* listener);
111 private:
112 typedef ObserverList<Listener> ListenerList;
114 friend class EmbeddedWorkerRegistry;
115 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop);
116 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, SortProcesses);
118 typedef std::map<int, int> ProcessRefMap;
120 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
121 // This instance holds a ref of |registry|.
122 EmbeddedWorkerInstance(base::WeakPtr<ServiceWorkerContextCore> context,
123 int embedded_worker_id);
125 // Called back from ServiceWorkerProcessManager after Start() passes control
126 // to the UI thread to acquire a reference to the process.
127 static void RunProcessAllocated(
128 base::WeakPtr<EmbeddedWorkerInstance> instance,
129 base::WeakPtr<ServiceWorkerContextCore> context,
130 scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
131 const EmbeddedWorkerInstance::StatusCallback& callback,
132 ServiceWorkerStatusCode status,
133 int process_id);
134 void ProcessAllocated(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
135 const StatusCallback& callback,
136 int process_id,
137 ServiceWorkerStatusCode status);
138 // Called back after ProcessAllocated() passes control to the UI thread to
139 // register to WorkerDevToolsManager.
140 void SendStartWorker(scoped_ptr<EmbeddedWorkerMsg_StartWorker_Params> params,
141 const StatusCallback& callback,
142 int worker_devtools_agent_route_id,
143 bool wait_for_debugger);
145 // Called back from Registry when the worker instance has ack'ed that
146 // it is ready for inspection.
147 void OnReadyForInspection();
149 // Called back from Registry when the worker instance has ack'ed that
150 // it finished loading the script and has started a worker thread.
151 void OnScriptLoaded(int thread_id);
153 // Called back from Registry when the worker instance has ack'ed that
154 // it failed to load the script.
155 void OnScriptLoadFailed();
157 // Called back from Registry when the worker instance has ack'ed that
158 // its WorkerGlobalScope is actually started and parsed.
159 // This will change the internal status from STARTING to RUNNING.
160 void OnStarted();
162 void OnPausedAfterDownload();
164 // Called back from Registry when the worker instance has ack'ed that
165 // its WorkerGlobalScope is actually stopped in the child process.
166 // This will change the internal status from STARTING or RUNNING to
167 // STOPPED.
168 void OnStopped();
170 // Called back from Registry when the worker instance sends message
171 // to the browser (i.e. EmbeddedWorker observers).
172 // Returns false if the message is not handled.
173 bool OnMessageReceived(const IPC::Message& message);
175 // Called back from Registry when the worker instance reports the exception.
176 void OnReportException(const base::string16& error_message,
177 int line_number,
178 int column_number,
179 const GURL& source_url);
181 // Called back from Registry when the worker instance reports to the console.
182 void OnReportConsoleMessage(int source_identifier,
183 int message_level,
184 const base::string16& message,
185 int line_number,
186 const GURL& source_url);
188 // Chooses a list of processes to try to start this worker in, ordered by how
189 // many clients are currently in those processes.
190 std::vector<int> SortProcesses(
191 const std::vector<int>& possible_process_ids) const;
193 base::WeakPtr<ServiceWorkerContextCore> context_;
194 scoped_refptr<EmbeddedWorkerRegistry> registry_;
195 const int embedded_worker_id_;
196 Status status_;
198 // Current running information. -1 indicates the worker is not running.
199 int process_id_;
200 int thread_id_;
201 int worker_devtools_agent_route_id_;
203 ProcessRefMap process_refs_;
204 ListenerList listener_list_;
206 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_;
208 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance);
211 } // namespace content
213 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_