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_util.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/pref_names.h"
22 #include "chrome/common/url_constants.h"
23 #include "content/public/browser/user_metrics.h"
24 #include "content/public/browser/web_ui.h"
25 #include "grit/generated_resources.h"
26 #include "ui/base/l10n/l10n_util.h"
30 ResetProfileSettingsHandler::ResetProfileSettingsHandler()
31 : automatic_profile_resetter_(NULL
), has_shown_confirmation_dialog_(false) {
32 google_util::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");
53 void ResetProfileSettingsHandler::Uninitialize() {
54 if (has_shown_confirmation_dialog_
&& automatic_profile_resetter_
) {
55 automatic_profile_resetter_
->NotifyDidCloseWebUIResetDialog(
56 false /*performed_reset*/);
60 void ResetProfileSettingsHandler::GetLocalizedValues(
61 base::DictionaryValue
* localized_strings
) {
62 DCHECK(localized_strings
);
64 static OptionsStringResource resources
[] = {
65 { "resetProfileSettingsBannerText",
66 IDS_RESET_PROFILE_SETTINGS_BANNER_TEXT
},
67 { "resetProfileSettingsCommit", IDS_RESET_PROFILE_SETTINGS_COMMIT_BUTTON
},
68 { "resetProfileSettingsExplanation",
69 IDS_RESET_PROFILE_SETTINGS_EXPLANATION
},
70 { "resetProfileSettingsFeedback", IDS_RESET_PROFILE_SETTINGS_FEEDBACK
}
73 RegisterStrings(localized_strings
, resources
, arraysize(resources
));
74 RegisterTitle(localized_strings
, "resetProfileSettingsOverlay",
75 IDS_RESET_PROFILE_SETTINGS_TITLE
);
76 localized_strings
->SetString(
77 "resetProfileSettingsLearnMoreUrl",
78 chrome::kResetProfileSettingsLearnMoreURL
);
81 void ResetProfileSettingsHandler::RegisterMessages() {
82 // Setup handlers specific to this panel.
83 web_ui()->RegisterMessageCallback("performResetProfileSettings",
84 base::Bind(&ResetProfileSettingsHandler::HandleResetProfileSettings
,
85 base::Unretained(this)));
86 web_ui()->RegisterMessageCallback("onShowResetProfileDialog",
87 base::Bind(&ResetProfileSettingsHandler::OnShowResetProfileDialog
,
88 base::Unretained(this)));
89 web_ui()->RegisterMessageCallback("onHideResetProfileDialog",
90 base::Bind(&ResetProfileSettingsHandler::OnHideResetProfileDialog
,
91 base::Unretained(this)));
92 web_ui()->RegisterMessageCallback("onDismissedResetProfileSettingsBanner",
93 base::Bind(&ResetProfileSettingsHandler::
94 OnDismissedResetProfileSettingsBanner
,
95 base::Unretained(this)));
98 void ResetProfileSettingsHandler::HandleResetProfileSettings(
99 const base::ListValue
* value
) {
100 bool send_settings
= false;
101 if (!value
->GetBoolean(0, &send_settings
))
104 DCHECK(brandcode_
.empty() || config_fetcher_
);
105 if (config_fetcher_
&& config_fetcher_
->IsActive()) {
106 // Reset once the prefs are fetched.
107 config_fetcher_
->SetCallback(
108 base::Bind(&ResetProfileSettingsHandler::ResetProfile
,
112 ResetProfile(send_settings
);
116 void ResetProfileSettingsHandler::OnResetProfileSettingsDone(
117 bool send_feedback
) {
118 web_ui()->CallJavascriptFunction("ResetProfileSettingsOverlay.doneResetting");
119 if (send_feedback
&& setting_snapshot_
) {
120 Profile
* profile
= Profile::FromWebUI(web_ui());
121 ResettableSettingsSnapshot
current_snapshot(profile
);
122 int difference
= setting_snapshot_
->FindDifferentFields(current_snapshot
);
124 setting_snapshot_
->Subtract(current_snapshot
);
125 std::string report
= SerializeSettingsReport(*setting_snapshot_
,
127 bool is_reset_prompt_active
= automatic_profile_resetter_
&&
128 automatic_profile_resetter_
->IsResetPromptFlowActive();
129 SendSettingsFeedback(report
, profile
, is_reset_prompt_active
?
130 PROFILE_RESET_PROMPT
: PROFILE_RESET_WEBUI
);
133 setting_snapshot_
.reset();
134 if (automatic_profile_resetter_
) {
135 automatic_profile_resetter_
->NotifyDidCloseWebUIResetDialog(
136 true /*performed_reset*/);
140 void ResetProfileSettingsHandler::OnShowResetProfileDialog(
141 const base::ListValue
* value
) {
142 if (!resetter_
->IsActive()) {
143 setting_snapshot_
.reset(
144 new ResettableSettingsSnapshot(Profile::FromWebUI(web_ui())));
145 setting_snapshot_
->RequestShortcuts(base::Bind(
146 &ResetProfileSettingsHandler::UpdateFeedbackUI
, AsWeakPtr()));
150 if (automatic_profile_resetter_
)
151 automatic_profile_resetter_
->NotifyDidOpenWebUIResetDialog();
152 has_shown_confirmation_dialog_
= true;
154 if (brandcode_
.empty())
156 config_fetcher_
.reset(new BrandcodeConfigFetcher(
157 base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched
,
159 GURL("https://tools.google.com/service/update2"),
163 void ResetProfileSettingsHandler::OnHideResetProfileDialog(
164 const base::ListValue
* value
) {
165 if (!resetter_
->IsActive())
166 setting_snapshot_
.reset();
169 void ResetProfileSettingsHandler::OnDismissedResetProfileSettingsBanner(
170 const base::ListValue
* args
) {
171 if (automatic_profile_resetter_
)
172 automatic_profile_resetter_
->NotifyDidCloseWebUIResetBanner();
175 void ResetProfileSettingsHandler::OnSettingsFetched() {
176 DCHECK(config_fetcher_
);
177 DCHECK(!config_fetcher_
->IsActive());
178 // The master prefs is fetched. We are waiting for user pressing 'Reset'.
181 void ResetProfileSettingsHandler::ResetProfile(bool send_settings
) {
183 DCHECK(!resetter_
->IsActive());
185 scoped_ptr
<BrandcodedDefaultSettings
> default_settings
;
186 if (config_fetcher_
) {
187 DCHECK(!config_fetcher_
->IsActive());
188 default_settings
= config_fetcher_
->GetSettings();
189 config_fetcher_
.reset();
191 DCHECK(brandcode_
.empty());
194 // If failed to fetch BrandcodedDefaultSettings or this is an organic
195 // installation, use default settings.
196 if (!default_settings
)
197 default_settings
.reset(new BrandcodedDefaultSettings
);
199 ProfileResetter::ALL
,
200 default_settings
.Pass(),
201 base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone
,
204 content::RecordAction(base::UserMetricsAction("ResetProfile"));
205 UMA_HISTOGRAM_BOOLEAN("ProfileReset.SendFeedback", send_settings
);
208 void ResetProfileSettingsHandler::UpdateFeedbackUI() {
209 if (!setting_snapshot_
)
211 scoped_ptr
<base::ListValue
> list
= GetReadableFeedbackForSnapshot(
212 Profile::FromWebUI(web_ui()),
214 base::DictionaryValue feedback_info
;
215 feedback_info
.Set("feedbackInfo", list
.release());
216 web_ui()->CallJavascriptFunction(
217 "ResetProfileSettingsOverlay.setFeedbackInfo",
221 } // namespace options