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 <vector> | 9 #include <vector> |
10 | 10 |
11 #include "base/callback.h" | 11 #include "base/callback.h" |
| 12 #include "base/gtest_prod_util.h" |
12 #include "base/memory/scoped_ptr.h" | 13 #include "base/memory/scoped_ptr.h" |
13 #include "base/memory/weak_ptr.h" | 14 #include "base/memory/weak_ptr.h" |
14 #include "content/common/service_worker/service_worker_status_code.h" | 15 #include "content/common/service_worker/service_worker_status_code.h" |
15 | 16 |
| 17 #include "content/public/browser/render_process_host_observer.h" |
| 18 |
16 class GURL; | 19 class GURL; |
17 | 20 |
18 namespace content { | 21 namespace content { |
19 | 22 |
20 class BrowserContext; | 23 class BrowserContext; |
21 class SiteInstance; | 24 class SiteInstance; |
22 | 25 |
23 // Interacts with the UI thread to keep RenderProcessHosts alive while the | 26 // Interacts with the UI thread to keep RenderProcessHosts alive while the |
24 // ServiceWorker system is using them. Each instance of | 27 // ServiceWorker system is using them. It also tracks candidate processes |
25 // ServiceWorkerProcessManager is destroyed on the UI thread shortly after its | 28 // for each scope. Each instance of ServiceWorkerProcessManager is destroyed |
26 // ServiceWorkerContextWrapper is destroyed. | 29 // on the UI thread shortly after its ServiceWorkerContextWrapper is destroyed. |
27 class CONTENT_EXPORT ServiceWorkerProcessManager { | 30 class CONTENT_EXPORT ServiceWorkerProcessManager |
| 31 : public RenderProcessHostObserver { |
28 public: | 32 public: |
29 // |*this| must be owned by a ServiceWorkerContextWrapper in a | 33 // |*this| must be owned by a ServiceWorkerContextWrapper in a |
30 // StoragePartition within |browser_context|. | 34 // StoragePartition within |browser_context|. |
31 explicit ServiceWorkerProcessManager(BrowserContext* browser_context); | 35 explicit ServiceWorkerProcessManager(BrowserContext* browser_context); |
32 | 36 |
33 // Shutdown must be called before the ProcessManager is destroyed. | 37 // Shutdown must be called before the ProcessManager is destroyed. |
34 ~ServiceWorkerProcessManager(); | 38 virtual ~ServiceWorkerProcessManager(); |
35 | 39 |
36 // Synchronously prevents new processes from being allocated. | 40 // Synchronously prevents new processes from being allocated. |
37 // TODO(jyasskin): Drop references to RenderProcessHosts too. | 41 // TODO(jyasskin): Drop references to RenderProcessHosts too. |
38 void Shutdown(); | 42 void Shutdown(); |
39 | 43 |
40 // Returns a reference to a running process suitable for starting the Service | 44 // Returns a reference to a running process suitable for starting the Service |
41 // Worker at |script_url|. Processes in |process_ids| will be checked in order | 45 // Worker at |script_url|. Posts |callback| to the IO thread to indicate |
42 // for existence, and if none exist, then a new process will be created. Posts | 46 // whether creation succeeded and the process ID that has a new reference. |
43 // |callback| to the IO thread to indicate whether creation succeeded and the | |
44 // process ID that has a new reference. | |
45 // | 47 // |
46 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if | 48 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if |
47 // RenderProcessHost::Init fails. | 49 // RenderProcessHost::Init fails. |
48 void AllocateWorkerProcess( | 50 void AllocateWorkerProcess( |
49 int embedded_worker_id, | 51 int embedded_worker_id, |
50 const std::vector<int>& process_ids, | 52 const GURL& scope, |
51 const GURL& script_url, | 53 const GURL& script_url, |
52 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 54 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
53 callback); | 55 callback); |
54 | 56 |
55 // Drops a reference to a process that was running a Service Worker, and its | 57 // Drops a reference to a process that was running a Service Worker, and its |
56 // SiteInstance. This must match a call to AllocateWorkerProcess. | 58 // SiteInstance. This must match a call to AllocateWorkerProcess. |
57 void ReleaseWorkerProcess(int embedded_worker_id); | 59 void ReleaseWorkerProcess(int embedded_worker_id); |
58 | 60 |
59 // Sets a single process ID that will be used for all embedded workers. This | 61 // Sets a single process ID that will be used for all embedded workers. This |
60 // bypasses the work of creating a process and managing its worker refcount so | 62 // bypasses the work of creating a process and managing its worker refcount so |
61 // that unittests can run without a BrowserContext. The test is in charge of | 63 // that unittests can run without a BrowserContext. The test is in charge of |
62 // making sure this is only called on the same thread as runs the UI message | 64 // making sure this is only called on the same thread as runs the UI message |
63 // loop. | 65 // loop. |
64 void SetProcessIdForTest(int process_id) { | 66 void SetProcessIdForTest(int process_id) { |
65 process_id_for_test_ = process_id; | 67 process_id_for_test_ = process_id; |
66 } | 68 } |
67 | 69 |
| 70 // Adds/removes process reference for the |scope|, the process with highest |
| 71 // score will be chosen to start worker. The pending processes will be added |
| 72 // with lowest priority(0). |
| 73 void AddScopePendingProcesses(const GURL& scope, |
| 74 const std::vector<int>& pending_processes); |
| 75 void AddScopeProcessReference(const GURL& scope, int process_id); |
| 76 void RemoveScopeProcessReference(const GURL& scope, int process_id); |
| 77 |
| 78 // Returns true if the |scope| has at least one process to run. |
| 79 bool ScopeHasProcessToRun(const GURL& scope) const; |
| 80 |
| 81 // RenderProcessHostObserver overrides: |
| 82 virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE; |
| 83 |
68 private: | 84 private: |
| 85 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProcessManagerTest, SortProcess); |
| 86 |
69 // Information about the process for an EmbeddedWorkerInstance. | 87 // Information about the process for an EmbeddedWorkerInstance. |
70 struct ProcessInfo { | 88 struct ProcessInfo { |
71 explicit ProcessInfo(const scoped_refptr<SiteInstance>& site_instance); | 89 explicit ProcessInfo(const scoped_refptr<SiteInstance>& site_instance); |
72 explicit ProcessInfo(int process_id); | 90 explicit ProcessInfo(int process_id); |
73 ~ProcessInfo(); | 91 ~ProcessInfo(); |
74 | 92 |
75 // Stores the SiteInstance the Worker lives inside. This needs to outlive | 93 // Stores the SiteInstance the Worker lives inside. This needs to outlive |
76 // the instance's use of its RPH to uphold assumptions in the | 94 // the instance's use of its RPH to uphold assumptions in the |
77 // ContentBrowserClient interface. | 95 // ContentBrowserClient interface. |
78 scoped_refptr<SiteInstance> site_instance; | 96 scoped_refptr<SiteInstance> site_instance; |
79 | 97 |
80 // In case the process was allocated without using a SiteInstance, we need | 98 // In case the process was allocated without using a SiteInstance, we need |
81 // to store a process ID to decrement a worker reference on shutdown. | 99 // to store a process ID to decrement a worker reference on shutdown. |
82 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in | 100 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in |
83 // so all processes can be allocated with a SiteInstance. | 101 // so all processes can be allocated with a SiteInstance. |
84 int process_id; | 102 int process_id; |
85 }; | 103 }; |
86 | 104 |
| 105 typedef std::map<int, int> ProcessRefMap; |
| 106 typedef std::map<const GURL, ProcessRefMap> ScopeProcessRefMap; |
| 107 |
| 108 // Returns a process vector sorted by the reference count for the |scope|. |
| 109 std::vector<int> SortProcessesForScope(const GURL& scope) const; |
| 110 |
87 // These fields are only accessed on the UI thread. | 111 // These fields are only accessed on the UI thread. |
88 BrowserContext* browser_context_; | 112 BrowserContext* browser_context_; |
89 | 113 |
90 // Maps the ID of a running EmbeddedWorkerInstance to information about the | 114 // Maps the ID of a running EmbeddedWorkerInstance to information about the |
91 // process it's running inside. Since the Instances themselves live on the IO | 115 // process it's running inside. Since the Instances themselves live on the IO |
92 // thread, this can be slightly out of date: | 116 // thread, this can be slightly out of date: |
93 // * instance_info_ is populated while an Instance is STARTING and before | 117 // * instance_info_ is populated while an Instance is STARTING and before |
94 // it's RUNNING. | 118 // it's RUNNING. |
95 // * instance_info_ is depopulated in a message sent as the Instance becomes | 119 // * instance_info_ is depopulated in a message sent as the Instance becomes |
96 // STOPPED. | 120 // STOPPED. |
97 std::map<int, ProcessInfo> instance_info_; | 121 std::map<int, ProcessInfo> instance_info_; |
98 | 122 |
99 // In unit tests, this will be returned as the process for all | 123 // In unit tests, this will be returned as the process for all |
100 // EmbeddedWorkerInstances. | 124 // EmbeddedWorkerInstances. |
101 int process_id_for_test_; | 125 int process_id_for_test_; |
102 | 126 |
| 127 ScopeProcessRefMap scope_processes_; |
| 128 void DumpProcessReferences(); |
| 129 |
103 // Used to double-check that we don't access *this after it's destroyed. | 130 // Used to double-check that we don't access *this after it's destroyed. |
104 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_; | 131 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_; |
105 const base::WeakPtr<ServiceWorkerProcessManager> weak_this_; | 132 const base::WeakPtr<ServiceWorkerProcessManager> weak_this_; |
106 }; | 133 }; |
107 | 134 |
108 } // namespace content | 135 } // namespace content |
109 | 136 |
110 namespace base { | 137 namespace base { |
111 // Specialized to post the deletion to the UI thread. | 138 // Specialized to post the deletion to the UI thread. |
112 template <> | 139 template <> |
113 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> { | 140 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> { |
114 void operator()(content::ServiceWorkerProcessManager* ptr) const; | 141 void operator()(content::ServiceWorkerProcessManager* ptr) const; |
115 }; | 142 }; |
116 } // namespace base | 143 } // namespace base |
117 | 144 |
118 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ | 145 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ |
OLD | NEW |