The options of clearStorageData should be optional
This commit is contained in:
parent
7dba4d1d8d
commit
868dee55de
3 changed files with 83 additions and 43 deletions
|
@ -5,6 +5,7 @@
|
||||||
#include "atom/browser/api/atom_api_session.h"
|
#include "atom/browser/api/atom_api_session.h"
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
|
#include <vector>
|
||||||
|
|
||||||
#include "atom/browser/api/atom_api_cookies.h"
|
#include "atom/browser/api/atom_api_cookies.h"
|
||||||
#include "atom/browser/atom_browser_context.h"
|
#include "atom/browser/atom_browser_context.h"
|
||||||
|
@ -28,49 +29,74 @@ using content::StoragePartition;
|
||||||
|
|
||||||
namespace {
|
namespace {
|
||||||
|
|
||||||
int GetStorageMask(const std::vector<std::string>& storage_types) {
|
struct ClearStorageDataOptions {
|
||||||
int storage_mask = 0;
|
GURL origin;
|
||||||
|
uint32 storage_types = StoragePartition::REMOVE_DATA_MASK_ALL;
|
||||||
|
uint32 quota_types = StoragePartition::QUOTA_MANAGED_STORAGE_MASK_ALL;
|
||||||
|
};
|
||||||
|
|
||||||
for (auto &it : storage_types) {
|
uint32 GetStorageMask(const std::vector<std::string>& storage_types) {
|
||||||
|
uint32 storage_mask = 0;
|
||||||
|
for (const auto& it : storage_types) {
|
||||||
auto type = base::StringToLowerASCII(it);
|
auto type = base::StringToLowerASCII(it);
|
||||||
if (type == "appcache") {
|
if (type == "appcache")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_APPCACHE;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_APPCACHE;
|
||||||
} else if (type == "cookies") {
|
else if (type == "cookies")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_COOKIES;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_COOKIES;
|
||||||
} else if (type == "filesystem") {
|
else if (type == "filesystem")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
|
||||||
} else if (type == "indexdb") {
|
else if (type == "indexdb")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
|
||||||
} else if (type == "localstorage") {
|
else if (type == "localstorage")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
|
||||||
} else if (type == "shadercache") {
|
else if (type == "shadercache")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE;
|
||||||
} else if (type == "websql") {
|
else if (type == "websql")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_WEBSQL;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_WEBSQL;
|
||||||
} else if (type == "serviceworkers") {
|
else if (type == "serviceworkers")
|
||||||
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
|
storage_mask |= StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS;
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
return storage_mask;
|
return storage_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
int GetQuotaMask(const std::vector<std::string>& quota_types) {
|
uint32 GetQuotaMask(const std::vector<std::string>& quota_types) {
|
||||||
int quota_mask = 0;
|
uint32 quota_mask = 0;
|
||||||
|
for (const auto& it : quota_types) {
|
||||||
for (auto &type : quota_types) {
|
auto type = base::StringToLowerASCII(it);
|
||||||
if (type == "temporary") {
|
if (type == "temporary")
|
||||||
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_TEMPORARY;
|
||||||
} else if (type == "persistent") {
|
else if (type == "persistent")
|
||||||
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_PERSISTENT;
|
||||||
}
|
else if (type == "syncable")
|
||||||
|
quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE;
|
||||||
}
|
}
|
||||||
|
|
||||||
return quota_mask;
|
return quota_mask;
|
||||||
}
|
}
|
||||||
|
|
||||||
} // namespace
|
} // namespace
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
|
||||||
|
template<>
|
||||||
|
struct Converter<ClearStorageDataOptions> {
|
||||||
|
static bool FromV8(v8::Isolate* isolate,
|
||||||
|
v8::Local<v8::Value> val,
|
||||||
|
ClearStorageDataOptions* out) {
|
||||||
|
mate::Dictionary options;
|
||||||
|
if (!ConvertFromV8(isolate, val, &options))
|
||||||
|
return false;
|
||||||
|
options.Get("origin", &out->origin);
|
||||||
|
std::vector<std::string> types;
|
||||||
|
if (options.Get("storages", &types))
|
||||||
|
out->storage_types = GetStorageMask(types);
|
||||||
|
if (options.Get("quotas", &types))
|
||||||
|
out->quota_types = GetQuotaMask(types);
|
||||||
|
return true;
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
} // namespace mate
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
namespace api {
|
namespace api {
|
||||||
|
@ -154,8 +180,12 @@ void ClearHttpCacheInIO(content::BrowserContext* browser_context,
|
||||||
auto request_context =
|
auto request_context =
|
||||||
browser_context->GetRequestContext()->GetURLRequestContext();
|
browser_context->GetRequestContext()->GetURLRequestContext();
|
||||||
auto http_cache = request_context->http_transaction_factory()->GetCache();
|
auto http_cache = request_context->http_transaction_factory()->GetCache();
|
||||||
|
if (!http_cache)
|
||||||
|
RunCallbackInUI(callback, net::ERR_FAILED);
|
||||||
|
|
||||||
disk_cache::Backend** backend_ptr = nullptr;
|
// Call GetBackend and make the backend's ptr accessable in OnGetBackend.
|
||||||
|
using BackendPtr = disk_cache::Backend*;
|
||||||
|
BackendPtr* backend_ptr = new BackendPtr(nullptr);
|
||||||
net::CompletionCallback on_get_backend =
|
net::CompletionCallback on_get_backend =
|
||||||
base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback);
|
base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback);
|
||||||
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
|
int rv = http_cache->GetBackend(backend_ptr, on_get_backend);
|
||||||
|
@ -184,14 +214,20 @@ void Session::ClearCache(const net::CompletionCallback& callback) {
|
||||||
callback));
|
callback));
|
||||||
}
|
}
|
||||||
|
|
||||||
void Session::ClearStorageData(const GURL& origin,
|
void Session::ClearStorageData(mate::Arguments* args) {
|
||||||
const std::vector<std::string>& storage_types,
|
// clearStorageData([options, ]callback)
|
||||||
const std::vector<std::string>& quota_types,
|
ClearStorageDataOptions options;
|
||||||
const base::Closure& callback) {
|
args->GetNext(&options);
|
||||||
|
base::Closure callback;
|
||||||
|
if (!args->GetNext(&callback)) {
|
||||||
|
args->ThrowError();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
|
||||||
auto storage_partition =
|
auto storage_partition =
|
||||||
content::BrowserContext::GetStoragePartition(browser_context_, nullptr);
|
content::BrowserContext::GetStoragePartition(browser_context_, nullptr);
|
||||||
storage_partition->ClearData(
|
storage_partition->ClearData(
|
||||||
GetStorageMask(storage_types), GetQuotaMask(quota_types), origin,
|
options.storage_types, options.quota_types, options.origin,
|
||||||
content::StoragePartition::OriginMatcherFunction(),
|
content::StoragePartition::OriginMatcherFunction(),
|
||||||
base::Time(), base::Time::Max(), callback);
|
base::Time(), base::Time::Max(), callback);
|
||||||
}
|
}
|
||||||
|
|
|
@ -6,7 +6,6 @@
|
||||||
#define ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
#define ATOM_BROWSER_API_ATOM_API_SESSION_H_
|
||||||
|
|
||||||
#include <string>
|
#include <string>
|
||||||
#include <vector>
|
|
||||||
|
|
||||||
#include "atom/browser/api/trackable_object.h"
|
#include "atom/browser/api/trackable_object.h"
|
||||||
#include "native_mate/handle.h"
|
#include "native_mate/handle.h"
|
||||||
|
@ -14,6 +13,10 @@
|
||||||
|
|
||||||
class GURL;
|
class GURL;
|
||||||
|
|
||||||
|
namespace mate {
|
||||||
|
class Arguments;
|
||||||
|
}
|
||||||
|
|
||||||
namespace atom {
|
namespace atom {
|
||||||
|
|
||||||
class AtomBrowserContext;
|
class AtomBrowserContext;
|
||||||
|
@ -39,10 +42,7 @@ class Session: public mate::TrackableObject<Session> {
|
||||||
private:
|
private:
|
||||||
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
void ResolveProxy(const GURL& url, ResolveProxyCallback callback);
|
||||||
void ClearCache(const net::CompletionCallback& callback);
|
void ClearCache(const net::CompletionCallback& callback);
|
||||||
void ClearStorageData(const GURL& origin,
|
void ClearStorageData(mate::Arguments* args);
|
||||||
const std::vector<std::string>& storage_types,
|
|
||||||
const std::vector<std::string>& quota_types,
|
|
||||||
const base::Closure& callback);
|
|
||||||
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
v8::Local<v8::Value> Cookies(v8::Isolate* isolate);
|
||||||
|
|
||||||
v8::Global<v8::Value> cookies_;
|
v8::Global<v8::Value> cookies_;
|
||||||
|
|
|
@ -1148,18 +1148,22 @@ win.webContents.on('did-finish-load', function() {
|
||||||
* `callback` Function - function(error)
|
* `callback` Function - function(error)
|
||||||
* `error` Error
|
* `error` Error
|
||||||
|
|
||||||
### Session.clearCache()
|
### Session.clearCache(callback)
|
||||||
|
|
||||||
Clears the session's http cache.
|
* `callback` Function - Called when operation is done
|
||||||
|
|
||||||
### Session.clearStorageData(origin, storageType, quotaType, callback)
|
Clears the session's HTTP cache.
|
||||||
|
|
||||||
* `origin` String - should follow `window.location.origin` representation `scheme`://`host`:`port`
|
### Session.clearStorageData([options, ]callback)
|
||||||
* `storageType` Array - specifies the type of storage, can contain -
|
|
||||||
`appcache`, `cookies`, `filesystem`, `indexdb`, `localstorage`, `shadercache`, `websql`,
|
|
||||||
`serviceworkers`
|
|
||||||
* `quotaType` Array - specifies the storage quota type, can contain -
|
|
||||||
`temporary`, `persistent`
|
|
||||||
* `callback` Function
|
|
||||||
|
|
||||||
`callback` is invoked when the deletion process is scheduled.
|
* `options` Object
|
||||||
|
* `origin` String - Should follow `window.location.origin`'s representation
|
||||||
|
`scheme://host:port`
|
||||||
|
* `storages` Array - The types of storages to clear, can contain:
|
||||||
|
`appcache`, `cookies`, `filesystem`, `indexdb`, `localstorage`,
|
||||||
|
`shadercache`, `websql`, `serviceworkers`
|
||||||
|
* `quotas` Array - The types of quotas to clear, can contain:
|
||||||
|
`temporary`, `persistent`, `syncable`
|
||||||
|
* `callback` Function - Called when operation is done
|
||||||
|
|
||||||
|
Clears the data of web storages.
|
||||||
|
|
Loading…
Reference in a new issue