Unwind the URL-based experiment IDs.
[chromium-blink-merge.git] / chrome / browser / signin / signin_promo.cc
bloba3404a91322bbbaf40d6d4c83b5c345915c9cf65
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 // 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() {
53 std::string brand;
54 google_brand::GetBrand(&brand);
56 if (brand.empty())
57 return true;
59 if (google_brand::IsInternetCafeBrandCode(brand))
60 return false;
62 // Enable for both organic and distribution.
63 return true;
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);
76 } // namespace
78 namespace signin {
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
83 // already logged in.
84 return false;
85 #else
87 // Don't bother if we don't have any kind of network connection.
88 if (net::NetworkChangeNotifier::IsOffline())
89 return false;
91 // Don't show for supervised profiles.
92 if (profile->IsSupervised())
93 return false;
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();
100 #endif
103 bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
104 DCHECK(profile);
106 // Don't show if the profile is an incognito.
107 if (profile->IsOffTheRecord())
108 return false;
110 if (!ShouldShowPromo(profile))
111 return false;
113 if (!is_new_profile) {
114 if (!HasShownPromoAtStartup(profile))
115 return false;
118 if (HasUserSkippedPromo(profile))
119 return false;
121 // For Chinese users skip the sign in promo.
122 if (g_browser_process->GetApplicationLocale() == "zh-CN")
123 return false;
125 PrefService* prefs = profile->GetPrefs();
126 int show_count = prefs->GetInteger(prefs::kSignInPromoStartupCount);
127 if (show_count >= kSignInPromoShowAtStartupMaximum)
128 return false;
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())
137 return false;
139 // Default to show the promo for Google Chrome builds.
140 #if defined(GOOGLE_CHROME_BUILD)
141 return true;
142 #else
143 return false;
144 #endif
147 void DidShowPromoAtStartup(Profile* profile) {
148 int show_count = profile->GetPrefs()->GetInteger(
149 prefs::kSignInPromoStartupCount);
150 show_count++;
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,
161 option,
162 value);
163 return GURL(url);
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,
171 bool auto_close,
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);
177 if (auto_close)
178 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose);
179 if (is_constrained)
180 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained);
181 return GURL(url);
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) {
201 std::string value;
202 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) {
203 GURL continue_url = GURL(value);
204 if (continue_url.is_valid())
205 return continue_url;
208 return GURL();
211 GURL GetSigninPartitionURL() {
212 return GURL("chrome-guest://chrome-signin/?");
215 signin_metrics::Source GetSourceForPromoURL(const GURL& url) {
216 std::string value;
217 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
218 int source = 0;
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) {
229 std::string value;
230 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
231 int enabled = 0;
232 if (base::StringToInt(value, &enabled) && enabled == 1)
233 return true;
235 return false;
238 bool ShouldShowAccountManagement(const GURL& url) {
239 std::string value;
240 if (net::GetValueForKeyInQuery(
241 url, kSignInPromoQueryKeyShowAccountManagement, &value)) {
242 int enabled = 0;
243 if (base::StringToInt(value, &enabled) && enabled == 1)
244 return true;
246 return false;
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,
261 false,
262 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
263 registry->RegisterBooleanPref(
264 prefs::kSignInPromoShowOnFirstRunAllowed,
265 true,
266 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
267 registry->RegisterBooleanPref(
268 prefs::kSignInPromoShowNTPBubble,
269 false,
270 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
273 } // namespace signin