Home | History | Annotate | Download | only in options
      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/prefs/pref_service.h"
     10 #include "base/strings/string16.h"
     11 #include "base/values.h"
     12 #include "chrome/browser/google/google_util.h"
     13 #include "chrome/browser/profile_resetter/brandcode_config_fetcher.h"
     14 #include "chrome/browser/profile_resetter/brandcoded_default_settings.h"
     15 #include "chrome/browser/profile_resetter/profile_resetter.h"
     16 #include "chrome/browser/profile_resetter/resettable_settings_snapshot.h"
     17 #include "chrome/browser/profiles/profile.h"
     18 #include "chrome/common/pref_names.h"
     19 #include "content/public/browser/user_metrics.h"
     20 #include "content/public/browser/web_ui.h"
     21 #include "grit/generated_resources.h"
     22 #include "ui/base/l10n/l10n_util.h"
     23 
     24 namespace {
     25 const char kResetProfileSettingsLearnMoreUrl[] =
     26     "https://support.google.com/chrome/?p=ui_reset_settings";
     27 }  // namespace
     28 
     29 namespace options {
     30 
     31 ResetProfileSettingsHandler::ResetProfileSettingsHandler() {
     32   google_util::GetBrand(&brandcode_);
     33 }
     34 
     35 ResetProfileSettingsHandler::~ResetProfileSettingsHandler() {
     36 }
     37 
     38 void ResetProfileSettingsHandler::InitializeHandler() {
     39   Profile* profile = Profile::FromWebUI(web_ui());
     40   resetter_.reset(new ProfileResetter(profile));
     41 }
     42 
     43 void ResetProfileSettingsHandler::GetLocalizedValues(
     44     DictionaryValue* localized_strings) {
     45   DCHECK(localized_strings);
     46 
     47   static OptionsStringResource resources[] = {
     48     { "resetProfileSettingsCommit", IDS_RESET_PROFILE_SETTINGS_COMMIT_BUTTON },
     49     { "resetProfileSettingsExplanation",
     50         IDS_RESET_PROFILE_SETTINGS_EXPLANATION},
     51     { "resetProfileSettingsFeedback", IDS_RESET_PROFILE_SETTINGS_FEEDBACK }
     52   };
     53 
     54   RegisterStrings(localized_strings, resources, arraysize(resources));
     55   RegisterTitle(localized_strings, "resetProfileSettingsOverlay",
     56                 IDS_RESET_PROFILE_SETTINGS_TITLE);
     57   localized_strings->SetString(
     58       "resetProfileSettingsLearnMoreUrl",
     59       google_util::StringAppendGoogleLocaleParam(
     60           kResetProfileSettingsLearnMoreUrl));
     61 }
     62 
     63 void ResetProfileSettingsHandler::RegisterMessages() {
     64   // Setup handlers specific to this panel.
     65   web_ui()->RegisterMessageCallback("performResetProfileSettings",
     66       base::Bind(&ResetProfileSettingsHandler::HandleResetProfileSettings,
     67                  base::Unretained(this)));
     68   web_ui()->RegisterMessageCallback("onShowResetProfileDialog",
     69       base::Bind(&ResetProfileSettingsHandler::OnShowResetProfileDialog,
     70                  base::Unretained(this)));
     71 }
     72 
     73 void ResetProfileSettingsHandler::HandleResetProfileSettings(
     74     const ListValue* value) {
     75   bool send_settings = false;
     76   if (!value->GetBoolean(0, &send_settings))
     77     NOTREACHED();
     78 
     79   DCHECK(brandcode_.empty() || config_fetcher_);
     80   if (config_fetcher_ && config_fetcher_->IsActive()) {
     81     // Reset once the prefs are fetched.
     82     config_fetcher_->SetCallback(
     83         base::Bind(&ResetProfileSettingsHandler::ResetProfile,
     84                    Unretained(this),
     85                    send_settings));
     86   } else {
     87     ResetProfile(send_settings);
     88   }
     89 }
     90 
     91 void ResetProfileSettingsHandler::OnResetProfileSettingsDone() {
     92   web_ui()->CallJavascriptFunction("ResetProfileSettingsOverlay.doneResetting");
     93   if (setting_snapshot_) {
     94     Profile* profile = Profile::FromWebUI(web_ui());
     95     ResettableSettingsSnapshot current_snapshot(profile);
     96     int difference = setting_snapshot_->FindDifferentFields(current_snapshot);
     97     if (difference) {
     98       setting_snapshot_->Subtract(current_snapshot);
     99       std::string report = SerializeSettingsReport(*setting_snapshot_,
    100                                                    difference);
    101       SendSettingsFeedback(report, profile);
    102     }
    103     setting_snapshot_.reset();
    104   }
    105 }
    106 
    107 void ResetProfileSettingsHandler::OnShowResetProfileDialog(const ListValue*) {
    108   if (brandcode_.empty())
    109     return;
    110   config_fetcher_.reset(new BrandcodeConfigFetcher(
    111       base::Bind(&ResetProfileSettingsHandler::OnSettingsFetched,
    112                  Unretained(this)),
    113       GURL("https://tools.google.com/service/update2"),
    114       brandcode_));
    115 }
    116 
    117 void ResetProfileSettingsHandler::OnSettingsFetched() {
    118   DCHECK(config_fetcher_);
    119   DCHECK(!config_fetcher_->IsActive());
    120   // The master prefs is fetched. We are waiting for user pressing 'Reset'.
    121 }
    122 
    123 void ResetProfileSettingsHandler::ResetProfile(bool send_settings) {
    124   DCHECK(resetter_);
    125   DCHECK(!resetter_->IsActive());
    126 
    127   scoped_ptr<BrandcodedDefaultSettings> default_settings;
    128   if (config_fetcher_) {
    129     DCHECK(!config_fetcher_->IsActive());
    130     default_settings = config_fetcher_->GetSettings();
    131     config_fetcher_.reset();
    132   } else {
    133     DCHECK(brandcode_.empty());
    134   }
    135 
    136   // If failed to fetch BrandcodedDefaultSettings or this is an organic
    137   // installation, use default settings.
    138   if (!default_settings)
    139     default_settings.reset(new BrandcodedDefaultSettings);
    140   // Save current settings if required.
    141   setting_snapshot_.reset(send_settings ?
    142       new ResettableSettingsSnapshot(Profile::FromWebUI(web_ui())) : NULL);
    143   resetter_->Reset(
    144       ProfileResetter::ALL,
    145       default_settings.Pass(),
    146       base::Bind(&ResetProfileSettingsHandler::OnResetProfileSettingsDone,
    147                  AsWeakPtr()));
    148   content::RecordAction(content::UserMetricsAction("ResetProfile"));
    149 }
    150 
    151 }  // namespace options
    152