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