OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 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_context_wrapper.h" | 5 #include "content/browser/service_worker/service_worker_context_wrapper.h" |
6 | 6 |
7 #include <map> | 7 #include <map> |
8 | 8 |
9 #include "base/barrier_closure.h" | 9 #include "base/barrier_closure.h" |
| 10 #include "base/callback_helpers.h" |
10 #include "base/files/file_path.h" | 11 #include "base/files/file_path.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/threading/sequenced_worker_pool.h" | 13 #include "base/threading/sequenced_worker_pool.h" |
13 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 14 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
14 #include "content/browser/service_worker/service_worker_context_core.h" | 15 #include "content/browser/service_worker/service_worker_context_core.h" |
15 #include "content/browser/service_worker/service_worker_context_observer.h" | 16 #include "content/browser/service_worker/service_worker_context_observer.h" |
16 #include "content/browser/service_worker/service_worker_process_manager.h" | 17 #include "content/browser/service_worker/service_worker_process_manager.h" |
17 #include "content/browser/service_worker/service_worker_quota_client.h" | 18 #include "content/browser/service_worker/service_worker_quota_client.h" |
18 #include "content/public/browser/browser_context.h" | 19 #include "content/public/browser/browser_context.h" |
19 #include "content/public/browser/browser_thread.h" | 20 #include "content/public/browser/browser_thread.h" |
(...skipping 133 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
153 context()->UnregisterServiceWorker( | 154 context()->UnregisterServiceWorker( |
154 pattern, | 155 pattern, |
155 base::Bind(&FinishUnregistrationOnIO, continuation)); | 156 base::Bind(&FinishUnregistrationOnIO, continuation)); |
156 } | 157 } |
157 | 158 |
158 void ServiceWorkerContextWrapper::Terminate() { | 159 void ServiceWorkerContextWrapper::Terminate() { |
159 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 160 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
160 process_manager_->Shutdown(); | 161 process_manager_->Shutdown(); |
161 } | 162 } |
162 | 163 |
| 164 void ServiceWorkerContextWrapper::CombineUsageInfo( |
| 165 std::vector<ServiceWorkerUsageInfo>* usage_info, |
| 166 std::map<GURL, int64>* usage_map, |
| 167 const GetUsageInfoCallback& callback) { |
| 168 DCHECK(usage_info); |
| 169 DCHECK(usage_map); |
| 170 for (auto& info : *usage_info) { |
| 171 auto it = usage_map->find(info.origin); |
| 172 info.total_size_bytes = it != usage_map->end() ? it->second : 0; |
| 173 } |
| 174 // We own usage_info, so as soon as we go out of scope it will be destroyed. |
| 175 // Since we are explicitly calling Run here, we should be good, as the |
| 176 // usage callback will be fully run before this callback is destroyed. |
| 177 callback.Run(*usage_info); |
| 178 } |
| 179 |
163 void ServiceWorkerContextWrapper::GetAllOriginsInfo( | 180 void ServiceWorkerContextWrapper::GetAllOriginsInfo( |
164 const GetUsageInfoCallback& callback) { | 181 const GetUsageInfoCallback& callback) { |
165 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 182 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
166 if (!context_core_.get()) { | 183 if (!context_core_.get()) { |
167 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; | 184 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; |
168 BrowserThread::PostTask( | 185 BrowserThread::PostTask( |
169 BrowserThread::IO, | 186 BrowserThread::IO, |
170 FROM_HERE, | 187 FROM_HERE, |
171 base::Bind(callback, std::vector<ServiceWorkerUsageInfo>())); | 188 base::Bind(callback, std::vector<ServiceWorkerUsageInfo>())); |
172 return; | 189 return; |
173 } | 190 } |
| 191 auto* info_out = new std::vector<ServiceWorkerUsageInfo>(); |
| 192 auto* usage_map_out = new std::map<GURL, int64>(); |
| 193 base::Closure barrier = base::BarrierClosure( |
| 194 2, |
| 195 base::Bind(&ServiceWorkerContextWrapper::CombineUsageInfo, |
| 196 this, |
| 197 base::Owned(info_out), |
| 198 base::Owned(usage_map_out), |
| 199 callback)); |
174 context()->storage()->GetAllRegistrations(base::Bind( | 200 context()->storage()->GetAllRegistrations(base::Bind( |
175 &ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins, | 201 &ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins, |
176 this, | 202 this, |
177 callback)); | 203 barrier, |
| 204 base::Unretained(info_out))); |
| 205 context()->storage()->GetAllUsageByOrigin( |
| 206 base::Bind(&ServiceWorkerContextWrapper::DidGetUsageForAllOrigins, |
| 207 this, |
| 208 barrier, |
| 209 base::Unretained(usage_map_out))); |
178 } | 210 } |
179 | 211 |
180 void ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins( | 212 void ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins( |
181 const GetUsageInfoCallback& callback, | 213 const base::Closure& closure, |
| 214 std::vector<ServiceWorkerUsageInfo>* registrations_out, |
182 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | 215 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { |
183 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 216 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 217 DCHECK(registrations_out); |
| 218 base::ScopedClosureRunner closureRunner(closure); |
184 std::vector<ServiceWorkerUsageInfo> usage_infos; | 219 std::vector<ServiceWorkerUsageInfo> usage_infos; |
185 | 220 |
186 std::map<GURL, ServiceWorkerUsageInfo> origins; | 221 std::map<GURL, ServiceWorkerUsageInfo> origins; |
187 for (std::vector<ServiceWorkerRegistrationInfo>::const_iterator it = | 222 for (std::vector<ServiceWorkerRegistrationInfo>::const_iterator it = |
188 registrations.begin(); | 223 registrations.begin(); |
189 it != registrations.end(); | 224 it != registrations.end(); |
190 ++it) { | 225 ++it) { |
191 const ServiceWorkerRegistrationInfo& registration_info = *it; | 226 const ServiceWorkerRegistrationInfo& registration_info = *it; |
192 GURL origin = registration_info.pattern.GetOrigin(); | 227 GURL origin = registration_info.pattern.GetOrigin(); |
193 | 228 |
194 ServiceWorkerUsageInfo& usage_info = origins[origin]; | 229 ServiceWorkerUsageInfo& usage_info = origins[origin]; |
195 if (usage_info.origin.is_empty()) | 230 if (usage_info.origin.is_empty()) |
196 usage_info.origin = origin; | 231 usage_info.origin = origin; |
197 usage_info.scopes.push_back(registration_info.pattern); | 232 usage_info.scopes.push_back(registration_info.pattern); |
198 } | 233 } |
199 | 234 |
200 for (std::map<GURL, ServiceWorkerUsageInfo>::const_iterator it = | 235 for (std::map<GURL, ServiceWorkerUsageInfo>::const_iterator it = |
201 origins.begin(); | 236 origins.begin(); |
202 it != origins.end(); | 237 it != origins.end(); |
203 ++it) { | 238 ++it) { |
204 usage_infos.push_back(it->second); | 239 usage_infos.push_back(it->second); |
205 } | 240 } |
| 241 usage_infos.swap(*registrations_out); |
| 242 } |
206 | 243 |
207 callback.Run(usage_infos); | 244 void ServiceWorkerContextWrapper::DidGetUsageForAllOrigins( |
| 245 const base::Closure& closure, |
| 246 std::map<GURL, int64>* usage_out, |
| 247 const std::map<GURL, int64>& usage_map) { |
| 248 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 249 DCHECK(usage_out); |
| 250 base::ScopedClosureRunner closureRunner(closure); |
| 251 usage_out->clear(); |
| 252 usage_out->insert(usage_map.begin(), usage_map.end()); |
208 } | 253 } |
209 | 254 |
210 namespace { | 255 namespace { |
211 void StatusCodeToBoolCallbackAdapter( | 256 void StatusCodeToBoolCallbackAdapter( |
212 const ServiceWorkerContext::ResultCallback& callback, | 257 const ServiceWorkerContext::ResultCallback& callback, |
213 ServiceWorkerStatusCode code) { | 258 ServiceWorkerStatusCode code) { |
214 callback.Run(code == ServiceWorkerStatusCode::SERVICE_WORKER_OK); | 259 callback.Run(code == ServiceWorkerStatusCode::SERVICE_WORKER_OK); |
215 } | 260 } |
216 | 261 |
217 void EmptySuccessCallback(bool success) { | 262 void EmptySuccessCallback(bool success) { |
(...skipping 87 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
305 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 350 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
306 if (status != SERVICE_WORKER_OK) { | 351 if (status != SERVICE_WORKER_OK) { |
307 context_core_.reset(); | 352 context_core_.reset(); |
308 return; | 353 return; |
309 } | 354 } |
310 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); | 355 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); |
311 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; | 356 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; |
312 } | 357 } |
313 | 358 |
314 } // namespace content | 359 } // namespace content |
OLD | NEW |