Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | |
| 2 // Use of this source code is governed by a BSD-style license that can be | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Defines the Chrome Extensions Clear API functions, which entail | |
| 6 // clearing browsing data, and clearing the browser's cache (which, let's be | |
| 7 // honest, are the same thing), as specified in | |
| 8 // chrome/common/extensions/api/extension_api.json. | |
| 9 | |
| 10 #include "chrome/browser/extensions/extension_clear_api.h" | |
| 11 | |
| 12 #include <string> | |
| 13 | |
| 14 #include "base/values.h" | |
| 15 #include "chrome/browser/browsing_data_remover.h" | |
| 16 #include "chrome/browser/extensions/extension_clear_api_constants.h" | |
| 17 #include "chrome/browser/profiles/profile.h" | |
| 18 #include "chrome/browser/ui/browser_list.h" | |
| 19 #include "chrome/common/extensions/extension.h" | |
| 20 #include "chrome/common/extensions/extension_error_utils.h" | |
| 21 #include "content/browser/browser_thread.h" | |
| 22 | |
| 23 namespace keys = extension_clear_api_constants; | |
| 24 | |
| 25 namespace { | |
| 26 | |
| 27 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { | |
| 28 bool value = false; | |
| 29 if (!dict->GetBoolean(key, &value)) | |
| 30 return false; | |
| 31 else | |
| 32 return value; | |
| 33 } | |
| 34 | |
| 35 } // Namespace. | |
| 36 | |
| 37 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 39 result_.reset(Value::CreateBooleanValue(true)); | |
| 40 this->SendResponse(true); | |
| 41 | |
| 42 Release(); // Balanced in RunImpl. | |
| 43 } | |
| 44 | |
| 45 bool BrowsingDataExtensionFunction::ParseTimePeriod( | |
| 46 const std::string& parse, BrowsingDataRemover::TimePeriod* period) const { | |
| 47 if (parse == keys::kHourEnum) | |
| 48 *period = BrowsingDataRemover::LAST_HOUR; | |
| 49 else if (parse == keys::kDayEnum) | |
| 50 *period = BrowsingDataRemover::LAST_DAY; | |
| 51 else if (parse == keys::kWeekEnum) | |
| 52 *period = BrowsingDataRemover::LAST_WEEK; | |
| 53 else if (parse == keys::kMonthEnum) | |
| 54 *period = BrowsingDataRemover::FOUR_WEEKS; | |
| 55 else if (parse == keys::kEverythingEnum) | |
| 56 *period = BrowsingDataRemover::EVERYTHING; | |
| 57 else | |
| 58 return false; | |
| 59 | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 bool BrowsingDataExtensionFunction::RunImpl() { | |
| 64 if (BrowsingDataRemover::is_removing()) { | |
| 65 error_ = keys::kOneAtATimeError; | |
| 66 return false; | |
| 67 } | |
| 68 | |
| 69 // Parse the |timeframe| argument to generate the TimePeriod. | |
| 70 std::string timeframe; | |
| 71 BrowsingDataRemover::TimePeriod period; | |
| 72 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); | |
| 73 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period)); | |
| 74 | |
| 75 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) | |
| 76 AddRef(); | |
| 77 | |
| 78 // Create a BrowsingDataRemover, set the current object as an observer (so | |
| 79 // that we're notified after removal) and call remove() with the arguments | |
| 80 // we've generated above. We can use a raw pointer here, as the browsing data | |
| 81 // remover is responsible for deleting itself once data removal is complete. | |
| 82 BrowsingDataRemover* remover = new BrowsingDataRemover( | |
| 83 GetCurrentBrowser()->profile(), period, base::Time::Now()); | |
| 84 remover->AddObserver(this); | |
| 85 remover->Remove(GetRemovalMask()); | |
| 86 | |
| 87 // Will finish asynchronously. | |
| 88 return true; | |
| 89 } | |
| 90 | |
| 91 int ClearBrowsingDataFunction::ParseRemovalMask( | |
|
Mihai Parparita -not on Chrome
2011/08/24 17:20:00
Nit: this can actually be static in this file.
Mike West
2011/08/25 09:27:54
You're right. In fact, I'll go a step further.
I'
| |
| 92 base::DictionaryValue* value) const { | |
| 93 int GetRemovalMask = 0; | |
| 94 if (DataRemovalRequested(value, keys::kCacheKey)) | |
| 95 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; | |
| 96 if (DataRemovalRequested(value, keys::kDownloadsKey)) | |
| 97 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 98 if (DataRemovalRequested(value, keys::kFormDataKey)) | |
| 99 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 100 if (DataRemovalRequested(value, keys::kHistoryKey)) | |
| 101 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; | |
| 102 if (DataRemovalRequested(value, keys::kPasswordsKey)) | |
| 103 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; | |
| 104 | |
| 105 // When we talk users about "cookies", we mean not just cookies, but pretty | |
| 106 // much everything associated with an origin. To that end, we explicitly | |
| 107 // include the REMOVE_LSO_DATA mask here, and BrowsingDataRemover interprets | |
| 108 // the REMOVE_COOKIES mask internally as "cookies and site data". | |
| 109 if (DataRemovalRequested(value, keys::kCookiesKey)) { | |
| 110 GetRemovalMask |= BrowsingDataRemover::REMOVE_COOKIES; | |
| 111 GetRemovalMask |= BrowsingDataRemover::REMOVE_LSO_DATA; | |
| 112 } | |
| 113 | |
| 114 return GetRemovalMask; | |
| 115 } | |
| 116 | |
| 117 int ClearBrowsingDataFunction::GetRemovalMask() const { | |
| 118 // Parse the |dataToRemove| argument to generate the removal mask. | |
| 119 base::DictionaryValue* data_to_remove; | |
| 120 if (args_->GetDictionary(1, &data_to_remove)) | |
| 121 return ParseRemovalMask(data_to_remove); | |
| 122 else | |
| 123 return 0; | |
| 124 } | |
| 125 | |
| 126 int ClearCacheFunction::GetRemovalMask() const { | |
| 127 return BrowsingDataRemover::REMOVE_CACHE; | |
| 128 } | |
| 129 | |
| 130 int ClearCookiesFunction::GetRemovalMask() const { | |
| 131 return BrowsingDataRemover::REMOVE_COOKIES | | |
| 132 BrowsingDataRemover::REMOVE_LSO_DATA; | |
| 133 } | |
| 134 | |
| 135 int ClearDownloadsFunction::GetRemovalMask() const { | |
| 136 return BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 137 } | |
| 138 | |
| 139 int ClearFormDataFunction::GetRemovalMask() const { | |
| 140 return BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 141 } | |
| 142 | |
| 143 int ClearHistoryFunction::GetRemovalMask() const { | |
| 144 return BrowsingDataRemover::REMOVE_HISTORY; | |
| 145 } | |
| 146 | |
| 147 int ClearPasswordsFunction::GetRemovalMask() const { | |
| 148 return BrowsingDataRemover::REMOVE_CACHE; | |
| 149 } | |
| OLD | NEW |