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"
42 #include "base/win/windows_version.h"
45 using content::WebContents
;
49 // The maximum number of times we want to show the sign in promo at startup.
50 const int kSignInPromoShowAtStartupMaximum
= 10;
52 // Forces the web based signin flow when set.
53 bool g_force_web_based_signin_flow
= false;
55 // Checks we want to show the sign in promo for the given brand.
56 bool AllowPromoAtStartupForCurrentBrand() {
58 google_brand::GetBrand(&brand
);
63 if (google_brand::IsInternetCafeBrandCode(brand
))
66 // Enable for both organic and distribution.
70 // Returns true if a user has seen the sign in promo at startup previously.
71 bool HasShownPromoAtStartup(Profile
* profile
) {
72 return profile
->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount
);
75 // Returns true if the user has previously skipped the sign in promo.
76 bool HasUserSkippedPromo(Profile
* profile
) {
77 return profile
->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped
);
84 bool ShouldShowPromo(Profile
* profile
) {
85 #if defined(OS_CHROMEOS)
86 // There's no need to show the sign in promo on cros since cros users are
91 // Don't bother if we don't have any kind of network connection.
92 if (net::NetworkChangeNotifier::IsOffline())
95 // Don't show for supervised profiles.
96 if (profile
->IsSupervised())
99 // Display the signin promo if the user is not signed in.
100 SigninManager
* signin
= SigninManagerFactory::GetForProfile(
101 profile
->GetOriginalProfile());
102 return !signin
->AuthInProgress() && signin
->IsSigninAllowed() &&
103 !signin
->IsAuthenticated();
107 bool ShouldShowPromoAtStartup(Profile
* profile
, bool is_new_profile
) {
110 // Don't show if the profile is an incognito.
111 if (profile
->IsOffTheRecord())
114 if (!ShouldShowPromo(profile
))
117 if (!is_new_profile
) {
118 if (!HasShownPromoAtStartup(profile
))
123 // Do not show the promo on first run on Win10 and newer.
124 if (is_new_profile
&& base::win::GetVersion() >= base::win::VERSION_WIN10
)
128 if (HasUserSkippedPromo(profile
))
131 // For Chinese users skip the sign in promo.
132 if (g_browser_process
->GetApplicationLocale() == "zh-CN")
135 PrefService
* prefs
= profile
->GetPrefs();
136 int show_count
= prefs
->GetInteger(prefs::kSignInPromoStartupCount
);
137 if (show_count
>= kSignInPromoShowAtStartupMaximum
)
140 // This pref can be set in the master preferences file to allow or disallow
141 // showing the sign in promo at startup.
142 if (prefs
->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed
))
143 return prefs
->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed
);
145 // For now don't show the promo for some brands.
146 if (!AllowPromoAtStartupForCurrentBrand())
149 // Default to show the promo for Google Chrome builds.
150 #if defined(GOOGLE_CHROME_BUILD)
157 void DidShowPromoAtStartup(Profile
* profile
) {
158 int show_count
= profile
->GetPrefs()->GetInteger(
159 prefs::kSignInPromoStartupCount
);
161 profile
->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount
, show_count
);
164 void SetUserSkippedPromo(Profile
* profile
) {
165 profile
->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped
, true);
168 GURL
GetLandingURL(const char* option
, int value
) {
169 std::string url
= base::StringPrintf("%s/success.html?%s=%d",
170 extensions::kGaiaAuthExtensionOrigin
,
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
,
182 bool is_constrained
) {
183 DCHECK_NE(signin_metrics::SOURCE_UNKNOWN
, source
);
185 std::string
url(chrome::kChromeUIChromeSigninURL
);
186 base::StringAppendF(&url
, "?%s=%d", kSignInPromoQueryKeySource
, source
);
188 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
190 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyConstrained
);
194 GURL
GetReauthURL(Profile
* profile
, const std::string
& account_id
) {
195 AccountTrackerService::AccountInfo info
=
196 AccountTrackerServiceFactory::GetForProfile(profile
)->
197 GetAccountInfo(account_id
);
199 signin_metrics::Source source
= switches::IsNewAvatarMenu() ?
200 signin_metrics::SOURCE_REAUTH
: signin_metrics::SOURCE_SETTINGS
;
202 GURL url
= signin::GetPromoURL(
203 source
, true /* auto_close */,
204 switches::IsNewAvatarMenu() /* is_constrained */);
205 url
= net::AppendQueryParameter(url
, "email", info
.email
);
206 url
= net::AppendQueryParameter(url
, "validateEmail", "1");
207 return net::AppendQueryParameter(url
, "readOnlyEmail", "1");
210 GURL
GetNextPageURLForPromoURL(const GURL
& url
) {
212 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyContinue
, &value
)) {
213 GURL continue_url
= GURL(value
);
214 if (continue_url
.is_valid())
221 GURL
GetSigninPartitionURL() {
222 return GURL("chrome-guest://chrome-signin/?");
225 signin_metrics::Source
GetSourceForPromoURL(const GURL
& url
) {
227 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeySource
, &value
)) {
229 if (base::StringToInt(value
, &source
) &&
230 source
>= signin_metrics::SOURCE_START_PAGE
&&
231 source
< signin_metrics::SOURCE_UNKNOWN
) {
232 return static_cast<signin_metrics::Source
>(source
);
235 return signin_metrics::SOURCE_UNKNOWN
;
238 bool IsAutoCloseEnabledInURL(const GURL
& url
) {
240 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyAutoClose
, &value
)) {
242 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
248 bool ShouldShowAccountManagement(const GURL
& url
) {
250 if (net::GetValueForKeyInQuery(
251 url
, kSignInPromoQueryKeyShowAccountManagement
, &value
)) {
253 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
259 void ForceWebBasedSigninFlowForTesting(bool force
) {
260 g_force_web_based_signin_flow
= force
;
263 void RegisterProfilePrefs(
264 user_prefs::PrefRegistrySyncable
* registry
) {
265 registry
->RegisterIntegerPref(prefs::kSignInPromoStartupCount
, 0);
266 registry
->RegisterBooleanPref(prefs::kSignInPromoUserSkipped
, false);
267 registry
->RegisterBooleanPref(prefs::kSignInPromoShowOnFirstRunAllowed
, true);
268 registry
->RegisterBooleanPref(prefs::kSignInPromoShowNTPBubble
, false);
271 } // namespace signin