OLD | NEW |
| (Empty) |
1 // Copyright (c) 2011 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/sync_promo_handler.h" | |
6 | |
7 #include "base/bind.h" | |
8 #include "base/bind_helpers.h" | |
9 #include "base/metrics/histogram.h" | |
10 #include "base/time.h" | |
11 #include "chrome/browser/prefs/pref_service.h" | |
12 #include "chrome/browser/profiles/profile.h" | |
13 #include "chrome/browser/sync/profile_sync_service.h" | |
14 #include "chrome/browser/sync/sync_setup_flow.h" | |
15 #include "chrome/browser/tabs/tab_strip_model.h" | |
16 #include "chrome/browser/ui/browser.h" | |
17 #include "chrome/browser/ui/browser_list.h" | |
18 #include "chrome/browser/ui/webui/sync_promo_trial.h" | |
19 #include "chrome/browser/ui/webui/sync_promo_ui.h" | |
20 #include "chrome/common/chrome_notification_types.h" | |
21 #include "chrome/common/extensions/extension_constants.h" | |
22 #include "chrome/common/pref_names.h" | |
23 #include "chrome/common/url_constants.h" | |
24 #include "content/browser/tab_contents/tab_contents.h" | |
25 #include "content/public/browser/notification_details.h" | |
26 #include "content/public/browser/notification_service.h" | |
27 | |
28 namespace { | |
29 | |
30 // User actions on the sync promo (aka "Sign in to Chrome"). | |
31 enum SyncPromoUserFlowActionEnums { | |
32 SYNC_PROMO_VIEWED, | |
33 SYNC_PROMO_LEARN_MORE_CLICKED, | |
34 SYNC_PROMO_ACCOUNT_HELP_CLICKED, | |
35 SYNC_PROMO_CREATE_ACCOUNT_CLICKED, | |
36 SYNC_PROMO_SKIP_CLICKED, | |
37 SYNC_PROMO_SIGN_IN_ATTEMPTED, | |
38 SYNC_PROMO_SIGNED_IN_SUCCESSFULLY, | |
39 SYNC_PROMO_ADVANCED_CLICKED, | |
40 SYNC_PROMO_ENCRYPTION_HELP_CLICKED, | |
41 SYNC_PROMO_CANCELLED_AFTER_SIGN_IN, | |
42 SYNC_PROMO_CONFIRMED_AFTER_SIGN_IN, | |
43 SYNC_PROMO_CLOSED_TAB, | |
44 SYNC_PROMO_CLOSED_WINDOW, | |
45 SYNC_PROMO_LEFT_DURING_THROBBER, | |
46 SYNC_PROMO_BUCKET_BOUNDARY, | |
47 SYNC_PROMO_FIRST_VALID_JS_ACTION = SYNC_PROMO_LEARN_MORE_CLICKED, | |
48 SYNC_PROMO_LAST_VALID_JS_ACTION = SYNC_PROMO_CONFIRMED_AFTER_SIGN_IN, | |
49 }; | |
50 | |
51 // This was added because of the need to change the existing UMA enum for the | |
52 // sync promo mid-flight. Ideally these values would be contiguous, but the | |
53 // real world is not always ideal. | |
54 static bool IsValidUserFlowAction(int action) { | |
55 return (action >= SYNC_PROMO_FIRST_VALID_JS_ACTION && | |
56 action <= SYNC_PROMO_LAST_VALID_JS_ACTION) || | |
57 action == SYNC_PROMO_LEFT_DURING_THROBBER; | |
58 } | |
59 | |
60 } // namespace | |
61 | |
62 SyncPromoHandler::SyncPromoHandler(ProfileManager* profile_manager) | |
63 : SyncSetupHandler(profile_manager), | |
64 window_already_closed_(false) { | |
65 } | |
66 | |
67 SyncPromoHandler::~SyncPromoHandler() { | |
68 } | |
69 | |
70 // static | |
71 void SyncPromoHandler::RegisterUserPrefs(PrefService* prefs) { | |
72 prefs->RegisterIntegerPref(prefs::kSyncPromoViewCount, 0, | |
73 PrefService::UNSYNCABLE_PREF); | |
74 prefs->RegisterBooleanPref(prefs::kSyncPromoShowNTPBubble, false, | |
75 PrefService::UNSYNCABLE_PREF); | |
76 } | |
77 | |
78 WebUIMessageHandler* SyncPromoHandler::Attach(WebUI* web_ui) { | |
79 DCHECK(web_ui); | |
80 // Keep a reference to the preferences service for convenience and it's | |
81 // probably a little faster that getting it via Profile::FromWebUI() every | |
82 // time we need to interact with preferences. | |
83 prefs_ = Profile::FromWebUI(web_ui)->GetPrefs(); | |
84 DCHECK(prefs_); | |
85 // Ignore events from view-source:chrome://syncpromo. | |
86 if (!web_ui->tab_contents()->controller().GetActiveEntry()-> | |
87 IsViewSourceMode()) { | |
88 // Listen to see if the tab we're in gets closed. | |
89 registrar_.Add(this, content::NOTIFICATION_TAB_CLOSING, | |
90 content::Source<NavigationController>( | |
91 &web_ui->tab_contents()->controller())); | |
92 // Listen to see if the window we're in gets closed. | |
93 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSING, | |
94 content::NotificationService::AllSources()); | |
95 } | |
96 return SyncSetupHandler::Attach(web_ui); | |
97 } | |
98 | |
99 void SyncPromoHandler::RegisterMessages() { | |
100 web_ui_->RegisterMessageCallback("SyncPromo:Close", | |
101 base::Bind(&SyncPromoHandler::HandleCloseSyncPromo, | |
102 base::Unretained(this))); | |
103 web_ui_->RegisterMessageCallback("SyncPromo:Initialize", | |
104 base::Bind(&SyncPromoHandler::HandleInitializeSyncPromo, | |
105 base::Unretained(this))); | |
106 web_ui_->RegisterMessageCallback("SyncPromo:RecordSignInAttempts", | |
107 base::Bind(&SyncPromoHandler::HandleRecordSignInAttempts, | |
108 base::Unretained(this))); | |
109 web_ui_->RegisterMessageCallback("SyncPromo:RecordThrobberTime", | |
110 base::Bind(&SyncPromoHandler::HandleRecordThrobberTime, | |
111 base::Unretained(this))); | |
112 web_ui_->RegisterMessageCallback("SyncPromo:ShowAdvancedSettings", | |
113 base::Bind(&SyncPromoHandler::HandleShowAdvancedSettings, | |
114 base::Unretained(this))); | |
115 web_ui_->RegisterMessageCallback("SyncPromo:UserFlowAction", | |
116 base::Bind(&SyncPromoHandler::HandleUserFlowAction, | |
117 base::Unretained(this))); | |
118 web_ui_->RegisterMessageCallback("SyncPromo:UserSkipped", | |
119 base::Bind(&SyncPromoHandler::HandleUserSkipped, | |
120 base::Unretained(this))); | |
121 SyncSetupHandler::RegisterMessages(); | |
122 } | |
123 | |
124 void SyncPromoHandler::ShowGaiaSuccessAndClose() { | |
125 if (sync_promo_trial::IsExperimentActive()) | |
126 sync_promo_trial::RecordUserSignedIn(); | |
127 | |
128 SyncSetupHandler::ShowGaiaSuccessAndClose(); | |
129 } | |
130 | |
131 void SyncPromoHandler::ShowGaiaSuccessAndSettingUp() { | |
132 if (sync_promo_trial::IsExperimentActive()) | |
133 sync_promo_trial::RecordUserSignedIn(); | |
134 | |
135 SyncSetupHandler::ShowGaiaSuccessAndSettingUp(); | |
136 } | |
137 | |
138 void SyncPromoHandler::ShowConfigure(const base::DictionaryValue& args) { | |
139 bool usePassphrase = false; | |
140 args.GetBoolean("usePassphrase", &usePassphrase); | |
141 | |
142 if (usePassphrase) { | |
143 // If a passphrase is required then we must show the configure pane. | |
144 SyncSetupHandler::ShowConfigure(args); | |
145 } else { | |
146 // If no passphrase is required then skip the configure pane and sync | |
147 // everything by default. This makes the first run experience simpler. | |
148 // Note, there's an advanced link in the sync promo that takes users | |
149 // to Settings where the configure pane is not skipped. | |
150 SyncConfiguration configuration; | |
151 configuration.sync_everything = true; | |
152 DCHECK(flow()); | |
153 flow()->OnUserConfigured(configuration); | |
154 } | |
155 } | |
156 | |
157 void SyncPromoHandler::Observe(int type, | |
158 const content::NotificationSource& source, | |
159 const content::NotificationDetails& details) { | |
160 switch (type) { | |
161 case content::NOTIFICATION_TAB_CLOSING: { | |
162 if (!window_already_closed_) | |
163 RecordUserFlowAction(SYNC_PROMO_CLOSED_TAB); | |
164 break; | |
165 } | |
166 case chrome::NOTIFICATION_BROWSER_CLOSING: { | |
167 // Make sure we're in the tab strip of the closing window. | |
168 Browser* browser = content::Source<Browser>(source).ptr(); | |
169 if (browser->tabstrip_model()->GetWrapperIndex( | |
170 web_ui_->tab_contents()) != TabStripModel::kNoTab) { | |
171 RecordUserFlowAction(SYNC_PROMO_CLOSED_WINDOW); | |
172 window_already_closed_ = true; | |
173 } | |
174 break; | |
175 } | |
176 default: { | |
177 NOTREACHED(); | |
178 } | |
179 } | |
180 } | |
181 | |
182 void SyncPromoHandler::StepWizardForShowSetupUI() { | |
183 } | |
184 | |
185 void SyncPromoHandler::ShowSetupUI() { | |
186 // SyncSetupWizard::Step should be called in StepWizardForShowSetupUI above, | |
187 // but it causes the sync promo page to not set focus properly to the login | |
188 // email address. This happens because focus is lost between the call to | |
189 // StepWizardForShowSetupUI and ShowSetupUI. | |
190 // TODO(binji): Move this function back and fix the focus the right way. | |
191 ProfileSyncService* service = | |
192 Profile::FromWebUI(web_ui_)->GetProfileSyncService(); | |
193 service->get_wizard().Step(SyncSetupWizard::GetLoginState()); | |
194 } | |
195 | |
196 void SyncPromoHandler::HandleCloseSyncPromo(const base::ListValue* args) { | |
197 CloseSyncSetup(); | |
198 | |
199 // If the user has signed in then set the pref to show them NTP bubble | |
200 // confirming that they're signed in. | |
201 std::string username = prefs_->GetString(prefs::kGoogleServicesUsername); | |
202 if (!username.empty()) | |
203 prefs_->SetBoolean(prefs::kSyncPromoShowNTPBubble, true); | |
204 | |
205 GURL url = SyncPromoUI::GetNextPageURLForSyncPromoURL( | |
206 web_ui_->tab_contents()->GetURL()); | |
207 web_ui_->tab_contents()->OpenURL(url, GURL(), CURRENT_TAB, | |
208 content::PAGE_TRANSITION_LINK); | |
209 } | |
210 | |
211 void SyncPromoHandler::HandleInitializeSyncPromo(const base::ListValue* args) { | |
212 // If the promo is also the Chrome launch page, we want to show the title and | |
213 // log an event if we are running an experiment. | |
214 bool is_launch_page = SyncPromoUI::GetIsLaunchPageForSyncPromoURL( | |
215 web_ui_->tab_contents()->GetURL()); | |
216 if (is_launch_page && sync_promo_trial::IsExperimentActive()) | |
217 sync_promo_trial::RecordUserSawMessage(); | |
218 base::FundamentalValue visible(is_launch_page); | |
219 web_ui_->CallJavascriptFunction("SyncSetupOverlay.setPromoTitleVisible", | |
220 visible); | |
221 | |
222 OpenSyncSetup(); | |
223 // We don't need to compute anything for this, just do this every time. | |
224 RecordUserFlowAction(SYNC_PROMO_VIEWED); | |
225 // Increment view count first and show natural numbers in stats rather than 0 | |
226 // based starting point (if it happened to be our first time showing this). | |
227 IncrementViewCountBy(1); | |
228 // Record +1 for every view. This is the only thing we record that's not part | |
229 // of the user flow histogram. | |
230 UMA_HISTOGRAM_COUNTS("SyncPromo.NumTimesViewed", GetViewCount()); | |
231 } | |
232 | |
233 void SyncPromoHandler::HandleShowAdvancedSettings( | |
234 const base::ListValue* args) { | |
235 CloseSyncSetup(); | |
236 std::string url(chrome::kChromeUISettingsURL); | |
237 url += chrome::kSyncSetupSubPage; | |
238 web_ui_->tab_contents()->OpenURL(GURL(url), GURL(), CURRENT_TAB, | |
239 content::PAGE_TRANSITION_LINK); | |
240 RecordUserFlowAction(SYNC_PROMO_ADVANCED_CLICKED); | |
241 } | |
242 | |
243 // TODO(dbeam): Replace with metricsHandler:recordHistogramTime when it exists. | |
244 void SyncPromoHandler::HandleRecordThrobberTime(const base::ListValue* args) { | |
245 double time_double; | |
246 CHECK(args->GetDouble(0, &time_double)); | |
247 UMA_HISTOGRAM_TIMES("SyncPromo.ThrobberTime", | |
248 base::TimeDelta::FromMilliseconds(time_double)); | |
249 } | |
250 | |
251 // TODO(dbeam): Replace with metricsHandler:recordHistogramCount when it exists. | |
252 void SyncPromoHandler::HandleRecordSignInAttempts(const base::ListValue* args) { | |
253 double count_double; | |
254 CHECK(args->GetDouble(0, &count_double)); | |
255 UMA_HISTOGRAM_COUNTS("SyncPromo.SignInAttempts", count_double); | |
256 } | |
257 | |
258 void SyncPromoHandler::HandleUserFlowAction(const base::ListValue* args) { | |
259 double action_double; | |
260 CHECK(args->GetDouble(0, &action_double)); | |
261 int action = static_cast<int>(action_double); | |
262 | |
263 if (IsValidUserFlowAction(action)) | |
264 RecordUserFlowAction(action); | |
265 else | |
266 NOTREACHED() << "Attempt to record invalid user flow action on sync promo."; | |
267 } | |
268 | |
269 void SyncPromoHandler::HandleUserSkipped(const base::ListValue* args) { | |
270 SyncPromoUI::SetUserSkippedSyncPromo(Profile::FromWebUI(web_ui_)); | |
271 RecordUserFlowAction(SYNC_PROMO_SKIP_CLICKED); | |
272 } | |
273 | |
274 int SyncPromoHandler::GetViewCount() const { | |
275 // The locally persistent number of times the user has seen the sync promo. | |
276 return prefs_->GetInteger(prefs::kSyncPromoViewCount); | |
277 } | |
278 | |
279 int SyncPromoHandler::IncrementViewCountBy(unsigned int amount) { | |
280 // Let the user increment by 0 if they really want. It might be useful for a | |
281 // weird way of sending preference change notifications... | |
282 int adjusted = GetViewCount() + amount; | |
283 prefs_->SetInteger(prefs::kSyncPromoViewCount, adjusted); | |
284 return adjusted; | |
285 } | |
286 | |
287 void SyncPromoHandler::RecordUserFlowAction(int action) { | |
288 // Send an enumeration to our single user flow histogram. | |
289 UMA_HISTOGRAM_ENUMERATION("SyncPromo.UserFlow", action, | |
290 SYNC_PROMO_BUCKET_BOUNDARY); | |
291 } | |
OLD | NEW |