Call callback of ClearCache after cache is doomed

This commit is contained in:
Cheng Zhao 2015-07-13 14:27:07 -07:00
parent 6840d424cd
commit 7dba4d1d8d
2 changed files with 30 additions and 22 deletions

View file

@ -130,28 +130,37 @@ class ResolveProxyHelper {
DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper); DISALLOW_COPY_AND_ASSIGN(ResolveProxyHelper);
}; };
void Noop(int result) { // Runs the callback in UI thread.
DCHECK(result == net::OK); void RunCallbackInUI(const net::CompletionCallback& callback, int result) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, base::Bind(callback, result));
} }
void DoClearCache(disk_cache::Backend** cache_ptr, // Callback of HttpCache::GetBackend.
void OnGetBackend(disk_cache::Backend** backend_ptr,
const net::CompletionCallback& callback,
int result) { int result) {
DCHECK(result == net::OK); if (result != net::OK) {
if (cache_ptr && *cache_ptr) RunCallbackInUI(callback, result);
(*cache_ptr)->DoomAllEntries(base::Bind(&Noop)); } else if (backend_ptr && *backend_ptr) {
(*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI, callback));
} else {
RunCallbackInUI(callback, net::ERR_FAILED);
}
} }
void ClearHttpCacheOnIO(net::URLRequestContextGetter* getter) { void ClearHttpCacheInIO(content::BrowserContext* browser_context,
typedef disk_cache::Backend* Backendptr; const net::CompletionCallback& callback) {
Backendptr* cache_ptr = new Backendptr(nullptr); auto request_context =
auto request_context = getter->GetURLRequestContext(); browser_context->GetRequestContext()->GetURLRequestContext();
net::CompletionCallback callback(base::Bind(&DoClearCache,
base::Owned(cache_ptr)));
auto http_cache = request_context->http_transaction_factory()->GetCache(); auto http_cache = request_context->http_transaction_factory()->GetCache();
int rv = http_cache->GetBackend(cache_ptr, callback);
disk_cache::Backend** backend_ptr = nullptr;
net::CompletionCallback on_get_backend =
base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback);
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
if (rv != net::ERR_IO_PENDING) if (rv != net::ERR_IO_PENDING)
callback.Run(net::OK); on_get_backend.Run(net::OK);
} }
} // namespace } // namespace
@ -168,12 +177,11 @@ void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(browser_context_, url, callback); new ResolveProxyHelper(browser_context_, url, callback);
} }
void Session::ClearCache(const base::Closure& callback) { void Session::ClearCache(const net::CompletionCallback& callback) {
auto getter = browser_context_->GetRequestContext(); BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
BrowserThread::PostTaskAndReply(BrowserThread::IO, FROM_HERE, base::Bind(&ClearHttpCacheInIO,
base::Bind(&ClearHttpCacheOnIO, base::Unretained(browser_context_),
base::Unretained(getter)), callback));
callback);
} }
void Session::ClearStorageData(const GURL& origin, void Session::ClearStorageData(const GURL& origin,

View file

@ -9,8 +9,8 @@
#include <vector> #include <vector>
#include "atom/browser/api/trackable_object.h" #include "atom/browser/api/trackable_object.h"
#include "base/callback.h"
#include "native_mate/handle.h" #include "native_mate/handle.h"
#include "net/base/completion_callback.h"
class GURL; class GURL;
@ -38,7 +38,7 @@ class Session: public mate::TrackableObject<Session> {
private: private:
void ResolveProxy(const GURL& url, ResolveProxyCallback callback); void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
void ClearCache(const base::Closure& callback); void ClearCache(const net::CompletionCallback& callback);
void ClearStorageData(const GURL& origin, void ClearStorageData(const GURL& origin,
const std::vector<std::string>& storage_types, const std::vector<std::string>& storage_types,
const std::vector<std::string>& quota_types, const std::vector<std::string>& quota_types,