Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(83)

Side by Side Diff: ios/chrome/browser/browsing_data/cache_counter.cc

Issue 2354643002: Add a cache counter for iOS. (Closed)
Patch Set: Fix another hangup. Created 4 years, 3 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
(Empty)
1 // Copyright 2016 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
5 #include "base/bind.h"
6 #include "base/threading/thread_task_runner_handle.h"
7 #include "components/browsing_data/core/pref_names.h"
8 #include "ios/chrome/browser/browsing_data/cache_counter.h"
9 #include "ios/web/public/browser_state.h"
10 #include "ios/web/public/web_thread.h"
11 #include "net/base/completion_callback.h"
12 #include "net/base/net_errors.h"
13 #include "net/disk_cache/disk_cache.h"
14 #include "net/http/http_cache.h"
15 #include "net/http/http_transaction_factory.h"
16 #include "net/url_request/url_request_context.h"
17 #include "net/url_request/url_request_context_getter.h"
18
19 namespace {
20
21 class IOThreadCacheCounter {
22 public:
23 IOThreadCacheCounter(
24 const scoped_refptr<net::URLRequestContextGetter>& context_getter,
25 const net::CompletionCallback& result_callback)
26 : next_step_(STEP_GET_BACKEND),
27 context_getter_(context_getter),
28 result_callback_(result_callback),
29 result_(0),
30 backend_(nullptr) {}
31
32 void Count() {
33 web::WebThread::PostTask(web::WebThread::IO, FROM_HERE,
34 base::Bind(&IOThreadCacheCounter::CountInternal,
35 base::Unretained(this), net::OK));
36 }
37
38 private:
39 enum Step {
40 STEP_GET_BACKEND, // Get the disk_cache::Backend instance.
41 STEP_COUNT, // Run CalculateSizeOfAllEntries() on it.
42 STEP_CALLBACK // Respond on the UI thread.
43 };
44
45 void CountInternal(int rv) {
droger 2016/09/22 14:13:01 Add DCHECK_CURRENTLY_ON(IO);
msramek 2016/09/22 17:02:40 Done.
46 while (rv != net::ERR_IO_PENDING) {
47 // In case of an error, skip to the last step.
48 if (rv < 0)
49 next_step_ = STEP_CALLBACK;
50
51 // Process the counting in three steps: STEP_GET_BACKEND -> STEP_COUNT ->
52 // -> STEP_CALLBACK.
53 switch (next_step_) {
54 case STEP_GET_BACKEND: {
55 next_step_ = STEP_COUNT;
56
57 net::HttpCache* http_cache = context_getter_->GetURLRequestContext()
58 ->http_transaction_factory()
59 ->GetCache();
60
61 rv = http_cache->GetBackend(
62 &backend_, base::Bind(&IOThreadCacheCounter::CountInternal,
63 base::Unretained(this)));
64 break;
droger 2016/09/22 14:13:01 Sometimes it's return, sometimes break. Maybe use
msramek 2016/09/22 17:02:40 That is intentional. All disk_cache::Backend metho
65 }
66
67 case STEP_COUNT: {
68 next_step_ = STEP_CALLBACK;
69
70 DCHECK(backend_);
71 rv = backend_->CalculateSizeOfAllEntries(base::Bind(
72 &IOThreadCacheCounter::CountInternal, base::Unretained(this)));
73 break;
74 }
75
76 case STEP_CALLBACK: {
77 result_ = rv;
78
79 web::WebThread::PostTask(
80 web::WebThread::UI, FROM_HERE,
81 base::Bind(&IOThreadCacheCounter::OnCountingFinished,
82 base::Unretained(this)));
83
84 return;
85 }
86 }
87 }
88 }
89
90 void OnCountingFinished() {
droger 2016/09/22 14:13:01 DCHECK_CURRENTLY_ON(UI); Or a base::ThreadChecker
msramek 2016/09/22 17:02:40 Done.
91 result_callback_.Run(result_);
92 base::ThreadTaskRunnerHandle::Get()->DeleteSoon(FROM_HERE, this);
droger 2016/09/22 14:13:01 Just curious: why not delete this;
msramek 2016/09/22 17:02:40 Changed to "delete this;". No particular reason -
93 }
94
95 Step next_step_;
96 scoped_refptr<net::URLRequestContextGetter> context_getter_;
97 net::CompletionCallback result_callback_;
98 int result_;
99 disk_cache::Backend* backend_;
100 };
101
102 } // namespace
103
104 CacheCounter::CacheCounter(web::BrowserState* browser_state)
105 : pending_(false), browser_state_(browser_state), weak_ptr_factory_(this) {}
106
107 CacheCounter::~CacheCounter() {}
108
109 const char* CacheCounter::GetPrefName() const {
110 return browsing_data::prefs::kDeleteCache;
111 }
112
113 void CacheCounter::Count() {
114 // TODO(msramek): disk_cache::Backend currently does not implement counting
115 // for subsets of cache, only for the entire cache. Thus, we ignore the time
116 // period setting and always request counting for the unbounded time interval.
117 // It is up to the UI to interpret the results for finite time intervals as
118 // upper estimates.
119 pending_ = true;
120
121 // IOThreadCacheCounter deletes itself when done.
122 (new IOThreadCacheCounter(browser_state_->GetRequestContext(),
123 base::Bind(&CacheCounter::OnCacheSizeCalculated,
124 weak_ptr_factory_.GetWeakPtr())))
125 ->Count();
126 }
127
128 void CacheCounter::OnCacheSizeCalculated(int result_bytes) {
129 pending_ = false;
130
131 // A value less than 0 means a net error code.
132 if (result_bytes < 0)
133 return;
134
135 ReportResult(result_bytes);
136 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698