| 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 // Implements mojom::EmbeddedWorkerInstanceHost. |
| 223 // it is ready for inspection. | 225 // These functions all run on the IO thread. |
| 224 void OnReadyForInspection(); | 226 void OnReadyForInspection() override; |
| 225 | 227 void OnScriptLoaded() override; |
| 226 // Called back from Registry when the worker instance has ack'ed that | 228 // Notifies the corresponding provider host that the thread has started and is |
| 227 // it finished loading the script. | 229 // ready to receive messages. |
| 228 void OnScriptLoaded(); | 230 void OnThreadStarted(int thread_id, int provider_id) override; |
| 229 | 231 void OnScriptLoadFailed() override; |
| 230 // Called back from Registry when the worker instance has ack'ed that | 232 // Fires the callback passed to Start(). |
| 231 // it has started a worker thread. | 233 void OnScriptEvaluated(bool success) override; |
| 232 void OnThreadStarted(int thread_id); | 234 // Changes the internal worker status from STARTING to RUNNING. |
| 233 | 235 void OnStarted() override; |
| 234 // Called back from Registry when the worker instance has ack'ed that | 236 // Resets the embedded worker instance to the initial state. This will change |
| 235 // it failed to load the script. | 237 // the internal status from STARTING or RUNNING to STOPPED. |
| 236 void OnScriptLoadFailed(); | 238 void OnStopped() override; |
| 237 | 239 void OnReportException(const base::string16& error_message, |
| 238 // Called back from Registry when the worker instance has ack'ed that | 240 int line_number, |
| 239 // it finished evaluating the script. This is called before OnStarted. | 241 int column_number, |
| 240 void OnScriptEvaluated(bool success); | 242 const GURL& source_url) override; |
| 241 | 243 void OnReportConsoleMessage(int source_identifier, |
| 242 // Called back from Registry when the worker instance has ack'ed that its | 244 int message_level, |
| 243 // WorkerGlobalScope has actually started and evaluated the script. This is | 245 const base::string16& message, |
| 244 // called after OnScriptEvaluated. | 246 int line_number, |
| 245 // This will change the internal status from STARTING to RUNNING. | 247 const GURL& source_url) override; |
| 246 void OnStarted(); | |
| 247 | |
| 248 // Called back from Registry when the worker instance has ack'ed that | |
| 249 // its WorkerGlobalScope is actually stopped in the child process. | |
| 250 // This will change the internal status from STARTING or RUNNING to | |
| 251 // STOPPED. | |
| 252 void OnStopped(); | |
| 253 | 248 |
| 254 // Called when ServiceWorkerDispatcherHost for the worker died while it was | 249 // Called when ServiceWorkerDispatcherHost for the worker died while it was |
| 255 // running. | 250 // running. |
| 256 void OnDetached(); | 251 void OnDetached(); |
| 257 | 252 |
| 258 // Called back from Registry when the worker instance sends message | 253 // Called back from Registry when the worker instance sends message |
| 259 // to the browser (i.e. EmbeddedWorker observers). | 254 // to the browser (i.e. EmbeddedWorker observers). |
| 260 // Returns false if the message is not handled. | 255 // Returns false if the message is not handled. |
| 261 bool OnMessageReceived(const IPC::Message& message); | 256 bool OnMessageReceived(const IPC::Message& message); |
| 262 | 257 |
| 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 | 258 // Resets all running state. After this function is called, |status_| is |
| 277 // STOPPED. | 259 // STOPPED. |
| 278 void ReleaseProcess(); | 260 void ReleaseProcess(); |
| 279 | 261 |
| 280 // Called back from StartTask when the startup sequence failed. Calls | 262 // Called back from StartTask when the startup sequence failed. Calls |
| 281 // ReleaseProcess() and invokes |callback| with |status|. May destroy |this|. | 263 // ReleaseProcess() and invokes |callback| with |status|. May destroy |this|. |
| 282 void OnStartFailed(const StatusCallback& callback, | 264 void OnStartFailed(const StatusCallback& callback, |
| 283 ServiceWorkerStatusCode status); | 265 ServiceWorkerStatusCode status); |
| 284 | 266 |
| 285 // Returns the time elapsed since |step_time_| and updates |step_time_| | 267 // Returns the time elapsed since |step_time_| and updates |step_time_| |
| (...skipping 10 matching lines...) Expand all Loading... |
| 296 StartingPhase starting_phase_; | 278 StartingPhase starting_phase_; |
| 297 int restart_count_; | 279 int restart_count_; |
| 298 | 280 |
| 299 // Current running information. | 281 // Current running information. |
| 300 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; | 282 std::unique_ptr<EmbeddedWorkerInstance::WorkerProcessHandle> process_handle_; |
| 301 int thread_id_; | 283 int thread_id_; |
| 302 | 284 |
| 303 // |client_| is used to send messages to the renderer process. | 285 // |client_| is used to send messages to the renderer process. |
| 304 mojom::EmbeddedWorkerInstanceClientPtr client_; | 286 mojom::EmbeddedWorkerInstanceClientPtr client_; |
| 305 | 287 |
| 288 // Binding for EmbeddedWorkerInstanceHost, runs on IO thread. |
| 289 mojo::AssociatedBinding<EmbeddedWorkerInstanceHost> instance_host_binding_; |
| 290 |
| 306 // TODO(shimazu): Remove this after EmbeddedWorkerStartParams is changed to | 291 // TODO(shimazu): Remove this after EmbeddedWorkerStartParams is changed to |
| 307 // a mojo struct. | 292 // a mojo struct. |
| 308 mojom::ServiceWorkerEventDispatcherRequest pending_dispatcher_request_; | 293 mojom::ServiceWorkerEventDispatcherRequest pending_dispatcher_request_; |
| 309 | 294 |
| 310 // Whether devtools is attached or not. | 295 // Whether devtools is attached or not. |
| 311 bool devtools_attached_; | 296 bool devtools_attached_; |
| 312 | 297 |
| 313 // True if the script load request accessed the network. If the script was | 298 // True if the script load request accessed the network. If the script was |
| 314 // served from HTTPCache or ServiceWorkerDatabase this value is false. | 299 // served from HTTPCache or ServiceWorkerDatabase this value is false. |
| 315 bool network_accessed_for_script_; | 300 bool network_accessed_for_script_; |
| (...skipping 11 matching lines...) Expand all Loading... |
| 327 base::TimeTicks step_time_; | 312 base::TimeTicks step_time_; |
| 328 | 313 |
| 329 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; | 314 base::WeakPtrFactory<EmbeddedWorkerInstance> weak_factory_; |
| 330 | 315 |
| 331 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 316 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
| 332 }; | 317 }; |
| 333 | 318 |
| 334 } // namespace content | 319 } // namespace content |
| 335 | 320 |
| 336 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 321 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
| OLD | NEW |