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"
41 using content::WebContents
;
45 // The maximum number of times we want to show the sign in promo at startup.
46 const int kSignInPromoShowAtStartupMaximum
= 10;
48 // Forces the web based signin flow when set.
49 bool g_force_web_based_signin_flow
= false;
51 // Checks we want to show the sign in promo for the given brand.
52 bool AllowPromoAtStartupForCurrentBrand() {
54 google_brand::GetBrand(&brand
);
59 if (google_brand::IsInternetCafeBrandCode(brand
))
62 // Enable for both organic and distribution.
66 // Returns true if a user has seen the sign in promo at startup previously.
67 bool HasShownPromoAtStartup(Profile
* profile
) {
68 return profile
->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount
);
71 // Returns true if the user has previously skipped the sign in promo.
72 bool HasUserSkippedPromo(Profile
* profile
) {
73 return profile
->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped
);
80 bool ShouldShowPromo(Profile
* profile
) {
81 #if defined(OS_CHROMEOS)
82 // There's no need to show the sign in promo on cros since cros users are
87 // Don't bother if we don't have any kind of network connection.
88 if (net::NetworkChangeNotifier::IsOffline())
91 // Don't show for supervised profiles.
92 if (profile
->IsSupervised())
95 // Display the signin promo if the user is not signed in.
96 SigninManager
* signin
= SigninManagerFactory::GetForProfile(
97 profile
->GetOriginalProfile());
98 return !signin
->AuthInProgress() && signin
->IsSigninAllowed() &&
99 !signin
->IsAuthenticated();
103 bool ShouldShowPromoAtStartup(Profile
* profile
, bool is_new_profile
) {
106 // Don't show if the profile is an incognito.
107 if (profile
->IsOffTheRecord())
110 if (!ShouldShowPromo(profile
))
113 if (!is_new_profile
) {
114 if (!HasShownPromoAtStartup(profile
))
118 if (HasUserSkippedPromo(profile
))
121 // For Chinese users skip the sign in promo.
122 if (g_browser_process
->GetApplicationLocale() == "zh-CN")
125 PrefService
* prefs
= profile
->GetPrefs();
126 int show_count
= prefs
->GetInteger(prefs::kSignInPromoStartupCount
);
127 if (show_count
>= kSignInPromoShowAtStartupMaximum
)
130 // This pref can be set in the master preferences file to allow or disallow
131 // showing the sign in promo at startup.
132 if (prefs
->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed
))
133 return prefs
->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed
);
135 // For now don't show the promo for some brands.
136 if (!AllowPromoAtStartupForCurrentBrand())
139 // Default to show the promo for Google Chrome builds.
140 #if defined(GOOGLE_CHROME_BUILD)
147 void DidShowPromoAtStartup(Profile
* profile
) {
148 int show_count
= profile
->GetPrefs()->GetInteger(
149 prefs::kSignInPromoStartupCount
);
151 profile
->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount
, show_count
);
154 void SetUserSkippedPromo(Profile
* profile
) {
155 profile
->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped
, true);
158 GURL
GetLandingURL(const char* option
, int value
) {
159 std::string url
= base::StringPrintf("%s/success.html?%s=%d",
160 extensions::kGaiaAuthExtensionOrigin
,
166 GURL
GetPromoURL(signin_metrics::Source source
, bool auto_close
) {
167 return GetPromoURL(source
, auto_close
, false /* is_constrained */);
170 GURL
GetPromoURL(signin_metrics::Source source
,
172 bool is_constrained
) {
173 DCHECK_NE(signin_metrics::SOURCE_UNKNOWN
, source
);
175 std::string
url(chrome::kChromeUIChromeSigninURL
);
176 base::StringAppendF(&url
, "?%s=%d", kSignInPromoQueryKeySource
, source
);
178 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyAutoClose
);
180 base::StringAppendF(&url
, "&%s=1", kSignInPromoQueryKeyConstrained
);
184 GURL
GetReauthURL(Profile
* profile
, const std::string
& account_id
) {
185 AccountTrackerService::AccountInfo info
=
186 AccountTrackerServiceFactory::GetForProfile(profile
)->
187 GetAccountInfo(account_id
);
189 signin_metrics::Source source
= switches::IsNewAvatarMenu() ?
190 signin_metrics::SOURCE_REAUTH
: signin_metrics::SOURCE_SETTINGS
;
192 GURL url
= signin::GetPromoURL(
193 source
, true /* auto_close */,
194 switches::IsNewAvatarMenu() /* is_constrained */);
195 url
= net::AppendQueryParameter(url
, "email", info
.email
);
196 url
= net::AppendQueryParameter(url
, "validateEmail", "1");
197 return net::AppendQueryParameter(url
, "readOnlyEmail", "1");
200 GURL
GetNextPageURLForPromoURL(const GURL
& url
) {
202 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyContinue
, &value
)) {
203 GURL continue_url
= GURL(value
);
204 if (continue_url
.is_valid())
211 GURL
GetSigninPartitionURL() {
212 return GURL("chrome-guest://chrome-signin/?");
215 signin_metrics::Source
GetSourceForPromoURL(const GURL
& url
) {
217 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeySource
, &value
)) {
219 if (base::StringToInt(value
, &source
) &&
220 source
>= signin_metrics::SOURCE_START_PAGE
&&
221 source
< signin_metrics::SOURCE_UNKNOWN
) {
222 return static_cast<signin_metrics::Source
>(source
);
225 return signin_metrics::SOURCE_UNKNOWN
;
228 bool IsAutoCloseEnabledInURL(const GURL
& url
) {
230 if (net::GetValueForKeyInQuery(url
, kSignInPromoQueryKeyAutoClose
, &value
)) {
232 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
238 bool ShouldShowAccountManagement(const GURL
& url
) {
240 if (net::GetValueForKeyInQuery(
241 url
, kSignInPromoQueryKeyShowAccountManagement
, &value
)) {
243 if (base::StringToInt(value
, &enabled
) && enabled
== 1)
249 void ForceWebBasedSigninFlowForTesting(bool force
) {
250 g_force_web_based_signin_flow
= force
;
253 void RegisterProfilePrefs(
254 user_prefs::PrefRegistrySyncable
* registry
) {
255 registry
->RegisterIntegerPref(
256 prefs::kSignInPromoStartupCount
,
258 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
259 registry
->RegisterBooleanPref(
260 prefs::kSignInPromoUserSkipped
,
262 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
263 registry
->RegisterBooleanPref(
264 prefs::kSignInPromoShowOnFirstRunAllowed
,
266 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
267 registry
->RegisterBooleanPref(
268 prefs::kSignInPromoShowNTPBubble
,
270 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
273 } // namespace signin