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 // Converts the JavaScript API's string input ("last_week") into the |
| 28 // appropriate BrowsingDataRemover::TimePeriod (in this case, |
| 29 // BrowsingDataRemover::LAST_WEEK). |
| 30 bool ParseTimePeriod(const std::string& parse, |
| 31 BrowsingDataRemover::TimePeriod* period) { |
| 32 if (parse == keys::kHourEnum) |
| 33 *period = BrowsingDataRemover::LAST_HOUR; |
| 34 else if (parse == keys::kDayEnum) |
| 35 *period = BrowsingDataRemover::LAST_DAY; |
| 36 else if (parse == keys::kWeekEnum) |
| 37 *period = BrowsingDataRemover::LAST_WEEK; |
| 38 else if (parse == keys::kMonthEnum) |
| 39 *period = BrowsingDataRemover::FOUR_WEEKS; |
| 40 else if (parse == keys::kEverythingEnum) |
| 41 *period = BrowsingDataRemover::EVERYTHING; |
| 42 else |
| 43 return false; |
| 44 |
| 45 return true; |
| 46 } |
| 47 |
| 48 // Given a DictionaryValue |dict|, returns either the value stored as |key|, or |
| 49 // false, if the given key doesn't exist in the dictionary. |
| 50 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { |
| 51 bool value = false; |
| 52 if (!dict->GetBoolean(key, &value)) |
| 53 return false; |
| 54 else |
| 55 return value; |
| 56 } |
| 57 |
| 58 // Convert the JavaScript API's object input ({ cookies: true }) into the |
| 59 // appropriate removal mask for the BrowsingDataRemover object. |
| 60 int ParseRemovalMask(base::DictionaryValue* value) { |
| 61 int GetRemovalMask = 0; |
| 62 if (DataRemovalRequested(value, keys::kCacheKey)) |
| 63 GetRemovalMask |= BrowsingDataRemover::REMOVE_CACHE; |
| 64 if (DataRemovalRequested(value, keys::kDownloadsKey)) |
| 65 GetRemovalMask |= BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 66 if (DataRemovalRequested(value, keys::kFormDataKey)) |
| 67 GetRemovalMask |= BrowsingDataRemover::REMOVE_FORM_DATA; |
| 68 if (DataRemovalRequested(value, keys::kHistoryKey)) |
| 69 GetRemovalMask |= BrowsingDataRemover::REMOVE_HISTORY; |
| 70 if (DataRemovalRequested(value, keys::kPasswordsKey)) |
| 71 GetRemovalMask |= BrowsingDataRemover::REMOVE_PASSWORDS; |
| 72 |
| 73 // When we talk users about "cookies", we mean not just cookies, but pretty |
| 74 // much everything associated with an origin. To that end, we explicitly |
| 75 // include the REMOVE_LSO_DATA mask here, and BrowsingDataRemover interprets |
| 76 // the REMOVE_COOKIES mask internally as "cookies and site data". |
| 77 if (DataRemovalRequested(value, keys::kCookiesKey)) { |
| 78 GetRemovalMask |= BrowsingDataRemover::REMOVE_COOKIES; |
| 79 GetRemovalMask |= BrowsingDataRemover::REMOVE_LSO_DATA; |
| 80 } |
| 81 |
| 82 return GetRemovalMask; |
| 83 } |
| 84 |
| 85 } // Namespace. |
| 86 |
| 87 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { |
| 88 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); |
| 89 this->SendResponse(true); |
| 90 |
| 91 Release(); // Balanced in RunImpl. |
| 92 } |
| 93 |
| 94 bool BrowsingDataExtensionFunction::RunImpl() { |
| 95 if (BrowsingDataRemover::is_removing()) { |
| 96 error_ = keys::kOneAtATimeError; |
| 97 return false; |
| 98 } |
| 99 |
| 100 // Parse the |timeframe| argument to generate the TimePeriod. |
| 101 std::string timeframe; |
| 102 BrowsingDataRemover::TimePeriod period; |
| 103 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); |
| 104 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period)); |
| 105 |
| 106 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) |
| 107 AddRef(); |
| 108 |
| 109 // Create a BrowsingDataRemover, set the current object as an observer (so |
| 110 // that we're notified after removal) and call remove() with the arguments |
| 111 // we've generated above. We can use a raw pointer here, as the browsing data |
| 112 // remover is responsible for deleting itself once data removal is complete. |
| 113 BrowsingDataRemover* remover = new BrowsingDataRemover( |
| 114 GetCurrentBrowser()->profile(), period, base::Time::Now()); |
| 115 remover->AddObserver(this); |
| 116 remover->Remove(GetRemovalMask()); |
| 117 |
| 118 // Will finish asynchronously. |
| 119 return true; |
| 120 } |
| 121 |
| 122 int ClearBrowsingDataFunction::GetRemovalMask() const { |
| 123 // Parse the |dataToRemove| argument to generate the removal mask. |
| 124 base::DictionaryValue* data_to_remove; |
| 125 if (args_->GetDictionary(1, &data_to_remove)) |
| 126 return ParseRemovalMask(data_to_remove); |
| 127 else |
| 128 return 0; |
| 129 } |
| 130 |
| 131 int ClearCacheFunction::GetRemovalMask() const { |
| 132 return BrowsingDataRemover::REMOVE_CACHE; |
| 133 } |
| 134 |
| 135 int ClearCookiesFunction::GetRemovalMask() const { |
| 136 return BrowsingDataRemover::REMOVE_COOKIES | |
| 137 BrowsingDataRemover::REMOVE_LSO_DATA; |
| 138 } |
| 139 |
| 140 int ClearDownloadsFunction::GetRemovalMask() const { |
| 141 return BrowsingDataRemover::REMOVE_DOWNLOADS; |
| 142 } |
| 143 |
| 144 int ClearFormDataFunction::GetRemovalMask() const { |
| 145 return BrowsingDataRemover::REMOVE_FORM_DATA; |
| 146 } |
| 147 |
| 148 int ClearHistoryFunction::GetRemovalMask() const { |
| 149 return BrowsingDataRemover::REMOVE_HISTORY; |
| 150 } |
| 151 |
| 152 int ClearPasswordsFunction::GetRemovalMask() const { |
| 153 return BrowsingDataRemover::REMOVE_CACHE; |
| 154 } |
OLD | NEW |