base::Time multiplicative operator overloading
[chromium-blink-merge.git] / chrome / browser / signin / signin_promo.cc
blobeed310afe70d4f0491922781a63df6cd6a68d97d
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
3 // found in the LICENSE file.
5 #include "chrome/browser/signin/signin_promo.h"
7 #include "base/prefs/pref_service.h"
8 #include "base/strings/string_number_conversions.h"
9 #include "base/strings/string_util.h"
10 #include "base/strings/stringprintf.h"
11 #include "base/strings/utf_string_conversions.h"
12 #include "chrome/browser/browser_process.h"
13 #include "chrome/browser/extensions/signin/gaia_auth_extension_loader.h"
14 #include "chrome/browser/first_run/first_run.h"
15 #include "chrome/browser/google/google_brand.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/account_tracker_service_factory.h"
20 #include "chrome/browser/signin/signin_manager_factory.h"
21 #include "chrome/browser/ui/webui/options/core_options_handler.h"
22 #include "chrome/browser/ui/webui/theme_source.h"
23 #include "chrome/common/net/url_util.h"
24 #include "chrome/common/pref_names.h"
25 #include "chrome/common/url_constants.h"
26 #include "components/google/core/browser/google_util.h"
27 #include "components/pref_registry/pref_registry_syncable.h"
28 #include "components/signin/core/browser/account_tracker_service.h"
29 #include "components/signin/core/browser/signin_manager.h"
30 #include "components/signin/core/common/profile_management_switches.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 "net/base/escape.h"
37 #include "net/base/network_change_notifier.h"
38 #include "net/base/url_util.h"
39 #include "url/gurl.h"
41 using content::WebContents;
43 namespace {
45 // Gaia cannot support about:blank as a continue URL, so using a hosted blank
46 // page instead.
47 const char kSignInLandingUrlPrefix[] =
48 "https://www.google.com/intl/%s/chrome/blank.html";
50 // The maximum number of times we want to show the sign in promo at startup.
51 const int kSignInPromoShowAtStartupMaximum = 10;
53 // Forces the web based signin flow when set.
54 bool g_force_web_based_signin_flow = false;
56 // Checks we want to show the sign in promo for the given brand.
57 bool AllowPromoAtStartupForCurrentBrand() {
58 std::string brand;
59 google_brand::GetBrand(&brand);
61 if (brand.empty())
62 return true;
64 if (google_brand::IsInternetCafeBrandCode(brand))
65 return false;
67 // Enable for both organic and distribution.
68 return true;
71 // Returns true if a user has seen the sign in promo at startup previously.
72 bool HasShownPromoAtStartup(Profile* profile) {
73 return profile->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount);
76 // Returns true if the user has previously skipped the sign in promo.
77 bool HasUserSkippedPromo(Profile* profile) {
78 return profile->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped);
81 } // namespace
83 namespace signin {
85 bool ShouldShowPromo(Profile* profile) {
86 #if defined(OS_CHROMEOS)
87 // There's no need to show the sign in promo on cros since cros users are
88 // already logged in.
89 return false;
90 #else
92 // Don't bother if we don't have any kind of network connection.
93 if (net::NetworkChangeNotifier::IsOffline())
94 return false;
96 // Don't show for supervised profiles.
97 if (profile->IsSupervised())
98 return false;
100 // Display the signin promo if the user is not signed in.
101 SigninManager* signin = SigninManagerFactory::GetForProfile(
102 profile->GetOriginalProfile());
103 return !signin->AuthInProgress() && signin->IsSigninAllowed() &&
104 !signin->IsAuthenticated();
105 #endif
108 bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
109 DCHECK(profile);
111 // Don't show if the profile is an incognito.
112 if (profile->IsOffTheRecord())
113 return false;
115 if (!ShouldShowPromo(profile))
116 return false;
118 if (!is_new_profile) {
119 if (!HasShownPromoAtStartup(profile))
120 return false;
123 if (HasUserSkippedPromo(profile))
124 return false;
126 // For Chinese users skip the sign in promo.
127 if (g_browser_process->GetApplicationLocale() == "zh-CN")
128 return false;
130 PrefService* prefs = profile->GetPrefs();
131 int show_count = prefs->GetInteger(prefs::kSignInPromoStartupCount);
132 if (show_count >= kSignInPromoShowAtStartupMaximum)
133 return false;
135 // This pref can be set in the master preferences file to allow or disallow
136 // showing the sign in promo at startup.
137 if (prefs->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed))
138 return prefs->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed);
140 // For now don't show the promo for some brands.
141 if (!AllowPromoAtStartupForCurrentBrand())
142 return false;
144 // Default to show the promo for Google Chrome builds.
145 #if defined(GOOGLE_CHROME_BUILD)
146 return true;
147 #else
148 return false;
149 #endif
152 void DidShowPromoAtStartup(Profile* profile) {
153 int show_count = profile->GetPrefs()->GetInteger(
154 prefs::kSignInPromoStartupCount);
155 show_count++;
156 profile->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount, show_count);
159 void SetUserSkippedPromo(Profile* profile) {
160 profile->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped, true);
163 GURL GetLandingURL(const char* option, int value) {
164 std::string url;
165 if (switches::IsEnableWebBasedSignin()) {
166 const std::string& locale = g_browser_process->GetApplicationLocale();
167 url = base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str());
168 } else {
169 url = base::StringPrintf(
170 "%s/success.html", extensions::kGaiaAuthExtensionOrigin);
172 base::StringAppendF(&url, "?%s=%d", option, value);
173 return GURL(url);
176 GURL GetPromoURL(signin_metrics::Source source, bool auto_close) {
177 return GetPromoURL(source, auto_close, false /* is_constrained */);
180 GURL GetPromoURL(signin_metrics::Source source,
181 bool auto_close,
182 bool is_constrained) {
183 DCHECK_NE(signin_metrics::SOURCE_UNKNOWN, source);
185 if (!switches::IsEnableWebBasedSignin()) {
186 std::string url(chrome::kChromeUIChromeSigninURL);
187 base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source);
188 if (auto_close)
189 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose);
190 if (is_constrained)
191 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained);
192 return GURL(url);
195 // Build a Gaia-based URL that can be used to sign the user into chrome.
196 // There are required request parameters:
198 // - tell Gaia which service the user is signing into. In this case,
199 // a chrome sign in uses the service "chromiumsync"
200 // - provide a continue URL. This is the URL that Gaia will redirect to
201 // once the sign is complete.
203 // The continue URL includes a source parameter that can be extracted using
204 // the function GetSourceForSignInPromoURL() below. This is used to know
205 // which of the chrome sign in access points was used to sign the user in.
206 // It is also parsed for the |auto_close| flag, which indicates that the tab
207 // must be closed after sync setup is successful.
208 // See OneClickSigninHelper for details.
209 std::string query_string = "?service=chromiumsync&sarp=1";
211 std::string continue_url = GetLandingURL(kSignInPromoQueryKeySource,
212 static_cast<int>(source)).spec();
213 if (auto_close)
214 base::StringAppendF(&continue_url, "&%s=1", kSignInPromoQueryKeyAutoClose);
216 base::StringAppendF(
217 &query_string,
218 "&%s=%s",
219 kSignInPromoQueryKeyContinue,
220 net::EscapeQueryParamValue(continue_url, false).c_str());
222 return GaiaUrls::GetInstance()->service_login_url().Resolve(query_string);
225 GURL GetReauthURL(Profile* profile, const std::string& account_id) {
226 AccountTrackerService::AccountInfo info =
227 AccountTrackerServiceFactory::GetForProfile(profile)->
228 GetAccountInfo(account_id);
230 if (switches::IsEnableWebBasedSignin()) {
231 return net::AppendQueryParameter(
232 signin::GetPromoURL(signin_metrics::SOURCE_SETTINGS, true),
233 "Email",
234 account_id);
237 signin_metrics::Source source = switches::IsNewAvatarMenu() ?
238 signin_metrics::SOURCE_REAUTH : signin_metrics::SOURCE_SETTINGS;
240 GURL url = signin::GetPromoURL(
241 source, true /* auto_close */,
242 switches::IsNewAvatarMenu() /* is_constrained */);
243 url = net::AppendQueryParameter(url, "email", info.email);
244 url = net::AppendQueryParameter(url, "validateEmail", "1");
245 return net::AppendQueryParameter(url, "readOnlyEmail", "1");
248 GURL GetNextPageURLForPromoURL(const GURL& url) {
249 std::string value;
250 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) {
251 GURL continue_url = GURL(value);
252 if (continue_url.is_valid())
253 return continue_url;
256 return GURL();
259 GURL GetSigninPartitionURL() {
260 return GURL(switches::IsEnableWebviewBasedSignin() ?
261 "chrome-guest://chrome-signin/?" :
262 chrome::kChromeUIChromeSigninURL);
265 signin_metrics::Source GetSourceForPromoURL(const GURL& url) {
266 std::string value;
267 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
268 int source = 0;
269 if (base::StringToInt(value, &source) &&
270 source >= signin_metrics::SOURCE_START_PAGE &&
271 source < signin_metrics::SOURCE_UNKNOWN) {
272 return static_cast<signin_metrics::Source>(source);
275 return signin_metrics::SOURCE_UNKNOWN;
278 bool IsAutoCloseEnabledInURL(const GURL& url) {
279 std::string value;
280 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
281 int enabled = 0;
282 if (base::StringToInt(value, &enabled) && enabled == 1)
283 return true;
285 return false;
288 bool ShouldShowAccountManagement(const GURL& url) {
289 std::string value;
290 if (net::GetValueForKeyInQuery(
291 url, kSignInPromoQueryKeyShowAccountManagement, &value)) {
292 int enabled = 0;
293 if (base::StringToInt(value, &enabled) && enabled == 1)
294 return true;
296 return false;
299 bool IsContinueUrlForWebBasedSigninFlow(const GURL& url) {
300 GURL::Replacements replacements;
301 replacements.ClearQuery();
302 const std::string& locale = g_browser_process->GetApplicationLocale();
303 GURL continue_url =
304 GURL(base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str()));
305 return (
306 google_util::IsGoogleDomainUrl(
307 url,
308 google_util::ALLOW_SUBDOMAIN,
309 google_util::DISALLOW_NON_STANDARD_PORTS) &&
310 url.ReplaceComponents(replacements).path() ==
311 continue_url.ReplaceComponents(replacements).path());
314 void ForceWebBasedSigninFlowForTesting(bool force) {
315 g_force_web_based_signin_flow = force;
318 void RegisterProfilePrefs(
319 user_prefs::PrefRegistrySyncable* registry) {
320 registry->RegisterIntegerPref(
321 prefs::kSignInPromoStartupCount,
323 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
324 registry->RegisterBooleanPref(
325 prefs::kSignInPromoUserSkipped,
326 false,
327 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
328 registry->RegisterBooleanPref(
329 prefs::kSignInPromoShowOnFirstRunAllowed,
330 true,
331 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
332 registry->RegisterBooleanPref(
333 prefs::kSignInPromoShowNTPBubble,
334 false,
335 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
338 } // namespace signin