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.
4
5 #include "chrome/browser/ui/webui/options/reset_profile_settings_handler.h"
6
7 #include "base/bind.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"
27
28 namespace options {
29
ResetProfileSettingsHandler()30 ResetProfileSettingsHandler::ResetProfileSettingsHandler()
31 : automatic_profile_resetter_(NULL), has_shown_confirmation_dialog_(false) {
32 google_util::GetBrand(&brandcode_);
33 }
34
~ResetProfileSettingsHandler()35 ResetProfileSettingsHandler::~ResetProfileSettingsHandler() {}
36
InitializeHandler()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);
42 }
43
InitializePage()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");
51 }
52
Uninitialize()53 void ResetProfileSettingsHandler::Uninitialize() {
54 if (has_shown_confirmation_dialog_ && automatic_profile_resetter_) {
55 automatic_profile_resetter_->NotifyDidCloseWebUIResetDialog(
56 false /*performed_reset*/);
57 }
58 }
59
GetLocalizedValues(DictionaryValue * localized_strings)60 void ResetProfileSettingsHandler::GetLocalizedValues(
61 DictionaryValue* localized_strings) {
62 DCHECK(localized_strings);
63
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 }
71 };
72
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);
79 }
80
RegisterMessages()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)));
93 }
94
HandleResetProfileSettings(const ListValue * value)95 void ResetProfileSettingsHandler::HandleResetProfileSettings(
96 const ListValue* value) {
97 bool send_settings = false;
98 if (!value->GetBoolean(0, &send_settings))
99 NOTREACHED();
100
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,
106 Unretained(this),
107 send_settings));
108 } else {
109 ResetProfile(send_settings);
110 }
111 }
112
OnResetProfileSettingsDone()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);
119 if (difference) {
120 setting_snapshot_->Subtract(current_snapshot);
121 std::string report = SerializeSettingsReport(*setting_snapshot_,
122 difference);
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);
127 }
128 setting_snapshot_.reset();
129 }
130 if (automatic_profile_resetter_) {
131 automatic_profile_resetter_->NotifyDidCloseWebUIResetDialog(
132 true /*performed_reset*/);
133 }
134 }
135
OnShowResetProfileDialog(const ListValue *)136 void ResetProfileSettingsHandler::OnShowResetProfileDialog(const ListValue*) {
137 DictionaryValue flashInfo;
138 flashInfo.Set("feedbackInfo", GetReadableFeedback(
139 Profile::FromWebUI(web_ui())));
140 web_ui()->CallJavascriptFunction(
141 "ResetProfileSettingsOverlay.setFeedbackInfo",
142 flashInfo);
143
144 if (automatic_profile_resetter_)
145 automatic_profile_resetter_->NotifyDidOpenWebUIResetDialog();
146 has_shown_confirmation_dialog_ = true;
147
148 if (brandcode_.empty())
149 return;
150 config_fetcher_.reset(new BrandcodeConfigFetcher(
151 base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched,
152 Unretained(this)),
153 GURL("https://tools.google.com/service/update2"),
154 brandcode_));
155 }
156
OnDismissedResetProfileSettingsBanner(const base::ListValue * args)157 void ResetProfileSettingsHandler::OnDismissedResetProfileSettingsBanner(
158 const base::ListValue* args) {
159 if (automatic_profile_resetter_)
160 automatic_profile_resetter_->NotifyDidCloseWebUIResetBanner();
161 }
162
OnSettingsFetched()163 void ResetProfileSettingsHandler::OnSettingsFetched() {
164 DCHECK(config_fetcher_);
165 DCHECK(!config_fetcher_->IsActive());
166 // The master prefs is fetched. We are waiting for user pressing 'Reset'.
167 }
168
ResetProfile(bool send_settings)169 void ResetProfileSettingsHandler::ResetProfile(bool send_settings) {
170 DCHECK(resetter_);
171 DCHECK(!resetter_->IsActive());
172
173 scoped_ptr<BrandcodedDefaultSettings> default_settings;
174 if (config_fetcher_) {
175 DCHECK(!config_fetcher_->IsActive());
176 default_settings = config_fetcher_->GetSettings();
177 config_fetcher_.reset();
178 } else {
179 DCHECK(brandcode_.empty());
180 }
181
182 // If failed to fetch BrandcodedDefaultSettings or this is an organic
183 // installation, use default settings.
184 if (!default_settings)
185 default_settings.reset(new BrandcodedDefaultSettings);
186 // Save current settings if required.
187 setting_snapshot_.reset(send_settings ?
188 new ResettableSettingsSnapshot(Profile::FromWebUI(web_ui())) : NULL);
189 resetter_->Reset(
190 ProfileResetter::ALL,
191 default_settings.Pass(),
192 base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone,
193 AsWeakPtr()));
194 content::RecordAction(content::UserMetricsAction("ResetProfile"));
195 UMA_HISTOGRAM_BOOLEAN("ProfileReset.SendFeedback", send_settings);
196 }
197
198 } // namespace options
199