diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 1b81decd2be..9c885795ae1 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -221,17 +221,32 @@ void RunCallbackInUI(const base::Callback& callback, T... result) { base::BindOnce(callback, result...)); } +void ResolveOrRejectPromiseInUI(atom::util::Promise promise, int net_error) { + if (net_error != net::OK) { + std::string err_msg = net::ErrorToString(net_error); + util::Promise::RejectPromise(std::move(promise), std::move(err_msg)); + } else { + util::Promise::ResolveEmptyPromise(std::move(promise)); + } +} + // Callback of HttpCache::GetBackend. void OnGetBackend(disk_cache::Backend** backend_ptr, Session::CacheAction action, - const net::CompletionCallback& callback, + const atom::util::CopyablePromise& promise, int result) { if (result != net::OK) { - RunCallbackInUI(callback, result); + std::string err_msg = + "Failed to retrieve cache backend: " + net::ErrorToString(result); + util::Promise::RejectPromise(promise.GetPromise(), std::move(err_msg)); } else if (backend_ptr && *backend_ptr) { if (action == Session::CacheAction::CLEAR) { - (*backend_ptr) - ->DoomAllEntries(base::Bind(&RunCallbackInUI, callback)); + auto success = + (*backend_ptr) + ->DoomAllEntries(base::BindOnce(&ResolveOrRejectPromiseInUI, + promise.GetPromise())); + if (success != net::ERR_IO_PENDING) + ResolveOrRejectPromiseInUI(promise.GetPromise(), success); } else if (action == Session::CacheAction::STATS) { base::StringPairs stats; (*backend_ptr)->GetStats(&stats); @@ -239,30 +254,35 @@ void OnGetBackend(disk_cache::Backend** backend_ptr, if (stat.first == "Current size") { int current_size; base::StringToInt(stat.second, ¤t_size); - RunCallbackInUI(callback, current_size); + util::Promise::ResolvePromise(promise.GetPromise(), + current_size); break; } } } - } else { - RunCallbackInUI(callback, net::ERR_FAILED); } } void DoCacheActionInIO( const scoped_refptr& context_getter, Session::CacheAction action, - const net::CompletionCallback& callback) { + atom::util::Promise promise) { auto* request_context = context_getter->GetURLRequestContext(); + auto* http_cache = request_context->http_transaction_factory()->GetCache(); - if (!http_cache) - RunCallbackInUI(callback, net::ERR_FAILED); + if (!http_cache) { + std::string err_msg = + "Failed to retrieve cache: " + net::ErrorToString(net::ERR_FAILED); + util::Promise::RejectPromise(std::move(promise), std::move(err_msg)); + return; + } // Call GetBackend and make the backend's ptr accessable in OnGetBackend. using BackendPtr = disk_cache::Backend*; auto** backend_ptr = new BackendPtr(nullptr); net::CompletionCallback on_get_backend = - base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, callback); + base::Bind(&OnGetBackend, base::Owned(backend_ptr), action, + atom::util::CopyablePromise(promise)); int rv = http_cache->GetBackend(backend_ptr, on_get_backend); if (rv != net::ERR_IO_PENDING) on_get_backend.Run(net::OK); @@ -428,12 +448,18 @@ v8::Local Session::ResolveProxy(mate::Arguments* args) { } template -void Session::DoCacheAction(const net::CompletionCallback& callback) { +v8::Local Session::DoCacheAction() { + v8::Isolate* isolate = v8::Isolate::GetCurrent(); + util::Promise promise(isolate); + v8::Local handle = promise.GetHandle(); + base::PostTaskWithTraits( FROM_HERE, {BrowserThread::IO}, base::BindOnce(&DoCacheActionInIO, WrapRefCounted(browser_context_->GetRequestContext()), - action, callback)); + action, std::move(promise))); + + return handle; } v8::Local Session::ClearStorageData(mate::Arguments* args) { diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 00a0bf73d83..7debed6c92c 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -11,6 +11,7 @@ #include "atom/browser/api/trackable_object.h" #include "atom/browser/atom_blob_reader.h" #include "atom/browser/net/resolve_proxy_helper.h" +#include "atom/common/promise_util.h" #include "base/values.h" #include "content/public/browser/download_manager.h" #include "native_mate/handle.h" @@ -64,7 +65,7 @@ class Session : public mate::TrackableObject, // Methods. v8::Local ResolveProxy(mate::Arguments* args); template - void DoCacheAction(const net::CompletionCallback& callback); + v8::Local DoCacheAction(); v8::Local ClearStorageData(mate::Arguments* args); void FlushStorageData(); v8::Local SetProxy(mate::Arguments* args); diff --git a/docs/api/promisification.md b/docs/api/promisification.md index 80d66bae974..8d2a8b0a9b2 100644 --- a/docs/api/promisification.md +++ b/docs/api/promisification.md @@ -13,8 +13,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [dialog.showCertificateTrustDialog([browserWindow, ]options, callback)](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showCertificateTrustDialog) - [inAppPurchase.purchaseProduct(productID, quantity, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#purchaseProduct) - [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts) -- [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize) -- [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache) - [ses.getBlobData(identifier, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getBlobData) - [ses.clearAuthCache(options[, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearAuthCache) - [contents.executeJavaScript(code[, userGesture, callback])](https://github.com/electron/electron/blob/master/docs/api/web-contents.md#executeJavaScript) @@ -47,6 +45,8 @@ When a majority of affected functions are migrated, this flag will be enabled by - [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData) - [ses.setProxy(config, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#setProxy) - [ses.resolveProxy(url, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#resolveProxy) +- [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize) +- [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache) - [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal) - [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage) - [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF) diff --git a/docs/api/session.md b/docs/api/session.md index 07160c8bae6..291794f4885 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -97,12 +97,28 @@ The following methods are available on instances of `Session`: * `callback` Function * `size` Integer - Cache size used in bytes. + * `error` Integer - The error code corresponding to the failure. Callback is invoked with the session's current cache size. +**[Deprecated Soon](promisification.md)** + +#### `ses.getCacheSize()` + +Returns `Promise` - the session's current cache size, in bytes. + #### `ses.clearCache(callback)` * `callback` Function - Called when operation is done. + * `error` Integer - The error code corresponding to the failure. + +Clears the session’s HTTP cache. + +**[Deprecated Soon](promisification.md)** + +#### `ses.clearCache()` + +Returns `Promise` - resolves when the cache clear operation is complete. Clears the session’s HTTP cache. diff --git a/docs/styleguide.md b/docs/styleguide.md index 155d83e8a0a..3a291ccaf08 100644 --- a/docs/styleguide.md +++ b/docs/styleguide.md @@ -128,7 +128,7 @@ Using the `Session` and `Cookies` classes as an example: ### Instance Methods -#### `ses.getCacheSize(callback)` +#### `ses.getCacheSize()` ### Instance Properties diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index fdff883335f..73cafa07c94 100644 --- a/lib/browser/api/session.js +++ b/lib/browser/api/session.js @@ -27,6 +27,8 @@ Session.prototype.clearStorageData = deprecate.promisify(Session.prototype.clear Session.prototype.clearHostResolverCache = deprecate.promisify(Session.prototype.clearHostResolverCache) Session.prototype.resolveProxy = deprecate.promisify(Session.prototype.resolveProxy) Session.prototype.setProxy = deprecate.promisify(Session.prototype.setProxy) +Session.prototype.getCacheSize = deprecate.promisify(Session.prototype.getCacheSize) +Session.prototype.clearCache = deprecate.promisify(Session.prototype.clearCache) Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore) Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get)