| OLD | NEW |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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_disk_cache_migrator.h" | 5 #include "content/browser/service_worker/service_worker_disk_cache_migrator.h" |
| 6 | 6 |
| 7 #include "base/memory/ref_counted.h" | 7 #include "base/memory/ref_counted.h" |
| 8 #include "base/metrics/histogram_macros.h" |
| 8 #include "base/strings/string_number_conversions.h" | 9 #include "base/strings/string_number_conversions.h" |
| 10 #include "base/time/time.h" |
| 9 #include "content/common/service_worker/service_worker_types.h" | 11 #include "content/common/service_worker/service_worker_types.h" |
| 10 #include "net/base/io_buffer.h" | 12 #include "net/base/io_buffer.h" |
| 11 #include "net/base/net_errors.h" | 13 #include "net/base/net_errors.h" |
| 12 #include "net/disk_cache/disk_cache.h" | 14 #include "net/disk_cache/disk_cache.h" |
| 13 | 15 |
| 14 namespace content { | 16 namespace content { |
| 15 | 17 |
| 18 namespace { |
| 19 |
| 20 void RecordMigrationResult(ServiceWorkerStatusCode status) { |
| 21 UMA_HISTOGRAM_ENUMERATION("ServiceWorker.DiskCacheMigrator.MigrationResult", |
| 22 status, SERVICE_WORKER_ERROR_MAX_VALUE); |
| 23 } |
| 24 |
| 25 void RecordNumberOfMigratedResources(size_t migrated_resources) { |
| 26 UMA_HISTOGRAM_CUSTOM_COUNTS( |
| 27 "ServiceWorker.DiskCacheMigrator.NumberOfMigratedResources", |
| 28 migrated_resources, 1, 1000, 50); |
| 29 } |
| 30 |
| 31 void RecordMigrationTime(const base::TimeDelta& time) { |
| 32 UMA_HISTOGRAM_MEDIUM_TIMES("ServiceWorker.DiskCacheMigrator.MigrationTime", |
| 33 time); |
| 34 } |
| 35 |
| 36 } // namespace |
| 37 |
| 16 // A task to move a cached resource from the src DiskCache to the dest | 38 // A task to move a cached resource from the src DiskCache to the dest |
| 17 // DiskCache. This is owned by ServiceWorkerDiskCacheMigrator. | 39 // DiskCache. This is owned by ServiceWorkerDiskCacheMigrator. |
| 18 class ServiceWorkerDiskCacheMigrator::Task { | 40 class ServiceWorkerDiskCacheMigrator::Task { |
| 19 public: | 41 public: |
| 20 Task(InflightTaskMap::KeyType task_id, | 42 Task(InflightTaskMap::KeyType task_id, |
| 21 int64 resource_id, | 43 int64 resource_id, |
| 22 disk_cache::ScopedEntryPtr entry, | 44 disk_cache::ScopedEntryPtr entry, |
| 23 ServiceWorkerDiskCache* src, | 45 ServiceWorkerDiskCache* src, |
| 24 ServiceWorkerDiskCache* dest, | 46 ServiceWorkerDiskCache* dest, |
| 25 const base::WeakPtr<ServiceWorkerDiskCacheMigrator>& owner); | 47 const base::WeakPtr<ServiceWorkerDiskCacheMigrator>& owner); |
| (...skipping 196 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 222 DCHECK(!src_->is_disabled()); | 244 DCHECK(!src_->is_disabled()); |
| 223 DCHECK(!dest_->is_disabled()); | 245 DCHECK(!dest_->is_disabled()); |
| 224 } | 246 } |
| 225 | 247 |
| 226 ServiceWorkerDiskCacheMigrator::~ServiceWorkerDiskCacheMigrator() { | 248 ServiceWorkerDiskCacheMigrator::~ServiceWorkerDiskCacheMigrator() { |
| 227 } | 249 } |
| 228 | 250 |
| 229 void ServiceWorkerDiskCacheMigrator::Start(const StatusCallback& callback) { | 251 void ServiceWorkerDiskCacheMigrator::Start(const StatusCallback& callback) { |
| 230 callback_ = callback; | 252 callback_ = callback; |
| 231 iterator_ = src_->disk_cache()->CreateIterator(); | 253 iterator_ = src_->disk_cache()->CreateIterator(); |
| 254 start_time_ = base::TimeTicks::Now(); |
| 232 ContinueMigratingResources(); | 255 ContinueMigratingResources(); |
| 233 } | 256 } |
| 234 | 257 |
| 235 void ServiceWorkerDiskCacheMigrator::ContinueMigratingResources() { | 258 void ServiceWorkerDiskCacheMigrator::ContinueMigratingResources() { |
| 236 scoped_ptr<WrappedEntry> wrapped_entry(new WrappedEntry); | 259 scoped_ptr<WrappedEntry> wrapped_entry(new WrappedEntry); |
| 237 disk_cache::Entry** entry_ptr = wrapped_entry->GetPtr(); | 260 disk_cache::Entry** entry_ptr = wrapped_entry->GetPtr(); |
| 238 | 261 |
| 239 net::CompletionCallback callback = base::Bind( | 262 net::CompletionCallback callback = base::Bind( |
| 240 &ServiceWorkerDiskCacheMigrator::OnOpenNextEntry, | 263 &ServiceWorkerDiskCacheMigrator::OnOpenNextEntry, |
| 241 weak_factory_.GetWeakPtr(), base::Passed(wrapped_entry.Pass())); | 264 weak_factory_.GetWeakPtr(), base::Passed(wrapped_entry.Pass())); |
| (...skipping 56 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 298 ServiceWorkerStatusCode status) { | 321 ServiceWorkerStatusCode status) { |
| 299 DCHECK(inflight_tasks_.Lookup(task_id)); | 322 DCHECK(inflight_tasks_.Lookup(task_id)); |
| 300 inflight_tasks_.Remove(task_id); | 323 inflight_tasks_.Remove(task_id); |
| 301 | 324 |
| 302 if (status != SERVICE_WORKER_OK) { | 325 if (status != SERVICE_WORKER_OK) { |
| 303 inflight_tasks_.Clear(); | 326 inflight_tasks_.Clear(); |
| 304 Complete(status); | 327 Complete(status); |
| 305 return; | 328 return; |
| 306 } | 329 } |
| 307 | 330 |
| 331 ++number_of_migrated_resources_; |
| 332 |
| 308 if (pending_task_) { | 333 if (pending_task_) { |
| 309 RunPendingTask(); | 334 RunPendingTask(); |
| 310 ContinueMigratingResources(); | 335 ContinueMigratingResources(); |
| 311 return; | 336 return; |
| 312 } | 337 } |
| 313 | 338 |
| 314 if (inflight_tasks_.IsEmpty()) { | 339 if (inflight_tasks_.IsEmpty()) { |
| 315 Complete(SERVICE_WORKER_OK); | 340 Complete(SERVICE_WORKER_OK); |
| 316 return; | 341 return; |
| 317 } | 342 } |
| 318 } | 343 } |
| 319 | 344 |
| 320 void ServiceWorkerDiskCacheMigrator::Complete(ServiceWorkerStatusCode status) { | 345 void ServiceWorkerDiskCacheMigrator::Complete(ServiceWorkerStatusCode status) { |
| 321 DCHECK(inflight_tasks_.IsEmpty()); | 346 DCHECK(inflight_tasks_.IsEmpty()); |
| 322 // TODO(nhiroki): Add UMA for the result of migration. | 347 if (status == SERVICE_WORKER_OK) { |
| 348 RecordMigrationTime(base::TimeTicks().Now() - start_time_); |
| 349 RecordNumberOfMigratedResources(number_of_migrated_resources_); |
| 350 } |
| 351 RecordMigrationResult(status); |
| 323 callback_.Run(status); | 352 callback_.Run(status); |
| 324 } | 353 } |
| 325 | 354 |
| 326 } // namespace content | 355 } // namespace content |
| OLD | NEW |