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