diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index e5e3809859..beaeb9351e 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -328,11 +328,6 @@ void AllowNTLMCredentialsForDomainsInIO( } } -void OnClearStorageDataDone(const base::Closure& callback) { - if (!callback.is_null()) - callback.Run(); -} - void DownloadIdCallback(content::DownloadManager* download_manager, const base::FilePath& path, const std::vector& url_chain, @@ -429,12 +424,13 @@ void Session::DoCacheAction(const net::CompletionCallback& callback) { action, callback)); } -void Session::ClearStorageData(mate::Arguments* args) { - // clearStorageData([options, callback]) +v8::Local Session::ClearStorageData(mate::Arguments* args) { + v8::Isolate* isolate = args->isolate(); + util::Promise promise(isolate); + v8::Local handle = promise.GetHandle(); + ClearStorageDataOptions options; - base::Closure callback; args->GetNext(&options); - args->GetNext(&callback); auto* storage_partition = content::BrowserContext::GetStoragePartition(browser_context(), nullptr); @@ -443,9 +439,13 @@ void Session::ClearStorageData(mate::Arguments* args) { // https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-deviceid MediaDeviceIDSalt::Reset(browser_context()->prefs()); } - storage_partition->ClearData(options.storage_types, options.quota_types, - options.origin, base::Time(), base::Time::Max(), - base::Bind(&OnClearStorageDataDone, callback)); + + storage_partition->ClearData( + options.storage_types, options.quota_types, options.origin, base::Time(), + base::Time::Max(), + base::Bind(util::CopyablePromise::ResolveEmptyCopyablePromise, + util::CopyablePromise(promise))); + return handle; } void Session::FlushStorageData() { diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 3a6216a031..5ecaebebca 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -66,7 +66,7 @@ class Session : public mate::TrackableObject, const ResolveProxyHelper::ResolveProxyCallback& callback); template void DoCacheAction(const net::CompletionCallback& callback); - void ClearStorageData(mate::Arguments* args); + v8::Local ClearStorageData(mate::Arguments* args); void FlushStorageData(); void SetProxy(const mate::Dictionary& options, const base::Closure& callback); void SetDownloadPath(const base::FilePath& path); diff --git a/docs/api/promisification.md b/docs/api/promisification.md index 42af84871a..c902a9b9ab 100644 --- a/docs/api/promisification.md +++ b/docs/api/promisification.md @@ -15,7 +15,6 @@ When a majority of affected functions are migrated, this flag will be enabled by - [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.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.clearHostResolverCache([callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearHostResolverCache) @@ -47,6 +46,7 @@ When a majority of affected functions are migrated, this flag will be enabled by - [dialog.showSaveDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showSaveDialog) - [netLog.stopLogging([callback])](https://github.com/electron/electron/blob/master/docs/api/net-log.md#stopLogging) - [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled) +- [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData) - [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 dd1fcab94d..96086889ed 100644 --- a/docs/api/session.md +++ b/docs/api/session.md @@ -106,7 +106,7 @@ Callback is invoked with the session's current cache size. Clears the session’s HTTP cache. -#### `ses.clearStorageData([options, callback])` +#### `ses.clearStorageData([options,] callback)` * `options` Object (optional) * `origin` String (optional) - Should follow `window.location.origin`’s representation @@ -118,7 +118,22 @@ Clears the session’s HTTP cache. `temporary`, `persistent`, `syncable`. * `callback` Function (optional) - Called when operation is done. -Clears the data of web storages. +Clears the storage data for the current session. + +**[Deprecated Soon](promisification.md)** + +#### `ses.clearStorageData([options])` + +* `options` Object (optional) + * `origin` String (optional) - Should follow `window.location.origin`’s representation + `scheme://host:port`. + * `storages` String[] (optional) - The types of storages to clear, can contain: + `appcache`, `cookies`, `filesystem`, `indexdb`, `localstorage`, + `shadercache`, `websql`, `serviceworkers`, `cachestorage`. + * `quotas` String[] (optional) - The types of quotas to clear, can contain: + `temporary`, `persistent`, `syncable`. + +Returns `Promise` - resolves when the storage data has been cleared. #### `ses.flushStorageData()` diff --git a/lib/browser/api/session.js b/lib/browser/api/session.js index 3f860fae2c..ed9a778516 100644 --- a/lib/browser/api/session.js +++ b/lib/browser/api/session.js @@ -23,6 +23,8 @@ Session.prototype._init = function () { app.emit('session-created', this) } +Session.prototype.clearStorageData = deprecate.promisify(Session.prototype.clearStorageData) + Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore) Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get) Cookies.prototype.remove = deprecate.promisify(Cookies.prototype.remove) diff --git a/spec/api-session-spec.js b/spec/api-session-spec.js index 6e8ca1d2e8..5da63e8655 100644 --- a/spec/api-session-spec.js +++ b/spec/api-session-spec.js @@ -324,6 +324,26 @@ describe('session module', () => { describe('ses.clearStorageData(options)', () => { fixtures = path.resolve(__dirname, 'fixtures') it('clears localstorage data', (done) => { + ipcMain.on('count', (event, count) => { + ipcMain.removeAllListeners('count') + assert.strictEqual(count, 0) + done() + }) + w.webContents.on('did-finish-load', () => { + const options = { + origin: 'file://', + storages: ['localstorage'], + quotas: ['persistent'] + } + w.webContents.session.clearStorageData(options).then(() => { + w.webContents.send('getcount') + }) + }) + w.loadFile(path.join(fixtures, 'api', 'localstorage.html')) + }) + + // TODO(codebytere): remove when promisification is complete + it('clears localstorage data (callback)', (done) => { ipcMain.on('count', (event, count) => { ipcMain.removeAllListeners('count') assert.strictEqual(count, 0) diff --git a/spec/chromium-spec.js b/spec/chromium-spec.js index 352ab6ac69..f78ab74846 100644 --- a/spec/chromium-spec.js +++ b/spec/chromium-spec.js @@ -184,7 +184,7 @@ describe('chromium feature', () => { done() }).catch((error) => done(error)) } else { - ses.clearStorageData(options, () => { + ses.clearStorageData(options).then(() => { w.webContents.reload() }) } @@ -225,7 +225,7 @@ describe('chromium feature', () => { assert.strictEqual(message, 'Hello from serviceWorker!') session.fromPartition('sw-file-scheme-spec').clearStorageData({ storages: ['serviceworkers'] - }, () => done()) + }).then(() => done()) } }) w.webContents.on('crashed', () => done(new Error('WebContents crashed.'))) @@ -264,8 +264,8 @@ describe('chromium feature', () => { assert.strictEqual(message, 'Hello from serviceWorker!') customSession.clearStorageData({ storages: ['serviceworkers'] - }, () => { - customSession.protocol.uninterceptProtocol('file', (error) => done(error)) + }).then(() => { + customSession.protocol.uninterceptProtocol('file', error => done(error)) }) } })