Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(62)

Side by Side Diff: content/browser/service_worker/service_worker_process_manager.h

Issue 443593002: ServiceWorker: Move worker candidate process knowledge to ServiceWorkerProcessManager. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: RPH observer DCHECK and browser tests build fix Created 6 years, 4 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
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"
16 #include "content/public/browser/render_process_host_observer.h"
15 17
16 class GURL; 18 class GURL;
17 19
18 namespace content { 20 namespace content {
19 21
20 class BrowserContext; 22 class BrowserContext;
21 class SiteInstance; 23 class SiteInstance;
22 24
23 // Interacts with the UI thread to keep RenderProcessHosts alive while the 25 // Interacts with the UI thread to keep RenderProcessHosts alive while the
24 // ServiceWorker system is using them. Each instance of 26 // ServiceWorker system is using them. It also tracks candidate processes
25 // ServiceWorkerProcessManager is destroyed on the UI thread shortly after its 27 // for each scope. Each instance of ServiceWorkerProcessManager is destroyed
26 // ServiceWorkerContextWrapper is destroyed. 28 // on the UI thread shortly after its ServiceWorkerContextWrapper is destroyed.
27 class CONTENT_EXPORT ServiceWorkerProcessManager { 29 class CONTENT_EXPORT ServiceWorkerProcessManager
30 : public RenderProcessHostObserver {
28 public: 31 public:
29 // |*this| must be owned by a ServiceWorkerContextWrapper in a 32 // |*this| must be owned by a ServiceWorkerContextWrapper in a
30 // StoragePartition within |browser_context|. 33 // StoragePartition within |browser_context|.
31 explicit ServiceWorkerProcessManager(BrowserContext* browser_context); 34 explicit ServiceWorkerProcessManager(BrowserContext* browser_context);
32 35
33 // Shutdown must be called before the ProcessManager is destroyed. 36 // Shutdown must be called before the ProcessManager is destroyed.
34 ~ServiceWorkerProcessManager(); 37 virtual ~ServiceWorkerProcessManager();
35 38
36 // Synchronously prevents new processes from being allocated. 39 // Synchronously prevents new processes from being allocated.
37 // TODO(jyasskin): Drop references to RenderProcessHosts too. 40 // TODO(jyasskin): Drop references to RenderProcessHosts too.
38 void Shutdown(); 41 void Shutdown();
39 42
40 // Returns a reference to a running process suitable for starting the Service 43 // 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 44 // 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 45 // 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 // 46 //
46 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if 47 // Allocation can fail with SERVICE_WORKER_ERROR_START_WORKER_FAILED if
47 // RenderProcessHost::Init fails. 48 // RenderProcessHost::Init fails.
48 void AllocateWorkerProcess( 49 void AllocateWorkerProcess(
49 int embedded_worker_id, 50 int embedded_worker_id,
50 const std::vector<int>& process_ids, 51 const GURL& scope,
51 const GURL& script_url, 52 const GURL& script_url,
52 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& 53 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>&
53 callback); 54 callback);
54 55
55 // Drops a reference to a process that was running a Service Worker, and its 56 // Drops a reference to a process that was running a Service Worker, and its
56 // SiteInstance. This must match a call to AllocateWorkerProcess. 57 // SiteInstance. This must match a call to AllocateWorkerProcess.
57 void ReleaseWorkerProcess(int embedded_worker_id); 58 void ReleaseWorkerProcess(int embedded_worker_id);
58 59
59 // Sets a single process ID that will be used for all embedded workers. This 60 // 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 61 // 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 62 // 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 63 // making sure this is only called on the same thread as runs the UI message
63 // loop. 64 // loop.
64 void SetProcessIdForTest(int process_id) { 65 void SetProcessIdForTest(int process_id) {
65 process_id_for_test_ = process_id; 66 process_id_for_test_ = process_id;
66 } 67 }
67 68
69 // Adds/removes process reference for the |scope|, the process with highest
70 // score will be chosen to start worker. The pending processes will be added
Jeffrey Yasskin 2014/08/19 23:42:24 It's not clear what a "score" or "priority" is her
xiang 2014/08/29 07:49:40 Done.
71 // with lowest priority(0).
72 void AddScopePendingProcesses(const GURL& scope,
73 const std::vector<int>& pending_processes);
kinuko 2014/08/12 15:27:07 This method and where we call it feels a bit sketc
Jeffrey Yasskin 2014/08/19 23:42:24 +1 to removing "pending" processes, if possible.
xiang 2014/08/29 07:49:40 Done.
74 void AddScopeProcessReference(const GURL& scope, int process_id);
75 void RemoveScopeProcessReference(const GURL& scope, int process_id);
76
77 // Returns true if the |scope| has at least one process to run.
78 bool ScopeHasProcessToRun(const GURL& scope) const;
79
80 // RenderProcessHostObserver overrides:
81 virtual void RenderProcessHostDestroyed(RenderProcessHost* host) OVERRIDE;
82
68 private: 83 private:
84 FRIEND_TEST_ALL_PREFIXES(ServiceWorkerProcessManagerTest, SortProcess);
85
69 // Information about the process for an EmbeddedWorkerInstance. 86 // Information about the process for an EmbeddedWorkerInstance.
70 struct ProcessInfo { 87 struct ProcessInfo {
71 explicit ProcessInfo(const scoped_refptr<SiteInstance>& site_instance); 88 explicit ProcessInfo(const scoped_refptr<SiteInstance>& site_instance);
72 explicit ProcessInfo(int process_id); 89 explicit ProcessInfo(int process_id);
73 ~ProcessInfo(); 90 ~ProcessInfo();
74 91
75 // Stores the SiteInstance the Worker lives inside. This needs to outlive 92 // Stores the SiteInstance the Worker lives inside. This needs to outlive
76 // the instance's use of its RPH to uphold assumptions in the 93 // the instance's use of its RPH to uphold assumptions in the
77 // ContentBrowserClient interface. 94 // ContentBrowserClient interface.
78 scoped_refptr<SiteInstance> site_instance; 95 scoped_refptr<SiteInstance> site_instance;
79 96
80 // In case the process was allocated without using a SiteInstance, we need 97 // 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. 98 // 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 99 // TODO(jyasskin): Implement http://crbug.com/372045 or thread a frame_id in
83 // so all processes can be allocated with a SiteInstance. 100 // so all processes can be allocated with a SiteInstance.
84 int process_id; 101 int process_id;
85 }; 102 };
86 103
104 typedef std::map<int, int> ProcessRefMap;
Jeffrey Yasskin 2014/08/19 23:42:24 Always document what an "int" means.
xiang 2014/08/29 07:49:39 Done.
105 typedef std::map<const GURL, ProcessRefMap> ScopeProcessRefMap;
106
107 // Returns a process vector sorted by the reference count for the |scope|.
108 std::vector<int> SortProcessesForScope(const GURL& scope) const;
109
87 // These fields are only accessed on the UI thread. 110 // These fields are only accessed on the UI thread.
88 BrowserContext* browser_context_; 111 BrowserContext* browser_context_;
89 112
90 // Maps the ID of a running EmbeddedWorkerInstance to information about the 113 // 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 114 // process it's running inside. Since the Instances themselves live on the IO
92 // thread, this can be slightly out of date: 115 // thread, this can be slightly out of date:
93 // * instance_info_ is populated while an Instance is STARTING and before 116 // * instance_info_ is populated while an Instance is STARTING and before
94 // it's RUNNING. 117 // it's RUNNING.
95 // * instance_info_ is depopulated in a message sent as the Instance becomes 118 // * instance_info_ is depopulated in a message sent as the Instance becomes
96 // STOPPED. 119 // STOPPED.
97 std::map<int, ProcessInfo> instance_info_; 120 std::map<int, ProcessInfo> instance_info_;
98 121
99 // In unit tests, this will be returned as the process for all 122 // In unit tests, this will be returned as the process for all
100 // EmbeddedWorkerInstances. 123 // EmbeddedWorkerInstances.
101 int process_id_for_test_; 124 int process_id_for_test_;
102 125
126 // Candidate process info for each scope, should be accessed on the UI thread.
127 ScopeProcessRefMap scope_processes_;
128
103 // Used to double-check that we don't access *this after it's destroyed. 129 // Used to double-check that we don't access *this after it's destroyed.
104 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_; 130 base::WeakPtrFactory<ServiceWorkerProcessManager> weak_this_factory_;
105 const base::WeakPtr<ServiceWorkerProcessManager> weak_this_; 131 const base::WeakPtr<ServiceWorkerProcessManager> weak_this_;
106 }; 132 };
107 133
108 } // namespace content 134 } // namespace content
109 135
110 namespace base { 136 namespace base {
111 // Specialized to post the deletion to the UI thread. 137 // Specialized to post the deletion to the UI thread.
112 template <> 138 template <>
113 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> { 139 struct CONTENT_EXPORT DefaultDeleter<content::ServiceWorkerProcessManager> {
114 void operator()(content::ServiceWorkerProcessManager* ptr) const; 140 void operator()(content::ServiceWorkerProcessManager* ptr) const;
115 }; 141 };
116 } // namespace base 142 } // namespace base
117 143
118 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_ 144 #endif // CONTENT_BROWSER_SERVICE_WORKER_SERVICE_WORKER_PROCESS_MANAGER_H_
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698