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

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

Powered by Google App Engine
This is Rietveld 408576698