feat: promisify session.clearStorageData() (#17249)

This commit is contained in:
Shelley Vohr 2019-03-08 09:02:30 -08:00 коммит произвёл GitHub
Родитель d34f81972d
Коммит bbfa63fd9d
Не найден ключ, соответствующий данной подписи
Идентификатор ключа GPG: 4AEE18F83AFDEB23
7 изменённых файлов: 57 добавлений и 20 удалений

Просмотреть файл

@ -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,
@ -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);
@ -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() {

Просмотреть файл

@ -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);

Просмотреть файл

@ -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)

Просмотреть файл

@ -106,7 +106,7 @@ Callback is invoked with the session's current cache size.
Clears the sessions 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 sessions 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()`

Просмотреть файл

@ -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)

Просмотреть файл

@ -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)

Просмотреть файл

@ -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))
})
}
})