| 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 kPluginDataKey[] = "pluginData"; |
| 37 const char kLocalStorageKey[] = "localStorage"; |
| 38 const char kPasswordsKey[] = "passwords"; |
| 39 const char kWebSQLKey[] = "webSQL"; |
| 40 |
| 41 // Errors! |
| 42 const char kOneAtATimeError[] = "Only one 'clear' API call can run at a time."; |
| 43 |
| 44 } // namespace extension_clear_api_constants |
| 27 | 45 |
| 28 namespace { | 46 namespace { |
| 29 | 47 // Converts the JavaScript API's numeric input (miliseconds since epoch) into an |
| 30 // Converts the JavaScript API's string input ("last_week") into the | 48 // appropriate base::Time that we can pass into the BrowsingDataRemove. |
| 31 // appropriate BrowsingDataRemover::TimePeriod (in this case, | 49 bool ParseTimeFromValue(const double& ms_since_epoch, base::Time* time) { |
| 32 // BrowsingDataRemover::LAST_WEEK). | |
| 33 bool ParseTimePeriod(const std::string& parse, | |
| 34 BrowsingDataRemover::TimePeriod* period) { | |
| 35 if (parse == keys::kHourEnum) | |
| 36 *period = BrowsingDataRemover::LAST_HOUR; | |
| 37 else if (parse == keys::kDayEnum) | |
| 38 *period = BrowsingDataRemover::LAST_DAY; | |
| 39 else if (parse == keys::kWeekEnum) | |
| 40 *period = BrowsingDataRemover::LAST_WEEK; | |
| 41 else if (parse == keys::kMonthEnum) | |
| 42 *period = BrowsingDataRemover::FOUR_WEEKS; | |
| 43 else if (parse == keys::kEverythingEnum) | |
| 44 *period = BrowsingDataRemover::EVERYTHING; | |
| 45 else | |
| 46 return false; | |
| 47 | |
| 48 return true; | 50 return true; |
| 49 } | 51 } |
| 50 | 52 |
| 51 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or | 53 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or |
| 52 // false, if the given key doesn't exist in the dictionary. | 54 // false, if the given key doesn't exist in the dictionary. |
| 53 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { | 55 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { |
| 54 bool value = false; | 56 bool value = false; |
| 55 if (!dict->GetBoolean(key, &value)) | 57 if (!dict->GetBoolean(key, &value)) |
| 56 return false; | 58 return false; |
| 57 else | 59 else |
| 58 return value; | 60 return value; |
| 59 } | 61 } |
| 60 | 62 |
| 61 // Convert the JavaScript API's object input ({ cookies: true }) into the | 63 // Convert the JavaScript API's object input ({ cookies: true }) into the |
| 62 // appropriate removal mask for the BrowsingDataRemover object. | 64 // appropriate removal mask for the BrowsingDataRemover object. |
| 63 int ParseRemovalMask(base::DictionaryValue* value) { | 65 int ParseRemovalMask(base::DictionaryValue* value) { |
| 64 int GetRemovalMask = 0; | 66 int GetRemovalMask = 0; |
| 65 if (DataRemovalRequested(value, keys::kCacheKey)) | 67 if (DataRemovalRequested(value, extension_clear_api_constants::kAppCacheKey)) |
| 68 GetRemovalMask |= BrowsingDataRemover::REMOVE_APPCACHE; |
| 69 if (DataRemovalRequested(value, extension_clear_api_constants::kCacheKey)) |
| 66 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; | 70 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; |
| 67 if (DataRemovalRequested(value, keys::kDownloadsKey)) | 71 if (DataRemovalRequested(value, extension_clear_api_constants::kCookiesKey)) |
| 72 GetRemovalMask |= BrowsingDataRemover::REMOVE_COOKIES; |
| 73 if (DataRemovalRequested(value, extension_clear_api_constants::kDownloadsKey)) |
| 68 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; | 74 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 69 if (DataRemovalRequested(value, keys::kFormDataKey)) | 75 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 76 GetRemovalMask |= BrowsingDataRemover::REMOVE_FILE_SYSTEMS; |
| 77 if (DataRemovalRequested(value, extension_clear_api_constants::kFormDataKey)) |
| 70 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; | 78 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; |
| 71 if (DataRemovalRequested(value, keys::kHistoryKey)) | 79 if (DataRemovalRequested(value, extension_clear_api_constants::kHistoryKey)) |
| 72 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; | 80 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; |
| 73 if (DataRemovalRequested(value, keys::kPasswordsKey)) | 81 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 82 GetRemovalMask |= BrowsingDataRemover::REMOVE_INDEXEDDB; |
| 83 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 84 GetRemovalMask |= BrowsingDataRemover::REMOVE_LOCAL_STORAGE; |
| 85 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 86 GetRemovalMask |= BrowsingDataRemover::REMOVE_PLUGIN_DATA; |
| 87 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 74 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; | 88 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; |
| 75 | 89 if (DataRemovalRequested(value, extension_clear_api_constants::kPasswordsKey)) |
| 76 // When we talk users about "cookies", we mean not just cookies, but pretty | 90 GetRemovalMask |= BrowsingDataRemover::REMOVE_WEBSQL; |
| 77 // much everything associated with an origin. | |
| 78 if (DataRemovalRequested(value, keys::kCookiesKey)) | |
| 79 GetRemovalMask |= BrowsingDataRemover::REMOVE_SITE_DATA; | |
| 80 | 91 |
| 81 return GetRemovalMask; | 92 return GetRemovalMask; |
| 82 } | 93 } |
| 83 | 94 |
| 84 } // Namespace. | 95 } // Namespace. |
| 85 | 96 |
| 86 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | 97 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| 87 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 98 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 88 this->SendResponse(true); | 99 this->SendResponse(true); |
| 89 | 100 |
| 90 Release(); // Balanced in RunImpl. | 101 Release(); // Balanced in RunImpl. |
| 91 } | 102 } |
| 92 | 103 |
| 93 bool BrowsingDataExtensionFunction::RunImpl() { | 104 bool BrowsingDataExtensionFunction::RunImpl() { |
| 94 if (BrowsingDataRemover::is_removing()) { | 105 if (BrowsingDataRemover::is_removing()) { |
| 95 error_ = keys::kOneAtATimeError; | 106 error_ = extension_clear_api_constants::kOneAtATimeError; |
| 96 return false; | 107 return false; |
| 97 } | 108 } |
| 98 | 109 |
| 99 // Parse the |timeframe| argument to generate the TimePeriod. | 110 double ms_since_epoch; |
| 100 std::string timeframe; | 111 EXTENSION_FUNCTION_VALIDATE(args_->GetDouble(0, &ms_since_epoch)); |
| 101 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); | 112 // base::Time takes a double that represents seconds since epoch. JavaScript |
| 102 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period_)); | 113 // gives developers milliseconds, so do a quick conversion before populating |
| 114 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time |
| 115 // object. So we need to do special handling here. |
| 116 remove_since_ = (ms_since_epoch == 0) ? |
| 117 base::Time::UnixEpoch() : |
| 118 base::Time::FromDoubleT(ms_since_epoch / 1000.0); |
| 103 | 119 |
| 104 removal_mask_ = GetRemovalMask(); | 120 removal_mask_ = GetRemovalMask(); |
| 105 | 121 |
| 106 if (removal_mask_ & BrowsingDataRemover::REMOVE_PLUGIN_DATA) { | 122 if (removal_mask_ & BrowsingDataRemover::REMOVE_PLUGIN_DATA) { |
| 107 // If we're being asked to remove plugin data, check whether it's actually | 123 // If we're being asked to remove plugin data, check whether it's actually |
| 108 // supported. | 124 // supported. |
| 109 Profile* profile = GetCurrentBrowser()->profile(); | 125 Profile* profile = GetCurrentBrowser()->profile(); |
| 110 BrowserThread::PostTask( | 126 BrowserThread::PostTask( |
| 111 BrowserThread::FILE, FROM_HERE, | 127 BrowserThread::FILE, FROM_HERE, |
| 112 base::Bind( | 128 base::Bind( |
| (...skipping 20 matching lines...) Expand all Loading... |
| 133 | 149 |
| 134 void BrowsingDataExtensionFunction::StartRemoving() { | 150 void BrowsingDataExtensionFunction::StartRemoving() { |
| 135 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) | 151 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) |
| 136 AddRef(); | 152 AddRef(); |
| 137 | 153 |
| 138 // Create a BrowsingDataRemover, set the current object as an observer (so | 154 // Create a BrowsingDataRemover, set the current object as an observer (so |
| 139 // that we're notified after removal) and call remove() with the arguments | 155 // that we're notified after removal) and call remove() with the arguments |
| 140 // we've generated above. We can use a raw pointer here, as the browsing data | 156 // we've generated above. We can use a raw pointer here, as the browsing data |
| 141 // remover is responsible for deleting itself once data removal is complete. | 157 // remover is responsible for deleting itself once data removal is complete. |
| 142 BrowsingDataRemover* remover = new BrowsingDataRemover( | 158 BrowsingDataRemover* remover = new BrowsingDataRemover( |
| 143 GetCurrentBrowser()->profile(), period_, base::Time::Now()); | 159 GetCurrentBrowser()->profile(), remove_since_, base::Time::Now()); |
| 144 remover->AddObserver(this); | 160 remover->AddObserver(this); |
| 145 remover->Remove(removal_mask_); | 161 remover->Remove(removal_mask_); |
| 146 } | 162 } |
| 147 | 163 |
| 148 int ClearBrowsingDataFunction::GetRemovalMask() const { | 164 int ClearBrowsingDataFunction::GetRemovalMask() const { |
| 149 // Parse the |dataToRemove| argument to generate the removal mask. | 165 // Parse the |dataToRemove| argument to generate the removal mask. |
| 150 base::DictionaryValue* data_to_remove; | 166 base::DictionaryValue* data_to_remove; |
| 151 if (args_->GetDictionary(1, &data_to_remove)) | 167 if (args_->GetDictionary(1, &data_to_remove)) |
| 152 return ParseRemovalMask(data_to_remove); | 168 return ParseRemovalMask(data_to_remove); |
| 153 else | 169 else |
| 154 return 0; | 170 return 0; |
| 155 } | 171 } |
| 156 | 172 |
| 173 int ClearAppCacheFunction::GetRemovalMask() const { |
| 174 return BrowsingDataRemover::REMOVE_APPCACHE; |
| 175 } |
| 176 |
| 157 int ClearCacheFunction::GetRemovalMask() const { | 177 int ClearCacheFunction::GetRemovalMask() const { |
| 158 return BrowsingDataRemover::REMOVE_CACHE; | 178 return BrowsingDataRemover::REMOVE_CACHE; |
| 159 } | 179 } |
| 160 | 180 |
| 161 int ClearCookiesFunction::GetRemovalMask() const { | 181 int ClearCookiesFunction::GetRemovalMask() const { |
| 162 return BrowsingDataRemover::REMOVE_SITE_DATA; | 182 return BrowsingDataRemover::REMOVE_COOKIES; |
| 163 } | 183 } |
| 164 | 184 |
| 165 int ClearDownloadsFunction::GetRemovalMask() const { | 185 int ClearDownloadsFunction::GetRemovalMask() const { |
| 166 return BrowsingDataRemover::REMOVE_DOWNLOADS; | 186 return BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 167 } | 187 } |
| 168 | 188 |
| 189 int ClearFileSystemsFunction::GetRemovalMask() const { |
| 190 return BrowsingDataRemover::REMOVE_FILE_SYSTEMS; |
| 191 } |
| 192 |
| 169 int ClearFormDataFunction::GetRemovalMask() const { | 193 int ClearFormDataFunction::GetRemovalMask() const { |
| 170 return BrowsingDataRemover::REMOVE_FORM_DATA; | 194 return BrowsingDataRemover::REMOVE_FORM_DATA; |
| 171 } | 195 } |
| 172 | 196 |
| 173 int ClearHistoryFunction::GetRemovalMask() const { | 197 int ClearHistoryFunction::GetRemovalMask() const { |
| 174 return BrowsingDataRemover::REMOVE_HISTORY; | 198 return BrowsingDataRemover::REMOVE_HISTORY; |
| 175 } | 199 } |
| 176 | 200 |
| 201 int ClearIndexedDBFunction::GetRemovalMask() const { |
| 202 return BrowsingDataRemover::REMOVE_INDEXEDDB; |
| 203 } |
| 204 |
| 205 int ClearLocalStorageFunction::GetRemovalMask() const { |
| 206 return BrowsingDataRemover::REMOVE_LOCAL_STORAGE; |
| 207 } |
| 208 |
| 209 int ClearPluginDataFunction::GetRemovalMask() const { |
| 210 return BrowsingDataRemover::REMOVE_PLUGIN_DATA; |
| 211 } |
| 212 |
| 177 int ClearPasswordsFunction::GetRemovalMask() const { | 213 int ClearPasswordsFunction::GetRemovalMask() const { |
| 178 return BrowsingDataRemover::REMOVE_CACHE; | 214 return BrowsingDataRemover::REMOVE_PASSWORDS; |
| 179 } | 215 } |
| 216 |
| 217 int ClearWebSQLFunction::GetRemovalMask() const { |
| 218 return BrowsingDataRemover::REMOVE_WEBSQL; |
| 219 } |
| OLD | NEW |