This source file includes following definitions.
- MaskForKey
- IsRemovalPermitted
- RunImpl
- SetDetails
- OnBrowsingDataRemoverDone
- RunImpl
- CheckRemovingPluginDataSupported
- StartRemoving
- ParseOriginSetMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
- GetRemovalMask
#include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h"
#include <string>
#include "base/strings/stringprintf.h"
#include "base/values.h"
#include "chrome/browser/browsing_data/browsing_data_helper.h"
#include "chrome/browser/browsing_data/browsing_data_remover.h"
#include "chrome/browser/plugins/plugin_data_remover_helper.h"
#include "chrome/browser/plugins/plugin_prefs.h"
#include "chrome/browser/profiles/profile.h"
#include "chrome/browser/ui/browser.h"
#include "chrome/common/pref_names.h"
#include "content/public/browser/browser_thread.h"
#include "extensions/common/error_utils.h"
#include "extensions/common/extension.h"
using content::BrowserThread;
namespace extension_browsing_data_api_constants {
const char kDataRemovalPermittedKey[] = "dataRemovalPermitted";
const char kDataToRemoveKey[] = "dataToRemove";
const char kOptionsKey[] = "options";
const char kAppCacheKey[] = "appcache";
const char kCacheKey[] = "cache";
const char kCookiesKey[] = "cookies";
const char kDownloadsKey[] = "downloads";
const char kFileSystemsKey[] = "fileSystems";
const char kFormDataKey[] = "formData";
const char kHistoryKey[] = "history";
const char kIndexedDBKey[] = "indexedDB";
const char kLocalStorageKey[] = "localStorage";
const char kServerBoundCertsKey[] = "serverBoundCertificates";
const char kPasswordsKey[] = "passwords";
const char kPluginDataKey[] = "pluginData";
const char kWebSQLKey[] = "webSQL";
const char kExtensionsKey[] = "extension";
const char kOriginTypesKey[] = "originTypes";
const char kProtectedWebKey[] = "protectedWeb";
const char kSinceKey[] = "since";
const char kUnprotectedWebKey[] = "unprotectedWeb";
const char kBadDataTypeDetails[] = "Invalid value for data type '%s'.";
const char kDeleteProhibitedError[] = "Browsing history and downloads are not "
"permitted to be removed.";
const char kOneAtATimeError[] = "Only one 'browsingData' API call can run at "
"a time.";
}
namespace {
int MaskForKey(const char* key) {
if (strcmp(key, extension_browsing_data_api_constants::kAppCacheKey) == 0)
return BrowsingDataRemover::REMOVE_APPCACHE;
if (strcmp(key, extension_browsing_data_api_constants::kCacheKey) == 0)
return BrowsingDataRemover::REMOVE_CACHE;
if (strcmp(key, extension_browsing_data_api_constants::kCookiesKey) == 0)
return BrowsingDataRemover::REMOVE_COOKIES;
if (strcmp(key, extension_browsing_data_api_constants::kDownloadsKey) == 0)
return BrowsingDataRemover::REMOVE_DOWNLOADS;
if (strcmp(key, extension_browsing_data_api_constants::kFileSystemsKey) == 0)
return BrowsingDataRemover::REMOVE_FILE_SYSTEMS;
if (strcmp(key, extension_browsing_data_api_constants::kFormDataKey) == 0)
return BrowsingDataRemover::REMOVE_FORM_DATA;
if (strcmp(key, extension_browsing_data_api_constants::kHistoryKey) == 0)
return BrowsingDataRemover::REMOVE_HISTORY;
if (strcmp(key, extension_browsing_data_api_constants::kIndexedDBKey) == 0)
return BrowsingDataRemover::REMOVE_INDEXEDDB;
if (strcmp(key, extension_browsing_data_api_constants::kLocalStorageKey) == 0)
return BrowsingDataRemover::REMOVE_LOCAL_STORAGE;
if (strcmp(key,
extension_browsing_data_api_constants::kServerBoundCertsKey) == 0)
return BrowsingDataRemover::REMOVE_SERVER_BOUND_CERTS;
if (strcmp(key, extension_browsing_data_api_constants::kPasswordsKey) == 0)
return BrowsingDataRemover::REMOVE_PASSWORDS;
if (strcmp(key, extension_browsing_data_api_constants::kPluginDataKey) == 0)
return BrowsingDataRemover::REMOVE_PLUGIN_DATA;
if (strcmp(key, extension_browsing_data_api_constants::kWebSQLKey) == 0)
return BrowsingDataRemover::REMOVE_WEBSQL;
return 0;
}
bool IsRemovalPermitted(int removal_mask, PrefService* prefs) {
if ((removal_mask & BrowsingDataRemover::REMOVE_HISTORY) ||
(removal_mask & BrowsingDataRemover::REMOVE_DOWNLOADS)) {
return prefs->GetBoolean(prefs::kAllowDeletingBrowserHistory);
}
return true;
}
}
bool BrowsingDataSettingsFunction::RunImpl() {
PrefService* prefs = GetProfile()->GetPrefs();
scoped_ptr<base::DictionaryValue> origin_types(new base::DictionaryValue);
origin_types->SetBoolean(
extension_browsing_data_api_constants::kUnprotectedWebKey,
prefs->GetBoolean(prefs::kDeleteCookies));
origin_types->SetBoolean(
extension_browsing_data_api_constants::kProtectedWebKey,
prefs->GetBoolean(prefs::kDeleteHostedAppsData));
origin_types->SetBoolean(
extension_browsing_data_api_constants::kExtensionsKey, false);
int period_pref = prefs->GetInteger(prefs::kDeleteTimePeriod);
BrowsingDataRemover::TimePeriod period =
static_cast<BrowsingDataRemover::TimePeriod>(period_pref);
double since = 0;
if (period != BrowsingDataRemover::EVERYTHING) {
base::Time time = BrowsingDataRemover::CalculateBeginDeleteTime(period);
since = time.ToJsTime();
}
scoped_ptr<base::DictionaryValue> options(new base::DictionaryValue);
options->Set(extension_browsing_data_api_constants::kOriginTypesKey,
origin_types.release());
options->SetDouble(extension_browsing_data_api_constants::kSinceKey, since);
scoped_ptr<base::DictionaryValue> selected(new base::DictionaryValue);
scoped_ptr<base::DictionaryValue> permitted(new base::DictionaryValue);
bool delete_site_data = prefs->GetBoolean(prefs::kDeleteCookies) ||
prefs->GetBoolean(prefs::kDeleteHostedAppsData);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kAppCacheKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kCookiesKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kFileSystemsKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kIndexedDBKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kLocalStorageKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kWebSQLKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kServerBoundCertsKey,
delete_site_data);
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kPluginDataKey,
delete_site_data && prefs->GetBoolean(prefs::kClearPluginLSODataEnabled));
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kHistoryKey,
prefs->GetBoolean(prefs::kDeleteBrowsingHistory));
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kDownloadsKey,
prefs->GetBoolean(prefs::kDeleteDownloadHistory));
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kCacheKey,
prefs->GetBoolean(prefs::kDeleteCache));
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kFormDataKey,
prefs->GetBoolean(prefs::kDeleteFormData));
SetDetails(selected.get(), permitted.get(),
extension_browsing_data_api_constants::kPasswordsKey,
prefs->GetBoolean(prefs::kDeletePasswords));
scoped_ptr<base::DictionaryValue> result(new base::DictionaryValue);
result->Set(extension_browsing_data_api_constants::kOptionsKey,
options.release());
result->Set(extension_browsing_data_api_constants::kDataToRemoveKey,
selected.release());
result->Set(extension_browsing_data_api_constants::kDataRemovalPermittedKey,
permitted.release());
SetResult(result.release());
return true;
}
void BrowsingDataSettingsFunction::SetDetails(
base::DictionaryValue* selected_dict,
base::DictionaryValue* permitted_dict,
const char* data_type,
bool is_selected) {
bool is_permitted =
IsRemovalPermitted(MaskForKey(data_type), GetProfile()->GetPrefs());
selected_dict->SetBoolean(data_type, is_selected && is_permitted);
permitted_dict->SetBoolean(data_type, is_permitted);
}
void BrowsingDataRemoverFunction::OnBrowsingDataRemoverDone() {
DCHECK_CURRENTLY_ON(BrowserThread::UI);
this->SendResponse(true);
Release();
}
bool BrowsingDataRemoverFunction::RunImpl() {
DCHECK(GetProfile());
base::DictionaryValue* options;
EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options));
DCHECK(options);
origin_set_mask_ = ParseOriginSetMask(*options);
double ms_since_epoch;
if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey,
&ms_since_epoch))
ms_since_epoch = 0;
remove_since_ = (ms_since_epoch == 0) ?
base::Time::UnixEpoch() :
base::Time::FromDoubleT(ms_since_epoch / 1000.0);
removal_mask_ = GetRemovalMask();
if (bad_message_)
return false;
if (!IsRemovalPermitted(removal_mask_, GetProfile()->GetPrefs())) {
error_ = extension_browsing_data_api_constants::kDeleteProhibitedError;
return false;
}
if (removal_mask_ & BrowsingDataRemover::REMOVE_PLUGIN_DATA) {
BrowserThread::PostTask(
BrowserThread::FILE,
FROM_HERE,
base::Bind(
&BrowsingDataRemoverFunction::CheckRemovingPluginDataSupported,
this,
PluginPrefs::GetForProfile(GetProfile())));
} else {
StartRemoving();
}
return true;
}
void BrowsingDataRemoverFunction::CheckRemovingPluginDataSupported(
scoped_refptr<PluginPrefs> plugin_prefs) {
if (!PluginDataRemoverHelper::IsSupported(plugin_prefs.get()))
removal_mask_ &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA;
BrowserThread::PostTask(
BrowserThread::UI, FROM_HERE,
base::Bind(&BrowsingDataRemoverFunction::StartRemoving, this));
}
void BrowsingDataRemoverFunction::StartRemoving() {
if (BrowsingDataRemover::is_removing()) {
error_ = extension_browsing_data_api_constants::kOneAtATimeError;
SendResponse(false);
return;
}
AddRef();
BrowsingDataRemover* remover = BrowsingDataRemover::CreateForRange(
GetProfile(), remove_since_, base::Time::Max());
remover->AddObserver(this);
remover->Remove(removal_mask_, origin_set_mask_);
}
int BrowsingDataRemoverFunction::ParseOriginSetMask(
const base::DictionaryValue& options) {
int mask = BrowsingDataHelper::UNPROTECTED_WEB;
const base::DictionaryValue* d = NULL;
if (options.HasKey(extension_browsing_data_api_constants::kOriginTypesKey)) {
EXTENSION_FUNCTION_VALIDATE(options.GetDictionary(
extension_browsing_data_api_constants::kOriginTypesKey, &d));
bool value;
mask = 0;
if (d->HasKey(extension_browsing_data_api_constants::kUnprotectedWebKey)) {
EXTENSION_FUNCTION_VALIDATE(d->GetBoolean(
extension_browsing_data_api_constants::kUnprotectedWebKey, &value));
mask |= value ? BrowsingDataHelper::UNPROTECTED_WEB : 0;
}
if (d->HasKey(extension_browsing_data_api_constants::kProtectedWebKey)) {
EXTENSION_FUNCTION_VALIDATE(d->GetBoolean(
extension_browsing_data_api_constants::kProtectedWebKey, &value));
mask |= value ? BrowsingDataHelper::PROTECTED_WEB : 0;
}
if (d->HasKey(extension_browsing_data_api_constants::kExtensionsKey)) {
EXTENSION_FUNCTION_VALIDATE(d->GetBoolean(
extension_browsing_data_api_constants::kExtensionsKey, &value));
mask |= value ? BrowsingDataHelper::EXTENSION : 0;
}
}
return mask;
}
int BrowsingDataRemoveFunction::GetRemovalMask() {
base::DictionaryValue* data_to_remove;
if (!args_->GetDictionary(1, &data_to_remove)) {
bad_message_ = true;
return 0;
}
int removal_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)
removal_mask |= MaskForKey(i.key().c_str());
}
return removal_mask;
}
int BrowsingDataRemoveAppcacheFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_APPCACHE;
}
int BrowsingDataRemoveCacheFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_CACHE;
}
int BrowsingDataRemoveCookiesFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_COOKIES |
BrowsingDataRemover::REMOVE_SERVER_BOUND_CERTS;
}
int BrowsingDataRemoveDownloadsFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_DOWNLOADS;
}
int BrowsingDataRemoveFileSystemsFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_FILE_SYSTEMS;
}
int BrowsingDataRemoveFormDataFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_FORM_DATA;
}
int BrowsingDataRemoveHistoryFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_HISTORY;
}
int BrowsingDataRemoveIndexedDBFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_INDEXEDDB;
}
int BrowsingDataRemoveLocalStorageFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_LOCAL_STORAGE;
}
int BrowsingDataRemovePluginDataFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_PLUGIN_DATA;
}
int BrowsingDataRemovePasswordsFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_PASSWORDS;
}
int BrowsingDataRemoveWebSQLFunction::GetRemovalMask() {
return BrowsingDataRemover::REMOVE_WEBSQL;
}