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("onDismissedResetProfileSettingsBanner",
90 base::Bind(&ResetProfileSettingsHandler::
91 OnDismissedResetProfileSettingsBanner
,
92 base::Unretained(this)));
95 void ResetProfileSettingsHandler::HandleResetProfileSettings(
96 const base::ListValue
* value
) {
97 bool send_settings
= false;
98 if (!value
->GetBoolean(0, &send_settings
))
101 DCHECK(brandcode_
.empty() || config_fetcher_
);
102 if (config_fetcher_
&& config_fetcher_
->IsActive()) {
103 // Reset once the prefs are fetched.
104 config_fetcher_
->SetCallback(
105 base::Bind(&ResetProfileSettingsHandler::ResetProfile
,
109 ResetProfile(send_settings
);
113 void ResetProfileSettingsHandler::OnResetProfileSettingsDone() {
114 web_ui()->CallJavascriptFunction("ResetProfileSettingsOverlay.doneResetting");
115 if (setting_snapshot_
) {
116 Profile
* profile
= Profile::FromWebUI(web_ui());
117 ResettableSettingsSnapshot
current_snapshot(profile
);
118 int difference
= setting_snapshot_
->FindDifferentFields(current_snapshot
);
120 setting_snapshot_
->Subtract(current_snapshot
);
121 std::string report
= SerializeSettingsReport(*setting_snapshot_
,
123 bool is_reset_prompt_active
= automatic_profile_resetter_
&&
124 automatic_profile_resetter_
->IsResetPromptFlowActive();
125 SendSettingsFeedback(report
, profile
, is_reset_prompt_active
?
126 PROFILE_RESET_PROMPT
: PROFILE_RESET_WEBUI
);
128 setting_snapshot_
.reset();
130 if (automatic_profile_resetter_
) {
131 automatic_profile_resetter_
->NotifyDidCloseWebUIResetDialog(
132 true /*performed_reset*/);
136 void ResetProfileSettingsHandler::OnShowResetProfileDialog(
137 const base::ListValue
*) {
138 base::DictionaryValue flashInfo
;
139 flashInfo
.Set("feedbackInfo", GetReadableFeedback(
140 Profile::FromWebUI(web_ui())));
141 web_ui()->CallJavascriptFunction(
142 "ResetProfileSettingsOverlay.setFeedbackInfo",
145 if (automatic_profile_resetter_
)
146 automatic_profile_resetter_
->NotifyDidOpenWebUIResetDialog();
147 has_shown_confirmation_dialog_
= true;
149 if (brandcode_
.empty())
151 config_fetcher_
.reset(new BrandcodeConfigFetcher(
152 base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched
,
154 GURL("https://tools.google.com/service/update2"),
158 void ResetProfileSettingsHandler::OnDismissedResetProfileSettingsBanner(
159 const base::ListValue
* args
) {
160 if (automatic_profile_resetter_
)
161 automatic_profile_resetter_
->NotifyDidCloseWebUIResetBanner();
164 void ResetProfileSettingsHandler::OnSettingsFetched() {
165 DCHECK(config_fetcher_
);
166 DCHECK(!config_fetcher_
->IsActive());
167 // The master prefs is fetched. We are waiting for user pressing 'Reset'.
170 void ResetProfileSettingsHandler::ResetProfile(bool send_settings
) {
172 DCHECK(!resetter_
->IsActive());
174 scoped_ptr
<BrandcodedDefaultSettings
> default_settings
;
175 if (config_fetcher_
) {
176 DCHECK(!config_fetcher_
->IsActive());
177 default_settings
= config_fetcher_
->GetSettings();
178 config_fetcher_
.reset();
180 DCHECK(brandcode_
.empty());
183 // If failed to fetch BrandcodedDefaultSettings or this is an organic
184 // installation, use default settings.
185 if (!default_settings
)
186 default_settings
.reset(new BrandcodedDefaultSettings
);
187 // Save current settings if required.
188 setting_snapshot_
.reset(send_settings
?
189 new ResettableSettingsSnapshot(Profile::FromWebUI(web_ui())) : NULL
);
191 ProfileResetter::ALL
,
192 default_settings
.Pass(),
193 base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone
,
195 content::RecordAction(base::UserMetricsAction("ResetProfile"));
196 UMA_HISTOGRAM_BOOLEAN("ProfileReset.SendFeedback", send_settings
);
199 } // namespace options