Chromium Code Reviews| 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 bool DataRemovalRequested(base::DictionaryValue* dict, std::string key) { | |
| 28 bool value = false; | |
| 29 if (!dict->GetBoolean(key, &value)) | |
| 30 return false; | |
| 31 else | |
| 32 return value; | |
| 33 } | |
| 34 | |
| 35 } // Namespace. | |
| 36 | |
| 37 void BrowsingDataExtensionFunction::OnBrowsingDataRemoverDone() { | |
| 38 DCHECK(BrowserThread::CurrentlyOn(BrowserThread::UI)); | |
| 39 result_.reset(Value::CreateBooleanValue(true)); | |
| 40 this->SendResponse(true); | |
| 41 | |
| 42 Release(); // Balanced in RunImpl. | |
|
Bernhard Bauer
2011/08/16 11:29:25
When clearing browsing data from the UI, we block
Mike West
2011/08/16 12:13:42
Idunno. Probably? :)
Jochen? Is there anything I
jochen (gone - plz use gerrit)
2011/08/16 15:11:44
In my opinion, the browsingdataremover should some
| |
| 43 } | |
| 44 | |
| 45 bool BrowsingDataExtensionFunction::ParseTimePeriod( | |
| 46 const std::string& parse, BrowsingDataRemover::TimePeriod* period) const { | |
| 47 if (parse == keys::kHourEnum) | |
| 48 *period = BrowsingDataRemover::LAST_HOUR; | |
| 49 else if (parse == keys::kDayEnum) | |
| 50 *period = BrowsingDataRemover::LAST_DAY; | |
| 51 else if (parse == keys::kWeekEnum) | |
| 52 *period = BrowsingDataRemover::LAST_WEEK; | |
| 53 else if (parse == keys::kMonthEnum) | |
| 54 *period = BrowsingDataRemover::FOUR_WEEKS; | |
| 55 else if (parse == keys::kEverythingEnum) | |
| 56 *period = BrowsingDataRemover::EVERYTHING; | |
| 57 else | |
| 58 return false; | |
| 59 | |
| 60 return true; | |
| 61 } | |
| 62 | |
| 63 int BrowsingDataExtensionFunction::ParseRemovalMask( | |
| 64 base::DictionaryValue* value) const { | |
| 65 int removal_mask = 0; | |
| 66 if (DataRemovalRequested(value, keys::kCacheKey)) | |
| 67 removal_mask |= BrowsingDataRemover::REMOVE_CACHE; | |
| 68 if (DataRemovalRequested(value, keys::kDownloadsKey)) | |
| 69 removal_mask |= BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 70 if (DataRemovalRequested(value, keys::kFormDataKey)) | |
| 71 removal_mask |= BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 72 if (DataRemovalRequested(value, keys::kHistoryKey)) | |
| 73 removal_mask |= BrowsingDataRemover::REMOVE_HISTORY; | |
| 74 if (DataRemovalRequested(value, keys::kPasswordsKey)) | |
| 75 removal_mask |= BrowsingDataRemover::REMOVE_PASSWORDS; | |
| 76 | |
| 77 // "Cookies" are cookies + site data + LSO. | |
| 78 if (DataRemovalRequested(value, keys::kCookiesKey)) { | |
| 79 removal_mask |= BrowsingDataRemover::REMOVE_COOKIES; | |
| 80 removal_mask |= BrowsingDataRemover::REMOVE_LSO_DATA; | |
| 81 } | |
| 82 | |
| 83 return removal_mask; | |
| 84 } | |
| 85 | |
| 86 bool BrowsingDataExtensionFunction::RunImpl() { | |
| 87 if (BrowsingDataRemover::is_removing()) { | |
| 88 error_ = keys::kOneAtATimeError; | |
| 89 return false; | |
| 90 } | |
| 91 | |
| 92 // Parse the |timeframe| argument to generate the TimePeriod. | |
| 93 std::string timeframe; | |
| 94 BrowsingDataRemover::TimePeriod period; | |
| 95 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &timeframe)); | |
| 96 EXTENSION_FUNCTION_VALIDATE(ParseTimePeriod(timeframe, &period)); | |
| 97 | |
| 98 // If we're good to go, add a ref (Balanced in OnBrowsingDataRemoverDone) | |
| 99 AddRef(); | |
| 100 | |
| 101 // Create a BrowsingDataRemover, set the current object as an observer (so | |
| 102 // that we're notified after removal) and call remove() with the arguments | |
| 103 // we've generated above. We can use a raw pointer here, as the browsing data | |
| 104 // remover is responsible for deleting itself once data removal is complete. | |
| 105 BrowsingDataRemover* remover(new BrowsingDataRemover( | |
|
Bernhard Bauer
2011/08/16 11:29:25
Nit: I think using = for initialization is more co
Mike West
2011/08/16 12:13:42
Done.
| |
| 106 GetCurrentBrowser()->profile(), period, base::Time::Now())); | |
| 107 remover->AddObserver(this); | |
| 108 remover->Remove(removal_mask()); | |
| 109 | |
| 110 // Will finish asynchronously. | |
| 111 return true; | |
| 112 } | |
| 113 | |
| 114 int ClearBrowsingDataFunction::removal_mask() const { | |
| 115 // Parse the |dataToRemove| argument to generate the removal mask. | |
| 116 base::DictionaryValue* data_to_remove; | |
| 117 if (args_->GetDictionary(1, &data_to_remove)) | |
| 118 return ParseRemovalMask(data_to_remove); | |
| 119 else | |
| 120 return 0; | |
| 121 } | |
| 122 | |
| 123 int ClearCacheFunction::removal_mask() const { | |
| 124 return BrowsingDataRemover::REMOVE_CACHE; | |
| 125 } | |
| 126 | |
| 127 int ClearCookiesFunction::removal_mask() const { | |
| 128 return BrowsingDataRemover::REMOVE_COOKIES | | |
| 129 BrowsingDataRemover::REMOVE_LSO_DATA; | |
| 130 } | |
| 131 | |
| 132 int ClearDownloadsFunction::removal_mask() const { | |
| 133 return BrowsingDataRemover::REMOVE_DOWNLOADS; | |
| 134 } | |
| 135 | |
| 136 int ClearFormDataFunction::removal_mask() const { | |
| 137 return BrowsingDataRemover::REMOVE_FORM_DATA; | |
| 138 } | |
| 139 | |
| 140 int ClearHistoryFunction::removal_mask() const { | |
| 141 return BrowsingDataRemover::REMOVE_HISTORY; | |
| 142 } | |
| 143 | |
| 144 int ClearPasswordsFunction::removal_mask() const { | |
| 145 return BrowsingDataRemover::REMOVE_CACHE; | |
| 146 } | |
| OLD | NEW |