OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 1 // Copyright 2013 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/signin/signin_promo.h" | 5 #include "chrome/browser/signin/signin_promo.h" |
6 | 6 |
7 #include "base/prefs/pref_service.h" | 7 #include "base/prefs/pref_service.h" |
8 #include "base/strings/string_number_conversions.h" | 8 #include "base/strings/string_number_conversions.h" |
9 #include "base/strings/string_util.h" | 9 #include "base/strings/string_util.h" |
10 #include "base/strings/stringprintf.h" | 10 #include "base/strings/stringprintf.h" |
(...skipping 22 matching lines...) Expand all Loading... |
33 #include "google_apis/gaia/gaia_urls.h" | 33 #include "google_apis/gaia/gaia_urls.h" |
34 #include "grit/browser_resources.h" | 34 #include "grit/browser_resources.h" |
35 #include "grit/generated_resources.h" | 35 #include "grit/generated_resources.h" |
36 #include "grit/theme_resources.h" | 36 #include "grit/theme_resources.h" |
37 #include "net/base/escape.h" | 37 #include "net/base/escape.h" |
38 #include "net/base/network_change_notifier.h" | 38 #include "net/base/network_change_notifier.h" |
39 #include "net/base/url_util.h" | 39 #include "net/base/url_util.h" |
40 #include "ui/base/l10n/l10n_util.h" | 40 #include "ui/base/l10n/l10n_util.h" |
41 | 41 |
42 using content::WebContents; | 42 using content::WebContents; |
| 43 using net::GetValueForKeyInQuery; |
43 | 44 |
44 namespace { | 45 namespace { |
45 | 46 |
46 const char kSignInPromoQueryKeyAutoClose[] = "auto_close"; | 47 const char kSignInPromoQueryKeyAutoClose[] = "auto_close"; |
47 const char kSignInPromoQueryKeyContinue[] = "continue"; | 48 const char kSignInPromoQueryKeyContinue[] = "continue"; |
| 49 const char kSignInPromoQueryKeyContinueUrl[] = "continue_url"; |
48 const char kSignInPromoQueryKeySource[] = "source"; | 50 const char kSignInPromoQueryKeySource[] = "source"; |
49 const char kSignInPromoQueryKeyConstrained[] = "constrained"; | 51 const char kSignInPromoQueryKeyConstrained[] = "constrained"; |
50 | 52 |
51 // Gaia cannot support about:blank as a continue URL, so using a hosted blank | 53 // Gaia cannot support about:blank as a continue URL, so using a hosted blank |
52 // page instead. | 54 // page instead. |
53 const char kSignInLandingUrlPrefix[] = | 55 const char kSignInLandingUrlPrefix[] = |
54 "https://www.google.com/intl/%s/chrome/blank.html"; | 56 "https://www.google.com/intl/%s/chrome/blank.html"; |
55 | 57 |
56 // The maximum number of times we want to show the sign in promo at startup. | 58 // The maximum number of times we want to show the sign in promo at startup. |
57 const int kSignInPromoShowAtStartupMaximum = 10; | 59 const int kSignInPromoShowAtStartupMaximum = 10; |
(...skipping 113 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
171 std::string url = base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str()); | 173 std::string url = base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str()); |
172 base::StringAppendF(&url, "?%s=%d", option, value); | 174 base::StringAppendF(&url, "?%s=%d", option, value); |
173 return GURL(url); | 175 return GURL(url); |
174 } | 176 } |
175 | 177 |
176 GURL GetPromoURL(Source source, bool auto_close) { | 178 GURL GetPromoURL(Source source, bool auto_close) { |
177 return GetPromoURL(source, auto_close, false /* is_constrained */); | 179 return GetPromoURL(source, auto_close, false /* is_constrained */); |
178 } | 180 } |
179 | 181 |
180 GURL GetPromoURL(Source source, bool auto_close, bool is_constrained) { | 182 GURL GetPromoURL(Source source, bool auto_close, bool is_constrained) { |
| 183 return GetPromoURLWithContinueURL(source, auto_close, is_constrained, GURL()); |
| 184 } |
| 185 |
| 186 GURL GetPromoURLWithContinueURL(Source source, |
| 187 bool auto_close, |
| 188 bool is_constrained, |
| 189 GURL continue_url) { |
181 DCHECK_NE(SOURCE_UNKNOWN, source); | 190 DCHECK_NE(SOURCE_UNKNOWN, source); |
182 | 191 |
183 if (!switches::IsEnableWebBasedSignin()) { | 192 if (!switches::IsEnableWebBasedSignin()) { |
184 std::string url(chrome::kChromeUIChromeSigninURL); | 193 std::string url(chrome::kChromeUIChromeSigninURL); |
185 base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source); | 194 base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source); |
186 if (auto_close) | 195 if (auto_close) |
187 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose); | 196 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose); |
188 if (is_constrained) | 197 if (is_constrained) |
189 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained); | 198 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained); |
| 199 if (!continue_url.is_empty()) { |
| 200 DCHECK(continue_url.is_valid()); |
| 201 std::string escaped_continue_url = |
| 202 net::EscapeQueryParamValue(continue_url.spec(), false); |
| 203 base::StringAppendF(&url, |
| 204 "&%s=%s", |
| 205 kSignInPromoQueryKeyContinueUrl, |
| 206 escaped_continue_url.c_str()); |
| 207 } |
190 return GURL(url); | 208 return GURL(url); |
191 } | 209 } |
192 | 210 |
193 // Build a Gaia-based URL that can be used to sign the user into chrome. | 211 // Build a Gaia-based URL that can be used to sign the user into chrome. |
194 // There are required request parameters: | 212 // There are required request parameters: |
195 // | 213 // |
196 // - tell Gaia which service the user is signing into. In this case, | 214 // - tell Gaia which service the user is signing into. In this case, |
197 // a chrome sign in uses the service "chromiumsync" | 215 // a chrome sign in uses the service "chromiumsync" |
198 // - provide a continue URL. This is the URL that Gaia will redirect to | 216 // - provide a continue URL. This is the URL that Gaia will redirect to |
199 // once the sign is complete. | 217 // once the sign is complete. |
200 // | 218 // |
201 // The continue URL includes a source parameter that can be extracted using | 219 // The continue URL includes a source parameter that can be extracted using |
202 // the function GetSourceForSignInPromoURL() below. This is used to know | 220 // the function GetSourceForSignInPromoURL() below. This is used to know |
203 // which of the chrome sign in access points was used to sign the user in. | 221 // which of the chrome sign in access points was used to sign the user in. |
204 // It is also parsed for the |auto_close| flag, which indicates that the tab | 222 // It is also parsed for the |auto_close| flag, which indicates that the tab |
205 // must be closed after sync setup is successful. | 223 // must be closed after sync setup is successful. |
206 // See OneClickSigninHelper for details. | 224 // See OneClickSigninHelper for details. |
207 std::string query_string = "?service=chromiumsync&sarp=1"; | 225 std::string query_string = "?service=chromiumsync&sarp=1"; |
208 | 226 |
209 std::string continue_url = GetLandingURL(kSignInPromoQueryKeySource, | 227 DCHECK(continue_url.is_empty()); |
210 static_cast<int>(source)).spec(); | 228 std::string continue_url_str = GetLandingURL(kSignInPromoQueryKeySource, |
211 if (auto_close) | 229 static_cast<int>(source)).spec(); |
212 base::StringAppendF(&continue_url, "&%s=1", kSignInPromoQueryKeyAutoClose); | 230 if (auto_close) { |
| 231 base::StringAppendF( |
| 232 &continue_url_str, "&%s=1", kSignInPromoQueryKeyAutoClose); |
| 233 } |
213 | 234 |
214 base::StringAppendF(&query_string, "&%s=%s", kSignInPromoQueryKeyContinue, | 235 base::StringAppendF( |
215 net::EscapeQueryParamValue( | 236 &query_string, |
216 continue_url, false).c_str()); | 237 "&%s=%s", |
| 238 kSignInPromoQueryKeyContinue, |
| 239 net::EscapeQueryParamValue(continue_url_str, false).c_str()); |
217 | 240 |
218 return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string); | 241 return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string); |
219 } | 242 } |
220 | 243 |
221 GURL GetReauthURL(Profile* profile, const std::string& account_id) { | 244 GURL GetReauthURL(Profile* profile, const std::string& account_id) { |
222 if (switches::IsEnableWebBasedSignin()) { | 245 if (switches::IsEnableWebBasedSignin()) { |
223 return net::AppendQueryParameter( | 246 return net::AppendQueryParameter( |
224 signin::GetPromoURL(signin::SOURCE_SETTINGS, true), | 247 signin::GetPromoURL(signin::SOURCE_SETTINGS, true), |
225 "Email", | 248 "Email", |
226 account_id); | 249 account_id); |
227 } | 250 } |
228 | 251 |
229 const std::string primary_account_id = | 252 const std::string primary_account_id = |
230 SigninManagerFactory::GetForProfile(profile)-> | 253 SigninManagerFactory::GetForProfile(profile)-> |
231 GetAuthenticatedAccountId(); | 254 GetAuthenticatedAccountId(); |
232 signin::Source source = account_id == primary_account_id ? | 255 signin::Source source = account_id == primary_account_id ? |
233 signin::SOURCE_SETTINGS : signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT; | 256 signin::SOURCE_SETTINGS : signin::SOURCE_AVATAR_BUBBLE_ADD_ACCOUNT; |
234 | 257 |
235 GURL url = signin::GetPromoURL(source, true); | 258 GURL url = signin::GetPromoURL(source, true); |
236 url = net::AppendQueryParameter(url, "email", account_id); | 259 url = net::AppendQueryParameter(url, "email", account_id); |
237 url = net::AppendQueryParameter(url, "validateEmail", "1"); | 260 url = net::AppendQueryParameter(url, "validateEmail", "1"); |
238 return net::AppendQueryParameter(url, "readOnlyEmail", "1"); | 261 return net::AppendQueryParameter(url, "readOnlyEmail", "1"); |
239 } | 262 } |
240 | 263 |
241 GURL GetNextPageURLForPromoURL(const GURL& url) { | 264 GURL GetNextPageURLForPromoURL(const GURL& url) { |
242 std::string value; | 265 std::string value; |
243 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) | 266 if (GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) |
244 return GURL(value); | 267 return GURL(value); |
245 | 268 |
| 269 value = std::string(); |
| 270 if (GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinueUrl, &value)) { |
| 271 GURL continue_url = GURL(value); |
| 272 if (continue_url.is_valid()) |
| 273 return continue_url; |
| 274 } |
| 275 |
246 return GURL(); | 276 return GURL(); |
247 } | 277 } |
248 | 278 |
249 Source GetSourceForPromoURL(const GURL& url) { | 279 Source GetSourceForPromoURL(const GURL& url) { |
250 std::string value; | 280 std::string value; |
251 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) { | 281 if (GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) { |
252 int source = 0; | 282 int source = 0; |
253 if (base::StringToInt(value, &source) && source >= SOURCE_START_PAGE && | 283 if (base::StringToInt(value, &source) && source >= SOURCE_START_PAGE && |
254 source < SOURCE_UNKNOWN) { | 284 source < SOURCE_UNKNOWN) { |
255 return static_cast<Source>(source); | 285 return static_cast<Source>(source); |
256 } | 286 } |
257 } | 287 } |
258 return SOURCE_UNKNOWN; | 288 return SOURCE_UNKNOWN; |
259 } | 289 } |
260 | 290 |
261 bool IsAutoCloseEnabledInURL(const GURL& url) { | 291 bool IsAutoCloseEnabledInURL(const GURL& url) { |
262 std::string value; | 292 std::string value; |
263 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) { | 293 if (GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) { |
264 int enabled = 0; | 294 int enabled = 0; |
265 if (base::StringToInt(value, &enabled) && enabled == 1) | 295 if (base::StringToInt(value, &enabled) && enabled == 1) |
266 return true; | 296 return true; |
267 } | 297 } |
268 return false; | 298 return false; |
269 } | 299 } |
270 | 300 |
271 bool IsContinueUrlForWebBasedSigninFlow(const GURL& url) { | 301 bool IsContinueUrlForWebBasedSigninFlow(const GURL& url) { |
272 GURL::Replacements replacements; | 302 GURL::Replacements replacements; |
273 replacements.ClearQuery(); | 303 replacements.ClearQuery(); |
(...skipping 27 matching lines...) Expand all Loading... |
301 prefs::kSignInPromoShowOnFirstRunAllowed, | 331 prefs::kSignInPromoShowOnFirstRunAllowed, |
302 true, | 332 true, |
303 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 333 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
304 registry->RegisterBooleanPref( | 334 registry->RegisterBooleanPref( |
305 prefs::kSignInPromoShowNTPBubble, | 335 prefs::kSignInPromoShowNTPBubble, |
306 false, | 336 false, |
307 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); | 337 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); |
308 } | 338 } |
309 | 339 |
310 } // namespace signin | 340 } // namespace signin |
OLD | NEW |