Skip to content

Commit

Permalink
feat: promisify session.clearStorageData() (electron#17249)
Browse files Browse the repository at this point in the history
  • Loading branch information
codebytere authored and kiku-jw committed May 16, 2019
1 parent bc720dd commit db1a7e4
Show file tree
Hide file tree
Showing 7 changed files with 57 additions and 20 deletions.
24 changes: 12 additions & 12 deletions atom/browser/api/atom_api_session.cc
Expand Up @@ -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<GURL>& url_chain,
Expand Down Expand Up @@ -429,12 +424,13 @@ void Session::DoCacheAction(const net::CompletionCallback& callback) {
action, callback));
}

void Session::ClearStorageData(mate::Arguments* args) {
// clearStorageData([options, callback])
v8::Local<v8::Promise> Session::ClearStorageData(mate::Arguments* args) {
v8::Isolate* isolate = args->isolate();
util::Promise promise(isolate);
v8::Local<v8::Promise> handle = promise.GetHandle();

ClearStorageDataOptions options;
base::Closure callback;
args->GetNext(&options);
args->GetNext(&callback);

auto* storage_partition =
content::BrowserContext::GetStoragePartition(browser_context(), nullptr);
Expand All @@ -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() {
Expand Down
2 changes: 1 addition & 1 deletion atom/browser/api/atom_api_session.h
Expand Up @@ -66,7 +66,7 @@ class Session : public mate::TrackableObject<Session>,
const ResolveProxyHelper::ResolveProxyCallback& callback);
template <CacheAction action>
void DoCacheAction(const net::CompletionCallback& callback);
void ClearStorageData(mate::Arguments* args);
v8::Local<v8::Promise> ClearStorageData(mate::Arguments* args);
void FlushStorageData();
void SetProxy(const mate::Dictionary& options, const base::Closure& callback);
void SetDownloadPath(const base::FilePath& path);
Expand Down
2 changes: 1 addition & 1 deletion docs/api/promisification.md
Expand Up @@ -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)
Expand Down Expand Up @@ -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)
Expand Down
19 changes: 17 additions & 2 deletions docs/api/session.md
Expand Up @@ -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
Expand All @@ -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<void>` - resolves when the storage data has been cleared.

#### `ses.flushStorageData()`

Expand Down
2 changes: 2 additions & 0 deletions lib/browser/api/session.js
Expand Up @@ -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)
Expand Down
20 changes: 20 additions & 0 deletions spec/api-session-spec.js
Expand Up @@ -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)
Expand Down
8 changes: 4 additions & 4 deletions spec/chromium-spec.js
Expand Up @@ -184,7 +184,7 @@ describe('chromium feature', () => {
done()
}).catch((error) => done(error))
} else {
ses.clearStorageData(options, () => {
ses.clearStorageData(options).then(() => {
w.webContents.reload()
})
}
Expand Down Expand Up @@ -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.')))
Expand Down Expand Up @@ -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))
})
}
})
Expand Down

0 comments on commit db1a7e4

Please sign in to comment.