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 <vector> | 9 #include <vector> |
9 | 10 |
10 #include "base/callback.h" | 11 #include "base/callback.h" |
11 #include "base/memory/scoped_ptr.h" | 12 #include "base/memory/scoped_ptr.h" |
12 #include "base/memory/weak_ptr.h" | 13 #include "base/memory/weak_ptr.h" |
13 #include "content/common/service_worker/service_worker_status_code.h" | 14 #include "content/common/service_worker/service_worker_status_code.h" |
14 | 15 |
15 class GURL; | 16 class GURL; |
16 | 17 |
17 namespace content { | 18 namespace content { |
18 | 19 |
19 class BrowserContext; | 20 class BrowserContext; |
20 class ServiceWorkerContextWrapper; | 21 class ServiceWorkerContextWrapper; |
22 class SiteInstance; | |
21 | 23 |
22 // Interacts with the UI thread to keep RenderProcessHosts alive while the | 24 // Interacts with the UI thread to keep RenderProcessHosts alive while the |
23 // ServiceWorker system is using them. Each instance of | 25 // ServiceWorker system is using them. Each instance of |
24 // ServiceWorkerProcessManager is destroyed on the UI thread shortly after its | 26 // ServiceWorkerProcessManager is destroyed on the UI thread shortly after its |
25 // ServiceWorkerContextCore is destroyed on the IO thread. | 27 // ServiceWorkerContextCore is destroyed on the IO thread. |
26 class CONTENT_EXPORT ServiceWorkerProcessManager { | 28 class CONTENT_EXPORT ServiceWorkerProcessManager { |
27 public: | 29 public: |
28 // |*this| must be owned by |context_wrapper|->context(). | 30 // |*this| must be owned by |context_wrapper|->context(). |
29 explicit ServiceWorkerProcessManager( | 31 explicit ServiceWorkerProcessManager( |
30 ServiceWorkerContextWrapper* context_wrapper); | 32 ServiceWorkerContextWrapper* context_wrapper); |
31 | 33 |
32 ~ServiceWorkerProcessManager(); | 34 ~ServiceWorkerProcessManager(); |
33 | 35 |
34 // Returns a reference to a running process suitable for starting the Service | 36 // Returns a reference to a running process suitable for starting the Service |
35 // Worker at |script_url|. Processes in |process_ids| will be checked in order | 37 // Worker at |script_url|. Processes in |process_ids| will be checked in order |
36 // for existence, and if none exist, then a new process will be created. Posts | 38 // for existence, and if none exist, then a new process will be created. Posts |
37 // |callback| to the IO thread to indicate whether creation succeeded and the | 39 // |callback| to the IO thread to indicate whether creation succeeded and the |
38 // process ID that has a new reference. | 40 // process ID that has a new reference. |
39 // | 41 // |
40 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if | 42 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if |
41 // RenderProcessHost::Init fails. | 43 // RenderProcessHost::Init fails. |
42 void AllocateWorkerProcess( | 44 void AllocateWorkerProcess( |
45 int embedded_worker_id, | |
43 const std::vector<int>& process_ids, | 46 const std::vector<int>& process_ids, |
44 const GURL& script_url, | 47 const GURL& script_url, |
45 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 48 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
46 callback) const; | 49 callback); |
47 | 50 |
48 // Drops a reference to a process that was running a Service Worker. This | 51 // Drops a reference to a process that was running a Service Worker, and its |
49 // must match a call to AllocateWorkerProcess. | 52 // SiteInstance. This must match a call to AllocateWorkerProcess. |
50 void ReleaseWorkerProcess(int process_id); | 53 void ReleaseWorkerProcess(int embedded_worker_id); |
dominicc (has gone to gerrit)
2014/05/20 02:01:18
The parallelism of Allocate/ReleaseWorkerProcess b
Jeffrey Yasskin
2014/05/20 05:20:10
I could rename them to ...For()?
| |
51 | 54 |
52 // |increment_for_test| and |decrement_for_test| define how to look up a | 55 // Sets a single process ID that will be used for all embedded workers. This |
53 // process by ID and increment or decrement its worker reference count. This | 56 // bypasses the work of creating a process and managing its worker refcount so |
54 // must be called before any reference to this object escapes to another | 57 // that unittests can run without a BrowserContext. The test is in charge of |
55 // thread, and is considered part of construction. | 58 // making sure this is only called on the same thread as runs the UI message |
56 void SetProcessRefcountOpsForTest( | 59 // loop. |
57 const base::Callback<bool(int)>& increment_for_test, | 60 void SetProcessIdForTest(int process_id) { |
58 const base::Callback<bool(int)>& decrement_for_test); | 61 process_id_for_test_ = process_id; |
62 } | |
59 | 63 |
60 private: | 64 private: |
61 bool IncrementWorkerRefcountByPid(int process_id) const; | 65 // Information about the process for an EmbeddedWorkerInstance. |
62 bool DecrementWorkerRefcountByPid(int process_id) const; | 66 struct ProcessInfo { |
67 explicit ProcessInfo(const scoped_refptr<SiteInstance>& site_instance); | |
68 explicit ProcessInfo(int process_id); | |
69 ~ProcessInfo(); | |
70 | |
71 // Stores the SiteInstance the Worker lives inside. This needs to outlive | |
72 // the instance's use of its RPH to uphold assumptions in the | |
73 // ContentBrowserClient interface. | |
74 scoped_refptr<SiteInstance> site_instance; | |
75 | |
76 // In case the process was allocated without using a SiteInstance, we need | |
77 // to store a process ID to decrement a worker reference on shutdown. | |
78 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in | |
79 // so all processes can be allocated with a SiteInstance. | |
80 int process_id; | |
81 }; | |
63 | 82 |
64 // These fields are only accessed on the UI thread after construction. | 83 // These fields are only accessed on the UI thread after construction. |
65 // The reference cycle through context_wrapper_ is broken in | 84 // The reference cycle through context_wrapper_ is broken in |
66 // ServiceWorkerContextWrapper::Shutdown(). | 85 // ServiceWorkerContextWrapper::Shutdown(). |
67 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; | 86 scoped_refptr<ServiceWorkerContextWrapper> context_wrapper_; |
68 base::Callback<bool(int)> increment_for_test_; | 87 |
69 base::Callback<bool(int)> decrement_for_test_; | 88 // Maps the ID of a running EmbeddedWorkerInstance to information about the |
89 // process it's running inside. Since the Instances themselves live on the IO | |
90 // thread, this can be slightly out of date: | |
91 // * instance_info_ is populated while an Instance is STARTING and before | |
92 // it's RUNNING. | |
93 // * instance_info_ is depopulated in a message sent as the Instance becomes | |
94 // STOPPED. | |
95 std::map<int, ProcessInfo> instance_info_; | |
96 | |
97 // In unit tests, this will be returned as the process for all | |
98 // EmbeddedWorkerInstances. | |
99 int process_id_for_test_; | |
70 | 100 |
71 // Used to double-check that we don't access *this after it's destroyed. | 101 // Used to double-check that we don't access *this after it's destroyed. |
72 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_; | 102 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_; |
73 base::WeakPtr<ServiceWorkerProcessManager> weak_this_; | 103 const base::WeakPtr<ServiceWorkerProcessManager> weak_this_; |
74 }; | 104 }; |
75 | 105 |
76 } // namespace content | 106 } // namespace content |
77 | 107 |
78 namespace base { | 108 namespace base { |
79 // Specialized to post the deletion to the UI thread. | 109 // Specialized to post the deletion to the UI thread. |
80 template <> | 110 template <> |
81 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> { | 111 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> { |
82 void operator()(content::ServiceWorkerProcessManager* ptr) const; | 112 void operator()(content::ServiceWorkerProcessManager* ptr) const; |
83 }; | 113 }; |
84 } // namespace base | 114 } // namespace base |
85 | 115 |
86 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ | 116 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ |
OLD | NEW |