| 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 <map> | 8 #include <map> |
| 9 | 9 |
| 10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
| 11 #include "base/callback_forward.h" | 11 #include "base/callback_forward.h" |
| 12 #include "base/gtest_prod_util.h" | 12 #include "base/gtest_prod_util.h" |
| 13 #include "base/logging.h" | 13 #include "base/logging.h" |
| 14 #include "base/memory/ref_counted.h" | 14 #include "base/memory/ref_counted.h" |
| 15 #include "base/observer_list.h" |
| 15 #include "content/common/content_export.h" | 16 #include "content/common/content_export.h" |
| 16 | 17 |
| 17 class GURL; | 18 class GURL; |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 class EmbeddedWorkerRegistry; | 22 class EmbeddedWorkerRegistry; |
| 22 struct ServiceWorkerFetchRequest; | 23 struct ServiceWorkerFetchRequest; |
| 23 | 24 |
| 24 // This gives an interface to control one EmbeddedWorker instance, which | 25 // This gives an interface to control one EmbeddedWorker instance, which |
| 25 // may be 'in-waiting' or running in one of the child processes added by | 26 // may be 'in-waiting' or running in one of the child processes added by |
| 26 // AddProcessReference(). | 27 // AddProcessReference(). |
| 27 class CONTENT_EXPORT EmbeddedWorkerInstance { | 28 class CONTENT_EXPORT EmbeddedWorkerInstance { |
| 28 public: | 29 public: |
| 29 enum Status { | 30 enum Status { |
| 30 STOPPED, | 31 STOPPED, |
| 31 STARTING, | 32 STARTING, |
| 32 RUNNING, | 33 RUNNING, |
| 33 STOPPING, | 34 STOPPING, |
| 34 }; | 35 }; |
| 35 | 36 |
| 37 class Observer { |
| 38 public: |
| 39 virtual ~Observer() {} |
| 40 virtual void OnStarted() = 0; |
| 41 virtual void OnStopped() = 0; |
| 42 }; |
| 43 |
| 36 ~EmbeddedWorkerInstance(); | 44 ~EmbeddedWorkerInstance(); |
| 37 | 45 |
| 38 // Starts the worker. It is invalid to call this when the worker is | 46 // Starts the worker. It is invalid to call this when the worker is |
| 39 // not in STOPPED status. | 47 // not in STOPPED status. |
| 40 // This returns false if starting a worker fails immediately, e.g. when | 48 // This returns false if starting a worker fails immediately, e.g. when |
| 41 // IPC couldn't be sent to the worker or no process was available. | 49 // IPC couldn't be sent to the worker or no process was available. |
| 42 bool Start(int64 service_worker_version_id, | 50 bool Start(int64 service_worker_version_id, |
| 43 const GURL& script_url); | 51 const GURL& script_url); |
| 44 | 52 |
| 45 // Stops the worker. It is invalid to call this when the worker is | 53 // Stops the worker. It is invalid to call this when the worker is |
| (...skipping 10 matching lines...) Expand all Loading... |
| 56 // Add or remove |process_id| to the internal process set where this | 64 // Add or remove |process_id| to the internal process set where this |
| 57 // worker can be started. | 65 // worker can be started. |
| 58 void AddProcessReference(int process_id); | 66 void AddProcessReference(int process_id); |
| 59 void ReleaseProcessReference(int process_id); | 67 void ReleaseProcessReference(int process_id); |
| 60 | 68 |
| 61 int embedded_worker_id() const { return embedded_worker_id_; } | 69 int embedded_worker_id() const { return embedded_worker_id_; } |
| 62 Status status() const { return status_; } | 70 Status status() const { return status_; } |
| 63 int process_id() const { return process_id_; } | 71 int process_id() const { return process_id_; } |
| 64 int thread_id() const { return thread_id_; } | 72 int thread_id() const { return thread_id_; } |
| 65 | 73 |
| 74 void AddObserver(Observer* observer); |
| 75 void RemoveObserver(Observer* observer); |
| 76 |
| 66 private: | 77 private: |
| 67 friend class EmbeddedWorkerRegistry; | 78 friend class EmbeddedWorkerRegistry; |
| 68 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); | 79 FRIEND_TEST_ALL_PREFIXES(EmbeddedWorkerInstanceTest, StartAndStop); |
| 69 | 80 |
| 70 typedef std::map<int, int> ProcessRefMap; | 81 typedef std::map<int, int> ProcessRefMap; |
| 71 | 82 |
| 72 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). | 83 // Constructor is called via EmbeddedWorkerRegistry::CreateWorker(). |
| 73 // This instance holds a ref of |registry|. | 84 // This instance holds a ref of |registry|. |
| 74 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, | 85 EmbeddedWorkerInstance(EmbeddedWorkerRegistry* registry, |
| 75 int embedded_worker_id); | 86 int embedded_worker_id); |
| (...skipping 16 matching lines...) Expand all Loading... |
| 92 | 103 |
| 93 scoped_refptr<EmbeddedWorkerRegistry> registry_; | 104 scoped_refptr<EmbeddedWorkerRegistry> registry_; |
| 94 const int embedded_worker_id_; | 105 const int embedded_worker_id_; |
| 95 Status status_; | 106 Status status_; |
| 96 | 107 |
| 97 // Current running information. -1 indicates the worker is not running. | 108 // Current running information. -1 indicates the worker is not running. |
| 98 int process_id_; | 109 int process_id_; |
| 99 int thread_id_; | 110 int thread_id_; |
| 100 | 111 |
| 101 ProcessRefMap process_refs_; | 112 ProcessRefMap process_refs_; |
| 113 ObserverList<Observer> observer_list_; |
| 102 | 114 |
| 103 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); | 115 DISALLOW_COPY_AND_ASSIGN(EmbeddedWorkerInstance); |
| 104 }; | 116 }; |
| 105 | 117 |
| 106 } // namespace content | 118 } // namespace content |
| 107 | 119 |
| 108 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ | 120 #endif // CONTENT_BROWSER_SERVICE_WORKER_EMBEDDED_WORKER_INSTANCE_H_ |
| OLD | NEW |