Skip to content

Commit bbfa63f

Browse files
authored
feat: promisify session.clearStorageData() (electron#17249)
1 parent d34f819 commit bbfa63f

File tree

7 files changed

+57
-20
lines changed

7 files changed

+57
-20
lines changed

atom/browser/api/atom_api_session.cc

Lines changed: 12 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -328,11 +328,6 @@ void AllowNTLMCredentialsForDomainsInIO(
328328
}
329329
}
330330

331-
void OnClearStorageDataDone(const base::Closure& callback) {
332-
if (!callback.is_null())
333-
callback.Run();
334-
}
335-
336331
void DownloadIdCallback(content::DownloadManager* download_manager,
337332
const base::FilePath& path,
338333
const std::vector<GURL>& url_chain,
@@ -429,12 +424,13 @@ void Session::DoCacheAction(const net::CompletionCallback& callback) {
429424
action, callback));
430425
}
431426

432-
void Session::ClearStorageData(mate::Arguments* args) {
433-
// clearStorageData([options, callback])
427+
v8::Local<v8::Promise> Session::ClearStorageData(mate::Arguments* args) {
428+
v8::Isolate* isolate = args->isolate();
429+
util::Promise promise(isolate);
430+
v8::Local<v8::Promise> handle = promise.GetHandle();
431+
434432
ClearStorageDataOptions options;
435-
base::Closure callback;
436433
args->GetNext(&options);
437-
args->GetNext(&callback);
438434

439435
auto* storage_partition =
440436
content::BrowserContext::GetStoragePartition(browser_context(), nullptr);
@@ -443,9 +439,13 @@ void Session::ClearStorageData(mate::Arguments* args) {
443439
// https://w3c.github.io/mediacapture-main/#dom-mediadeviceinfo-deviceid
444440
MediaDeviceIDSalt::Reset(browser_context()->prefs());
445441
}
446-
storage_partition->ClearData(options.storage_types, options.quota_types,
447-
options.origin, base::Time(), base::Time::Max(),
448-
base::Bind(&OnClearStorageDataDone, callback));
442+
443+
storage_partition->ClearData(
444+
options.storage_types, options.quota_types, options.origin, base::Time(),
445+
base::Time::Max(),
446+
base::Bind(util::CopyablePromise::ResolveEmptyCopyablePromise,
447+
util::CopyablePromise(promise)));
448+
return handle;
449449
}
450450

451451
void Session::FlushStorageData() {

atom/browser/api/atom_api_session.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -66,7 +66,7 @@ class Session : public mate::TrackableObject<Session>,
6666
const ResolveProxyHelper::ResolveProxyCallback& callback);
6767
template <CacheAction action>
6868
void DoCacheAction(const net::CompletionCallback& callback);
69-
void ClearStorageData(mate::Arguments* args);
69+
v8::Local<v8::Promise> ClearStorageData(mate::Arguments* args);
7070
void FlushStorageData();
7171
void SetProxy(const mate::Dictionary& options, const base::Closure& callback);
7272
void SetDownloadPath(const base::FilePath& path);

docs/api/promisification.md

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -15,7 +15,6 @@ When a majority of affected functions are migrated, this flag will be enabled by
1515
- [inAppPurchase.getProducts(productIDs, callback)](https://github.com/electron/electron/blob/master/docs/api/in-app-purchase.md#getProducts)
1616
- [ses.getCacheSize(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#getCacheSize)
1717
- [ses.clearCache(callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#clearCache)
18-
- [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData)
1918
- [ses.setProxy(config, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#setProxy)
2019
- [ses.resolveProxy(url, callback)](https://github.com/electron/electron/blob/master/docs/api/session.md#resolveProxy)
2120
- [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
4746
- [dialog.showSaveDialog([browserWindow, ]options[, callback])](https://github.com/electron/electron/blob/master/docs/api/dialog.md#showSaveDialog)
4847
- [netLog.stopLogging([callback])](https://github.com/electron/electron/blob/master/docs/api/net-log.md#stopLogging)
4948
- [protocol.isProtocolHandled(scheme, callback)](https://github.com/electron/electron/blob/master/docs/api/protocol.md#isProtocolHandled)
49+
- [ses.clearStorageData([options, callback])](https://github.com/electron/electron/blob/master/docs/api/session.md#clearStorageData)
5050
- [shell.openExternal(url[, options, callback])](https://github.com/electron/electron/blob/master/docs/api/shell.md#openExternal)
5151
- [webviewTag.capturePage([rect, ]callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#capturePage)
5252
- [webviewTag.printToPDF(options, callback)](https://github.com/electron/electron/blob/master/docs/api/webview-tag.md#printToPDF)

docs/api/session.md

Lines changed: 17 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -106,7 +106,7 @@ Callback is invoked with the session's current cache size.
106106

107107
Clears the session’s HTTP cache.
108108

109-
#### `ses.clearStorageData([options, callback])`
109+
#### `ses.clearStorageData([options,] callback)`
110110

111111
* `options` Object (optional)
112112
* `origin` String (optional) - Should follow `window.location.origin`’s representation
@@ -118,7 +118,22 @@ Clears the session’s HTTP cache.
118118
`temporary`, `persistent`, `syncable`.
119119
* `callback` Function (optional) - Called when operation is done.
120120

121-
Clears the data of web storages.
121+
Clears the storage data for the current session.
122+
123+
**[Deprecated Soon](promisification.md)**
124+
125+
#### `ses.clearStorageData([options])`
126+
127+
* `options` Object (optional)
128+
* `origin` String (optional) - Should follow `window.location.origin`’s representation
129+
`scheme://host:port`.
130+
* `storages` String[] (optional) - The types of storages to clear, can contain:
131+
`appcache`, `cookies`, `filesystem`, `indexdb`, `localstorage`,
132+
`shadercache`, `websql`, `serviceworkers`, `cachestorage`.
133+
* `quotas` String[] (optional) - The types of quotas to clear, can contain:
134+
`temporary`, `persistent`, `syncable`.
135+
136+
Returns `Promise<void>` - resolves when the storage data has been cleared.
122137

123138
#### `ses.flushStorageData()`
124139

lib/browser/api/session.js

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,8 @@ Session.prototype._init = function () {
2323
app.emit('session-created', this)
2424
}
2525

26+
Session.prototype.clearStorageData = deprecate.promisify(Session.prototype.clearStorageData)
27+
2628
Cookies.prototype.flushStore = deprecate.promisify(Cookies.prototype.flushStore)
2729
Cookies.prototype.get = deprecate.promisify(Cookies.prototype.get)
2830
Cookies.prototype.remove = deprecate.promisify(Cookies.prototype.remove)

spec/api-session-spec.js

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -324,6 +324,26 @@ describe('session module', () => {
324324
describe('ses.clearStorageData(options)', () => {
325325
fixtures = path.resolve(__dirname, 'fixtures')
326326
it('clears localstorage data', (done) => {
327+
ipcMain.on('count', (event, count) => {
328+
ipcMain.removeAllListeners('count')
329+
assert.strictEqual(count, 0)
330+
done()
331+
})
332+
w.webContents.on('did-finish-load', () => {
333+
const options = {
334+
origin: 'file://',
335+
storages: ['localstorage'],
336+
quotas: ['persistent']
337+
}
338+
w.webContents.session.clearStorageData(options).then(() => {
339+
w.webContents.send('getcount')
340+
})
341+
})
342+
w.loadFile(path.join(fixtures, 'api', 'localstorage.html'))
343+
})
344+
345+
// TODO(codebytere): remove when promisification is complete
346+
it('clears localstorage data (callback)', (done) => {
327347
ipcMain.on('count', (event, count) => {
328348
ipcMain.removeAllListeners('count')
329349
assert.strictEqual(count, 0)

spec/chromium-spec.js

Lines changed: 4 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -184,7 +184,7 @@ describe('chromium feature', () => {
184184
done()
185185
}).catch((error) => done(error))
186186
} else {
187-
ses.clearStorageData(options, () => {
187+
ses.clearStorageData(options).then(() => {
188188
w.webContents.reload()
189189
})
190190
}
@@ -225,7 +225,7 @@ describe('chromium feature', () => {
225225
assert.strictEqual(message, 'Hello from serviceWorker!')
226226
session.fromPartition('sw-file-scheme-spec').clearStorageData({
227227
storages: ['serviceworkers']
228-
}, () => done())
228+
}).then(() => done())
229229
}
230230
})
231231
w.webContents.on('crashed', () => done(new Error('WebContents crashed.')))
@@ -264,8 +264,8 @@ describe('chromium feature', () => {
264264
assert.strictEqual(message, 'Hello from serviceWorker!')
265265
customSession.clearStorageData({
266266
storages: ['serviceworkers']
267-
}, () => {
268-
customSession.protocol.uninterceptProtocol('file', (error) => done(error))
267+
}).then(() => {
268+
customSession.protocol.uninterceptProtocol('file', error => done(error))
269269
})
270270
}
271271
})

0 commit comments

Comments
 (0)