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
commit f93b4e76cb
3 changed files with 41 additions and 8 deletions

View file

@ -22,6 +22,7 @@
#include "atom/common/node_includes.h" #include "atom/common/node_includes.h"
#include "base/files/file_path.h" #include "base/files/file_path.h"
#include "base/prefs/pref_service.h" #include "base/prefs/pref_service.h"
#include "base/strings/string_number_conversions.h"
#include "base/strings/string_util.h" #include "base/strings/string_util.h"
#include "base/thread_task_runner_handle.h" #include "base/thread_task_runner_handle.h"
#include "brightray/browser/net/devtools_network_conditions.h" #include "brightray/browser/net/devtools_network_conditions.h"
@ -204,7 +205,7 @@ class ResolveProxyHelper {
}; };
// Runs the callback in UI thread. // Runs the callback in UI thread.
template <typename ...T> template<typename ...T>
void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) { void RunCallbackInUI(const base::Callback<void(T...)>& callback, T... result) {
BrowserThread::PostTask( BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE, base::Bind(callback, result...)); 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. // Callback of HttpCache::GetBackend.
void OnGetBackend(disk_cache::Backend** backend_ptr, void OnGetBackend(disk_cache::Backend** backend_ptr,
Session::CacheAction action,
const net::CompletionCallback& callback, const net::CompletionCallback& callback,
int result) { int result) {
if (result != net::OK) { if (result != net::OK) {
RunCallbackInUI(callback, result); RunCallbackInUI(callback, result);
} else if (backend_ptr && *backend_ptr) { } 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 { } else {
RunCallbackInUI<int>(callback, net::ERR_FAILED); RunCallbackInUI<int>(callback, net::ERR_FAILED);
} }
} }
void ClearHttpCacheInIO( void GetHttpCacheInIO(
const scoped_refptr<net::URLRequestContextGetter>& context_getter, const scoped_refptr<net::URLRequestContextGetter>& context_getter,
Session::CacheAction action,
const net::CompletionCallback& callback) { const net::CompletionCallback& callback) {
auto request_context = context_getter->GetURLRequestContext(); auto request_context = context_getter->GetURLRequestContext();
auto http_cache = request_context->http_transaction_factory()->GetCache(); auto http_cache = request_context->http_transaction_factory()->GetCache();
@ -235,7 +252,7 @@ void ClearHttpCacheInIO(
using BackendPtr = disk_cache::Backend*; using BackendPtr = disk_cache::Backend*;
BackendPtr* backend_ptr = new BackendPtr(nullptr); BackendPtr* backend_ptr = new BackendPtr(nullptr);
net::CompletionCallback on_get_backend = 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); int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
if (rv != net::ERR_IO_PENDING) if (rv != net::ERR_IO_PENDING)
on_get_backend.Run(net::OK); on_get_backend.Run(net::OK);
@ -287,10 +304,12 @@ void Session::ResolveProxy(const GURL& url, ResolveProxyCallback callback) {
new ResolveProxyHelper(browser_context(), url, 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, BrowserThread::PostTask(BrowserThread::IO, FROM_HERE,
base::Bind(&ClearHttpCacheInIO, base::Bind(&GetHttpCacheInIO,
make_scoped_refptr(browser_context_->GetRequestContext()), make_scoped_refptr(browser_context_->GetRequestContext()),
action,
callback)); callback));
} }
@ -420,7 +439,8 @@ void Session::BuildPrototype(v8::Isolate* isolate,
mate::ObjectTemplateBuilder(isolate, prototype) mate::ObjectTemplateBuilder(isolate, prototype)
.MakeDestroyable() .MakeDestroyable()
.SetMethod("resolveProxy", &Session::ResolveProxy) .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("clearStorageData", &Session::ClearStorageData)
.SetMethod("flushStorageData", &Session::FlushStorageData) .SetMethod("flushStorageData", &Session::FlushStorageData)
.SetMethod("setProxy", &Session::SetProxy) .SetMethod("setProxy", &Session::SetProxy)

View file

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

View file

@ -159,6 +159,13 @@ on complete.
Removes the cookies matching `url` and `name`, `callback` will called with Removes the cookies matching `url` and `name`, `callback` will called with
`callback()` on complete. `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)` #### `ses.clearCache(callback)`
* `callback` Function - Called when operation is done * `callback` Function - Called when operation is done