From 868dee55de1144f77595470755eb78a4becae4f0 Mon Sep 17 00:00:00 2001 From: Cheng Zhao Date: Mon, 13 Jul 2015 15:13:17 -0700 Subject: [PATCH] The options of clearStorageData should be optional --- atom/browser/api/atom_api_session.cc | 90 +++++++++++++++++++--------- atom/browser/api/atom_api_session.h | 10 ++-- docs/api/browser-window.md | 26 ++++---- 3 files changed, 83 insertions(+), 43 deletions(-) diff --git a/atom/browser/api/atom_api_session.cc b/atom/browser/api/atom_api_session.cc index 9f8595697984..93f8de52dd45 100644 --- a/atom/browser/api/atom_api_session.cc +++ b/atom/browser/api/atom_api_session.cc @@ -5,6 +5,7 @@ #include "atom/browser/api/atom_api_session.h" #include +#include #include "atom/browser/api/atom_api_cookies.h" #include "atom/browser/atom_browser_context.h" @@ -28,49 +29,74 @@ using content::StoragePartition; namespace { -int GetStorageMask(const std::vector& storage_types) { - int storage_mask = 0; +struct ClearStorageDataOptions { + 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& storage_types) { + uint32 storage_mask = 0; + for (const auto& it : storage_types) { auto type = base::StringToLowerASCII(it); - if (type == "appcache") { + if (type == "appcache") storage_mask |= StoragePartition::REMOVE_DATA_MASK_APPCACHE; - } else if (type == "cookies") { + else if (type == "cookies") storage_mask |= StoragePartition::REMOVE_DATA_MASK_COOKIES; - } else if (type == "filesystem") { + else if (type == "filesystem") storage_mask |= StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS; - } else if (type == "indexdb") { + else if (type == "indexdb") storage_mask |= StoragePartition::REMOVE_DATA_MASK_INDEXEDDB; - } else if (type == "localstorage") { + else if (type == "localstorage") storage_mask |= StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE; - } else if (type == "shadercache") { + else if (type == "shadercache") storage_mask |= StoragePartition::REMOVE_DATA_MASK_SHADER_CACHE; - } else if (type == "websql") { + else if (type == "websql") storage_mask |= StoragePartition::REMOVE_DATA_MASK_WEBSQL; - } else if (type == "serviceworkers") { + else if (type == "serviceworkers") storage_mask |= StoragePartition::REMOVE_DATA_MASK_SERVICE_WORKERS; - } } - return storage_mask; } -int GetQuotaMask(const std::vector& quota_types) { - int quota_mask = 0; - - for (auto &type : quota_types) { - if (type == "temporary") { +uint32 GetQuotaMask(const std::vector& quota_types) { + uint32 quota_mask = 0; + for (const auto& it : quota_types) { + auto type = base::StringToLowerASCII(it); + if (type == "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; - } + else if (type == "syncable") + quota_mask |= StoragePartition::QUOTA_MANAGED_STORAGE_MASK_SYNCABLE; } - return quota_mask; } } // namespace +namespace mate { + +template<> +struct Converter { + static bool FromV8(v8::Isolate* isolate, + v8::Local val, + ClearStorageDataOptions* out) { + mate::Dictionary options; + if (!ConvertFromV8(isolate, val, &options)) + return false; + options.Get("origin", &out->origin); + std::vector 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 api { @@ -154,8 +180,12 @@ void ClearHttpCacheInIO(content::BrowserContext* browser_context, auto request_context = browser_context->GetRequestContext()->GetURLRequestContext(); 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 = base::Bind(&OnGetBackend, base::Owned(backend_ptr), callback); int rv = http_cache->GetBackend(backend_ptr, on_get_backend); @@ -184,14 +214,20 @@ void Session::ClearCache(const net::CompletionCallback& callback) { callback)); } -void Session::ClearStorageData(const GURL& origin, - const std::vector& storage_types, - const std::vector& quota_types, - const base::Closure& callback) { +void Session::ClearStorageData(mate::Arguments* args) { + // clearStorageData([options, ]callback) + ClearStorageDataOptions options; + args->GetNext(&options); + base::Closure callback; + if (!args->GetNext(&callback)) { + args->ThrowError(); + return; + } + auto storage_partition = content::BrowserContext::GetStoragePartition(browser_context_, nullptr); storage_partition->ClearData( - GetStorageMask(storage_types), GetQuotaMask(quota_types), origin, + options.storage_types, options.quota_types, options.origin, content::StoragePartition::OriginMatcherFunction(), base::Time(), base::Time::Max(), callback); } diff --git a/atom/browser/api/atom_api_session.h b/atom/browser/api/atom_api_session.h index 9b0f6b23214a..59e1a9380796 100644 --- a/atom/browser/api/atom_api_session.h +++ b/atom/browser/api/atom_api_session.h @@ -6,7 +6,6 @@ #define ATOM_BROWSER_API_ATOM_API_SESSION_H_ #include -#include #include "atom/browser/api/trackable_object.h" #include "native_mate/handle.h" @@ -14,6 +13,10 @@ class GURL; +namespace mate { +class Arguments; +} + namespace atom { class AtomBrowserContext; @@ -39,10 +42,7 @@ class Session: public mate::TrackableObject { private: void ResolveProxy(const GURL& url, ResolveProxyCallback callback); void ClearCache(const net::CompletionCallback& callback); - void ClearStorageData(const GURL& origin, - const std::vector& storage_types, - const std::vector& quota_types, - const base::Closure& callback); + void ClearStorageData(mate::Arguments* args); v8::Local Cookies(v8::Isolate* isolate); v8::Global cookies_; diff --git a/docs/api/browser-window.md b/docs/api/browser-window.md index ec6d10c7aaeb..fca705bdf759 100644 --- a/docs/api/browser-window.md +++ b/docs/api/browser-window.md @@ -1148,18 +1148,22 @@ win.webContents.on('did-finish-load', function() { * `callback` Function - function(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` -* `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 +### Session.clearStorageData([options, ]callback) -`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.