| OLD | NEW |
| 1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 // Defines the Chrome Extensions Clear API functions, which entail | 5 // Defines the Chrome Extensions Clear API functions, which entail |
| 6 // clearing browsing data, and clearing the browser's cache (which, let's be | 6 // clearing browsing data, and clearing the browser's cache (which, let's be |
| 7 // honest, are the same thing), as specified in the extension API JSON. | 7 // honest, are the same thing), as specified in the extension API JSON. |
| 8 | 8 |
| 9 #include "chrome/browser/extensions/extension_clear_api.h" | 9 #include "chrome/browser/extensions/extension_clear_api.h" |
| 10 | 10 |
| 11 #include <string> | 11 #include <string> |
| 12 | 12 |
| 13 #include "base/values.h" | 13 #include "base/values.h" |
| 14 #include "chrome/browser/browsing_data_remover.h" | 14 #include "chrome/browser/browsing_data_remover.h" |
| 15 #include "chrome/browser/extensions/extension_clear_api_constants.h" | |
| 16 #include "chrome/browser/plugin_data_remover_helper.h" | 15 #include "chrome/browser/plugin_data_remover_helper.h" |
| 17 #include "chrome/browser/plugin_prefs.h" | 16 #include "chrome/browser/plugin_prefs.h" |
| 18 #include "chrome/browser/profiles/profile.h" | 17 #include "chrome/browser/profiles/profile.h" |
| 19 #include "chrome/browser/ui/browser_list.h" | 18 #include "chrome/browser/ui/browser_list.h" |
| 20 #include "chrome/common/extensions/extension.h" | 19 #include "chrome/common/extensions/extension.h" |
| 21 #include "chrome/common/extensions/extension_error_utils.h" | 20 #include "chrome/common/extensions/extension_error_utils.h" |
| 22 #include "content/public/browser/browser_thread.h" | 21 #include "content/public/browser/browser_thread.h" |
| 23 | 22 |
| 24 using content::BrowserThread; | 23 using content::BrowserThread; |
| 25 | 24 |
| 26 namespace keys = extension_clear_api_constants; | 25 namespace extension_clear_api_constants { |
| 26 |
| 27 // Keys. |
| 28 const char kAppCacheKey[] = "appcache"; |
| 29 const char kCacheKey[] = "cache"; |
| 30 const char kCookiesKey[] = "cookies"; |
| 31 const char kDownloadsKey[] = "downloads"; |
| 32 const char kFileSystemsKey[] = "fileSystems"; |
| 33 const char kFormDataKey[] = "formData"; |
| 34 const char kHistoryKey[] = "history"; |
| 35 const char kIndexedDBKey[] = "indexedDB"; |
| 36 const char kLSODataKey[] = "lsoData"; |
| 37 const char kLocalStorageKey[] = "localStorage"; |
| 38 const char kPasswordsKey[] = "passwords"; |
| 39 const char kWebSQLKey[] = "webSQL"; |
| 40 |
| 41 // Timeframe "enum" values. |
| 42 const char kHourEnum[] = "last_hour"; |
| 43 const char kDayEnum[] = "last_day"; |
| 44 const char kWeekEnum[] = "last_week"; |
| 45 const char kMonthEnum[] = "last_month"; |
| 46 const char kEverythingEnum[] = "everything"; |
| 47 |
| 48 // Errors! |
| 49 const char kOneAtATimeError[] = "Only one 'clear' API call can run at a time."; |
| 50 |
| 51 } // namespace extension_clear_api_constants |
| 27 | 52 |
| 28 namespace { | 53 namespace { |
| 29 | |
| 30 // Converts the JavaScript API's string input ("last_week") into the | 54 // Converts the JavaScript API's string input ("last_week") into the |
| 31 // appropriate BrowsingDataRemover::TimePeriod (in this case, | 55 // appropriate BrowsingDataRemover::TimePeriod (in this case, |
| 32 // BrowsingDataRemover::LAST_WEEK). | 56 // BrowsingDataRemover::LAST_WEEK). |
| 33 bool ParseTimePeriod(const std::string& parse, | 57 bool ParseTimePeriod(const std::string& parse, |
| 34 BrowsingDataRemover::TimePeriod* period) { | 58 BrowsingDataRemover::TimePeriod* period) { |
| 35 if (parse == keys::kHourEnum) | 59 if (parse == extension_clear_api_constants::kHourEnum) |
| 36 *period = BrowsingDataRemover::LAST_HOUR; | 60 *period = BrowsingDataRemover::LAST_HOUR; |
| 37 else if (parse == keys::kDayEnum) | 61 else if (parse == extension_clear_api_constants::kDayEnum) |
| 38 *period = BrowsingDataRemover::LAST_DAY; | 62 *period = BrowsingDataRemover::LAST_DAY; |
| 39 else if (parse == keys::kWeekEnum) | 63 else if (parse == extension_clear_api_constants::kWeekEnum) |
| 40 *period = BrowsingDataRemover::LAST_WEEK; | 64 *period = BrowsingDataRemover::LAST_WEEK; |
| 41 else if (parse == keys::kMonthEnum) | 65 else if (parse == extension_clear_api_constants::kMonthEnum) |
| 42 *period = BrowsingDataRemover::FOUR_WEEKS; | 66 *period = BrowsingDataRemover::FOUR_WEEKS; |
| 43 else if (parse == keys::kEverythingEnum) | 67 else if (parse == extension_clear_api_constants::kEverythingEnum) |
| 44 *period = BrowsingDataRemover::EVERYTHING; | 68 *period = BrowsingDataRemover::EVERYTHING; |
| 45 else | 69 else |
| 46 return false; | 70 return false; |
| 47 | 71 |
| 48 return true; | 72 return true; |
| 49 } | 73 } |
| 50 | 74 |
| 51 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or | 75 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or |
| 52 // false, if the given key doesn't exist in the dictionary. | 76 // false, if the given key doesn't exist in the dictionary. |
| 53 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { | 77 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { |
| 54 bool value = false; | 78 bool value = false; |
| 55 if (!dict->GetBoolean(key, &value)) | 79 if (!dict->GetBoolean(key, &value)) |
| 56 return false; | 80 return false; |
| 57 else | 81 else |
| 58 return value; | 82 return value; |
| 59 } | 83 } |
| 60 | 84 |
| 61 // Convert the JavaScript API's object input ({ cookies: true }) into the | 85 // Convert the JavaScript API's object input ({ cookies: true }) into the |
| 62 // appropriate removal mask for the BrowsingDataRemover object. | 86 // appropriate removal mask for the BrowsingDataRemover object. |
| 63 int ParseRemovalMask(base::DictionaryValue* value) { | 87 int ParseRemovalMask(base::DictionaryValue* value) { |
| 64 int GetRemovalMask = 0; | 88 int GetRemovalMask = 0; |
| 65 if (DataRemovalRequested(value, keys::kCacheKey)) | 89 if (DataRemovalRequested(value, extension_clear_api_constants::kAppCacheKey)) |
| 90 GetRemovalMask |= BrowsingDataRemover::REMOVE_APPCACHE; |
| 91 if (DataRemovalRequested(value, extension_clear_api_constants::kCacheKey)) |
| 66 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; | 92 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; |
| 67 if (DataRemovalRequested(value, keys::kDownloadsKey)) | 93 if (DataRemovalRequested(value, extension_clear_api_constants::kCookiesKey)) |
| 94 GetRemovalMask |= BrowsingDataRemover::REMOVE_COOKIES; |
| 95 if (DataRemovalRequested(value, extension_clear_api_constants::kDownloadsKey)) |
| 68 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; | 96 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 69 if (DataRemovalRequested(value, keys::kFormDataKey)) | 97 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 98 GetRemovalMask |= BrowsingDataRemover::REMOVE_FILE_SYSTEMS; |
| 99 if (DataRemovalRequested(value, extension_clear_api_constants::kFormDataKey)) |
| 70 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; | 100 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; |
| 71 if (DataRemovalRequested(value, keys::kHistoryKey)) | 101 if (DataRemovalRequested(value, extension_clear_api_constants::kHistoryKey)) |
| 72 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; | 102 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; |
| 73 if (DataRemovalRequested(value, keys::kPasswordsKey)) | 103 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 104 GetRemovalMask |= BrowsingDataRemover::REMOVE_INDEXEDDB; |
| 105 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 106 GetRemovalMask |= BrowsingDataRemover::REMOVE_LOCAL_STORAGE; |
| 107 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 108 GetRemovalMask |= BrowsingDataRemover::REMOVE_LSO_DATA; |
| 109 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 74 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; | 110 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; |
| 75 | 111 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 76 // When we talk users about "cookies", we mean not just cookies, but pretty | 112 GetRemovalMask |= BrowsingDataRemover::REMOVE_WEBSQL; |
| 77 // much everything associated with an origin. | |
| 78 if (DataRemovalRequested(value, keys::kCookiesKey)) | |
| 79 GetRemovalMask |= BrowsingDataRemover::REMOVE_SITE_DATA; | |
| 80 | 113 |
| 81 return GetRemovalMask; | 114 return GetRemovalMask; |
| 82 } | 115 } |
| 83 | 116 |
| 84 } // Namespace. | 117 } // Namespace. |
| 85 | 118 |
| 86 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | 119 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 120 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 88 this->SendResponse(true); | 121 this->SendResponse(true); |
| 89 | 122 |
| 90 Release(); // Balanced in RunImpl. | 123 Release(); // Balanced in RunImpl. |
| 91 } | 124 } |
| 92 | 125 |
| 93 bool BrowsingDataExtensionFunction::RunImpl() { | 126 bool BrowsingDataExtensionFunction::RunImpl() { |
| 94 if (BrowsingDataRemover::is_removing()) { | 127 if (BrowsingDataRemover::is_removing()) { |
| 95 error_ = keys::kOneAtATimeError; | 128 error_ = extension_clear_api_constants::kOneAtATimeError; |
| 96 return false; | 129 return false; |
| 97 } | 130 } |
| 98 | 131 |
| 99 // Parse the |timeframe| argument to generate the TimePeriod. | 132 // Parse the |timeframe| argument to generate the TimePeriod. |
| 100 std::string timeframe; | 133 std::string timeframe; |
| 101 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); | 134 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); |
| 102 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period_)); | 135 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period_)); |
| 103 | 136 |
| 104 removal_mask_ = GetRemovalMask(); | 137 removal_mask_ = GetRemovalMask(); |
| 105 | 138 |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 147 | 180 |
| 148 int ClearBrowsingDataFunction::GetRemovalMask() const { | 181 int ClearBrowsingDataFunction::GetRemovalMask() const { |
| 149 // Parse the |dataToRemove| argument to generate the removal mask. | 182 // Parse the |dataToRemove| argument to generate the removal mask. |
| 150 base::DictionaryValue* data_to_remove; | 183 base::DictionaryValue* data_to_remove; |
| 151 if (args_->GetDictionary(1, &data_to_remove)) | 184 if (args_->GetDictionary(1, &data_to_remove)) |
| 152 return ParseRemovalMask(data_to_remove); | 185 return ParseRemovalMask(data_to_remove); |
| 153 else | 186 else |
| 154 return 0; | 187 return 0; |
| 155 } | 188 } |
| 156 | 189 |
| 190 int ClearAppCacheFunction::GetRemovalMask() const { |
| 191 return BrowsingDataRemover::REMOVE_APPCACHE; |
| 192 } |
| 193 |
| 157 int ClearCacheFunction::GetRemovalMask() const { | 194 int ClearCacheFunction::GetRemovalMask() const { |
| 158 return BrowsingDataRemover::REMOVE_CACHE; | 195 return BrowsingDataRemover::REMOVE_CACHE; |
| 159 } | 196 } |
| 160 | 197 |
| 161 int ClearCookiesFunction::GetRemovalMask() const { | 198 int ClearCookiesFunction::GetRemovalMask() const { |
| 162 return BrowsingDataRemover::REMOVE_SITE_DATA; | 199 return BrowsingDataRemover::REMOVE_COOKIES; |
| 163 } | 200 } |
| 164 | 201 |
| 165 int ClearDownloadsFunction::GetRemovalMask() const { | 202 int ClearDownloadsFunction::GetRemovalMask() const { |
| 166 return BrowsingDataRemover::REMOVE_DOWNLOADS; | 203 return BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 167 } | 204 } |
| 168 | 205 |
| 206 int ClearFileSystemsFunction::GetRemovalMask() const { |
| 207 return BrowsingDataRemover::REMOVE_FILE_SYSTEMS; |
| 208 } |
| 209 |
| 169 int ClearFormDataFunction::GetRemovalMask() const { | 210 int ClearFormDataFunction::GetRemovalMask() const { |
| 170 return BrowsingDataRemover::REMOVE_FORM_DATA; | 211 return BrowsingDataRemover::REMOVE_FORM_DATA; |
| 171 } | 212 } |
| 172 | 213 |
| 173 int ClearHistoryFunction::GetRemovalMask() const { | 214 int ClearHistoryFunction::GetRemovalMask() const { |
| 174 return BrowsingDataRemover::REMOVE_HISTORY; | 215 return BrowsingDataRemover::REMOVE_HISTORY; |
| 175 } | 216 } |
| 176 | 217 |
| 218 int ClearIndexedDBFunction::GetRemovalMask() const { |
| 219 return BrowsingDataRemover::REMOVE_INDEXEDDB; |
| 220 } |
| 221 |
| 222 int ClearLocalStorageFunction::GetRemovalMask() const { |
| 223 return BrowsingDataRemover::REMOVE_LOCAL_STORAGE; |
| 224 } |
| 225 |
| 226 int ClearLSODataFunction::GetRemovalMask() const { |
| 227 return BrowsingDataRemover::REMOVE_LSO_DATA; |
| 228 } |
| 229 |
| 177 int ClearPasswordsFunction::GetRemovalMask() const { | 230 int ClearPasswordsFunction::GetRemovalMask() const { |
| 178 return BrowsingDataRemover::REMOVE_CACHE; | 231 return BrowsingDataRemover::REMOVE_PASSWORDS; |
| 179 } | 232 } |
| 233 |
| 234 int ClearWebSQLFunction::GetRemovalMask() const { |
| 235 return BrowsingDataRemover::REMOVE_WEBSQL; |
| 236 } |
| OLD | NEW |