OLD | NEW |
---|---|
1 // Copyright 2015 The Chromium Authors. All rights reserved. | 1 // Copyright 2015 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 #include "chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler. h" | 5 #include "chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler. h" |
6 | 6 |
7 #include <stddef.h> | 7 #include <stddef.h> |
8 | 8 |
9 #include "base/macros.h" | 9 #include "base/macros.h" |
10 #include "base/memory/ptr_util.h" | 10 #include "base/memory/ptr_util.h" |
(...skipping 23 matching lines...) Expand all Loading... | |
34 const int kMaxTimesHistoryNoticeShown = 1; | 34 const int kMaxTimesHistoryNoticeShown = 1; |
35 | 35 |
36 } | 36 } |
37 | 37 |
38 namespace settings { | 38 namespace settings { |
39 | 39 |
40 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) | 40 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) |
41 : profile_(Profile::FromWebUI(webui)), | 41 : profile_(Profile::FromWebUI(webui)), |
42 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), | 42 sync_service_(ProfileSyncServiceFactory::GetForProfile(profile_)), |
43 sync_service_observer_(this), | 43 sync_service_observer_(this), |
44 remover_(nullptr), | 44 remover_(BrowsingDataRemoverFactory::GetForBrowserContext(profile_)), |
Dan Beam
2016/07/07 18:06:49
is there any big penalty for creating this from co
dschuyler
2016/07/20 19:01:15
Good point. Even if the penalty is not big, it's n
| |
45 show_history_footer_(false), | 45 show_history_footer_(false), |
46 show_history_deletion_dialog_(false), | 46 show_history_deletion_dialog_(false), |
47 weak_ptr_factory_(this) {} | 47 weak_ptr_factory_(this) {} |
48 | 48 |
49 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { | 49 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { |
50 if (remover_) | 50 remover_->RemoveObserver(this); |
Dan Beam
2016/07/07 18:06:49
can you make this a ScopedObserver like sync_servi
dschuyler
2016/07/20 19:01:14
Done.
| |
51 remover_->RemoveObserver(this); | |
52 } | 51 } |
53 | 52 |
54 void ClearBrowsingDataHandler::RegisterMessages() { | 53 void ClearBrowsingDataHandler::RegisterMessages() { |
55 web_ui()->RegisterMessageCallback( | 54 web_ui()->RegisterMessageCallback( |
56 "clearBrowsingData", | 55 "clearBrowsingData", |
57 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, | 56 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, |
58 base::Unretained(this))); | 57 base::Unretained(this))); |
59 | 58 |
60 web_ui()->RegisterMessageCallback( | 59 web_ui()->RegisterMessageCallback( |
61 "initializeClearBrowsingData", | 60 "initializeClearBrowsingData", |
62 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, | 61 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, |
63 base::Unretained(this))); | 62 base::Unretained(this))); |
64 } | 63 } |
65 | 64 |
66 void ClearBrowsingDataHandler::OnJavascriptAllowed() { | 65 void ClearBrowsingDataHandler::OnJavascriptAllowed() { |
67 PrefService* prefs = profile_->GetPrefs(); | 66 PrefService* prefs = profile_->GetPrefs(); |
68 profile_pref_registrar_.Init(prefs); | 67 profile_pref_registrar_.Init(prefs); |
69 profile_pref_registrar_.Add( | 68 profile_pref_registrar_.Add( |
70 prefs::kAllowDeletingBrowserHistory, | 69 prefs::kAllowDeletingBrowserHistory, |
71 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged, | 70 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged, |
72 base::Unretained(this))); | 71 base::Unretained(this))); |
73 | 72 |
74 if (sync_service_) | 73 if (sync_service_) |
75 sync_service_observer_.Add(sync_service_); | 74 sync_service_observer_.Add(sync_service_); |
75 | |
76 remover_->AddObserver(this); | |
76 } | 77 } |
77 | 78 |
78 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { | 79 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { |
80 remover_->RemoveObserver(this); | |
79 profile_pref_registrar_.RemoveAll(); | 81 profile_pref_registrar_.RemoveAll(); |
80 sync_service_observer_.RemoveAll(); | 82 sync_service_observer_.RemoveAll(); |
81 } | 83 } |
82 | 84 |
83 void ClearBrowsingDataHandler::HandleClearBrowsingData( | 85 void ClearBrowsingDataHandler::HandleClearBrowsingData( |
84 const base::ListValue* args) { | 86 const base::ListValue* args) { |
85 // We should never be called when the previous clearing has not yet finished. | 87 // We should never be called when the previous clearing has not yet finished. |
86 CHECK(!remover_); | 88 CHECK(!remover_->is_removing()); |
87 CHECK_EQ(1U, args->GetSize()); | 89 CHECK_EQ(1U, args->GetSize()); |
88 CHECK(webui_callback_id_.empty()); | 90 CHECK(webui_callback_id_.empty()); |
89 CHECK(args->GetString(0, &webui_callback_id_)); | 91 CHECK(args->GetString(0, &webui_callback_id_)); |
90 | 92 |
91 PrefService* prefs = profile_->GetPrefs(); | 93 PrefService* prefs = profile_->GetPrefs(); |
92 | 94 |
93 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; | 95 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; |
94 // Don't try to clear LSO data if it's not supported. | 96 // Don't try to clear LSO data if it's not supported. |
95 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) | 97 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) |
96 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; | 98 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; |
(...skipping 59 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
156 int checked_other_types = std::count_if( | 158 int checked_other_types = std::count_if( |
157 other_types, other_types + num_other_types, | 159 other_types, other_types + num_other_types, |
158 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); | 160 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); |
159 UMA_HISTOGRAM_SPARSE_SLOWLY( | 161 UMA_HISTOGRAM_SPARSE_SLOWLY( |
160 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", | 162 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", |
161 checked_other_types); | 163 checked_other_types); |
162 } | 164 } |
163 | 165 |
164 int period_selected = | 166 int period_selected = |
165 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); | 167 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); |
166 remover_ = BrowsingDataRemoverFactory::GetForBrowserContext(profile_); | |
167 remover_->AddObserver(this); | |
168 remover_->Remove(BrowsingDataRemover::Period( | 168 remover_->Remove(BrowsingDataRemover::Period( |
169 static_cast<browsing_data::TimePeriod>(period_selected)), | 169 static_cast<browsing_data::TimePeriod>(period_selected)), |
170 remove_mask, origin_mask); | 170 remove_mask, origin_mask); |
171 } | 171 } |
172 | 172 |
173 void ClearBrowsingDataHandler::OnBrowsingDataRemoving(bool is_removing) { | |
174 CallJavascriptFunction("cr.webUIListenerCallback", | |
175 base::StringValue("browsing-data-removing"), | |
176 base::FundamentalValue(is_removing)); | |
177 } | |
178 | |
173 void ClearBrowsingDataHandler::OnBrowsingDataRemoverDone() { | 179 void ClearBrowsingDataHandler::OnBrowsingDataRemoverDone() { |
174 remover_->RemoveObserver(this); | |
175 remover_ = nullptr; | |
176 | |
177 PrefService* prefs = profile_->GetPrefs(); | 180 PrefService* prefs = profile_->GetPrefs(); |
178 int notice_shown_times = | 181 int notice_shown_times = |
179 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); | 182 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); |
180 | 183 |
181 // When the deletion is complete, we might show an additional dialog with | 184 // When the deletion is complete, we might show an additional dialog with |
182 // a notice about other forms of browsing history. This is the case if | 185 // a notice about other forms of browsing history. This is the case if |
183 const bool show_notice = | 186 const bool show_notice = |
184 // 1. The dialog is relevant for the user. | 187 // 1. The dialog is relevant for the user. |
185 show_history_deletion_dialog_ && | 188 show_history_deletion_dialog_ && |
186 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|. | 189 // 2. The notice has been shown less than |kMaxTimesHistoryNoticeShown|. |
(...skipping 20 matching lines...) Expand all Loading... | |
207 CallJavascriptFunction( | 210 CallJavascriptFunction( |
208 "cr.webUIListenerCallback", | 211 "cr.webUIListenerCallback", |
209 base::StringValue("browsing-history-pref-changed"), | 212 base::StringValue("browsing-history-pref-changed"), |
210 base::FundamentalValue( | 213 base::FundamentalValue( |
211 profile_->GetPrefs()->GetBoolean( | 214 profile_->GetPrefs()->GetBoolean( |
212 prefs::kAllowDeletingBrowserHistory))); | 215 prefs::kAllowDeletingBrowserHistory))); |
213 } | 216 } |
214 | 217 |
215 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { | 218 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { |
216 AllowJavascript(); | 219 AllowJavascript(); |
220 const base::Value* callback_id; | |
221 CHECK(args->Get(0, &callback_id)); | |
217 | 222 |
218 // TODO(msramek): Simplify this using a factory. | 223 // TODO(msramek): Simplify this using a factory. |
219 AddCounter(base::WrapUnique(new AutofillCounter(profile_))); | 224 AddCounter(base::WrapUnique(new AutofillCounter(profile_))); |
220 AddCounter(base::WrapUnique(new CacheCounter(profile_))); | 225 AddCounter(base::WrapUnique(new CacheCounter(profile_))); |
221 AddCounter(base::WrapUnique(new DownloadsCounter(profile_))); | 226 AddCounter(base::WrapUnique(new DownloadsCounter(profile_))); |
222 AddCounter(base::WrapUnique(new HistoryCounter(profile_))); | 227 AddCounter(base::WrapUnique(new HistoryCounter(profile_))); |
223 AddCounter(base::WrapUnique(new HostedAppsCounter(profile_))); | 228 AddCounter(base::WrapUnique(new HostedAppsCounter(profile_))); |
224 AddCounter(base::WrapUnique(new PasswordsCounter(profile_))); | 229 AddCounter(base::WrapUnique(new PasswordsCounter(profile_))); |
225 AddCounter(base::WrapUnique(new MediaLicensesCounter(profile_))); | 230 AddCounter(base::WrapUnique(new MediaLicensesCounter(profile_))); |
226 | 231 |
227 OnStateChanged(); | 232 OnStateChanged(); |
228 RefreshHistoryNotice(); | 233 RefreshHistoryNotice(); |
234 OnBrowsingDataRemoving(remover_->is_removing()); | |
235 | |
236 ResolveJavascriptCallback(*callback_id, *base::Value::CreateNullValue()); | |
229 } | 237 } |
230 | 238 |
231 void ClearBrowsingDataHandler::OnStateChanged() { | 239 void ClearBrowsingDataHandler::OnStateChanged() { |
232 CallJavascriptFunction( | 240 CallJavascriptFunction( |
233 "cr.webUIListenerCallback", | 241 "cr.webUIListenerCallback", |
234 base::StringValue("update-footer"), | 242 base::StringValue("update-footer"), |
235 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), | 243 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), |
236 base::FundamentalValue(show_history_footer_)); | 244 base::FundamentalValue(show_history_footer_)); |
237 } | 245 } |
238 | 246 |
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
285 void ClearBrowsingDataHandler::UpdateCounterText( | 293 void ClearBrowsingDataHandler::UpdateCounterText( |
286 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 294 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
287 CallJavascriptFunction( | 295 CallJavascriptFunction( |
288 "cr.webUIListenerCallback", | 296 "cr.webUIListenerCallback", |
289 base::StringValue("update-counter-text"), | 297 base::StringValue("update-counter-text"), |
290 base::StringValue(result->source()->GetPrefName()), | 298 base::StringValue(result->source()->GetPrefName()), |
291 base::StringValue(GetCounterTextFromResult(result.get()))); | 299 base::StringValue(GetCounterTextFromResult(result.get()))); |
292 } | 300 } |
293 | 301 |
294 } // namespace settings | 302 } // namespace settings |
OLD | NEW |