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_
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"
25 // Windows headers will redefine SendMessage.
30 struct EmbeddedWorkerMsg_StartWorker_Params
;
38 class EmbeddedWorkerRegistry
;
39 class MessagePortMessageFilter
;
40 class ServiceRegistry
;
41 class ServiceRegistryImpl
;
42 class ServiceWorkerContextCore
;
43 struct ServiceWorkerFetchRequest
;
45 // This gives an interface to control one EmbeddedWorker instance, which
46 // may be 'in-waiting' or running in one of the child processes added by
47 // AddProcessReference().
48 class CONTENT_EXPORT EmbeddedWorkerInstance
{
50 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
58 // This enum is used in UMA histograms, so don't change the order or remove
63 REGISTERING_TO_DEVTOOLS
,
68 STARTING_PHASE_MAX_VALUE
,
73 virtual ~Listener() {}
74 virtual void OnScriptLoaded() {}
75 virtual void OnStarting() {}
76 virtual void OnStarted() {}
77 virtual void OnStopping() {}
78 // Received ACK from renderer that the worker context terminated.
79 virtual void OnStopped(Status old_status
) {}
80 // The browser-side IPC endpoint for communication with the worker died.
81 virtual void OnDetached(Status old_status
) {}
82 virtual void OnReportException(const base::string16
& error_message
,
85 const GURL
& source_url
) {}
86 virtual void OnReportConsoleMessage(int source_identifier
,
88 const base::string16
& message
,
90 const GURL
& source_url
) {}
91 // These should return false if the message is not handled by this
92 // listener. (TODO(kinuko): consider using IPC::Listener interface)
93 // TODO(kinuko): Deprecate OnReplyReceived.
94 virtual bool OnMessageReceived(const IPC::Message
& message
) = 0;
97 ~EmbeddedWorkerInstance();
99 // Starts the worker. It is invalid to call this when the worker is not in
100 // STOPPED status. |callback| is invoked after the worker script has been
101 // started and evaluated, or when an error occurs.
102 void Start(int64 service_worker_version_id
,
104 const GURL
& script_url
,
105 const StatusCallback
& callback
);
107 // Stops the worker. It is invalid to call this when the worker is
108 // not in STARTING or RUNNING status.
109 // This returns false if stopping a worker fails immediately, e.g. when
110 // IPC couldn't be sent to the worker.
111 ServiceWorkerStatusCode
Stop();
113 // Stops the worker if the worker is not being debugged (i.e. devtools is
114 // not attached). This method is called by a stop-worker timer to kill
118 // Sends |message| to the embedded worker running in the child process.
119 // It is invalid to call this while the worker is not in STARTING or RUNNING
121 ServiceWorkerStatusCode
SendMessage(const IPC::Message
& message
);
123 // Returns the ServiceRegistry for this worker. It is invalid to call this
124 // when the worker is not in STARTING or RUNNING status.
125 ServiceRegistry
* GetServiceRegistry();
127 int embedded_worker_id() const { return embedded_worker_id_
; }
128 Status
status() const { return status_
; }
129 StartingPhase
starting_phase() const {
130 DCHECK_EQ(STARTING
, status());
131 return starting_phase_
;
133 int process_id() const { return process_id_
; }
134 int thread_id() const { return thread_id_
; }
135 int worker_devtools_agent_route_id() const;
136 MessagePortMessageFilter
* message_port_message_filter() const;
138 void AddListener(Listener
* listener
);
139 void RemoveListener(Listener
* listener
);
141 void set_devtools_attached(bool attached
) { devtools_attached_
= attached
; }
142 bool devtools_attached() const { return devtools_attached_
; }
144 // Called when the script load request accessed the network.
145 void OnNetworkAccessedForScriptLoad();
147 static std::string
StatusToString(Status status
);
148 static std::string
StartingPhaseToString(StartingPhase phase
);
151 typedef base::ObserverList
<Listener
> ListenerList
;
153 friend class EmbeddedWorkerRegistry
;
154 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest
, StartAndStop
);
156 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
157 // This instance holds a ref of |registry|.
158 EmbeddedWorkerInstance(base::WeakPtr
<ServiceWorkerContextCore
> context
,
159 int embedded_worker_id
);
161 // Called back from ServiceWorkerProcessManager after Start() passes control
162 // to the UI thread to acquire a reference to the process.
163 static void RunProcessAllocated(
164 base::WeakPtr
<EmbeddedWorkerInstance
> instance
,
165 base::WeakPtr
<ServiceWorkerContextCore
> context
,
166 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
167 const EmbeddedWorkerInstance::StatusCallback
& callback
,
168 ServiceWorkerStatusCode status
,
170 bool is_new_process
);
171 void ProcessAllocated(scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
172 const StatusCallback
& callback
,
175 ServiceWorkerStatusCode status
);
176 // Called back after ProcessAllocated() passes control to the UI thread to
177 // register to WorkerDevToolsManager.
178 void SendStartWorker(scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
179 const StatusCallback
& callback
,
181 int worker_devtools_agent_route_id
,
182 bool wait_for_debugger
);
184 // Called back from Registry when the worker instance has ack'ed that
185 // it is ready for inspection.
186 void OnReadyForInspection();
188 // Called back from Registry when the worker instance has ack'ed that
189 // it finished loading the script and has started a worker thread.
190 void OnScriptLoaded(int thread_id
);
192 // Called back from Registry when the worker instance has ack'ed that
193 // it failed to load the script.
194 void OnScriptLoadFailed();
196 // Called back from Registry when the worker instance has ack'ed that
197 // it finished evaluating the script. This is called before OnStarted.
198 void OnScriptEvaluated(bool success
);
200 // Called back from Registry when the worker instance has ack'ed that its
201 // WorkerGlobalScope has actually started and evaluated the script. This is
202 // called after OnScriptEvaluated.
203 // This will change the internal status from STARTING to RUNNING.
206 // Called back from Registry when the worker instance has ack'ed that
207 // its WorkerGlobalScope is actually stopped in the child process.
208 // This will change the internal status from STARTING or RUNNING to
211 // Called when ServiceWorkerDispatcherHost for the worker died while it was
215 // Called back from Registry when the worker instance sends message
216 // to the browser (i.e. EmbeddedWorker observers).
217 // Returns false if the message is not handled.
218 bool OnMessageReceived(const IPC::Message
& message
);
220 // Called back from Registry when the worker instance reports the exception.
221 void OnReportException(const base::string16
& error_message
,
224 const GURL
& source_url
);
226 // Called back from Registry when the worker instance reports to the console.
227 void OnReportConsoleMessage(int source_identifier
,
229 const base::string16
& message
,
231 const GURL
& source_url
);
233 void ReleaseProcess();
235 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
236 scoped_refptr
<EmbeddedWorkerRegistry
> registry_
;
237 const int embedded_worker_id_
;
239 StartingPhase starting_phase_
;
241 // Current running information. -1 indicates the worker is not running.
244 scoped_ptr
<ServiceRegistryImpl
> service_registry_
;
246 // Whether devtools is attached or not.
247 bool devtools_attached_
;
249 // True if the script load request accessed the network. If the script was
250 // served from HTTPCache or ServiceWorkerDatabase this value is false.
251 bool network_accessed_for_script_
;
253 StatusCallback start_callback_
;
254 ListenerList listener_list_
;
255 scoped_ptr
<DevToolsProxy
> devtools_proxy_
;
257 base::TimeTicks start_timing_
;
259 base::WeakPtrFactory
<EmbeddedWorkerInstance
> weak_factory_
;
261 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance
);
264 } // namespace content
266 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_