Merge pull request #4098 from deepak1556/session_cache_size_patch

session: api to get current cache size
This commit is contained in:
Cheng Zhao 2016-01-14 20:18:21 +08:00
Родитель 88725d4fd7 10e4698baa
Коммит f93b4e76cb
3 изменённых файлов: 41 добавлений и 8 удалений

Просмотреть файл

@ -22,6 +22,7 @@
#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"
@ -204,7 +205,7 @@ class ResolveProxyHelper {
};
// 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...));
@ -212,19 +213,35 @@ void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... 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, &current_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();
@ -235,7 +252,7 @@ void ClearHttpCacheInIO(
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);
@ -287,10 +304,12 @@ void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
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));
}
@ -420,7 +439,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
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)

Просмотреть файл

@ -38,6 +38,11 @@ class Session: public mate::TrackableObject<Session>,
public:
using ResolveProxyCallback = base::Callback<void(std::string)>;
enum class CacheAction {
CLEAR,
STATS,
};
// Gets or creates Session from the |browser_context|.
static mate::Handle<Session> CreateFrom(
v8::Isolate* isolate, AtomBrowserContext* browser_context);
@ -62,7 +67,8 @@ class Session: public mate::TrackableObject<Session>,
private:
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
void ClearCache(const net::CompletionCallback& callback);
template<CacheAction action>
void DoCacheAction(const net::CompletionCallback& callback);
void ClearStorageData(mate::Arguments* args);
void FlushStorageData();
void SetProxy(const net::ProxyConfig& config, const base::Closure& callback);

Просмотреть файл

@ -159,6 +159,13 @@ on complete.
Removes the cookies matching `url` and `name`, `callback` will called with
`callback()` on complete.
#### `ses.getCacheSize(callback)`
* `callback` Function
* `size` Integer - Cache size used in bytes.
Returns the session's current cache size.
#### `ses.clearCache(callback)`
* `callback` Function - Called when operation is done