Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / browser / signin / signin_promo.cc
blob0637886778a4c363e2bbe264b2b937f7082779e0
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/pref_names.h"
24 #include "chrome/common/url_constants.h"
25 #include "components/google/core/browser/google_util.h"
26 #include "components/pref_registry/pref_registry_syncable.h"
27 #include "components/signin/core/browser/account_tracker_service.h"
28 #include "components/signin/core/browser/signin_manager.h"
29 #include "components/signin/core/common/profile_management_switches.h"
30 #include "content/public/browser/url_data_source.h"
31 #include "content/public/browser/web_contents.h"
32 #include "content/public/browser/web_ui.h"
33 #include "content/public/browser/web_ui_data_source.h"
34 #include "google_apis/gaia/gaia_urls.h"
35 #include "net/base/escape.h"
36 #include "net/base/network_change_notifier.h"
37 #include "net/base/url_util.h"
38 #include "url/gurl.h"
40 #if defined(OS_WIN)
41 #include "base/win/windows_version.h"
42 #endif
44 using content::WebContents;
46 namespace {
48 // The maximum number of times we want to show the sign in promo at startup.
49 const int kSignInPromoShowAtStartupMaximum = 10;
51 // Forces the web based signin flow when set.
52 bool g_force_web_based_signin_flow = false;
54 // Checks we want to show the sign in promo for the given brand.
55 bool AllowPromoAtStartupForCurrentBrand() {
56 std::string brand;
57 google_brand::GetBrand(&brand);
59 if (brand.empty())
60 return true;
62 if (google_brand::IsInternetCafeBrandCode(brand))
63 return false;
65 // Enable for both organic and distribution.
66 return true;
69 // Returns true if a user has seen the sign in promo at startup previously.
70 bool HasShownPromoAtStartup(Profile* profile) {
71 return profile->GetPrefs()->HasPrefPath(prefs::kSignInPromoStartupCount);
74 // Returns true if the user has previously skipped the sign in promo.
75 bool HasUserSkippedPromo(Profile* profile) {
76 return profile->GetPrefs()->GetBoolean(prefs::kSignInPromoUserSkipped);
79 } // namespace
81 namespace signin {
83 bool ShouldShowPromo(Profile* profile) {
84 #if defined(OS_CHROMEOS)
85 // There's no need to show the sign in promo on cros since cros users are
86 // already logged in.
87 return false;
88 #else
90 // Don't bother if we don't have any kind of network connection.
91 if (net::NetworkChangeNotifier::IsOffline())
92 return false;
94 // Don't show for supervised profiles.
95 if (profile->IsSupervised())
96 return false;
98 // Display the signin promo if the user is not signed in.
99 SigninManager* signin = SigninManagerFactory::GetForProfile(
100 profile->GetOriginalProfile());
101 return !signin->AuthInProgress() && signin->IsSigninAllowed() &&
102 !signin->IsAuthenticated();
103 #endif
106 bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
107 DCHECK(profile);
109 // Don't show if the profile is an incognito.
110 if (profile->IsOffTheRecord())
111 return false;
113 if (!ShouldShowPromo(profile))
114 return false;
116 if (!is_new_profile) {
117 if (!HasShownPromoAtStartup(profile))
118 return false;
121 #if defined(OS_WIN)
122 // Do not show the promo on first run on Win10 and newer.
123 if (is_new_profile && base::win::GetVersion() >= base::win::VERSION_WIN10)
124 return false;
125 #endif
127 if (HasUserSkippedPromo(profile))
128 return false;
130 // For Chinese users skip the sign in promo.
131 if (g_browser_process->GetApplicationLocale() == "zh-CN")
132 return false;
134 PrefService* prefs = profile->GetPrefs();
135 int show_count = prefs->GetInteger(prefs::kSignInPromoStartupCount);
136 if (show_count >= kSignInPromoShowAtStartupMaximum)
137 return false;
139 // This pref can be set in the master preferences file to allow or disallow
140 // showing the sign in promo at startup.
141 if (prefs->HasPrefPath(prefs::kSignInPromoShowOnFirstRunAllowed))
142 return prefs->GetBoolean(prefs::kSignInPromoShowOnFirstRunAllowed);
144 // For now don't show the promo for some brands.
145 if (!AllowPromoAtStartupForCurrentBrand())
146 return false;
148 // Default to show the promo for Google Chrome builds.
149 #if defined(GOOGLE_CHROME_BUILD)
150 return true;
151 #else
152 return false;
153 #endif
156 void DidShowPromoAtStartup(Profile* profile) {
157 int show_count = profile->GetPrefs()->GetInteger(
158 prefs::kSignInPromoStartupCount);
159 show_count++;
160 profile->GetPrefs()->SetInteger(prefs::kSignInPromoStartupCount, show_count);
163 void SetUserSkippedPromo(Profile* profile) {
164 profile->GetPrefs()->SetBoolean(prefs::kSignInPromoUserSkipped, true);
167 GURL GetLandingURL(const char* option, int value) {
168 std::string url = base::StringPrintf("%s/success.html?%s=%d",
169 extensions::kGaiaAuthExtensionOrigin,
170 option,
171 value);
172 return GURL(url);
175 GURL GetPromoURL(signin_metrics::Source source, bool auto_close) {
176 return GetPromoURL(source, auto_close, false /* is_constrained */);
179 GURL GetPromoURL(signin_metrics::Source source,
180 bool auto_close,
181 bool is_constrained) {
182 DCHECK_NE(signin_metrics::SOURCE_UNKNOWN, source);
184 std::string url(chrome::kChromeUIChromeSigninURL);
185 base::StringAppendF(&url, "?%s=%d", kSignInPromoQueryKeySource, source);
186 if (auto_close)
187 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyAutoClose);
188 if (is_constrained)
189 base::StringAppendF(&url, "&%s=1", kSignInPromoQueryKeyConstrained);
190 return GURL(url);
193 GURL GetReauthURL(Profile* profile, const std::string& account_id) {
194 AccountInfo info = AccountTrackerServiceFactory::GetForProfile(profile)
195 ->GetAccountInfo(account_id);
196 return GetReauthURLWithEmail(info.email);
199 GURL GetReauthURLWithEmail(const std::string& email) {
200 signin_metrics::Source source = switches::IsNewAvatarMenu() ?
201 signin_metrics::SOURCE_REAUTH : signin_metrics::SOURCE_SETTINGS;
203 GURL url = signin::GetPromoURL(
204 source, true /* auto_close */,
205 switches::IsNewAvatarMenu() /* is_constrained */);
207 url = net::AppendQueryParameter(url, "email", email);
208 url = net::AppendQueryParameter(url, "validateEmail", "1");
209 return net::AppendQueryParameter(url, "readOnlyEmail", "1");
212 GURL GetNextPageURLForPromoURL(const GURL& url) {
213 std::string value;
214 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value)) {
215 GURL continue_url = GURL(value);
216 if (continue_url.is_valid())
217 return continue_url;
220 return GURL();
223 GURL GetSigninPartitionURL() {
224 return GURL("chrome-guest://chrome-signin/?");
227 signin_metrics::Source GetSourceForPromoURL(const GURL& url) {
228 std::string value;
229 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
230 int source = 0;
231 if (base::StringToInt(value, &source) &&
232 source >= signin_metrics::SOURCE_START_PAGE &&
233 source < signin_metrics::SOURCE_UNKNOWN) {
234 return static_cast<signin_metrics::Source>(source);
237 return signin_metrics::SOURCE_UNKNOWN;
240 bool IsAutoCloseEnabledInURL(const GURL& url) {
241 std::string value;
242 if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
243 int enabled = 0;
244 if (base::StringToInt(value, &enabled) && enabled == 1)
245 return true;
247 return false;
250 bool ShouldShowAccountManagement(const GURL& url) {
251 std::string value;
252 if (net::GetValueForKeyInQuery(
253 url, kSignInPromoQueryKeyShowAccountManagement, &value)) {
254 int enabled = 0;
255 if (base::StringToInt(value, &enabled) && enabled == 1)
256 return true;
258 return false;
261 void ForceWebBasedSigninFlowForTesting(bool force) {
262 g_force_web_based_signin_flow = force;
265 void RegisterProfilePrefs(
266 user_prefs::PrefRegistrySyncable* registry) {
267 registry->RegisterIntegerPref(prefs::kSignInPromoStartupCount, 0);
268 registry->RegisterBooleanPref(prefs::kSignInPromoUserSkipped, false);
269 registry->RegisterBooleanPref(prefs::kSignInPromoShowOnFirstRunAllowed, true);
270 registry->RegisterBooleanPref(prefs::kSignInPromoShowNTPBubble, false);
273 } // namespace signin