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

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

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