#include "atom/common/node_includes.h"
#include "base/files/file_path.h"
#include "base/prefs/pref_service.h"
+#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h"
#include "base/thread_task_runner_handle.h"
#include "brightray/browser/net/devtools_network_conditions.h"
};
// Runs the callback in UI thread.
-template <typename ...T>
+template<typename ...T>
void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, base::Bind(callback, result...));
// Callback of HttpCache::GetBackend.
void OnGetBackend(disk_cache::Backend** backend_ptr,
+ Session::CacheAction action,
const net::CompletionCallback& callback,
int result) {
if (result != net::OK) {
RunCallbackInUI(callback, result);
} else if (backend_ptr && *backend_ptr) {
- (*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI<int>, callback));
+ if (action == Session::CacheAction::CLEAR) {
+ (*backend_ptr)->DoomAllEntries(base::Bind(&RunCallbackInUI<int>,
+ callback));
+ } else if (action == Session::CacheAction::STATS) {
+ base::StringPairs stats;
+ (*backend_ptr)->GetStats(&stats);
+ for (size_t i = 0; i < stats.size(); ++i) {
+ if (stats[i].first == "Current size") {
+ int current_size;
+ base::StringToInt(stats[i].second, ¤t_size);
+ RunCallbackInUI(callback, current_size);
+ break;
+ }
+ }
+ }
} else {
RunCallbackInUI<int>(callback, net::ERR_FAILED);
}
}
-void ClearHttpCacheInIO(
+void GetHttpCacheInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter,
+ Session::CacheAction action,
const net::CompletionCallback& callback) {
auto request_context = context_getter->GetURLRequestContext();
auto http_cache = request_context->http_transaction_factory()->GetCache();
using BackendPtr = disk_cache::Backend*;
BackendPtr* backend_ptr = new BackendPtr(nullptr);
net::CompletionCallback on_get_backend =
- base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback);
+ base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, callback);
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
if (rv != net::ERR_IO_PENDING)
on_get_backend.Run(net::OK);
new ResolveProxyHelper(browser_context(), url, callback);
}
-void Session::ClearCache(const net::CompletionCallback& callback) {
+template<Session::CacheAction action>
+void Session::DoCacheAction(const net::CompletionCallback& callback) {
BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
- base::Bind(&ClearHttpCacheInIO,
+ base::Bind(&GetHttpCacheInIO,
make_scoped_refptr(browser_context_->GetRequestContext()),
+ action,
callback));
}
mate::ObjectTemplateBuilder(isolate, prototype)
.MakeDestroyable()
.SetMethod("resolveProxy", &Session::ResolveProxy)
- .SetMethod("clearCache", &Session::ClearCache)
+ .SetMethod("getCacheSize", &Session::DoCacheAction<CacheAction::STATS>)
+ .SetMethod("clearCache", &Session::DoCacheAction<CacheAction::CLEAR>)
.SetMethod("clearStorageData", &Session::ClearStorageData)
.SetMethod("flushStorageData", &Session::FlushStorageData)
.SetMethod("setProxy", &Session::SetProxy)