signin_promo.cc revision 2385ea399aae016c0806a4f9ef3c9cfe3d2a39df
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.
4
5#include "chrome/browser/signin/signin_promo.h"
6
7#include "base/command_line.h"
8#include "base/prefs/pref_service.h"
9#include "base/strings/string_number_conversions.h"
10#include "base/strings/string_util.h"
11#include "base/strings/stringprintf.h"
12#include "base/strings/utf_string_conversions.h"
13#include "chrome/browser/browser_process.h"
14#include "chrome/browser/first_run/first_run.h"
15#include "chrome/browser/google/google_util.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/signin_manager.h"
20#include "chrome/browser/signin/signin_manager_factory.h"
21#include "chrome/browser/sync/profile_sync_service.h"
22#include "chrome/browser/sync/profile_sync_service_factory.h"
23#include "chrome/browser/ui/webui/options/core_options_handler.h"
24#include "chrome/browser/ui/webui/sync_promo/sync_promo_trial.h"
25#include "chrome/browser/ui/webui/theme_source.h"
26#include "chrome/common/chrome_switches.h"
27#include "chrome/common/net/url_util.h"
28#include "chrome/common/pref_names.h"
29#include "chrome/common/url_constants.h"
30#include "components/user_prefs/pref_registry_syncable.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 "grit/browser_resources.h"
37#include "grit/generated_resources.h"
38#include "grit/theme_resources.h"
39#include "net/base/escape.h"
40#include "net/base/network_change_notifier.h"
41#include "net/base/url_util.h"
42#include "ui/base/l10n/l10n_util.h"
43
44using content::WebContents;
45
46namespace {
47
48const char kStringsJsFile[] = "strings.js";
49const char kSignInPromoJsFile[] = "sync_promo.js";
50
51const char kSignInPromoQueryKeyAutoClose[] = "auto_close";
52const char kSignInPromoQueryKeyContinue[] = "continue";
53const char kSignInPromoQueryKeySource[] = "source";
54
55// Gaia cannot support about:blank as a continue URL, so using a hosted blank
56// page instead.
57const char kSignInLandingUrlPrefix[] =
58    "https://www.google.com/intl/%s/chrome/blank.html";
59
60// The maximum number of times we want to show the sign in promo at startup.
61const int kSignInPromoShowAtStartupMaximum = 10;
62
63// Forces the web based signin flow when set.
64bool g_force_web_based_signin_flow = false;
65
66// Checks we want to show the sign in promo for the given brand.
67bool AllowPromoAtStartupForCurrentBrand() {
68  std::string brand;
69  google_util::GetBrand(&brand);
70
71  if (brand.empty())
72    return true;
73
74  if (google_util::IsInternetCafeBrandCode(brand))
75    return false;
76
77  // Enable for both organic and distribution.
78  return true;
79}
80
81// Returns true if a user has seen the sign in promo at startup previously.
82bool HasShownPromoAtStartup(Profile* profile) {
83  return profile->GetPrefs()->HasPrefPath(prefs::kSyncPromoStartupCount);
84}
85
86// Returns true if the user has previously skipped the sign in promo.
87bool HasUserSkippedPromo(Profile* profile) {
88  return profile->GetPrefs()->GetBoolean(prefs::kSyncPromoUserSkipped);
89}
90
91}  // namespace
92
93namespace signin {
94
95bool ShouldShowPromo(Profile* profile) {
96#if defined(OS_CHROMEOS)
97  // There's no need to show the sign in promo on cros since cros users are
98  // already logged in.
99  return false;
100#else
101
102  // Don't bother if we don't have any kind of network connection.
103  if (net::NetworkChangeNotifier::IsOffline())
104    return false;
105
106  // Don't show for managed profiles.
107  if (profile->IsManaged())
108    return false;
109
110  // Display the signin promo if the user is not signed in.
111  SigninManager* signin = SigninManagerFactory::GetForProfile(
112      profile->GetOriginalProfile());
113  return !signin->AuthInProgress() && signin->IsSigninAllowed() &&
114      signin->GetAuthenticatedUsername().empty();
115#endif
116}
117
118bool ShouldShowPromoAtStartup(Profile* profile, bool is_new_profile) {
119  DCHECK(profile);
120
121  // Don't show if the profile is an incognito.
122  if (profile->IsOffTheRecord())
123    return false;
124
125  if (!ShouldShowPromo(profile))
126    return false;
127
128  if (!is_new_profile) {
129    if (!HasShownPromoAtStartup(profile))
130      return false;
131  }
132
133  if (HasUserSkippedPromo(profile))
134    return false;
135
136  // For Chinese users skip the sign in promo.
137  if (g_browser_process->GetApplicationLocale() == "zh-CN")
138    return false;
139
140  PrefService* prefs = profile->GetPrefs();
141  int show_count = prefs->GetInteger(prefs::kSyncPromoStartupCount);
142  if (show_count >= kSignInPromoShowAtStartupMaximum)
143    return false;
144
145  // This pref can be set in the master preferences file to allow or disallow
146  // showing the sign in promo at startup.
147  if (prefs->HasPrefPath(prefs::kSyncPromoShowOnFirstRunAllowed))
148    return prefs->GetBoolean(prefs::kSyncPromoShowOnFirstRunAllowed);
149
150  // For now don't show the promo for some brands.
151  if (!AllowPromoAtStartupForCurrentBrand())
152    return false;
153
154  // Default to show the promo for Google Chrome builds.
155#if defined(GOOGLE_CHROME_BUILD)
156  return true;
157#else
158  return false;
159#endif
160}
161
162void DidShowPromoAtStartup(Profile* profile) {
163  int show_count = profile->GetPrefs()->GetInteger(
164      prefs::kSyncPromoStartupCount);
165  show_count++;
166  profile->GetPrefs()->SetInteger(prefs::kSyncPromoStartupCount, show_count);
167}
168
169void SetUserSkippedPromo(Profile* profile) {
170  profile->GetPrefs()->SetBoolean(prefs::kSyncPromoUserSkipped, true);
171}
172
173GURL GetLandingURL(const char* option, int value) {
174  const std::string& locale = g_browser_process->GetApplicationLocale();
175  std::string url = base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str());
176  base::StringAppendF(&url, "?%s=%d", option, value);
177  return GURL(url);
178}
179
180GURL GetPromoURL(Source source, bool auto_close) {
181  DCHECK_NE(SOURCE_UNKNOWN, source);
182
183  std::string url_string;
184
185  // Build a Gaia-based URL that can be used to sign the user into chrome.
186  // There are required request parameters:
187  //
188  //  - tell Gaia which service the user is signing into.  In this case,
189  //    a chrome sign in uses the service "chromiumsync"
190  //  - provide a continue URL.  This is the URL that Gaia will redirect to
191  //    once the sign is complete.
192  //
193  // The continue URL includes a source parameter that can be extracted using
194  // the function GetSourceForSignInPromoURL() below.  This is used to know
195  // which of the chrome sign in access points was used to sign the user in.
196  // It is also parsed for the |auto_close| flag, which indicates that the tab
197  // must be closed after sync setup is successful.
198  // See OneClickSigninHelper for details.
199  url_string = GaiaUrls::GetInstance()->service_login_url();
200  url_string.append("?service=chromiumsync&sarp=1");
201
202  std::string continue_url = GetLandingURL(kSignInPromoQueryKeySource,
203                                           static_cast<int>(source)).spec();
204  if (auto_close)
205    base::StringAppendF(&continue_url, "&%s=1", kSignInPromoQueryKeyAutoClose);
206
207  base::StringAppendF(&url_string, "&%s=%s", kSignInPromoQueryKeyContinue,
208                      net::EscapeQueryParamValue(
209                          continue_url, false).c_str());
210
211  return GURL(url_string);
212}
213
214GURL GetNextPageURLForPromoURL(const GURL& url) {
215  std::string value;
216  if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyContinue, &value))
217    return GURL(value);
218
219  return GURL();
220}
221
222Source GetSourceForPromoURL(const GURL& url) {
223  std::string value;
224  if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeySource, &value)) {
225    int source = 0;
226    if (base::StringToInt(value, &source) && source >= SOURCE_START_PAGE &&
227        source < SOURCE_UNKNOWN) {
228      return static_cast<Source>(source);
229    }
230  }
231  return SOURCE_UNKNOWN;
232}
233
234bool IsAutoCloseEnabledInURL(const GURL& url) {
235  std::string value;
236  if (net::GetValueForKeyInQuery(url, kSignInPromoQueryKeyAutoClose, &value)) {
237    int enabled = 0;
238    if (base::StringToInt(value, &enabled) && enabled == 1)
239      return true;
240  }
241  return false;
242}
243
244bool IsContinueUrlForWebBasedSigninFlow(const GURL& url) {
245  GURL::Replacements replacements;
246  replacements.ClearQuery();
247  const std::string& locale = g_browser_process->GetApplicationLocale();
248  return url.ReplaceComponents(replacements) ==
249      GURL(base::StringPrintf(kSignInLandingUrlPrefix, locale.c_str()));
250}
251
252void ForceWebBasedSigninFlowForTesting(bool force) {
253  g_force_web_based_signin_flow = force;
254}
255
256void RegisterProfilePrefs(
257    user_prefs::PrefRegistrySyncable* registry) {
258  // TODO(fdoray): Rename these preferences to start with kSignInPromo.
259  // (crbug.com/264283)
260  registry->RegisterIntegerPref(
261      prefs::kSyncPromoStartupCount,
262      0,
263      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
264  registry->RegisterBooleanPref(
265      prefs::kSyncPromoUserSkipped,
266      false,
267      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
268  registry->RegisterBooleanPref(
269      prefs::kSyncPromoShowOnFirstRunAllowed,
270      true,
271      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
272  registry->RegisterBooleanPref(
273      prefs::kSyncPromoShowNTPBubble,
274      false,
275      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
276  registry->RegisterStringPref(
277      prefs::kSyncPromoErrorMessage,
278      std::string(),
279      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
280}
281
282}  // namespace signin
283