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 #include "content/browser/service_worker/service_worker_process_manager.h" | 5 #include "content/browser/service_worker/service_worker_process_manager.h" |
6 | 6 |
7 #include "content/browser/renderer_host/render_process_host_impl.h" | 7 #include "content/browser/renderer_host/render_process_host_impl.h" |
8 #include "content/browser/service_worker/service_worker_context_wrapper.h" | 8 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
9 #include "content/public/browser/browser_thread.h" | 9 #include "content/public/browser/browser_thread.h" |
10 #include "content/public/browser/site_instance.h" | 10 #include "content/public/browser/site_instance.h" |
11 #include "url/gurl.h" | 11 #include "url/gurl.h" |
12 | 12 |
13 namespace content { | 13 namespace content { |
14 | 14 |
| 15 namespace { |
| 16 |
| 17 // Functor to sort by the .second element of a struct. |
| 18 struct SecondGreater { |
| 19 template <typename Value> |
| 20 bool operator()(const Value& lhs, const Value& rhs) { |
| 21 return lhs.second > rhs.second; |
| 22 } |
| 23 }; |
| 24 |
| 25 } // namespace |
| 26 |
15 static bool IncrementWorkerRefCountByPid(int process_id) { | 27 static bool IncrementWorkerRefCountByPid(int process_id) { |
16 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); | 28 RenderProcessHost* rph = RenderProcessHost::FromID(process_id); |
17 if (!rph || rph->FastShutdownStarted()) | 29 if (!rph || rph->FastShutdownStarted()) |
18 return false; | 30 return false; |
19 | 31 |
20 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); | 32 static_cast<RenderProcessHostImpl*>(rph)->IncrementWorkerRefCount(); |
21 return true; | 33 return true; |
22 } | 34 } |
23 | 35 |
24 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( | 36 ServiceWorkerProcessManager::ProcessInfo::ProcessInfo( |
(...skipping 29 matching lines...) Expand all Loading... |
54 for (std::map<int, ProcessInfo>::const_iterator it = instance_info_.begin(); | 66 for (std::map<int, ProcessInfo>::const_iterator it = instance_info_.begin(); |
55 it != instance_info_.end(); | 67 it != instance_info_.end(); |
56 ++it) { | 68 ++it) { |
57 RenderProcessHost* rph = RenderProcessHost::FromID(it->second.process_id); | 69 RenderProcessHost* rph = RenderProcessHost::FromID(it->second.process_id); |
58 DCHECK(rph); | 70 DCHECK(rph); |
59 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | 71 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); |
60 } | 72 } |
61 instance_info_.clear(); | 73 instance_info_.clear(); |
62 } | 74 } |
63 | 75 |
| 76 void ServiceWorkerProcessManager::AddProcessReferenceToPattern( |
| 77 const GURL& pattern, int process_id) { |
| 78 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 79 BrowserThread::PostTask( |
| 80 BrowserThread::UI, |
| 81 FROM_HERE, |
| 82 base::Bind(&ServiceWorkerProcessManager::AddProcessReferenceToPattern, |
| 83 weak_this_, |
| 84 pattern, |
| 85 process_id)); |
| 86 return; |
| 87 } |
| 88 |
| 89 ProcessRefMap& process_refs = pattern_processes_[pattern]; |
| 90 ++process_refs[process_id]; |
| 91 } |
| 92 |
| 93 void ServiceWorkerProcessManager::RemoveProcessReferenceFromPattern( |
| 94 const GURL& pattern, int process_id) { |
| 95 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
| 96 BrowserThread::PostTask( |
| 97 BrowserThread::UI, |
| 98 FROM_HERE, |
| 99 base::Bind( |
| 100 &ServiceWorkerProcessManager::RemoveProcessReferenceFromPattern, |
| 101 weak_this_, |
| 102 pattern, |
| 103 process_id)); |
| 104 return; |
| 105 } |
| 106 |
| 107 PatternProcessRefMap::iterator it = pattern_processes_.find(pattern); |
| 108 if (it == pattern_processes_.end()) { |
| 109 NOTREACHED() << "process refrences not found for pattern: " << pattern; |
| 110 return; |
| 111 } |
| 112 ProcessRefMap& process_refs = it->second; |
| 113 ProcessRefMap::iterator found = process_refs.find(process_id); |
| 114 if (found == process_refs.end()) { |
| 115 NOTREACHED() << "Releasing unknown process ref " << process_id; |
| 116 return; |
| 117 } |
| 118 if (--found->second == 0) { |
| 119 process_refs.erase(found); |
| 120 if (process_refs.empty()) |
| 121 pattern_processes_.erase(it); |
| 122 } |
| 123 } |
| 124 |
| 125 bool ServiceWorkerProcessManager::PatternHasProcessToRun( |
| 126 const GURL& pattern) const { |
| 127 PatternProcessRefMap::const_iterator it = pattern_processes_.find(pattern); |
| 128 if (it == pattern_processes_.end()) |
| 129 return false; |
| 130 return !it->second.empty(); |
| 131 } |
| 132 |
64 void ServiceWorkerProcessManager::AllocateWorkerProcess( | 133 void ServiceWorkerProcessManager::AllocateWorkerProcess( |
65 int embedded_worker_id, | 134 int embedded_worker_id, |
66 const std::vector<int>& process_ids, | 135 const GURL& pattern, |
67 const GURL& script_url, | 136 const GURL& script_url, |
68 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& | 137 const base::Callback<void(ServiceWorkerStatusCode, int process_id)>& |
69 callback) { | 138 callback) { |
70 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { | 139 if (!BrowserThread::CurrentlyOn(BrowserThread::UI)) { |
71 BrowserThread::PostTask( | 140 BrowserThread::PostTask( |
72 BrowserThread::UI, | 141 BrowserThread::UI, |
73 FROM_HERE, | 142 FROM_HERE, |
74 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, | 143 base::Bind(&ServiceWorkerProcessManager::AllocateWorkerProcess, |
75 weak_this_, | 144 weak_this_, |
76 embedded_worker_id, | 145 embedded_worker_id, |
77 process_ids, | 146 pattern, |
78 script_url, | 147 script_url, |
79 callback)); | 148 callback)); |
80 return; | 149 return; |
81 } | 150 } |
82 | 151 |
83 if (process_id_for_test_ != -1) { | 152 if (process_id_for_test_ != -1) { |
84 // Let tests specify the returned process ID. Note: We may need to be able | 153 // Let tests specify the returned process ID. Note: We may need to be able |
85 // to specify the error code too. | 154 // to specify the error code too. |
86 BrowserThread::PostTask( | 155 BrowserThread::PostTask( |
87 BrowserThread::IO, | 156 BrowserThread::IO, |
88 FROM_HERE, | 157 FROM_HERE, |
89 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); | 158 base::Bind(callback, SERVICE_WORKER_OK, process_id_for_test_)); |
90 return; | 159 return; |
91 } | 160 } |
92 | 161 |
93 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) | 162 DCHECK(!ContainsKey(instance_info_, embedded_worker_id)) |
94 << embedded_worker_id << " already has a process allocated"; | 163 << embedded_worker_id << " already has a process allocated"; |
95 | 164 |
96 for (std::vector<int>::const_iterator it = process_ids.begin(); | 165 std::vector<int> sorted_candidates = SortProcessesForPattern(pattern); |
97 it != process_ids.end(); | 166 for (std::vector<int>::const_iterator it = sorted_candidates.begin(); |
| 167 it != sorted_candidates.end(); |
98 ++it) { | 168 ++it) { |
99 if (IncrementWorkerRefCountByPid(*it)) { | 169 if (!IncrementWorkerRefCountByPid(*it)) |
100 instance_info_.insert( | 170 continue; |
101 std::make_pair(embedded_worker_id, ProcessInfo(*it))); | 171 instance_info_.insert( |
102 BrowserThread::PostTask(BrowserThread::IO, | 172 std::make_pair(embedded_worker_id, ProcessInfo(*it))); |
103 FROM_HERE, | 173 BrowserThread::PostTask(BrowserThread::IO, |
104 base::Bind(callback, SERVICE_WORKER_OK, *it)); | 174 FROM_HERE, |
105 return; | 175 base::Bind(callback, SERVICE_WORKER_OK, *it)); |
106 } | 176 return; |
107 } | 177 } |
108 | 178 |
109 if (!browser_context_) { | 179 if (!browser_context_) { |
110 // Shutdown has started. | 180 // Shutdown has started. |
111 BrowserThread::PostTask( | 181 BrowserThread::PostTask( |
112 BrowserThread::IO, | 182 BrowserThread::IO, |
113 FROM_HERE, | 183 FROM_HERE, |
114 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); | 184 base::Bind(callback, SERVICE_WORKER_ERROR_START_WORKER_FAILED, -1)); |
115 return; | 185 return; |
116 } | 186 } |
(...skipping 55 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
172 } else { | 242 } else { |
173 rph = RenderProcessHost::FromID(info->second.process_id); | 243 rph = RenderProcessHost::FromID(info->second.process_id); |
174 DCHECK(rph) | 244 DCHECK(rph) |
175 << "Process " << info->second.process_id | 245 << "Process " << info->second.process_id |
176 << " was destroyed unexpectedly. Did we actually hold a reference?"; | 246 << " was destroyed unexpectedly. Did we actually hold a reference?"; |
177 } | 247 } |
178 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); | 248 static_cast<RenderProcessHostImpl*>(rph)->DecrementWorkerRefCount(); |
179 instance_info_.erase(info); | 249 instance_info_.erase(info); |
180 } | 250 } |
181 | 251 |
| 252 std::vector<int> ServiceWorkerProcessManager::SortProcessesForPattern( |
| 253 const GURL& pattern) const { |
| 254 PatternProcessRefMap::const_iterator it = pattern_processes_.find(pattern); |
| 255 if (it == pattern_processes_.end()) |
| 256 return std::vector<int>(); |
| 257 |
| 258 std::vector<std::pair<int, int> > counted( |
| 259 it->second.begin(), it->second.end()); |
| 260 std::sort(counted.begin(), counted.end(), SecondGreater()); |
| 261 |
| 262 std::vector<int> result(counted.size()); |
| 263 for (size_t i = 0; i < counted.size(); ++i) |
| 264 result[i] = counted[i].first; |
| 265 return result; |
| 266 } |
| 267 |
182 } // namespace content | 268 } // namespace content |
183 | 269 |
184 namespace base { | 270 namespace base { |
185 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the | 271 // Destroying ServiceWorkerProcessManagers only on the UI thread allows the |
186 // member WeakPtr to safely guard the object's lifetime when used on that | 272 // member WeakPtr to safely guard the object's lifetime when used on that |
187 // thread. | 273 // thread. |
188 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( | 274 void DefaultDeleter<content::ServiceWorkerProcessManager>::operator()( |
189 content::ServiceWorkerProcessManager* ptr) const { | 275 content::ServiceWorkerProcessManager* ptr) const { |
190 content::BrowserThread::DeleteSoon( | 276 content::BrowserThread::DeleteSoon( |
191 content::BrowserThread::UI, FROM_HERE, ptr); | 277 content::BrowserThread::UI, FROM_HERE, ptr); |
192 } | 278 } |
193 } // namespace base | 279 } // namespace base |
OLD | NEW |