| OLD | NEW |
| (Empty) |
| 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 | |
| 3 // found in the LICENSE file. | |
| 4 | |
| 5 // Defines the Chrome Extensions BrowsingData 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 the extension API JSON. | |
| 8 | |
| 9 #include "chrome/browser/extensions/extension_browsing_data_api.h" | |
| 10 | |
| 11 #include <string> | |
| 12 | |
| 13 #include "base/values.h" | |
| 14 #include "chrome/browser/browsing_data_remover.h" | |
| 15 #include "chrome/browser/plugin_data_remover_helper.h" | |
| 16 #include "chrome/browser/plugin_prefs.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/public/browser/browser_thread.h" | |
| 22 | |
| 23 using content::BrowserThread; | |
| 24 | |
| 25 namespace extension_browsing_data_api_constants { | |
| 26 | |
| 27 // Type 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 kLocalStorageKey[] = "localStorage"; | |
| 37 const char kOriginBoundCertsKey[] = "originBoundCerts"; | |
| 38 const char kPasswordsKey[] = "passwords"; | |
| 39 const char kPluginDataKey[] = "pluginData"; | |
| 40 const char kWebSQLKey[] = "webSQL"; | |
| 41 | |
| 42 // Option Keys. | |
| 43 const char kSinceKey[] = "since"; | |
| 44 | |
| 45 // Errors! | |
| 46 const char kOneAtATimeError[] = "Only one 'browsingData' API call can run at" | |
| 47 "a time."; | |
| 48 | |
| 49 } // namespace extension_browsing_data_api_constants | |
| 50 | |
| 51 namespace { | |
| 52 // Converts the JavaScript API's numeric input (miliseconds since epoch) into an | |
| 53 // appropriate base::Time that we can pass into the BrowsingDataRemove. | |
| 54 bool ParseTimeFromValue(const double& ms_since_epoch, base::Time* time) { | |
| 55 return true; | |
| 56 } | |
| 57 | |
| 58 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or | |
| 59 // false, if the given key doesn't exist in the dictionary. | |
| 60 bool RemoveType(base::DictionaryValue* dict, const std::string& key) { | |
| 61 bool value = false; | |
| 62 if (!dict->GetBoolean(key, &value)) | |
| 63 return false; | |
| 64 else | |
| 65 return value; | |
| 66 } | |
| 67 | |
| 68 // Convert the JavaScript API's object input ({ cookies: true }) into the | |
| 69 // appropriate removal mask for the BrowsingDataRemover object. | |
| 70 int ParseRemovalMask(base::DictionaryValue* value) { | |
| 71 int GetRemovalMask = 0; | |
| 72 if (RemoveType(value, extension_browsing_data_api_constants::kAppCacheKey)) | |
| 73 GetRemovalMask |= BrowsingDataRemover::REMOVE_APPCACHE; | |
| 74 if (RemoveType(value, extension_browsing_data_api_constants::kCacheKey)) | |
| 75 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; | |
| 76 if (RemoveType(value, extension_browsing_data_api_constants::kCookiesKey)) | |
| 77 GetRemovalMask |= BrowsingDataRemover::REMOVE_COOKIES; | |
| 78 if (RemoveType(value, extension_browsing_data_api_constants::kDownloadsKey)) | |
| 79 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 80 if (RemoveType(value, extension_browsing_data_api_constants::kFileSystemsKey)) | |
| 81 GetRemovalMask |= BrowsingDataRemover::REMOVE_FILE_SYSTEMS; | |
| 82 if (RemoveType(value, extension_browsing_data_api_constants::kFormDataKey)) | |
| 83 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 84 if (RemoveType(value, extension_browsing_data_api_constants::kHistoryKey)) | |
| 85 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; | |
| 86 if (RemoveType(value, extension_browsing_data_api_constants::kIndexedDBKey)) | |
| 87 GetRemovalMask |= BrowsingDataRemover::REMOVE_INDEXEDDB; | |
| 88 if (RemoveType(value, | |
| 89 extension_browsing_data_api_constants::kLocalStorageKey)) | |
| 90 GetRemovalMask |= BrowsingDataRemover::REMOVE_LOCAL_STORAGE; | |
| 91 if (RemoveType(value, | |
| 92 extension_browsing_data_api_constants::kOriginBoundCertsKey)) | |
| 93 GetRemovalMask |= BrowsingDataRemover::REMOVE_ORIGIN_BOUND_CERTS; | |
| 94 if (RemoveType(value, extension_browsing_data_api_constants::kPasswordsKey)) | |
| 95 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; | |
| 96 if (RemoveType(value, extension_browsing_data_api_constants::kPluginDataKey)) | |
| 97 GetRemovalMask |= BrowsingDataRemover::REMOVE_PLUGIN_DATA; | |
| 98 if (RemoveType(value, extension_browsing_data_api_constants::kWebSQLKey)) | |
| 99 GetRemovalMask |= BrowsingDataRemover::REMOVE_WEBSQL; | |
| 100 | |
| 101 return GetRemovalMask; | |
| 102 } | |
| 103 | |
| 104 } // Namespace. | |
| 105 | |
| 106 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | |
| 107 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 108 this->SendResponse(true); | |
| 109 | |
| 110 Release(); // Balanced in RunImpl. | |
| 111 } | |
| 112 | |
| 113 bool BrowsingDataExtensionFunction::RunImpl() { | |
| 114 if (BrowsingDataRemover::is_removing()) { | |
| 115 error_ = extension_browsing_data_api_constants::kOneAtATimeError; | |
| 116 return false; | |
| 117 } | |
| 118 | |
| 119 // Grab the initial |options| parameter, and parse out the arguments. | |
| 120 DictionaryValue* options; | |
| 121 EXTENSION_FUNCTION_VALIDATE(args_->GetDictionary(0, &options)); | |
| 122 DCHECK(options); | |
| 123 | |
| 124 // If |ms_since_epoch| isn't set, default it to 0. | |
| 125 double ms_since_epoch; | |
| 126 if (!options->GetDouble(extension_browsing_data_api_constants::kSinceKey, | |
| 127 &ms_since_epoch)) | |
| 128 ms_since_epoch = 0; | |
| 129 | |
| 130 // base::Time takes a double that represents seconds since epoch. JavaScript | |
| 131 // gives developers milliseconds, so do a quick conversion before populating | |
| 132 // the object. Also, Time::FromDoubleT converts double time 0 to empty Time | |
| 133 // object. So we need to do special handling here. | |
| 134 remove_since_ = (ms_since_epoch == 0) ? | |
| 135 base::Time::UnixEpoch() : | |
| 136 base::Time::FromDoubleT(ms_since_epoch / 1000.0); | |
| 137 | |
| 138 removal_mask_ = GetRemovalMask(); | |
| 139 | |
| 140 if (removal_mask_ & BrowsingDataRemover::REMOVE_PLUGIN_DATA) { | |
| 141 // If we're being asked to remove plugin data, check whether it's actually | |
| 142 // supported. | |
| 143 Profile* profile = GetCurrentBrowser()->profile(); | |
| 144 BrowserThread::PostTask( | |
| 145 BrowserThread::FILE, FROM_HERE, | |
| 146 base::Bind( | |
| 147 &BrowsingDataExtensionFunction::CheckRemovingPluginDataSupported, | |
| 148 this, | |
| 149 make_scoped_refptr(PluginPrefs::GetForProfile(profile)))); | |
| 150 } else { | |
| 151 StartRemoving(); | |
| 152 } | |
| 153 | |
| 154 // Will finish asynchronously. | |
| 155 return true; | |
| 156 } | |
| 157 | |
| 158 void BrowsingDataExtensionFunction::CheckRemovingPluginDataSupported( | |
| 159 scoped_refptr<PluginPrefs> plugin_prefs) { | |
| 160 if (!PluginDataRemoverHelper::IsSupported(plugin_prefs)) | |
| 161 removal_mask_ &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; | |
| 162 | |
| 163 BrowserThread::PostTask( | |
| 164 BrowserThread::UI, FROM_HERE, | |
| 165 base::Bind(&BrowsingDataExtensionFunction::StartRemoving, this)); | |
| 166 } | |
| 167 | |
| 168 void BrowsingDataExtensionFunction::StartRemoving() { | |
| 169 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) | |
| 170 AddRef(); | |
| 171 | |
| 172 // Create a BrowsingDataRemover, set the current object as an observer (so | |
| 173 // that we're notified after removal) and call remove() with the arguments | |
| 174 // we've generated above. We can use a raw pointer here, as the browsing data | |
| 175 // remover is responsible for deleting itself once data removal is complete. | |
| 176 BrowsingDataRemover* remover = new BrowsingDataRemover( | |
| 177 GetCurrentBrowser()->profile(), remove_since_, base::Time::Now()); | |
| 178 remover->AddObserver(this); | |
| 179 remover->Remove(removal_mask_); | |
| 180 } | |
| 181 | |
| 182 int RemoveBrowsingDataFunction::GetRemovalMask() const { | |
| 183 // Parse the |dataToRemove| argument to generate the removal mask. | |
| 184 base::DictionaryValue* data_to_remove; | |
| 185 if (args_->GetDictionary(1, &data_to_remove)) | |
| 186 return ParseRemovalMask(data_to_remove); | |
| 187 else | |
| 188 return 0; | |
| 189 } | |
| 190 | |
| 191 int RemoveAppCacheFunction::GetRemovalMask() const { | |
| 192 return BrowsingDataRemover::REMOVE_APPCACHE; | |
| 193 } | |
| 194 | |
| 195 int RemoveCacheFunction::GetRemovalMask() const { | |
| 196 return BrowsingDataRemover::REMOVE_CACHE; | |
| 197 } | |
| 198 | |
| 199 int RemoveCookiesFunction::GetRemovalMask() const { | |
| 200 return BrowsingDataRemover::REMOVE_COOKIES; | |
| 201 } | |
| 202 | |
| 203 int RemoveDownloadsFunction::GetRemovalMask() const { | |
| 204 return BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 205 } | |
| 206 | |
| 207 int RemoveFileSystemsFunction::GetRemovalMask() const { | |
| 208 return BrowsingDataRemover::REMOVE_FILE_SYSTEMS; | |
| 209 } | |
| 210 | |
| 211 int RemoveFormDataFunction::GetRemovalMask() const { | |
| 212 return BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 213 } | |
| 214 | |
| 215 int RemoveHistoryFunction::GetRemovalMask() const { | |
| 216 return BrowsingDataRemover::REMOVE_HISTORY; | |
| 217 } | |
| 218 | |
| 219 int RemoveIndexedDBFunction::GetRemovalMask() const { | |
| 220 return BrowsingDataRemover::REMOVE_INDEXEDDB; | |
| 221 } | |
| 222 | |
| 223 int RemoveLocalStorageFunction::GetRemovalMask() const { | |
| 224 return BrowsingDataRemover::REMOVE_LOCAL_STORAGE; | |
| 225 } | |
| 226 | |
| 227 int RemoveOriginBoundCertsFunction::GetRemovalMask() const { | |
| 228 return BrowsingDataRemover::REMOVE_ORIGIN_BOUND_CERTS; | |
| 229 } | |
| 230 | |
| 231 int RemovePluginDataFunction::GetRemovalMask() const { | |
| 232 return BrowsingDataRemover::REMOVE_PLUGIN_DATA; | |
| 233 } | |
| 234 | |
| 235 int RemovePasswordsFunction::GetRemovalMask() const { | |
| 236 return BrowsingDataRemover::REMOVE_PASSWORDS; | |
| 237 } | |
| 238 | |
| 239 int RemoveWebSQLFunction::GetRemovalMask() const { | |
| 240 return BrowsingDataRemover::REMOVE_WEBSQL; | |
| 241 } | |
| OLD | NEW |