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 "chrome/browser/ui/webui/sync_promo/sync_promo_ui.h" | |
6 | |
7 #include "base/command_line.h" | |
8 #include "base/prefs/pref_service.h" | |
9 #include "base/strings/string_number_conversions.h" | |
10 #include "base/strings/string_util.h" | |
11 #include "base/strings/stringprintf.h" | |
12 #include "base/strings/utf_string_conversions.h" | |
13 #include "chrome/browser/browser_process.h" | |
14 #include "chrome/browser/first_run/first_run.h" | |
15 #include "chrome/browser/google/google_util.h" | |
16 #include "chrome/browser/profiles/profile.h" | |
17 #include "chrome/browser/profiles/profile_info_cache.h" | |
18 #include "chrome/browser/profiles/profile_manager.h" | |
19 #include "chrome/browser/signin/signin_manager.h" | |
20 #include "chrome/browser/signin/signin_manager_factory.h" | |
21 #include "chrome/browser/sync/profile_sync_service.h" | |
22 #include "chrome/browser/sync/profile_sync_service_factory.h" | |
23 #include "chrome/browser/ui/webui/options/core_options_handler.h" | |
24 #include "chrome/browser/ui/webui/sync_promo/sync_promo_trial.h" | |
25 #include "chrome/browser/ui/webui/theme_source.h" | |
26 #include "chrome/common/chrome_switches.h" | |
27 #include "chrome/common/net/url_util.h" | |
28 #include "chrome/common/pref_names.h" | |
29 #include "chrome/common/url_constants.h" | |
30 #include "components/user_prefs/pref_registry_syncable.h" | |
31 #include "content/public/browser/url_data_source.h" | |
32 #include "content/public/browser/web_contents.h" | |
33 #include "content/public/browser/web_ui.h" | |
34 #include "content/public/browser/web_ui_data_source.h" | |
35 #include "google_apis/gaia/gaia_urls.h" | |
36 #include "grit/browser_resources.h" | |
37 #include "grit/generated_resources.h" | |
38 #include "grit/theme_resources.h" | |
39 #include "net/base/escape.h" | |
40 #include "net/base/network_change_notifier.h" | |
41 #include "net/base/url_util.h" | |
42 #include "ui/base/l10n/l10n_util.h" | |
43 | |
44 using content::WebContents; | |
45 | |
46 namespace { | |
47 | |
48 const char kStringsJsFile[] = "strings.js"; | |
49 const char kSyncPromoJsFile[] = "sync_promo.js"; | |
50 | |
51 const char kSyncPromoQueryKeyAutoClose[] = "auto_close"; | |
52 const char kSyncPromoQueryKeyContinue[] = "continue"; | |
53 const char kSyncPromoQueryKeySource[] = "source"; | |
54 | |
55 // Gaia cannot support about:blank as a continue URL, so using a hosted blank | |
56 // page instead. | |
57 const char kSyncLandingUrlPrefix[] = | |
58 "https://www.google.com/intl/%s/chrome/blank.html"; | |
59 | |
60 // The maximum number of times we want to show the sync promo at startup. | |
61 const int kSyncPromoShowAtStartupMaximum = 10; | |
62 | |
63 // Forces the web based signin flow when set. | |
64 bool g_force_web_based_signin_flow = false; | |
65 | |
66 // Checks we want to show the sync promo for the given brand. | |
67 bool AllowPromoAtStartupForCurrentBrand() { | |
68 std::string brand; | |
69 google_util::GetBrand(&brand); | |
70 | |
71 if (brand.empty()) | |
72 return true; | |
73 | |
74 if (google_util::IsInternetCafeBrandCode(brand)) | |
75 return false; | |
76 | |
77 // Enable for both organic and distribution. | |
78 return true; | |
79 } | |
80 | |
81 } // namespace | |
82 | |
83 // static | |
84 bool SyncPromoUI::HasShownPromoAtStartup(Profile* profile) { | |
85 return profile->GetPrefs()->HasPrefPath(prefs::kSyncPromoStartupCount); | |
86 } | |
87 | |
88 // static | |
89 bool SyncPromoUI::ShouldShowSyncPromo(Profile* profile) { | |
90 #if defined(OS_CHROMEOS) | |
91 // There's no need to show the sync promo on cros since cros users are logged | |
92 // into sync already. | |
93 return false; | |
94 #else | |
95 | |
96 // Don't bother if we don't have any kind of network connection. | |
97 if (net::NetworkChangeNotifier::IsOffline()) | |
98 return false; | |
99 | |
100 // Don't show if the profile is an incognito. | |
101 if (profile->IsOffTheRecord()) | |
102 return false; | |
103 | |
104 // Don't show for managed profiles. | |
105 if (profile->GetPrefs()->GetBoolean(prefs::kProfileIsManaged)) | |
106 return false; | |
107 | |
108 // Display the signin promo if the user is not signed in. | |
109 SigninManager* signin = SigninManagerFactory::GetForProfile( | |
110 profile->GetOriginalProfile()); | |
111 return !signin->AuthInProgress() && signin->IsSigninAllowed() && | |
112 signin->GetAuthenticatedUsername().empty(); | |
113 #endif | |
114 } | |
115 | |
116 // static | |
117 void SyncPromoUI::RegisterUserPrefs( | |
118 user_prefs::PrefRegistrySyncable* registry) { | |
119 registry->RegisterIntegerPref( | |
120 prefs::kSyncPromoStartupCount, | |
121 0, | |
122 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
123 registry->RegisterBooleanPref( | |
124 prefs::kSyncPromoUserSkipped, | |
125 false, | |
126 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
127 registry->RegisterBooleanPref( | |
128 prefs::kSyncPromoShowOnFirstRunAllowed, | |
129 true, | |
130 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
131 registry->RegisterBooleanPref( | |
132 prefs::kSyncPromoShowNTPBubble, | |
133 false, | |
134 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
135 registry->RegisterStringPref( | |
136 prefs::kSyncPromoErrorMessage, | |
137 std::string(), | |
138 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | |
139 } | |
140 | |
141 // static | |
142 bool SyncPromoUI::ShouldShowSyncPromoAtStartup(Profile* profile, | |
143 bool is_new_profile) { | |
144 DCHECK(profile); | |
145 | |
146 if (!ShouldShowSyncPromo(profile)) | |
147 return false; | |
148 | |
149 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | |
150 if (command_line.HasSwitch(switches::kNoFirstRun)) | |
151 is_new_profile = false; | |
152 | |
153 if (!is_new_profile) { | |
154 if (!HasShownPromoAtStartup(profile)) | |
155 return false; | |
156 } | |
157 | |
158 if (HasUserSkippedSyncPromo(profile)) | |
159 return false; | |
160 | |
161 // For Chinese users skip the sync promo. | |
162 if (g_browser_process->GetApplicationLocale() == "zh-CN") | |
163 return false; | |
164 | |
165 PrefService* prefs = profile->GetPrefs(); | |
166 int show_count = prefs->GetInteger(prefs::kSyncPromoStartupCount); | |
167 if (show_count >= kSyncPromoShowAtStartupMaximum) | |
168 return false; | |
169 | |
170 // This pref can be set in the master preferences file to allow or disallow | |
171 // showing the sync promo at startup. | |
172 if (prefs->HasPrefPath(prefs::kSyncPromoShowOnFirstRunAllowed)) | |
173 return prefs->GetBoolean(prefs::kSyncPromoShowOnFirstRunAllowed); | |
174 | |
175 // For now don't show the promo for some brands. | |
176 if (!AllowPromoAtStartupForCurrentBrand()) | |
177 return false; | |
178 | |
179 // Default to show the promo for Google Chrome builds. | |
180 #if defined(GOOGLE_CHROME_BUILD) | |
181 return true; | |
182 #else | |
183 return false; | |
184 #endif | |
185 } | |
186 | |
187 void SyncPromoUI::DidShowSyncPromoAtStartup(Profile* profile) { | |
188 int show_count = profile->GetPrefs()->GetInteger( | |
189 prefs::kSyncPromoStartupCount); | |
190 show_count++; | |
191 profile->GetPrefs()->SetInteger(prefs::kSyncPromoStartupCount, show_count); | |
192 } | |
193 | |
194 bool SyncPromoUI::HasUserSkippedSyncPromo(Profile* profile) { | |
195 return profile->GetPrefs()->GetBoolean(prefs::kSyncPromoUserSkipped); | |
196 } | |
197 | |
198 void SyncPromoUI::SetUserSkippedSyncPromo(Profile* profile) { | |
199 profile->GetPrefs()->SetBoolean(prefs::kSyncPromoUserSkipped, true); | |
200 } | |
201 | |
202 // static | |
203 std::string SyncPromoUI::GetSyncLandingURL(const char* option, int value) { | |
204 const std::string& locale = g_browser_process->GetApplicationLocale(); | |
205 std::string url = base::StringPrintf(kSyncLandingUrlPrefix, locale.c_str()); | |
206 base::StringAppendF(&url, "?%s=%d", option, value); | |
207 return url; | |
208 } | |
209 | |
210 // static | |
211 GURL SyncPromoUI::GetSyncPromoURL(Source source, bool auto_close) { | |
212 DCHECK_NE(SOURCE_UNKNOWN, source); | |
213 | |
214 std::string url_string; | |
215 | |
216 // Build a Gaia-based URL that can be used to sign the user into chrome. | |
217 // There are required request parameters: | |
218 // | |
219 // - tell Gaia which service the user is signing into. In this case, | |
220 // a chrome sign in uses the service "chromiumsync" | |
221 // - provide a continue URL. This is the URL that Gaia will redirect to | |
222 // once the sign is complete. | |
223 // | |
224 // The continue URL includes a source parameter that can be extracted using | |
225 // the function GetSourceForSyncPromoURL() below. This is used to know | |
226 // which of the chrome sign in access points was used to sign the user in. | |
227 // See OneClickSigninHelper for details. | |
228 url_string = GaiaUrls::GetInstance()->service_login_url(); | |
229 url_string.append("?service=chromiumsync&sarp=1"); | |
230 | |
231 std::string continue_url = GetSyncLandingURL( | |
232 kSyncPromoQueryKeySource, static_cast<int>(source)); | |
233 | |
234 base::StringAppendF(&url_string, "&%s=%s", kSyncPromoQueryKeyContinue, | |
235 net::EscapeQueryParamValue( | |
236 continue_url, false).c_str()); | |
237 | |
238 return GURL(url_string); | |
239 } | |
240 | |
241 // static | |
242 GURL SyncPromoUI::GetNextPageURLForSyncPromoURL(const GURL& url) { | |
243 std::string value; | |
244 if (net::GetValueForKeyInQuery(url, kSyncPromoQueryKeyContinue, &value)) | |
245 return GURL(value); | |
246 | |
247 return GURL(); | |
248 } | |
249 | |
250 // static | |
251 SyncPromoUI::Source SyncPromoUI::GetSourceForSyncPromoURL(const GURL& url) { | |
252 std::string value; | |
253 if (net::GetValueForKeyInQuery(url, kSyncPromoQueryKeySource, &value)) { | |
254 int source = 0; | |
255 if (base::StringToInt(value, &source) && source >= SOURCE_START_PAGE && | |
256 source < SOURCE_UNKNOWN) { | |
257 return static_cast<Source>(source); | |
258 } | |
259 } | |
260 return SOURCE_UNKNOWN; | |
261 } | |
262 | |
263 // static | |
264 bool SyncPromoUI::IsContinueUrlForWebBasedSigninFlow(const GURL& url) { | |
265 GURL::Replacements replacements; | |
266 replacements.ClearQuery(); | |
267 const std::string& locale = g_browser_process->GetApplicationLocale(); | |
268 return url.ReplaceComponents(replacements) == | |
269 GURL(base::StringPrintf(kSyncLandingUrlPrefix, locale.c_str())); | |
270 } | |
271 | |
272 // static | |
273 void SyncPromoUI::ForceWebBasedSigninFlowForTesting(bool force) { | |
274 g_force_web_based_signin_flow = force; | |
275 } | |
OLD | NEW |