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..4d15b231fac98fede2496d197666463b5fb847d0 |
| --- /dev/null |
| +++ b/chrome/browser/extensions/extension_clear_api.cc |
| @@ -0,0 +1,145 @@ |
| +// 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) { |
|
Bernhard Bauer
2011/08/16 09:49:36
Nit: namespaces aren't indented.
Mike West
2011/08/16 11:05:56
Done.
|
| + bool value = false; |
| + if (!dict->GetBoolean(keys::kCookiesKey, &value)) |
|
Bernhard Bauer
2011/08/16 09:49:36
Shouldn't you pass |key| in here? :)
Mike West
2011/08/16 11:05:56
Done.
|
| + return false; |
| + else |
| + return value; |
| + } |
| +} |
| + |
| +void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| + DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| + result_.reset(Value::CreateBooleanValue(true)); |
| + this->SendResponse(true); |
| + |
| + Release(); // Balanced in RunImpl. |
| +} |
| + |
| +BrowsingDataRemover::TimePeriod BrowsingDataExtensionFunction::ParseTimePeriod( |
| + const std::string& period) const { |
| + BrowsingDataRemover::TimePeriod to_return; |
| + if (period == keys::kHourEnum) |
| + to_return = BrowsingDataRemover::LAST_HOUR; |
|
Bernhard Bauer
2011/08/16 09:49:36
Nit: just directly return the value?
Mike West
2011/08/16 11:05:56
I took your next bit of advice instead.
|
| + else if (period == keys::kDayEnum) |
| + to_return = BrowsingDataRemover::LAST_DAY; |
| + else if (period == keys::kWeekEnum) |
| + to_return = BrowsingDataRemover::LAST_WEEK; |
| + else if (period == keys::kMonthEnum) |
| + to_return = BrowsingDataRemover::FOUR_WEEKS; |
| + else if (period == keys::kEverythingEnum) |
| + to_return = BrowsingDataRemover::EVERYTHING; |
| + else |
| + NOTREACHED() << "Bad timeperiod: " << period; |
|
Bernhard Bauer
2011/08/16 09:49:36
I think you should return a value specifying wheth
Mike West
2011/08/16 11:05:56
Done.
|
| + |
| + return to_return; |
| +} |
| + |
| +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. |
| + 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; |
| + } |
| + |
| + // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) |
| + AddRef(); |
|
Bernhard Bauer
2011/08/16 09:49:36
Can you move this below, to after you have validat
Mike West
2011/08/16 11:05:56
Done.
|
| + |
| + // Parse the |timeframe| argument to generate the TimePeriod. |
| + std::string timeframe; |
| + EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); |
| + BrowsingDataRemover::TimePeriod period = ParseTimePeriod(timeframe); |
| + |
| + |
| + // 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* bdr(new BrowsingDataRemover( |
|
Bernhard Bauer
2011/08/16 09:49:36
Nit: Can you call it |remover| or something? Abbre
Mike West
2011/08/16 11:05:56
Done.
|
| + GetCurrentBrowser()->profile(), period, base::Time::Now())); |
| + bdr->AddObserver(this); |
| + bdr->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; |
| +} |