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

Unified 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 side-by-side diff with in-line comments
Download patch
Index: chrome/browser/browsing_data/cache_test_util.cc
diff --git a/chrome/browser/browsing_data/cache_test_util.cc b/chrome/browser/browsing_data/cache_test_util.cc
new file mode 100644
index 0000000000000000000000000000000000000000..202caadaac256223fe79c303c5dc35b84660c81a
--- /dev/null
+++ b/chrome/browser/browsing_data/cache_test_util.cc
@@ -0,0 +1,147 @@
+// Copyright 2016 The Chromium Authors. All rights reserved.
+// Use of this source code is governed by a BSD-style license that can be
+// found in the LICENSE file.
+
+#include "chrome/browser/browsing_data/cache_test_util.h"
+#include "net/disk_cache/disk_cache.h"
+#include "net/http/http_cache.h"
+#include "net/url_request/url_request_context.h"
+#include "net/url_request/url_request_context_getter.h"
+
+using content::BrowserThread;
+
+CacheTestUtil::CacheTestUtil(content::StoragePartition* partition)
+ : partition_(partition), remaining_tasks_(0) {}
+
+CacheTestUtil::~CacheTestUtil() {}
+
+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.
+ done_callback_ =
+ base::Bind(&CacheTestUtil::DoneCallback, base::Unretained(this));
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&CacheTestUtil::SetUpOnIOThread, base::Unretained(this)));
+ WaitForTasksOnIOThread();
+}
+
+void CacheTestUtil::TearDownOnMainThread() {
+ // The cache iterator must be deleted on the thread where it was created,
+ // which is the IO thread.
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&CacheTestUtil::TearDownOnIOThread, base::Unretained(this)));
+ WaitForTasksOnIOThread();
+}
+
+void CacheTestUtil::CreateCacheEntries(const std::set<std::string>& keys) {
+ BrowserThread::PostTask(
+ BrowserThread::IO, FROM_HERE,
+ base::Bind(&CacheTestUtil::CreateCacheEntriesOnIOThread,
+ base::Unretained(this), base::ConstRef(keys)));
+ WaitForTasksOnIOThread();
+}
+
+void CacheTestUtil::SetUpOnIOThread() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ net::URLRequestContextGetter* context = partition_->GetURLRequestContext();
+
+ net::HttpCache* cache =
+ context->GetURLRequestContext()->http_transaction_factory()->GetCache();
+
+ SetNumberOfWaitedTasks(1);
+ WaitForCompletion(cache->GetBackend(&backend_, done_callback_));
+}
+
+void CacheTestUtil::TearDownOnIOThread() {
+ iterator_.reset();
+ DoneCallback(net::OK);
+}
+
+void CacheTestUtil::CreateCacheEntriesOnIOThread(
+ const std::set<std::string>& keys) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+
+ entries_.resize(keys.size());
+ SetNumberOfWaitedTasks(keys.size());
+
+ int pos = 0;
+ for (const std::string& key : keys) {
+ WaitForCompletion(
+ backend_->CreateEntry(key, &entries_[pos++], done_callback_));
+ }
+}
+
+// Waiting for tasks to be done on IO thread. --------------------------------
+
+void CacheTestUtil::WaitForTasksOnIOThread() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ run_loop_.reset(new base::RunLoop());
+ run_loop_->Run();
+}
+
+void CacheTestUtil::SetNumberOfWaitedTasks(int count) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ remaining_tasks_ = count;
+}
+
+void CacheTestUtil::WaitForCompletion(int value) {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (value >= 0) {
+ // We got the result immediately.
+ DoneCallback(value);
+ } else if (value == net::ERR_IO_PENDING) {
+ // We need to wait for the callback.
+ } else {
+ // An error has occurred.
+ NOTREACHED();
+ }
+}
+
+void CacheTestUtil::DoneCallback(int value) {
+ DCHECK_GE(value, 0); // Negative values represent an error.
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ if (--remaining_tasks_ > 0)
+ return;
+
+ if (run_loop_) {
+ BrowserThread::PostTask(
+ BrowserThread::UI, FROM_HERE,
+ base::Bind(&base::RunLoop::Quit, base::Unretained(run_loop_.get())));
+ }
+}
+
+// Check cache content
msramek 2016/12/20 01:02:59 nit: Period at the end of the sentence.
+
+std::vector<std::string> CacheTestUtil::GetEntryKeys() {
+ DCHECK_CURRENTLY_ON(BrowserThread::UI);
+ BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
+ base::Bind(&CacheTestUtil::GetEntryKeysOnIOThread,
+ base::Unretained(this)));
+ WaitForTasksOnIOThread();
+ return remaining_keys_;
+}
+
+void CacheTestUtil::GetEntryKeysOnIOThread() {
+ DCHECK_CURRENTLY_ON(BrowserThread::IO);
+ remaining_keys_.clear();
+ current_entry_ = nullptr;
+ iterator_ = backend_->CreateIterator();
+ GetNextKey(net::OK);
+}
+
+void CacheTestUtil::GetNextKey(int error) {
+ while (error != net::ERR_IO_PENDING) {
+ if (error == net::ERR_FAILED) {
+ DoneCallback(net::OK);
+ return;
+ }
+
+ if (current_entry_) {
+ remaining_keys_.push_back(current_entry_->GetKey());
+ }
+
+ error = iterator_->OpenNextEntry(
+ &current_entry_,
+ base::Bind(&CacheTestUtil::GetNextKey, base::Unretained(this)));
+ }
+}

Powered by Google App Engine
This is Rietveld 408576698