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 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
{
48 typedef base::Callback
<void(ServiceWorkerStatusCode
)> StatusCallback
;
58 virtual ~Listener() {}
59 virtual void OnScriptLoaded() {}
60 virtual void OnStarted() {}
61 virtual void OnStopped(Status old_status
) {}
62 virtual void OnPausedAfterDownload() {}
63 virtual void OnReportException(const base::string16
& error_message
,
66 const GURL
& source_url
) {}
67 virtual void OnReportConsoleMessage(int source_identifier
,
69 const base::string16
& message
,
71 const GURL
& source_url
) {}
72 // These should return false if the message is not handled by this
73 // listener. (TODO(kinuko): consider using IPC::Listener interface)
74 // TODO(kinuko): Deprecate OnReplyReceived.
75 virtual bool OnMessageReceived(const IPC::Message
& message
) = 0;
78 ~EmbeddedWorkerInstance();
80 // Starts the worker. It is invalid to call this when the worker is not in
81 // STOPPED status. |callback| is invoked after the worker script has been
82 // started and evaluated, or when an error occurs.
83 void Start(int64 service_worker_version_id
,
85 const GURL
& script_url
,
86 bool pause_after_download
,
87 const StatusCallback
& callback
);
89 // Stops the worker. It is invalid to call this when the worker is
90 // not in STARTING or RUNNING status.
91 // This returns false if stopping a worker fails immediately, e.g. when
92 // IPC couldn't be sent to the worker.
93 ServiceWorkerStatusCode
Stop();
95 // Stops the worker if the worker is not being debugged (i.e. devtools is
96 // not attached). This method is called by a stop-worker timer to kill
100 // Sends |message| to the embedded worker running in the child process.
101 // It is invalid to call this while the worker is not in STARTING or RUNNING
103 ServiceWorkerStatusCode
SendMessage(const IPC::Message
& message
);
105 void ResumeAfterDownload();
107 int embedded_worker_id() const { return embedded_worker_id_
; }
108 Status
status() const { return status_
; }
109 int process_id() const { return process_id_
; }
110 int thread_id() const { return thread_id_
; }
111 int worker_devtools_agent_route_id() const;
112 MessagePortMessageFilter
* message_port_message_filter() const;
114 void AddListener(Listener
* listener
);
115 void RemoveListener(Listener
* listener
);
117 void set_devtools_attached(bool attached
) { devtools_attached_
= attached
; }
119 // Called when the script load request accessed the network.
120 void OnNetworkAccessedForScriptLoad();
123 typedef ObserverList
<Listener
> ListenerList
;
125 friend class EmbeddedWorkerRegistry
;
126 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest
, StartAndStop
);
128 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker().
129 // This instance holds a ref of |registry|.
130 EmbeddedWorkerInstance(base::WeakPtr
<ServiceWorkerContextCore
> context
,
131 int embedded_worker_id
);
133 // Called back from ServiceWorkerProcessManager after Start() passes control
134 // to the UI thread to acquire a reference to the process.
135 static void RunProcessAllocated(
136 base::WeakPtr
<EmbeddedWorkerInstance
> instance
,
137 base::WeakPtr
<ServiceWorkerContextCore
> context
,
138 scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
139 const EmbeddedWorkerInstance::StatusCallback
& callback
,
140 ServiceWorkerStatusCode status
,
142 void ProcessAllocated(scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
143 const StatusCallback
& callback
,
145 ServiceWorkerStatusCode status
);
146 // Called back after ProcessAllocated() passes control to the UI thread to
147 // register to WorkerDevToolsManager.
148 void SendStartWorker(scoped_ptr
<EmbeddedWorkerMsg_StartWorker_Params
> params
,
149 const StatusCallback
& callback
,
150 int worker_devtools_agent_route_id
,
151 bool wait_for_debugger
);
153 // Called back from Registry when the worker instance has ack'ed that
154 // it is ready for inspection.
155 void OnReadyForInspection();
157 // Called back from Registry when the worker instance has ack'ed that
158 // it finished loading the script and has started a worker thread.
159 void OnScriptLoaded(int thread_id
);
161 // Called back from Registry when the worker instance has ack'ed that
162 // it failed to load the script.
163 void OnScriptLoadFailed();
165 // Called back from Registry when the worker instance has ack'ed that
166 // it finished evaluating the script. This is called before OnStarted.
167 void OnScriptEvaluated(bool success
);
169 // Called back from Registry when the worker instance has ack'ed that its
170 // WorkerGlobalScope has actually started and evaluated the script. This is
171 // called after OnScriptEvaluated.
172 // This will change the internal status from STARTING to RUNNING.
175 void OnPausedAfterDownload();
177 // Called back from Registry when the worker instance has ack'ed that
178 // its WorkerGlobalScope is actually stopped in the child process.
179 // This will change the internal status from STARTING or RUNNING to
183 // Called back from Registry when the worker instance sends message
184 // to the browser (i.e. EmbeddedWorker observers).
185 // Returns false if the message is not handled.
186 bool OnMessageReceived(const IPC::Message
& message
);
188 // Called back from Registry when the worker instance reports the exception.
189 void OnReportException(const base::string16
& error_message
,
192 const GURL
& source_url
);
194 // Called back from Registry when the worker instance reports to the console.
195 void OnReportConsoleMessage(int source_identifier
,
197 const base::string16
& message
,
199 const GURL
& source_url
);
201 base::WeakPtr
<ServiceWorkerContextCore
> context_
;
202 scoped_refptr
<EmbeddedWorkerRegistry
> registry_
;
203 const int embedded_worker_id_
;
206 // Current running information. -1 indicates the worker is not running.
210 // Whether devtools is attached or not.
211 bool devtools_attached_
;
213 // True if the script load request accessed the network. If the script was
214 // served from HTTPCache or ServiceWorkerDatabase this value is false.
215 bool network_accessed_for_script_
;
217 StatusCallback start_callback_
;
218 ListenerList listener_list_
;
219 scoped_ptr
<DevToolsProxy
> devtools_proxy_
;
221 base::TimeTicks start_timing_
;
223 base::WeakPtrFactory
<EmbeddedWorkerInstance
> weak_factory_
;
225 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance
);
228 } // namespace content
230 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_