1 // Copyright 2013 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.
5 #include "chrome/browser/ui/webui/options/reset_profile_settings_handler.h"
8 #include "base/bind_helpers.h"
9 #include "base/metrics/histogram.h"
10 #include "base/prefs/pref_service.h"
11 #include "base/strings/string16.h"
12 #include "base/values.h"
13 #include "chrome/browser/google/google_brand.h"
14 #include "chrome/browser/profile_resetter/automatic_profile_resetter.h"
15 #include "chrome/browser/profile_resetter/automatic_profile_resetter_factory.h"
16 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
17 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
18 #include "chrome/browser/profile_resetter/profile_resetter.h"
19 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
20 #include "chrome/browser/profiles/profile.h"
21 #include "chrome/common/url_constants.h"
22 #include "chrome/grit/chromium_strings.h"
23 #include "chrome/grit/generated_resources.h"
24 #include "content/public/browser/user_metrics.h"
25 #include "content/public/browser/web_ui.h"
29 ResetProfileSettingsHandler::ResetProfileSettingsHandler()
30 : automatic_profile_resetter_(NULL
),
31 has_shown_confirmation_dialog_(false) {
32 google_brand::GetBrand(&brandcode_
);
35 ResetProfileSettingsHandler::~ResetProfileSettingsHandler() {}
37 void ResetProfileSettingsHandler::InitializeHandler() {
38 Profile
* profile
= Profile::FromWebUI(web_ui());
39 resetter_
.reset(new ProfileResetter(profile
));
40 automatic_profile_resetter_
=
41 AutomaticProfileResetterFactory::GetForBrowserContext(profile
);
44 void ResetProfileSettingsHandler::InitializePage() {
45 web_ui()->CallJavascriptFunction(
46 "ResetProfileSettingsOverlay.setResettingState",
47 base::FundamentalValue(resetter_
->IsActive()));
48 if (automatic_profile_resetter_
&&
49 automatic_profile_resetter_
->ShouldShowResetBanner()) {
50 web_ui()->CallJavascriptFunction("ResetProfileSettingsBanner.show");
54 void ResetProfileSettingsHandler::Uninitialize() {
55 if (has_shown_confirmation_dialog_
&& automatic_profile_resetter_
) {
56 automatic_profile_resetter_
->NotifyDidCloseWebUIResetDialog(
57 false /*performed_reset*/);
61 void ResetProfileSettingsHandler::GetLocalizedValues(
62 base::DictionaryValue
* localized_strings
) {
63 DCHECK(localized_strings
);
65 static OptionsStringResource resources
[] = {
66 { "resetProfileSettingsBannerText",
67 IDS_RESET_PROFILE_SETTINGS_BANNER_TEXT
},
68 { "resetProfileSettingsCommit", IDS_RESET_PROFILE_SETTINGS_COMMIT_BUTTON
},
69 { "resetProfileSettingsExplanation",
70 IDS_RESET_PROFILE_SETTINGS_EXPLANATION
},
71 { "resetProfileSettingsFeedback", IDS_RESET_PROFILE_SETTINGS_FEEDBACK
}
74 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
75 RegisterTitle(localized_strings
, "resetProfileSettingsOverlay",
76 IDS_RESET_PROFILE_SETTINGS_TITLE
);
77 localized_strings
->SetString(
78 "resetProfileSettingsLearnMoreUrl",
79 chrome::kResetProfileSettingsLearnMoreURL
);
82 void ResetProfileSettingsHandler::RegisterMessages() {
83 // Setup handlers specific to this panel.
84 web_ui()->RegisterMessageCallback("performResetProfileSettings",
85 base::Bind(&ResetProfileSettingsHandler::HandleResetProfileSettings
,
86 base::Unretained(this)));
87 web_ui()->RegisterMessageCallback("onShowResetProfileDialog",
88 base::Bind(&ResetProfileSettingsHandler::OnShowResetProfileDialog
,
89 base::Unretained(this)));
90 web_ui()->RegisterMessageCallback("onHideResetProfileDialog",
91 base::Bind(&ResetProfileSettingsHandler::OnHideResetProfileDialog
,
92 base::Unretained(this)));
93 web_ui()->RegisterMessageCallback("onDismissedResetProfileSettingsBanner",
94 base::Bind(&ResetProfileSettingsHandler::
95 OnDismissedResetProfileSettingsBanner
,
96 base::Unretained(this)));
99 void ResetProfileSettingsHandler::HandleResetProfileSettings(
100 const base::ListValue
* value
) {
101 bool send_settings
= false;
102 if (!value
->GetBoolean(0, &send_settings
))
105 DCHECK(brandcode_
.empty() || config_fetcher_
);
106 if (config_fetcher_
&& config_fetcher_
->IsActive()) {
107 // Reset once the prefs are fetched.
108 config_fetcher_
->SetCallback(
109 base::Bind(&ResetProfileSettingsHandler::ResetProfile
,
113 ResetProfile(send_settings
);
117 void ResetProfileSettingsHandler::OnResetProfileSettingsDone(
118 bool send_feedback
) {
119 web_ui()->CallJavascriptFunction("ResetProfileSettingsOverlay.doneResetting");
120 if (send_feedback
&& setting_snapshot_
) {
121 Profile
* profile
= Profile::FromWebUI(web_ui());
122 ResettableSettingsSnapshot
current_snapshot(profile
);
123 int difference
= setting_snapshot_
->FindDifferentFields(current_snapshot
);
125 setting_snapshot_
->Subtract(current_snapshot
);
126 std::string report
= SerializeSettingsReport(*setting_snapshot_
,
128 bool is_reset_prompt_active
= automatic_profile_resetter_
&&
129 automatic_profile_resetter_
->IsResetPromptFlowActive();
130 SendSettingsFeedback(report
, profile
, is_reset_prompt_active
?
131 PROFILE_RESET_PROMPT
: PROFILE_RESET_WEBUI
);
134 setting_snapshot_
.reset();
135 if (automatic_profile_resetter_
) {
136 automatic_profile_resetter_
->NotifyDidCloseWebUIResetDialog(
137 true /*performed_reset*/);
141 void ResetProfileSettingsHandler::OnShowResetProfileDialog(
142 const base::ListValue
* value
) {
143 if (!resetter_
->IsActive()) {
144 setting_snapshot_
.reset(
145 new ResettableSettingsSnapshot(Profile::FromWebUI(web_ui())));
146 setting_snapshot_
->RequestShortcuts(base::Bind(
147 &ResetProfileSettingsHandler::UpdateFeedbackUI
, AsWeakPtr()));
151 if (automatic_profile_resetter_
)
152 automatic_profile_resetter_
->NotifyDidOpenWebUIResetDialog();
153 has_shown_confirmation_dialog_
= true;
155 if (brandcode_
.empty())
157 config_fetcher_
.reset(new BrandcodeConfigFetcher(
158 base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched
,
160 GURL("https://tools.google.com/service/update2"),
164 void ResetProfileSettingsHandler::OnHideResetProfileDialog(
165 const base::ListValue
* value
) {
166 if (!resetter_
->IsActive())
167 setting_snapshot_
.reset();
170 void ResetProfileSettingsHandler::OnDismissedResetProfileSettingsBanner(
171 const base::ListValue
* args
) {
172 if (automatic_profile_resetter_
)
173 automatic_profile_resetter_
->NotifyDidCloseWebUIResetBanner();
176 void ResetProfileSettingsHandler::OnSettingsFetched() {
177 DCHECK(config_fetcher_
);
178 DCHECK(!config_fetcher_
->IsActive());
179 // The master prefs is fetched. We are waiting for user pressing 'Reset'.
182 void ResetProfileSettingsHandler::ResetProfile(bool send_settings
) {
184 DCHECK(!resetter_
->IsActive());
186 scoped_ptr
<BrandcodedDefaultSettings
> default_settings
;
187 if (config_fetcher_
) {
188 DCHECK(!config_fetcher_
->IsActive());
189 default_settings
= config_fetcher_
->GetSettings();
190 config_fetcher_
.reset();
192 DCHECK(brandcode_
.empty());
195 // If failed to fetch BrandcodedDefaultSettings or this is an organic
196 // installation, use default settings.
197 if (!default_settings
)
198 default_settings
.reset(new BrandcodedDefaultSettings
);
200 ProfileResetter::ALL
,
201 default_settings
.Pass(),
203 base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone
,
206 content::RecordAction(base::UserMetricsAction("ResetProfile"));
207 UMA_HISTOGRAM_BOOLEAN("ProfileReset.SendFeedback", send_settings
);
210 void ResetProfileSettingsHandler::UpdateFeedbackUI() {
211 if (!setting_snapshot_
)
213 scoped_ptr
<base::ListValue
> list
= GetReadableFeedbackForSnapshot(
214 Profile::FromWebUI(web_ui()),
216 base::DictionaryValue feedback_info
;
217 feedback_info
.Set("feedbackInfo", list
.release());
218 web_ui()->CallJavascriptFunction(
219 "ResetProfileSettingsOverlay.setFeedbackInfo",
223 } // namespace options