OLD | NEW |
---|---|
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
7 | 7 |
8 #include <stdint.h> | 8 #include <stdint.h> |
9 | 9 |
10 #include <memory> | 10 #include <memory> |
11 #include <string> | 11 #include <string> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/callback_forward.h" | 14 #include "base/callback_forward.h" |
15 #include "base/gtest_prod_util.h" | 15 #include "base/gtest_prod_util.h" |
16 #include "base/logging.h" | 16 #include "base/logging.h" |
17 #include "base/macros.h" | 17 #include "base/macros.h" |
18 #include "base/memory/ref_counted.h" | 18 #include "base/memory/ref_counted.h" |
19 #include "base/memory/weak_ptr.h" | 19 #include "base/memory/weak_ptr.h" |
20 #include "base/observer_list.h" | 20 #include "base/observer_list.h" |
21 #include "base/strings/string16.h" | 21 #include "base/strings/string16.h" |
22 #include "base/time/time.h" | 22 #include "base/time/time.h" |
23 #include "content/browser/service_worker/embedded_worker_status.h" | 23 #include "content/browser/service_worker/embedded_worker_status.h" |
24 #include "content/browser/service_worker/service_worker_metrics.h" | 24 #include "content/browser/service_worker/service_worker_metrics.h" |
25 #include "content/common/content_export.h" | 25 #include "content/common/content_export.h" |
26 #include "content/common/service_worker/embedded_worker.mojom.h" | 26 #include "content/common/service_worker/embedded_worker.mojom.h" |
27 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" | 27 #include "content/common/service_worker/service_worker_event_dispatcher.mojom.h" |
28 #include "content/common/service_worker/service_worker_status_code.h" | 28 #include "content/common/service_worker/service_worker_status_code.h" |
29 #include "mojo/public/cpp/bindings/associated_binding.h" | |
29 #include "url/gurl.h" | 30 #include "url/gurl.h" |
30 | 31 |
31 // Windows headers will redefine SendMessage. | 32 // Windows headers will redefine SendMessage. |
32 #ifdef SendMessage | 33 #ifdef SendMessage |
33 #undef SendMessage | 34 #undef SendMessage |
34 #endif | 35 #endif |
35 | 36 |
36 namespace IPC { | 37 namespace IPC { |
37 class Message; | 38 class Message; |
38 } | 39 } |
39 | 40 |
40 namespace content { | 41 namespace content { |
41 | 42 |
42 class EmbeddedWorkerRegistry; | 43 class EmbeddedWorkerRegistry; |
43 struct EmbeddedWorkerStartParams; | 44 struct EmbeddedWorkerStartParams; |
44 class ServiceWorkerContextCore; | 45 class ServiceWorkerContextCore; |
45 | 46 |
46 // This gives an interface to control one EmbeddedWorker instance, which | 47 // This gives an interface to control one EmbeddedWorker instance, which |
47 // may be 'in-waiting' or running in one of the child processes added by | 48 // may be 'in-waiting' or running in one of the child processes added by |
48 // AddProcessReference(). | 49 // AddProcessReference(). |
49 class CONTENT_EXPORT EmbeddedWorkerInstance { | 50 class CONTENT_EXPORT EmbeddedWorkerInstance |
51 : NON_EXPORTED_BASE(public mojom::EmbeddedWorkerInstanceHost) { | |
50 public: | 52 public: |
51 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; | 53 typedef base::Callback<void(ServiceWorkerStatusCode)> StatusCallback; |
52 | 54 |
53 // This enum is used in UMA histograms. Append-only. | 55 // This enum is used in UMA histograms. Append-only. |
54 enum StartingPhase { | 56 enum StartingPhase { |
55 NOT_STARTING, | 57 NOT_STARTING, |
56 ALLOCATING_PROCESS, | 58 ALLOCATING_PROCESS, |
57 REGISTERING_TO_DEVTOOLS, | 59 REGISTERING_TO_DEVTOOLS, |
58 SENT_START_WORKER, | 60 SENT_START_WORKER, |
59 SCRIPT_DOWNLOADING, | 61 SCRIPT_DOWNLOADING, |
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
92 const GURL& source_url) {} | 94 const GURL& source_url) {} |
93 virtual void OnReportConsoleMessage(int source_identifier, | 95 virtual void OnReportConsoleMessage(int source_identifier, |
94 int message_level, | 96 int message_level, |
95 const base::string16& message, | 97 const base::string16& message, |
96 int line_number, | 98 int line_number, |
97 const GURL& source_url) {} | 99 const GURL& source_url) {} |
98 // Returns false if the message is not handled by this listener. | 100 // Returns false if the message is not handled by this listener. |
99 CONTENT_EXPORT virtual bool OnMessageReceived(const IPC::Message& message); | 101 CONTENT_EXPORT virtual bool OnMessageReceived(const IPC::Message& message); |
100 }; | 102 }; |
101 | 103 |
102 ~EmbeddedWorkerInstance(); | 104 ~EmbeddedWorkerInstance() override; |
103 | 105 |
104 // Starts the worker. It is invalid to call this when the worker is not in | 106 // Starts the worker. It is invalid to call this when the worker is not in |
105 // STOPPED status. |callback| is invoked after the worker script has been | 107 // STOPPED status. |callback| is invoked after the worker script has been |
106 // started and evaluated, or when an error occurs. | 108 // started and evaluated, or when an error occurs. |
107 // |params| should be populated with service worker version info needed | 109 // |params| should be populated with service worker version info needed |
108 // to start the worker. | 110 // to start the worker. |
109 void Start(std::unique_ptr<EmbeddedWorkerStartParams> params, | 111 void Start(std::unique_ptr<EmbeddedWorkerStartParams> params, |
110 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, | 112 mojom::ServiceWorkerEventDispatcherRequest dispatcher_request, |
111 const StatusCallback& callback); | 113 const StatusCallback& callback); |
112 | 114 |
(...skipping 99 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
212 int worker_devtools_agent_route_id, | 214 int worker_devtools_agent_route_id, |
213 bool wait_for_debugger); | 215 bool wait_for_debugger); |
214 | 216 |
215 // Sends StartWorker message via Mojo. | 217 // Sends StartWorker message via Mojo. |
216 ServiceWorkerStatusCode SendStartWorker( | 218 ServiceWorkerStatusCode SendStartWorker( |
217 std::unique_ptr<EmbeddedWorkerStartParams> params); | 219 std::unique_ptr<EmbeddedWorkerStartParams> params); |
218 | 220 |
219 // Called back from StartTask after a start worker message is sent. | 221 // Called back from StartTask after a start worker message is sent. |
220 void OnStartWorkerMessageSent(); | 222 void OnStartWorkerMessageSent(); |
221 | 223 |
222 // Called back from Registry when the worker instance has ack'ed that | 224 // mojom::EmbeddedWorkerInstanceHost |
shimazu
2017/04/06 05:01:36
// Implements mojom::EmbeddedWorkerInstanceHost.
leonhsl(Using Gerrit)
2017/04/06 09:58:55
Done.
| |
225 // Be called when the worker instance has ack'ed that | |
223 // it is ready for inspection. | 226 // it is ready for inspection. |
224 void OnReadyForInspection(); | 227 void OnReadyForInspection() override; |
225 | 228 // Be called when the worker instance has ack'ed that |
226 // Called back from Registry when the worker instance has ack'ed that | |
227 // it finished loading the script. | 229 // it finished loading the script. |
228 void OnScriptLoaded(); | 230 void OnScriptLoaded() override; |
229 | 231 // Be called when the worker instance has ack'ed that |
230 // Called back from Registry when the worker instance has ack'ed that | |
231 // it has started a worker thread. | 232 // it has started a worker thread. |
232 void OnThreadStarted(int thread_id); | 233 void OnThreadStarted(int thread_id, int provider_id) override; |
233 | 234 // Be called when the worker instance has ack'ed that |
234 // Called back from Registry when the worker instance has ack'ed that | |
235 // it failed to load the script. | 235 // it failed to load the script. |
236 void OnScriptLoadFailed(); | 236 void OnScriptLoadFailed() override; |
237 | 237 // Be called when the worker instance has ack'ed that |
238 // Called back from Registry when the worker instance has ack'ed that | |
239 // it finished evaluating the script. This is called before OnStarted. | 238 // it finished evaluating the script. This is called before OnStarted. |
240 void OnScriptEvaluated(bool success); | 239 void OnScriptEvaluated(bool success) override; |
241 | 240 // Be called when the worker instance has ack'ed that |
242 // Called back from Registry when the worker instance has ack'ed that its | |
243 // WorkerGlobalScope has actually started and evaluated the script. This is | 241 // WorkerGlobalScope has actually started and evaluated the script. This is |
244 // called after OnScriptEvaluated. | 242 // called after OnScriptEvaluated. |
245 // This will change the internal status from STARTING to RUNNING. | 243 // This will change the internal status from STARTING to RUNNING. |
246 void OnStarted(); | 244 void OnStarted() override; |
247 | 245 // Be called when the worker instance has ack'ed that |
248 // Called back from Registry when the worker instance has ack'ed that | |
249 // its WorkerGlobalScope is actually stopped in the child process. | 246 // its WorkerGlobalScope is actually stopped in the child process. |
250 // This will change the internal status from STARTING or RUNNING to | 247 // This will change the internal status from STARTING or RUNNING to |
251 // STOPPED. | 248 // STOPPED. |
252 void OnStopped(); | 249 void OnStopped() override; |
250 // Be called when the worker instance reports the exception. | |
251 void OnReportException(const base::string16& error_message, | |
252 int line_number, | |
253 int column_number, | |
254 const GURL& source_url) override; | |
255 // Be called when the worker instance reports to the console. | |
256 void OnReportConsoleMessage(int source_identifier, | |
257 int message_level, | |
258 const base::string16& message, | |
259 int line_number, | |
260 const GURL& source_url) override; | |
253 | 261 |
254 // Called when ServiceWorkerDispatcherHost for the worker died while it was | 262 // Called when ServiceWorkerDispatcherHost for the worker died while it was |
255 // running. | 263 // running. |
256 void OnDetached(); | 264 void OnDetached(); |
257 | 265 |
258 // Called back from Registry when the worker instance sends message | 266 // Called back from Registry when the worker instance sends message |
259 // to the browser (i.e. EmbeddedWorker observers). | 267 // to the browser (i.e. EmbeddedWorker observers). |
260 // Returns false if the message is not handled. | 268 // Returns false if the message is not handled. |
261 bool OnMessageReceived(const IPC::Message& message); | 269 bool OnMessageReceived(const IPC::Message& message); |
262 | 270 |
263 // Called back from Registry when the worker instance reports the exception. | |
264 void OnReportException(const base::string16& error_message, | |
265 int line_number, | |
266 int column_number, | |
267 const GURL& source_url); | |
268 | |
269 // Called back from Registry when the worker instance reports to the console. | |
270 void OnReportConsoleMessage(int source_identifier, | |
271 int message_level, | |
272 const base::string16& message, | |
273 int line_number, | |
274 const GURL& source_url); | |
275 | |
276 // Resets all running state. After this function is called, |status_| is | 271 // Resets all running state. After this function is called, |status_| is |
277 // STOPPED. | 272 // STOPPED. |
278 void ReleaseProcess(); | 273 void ReleaseProcess(); |
279 | 274 |
280 // Called back from StartTask when the startup sequence failed. Calls | 275 // Called back from StartTask when the startup sequence failed. Calls |
281 // ReleaseProcess() and invokes |callback| with |status|. May destroy |this|. | 276 // ReleaseProcess() and invokes |callback| with |status|. May destroy |this|. |
282 void OnStartFailed(const StatusCallback& callback, | 277 void OnStartFailed(const StatusCallback& callback, |
283 ServiceWorkerStatusCode status); | 278 ServiceWorkerStatusCode status); |
284 | 279 |
285 // Returns the time elapsed since |step_time_| and updates |step_time_| | 280 // Returns the time elapsed since |step_time_| and updates |step_time_| |
(...skipping 10 matching lines...) Expand all Loading... | |
296 StartingPhase starting_phase_; | 291 StartingPhase starting_phase_; |
297 int restart_count_; | 292 int restart_count_; |
298 | 293 |
299 // Current running information. | 294 // Current running information. |
300 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; | 295 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; |
301 int thread_id_; | 296 int thread_id_; |
302 | 297 |
303 // |client_| is used to send messages to the renderer process. | 298 // |client_| is used to send messages to the renderer process. |
304 mojom::EmbeddedWorkerInstanceClientPtr client_; | 299 mojom::EmbeddedWorkerInstanceClientPtr client_; |
305 | 300 |
301 // Binding for EmbeddedWorkerInstanceHost, runs on IO thread. | |
302 mojo::AssociatedBinding<EmbeddedWorkerInstanceHost> instance_host_binding_; | |
303 | |
306 // TODO(shimazu): Remove this after EmbeddedWorkerStartParams is changed to | 304 // TODO(shimazu): Remove this after EmbeddedWorkerStartParams is changed to |
307 // a mojo struct. | 305 // a mojo struct. |
308 mojom::ServiceWorkerEventDispatcherRequest pending_dispatcher_request_; | 306 mojom::ServiceWorkerEventDispatcherRequest pending_dispatcher_request_; |
309 | 307 |
310 // Whether devtools is attached or not. | 308 // Whether devtools is attached or not. |
311 bool devtools_attached_; | 309 bool devtools_attached_; |
312 | 310 |
313 // True if the script load request accessed the network. If the script was | 311 // True if the script load request accessed the network. If the script was |
314 // served from HTTPCache or ServiceWorkerDatabase this value is false. | 312 // served from HTTPCache or ServiceWorkerDatabase this value is false. |
315 bool network_accessed_for_script_; | 313 bool network_accessed_for_script_; |
(...skipping 11 matching lines...) Expand all Loading... | |
327 base::TimeTicks step_time_; | 325 base::TimeTicks step_time_; |
328 | 326 |
329 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; | 327 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; |
330 | 328 |
331 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 329 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
332 }; | 330 }; |
333 | 331 |
334 } // namespace content | 332 } // namespace content |
335 | 333 |
336 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 334 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
OLD | NEW |