Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(1173)

Side by Side Diff: chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.cc

Issue 1961183002: MD Settings: Update some Settings handlers to use new JS lifecycle management. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 7 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
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/metrics/histogram_macros.h" 10 #include "base/metrics/histogram_macros.h"
11 #include "base/metrics/sparse_histogram.h" 11 #include "base/metrics/sparse_histogram.h"
12 #include "chrome/browser/browsing_data/browsing_data_helper.h" 12 #include "chrome/browser/browsing_data/browsing_data_helper.h"
13 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h" 13 #include "chrome/browser/browsing_data/browsing_data_remover_factory.h"
14 #include "chrome/browser/history/web_history_service_factory.h" 14 #include "chrome/browser/history/web_history_service_factory.h"
15 #include "chrome/browser/sync/profile_sync_service_factory.h" 15 #include "chrome/browser/sync/profile_sync_service_factory.h"
16 #include "chrome/common/pref_names.h" 16 #include "chrome/common/pref_names.h"
17 #include "components/browsing_data_ui/history_notice_utils.h" 17 #include "components/browsing_data_ui/history_notice_utils.h"
18 #include "components/prefs/pref_service.h" 18 #include "components/prefs/pref_service.h"
19 #include "content/public/browser/web_ui.h" 19 #include "content/public/browser/web_ui.h"
20 20
21 namespace settings { 21 namespace settings {
22 22
23 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui) 23 ClearBrowsingDataHandler::ClearBrowsingDataHandler(content::WebUI* webui)
24 : sync_service_(nullptr), 24 : sync_service_(nullptr),
25 sync_service_observer_(this),
25 remover_(nullptr), 26 remover_(nullptr),
26 should_show_history_footer_(false), 27 should_show_history_footer_(false),
27 weak_ptr_factory_(this) { 28 weak_ptr_factory_(this) {
28 PrefService* prefs = Profile::FromWebUI(webui)->GetPrefs(); 29 PrefService* prefs = Profile::FromWebUI(webui)->GetPrefs();
29 clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, prefs); 30 clear_plugin_lso_data_enabled_.Init(prefs::kClearPluginLSODataEnabled, prefs);
30 pepper_flash_settings_enabled_.Init(prefs::kPepperFlashSettingsEnabled, 31 pepper_flash_settings_enabled_.Init(prefs::kPepperFlashSettingsEnabled,
31 prefs); 32 prefs);
32 allow_deleting_browser_history_.Init(
33 prefs::kAllowDeletingBrowserHistory, prefs,
34 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged,
35 base::Unretained(this)));
36 sync_service_ = 33 sync_service_ =
37 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(webui)); 34 ProfileSyncServiceFactory::GetForProfile(Profile::FromWebUI(webui));
38 } 35 }
39 36
40 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() { 37 ClearBrowsingDataHandler::~ClearBrowsingDataHandler() {
41 if (remover_) 38 if (remover_)
42 remover_->RemoveObserver(this); 39 remover_->RemoveObserver(this);
43 } 40 }
44 41
45 void ClearBrowsingDataHandler::RegisterMessages() { 42 void ClearBrowsingDataHandler::RegisterMessages() {
46 web_ui()->RegisterMessageCallback( 43 web_ui()->RegisterMessageCallback(
47 "clearBrowsingData", 44 "clearBrowsingData",
48 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData, 45 base::Bind(&ClearBrowsingDataHandler::HandleClearBrowsingData,
49 base::Unretained(this))); 46 base::Unretained(this)));
50 47
51 web_ui()->RegisterMessageCallback( 48 web_ui()->RegisterMessageCallback(
52 "initializeClearBrowsingData", 49 "initializeClearBrowsingData",
53 base::Bind(&ClearBrowsingDataHandler::HandleInitialize, 50 base::Bind(&ClearBrowsingDataHandler::HandleInitialize,
54 base::Unretained(this))); 51 base::Unretained(this)));
55 } 52 }
56 53
57 void ClearBrowsingDataHandler::OnJavascriptAllowed() { 54 void ClearBrowsingDataHandler::OnJavascriptAllowed() {
55 PrefService* prefs = Profile::FromWebUI(web_ui())->GetPrefs();
56 allow_deleting_browser_history_.Init(
57 prefs::kAllowDeletingBrowserHistory, prefs,
58 base::Bind(&ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged,
59 base::Unretained(this)));
60
58 if (sync_service_) 61 if (sync_service_)
59 sync_service_->AddObserver(this); 62 sync_service_observer_.Add(sync_service_);
60 } 63 }
61 64
62 void ClearBrowsingDataHandler::OnJavascriptDisallowed() { 65 void ClearBrowsingDataHandler::OnJavascriptDisallowed() {
63 if (sync_service_) 66 allow_deleting_browser_history_.Destroy();
64 sync_service_->RemoveObserver(this); 67 sync_service_observer_.RemoveAll();
65 } 68 }
66 69
67 void ClearBrowsingDataHandler::HandleClearBrowsingData( 70 void ClearBrowsingDataHandler::HandleClearBrowsingData(
68 const base::ListValue* args) { 71 const base::ListValue* args) {
69 // We should never be called when the previous clearing has not yet finished. 72 // We should never be called when the previous clearing has not yet finished.
70 CHECK(!remover_); 73 CHECK(!remover_);
71 CHECK_EQ(1U, args->GetSize()); 74 CHECK_EQ(1U, args->GetSize());
72 CHECK(webui_callback_id_.empty()); 75 CHECK(webui_callback_id_.empty());
73 CHECK(args->GetString(0, &webui_callback_id_)); 76 CHECK(args->GetString(0, &webui_callback_id_));
74 77
(...skipping 86 matching lines...) Expand 10 before | Expand all | Expand 10 after
161 void ClearBrowsingDataHandler::OnBrowsingDataRemoverDone() { 164 void ClearBrowsingDataHandler::OnBrowsingDataRemoverDone() {
162 remover_->RemoveObserver(this); 165 remover_->RemoveObserver(this);
163 remover_ = nullptr; 166 remover_ = nullptr;
164 ResolveJavascriptCallback( 167 ResolveJavascriptCallback(
165 base::StringValue(webui_callback_id_), 168 base::StringValue(webui_callback_id_),
166 *base::Value::CreateNullValue()); 169 *base::Value::CreateNullValue());
167 webui_callback_id_.clear(); 170 webui_callback_id_.clear();
168 } 171 }
169 172
170 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() { 173 void ClearBrowsingDataHandler::OnBrowsingHistoryPrefChanged() {
171 web_ui()->CallJavascriptFunction( 174 // This is lame, but PrefMembers don't support starting/stopping observers.
175 if (!IsJavascriptAllowed())
176 return;
Dan Beam 2016/05/11 17:19:57 ^ did you write this before you found Destroy()?
tommycli 2016/05/11 17:22:05 Done. Oops yes exactly. Removed.
177
178 CallJavascriptFunction(
172 "cr.webUIListenerCallback", 179 "cr.webUIListenerCallback",
173 base::StringValue("browsing-history-pref-changed"), 180 base::StringValue("browsing-history-pref-changed"),
174 base::FundamentalValue(*allow_deleting_browser_history_)); 181 base::FundamentalValue(*allow_deleting_browser_history_));
175 } 182 }
176 183
177 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) { 184 void ClearBrowsingDataHandler::HandleInitialize(const base::ListValue* args) {
178 AllowJavascript(); 185 AllowJavascript();
179 OnStateChanged(); 186 OnStateChanged();
180 RefreshHistoryNotice(); 187 RefreshHistoryNotice();
181 } 188 }
182 189
183 void ClearBrowsingDataHandler::OnStateChanged() { 190 void ClearBrowsingDataHandler::OnStateChanged() {
184 web_ui()->CallJavascriptFunction( 191 CallJavascriptFunction(
185 "cr.webUIListenerCallback", 192 "cr.webUIListenerCallback",
186 base::StringValue("update-footer"), 193 base::StringValue("update-footer"),
187 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()), 194 base::FundamentalValue(sync_service_ && sync_service_->IsSyncActive()),
188 base::FundamentalValue(should_show_history_footer_)); 195 base::FundamentalValue(should_show_history_footer_));
189 } 196 }
190 197
191 void ClearBrowsingDataHandler::RefreshHistoryNotice() { 198 void ClearBrowsingDataHandler::RefreshHistoryNotice() {
192 browsing_data_ui::ShouldShowNoticeAboutOtherFormsOfBrowsingHistory( 199 browsing_data_ui::ShouldShowNoticeAboutOtherFormsOfBrowsingHistory(
193 sync_service_, 200 sync_service_,
194 WebHistoryServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())), 201 WebHistoryServiceFactory::GetForProfile(Profile::FromWebUI(web_ui())),
195 base::Bind(&ClearBrowsingDataHandler::UpdateHistoryNotice, 202 base::Bind(&ClearBrowsingDataHandler::UpdateHistoryNotice,
196 weak_ptr_factory_.GetWeakPtr())); 203 weak_ptr_factory_.GetWeakPtr()));
197 } 204 }
198 205
199 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) { 206 void ClearBrowsingDataHandler::UpdateHistoryNotice(bool show) {
200 should_show_history_footer_ = show; 207 should_show_history_footer_ = show;
201 OnStateChanged(); 208 OnStateChanged();
202 209
203 UMA_HISTOGRAM_BOOLEAN( 210 UMA_HISTOGRAM_BOOLEAN(
204 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated", 211 "History.ClearBrowsingData.HistoryNoticeShownInFooterWhenUpdated",
205 should_show_history_footer_); 212 should_show_history_footer_);
206 } 213 }
207 214
208 } // namespace settings 215 } // namespace settings
OLDNEW
« no previous file with comments | « chrome/browser/ui/webui/settings/settings_clear_browsing_data_handler.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698