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

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

Powered by Google App Engine
This is Rietveld 408576698