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 24 matching lines...) Expand all Loading... | |
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_(nullptr), |
45 remover_observer_(this), | |
45 show_history_footer_(false), | 46 show_history_footer_(false), |
46 show_history_deletion_dialog_(false), | 47 show_history_deletion_dialog_(false), |
47 weak_ptr_factory_(this) {} | 48 weak_ptr_factory_(this) {} |
48 | 49 |
49 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { | 50 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { |
50 if (remover_) | |
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 if (!remover_) | |
77 remover_ = BrowsingDataRemoverFactory::GetForBrowserContext(profile_); | |
78 | |
79 remover_observer_.Add(remover_); | |
76 } | 80 } |
77 | 81 |
78 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { | 82 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { |
83 remover_observer_.RemoveAll(); | |
79 profile_pref_registrar_.RemoveAll(); | 84 profile_pref_registrar_.RemoveAll(); |
80 sync_service_observer_.RemoveAll(); | 85 sync_service_observer_.RemoveAll(); |
81 } | 86 } |
82 | 87 |
83 void ClearBrowsingDataHandler::HandleClearBrowsingData( | 88 void ClearBrowsingDataHandler::HandleClearBrowsingData( |
84 const base::ListValue* args) { | 89 const base::ListValue* args) { |
85 // We should never be called when the previous clearing has not yet finished. | 90 // We should never be called when the previous clearing has not yet finished. |
86 CHECK(!remover_); | 91 CHECK(!remover_->is_removing()); |
87 CHECK_EQ(1U, args->GetSize()); | 92 CHECK_EQ(1U, args->GetSize()); |
88 CHECK(webui_callback_id_.empty()); | 93 CHECK(webui_callback_id_.empty()); |
89 CHECK(args->GetString(0, &webui_callback_id_)); | 94 CHECK(args->GetString(0, &webui_callback_id_)); |
90 | 95 |
91 PrefService* prefs = profile_->GetPrefs(); | 96 PrefService* prefs = profile_->GetPrefs(); |
92 | 97 |
93 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; | 98 int site_data_mask = BrowsingDataRemover::REMOVE_SITE_DATA; |
94 // Don't try to clear LSO data if it's not supported. | 99 // Don't try to clear LSO data if it's not supported. |
95 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) | 100 if (!prefs->GetBoolean(prefs::kClearPluginLSODataEnabled)) |
96 site_data_mask &= ~BrowsingDataRemover::REMOVE_PLUGIN_DATA; | 101 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( | 161 int checked_other_types = std::count_if( |
157 other_types, other_types + num_other_types, | 162 other_types, other_types + num_other_types, |
158 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); | 163 [prefs](const std::string& pref) { return prefs->GetBoolean(pref); }); |
159 UMA_HISTOGRAM_SPARSE_SLOWLY( | 164 UMA_HISTOGRAM_SPARSE_SLOWLY( |
160 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", | 165 "History.ClearBrowsingData.PasswordsDeletion.AdditionalDatatypesCount", |
161 checked_other_types); | 166 checked_other_types); |
162 } | 167 } |
163 | 168 |
164 int period_selected = | 169 int period_selected = |
165 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); | 170 prefs->GetInteger(browsing_data::prefs::kDeleteTimePeriod); |
166 remover_ = BrowsingDataRemoverFactory::GetForBrowserContext(profile_); | |
167 remover_->AddObserver(this); | |
168 remover_->Remove(BrowsingDataRemover::Period( | 171 remover_->Remove(BrowsingDataRemover::Period( |
169 static_cast<browsing_data::TimePeriod>(period_selected)), | 172 static_cast<browsing_data::TimePeriod>(period_selected)), |
170 remove_mask, origin_mask); | 173 remove_mask, origin_mask); |
171 } | 174 } |
172 | 175 |
173 void ClearBrowsingDataHandler::OnBrowsingDataRemoverDone() { | 176 void ClearBrowsingDataHandler::OnBrowsingDataRemoving(bool is_removing) { |
174 remover_->RemoveObserver(this); | 177 CallJavascriptFunction("cr.webUIListenerCallback", |
175 remover_ = nullptr; | 178 base::StringValue("browsing-data-removing"), |
179 base::FundamentalValue(is_removing)); | |
180 | |
181 if (is_removing || webui_callback_id_.empty()) | |
182 return; | |
176 | 183 |
177 PrefService* prefs = profile_->GetPrefs(); | 184 PrefService* prefs = profile_->GetPrefs(); |
178 int notice_shown_times = | 185 int notice_shown_times = |
179 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); | 186 prefs->GetInteger(prefs::kClearBrowsingDataHistoryNoticeShownTimes); |
180 | 187 |
181 // When the deletion is complete, we might show an additional dialog with | 188 // 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 | 189 // a notice about other forms of browsing history. This is the case if |
183 const bool show_notice = | 190 const bool show_notice = |
184 // 1. The dialog is relevant for the user. | 191 // 1. The dialog is relevant for the user. |
185 show_history_deletion_dialog_ && | 192 show_history_deletion_dialog_ && |
(...skipping 21 matching lines...) Expand all Loading... | |
207 CallJavascriptFunction( | 214 CallJavascriptFunction( |
208 "cr.webUIListenerCallback", | 215 "cr.webUIListenerCallback", |
209 base::StringValue("browsing-history-pref-changed"), | 216 base::StringValue("browsing-history-pref-changed"), |
210 base::FundamentalValue( | 217 base::FundamentalValue( |
211 profile_->GetPrefs()->GetBoolean( | 218 profile_->GetPrefs()->GetBoolean( |
212 prefs::kAllowDeletingBrowserHistory))); | 219 prefs::kAllowDeletingBrowserHistory))); |
213 } | 220 } |
214 | 221 |
215 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { | 222 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { |
216 AllowJavascript(); | 223 AllowJavascript(); |
224 const base::Value* callback_id; | |
225 CHECK(args->Get(0, &callback_id)); | |
217 | 226 |
218 // TODO(msramek): Simplify this using a factory. | 227 // TODO(msramek): Simplify this using a factory. |
219 AddCounter(base::WrapUnique(new AutofillCounter(profile_))); | 228 AddCounter(base::WrapUnique(new AutofillCounter(profile_))); |
dpapad
2016/07/08 23:09:43
initialize() can be called multiple times (close d
Dan Beam
2016/07/08 23:26:58
Done.
| |
220 AddCounter(base::WrapUnique(new CacheCounter(profile_))); | 229 AddCounter(base::WrapUnique(new CacheCounter(profile_))); |
221 AddCounter(base::WrapUnique(new DownloadsCounter(profile_))); | 230 AddCounter(base::WrapUnique(new DownloadsCounter(profile_))); |
222 AddCounter(base::WrapUnique(new HistoryCounter(profile_))); | 231 AddCounter(base::WrapUnique(new HistoryCounter(profile_))); |
223 AddCounter(base::WrapUnique(new HostedAppsCounter(profile_))); | 232 AddCounter(base::WrapUnique(new HostedAppsCounter(profile_))); |
224 AddCounter(base::WrapUnique(new PasswordsCounter(profile_))); | 233 AddCounter(base::WrapUnique(new PasswordsCounter(profile_))); |
225 AddCounter(base::WrapUnique(new MediaLicensesCounter(profile_))); | 234 AddCounter(base::WrapUnique(new MediaLicensesCounter(profile_))); |
226 | 235 |
227 OnStateChanged(); | 236 OnStateChanged(); |
228 RefreshHistoryNotice(); | 237 RefreshHistoryNotice(); |
238 | |
239 ResolveJavascriptCallback(*callback_id, | |
240 base::FundamentalValue(remover_->is_removing())); | |
229 } | 241 } |
230 | 242 |
231 void ClearBrowsingDataHandler::OnStateChanged() { | 243 void ClearBrowsingDataHandler::OnStateChanged() { |
232 CallJavascriptFunction( | 244 CallJavascriptFunction( |
233 "cr.webUIListenerCallback", | 245 "cr.webUIListenerCallback", |
234 base::StringValue("update-footer"), | 246 base::StringValue("update-footer"), |
235 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), | 247 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), |
236 base::FundamentalValue(show_history_footer_)); | 248 base::FundamentalValue(show_history_footer_)); |
237 } | 249 } |
238 | 250 |
(...skipping 23 matching lines...) Expand all Loading... | |
262 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) { | 274 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) { |
263 show_history_footer_ = show; | 275 show_history_footer_ = show; |
264 OnStateChanged(); | 276 OnStateChanged(); |
265 | 277 |
266 UMA_HISTOGRAM_BOOLEAN( | 278 UMA_HISTOGRAM_BOOLEAN( |
267 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated", | 279 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated", |
268 show_history_footer_); | 280 show_history_footer_); |
269 } | 281 } |
270 | 282 |
271 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) { | 283 void ClearBrowsingDataHandler::UpdateHistoryDeletionDialog(bool show) { |
272 // This is used by OnBrowsingDataRemoverDone (when the deletion finishes). | 284 // This is used by OnBrowsingDataRemoving (when the deletion finishes). |
273 show_history_deletion_dialog_ = show; | 285 show_history_deletion_dialog_ = show; |
274 } | 286 } |
275 | 287 |
276 void ClearBrowsingDataHandler::AddCounter( | 288 void ClearBrowsingDataHandler::AddCounter( |
277 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) { | 289 std::unique_ptr<browsing_data::BrowsingDataCounter> counter) { |
278 counter->Init(profile_->GetPrefs(), | 290 counter->Init(profile_->GetPrefs(), |
279 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText, | 291 base::Bind(&ClearBrowsingDataHandler::UpdateCounterText, |
280 base::Unretained(this))); | 292 base::Unretained(this))); |
281 counter->Restart(); | 293 counter->Restart(); |
282 counters_.push_back(std::move(counter)); | 294 counters_.push_back(std::move(counter)); |
283 } | 295 } |
284 | 296 |
285 void ClearBrowsingDataHandler::UpdateCounterText( | 297 void ClearBrowsingDataHandler::UpdateCounterText( |
286 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { | 298 std::unique_ptr<browsing_data::BrowsingDataCounter::Result> result) { |
287 CallJavascriptFunction( | 299 CallJavascriptFunction( |
288 "cr.webUIListenerCallback", | 300 "cr.webUIListenerCallback", |
289 base::StringValue("update-counter-text"), | 301 base::StringValue("update-counter-text"), |
290 base::StringValue(result->source()->GetPrefName()), | 302 base::StringValue(result->source()->GetPrefName()), |
291 base::StringValue(GetCounterTextFromResult(result.get()))); | 303 base::StringValue(GetCounterTextFromResult(result.get()))); |
292 } | 304 } |
293 | 305 |
294 } // namespace settings | 306 } // namespace settings |
OLD | NEW |