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/profiles/profile.h" | |
10 #include "chrome/browser/protector/base_prefs_change.h" | |
11 #include "chrome/browser/protector/histograms.h" | |
12 #include "chrome/common/pref_names.h" | |
13 #include "grit/chromium_strings.h" | |
14 #include "grit/generated_resources.h" | |
15 #include "grit/theme_resources.h" | |
16 #include "ui/base/l10n/l10n_util.h" | |
17 | |
18 namespace protector { | |
19 | |
20 // Homepage change tracked by Protector. | |
21 class HomepageChange : public BasePrefsChange { | |
22 public: | |
23 // Enum for reporting UMA statistics. | |
24 enum HomepageType { | |
25 HOMEPAGE_NTP = 0, | |
26 HOMEPAGE_URL, | |
27 | |
28 // Must be the last value | |
29 HOMEPAGE_TYPE_COUNT | |
30 }; | |
31 | |
32 HomepageChange(const std::string& actual_homepage, | |
33 bool actual_homepage_is_ntp, | |
34 bool actual_show_homepage, | |
35 const std::string& backup_homepage, | |
36 bool backup_homepage_is_ntp, | |
37 bool backup_show_homepage); | |
38 | |
39 // BaseSettingChange overrides: | |
40 virtual bool Init(Profile* profile) OVERRIDE; | |
41 virtual void Apply(Browser* browser) OVERRIDE; | |
42 virtual void Discard(Browser* browser) OVERRIDE; | |
43 virtual void Timeout() OVERRIDE; | |
44 virtual int GetBadgeIconID() const OVERRIDE; | |
45 virtual int GetMenuItemIconID() const OVERRIDE; | |
46 virtual int GetBubbleIconID() const OVERRIDE; | |
47 virtual string16 GetBubbleTitle() const OVERRIDE; | |
48 virtual string16 GetBubbleMessage() const OVERRIDE; | |
49 virtual string16 GetApplyButtonText() const OVERRIDE; | |
50 virtual string16 GetDiscardButtonText() const OVERRIDE; | |
51 virtual DisplayName GetApplyDisplayName() const OVERRIDE; | |
52 virtual GURL GetNewSettingURL() 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 UMA_HISTOGRAM_ENUMERATION( | |
106 kProtectorHistogramHomepageApplied, | |
107 new_homepage_type_, | |
108 HOMEPAGE_TYPE_COUNT); | |
109 IgnorePrefChanges(); | |
110 PrefService* prefs = profile()->GetPrefs(); | |
111 prefs->SetString(prefs::kHomePage, new_homepage_); | |
112 prefs->SetBoolean(prefs::kHomePageIsNewTabPage, new_homepage_is_ntp_); | |
113 prefs->SetBoolean(prefs::kShowHomeButton, new_show_homepage_); | |
114 } | |
115 | |
116 void HomepageChange::Discard(Browser* browser) { | |
117 UMA_HISTOGRAM_ENUMERATION( | |
118 kProtectorHistogramHomepageDiscarded, | |
119 new_homepage_type_, | |
120 HOMEPAGE_TYPE_COUNT); | |
121 IgnorePrefChanges(); | |
122 // Nothing to do here since backup has already been made active by Init(). | |
123 } | |
124 | |
125 void HomepageChange::Timeout() { | |
126 UMA_HISTOGRAM_ENUMERATION( | |
127 kProtectorHistogramHomepageTimeout, | |
128 new_homepage_type_, | |
129 HOMEPAGE_TYPE_COUNT); | |
130 } | |
131 | |
132 int HomepageChange::GetBadgeIconID() const { | |
133 // Icons are the same for homepage and startup settings. | |
134 return IDR_HOMEPAGE_CHANGE_BADGE; | |
135 } | |
136 | |
137 int HomepageChange::GetMenuItemIconID() const { | |
138 return IDR_HOMEPAGE_CHANGE_MENU; | |
139 } | |
140 | |
141 int HomepageChange::GetBubbleIconID() const { | |
142 return IDR_HOMEPAGE_CHANGE_ALERT; | |
143 } | |
144 | |
145 string16 HomepageChange::GetBubbleTitle() const { | |
146 return l10n_util::GetStringUTF16(IDS_HOMEPAGE_CHANGE_TITLE); | |
147 } | |
148 | |
149 string16 HomepageChange::GetBubbleMessage() const { | |
150 return l10n_util::GetStringUTF16(IDS_HOMEPAGE_CHANGE_BUBBLE_MESSAGE); | |
151 } | |
152 | |
153 string16 HomepageChange::GetApplyButtonText() const { | |
154 GURL homepage_url(GetNewSettingURL()); | |
155 return homepage_url.is_empty() ? | |
156 l10n_util::GetStringUTF16(IDS_CHANGE_HOMEPAGE_NTP) : | |
157 l10n_util::GetStringFUTF16(IDS_CHANGE_HOMEPAGE, | |
158 UTF8ToUTF16(homepage_url.host())); | |
159 } | |
160 | |
161 string16 HomepageChange::GetDiscardButtonText() const { | |
162 GURL new_homepage_url(GetNewSettingURL()); | |
163 GURL backup_homepage_url; | |
164 if (!backup_homepage_is_ntp_) | |
165 backup_homepage_url = GURL(backup_homepage_); | |
166 if (backup_homepage_url.host() == new_homepage_url.host()) { | |
167 // Display a generic string if new setting looks the same as the backup (for | |
168 // example, when homepage hasn't changed but 'show homepage button' has). | |
169 return l10n_util::GetStringUTF16(IDS_KEEP_SETTING); | |
170 } | |
171 return backup_homepage_url.is_empty() ? | |
172 l10n_util::GetStringUTF16(IDS_KEEP_HOMEPAGE_NTP) : | |
173 l10n_util::GetStringFUTF16(IDS_KEEP_HOMEPAGE, | |
174 UTF8ToUTF16(backup_homepage_url.host())); | |
175 } | |
176 | |
177 BaseSettingChange::DisplayName HomepageChange::GetApplyDisplayName() const { | |
178 GURL homepage_url(GetNewSettingURL()); | |
179 return homepage_url.is_empty() ? | |
180 DisplayName(kDefaultNamePriority, string16()) : | |
181 DisplayName(kHomepageChangeNamePriority, | |
182 UTF8ToUTF16(homepage_url.host())); | |
183 } | |
184 | |
185 GURL HomepageChange::GetNewSettingURL() const { | |
186 return new_homepage_is_ntp_ ? GURL() : GURL(new_homepage_); | |
187 } | |
188 | |
189 BaseSettingChange* CreateHomepageChange( | |
190 const std::string& actual_homepage, | |
191 bool actual_homepage_is_ntp, | |
192 bool actual_show_homepage, | |
193 const std::string& backup_homepage, | |
194 bool backup_homepage_is_ntp, | |
195 bool backup_show_homepage) { | |
196 return new HomepageChange( | |
197 actual_homepage, actual_homepage_is_ntp, actual_show_homepage, | |
198 backup_homepage, backup_homepage_is_ntp, backup_show_homepage); | |
199 } | |
200 | |
201 } // namespace protector | |
OLD | NEW |