Update V8 to version 4.6.22.
[chromium-blink-merge.git] / chrome / browser / signin / signin_promo.cc
blob0f0c1e0ddaec0b6c4dc0b8b0c76adcfae9c82ca2
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 #if defined(OS_WIN)
42 #include "base/win/windows_version.h"
43 #endif
45 using content::WebContents;
47 namespace {
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() {
57 std::string brand;
58 google_brand::GetBrand(&brand);
60 if (brand.empty())
61 return true;
63 if (google_brand::IsInternetCafeBrandCode(brand))
64 return false;
66 // Enable for both organic and distribution.
67 return true;
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);
80 } // namespace
82 namespace signin {
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
87 // already logged in.
88 return false;
89 #else
91 // Don't bother if we don't have any kind of network connection.
92 if (net::NetworkChangeNotifier::IsOffline())
93 return false;
95 // Don't show for supervised profiles.
96 if (profile->IsSupervised())
97 return false;
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();
104 #endif
107 bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
108 DCHECK(profile);
110 // Don't show if the profile is an incognito.
111 if (profile->IsOffTheRecord())
112 return false;
114 if (!ShouldShowPromo(profile))
115 return false;
117 if (!is_new_profile) {
118 if (!HasShownPromoAtStartup(profile))
119 return false;
122 #if defined(OS_WIN)
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)
125 return false;
126 #endif
128 if (HasUserSkippedPromo(profile))
129 return false;
131 // For Chinese users skip the sign in promo.
132 if (g_browser_process->GetApplicationLocale() == "zh-CN")
133 return false;
135 PrefService* prefs = profile->GetPrefs();
136 int show_count = prefs->GetInteger(prefs::kSignInPromoStartupCount);
137 if (show_count >= kSignInPromoShowAtStartupMaximum)
138 return false;
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())
147 return false;
149 // Default to show the promo for Google Chrome builds.
150 #if defined(GOOGLE_CHROME_BUILD)
151 return true;
152 #else
153 return false;
154 #endif
157 void DidShowPromoAtStartup(Profile* profile) {
158 int show_count = profile->GetPrefs()->GetInteger(
159 prefs::kSignInPromoStartupCount);
160 show_count++;
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,
171 option,
172 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 std::string url(chrome::kChromeUIChromeSigninURL);
186 base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source);
187 if (auto_close)
188 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose);
189 if (is_constrained)
190 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained);
191 return GURL(url);
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) {
211 std::string value;
212 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) {
213 GURL continue_url = GURL(value);
214 if (continue_url.is_valid())
215 return continue_url;
218 return GURL();
221 GURL GetSigninPartitionURL() {
222 return GURL("chrome-guest://chrome-signin/?");
225 signin_metrics::Source GetSourceForPromoURL(const GURL& url) {
226 std::string value;
227 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
228 int source = 0;
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) {
239 std::string value;
240 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
241 int enabled = 0;
242 if (base::StringToInt(value, &enabled) && enabled == 1)
243 return true;
245 return false;
248 bool ShouldShowAccountManagement(const GURL& url) {
249 std::string value;
250 if (net::GetValueForKeyInQuery(
251 url, kSignInPromoQueryKeyShowAccountManagement, &value)) {
252 int enabled = 0;
253 if (base::StringToInt(value, &enabled) && enabled == 1)
254 return true;
256 return false;
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