http_cache_data_counter.cc 3.9 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116
  1. // Copyright 2018 The Chromium Authors. All rights reserved.
  2. // Use of this source code is governed by a BSD-style license that can be
  3. // found in the LICENSE file.
  4. #include "services/network/http_cache_data_counter.h"
  5. #include <utility>
  6. #include "base/bind.h"
  7. #include "base/callback.h"
  8. #include "base/location.h"
  9. #include "base/memory/ptr_util.h"
  10. #include "base/threading/sequenced_task_runner_handle.h"
  11. #include "net/disk_cache/disk_cache.h"
  12. #include "net/http/http_cache.h"
  13. #include "net/url_request/url_request_context.h"
  14. namespace network {
  15. std::unique_ptr<HttpCacheDataCounter> HttpCacheDataCounter::CreateAndStart(
  16. net::URLRequestContext* url_request_context,
  17. base::Time start_time,
  18. base::Time end_time,
  19. HttpCacheDataCounterCallback callback) {
  20. HttpCacheDataCounter* instance =
  21. new HttpCacheDataCounter(start_time, end_time, std::move(callback));
  22. net::HttpCache* http_cache =
  23. url_request_context->http_transaction_factory()->GetCache();
  24. if (!http_cache) {
  25. // No cache, no space used. Posts a task, so it will run after the return.
  26. instance->PostResult(false, 0);
  27. } else {
  28. // Tricky here: if |this| gets deleted before |http_cache| gets deleted,
  29. // GetBackend may still write things out (even though the callback will
  30. // abort due to weak pointer), so the destination for the pointer can't be
  31. // owned by |this|.
  32. //
  33. // While it can be transferred to the callback to GetBackend, that callback
  34. // also needs to be kept alive for the duration of this method in order to
  35. // get at the backend pointer in the synchronous result case.
  36. auto backend = std::make_unique<disk_cache::Backend*>();
  37. disk_cache::Backend** backend_ptr = backend.get();
  38. auto get_backend_callback =
  39. base::BindRepeating(&HttpCacheDataCounter::GotBackend,
  40. instance->GetWeakPtr(), base::Passed(&backend));
  41. int rv = http_cache->GetBackend(backend_ptr, get_backend_callback);
  42. if (rv != net::ERR_IO_PENDING) {
  43. instance->GotBackend(std::make_unique<disk_cache::Backend*>(*backend_ptr),
  44. rv);
  45. }
  46. }
  47. return base::WrapUnique(instance);
  48. }
  49. HttpCacheDataCounter::HttpCacheDataCounter(
  50. base::Time start_time,
  51. base::Time end_time,
  52. HttpCacheDataCounterCallback callback)
  53. : start_time_(start_time),
  54. end_time_(end_time),
  55. callback_(std::move(callback)) {}
  56. HttpCacheDataCounter::~HttpCacheDataCounter() {}
  57. void HttpCacheDataCounter::GotBackend(
  58. std::unique_ptr<disk_cache::Backend*> backend,
  59. int error_code) {
  60. DCHECK_LE(error_code, 0);
  61. bool is_upper_limit = false;
  62. if (error_code != net::OK) {
  63. PostResult(is_upper_limit, error_code);
  64. return;
  65. }
  66. if (!*backend) {
  67. PostResult(is_upper_limit, 0);
  68. return;
  69. }
  70. int64_t rv;
  71. disk_cache::Backend* cache = *backend;
  72. // Handle this here since some backends would DCHECK on this.
  73. if (start_time_ > end_time_) {
  74. PostResult(is_upper_limit, 0);
  75. return;
  76. }
  77. if (start_time_.is_null() && end_time_.is_max()) {
  78. rv = cache->CalculateSizeOfAllEntries(base::BindOnce(
  79. &HttpCacheDataCounter::PostResult, GetWeakPtr(), is_upper_limit));
  80. } else {
  81. rv = cache->CalculateSizeOfEntriesBetween(
  82. start_time_, end_time_,
  83. base::BindOnce(&HttpCacheDataCounter::PostResult, GetWeakPtr(),
  84. is_upper_limit));
  85. if (rv == net::ERR_NOT_IMPLEMENTED) {
  86. is_upper_limit = true;
  87. rv = cache->CalculateSizeOfAllEntries(base::BindOnce(
  88. &HttpCacheDataCounter::PostResult, GetWeakPtr(), is_upper_limit));
  89. }
  90. }
  91. if (rv != net::ERR_IO_PENDING)
  92. PostResult(is_upper_limit, rv);
  93. }
  94. void HttpCacheDataCounter::PostResult(bool is_upper_limit,
  95. int64_t result_or_error) {
  96. base::SequencedTaskRunnerHandle::Get()->PostTask(
  97. FROM_HERE, base::BindOnce(std::move(callback_), this, is_upper_limit,
  98. result_or_error));
  99. }
  100. } // namespace network