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