| 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/files/file_path.h" | 10 #include "base/files/file_path.h" |
| 10 #include "base/logging.h" | 11 #include "base/logging.h" |
| 11 #include "base/threading/sequenced_worker_pool.h" | 12 #include "base/threading/sequenced_worker_pool.h" |
| 12 #include "content/browser/fileapi/chrome_blob_storage_context.h" | 13 #include "content/browser/fileapi/chrome_blob_storage_context.h" |
| 13 #include "content/browser/service_worker/service_worker_context_core.h" | 14 #include "content/browser/service_worker/service_worker_context_core.h" |
| 14 #include "content/browser/service_worker/service_worker_context_observer.h" | 15 #include "content/browser/service_worker/service_worker_context_observer.h" |
| 15 #include "content/browser/service_worker/service_worker_process_manager.h" | 16 #include "content/browser/service_worker/service_worker_process_manager.h" |
| 17 #include "content/browser/service_worker/service_worker_quota_client.h" |
| 16 #include "content/public/browser/browser_context.h" | 18 #include "content/public/browser/browser_context.h" |
| 17 #include "content/public/browser/browser_thread.h" | 19 #include "content/public/browser/browser_thread.h" |
| 18 #include "net/url_request/url_request_context_getter.h" | 20 #include "net/url_request/url_request_context_getter.h" |
| 19 #include "storage/browser/blob/blob_storage_context.h" | 21 #include "storage/browser/blob/blob_storage_context.h" |
| 20 #include "storage/browser/quota/quota_manager_proxy.h" | 22 #include "storage/browser/quota/quota_manager_proxy.h" |
| 21 #include "storage/browser/quota/special_storage_policy.h" | 23 #include "storage/browser/quota/special_storage_policy.h" |
| 22 | 24 |
| 23 namespace content { | 25 namespace content { |
| 24 | 26 |
| 25 ServiceWorkerContextWrapper::ServiceWorkerContextWrapper( | 27 ServiceWorkerContextWrapper::ServiceWorkerContextWrapper( |
| (...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 94 BrowserThread::PostTask( | 96 BrowserThread::PostTask( |
| 95 BrowserThread::IO, | 97 BrowserThread::IO, |
| 96 FROM_HERE, | 98 FROM_HERE, |
| 97 base::Bind(&ServiceWorkerContextWrapper::RegisterServiceWorker, | 99 base::Bind(&ServiceWorkerContextWrapper::RegisterServiceWorker, |
| 98 this, | 100 this, |
| 99 pattern, | 101 pattern, |
| 100 script_url, | 102 script_url, |
| 101 continuation)); | 103 continuation)); |
| 102 return; | 104 return; |
| 103 } | 105 } |
| 104 | 106 if (!context_core_.get()) { |
| 107 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; |
| 108 BrowserThread::PostTask( |
| 109 BrowserThread::IO, |
| 110 FROM_HERE, |
| 111 base::Bind(continuation, false)); |
| 112 return; |
| 113 } |
| 105 context()->RegisterServiceWorker( | 114 context()->RegisterServiceWorker( |
| 106 pattern, | 115 pattern, |
| 107 script_url, | 116 script_url, |
| 108 NULL /* provider_host */, | 117 NULL /* provider_host */, |
| 109 base::Bind(&FinishRegistrationOnIO, continuation)); | 118 base::Bind(&FinishRegistrationOnIO, continuation)); |
| 110 } | 119 } |
| 111 | 120 |
| 112 static void FinishUnregistrationOnIO( | 121 static void FinishUnregistrationOnIO( |
| 113 const ServiceWorkerContext::ResultCallback& continuation, | 122 const ServiceWorkerContext::ResultCallback& continuation, |
| 114 ServiceWorkerStatusCode status) { | 123 ServiceWorkerStatusCode status) { |
| (...skipping 10 matching lines...) Expand all Loading... |
| 125 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { | 134 if (!BrowserThread::CurrentlyOn(BrowserThread::IO)) { |
| 126 BrowserThread::PostTask( | 135 BrowserThread::PostTask( |
| 127 BrowserThread::IO, | 136 BrowserThread::IO, |
| 128 FROM_HERE, | 137 FROM_HERE, |
| 129 base::Bind(&ServiceWorkerContextWrapper::UnregisterServiceWorker, | 138 base::Bind(&ServiceWorkerContextWrapper::UnregisterServiceWorker, |
| 130 this, | 139 this, |
| 131 pattern, | 140 pattern, |
| 132 continuation)); | 141 continuation)); |
| 133 return; | 142 return; |
| 134 } | 143 } |
| 144 if (!context_core_.get()) { |
| 145 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; |
| 146 BrowserThread::PostTask( |
| 147 BrowserThread::IO, |
| 148 FROM_HERE, |
| 149 base::Bind(continuation, false)); |
| 150 return; |
| 151 } |
| 135 | 152 |
| 136 context()->UnregisterServiceWorker( | 153 context()->UnregisterServiceWorker( |
| 137 pattern, | 154 pattern, |
| 138 base::Bind(&FinishUnregistrationOnIO, continuation)); | 155 base::Bind(&FinishUnregistrationOnIO, continuation)); |
| 139 } | 156 } |
| 140 | 157 |
| 141 void ServiceWorkerContextWrapper::Terminate() { | 158 void ServiceWorkerContextWrapper::Terminate() { |
| 142 DCHECK_CURRENTLY_ON(BrowserThread::UI); | 159 DCHECK_CURRENTLY_ON(BrowserThread::UI); |
| 143 process_manager_->Shutdown(); | 160 process_manager_->Shutdown(); |
| 144 } | 161 } |
| 145 | 162 |
| 146 void ServiceWorkerContextWrapper::GetAllOriginsInfo( | 163 void ServiceWorkerContextWrapper::GetAllOriginsInfo( |
| 147 const GetUsageInfoCallback& callback) { | 164 const GetUsageInfoCallback& callback) { |
| 148 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 165 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 149 context_core_->storage()->GetAllRegistrations(base::Bind( | 166 if (!context_core_.get()) { |
| 167 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; |
| 168 BrowserThread::PostTask( |
| 169 BrowserThread::IO, |
| 170 FROM_HERE, |
| 171 base::Bind(callback, std::vector<ServiceWorkerUsageInfo>())); |
| 172 return; |
| 173 } |
| 174 context()->storage()->GetAllRegistrations(base::Bind( |
| 150 &ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins, | 175 &ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins, |
| 151 this, | 176 this, |
| 152 callback)); | 177 callback)); |
| 153 } | 178 } |
| 154 | 179 |
| 155 void ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins( | 180 void ServiceWorkerContextWrapper::DidGetAllRegistrationsForGetAllOrigins( |
| 156 const GetUsageInfoCallback& callback, | 181 const GetUsageInfoCallback& callback, |
| 157 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | 182 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { |
| 158 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 183 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 159 std::vector<ServiceWorkerUsageInfo> usage_infos; | 184 std::vector<ServiceWorkerUsageInfo> usage_infos; |
| (...skipping 16 matching lines...) Expand all Loading... |
| 176 origins.begin(); | 201 origins.begin(); |
| 177 it != origins.end(); | 202 it != origins.end(); |
| 178 ++it) { | 203 ++it) { |
| 179 usage_infos.push_back(it->second); | 204 usage_infos.push_back(it->second); |
| 180 } | 205 } |
| 181 | 206 |
| 182 callback.Run(usage_infos); | 207 callback.Run(usage_infos); |
| 183 } | 208 } |
| 184 | 209 |
| 185 namespace { | 210 namespace { |
| 211 void StatusCodeToBoolCallbackAdapter( |
| 212 const ServiceWorkerContext::ResultCallback& callback, |
| 213 ServiceWorkerStatusCode code) { |
| 214 callback.Run(code == ServiceWorkerStatusCode::SERVICE_WORKER_OK); |
| 215 } |
| 186 | 216 |
| 187 void EmptySuccessCallback(bool success) { | 217 void EmptySuccessCallback(bool success) { |
| 188 } | 218 } |
| 189 | |
| 190 } // namespace | 219 } // namespace |
| 191 | 220 |
| 192 void ServiceWorkerContextWrapper::DeleteForOrigin(const GURL& origin_url) { | 221 void ServiceWorkerContextWrapper::DeleteForOrigin( |
| 222 const GURL& origin_url, |
| 223 const ResultCallback& result) { |
| 193 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 224 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 194 context_core_->storage()->GetAllRegistrations(base::Bind( | 225 if (!context_core_.get()) { |
| 195 &ServiceWorkerContextWrapper::DidGetAllRegistrationsForDeleteForOrigin, | 226 LOG(ERROR) << "ServiceWorkerContextCore is no longer alive."; |
| 196 this, | 227 BrowserThread::PostTask( |
| 197 origin_url)); | 228 BrowserThread::IO, |
| 229 FROM_HERE, |
| 230 base::Bind(result, false)); |
| 231 return; |
| 232 } |
| 233 context()->UnregisterServiceWorkers( |
| 234 origin_url, base::Bind(&StatusCodeToBoolCallbackAdapter, result)); |
| 198 } | 235 } |
| 199 | 236 |
| 200 void ServiceWorkerContextWrapper::DidGetAllRegistrationsForDeleteForOrigin( | 237 void ServiceWorkerContextWrapper::DeleteForOrigin(const GURL& origin_url) { |
| 201 const GURL& origin, | 238 DeleteForOrigin(origin_url, base::Bind(&EmptySuccessCallback)); |
| 202 const std::vector<ServiceWorkerRegistrationInfo>& registrations) { | |
| 203 DCHECK_CURRENTLY_ON(BrowserThread::IO); | |
| 204 | |
| 205 for (std::vector<ServiceWorkerRegistrationInfo>::const_iterator it = | |
| 206 registrations.begin(); | |
| 207 it != registrations.end(); | |
| 208 ++it) { | |
| 209 const ServiceWorkerRegistrationInfo& registration_info = *it; | |
| 210 if (origin == registration_info.pattern.GetOrigin()) { | |
| 211 UnregisterServiceWorker(registration_info.pattern, | |
| 212 base::Bind(&EmptySuccessCallback)); | |
| 213 } | |
| 214 } | |
| 215 } | 239 } |
| 216 | 240 |
| 217 void ServiceWorkerContextWrapper::AddObserver( | 241 void ServiceWorkerContextWrapper::AddObserver( |
| 218 ServiceWorkerContextObserver* observer) { | 242 ServiceWorkerContextObserver* observer) { |
| 219 observer_list_->AddObserver(observer); | 243 observer_list_->AddObserver(observer); |
| 220 } | 244 } |
| 221 | 245 |
| 222 void ServiceWorkerContextWrapper::RemoveObserver( | 246 void ServiceWorkerContextWrapper::RemoveObserver( |
| 223 ServiceWorkerContextObserver* observer) { | 247 ServiceWorkerContextObserver* observer) { |
| 224 observer_list_->RemoveObserver(observer); | 248 observer_list_->RemoveObserver(observer); |
| (...skipping 26 matching lines...) Expand all Loading... |
| 251 this, | 275 this, |
| 252 user_data_directory, | 276 user_data_directory, |
| 253 stores_task_runner, | 277 stores_task_runner, |
| 254 base::Passed(&database_task_manager), | 278 base::Passed(&database_task_manager), |
| 255 disk_cache_thread, | 279 disk_cache_thread, |
| 256 make_scoped_refptr(quota_manager_proxy), | 280 make_scoped_refptr(quota_manager_proxy), |
| 257 make_scoped_refptr(special_storage_policy))); | 281 make_scoped_refptr(special_storage_policy))); |
| 258 return; | 282 return; |
| 259 } | 283 } |
| 260 DCHECK(!context_core_); | 284 DCHECK(!context_core_); |
| 285 if (quota_manager_proxy) { |
| 286 quota_manager_proxy->RegisterClient(new ServiceWorkerQuotaClient(this)); |
| 287 } |
| 261 context_core_.reset(new ServiceWorkerContextCore(user_data_directory, | 288 context_core_.reset(new ServiceWorkerContextCore(user_data_directory, |
| 262 stores_task_runner, | 289 stores_task_runner, |
| 263 database_task_manager.Pass(), | 290 database_task_manager.Pass(), |
| 264 disk_cache_thread, | 291 disk_cache_thread, |
| 265 quota_manager_proxy, | 292 quota_manager_proxy, |
| 266 special_storage_policy, | 293 special_storage_policy, |
| 267 observer_list_.get(), | 294 observer_list_.get(), |
| 268 this)); | 295 this)); |
| 269 } | 296 } |
| 270 | 297 |
| 271 void ServiceWorkerContextWrapper::ShutdownOnIO() { | 298 void ServiceWorkerContextWrapper::ShutdownOnIO() { |
| 272 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 299 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 273 context_core_.reset(); | 300 context_core_.reset(); |
| 274 } | 301 } |
| 275 | 302 |
| 276 void ServiceWorkerContextWrapper::DidDeleteAndStartOver( | 303 void ServiceWorkerContextWrapper::DidDeleteAndStartOver( |
| 277 ServiceWorkerStatusCode status) { | 304 ServiceWorkerStatusCode status) { |
| 278 DCHECK_CURRENTLY_ON(BrowserThread::IO); | 305 DCHECK_CURRENTLY_ON(BrowserThread::IO); |
| 279 if (status != SERVICE_WORKER_OK) { | 306 if (status != SERVICE_WORKER_OK) { |
| 280 context_core_.reset(); | 307 context_core_.reset(); |
| 281 return; | 308 return; |
| 282 } | 309 } |
| 283 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); | 310 context_core_.reset(new ServiceWorkerContextCore(context_core_.get(), this)); |
| 284 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; | 311 DVLOG(1) << "Restarted ServiceWorkerContextCore successfully."; |
| 285 } | 312 } |
| 286 | 313 |
| 287 } // namespace content | 314 } // namespace content |
| OLD | NEW |