| OLD | NEW |
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 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 BrowsingData API functions, which entail | 5 // Defines the Chrome Extensions BrowsingData 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/api/browsing_data/browsing_data_api.h" | 9 #include "chrome/browser/extensions/api/browsing_data/browsing_data_api.h" |
| 10 | 10 |
| (...skipping 23 matching lines...) Expand all Loading... |
| 34 const char kFormDataKey[] = "formData"; | 34 const char kFormDataKey[] = "formData"; |
| 35 const char kHistoryKey[] = "history"; | 35 const char kHistoryKey[] = "history"; |
| 36 const char kIndexedDBKey[] = "indexedDB"; | 36 const char kIndexedDBKey[] = "indexedDB"; |
| 37 const char kLocalStorageKey[] = "localStorage"; | 37 const char kLocalStorageKey[] = "localStorage"; |
| 38 const char kServerBoundCertsKey[] = "serverBoundCerts"; | 38 const char kServerBoundCertsKey[] = "serverBoundCerts"; |
| 39 const char kPasswordsKey[] = "passwords"; | 39 const char kPasswordsKey[] = "passwords"; |
| 40 const char kPluginDataKey[] = "pluginData"; | 40 const char kPluginDataKey[] = "pluginData"; |
| 41 const char kWebSQLKey[] = "webSQL"; | 41 const char kWebSQLKey[] = "webSQL"; |
| 42 | 42 |
| 43 // Option Keys. | 43 // Option Keys. |
| 44 const char kExtensionsKey[] = "extension"; |
| 45 const char kOriginTypesKey[] = "originType"; |
| 46 const char kProtectedWebKey[] = "protectedWeb"; |
| 44 const char kSinceKey[] = "since"; | 47 const char kSinceKey[] = "since"; |
| 48 const char kUnprotectedWebKey[] = "unprotectedWeb"; |
| 45 | 49 |
| 46 // Errors! | 50 // Errors! |
| 47 const char kOneAtATimeError[] = "Only one 'browsingData' API call can run at" | 51 const char kOneAtATimeError[] = "Only one 'browsingData' API call can run at " |
| 48 "a time."; | 52 "a time."; |
| 49 | 53 |
| 50 } // namespace extension_browsing_data_api_constants | 54 } // namespace extension_browsing_data_api_constants |
| 51 | 55 |
| 52 namespace { | 56 namespace { |
| 53 // Converts the JavaScript API's numeric input (miliseconds since epoch) into an | 57 // Converts the JavaScript API's numeric input (miliseconds since epoch) into an |
| 54 // appropriate base::Time that we can pass into the BrowsingDataRemove. | 58 // appropriate base::Time that we can pass into the BrowsingDataRemove. |
| 55 bool ParseTimeFromValue(const double& ms_since_epoch, base::Time* time) { | 59 bool ParseTimeFromValue(const double& ms_since_epoch, base::Time* time) { |
| 56 return true; | 60 return true; |
| 57 } | 61 } |
| (...skipping 37 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 95 if (RemoveType(value, extension_browsing_data_api_constants::kPasswordsKey)) | 99 if (RemoveType(value, extension_browsing_data_api_constants::kPasswordsKey)) |
| 96 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; | 100 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; |
| 97 if (RemoveType(value, extension_browsing_data_api_constants::kPluginDataKey)) | 101 if (RemoveType(value, extension_browsing_data_api_constants::kPluginDataKey)) |
| 98 GetRemovalMask |= BrowsingDataRemover::REMOVE_PLUGIN_DATA; | 102 GetRemovalMask |= BrowsingDataRemover::REMOVE_PLUGIN_DATA; |
| 99 if (RemoveType(value, extension_browsing_data_api_constants::kWebSQLKey)) | 103 if (RemoveType(value, extension_browsing_data_api_constants::kWebSQLKey)) |
| 100 GetRemovalMask |= BrowsingDataRemover::REMOVE_WEBSQL; | 104 GetRemovalMask |= BrowsingDataRemover::REMOVE_WEBSQL; |
| 101 | 105 |
| 102 return GetRemovalMask; | 106 return GetRemovalMask; |
| 103 } | 107 } |
| 104 | 108 |
| 105 } // Namespace. | 109 } // namespace |
| 106 | 110 |
| 107 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | 111 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| 108 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | 112 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 109 this->SendResponse(true); | 113 this->SendResponse(true); |
| 110 | 114 |
| 111 Release(); // Balanced in RunImpl. | 115 Release(); // Balanced in RunImpl. |
| 112 } | 116 } |
| 113 | 117 |
| 114 bool BrowsingDataExtensionFunction::RunImpl() { | 118 bool BrowsingDataExtensionFunction::RunImpl() { |
| 115 if (BrowsingDataRemover::is_removing()) { | 119 if (BrowsingDataRemover::is_removing()) { |
| 116 error_ = extension_browsing_data_api_constants::kOneAtATimeError; | 120 error_ = extension_browsing_data_api_constants::kOneAtATimeError; |
| 117 return false; | 121 return false; |
| 118 } | 122 } |
| 119 | 123 |
| 120 // Grab the initial |options| parameter, and parse out the arguments. | 124 // Grab the initial |options| parameter, and parse out the arguments. |
| 121 DictionaryValue* options; | 125 DictionaryValue* options; |
| 122 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | 126 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); |
| 123 DCHECK(options); | 127 DCHECK(options); |
| 124 | 128 |
| 129 origin_set_mask_ = ParseOriginSetMask(*options); |
| 130 |
| 125 // If |ms_since_epoch| isn't set, default it to 0. | 131 // If |ms_since_epoch| isn't set, default it to 0. |
| 126 double ms_since_epoch; | 132 double ms_since_epoch; |
| 127 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | 133 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, |
| 128 &ms_since_epoch)) | 134 &ms_since_epoch)) |
| 129 ms_since_epoch = 0; | 135 ms_since_epoch = 0; |
| 130 | 136 |
| 131 // base::Time takes a double that represents seconds since epoch. JavaScript | 137 // base::Time takes a double that represents seconds since epoch. JavaScript |
| 132 // gives developers milliseconds, so do a quick conversion before populating | 138 // gives developers milliseconds, so do a quick conversion before populating |
| 133 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | 139 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time |
| 134 // object. So we need to do special handling here. | 140 // object. So we need to do special handling here. |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 170 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) | 176 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) |
| 171 AddRef(); | 177 AddRef(); |
| 172 | 178 |
| 173 // Create a BrowsingDataRemover, set the current object as an observer (so | 179 // Create a BrowsingDataRemover, set the current object as an observer (so |
| 174 // that we're notified after removal) and call remove() with the arguments | 180 // that we're notified after removal) and call remove() with the arguments |
| 175 // we've generated above. We can use a raw pointer here, as the browsing data | 181 // we've generated above. We can use a raw pointer here, as the browsing data |
| 176 // remover is responsible for deleting itself once data removal is complete. | 182 // remover is responsible for deleting itself once data removal is complete. |
| 177 BrowsingDataRemover* remover = new BrowsingDataRemover( | 183 BrowsingDataRemover* remover = new BrowsingDataRemover( |
| 178 GetCurrentBrowser()->profile(), remove_since_, base::Time::Now()); | 184 GetCurrentBrowser()->profile(), remove_since_, base::Time::Now()); |
| 179 remover->AddObserver(this); | 185 remover->AddObserver(this); |
| 180 remover->Remove(removal_mask_, BrowsingDataHelper::UNPROTECTED_WEB); | 186 remover->Remove(removal_mask_, origin_set_mask_); |
| 187 } |
| 188 |
| 189 int BrowsingDataExtensionFunction::ParseOriginSetMask( |
| 190 const base::DictionaryValue& options) { |
| 191 // Parse the |options| dictionary to generate the origin set mask. Default to |
| 192 // UNPROTECTED_WEB if the developer doesn't specify anything. |
| 193 int mask = BrowsingDataHelper::UNPROTECTED_WEB; |
| 194 |
| 195 DictionaryValue* d = NULL; |
| 196 if (options.HasKey(extension_browsing_data_api_constants::kOriginTypesKey)) { |
| 197 EXTENSION_FUNCTION_VALIDATE(options.GetDictionary( |
| 198 extension_browsing_data_api_constants::kOriginTypesKey, &d)); |
| 199 bool value; |
| 200 |
| 201 // The developer specified something! Reset to 0 and parse the dictionary. |
| 202 mask = 0; |
| 203 |
| 204 // Unprotected web. |
| 205 if (d->HasKey(extension_browsing_data_api_constants::kUnprotectedWebKey)) { |
| 206 EXTENSION_FUNCTION_VALIDATE(d->GetBoolean( |
| 207 extension_browsing_data_api_constants::kUnprotectedWebKey, &value)); |
| 208 mask |= value ? BrowsingDataHelper::UNPROTECTED_WEB : 0; |
| 209 } |
| 210 |
| 211 // Protected web. |
| 212 if (d->HasKey(extension_browsing_data_api_constants::kProtectedWebKey)) { |
| 213 EXTENSION_FUNCTION_VALIDATE(d->GetBoolean( |
| 214 extension_browsing_data_api_constants::kProtectedWebKey, &value)); |
| 215 mask |= value ? BrowsingDataHelper::PROTECTED_WEB : 0; |
| 216 } |
| 217 |
| 218 // Extensions. |
| 219 if (d->HasKey(extension_browsing_data_api_constants::kExtensionsKey)) { |
| 220 EXTENSION_FUNCTION_VALIDATE(d->GetBoolean( |
| 221 extension_browsing_data_api_constants::kExtensionsKey, &value)); |
| 222 mask |= value ? BrowsingDataHelper::EXTENSION : 0; |
| 223 } |
| 224 } |
| 225 |
| 226 return mask; |
| 181 } | 227 } |
| 182 | 228 |
| 183 int RemoveBrowsingDataFunction::GetRemovalMask() const { | 229 int RemoveBrowsingDataFunction::GetRemovalMask() const { |
| 184 // Parse the |dataToRemove| argument to generate the removal mask. | 230 // Parse the |dataToRemove| argument to generate the removal mask. |
| 185 base::DictionaryValue* data_to_remove; | 231 base::DictionaryValue* data_to_remove; |
| 186 if (args_->GetDictionary(1, &data_to_remove)) | 232 if (args_->GetDictionary(1, &data_to_remove)) |
| 187 return ParseRemovalMask(data_to_remove); | 233 return ParseRemovalMask(data_to_remove); |
| 188 else | 234 else |
| 189 return 0; | 235 return 0; |
| 190 } | 236 } |
| (...skipping 42 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 233 return BrowsingDataRemover::REMOVE_PLUGIN_DATA; | 279 return BrowsingDataRemover::REMOVE_PLUGIN_DATA; |
| 234 } | 280 } |
| 235 | 281 |
| 236 int RemovePasswordsFunction::GetRemovalMask() const { | 282 int RemovePasswordsFunction::GetRemovalMask() const { |
| 237 return BrowsingDataRemover::REMOVE_PASSWORDS; | 283 return BrowsingDataRemover::REMOVE_PASSWORDS; |
| 238 } | 284 } |
| 239 | 285 |
| 240 int RemoveWebSQLFunction::GetRemovalMask() const { | 286 int RemoveWebSQLFunction::GetRemovalMask() const { |
| 241 return BrowsingDataRemover::REMOVE_WEBSQL; | 287 return BrowsingDataRemover::REMOVE_WEBSQL; |
| 242 } | 288 } |
| OLD | NEW |