Commit b6eaeb96 authored by lfg's avatar lfg Committed by Commit bot

Moving webview cleardata function.

BUG=352293

Review URL: https://codereview.chromium.org/567643002

Cr-Commit-Position: refs/heads/master@{#294420}
parent 0f37669d
......@@ -4,13 +4,10 @@
#include "chrome/browser/extensions/api/web_view/chrome_web_view_internal_api.h"
#include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h"
#include "chrome/browser/extensions/api/context_menus/context_menus_api.h"
#include "chrome/browser/extensions/api/context_menus/context_menus_api_helpers.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/common/extensions/api/chrome_web_view_internal.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "extensions/common/error_utils.h"
namespace helpers = extensions::context_menus_api_helpers;
......@@ -18,25 +15,6 @@ namespace webview = extensions::api::chrome_web_view_internal;
namespace extensions {
namespace {
int MaskForKey(const char* key) {
if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
return 0;
}
} // namespace
// TODO(lazyboy): Add checks similar to
// WebViewInternalExtensionFunction::RunAsyncSafe(WebViewGuest*).
bool ChromeWebViewInternalContextMenusCreateFunction::RunAsync() {
......@@ -143,92 +121,6 @@ bool ChromeWebViewInternalContextMenusRemoveAllFunction::RunAsync() {
return true;
}
ChromeWebViewInternalClearDataFunction::ChromeWebViewInternalClearDataFunction()
: remove_mask_(0), bad_message_(false) {
}
ChromeWebViewInternalClearDataFunction::
~ChromeWebViewInternalClearDataFunction() {
}
// Parses the |dataToRemove| argument to generate the remove mask. Sets
// |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool
// method) if 'dataToRemove' is not present.
uint32 ChromeWebViewInternalClearDataFunction::GetRemovalMask() {
base::DictionaryValue* data_to_remove;
if (!args_->GetDictionary(2, &data_to_remove)) {
bad_message_ = true;
return 0;
}
uint32 remove_mask = 0;
for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
i.Advance()) {
bool selected = false;
if (!i.value().GetAsBoolean(&selected)) {
bad_message_ = true;
return 0;
}
if (selected)
remove_mask |= MaskForKey(i.key().c_str());
}
return remove_mask;
}
// TODO(lazyboy): Parameters in this extension function are similar (or a
// sub-set) to BrowsingDataRemoverFunction. How can we share this code?
// TODO(lf): Move ClearDataFunction to extensions
bool ChromeWebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) {
// Grab the initial |options| parameter, and parse out the arguments.
base::DictionaryValue* options;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
DCHECK(options);
// If |ms_since_epoch| isn't set, default it to 0.
double ms_since_epoch;
if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey,
&ms_since_epoch)) {
ms_since_epoch = 0;
}
// base::Time takes a double that represents seconds since epoch. JavaScript
// gives developers milliseconds, so do a quick conversion before populating
// the object. Also, Time::FromDoubleT converts double time 0 to empty Time
// object. So we need to do special handling here.
remove_since_ = (ms_since_epoch == 0)
? base::Time::UnixEpoch()
: base::Time::FromDoubleT(ms_since_epoch / 1000.0);
remove_mask_ = GetRemovalMask();
if (bad_message_)
return false;
AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done().
bool scheduled = false;
if (remove_mask_) {
scheduled = guest->ClearData(
remove_since_,
remove_mask_,
base::Bind(&ChromeWebViewInternalClearDataFunction::ClearDataDone,
this));
}
if (!remove_mask_ || !scheduled) {
SendResponse(false);
Release(); // Balanced above.
return false;
}
// Will finish asynchronously.
return true;
}
void ChromeWebViewInternalClearDataFunction::ClearDataDone() {
Release(); // Balanced in RunAsync().
SendResponse(true);
}
ChromeWebViewInternalShowContextMenuFunction::
ChromeWebViewInternalShowContextMenuFunction() {
}
......
......@@ -83,34 +83,6 @@ class ChromeWebViewInternalContextMenusRemoveAllFunction
DISALLOW_COPY_AND_ASSIGN(ChromeWebViewInternalContextMenusRemoveAllFunction);
};
class ChromeWebViewInternalClearDataFunction
: public WebViewInternalExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("chromeWebViewInternal.clearData",
WEBVIEWINTERNAL_CLEARDATA);
ChromeWebViewInternalClearDataFunction();
protected:
virtual ~ChromeWebViewInternalClearDataFunction();
private:
// WebViewInternalExtensionFunction implementation.
virtual bool RunAsyncSafe(WebViewGuest* guest) OVERRIDE;
uint32 GetRemovalMask();
void ClearDataDone();
// Removal start time.
base::Time remove_since_;
// Removal mask, corresponds to StoragePartition::RemoveDataMask enum.
uint32 remove_mask_;
// Tracks any data related or parse errors.
bool bad_message_;
DISALLOW_COPY_AND_ASSIGN(ChromeWebViewInternalClearDataFunction);
};
class ChromeWebViewInternalShowContextMenuFunction
: public WebViewInternalExtensionFunction {
public:
......
......@@ -11,43 +11,6 @@
},
"dependencies": ["contextMenusInternal"],
"types": [
{
"id": "DataTypeSet",
"type": "object",
"description": "A set of data types. Missing data types are interpreted as <code>false</code>.",
"properties": {
"appcache": {
"type": "boolean",
"optional": true,
"description": "Websites' appcaches."
},
"cookies": {
"type": "boolean",
"optional": true,
"description": "The browser's cookies."
},
"fileSystems": {
"type": "boolean",
"optional": true,
"description": "Websites' file systems."
},
"indexedDB": {
"type": "boolean",
"optional": true,
"description": "Websites' IndexedDB data."
},
"localStorage": {
"type": "boolean",
"optional": true,
"description": "Websites' local storage data."
},
"webSQL": {
"type": "boolean",
"optional": true,
"description": "Websites' WebSQL data."
}
}
},
{
"id": "ContextMenuItem",
"type": "object",
......@@ -63,18 +26,6 @@
"description": "id of the input item"
}
}
},
{
"id": "RemovalOptions",
"type": "object",
"description": "Options that determine exactly what data will be removed.",
"properties": {
"since": {
"type": "number",
"optional": true,
"description": "Remove data accumulated on or after this date, represented in milliseconds since the epoch (accessible via the <code>getTime</code> method of the JavaScript <code>Date</code> object). If absent, defaults to 0 (which would remove all browsing data)."
}
}
}
],
"functions": [
......@@ -307,34 +258,6 @@
}
]
},
{
"name": "clearData",
"type": "function",
"description": "Clears various types of browsing data stored in a storage partition of a <webview>.",
"parameters": [
{
"type": "integer",
"name": "instanceId",
"description": "The instance ID of the guest <webview> process."
},
{
"$ref": "RemovalOptions",
"name": "options"
},
{
"name": "dataToRemove",
"$ref": "DataTypeSet",
"description": "The set of data types to remove."
},
{
"name": "callback",
"type": "function",
"description": "Called when deletion has completed.",
"optional": true,
"parameters": []
}
]
},
{
"name": "showContextMenu",
"type": "function",
......
......@@ -239,7 +239,7 @@ WebViewInternal.prototype.clearData = function() {
return;
}
var args = $Array.concat([this.guestInstanceId], $Array.slice(arguments));
$Function.apply(ChromeWebView.clearData, null, args);
$Function.apply(WebView.clearData, null, args);
};
/**
......
......@@ -7,6 +7,7 @@
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/render_process_host.h"
#include "content/public/browser/render_view_host.h"
#include "content/public/browser/storage_partition.h"
#include "content/public/browser/web_contents.h"
#include "content/public/common/stop_find_action.h"
#include "extensions/common/api/web_view_internal.h"
......@@ -17,6 +18,34 @@ using extensions::core_api::web_view_internal::SetPermission::Params;
using extensions::core_api::extension_types::InjectDetails;
namespace webview = extensions::core_api::web_view_internal;
namespace {
const char kAppCacheKey[] = "appcache";
const char kCookiesKey[] = "cookies";
const char kFileSystemsKey[] = "fileSystems";
const char kIndexedDBKey[] = "indexedDB";
const char kLocalStorageKey[] = "localStorage";
const char kWebSQLKey[] = "webSQL";
const char kSinceKey[] = "since";
int MaskForKey(const char* key) {
if (strcmp(key, kAppCacheKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_APPCACHE;
if (strcmp(key, kCookiesKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_COOKIES;
if (strcmp(key, kFileSystemsKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_FILE_SYSTEMS;
if (strcmp(key, kIndexedDBKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_INDEXEDDB;
if (strcmp(key, kLocalStorageKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_LOCAL_STORAGE;
if (strcmp(key, kWebSQLKey) == 0)
return content::StoragePartition::REMOVE_DATA_MASK_WEBSQL;
return 0;
}
} // namespace
namespace extensions {
bool WebViewInternalExtensionFunction::RunAsync() {
......@@ -364,4 +393,86 @@ bool WebViewInternalTerminateFunction::RunAsyncSafe(WebViewGuest* guest) {
return true;
}
WebViewInternalClearDataFunction::WebViewInternalClearDataFunction()
: remove_mask_(0), bad_message_(false) {
}
WebViewInternalClearDataFunction::~WebViewInternalClearDataFunction() {
}
// Parses the |dataToRemove| argument to generate the remove mask. Sets
// |bad_message_| (like EXTENSION_FUNCTION_VALIDATE would if this were a bool
// method) if 'dataToRemove' is not present.
uint32 WebViewInternalClearDataFunction::GetRemovalMask() {
base::DictionaryValue* data_to_remove;
if (!args_->GetDictionary(2, &data_to_remove)) {
bad_message_ = true;
return 0;
}
uint32 remove_mask = 0;
for (base::DictionaryValue::Iterator i(*data_to_remove); !i.IsAtEnd();
i.Advance()) {
bool selected = false;
if (!i.value().GetAsBoolean(&selected)) {
bad_message_ = true;
return 0;
}
if (selected)
remove_mask |= MaskForKey(i.key().c_str());
}
return remove_mask;
}
// TODO(lazyboy): Parameters in this extension function are similar (or a
// sub-set) to BrowsingDataRemoverFunction. How can we share this code?
bool WebViewInternalClearDataFunction::RunAsyncSafe(WebViewGuest* guest) {
// Grab the initial |options| parameter, and parse out the arguments.
base::DictionaryValue* options;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(1, &options));
DCHECK(options);
// If |ms_since_epoch| isn't set, default it to 0.
double ms_since_epoch;
if (!options->GetDouble(kSinceKey, &ms_since_epoch)) {
ms_since_epoch = 0;
}
// base::Time takes a double that represents seconds since epoch. JavaScript
// gives developers milliseconds, so do a quick conversion before populating
// the object. Also, Time::FromDoubleT converts double time 0 to empty Time
// object. So we need to do special handling here.
remove_since_ = (ms_since_epoch == 0)
? base::Time::UnixEpoch()
: base::Time::FromDoubleT(ms_since_epoch / 1000.0);
remove_mask_ = GetRemovalMask();
if (bad_message_)
return false;
AddRef(); // Balanced below or in WebViewInternalClearDataFunction::Done().
bool scheduled = false;
if (remove_mask_) {
scheduled = guest->ClearData(
remove_since_,
remove_mask_,
base::Bind(&WebViewInternalClearDataFunction::ClearDataDone, this));
}
if (!remove_mask_ || !scheduled) {
SendResponse(false);
Release(); // Balanced above.
return false;
}
// Will finish asynchronously.
return true;
}
void WebViewInternalClearDataFunction::ClearDataDone() {
Release(); // Balanced in RunAsync().
SendResponse(true);
}
} // namespace extensions
......@@ -321,6 +321,34 @@ class WebViewInternalTerminateFunction
DISALLOW_COPY_AND_ASSIGN(WebViewInternalTerminateFunction);
};
class WebViewInternalClearDataFunction
: public WebViewInternalExtensionFunction {
public:
DECLARE_EXTENSION_FUNCTION("webViewInternal.clearData",
WEBVIEWINTERNAL_CLEARDATA);
WebViewInternalClearDataFunction();
protected:
virtual ~WebViewInternalClearDataFunction();
private:
// WebViewInternalExtensionFunction implementation.
virtual bool RunAsyncSafe(WebViewGuest* guest) OVERRIDE;
uint32 GetRemovalMask();
void ClearDataDone();
// Removal start time.
base::Time remove_since_;
// Removal mask, corresponds to StoragePartition::RemoveDataMask enum.
uint32 remove_mask_;
// Tracks any data related or parse errors.
bool bad_message_;
DISALLOW_COPY_AND_ASSIGN(WebViewInternalClearDataFunction);
};
} // namespace extensions
#endif // CHROME_BROWSER_EXTENSIONS_API_WEB_VIEW_WEB_VIEW_INTERNAL_API_H_
......@@ -9,6 +9,57 @@
"compiler_options": {
"implemented_in": "extensions/browser/api/web_view/web_view_internal_api.h"
},
"types": [
{
"id": "DataTypeSet",
"type": "object",
"description": "A set of data types. Missing data types are interpreted as <code>false</code>.",
"properties": {
"appcache": {
"type": "boolean",
"optional": true,
"description": "Websites' appcaches."
},
"cookies": {
"type": "boolean",
"optional": true,
"description": "The browser's cookies."
},
"fileSystems": {
"type": "boolean",
"optional": true,
"description": "Websites' file systems."
},
"indexedDB": {
"type": "boolean",
"optional": true,
"description": "Websites' IndexedDB data."
},
"localStorage": {
"type": "boolean",
"optional": true,
"description": "Websites' local storage data."
},
"webSQL": {
"type": "boolean",
"optional": true,
"description": "Websites' WebSQL data."
}
}
},
{
"id": "RemovalOptions",
"type": "object",
"description": "Options that determine exactly what data will be removed.",
"properties": {
"since": {
"type": "number",
"optional": true,
"description": "Remove data accumulated on or after this date, represented in milliseconds since the epoch (accessible via the <code>getTime</code> method of the JavaScript <code>Date</code> object). If absent, defaults to 0 (which would remove all browsing data)."
}
}
}
],
"functions": [
{
"name": "executeScript",
......@@ -362,6 +413,34 @@
"name": "instanceId"
}
]
},
{
"name": "clearData",
"type": "function",
"description": "Clears various types of browsing data stored in a storage partition of a <webview>.",
"parameters": [
{
"type": "integer",
"name": "instanceId",
"description": "The instance ID of the guest <webview> process."
},
{
"$ref": "RemovalOptions",
"name": "options"
},
{
"name": "dataToRemove",
"$ref": "DataTypeSet",
"description": "The set of data types to remove."
},
{
"name": "callback",
"type": "function",
"description": "Called when deletion has completed.",
"optional": true,
"parameters": []
}
]
}
]
}
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment