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_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/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 30 ResetProfileSettingsHandler::ResetProfileSettingsHandler() 31 : automatic_profile_resetter_(NULL), has_shown_confirmation_dialog_(false) { 32 google_brand::GetBrand(&brandcode_); 33 } 34 35 ResetProfileSettingsHandler::~ResetProfileSettingsHandler() {} 36 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 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 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 60 void ResetProfileSettingsHandler::GetLocalizedValues( 61 base::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 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))); 96 } 97 98 void ResetProfileSettingsHandler::HandleResetProfileSettings( 99 const base::ListValue* value) { 100 bool send_settings = false; 101 if (!value->GetBoolean(0, &send_settings)) 102 NOTREACHED(); 103 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, 109 Unretained(this), 110 send_settings)); 111 } else { 112 ResetProfile(send_settings); 113 } 114 } 115 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); 123 if (difference) { 124 setting_snapshot_->Subtract(current_snapshot); 125 std::string report = SerializeSettingsReport(*setting_snapshot_, 126 difference); 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); 131 } 132 } 133 setting_snapshot_.reset(); 134 if (automatic_profile_resetter_) { 135 automatic_profile_resetter_->NotifyDidCloseWebUIResetDialog( 136 true /*performed_reset*/); 137 } 138 } 139 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())); 147 UpdateFeedbackUI(); 148 } 149 150 if (automatic_profile_resetter_) 151 automatic_profile_resetter_->NotifyDidOpenWebUIResetDialog(); 152 has_shown_confirmation_dialog_ = true; 153 154 if (brandcode_.empty()) 155 return; 156 config_fetcher_.reset(new BrandcodeConfigFetcher( 157 base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched, 158 Unretained(this)), 159 GURL("https://tools.google.com/service/update2"), 160 brandcode_)); 161 } 162 163 void ResetProfileSettingsHandler::OnHideResetProfileDialog( 164 const base::ListValue* value) { 165 if (!resetter_->IsActive()) 166 setting_snapshot_.reset(); 167 } 168 169 void ResetProfileSettingsHandler::OnDismissedResetProfileSettingsBanner( 170 const base::ListValue* args) { 171 if (automatic_profile_resetter_) 172 automatic_profile_resetter_->NotifyDidCloseWebUIResetBanner(); 173 } 174 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'. 179 } 180 181 void ResetProfileSettingsHandler::ResetProfile(bool send_settings) { 182 DCHECK(resetter_); 183 DCHECK(!resetter_->IsActive()); 184 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(); 190 } else { 191 DCHECK(brandcode_.empty()); 192 } 193 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); 198 resetter_->Reset( 199 ProfileResetter::ALL, 200 default_settings.Pass(), 201 send_settings, 202 base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone, 203 AsWeakPtr(), 204 send_settings)); 205 content::RecordAction(base::UserMetricsAction("ResetProfile")); 206 UMA_HISTOGRAM_BOOLEAN("ProfileReset.SendFeedback", send_settings); 207 } 208 209 void ResetProfileSettingsHandler::UpdateFeedbackUI() { 210 if (!setting_snapshot_) 211 return; 212 scoped_ptr<base::ListValue> list = GetReadableFeedbackForSnapshot( 213 Profile::FromWebUI(web_ui()), 214 *setting_snapshot_); 215 base::DictionaryValue feedback_info; 216 feedback_info.Set("feedbackInfo", list.release()); 217 web_ui()->CallJavascriptFunction( 218 "ResetProfileSettingsOverlay.setFeedbackInfo", 219 feedback_info); 220 } 221 222 } // namespace options 223