Chromium Code Reviews| Index: chrome/browser/extensions/extension_clear_api.cc |
| diff --git a/chrome/browser/extensions/extension_clear_api.cc b/chrome/browser/extensions/extension_clear_api.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..e8aabb764f46de45d08f29f2a57cbe26dd47a641 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_clear_api.cc |
| @@ -0,0 +1,146 @@ |
| +// Copyright (c) 2011 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +// Defines the Chrome Extensions Clear API functions, which entail |
| +// clearing browsing data, and clearing the browser's cache (which, let's be |
| +// honest, are the same thing), as specified in |
| +// chrome/common/extensions/api/extension_api.json. |
| + |
| +#include "chrome/browser/extensions/extension_clear_api.h" |
| + |
| +#include <string> |
| + |
| +#include "base/values.h" |
| +#include "chrome/browser/browsing_data_remover.h" |
| +#include "chrome/browser/extensions/extension_clear_api_constants.h" |
| +#include "chrome/browser/profiles/profile.h" |
| +#include "chrome/browser/ui/browser_list.h" |
| +#include "chrome/common/extensions/extension.h" |
| +#include "chrome/common/extensions/extension_error_utils.h" |
| +#include "content/browser/browser_thread.h" |
| + |
| +namespace keys = extension_clear_api_constants; |
| + |
| +namespace { |
| + |
| +bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { |
| + bool value = false; |
| + if (!dict->GetBoolean(key, &value)) |
| + return false; |
| + else |
| + return value; |
| +} |
| + |
| +} // Namespace. |
| + |
| +void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + result_.reset(Value::CreateBooleanValue(true)); |
| + this->SendResponse(true); |
| + |
| + Release(); // Balanced in RunImpl. |
| +} |
| + |
| +bool BrowsingDataExtensionFunction::ParseTimePeriod( |
| + const std::string& parse, BrowsingDataRemover::TimePeriod* period) const { |
| + if (parse == keys::kHourEnum) |
| + *period = BrowsingDataRemover::LAST_HOUR; |
| + else if (parse == keys::kDayEnum) |
| + *period = BrowsingDataRemover::LAST_DAY; |
| + else if (parse == keys::kWeekEnum) |
| + *period = BrowsingDataRemover::LAST_WEEK; |
| + else if (parse == keys::kMonthEnum) |
| + *period = BrowsingDataRemover::FOUR_WEEKS; |
| + else if (parse == keys::kEverythingEnum) |
| + *period = BrowsingDataRemover::EVERYTHING; |
| + else |
| + return false; |
| + |
| + return true; |
| +} |
| + |
| +int BrowsingDataExtensionFunction::ParseRemovalMask( |
| + base::DictionaryValue* value) const { |
| + int removal_mask = 0; |
| + if (DataRemovalRequested(value, keys::kCacheKey)) |
| + removal_mask |= BrowsingDataRemover::REMOVE_CACHE; |
| + if (DataRemovalRequested(value, keys::kDownloadsKey)) |
| + removal_mask |= BrowsingDataRemover::REMOVE_DOWNLOADS; |
| + if (DataRemovalRequested(value, keys::kFormDataKey)) |
| + removal_mask |= BrowsingDataRemover::REMOVE_FORM_DATA; |
| + if (DataRemovalRequested(value, keys::kHistoryKey)) |
| + removal_mask |= BrowsingDataRemover::REMOVE_HISTORY; |
| + if (DataRemovalRequested(value, keys::kPasswordsKey)) |
| + removal_mask |= BrowsingDataRemover::REMOVE_PASSWORDS; |
| + |
| + // "Cookies" are cookies + site data + LSO. |
|
Mihai Parparita -not on Chrome
2011/08/23 23:24:13
I'm not seeing a mask value corresponding to "site
Mike West
2011/08/24 09:10:33
You're right, this was poorly phrased. I've update
|
| + if (DataRemovalRequested(value, keys::kCookiesKey)) { |
| + removal_mask |= BrowsingDataRemover::REMOVE_COOKIES; |
| + removal_mask |= BrowsingDataRemover::REMOVE_LSO_DATA; |
| + } |
| + |
| + return removal_mask; |
| +} |
| + |
| +bool BrowsingDataExtensionFunction::RunImpl() { |
| + if (BrowsingDataRemover::is_removing()) { |
| + error_ = keys::kOneAtATimeError; |
| + return false; |
| + } |
| + |
| + // Parse the |timeframe| argument to generate the TimePeriod. |
| + std::string timeframe; |
| + BrowsingDataRemover::TimePeriod period; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); |
| + EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period)); |
| + |
| + // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) |
| + AddRef(); |
| + |
| + // Create a BrowsingDataRemover, set the current object as an observer (so |
| + // that we're notified after removal) and call remove() with the arguments |
| + // we've generated above. We can use a raw pointer here, as the browsing data |
| + // remover is responsible for deleting itself once data removal is complete. |
| + BrowsingDataRemover* remover = new BrowsingDataRemover( |
| + GetCurrentBrowser()->profile(), period, base::Time::Now()); |
| + remover->AddObserver(this); |
| + remover->Remove(removal_mask()); |
| + |
| + // Will finish asynchronously. |
| + return true; |
| +} |
| + |
| +int ClearBrowsingDataFunction::removal_mask() const { |
| + // Parse the |dataToRemove| argument to generate the removal mask. |
| + base::DictionaryValue* data_to_remove; |
| + if (args_->GetDictionary(1, &data_to_remove)) |
| + return ParseRemovalMask(data_to_remove); |
| + else |
| + return 0; |
| +} |
| + |
| +int ClearCacheFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_CACHE; |
| +} |
| + |
| +int ClearCookiesFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_COOKIES | |
| + BrowsingDataRemover::REMOVE_LSO_DATA; |
| +} |
| + |
| +int ClearDownloadsFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_DOWNLOADS; |
| +} |
| + |
| +int ClearFormDataFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_FORM_DATA; |
| +} |
| + |
| +int ClearHistoryFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_HISTORY; |
| +} |
| + |
| +int ClearPasswordsFunction::removal_mask() const { |
| + return BrowsingDataRemover::REMOVE_CACHE; |
| +} |