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

Side by Side Diff: chrome/browser/browsing_data/cache_test_util.cc

Issue 2556363003: Refactor cache counting into a separate helper class (Closed)
Patch Set: extract cache_test_util and fixes Created 4 years 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 "chrome/browser/browsing_data/cache_test_util.h"
6 #include "net/disk_cache/disk_cache.h"
7 #include "net/http/http_cache.h"
8 #include "net/url_request/url_request_context.h"
9 #include "net/url_request/url_request_context_getter.h"
10
11 using content::BrowserThread;
12
13 CacheTestUtil::CacheTestUtil(content::StoragePartition* partition)
14 : partition_(partition), remaining_tasks_(0) {}
15
16 CacheTestUtil::~CacheTestUtil() {}
17
18 void CacheTestUtil::SetUpOnMainThread() {
msramek 2016/12/20 01:02:59 Couldn't this method's body be merged into the con
dullweber 2016/12/21 10:29:19 yes, sounds good.
19 done_callback_ =
20 base::Bind(&CacheTestUtil::DoneCallback, base::Unretained(this));
21 BrowserThread::PostTask(
22 BrowserThread::IO, FROM_HERE,
23 base::Bind(&CacheTestUtil::SetUpOnIOThread, base::Unretained(this)));
24 WaitForTasksOnIOThread();
25 }
26
27 void CacheTestUtil::TearDownOnMainThread() {
28 // The cache iterator must be deleted on the thread where it was created,
29 // which is the IO thread.
30 BrowserThread::PostTask(
31 BrowserThread::IO, FROM_HERE,
32 base::Bind(&CacheTestUtil::TearDownOnIOThread, base::Unretained(this)));
33 WaitForTasksOnIOThread();
34 }
35
36 void CacheTestUtil::CreateCacheEntries(const std::set<std::string>& keys) {
37 BrowserThread::PostTask(
38 BrowserThread::IO, FROM_HERE,
39 base::Bind(&CacheTestUtil::CreateCacheEntriesOnIOThread,
40 base::Unretained(this), base::ConstRef(keys)));
41 WaitForTasksOnIOThread();
42 }
43
44 void CacheTestUtil::SetUpOnIOThread() {
45 DCHECK_CURRENTLY_ON(BrowserThread::IO);
46 net::URLRequestContextGetter* context = partition_->GetURLRequestContext();
47
48 net::HttpCache* cache =
49 context->GetURLRequestContext()->http_transaction_factory()->GetCache();
50
51 SetNumberOfWaitedTasks(1);
52 WaitForCompletion(cache->GetBackend(&backend_, done_callback_));
53 }
54
55 void CacheTestUtil::TearDownOnIOThread() {
56 iterator_.reset();
57 DoneCallback(net::OK);
58 }
59
60 void CacheTestUtil::CreateCacheEntriesOnIOThread(
61 const std::set<std::string>& keys) {
62 DCHECK_CURRENTLY_ON(BrowserThread::IO);
63
64 entries_.resize(keys.size());
65 SetNumberOfWaitedTasks(keys.size());
66
67 int pos = 0;
68 for (const std::string& key : keys) {
69 WaitForCompletion(
70 backend_->CreateEntry(key, &entries_[pos++], done_callback_));
71 }
72 }
73
74 // Waiting for tasks to be done on IO thread. --------------------------------
75
76 void CacheTestUtil::WaitForTasksOnIOThread() {
77 DCHECK_CURRENTLY_ON(BrowserThread::UI);
78 run_loop_.reset(new base::RunLoop());
79 run_loop_->Run();
80 }
81
82 void CacheTestUtil::SetNumberOfWaitedTasks(int count) {
83 DCHECK_CURRENTLY_ON(BrowserThread::IO);
84 remaining_tasks_ = count;
85 }
86
87 void CacheTestUtil::WaitForCompletion(int value) {
88 DCHECK_CURRENTLY_ON(BrowserThread::IO);
89 if (value >= 0) {
90 // We got the result immediately.
91 DoneCallback(value);
92 } else if (value == net::ERR_IO_PENDING) {
93 // We need to wait for the callback.
94 } else {
95 // An error has occurred.
96 NOTREACHED();
97 }
98 }
99
100 void CacheTestUtil::DoneCallback(int value) {
101 DCHECK_GE(value, 0); // Negative values represent an error.
102 DCHECK_CURRENTLY_ON(BrowserThread::IO);
103 if (--remaining_tasks_ > 0)
104 return;
105
106 if (run_loop_) {
107 BrowserThread::PostTask(
108 BrowserThread::UI, FROM_HERE,
109 base::Bind(&base::RunLoop::Quit, base::Unretained(run_loop_.get())));
110 }
111 }
112
113 // Check cache content
msramek 2016/12/20 01:02:59 nit: Period at the end of the sentence.
114
115 std::vector<std::string> CacheTestUtil::GetEntryKeys() {
116 DCHECK_CURRENTLY_ON(BrowserThread::UI);
117 BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
118 base::Bind(&CacheTestUtil::GetEntryKeysOnIOThread,
119 base::Unretained(this)));
120 WaitForTasksOnIOThread();
121 return remaining_keys_;
122 }
123
124 void CacheTestUtil::GetEntryKeysOnIOThread() {
125 DCHECK_CURRENTLY_ON(BrowserThread::IO);
126 remaining_keys_.clear();
127 current_entry_ = nullptr;
128 iterator_ = backend_->CreateIterator();
129 GetNextKey(net::OK);
130 }
131
132 void CacheTestUtil::GetNextKey(int error) {
133 while (error != net::ERR_IO_PENDING) {
134 if (error == net::ERR_FAILED) {
135 DoneCallback(net::OK);
136 return;
137 }
138
139 if (current_entry_) {
140 remaining_keys_.push_back(current_entry_->GetKey());
141 }
142
143 error = iterator_->OpenNextEntry(
144 &current_entry_,
145 base::Bind(&CacheTestUtil::GetNextKey, base::Unretained(this)));
146 }
147 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698