OLD | NEW |
(Empty) | |
| 1 // Copyright (c) 2012 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 "base/basictypes.h" |
| 6 #include "base/metrics/histogram.h" |
| 7 #include "base/utf_string_conversions.h" |
| 8 #include "chrome/browser/prefs/pref_service.h" |
| 9 #include "chrome/browser/protector/base_prefs_change.h" |
| 10 #include "chrome/browser/protector/histograms.h" |
| 11 #include "chrome/common/pref_names.h" |
| 12 #include "grit/chromium_strings.h" |
| 13 #include "grit/generated_resources.h" |
| 14 #include "grit/theme_resources.h" |
| 15 #include "ui/base/l10n/l10n_util.h" |
| 16 |
| 17 namespace protector { |
| 18 |
| 19 // Homepage change tracked by Protector. |
| 20 class HomepageChange : public BasePrefsChange { |
| 21 public: |
| 22 // Enum for reporting UMA statistics. |
| 23 enum HomepageType { |
| 24 HOMEPAGE_NTP = 0, |
| 25 HOMEPAGE_URL, |
| 26 |
| 27 // Must be the last value |
| 28 HOMEPAGE_TYPE_COUNT |
| 29 }; |
| 30 |
| 31 HomepageChange(const std::string& actual_homepage, |
| 32 bool actual_homepage_is_ntp, |
| 33 bool actual_show_homepage, |
| 34 const std::string& backup_homepage, |
| 35 bool backup_homepage_is_ntp, |
| 36 bool backup_show_homepage); |
| 37 |
| 38 // BaseSettingChange overrides: |
| 39 virtual bool Init(Profile* profile) OVERRIDE; |
| 40 virtual void Apply(Browser* browser) OVERRIDE; |
| 41 virtual void Discard(Browser* browser) OVERRIDE; |
| 42 virtual void Timeout() OVERRIDE; |
| 43 virtual int GetBadgeIconID() const OVERRIDE; |
| 44 virtual int GetMenuItemIconID() const OVERRIDE; |
| 45 virtual int GetBubbleIconID() const OVERRIDE; |
| 46 virtual string16 GetBubbleTitle() const OVERRIDE; |
| 47 virtual string16 GetBubbleMessage() const OVERRIDE; |
| 48 virtual string16 GetApplyButtonText() const OVERRIDE; |
| 49 virtual string16 GetDiscardButtonText() const OVERRIDE; |
| 50 virtual DisplayName GetApplyDisplayName() const OVERRIDE; |
| 51 virtual GURL GetNewSettingURL() const OVERRIDE; |
| 52 virtual bool IsUserVisible() const OVERRIDE; |
| 53 |
| 54 private: |
| 55 virtual ~HomepageChange(); |
| 56 |
| 57 const std::string new_homepage_; |
| 58 const std::string backup_homepage_; |
| 59 const bool new_homepage_is_ntp_; |
| 60 const bool backup_homepage_is_ntp_; |
| 61 const bool new_show_homepage_; |
| 62 const bool backup_show_homepage_; |
| 63 const HomepageType new_homepage_type_; |
| 64 |
| 65 DISALLOW_COPY_AND_ASSIGN(HomepageChange); |
| 66 }; |
| 67 |
| 68 HomepageChange::HomepageChange( |
| 69 const std::string& actual_homepage, |
| 70 bool actual_homepage_is_ntp, |
| 71 bool actual_show_homepage, |
| 72 const std::string& backup_homepage, |
| 73 bool backup_homepage_is_ntp, |
| 74 bool backup_show_homepage) |
| 75 : new_homepage_(actual_homepage), |
| 76 backup_homepage_(backup_homepage), |
| 77 new_homepage_is_ntp_(actual_homepage_is_ntp), |
| 78 backup_homepage_is_ntp_(backup_homepage_is_ntp), |
| 79 new_show_homepage_(actual_show_homepage), |
| 80 backup_show_homepage_(backup_show_homepage), |
| 81 new_homepage_type_(actual_homepage_is_ntp ? HOMEPAGE_NTP : HOMEPAGE_URL) { |
| 82 UMA_HISTOGRAM_ENUMERATION( |
| 83 kProtectorHistogramHomepageChanged, |
| 84 new_homepage_type_, |
| 85 HOMEPAGE_TYPE_COUNT); |
| 86 } |
| 87 |
| 88 HomepageChange::~HomepageChange() { |
| 89 } |
| 90 |
| 91 bool HomepageChange::Init(Profile* profile) { |
| 92 if (!BasePrefsChange::Init(profile)) |
| 93 return false; |
| 94 PrefService* prefs = profile->GetPrefs(); |
| 95 prefs->SetString(prefs::kHomePage, backup_homepage_); |
| 96 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, backup_homepage_is_ntp_); |
| 97 prefs->SetBoolean(prefs::kShowHomeButton, backup_show_homepage_); |
| 98 DismissOnPrefChange(prefs::kHomePage); |
| 99 DismissOnPrefChange(prefs::kHomePageIsNewTabPage); |
| 100 DismissOnPrefChange(prefs::kShowHomeButton); |
| 101 return true; |
| 102 } |
| 103 |
| 104 void HomepageChange::Apply(Browser* browser) { |
| 105 if (IsUserVisible()) { |
| 106 // Don't report statistics if this change was applied as part of a composite |
| 107 // change and is not user-visible. |
| 108 UMA_HISTOGRAM_ENUMERATION( |
| 109 kProtectorHistogramHomepageApplied, |
| 110 new_homepage_type_, |
| 111 HOMEPAGE_TYPE_COUNT); |
| 112 } |
| 113 IgnorePrefChanges(); |
| 114 PrefService* prefs = profile()->GetPrefs(); |
| 115 prefs->SetString(prefs::kHomePage, new_homepage_); |
| 116 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, new_homepage_is_ntp_); |
| 117 prefs->SetBoolean(prefs::kShowHomeButton, new_show_homepage_); |
| 118 } |
| 119 |
| 120 void HomepageChange::Discard(Browser* browser) { |
| 121 if (IsUserVisible()) { |
| 122 UMA_HISTOGRAM_ENUMERATION( |
| 123 kProtectorHistogramHomepageDiscarded, |
| 124 new_homepage_type_, |
| 125 HOMEPAGE_TYPE_COUNT); |
| 126 } |
| 127 IgnorePrefChanges(); |
| 128 // Nothing to do here since backup has already been made active by Init(). |
| 129 } |
| 130 |
| 131 void HomepageChange::Timeout() { |
| 132 if (IsUserVisible()) { |
| 133 UMA_HISTOGRAM_ENUMERATION( |
| 134 kProtectorHistogramHomepageTimeout, |
| 135 new_homepage_type_, |
| 136 HOMEPAGE_TYPE_COUNT); |
| 137 } |
| 138 } |
| 139 |
| 140 int HomepageChange::GetBadgeIconID() const { |
| 141 // Icons are the same for homepage and startup settings. |
| 142 return IDR_HOMEPAGE_CHANGE_BADGE; |
| 143 } |
| 144 |
| 145 int HomepageChange::GetMenuItemIconID() const { |
| 146 return IDR_HOMEPAGE_CHANGE_MENU; |
| 147 } |
| 148 |
| 149 int HomepageChange::GetBubbleIconID() const { |
| 150 return IDR_HOMEPAGE_CHANGE_ALERT; |
| 151 } |
| 152 |
| 153 string16 HomepageChange::GetBubbleTitle() const { |
| 154 return l10n_util::GetStringUTF16(IDS_HOMEPAGE_CHANGE_TITLE); |
| 155 } |
| 156 |
| 157 string16 HomepageChange::GetBubbleMessage() const { |
| 158 return l10n_util::GetStringUTF16(IDS_HOMEPAGE_CHANGE_BUBBLE_MESSAGE); |
| 159 } |
| 160 |
| 161 string16 HomepageChange::GetApplyButtonText() const { |
| 162 return new_homepage_is_ntp_ ? |
| 163 l10n_util::GetStringUTF16(IDS_CHANGE_HOMEPAGE_NTP) : |
| 164 l10n_util::GetStringFUTF16(IDS_CHANGE_HOMEPAGE, |
| 165 UTF8ToUTF16(GURL(new_homepage_).host())); |
| 166 } |
| 167 |
| 168 string16 HomepageChange::GetDiscardButtonText() const { |
| 169 return backup_homepage_is_ntp_ ? |
| 170 l10n_util::GetStringUTF16(IDS_KEEP_HOMEPAGE_NTP) : |
| 171 l10n_util::GetStringFUTF16(IDS_KEEP_HOMEPAGE, |
| 172 UTF8ToUTF16(GURL(backup_homepage_).host())); |
| 173 } |
| 174 |
| 175 BaseSettingChange::DisplayName HomepageChange::GetApplyDisplayName() const { |
| 176 return new_homepage_is_ntp_ ? |
| 177 DisplayName(kDefaultNamePriority, string16()) : |
| 178 DisplayName(kHomepageChangeNamePriority, |
| 179 UTF8ToUTF16(GURL(new_homepage_).host())); |
| 180 } |
| 181 |
| 182 GURL HomepageChange::GetNewSettingURL() const { |
| 183 return new_homepage_is_ntp_ ? GURL() : GURL(new_homepage_); |
| 184 } |
| 185 |
| 186 bool HomepageChange::IsUserVisible() const { |
| 187 // Should not be presented to user unless the homepage button was previously |
| 188 // visible or has been made visible by this change. |
| 189 return new_show_homepage_ || backup_show_homepage_; |
| 190 } |
| 191 |
| 192 BaseSettingChange* CreateHomepageChange( |
| 193 const std::string& actual_homepage, |
| 194 bool actual_homepage_is_ntp, |
| 195 bool actual_show_homepage, |
| 196 const std::string& backup_homepage, |
| 197 bool backup_homepage_is_ntp, |
| 198 bool backup_show_homepage) { |
| 199 return new HomepageChange( |
| 200 actual_homepage, actual_homepage_is_ntp, actual_show_homepage, |
| 201 backup_homepage, backup_homepage_is_ntp, backup_show_homepage); |
| 202 } |
| 203 |
| 204 } // namespace protector |
OLD | NEW |