| OLD | NEW |
| 1 // Copyright 2014 The Chromium Authors. All rights reserved. | 1 // Copyright 2014 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_SERVICE_WORKER_PROCESS_MANAGER_H_ | 5 #ifndef CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ |
| 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ | 6 #define CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ |
| 7 | 7 |
| 8 #include <map> | 8 #include <map> |
| 9 #include <memory> | 9 #include <memory> |
| 10 #include <vector> | 10 #include <vector> |
| 11 | 11 |
| 12 #include "base/callback.h" | 12 #include "base/callback.h" |
| 13 #include "base/gtest_prod_util.h" | 13 #include "base/gtest_prod_util.h" |
| 14 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
| 15 #include "base/synchronization/lock.h" |
| 15 #include "content/common/service_worker/service_worker_status_code.h" | 16 #include "content/common/service_worker/service_worker_status_code.h" |
| 16 | 17 |
| 17 class GURL; | 18 class GURL; |
| 18 | 19 |
| 19 namespace content { | 20 namespace content { |
| 20 | 21 |
| 21 class BrowserContext; | 22 class BrowserContext; |
| 22 struct EmbeddedWorkerSettings; | 23 struct EmbeddedWorkerSettings; |
| 23 class SiteInstance; | 24 class SiteInstance; |
| 24 | 25 |
| 25 // Interacts with the UI thread to keep RenderProcessHosts alive while the | 26 // Interacts with the UI thread to keep RenderProcessHosts alive while the |
| 26 // ServiceWorker system is using them. It also tracks candidate processes | 27 // ServiceWorker system is using them. It also tracks candidate processes |
| 27 // for each pattern. Each instance of ServiceWorkerProcessManager is destroyed | 28 // for each pattern. Each instance of ServiceWorkerProcessManager is destroyed |
| 28 // on the UI thread shortly after its ServiceWorkerContextWrapper is destroyed. | 29 // on the UI thread shortly after its ServiceWorkerContextWrapper is destroyed. |
| 29 class CONTENT_EXPORT ServiceWorkerProcessManager { | 30 class CONTENT_EXPORT ServiceWorkerProcessManager { |
| 30 public: | 31 public: |
| 31 // |*this| must be owned by a ServiceWorkerContextWrapper in a | 32 // |*this| must be owned by a ServiceWorkerContextWrapper in a |
| 32 // StoragePartition within |browser_context|. | 33 // StoragePartition within |browser_context|. |
| 33 explicit ServiceWorkerProcessManager(BrowserContext* browser_context); | 34 explicit ServiceWorkerProcessManager(BrowserContext* browser_context); |
| 34 | 35 |
| 35 // Shutdown must be called before the ProcessManager is destroyed. | 36 // Shutdown must be called before the ProcessManager is destroyed. |
| 36 ~ServiceWorkerProcessManager(); | 37 ~ServiceWorkerProcessManager(); |
| 37 | 38 |
| 38 // Synchronously prevents new processes from being allocated | 39 // Synchronously prevents new processes from being allocated |
| 39 // and drops references to RenderProcessHosts. | 40 // and drops references to RenderProcessHosts. Called on the UI thread. |
| 40 void Shutdown(); | 41 void Shutdown(); |
| 41 | 42 |
| 43 // Returns true if Shutdown() has been called. May be called by any thread. |
| 44 bool IsShutdown(); |
| 45 |
| 42 // Returns a reference to a running process suitable for starting the Service | 46 // Returns a reference to a running process suitable for starting the Service |
| 43 // Worker at |script_url|. Posts |callback| to the IO thread to indicate | 47 // Worker at |script_url|. Posts |callback| to the IO thread to indicate |
| 44 // whether creation succeeded and the process ID that has a new reference. | 48 // whether creation succeeded and the process ID that has a new reference. |
| 45 // | 49 // |
| 46 // Allocation can fail with SERVICE_WORKER_PROCESS_NOT_FOUND if | 50 // Allocation can fail with SERVICE_WORKER_PROCESS_NOT_FOUND if |
| 47 // RenderProcessHost::Init fails. | 51 // RenderProcessHost::Init fails. |
| 48 void AllocateWorkerProcess( | 52 void AllocateWorkerProcess( |
| 49 int embedded_worker_id, | 53 int embedded_worker_id, |
| 50 const GURL& pattern, | 54 const GURL& pattern, |
| 51 const GURL& script_url, | 55 const GURL& script_url, |
| (...skipping 50 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 102 // ContentBrowserClient interface. | 106 // ContentBrowserClient interface. |
| 103 scoped_refptr<SiteInstance> site_instance; | 107 scoped_refptr<SiteInstance> site_instance; |
| 104 | 108 |
| 105 // In case the process was allocated without using a SiteInstance, we need | 109 // In case the process was allocated without using a SiteInstance, we need |
| 106 // to store a process ID to decrement a worker reference on shutdown. | 110 // to store a process ID to decrement a worker reference on shutdown. |
| 107 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in | 111 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in |
| 108 // so all processes can be allocated with a SiteInstance. | 112 // so all processes can be allocated with a SiteInstance. |
| 109 int process_id; | 113 int process_id; |
| 110 }; | 114 }; |
| 111 | 115 |
| 112 // Returns true if Shutdown() has been called. | |
| 113 bool IsShutdown() const { return !browser_context_; } | |
| 114 | |
| 115 // Maps the process ID to its reference count. | 116 // Maps the process ID to its reference count. |
| 116 typedef std::map<int, int> ProcessRefMap; | 117 typedef std::map<int, int> ProcessRefMap; |
| 117 | 118 |
| 118 // Maps registration scope pattern to ProcessRefMap. | 119 // Maps registration scope pattern to ProcessRefMap. |
| 119 typedef std::map<const GURL, ProcessRefMap> PatternProcessRefMap; | 120 typedef std::map<const GURL, ProcessRefMap> PatternProcessRefMap; |
| 120 | 121 |
| 121 // Returns a process vector sorted by the reference count for the |pattern|. | 122 // Returns a process vector sorted by the reference count for the |pattern|. |
| 122 std::vector<int> SortProcessesForPattern(const GURL& pattern) const; | 123 std::vector<int> SortProcessesForPattern(const GURL& pattern) const; |
| 123 | 124 |
| 124 // Returns the id of an available process for this pattern, or | 125 // Returns the id of an available process for this pattern, or |
| 125 // ChildProcessHost::kInvalidUniqueID if there is none. | 126 // ChildProcessHost::kInvalidUniqueID if there is none. |
| 126 int FindAvailableProcess(const GURL& pattern); | 127 int FindAvailableProcess(const GURL& pattern); |
| 127 | 128 |
| 128 // These fields are only accessed on the UI thread. | 129 // Guarded by |browser_context_lock_|. |
| 130 // Written only on the UI thread, so the UI thread doesn't need to acquire the |
| 131 // lock when reading. Can be read from other threads with the lock. |
| 129 BrowserContext* browser_context_; | 132 BrowserContext* browser_context_; |
| 130 | 133 |
| 134 // Protects |browser_context_|. |
| 135 base::Lock browser_context_lock_; |
| 136 |
| 137 // All fields below are only accessed on the UI thread. |
| 138 |
| 131 // Maps the ID of a running EmbeddedWorkerInstance to information about the | 139 // Maps the ID of a running EmbeddedWorkerInstance to information about the |
| 132 // process it's running inside. Since the Instances themselves live on the IO | 140 // process it's running inside. Since the Instances themselves live on the IO |
| 133 // thread, this can be slightly out of date: | 141 // thread, this can be slightly out of date: |
| 134 // * instance_info_ is populated while an Instance is STARTING and before | 142 // * instance_info_ is populated while an Instance is STARTING and before |
| 135 // it's RUNNING. | 143 // it's RUNNING. |
| 136 // * instance_info_ is depopulated in a message sent as the Instance becomes | 144 // * instance_info_ is depopulated in a message sent as the Instance becomes |
| 137 // STOPPED. | 145 // STOPPED. |
| 138 std::map<int, ProcessInfo> instance_info_; | 146 std::map<int, ProcessInfo> instance_info_; |
| 139 | 147 |
| 140 // In unit tests, this will be returned as the process for all | 148 // In unit tests, this will be returned as the process for all |
| (...skipping 14 matching lines...) Expand all Loading... |
| 155 | 163 |
| 156 namespace std { | 164 namespace std { |
| 157 // Specialized to post the deletion to the UI thread. | 165 // Specialized to post the deletion to the UI thread. |
| 158 template <> | 166 template <> |
| 159 struct CONTENT_EXPORT default_delete<content::ServiceWorkerProcessManager> { | 167 struct CONTENT_EXPORT default_delete<content::ServiceWorkerProcessManager> { |
| 160 void operator()(content::ServiceWorkerProcessManager* ptr) const; | 168 void operator()(content::ServiceWorkerProcessManager* ptr) const; |
| 161 }; | 169 }; |
| 162 } // namespace std | 170 } // namespace std |
| 163 | 171 |
| 164 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ | 172 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ |
| OLD | NEW |