blob: 83251fbb5d6d757ed47702baab52a593361ba641 [file] [log] [blame]
tmartinoe3f9c38e2016-09-13 14:05:321// Copyright 2016 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
Scott Chen532ec2c2018-09-26 21:50:055#include "chrome/browser/ui/webui/welcome/welcome_ui.h"
tmartinoe3f9c38e2016-09-13 14:05:326
Scott Chenf68c9b22018-10-12 02:15:257#include "base/metrics/histogram_macros.h"
Hector Carmonac98557a992018-06-27 17:51:438#include "build/build_config.h"
David Rogerf53a0752018-06-18 14:46:489#include "chrome/browser/signin/account_consistency_mode_manager.h"
Dan Beam18cd5972019-02-07 03:54:2910#include "chrome/browser/ui/webui/dark_mode_handler.h"
Lei Zhang0a1c1c72018-12-13 20:41:3311#include "chrome/browser/ui/webui/localized_string.h"
Hector Carmona207744f2018-10-25 23:49:5412#include "chrome/browser/ui/webui/welcome/nux/bookmark_handler.h"
Scott Chenf68c9b22018-10-12 02:15:2513#include "chrome/browser/ui/webui/welcome/nux/constants.h"
14#include "chrome/browser/ui/webui/welcome/nux/email_handler.h"
15#include "chrome/browser/ui/webui/welcome/nux/google_apps_handler.h"
John Leeeba59092019-02-05 23:44:5816#include "chrome/browser/ui/webui/welcome/nux/ntp_background_handler.h"
Scott Chenf68c9b22018-10-12 02:15:2517#include "chrome/browser/ui/webui/welcome/nux/set_as_default_handler.h"
Scott Chenc74ed762018-09-21 00:23:1618#include "chrome/browser/ui/webui/welcome/nux_helper.h"
Scott Chen532ec2c2018-09-26 21:50:0519#include "chrome/browser/ui/webui/welcome/welcome_handler.h"
tmartino6254f472016-11-21 01:22:5620#include "chrome/common/pref_names.h"
tmartinoe3f9c38e2016-09-13 14:05:3221#include "chrome/grit/browser_resources.h"
22#include "chrome/grit/chrome_unscaled_resources.h"
23#include "chrome/grit/chromium_strings.h"
24#include "chrome/grit/generated_resources.h"
dpapadb1f8e9a2018-10-18 21:36:0825#include "chrome/grit/onboarding_welcome_resources.h"
26#include "chrome/grit/onboarding_welcome_resources_map.h"
tmartino6254f472016-11-21 01:22:5627#include "components/prefs/pref_service.h"
Scott Chen395516b2018-10-23 00:05:4828#include "components/strings/grit/components_strings.h"
Scott Chenf68c9b22018-10-12 02:15:2529#include "content/public/browser/web_contents.h"
tmartino7f0818b2016-09-27 23:39:0730#include "net/base/url_util.h"
tmartinoe3f9c38e2016-09-13 14:05:3231#include "ui/base/l10n/l10n_util.h"
tmartinoe3f9c38e2016-09-13 14:05:3232
Scott Chene8414882018-10-30 03:14:3533#if defined(OS_WIN)
34#include "base/win/windows_version.h"
35#endif
36
Scott Chenc6706672017-11-17 17:40:2737namespace {
Scott Chen532ec2c2018-09-26 21:50:0538const bool kIsBranded =
Scott Chenc6706672017-11-17 17:40:2739#if defined(GOOGLE_CHROME_BUILD)
Scott Chen532ec2c2018-09-26 21:50:0540 true;
Scott Chenc6706672017-11-17 17:40:2741#else
Scott Chen532ec2c2018-09-26 21:50:0542 false;
Scott Chenc6706672017-11-17 17:40:2743#endif
Scott Chen532ec2c2018-09-26 21:50:0544} // namespace
Scott Chenc6706672017-11-17 17:40:2745
John Lee6eb43ae2019-02-28 21:25:2646bool HandleRequestCallback(
47 base::WeakPtr<WelcomeUI> weak_ptr,
48 const std::string& path,
49 const content::WebUIDataSource::GotDataCallback& callback) {
50 if (!base::StartsWith(path, "preview-background.jpg",
51 base::CompareCase::SENSITIVE)) {
52 return false;
53 }
54
55 std::string index_param = path.substr(path.find_first_of("?") + 1);
56 int background_index = -1;
57 if (!base::StringToInt(index_param, &background_index) ||
58 background_index < 0) {
59 return false;
60 }
61
62 if (weak_ptr) {
63 weak_ptr->CreateBackgroundFetcher(background_index, callback);
64 return true;
65 }
66
67 return false;
68}
69
Scott Chen395516b2018-10-23 00:05:4870void AddOnboardingStrings(content::WebUIDataSource* html_source) {
71 static constexpr LocalizedString kLocalizedStrings[] = {
72 // Shared strings.
Scott Chen395516b2018-10-23 00:05:4873 {"acceptText", IDS_WELCOME_ACCEPT_BUTTON},
Scott Chen395516b2018-10-23 00:05:4874 {"bookmarkAdded", IDS_ONBOARDING_WELCOME_BOOKMARK_ADDED},
Hector Carmona6d779282018-10-30 23:48:4075 {"bookmarksAdded", IDS_ONBOARDING_WELCOME_BOOKMARKS_ADDED},
Scott Chen395516b2018-10-23 00:05:4876 {"bookmarkRemoved", IDS_ONBOARDING_WELCOME_BOOKMARK_REMOVED},
Hector Carmona6d779282018-10-30 23:48:4077 {"bookmarksRemoved", IDS_ONBOARDING_WELCOME_BOOKMARKS_REMOVED},
Scott Chen395516b2018-10-23 00:05:4878 {"bookmarkReplaced", IDS_ONBOARDING_WELCOME_BOOKMARK_REPLACED},
Scott Chenebfdce732018-10-26 02:34:2679 {"getStarted", IDS_ONBOARDING_WELCOME_GET_STARTED},
80 {"headerText", IDS_WELCOME_HEADER},
81 {"next", IDS_ONBOARDING_WELCOME_NEXT},
82 {"noThanks", IDS_NO_THANKS},
83 {"skip", IDS_ONBOARDING_WELCOME_SKIP},
Scott Chen395516b2018-10-23 00:05:4884
85 // Sign-in view strings.
86 {"signInHeader", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_HEADER},
87 {"signInSubHeader", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_SUB_HEADER},
88 {"signIn", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_SIGNIN},
89
90 // Email provider module strings.
Scott Chenebfdce732018-10-26 02:34:2691 {"emailProviderTitle", IDS_ONBOARDING_WELCOME_NUX_EMAIL_TITLE},
Scott Chen395516b2018-10-23 00:05:4892
93 // Google apps module strings.
94 {"googleAppsDescription",
95 IDS_ONBOARDING_WELCOME_NUX_GOOGLE_APPS_DESCRIPTION},
96
John Lee0c3ede532019-01-30 02:58:5097 // New Tab Page background module strings.
98 {"ntpBackgroundDescription",
99 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_DESCRIPTION},
John Leecea68d42019-02-12 20:02:57100 {"ntpBackgroundDefault",
101 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_DEFAULT_TITLE},
John Lee10f19872019-03-11 21:28:50102 {"ntpBackgroundPreviewUpdated",
103 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_PREVIEW_UPDATED},
John Leeb5d871e2019-03-12 03:30:18104 {"ntpBackgroundReset", IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_RESET},
John Lee0c3ede532019-01-30 02:58:50105
Scott Chen395516b2018-10-23 00:05:48106 // Set as default module strings.
107 {"setDefaultHeader", IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_HEADER},
108 {"setDefaultSubHeader",
109 IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SUB_HEADER},
110 {"setDefaultSkip", IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SKIP},
111 {"setDefaultConfirm",
112 IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SET_AS_DEFAULT},
Scott Chen618035d2018-10-24 00:48:15113
114 // Landing view strings.
115 {"landingTitle", IDS_ONBOARDING_WELCOME_LANDING_TITLE},
116 {"landingDescription", IDS_ONBOARDING_WELCOME_LANDING_DESCRIPTION},
117 {"landingNewUser", IDS_ONBOARDING_WELCOME_LANDING_NEW_USER},
118 {"landingExistingUser", IDS_ONBOARDING_WELCOME_LANDING_EXISTING_USER},
119
120 // Email interstitial strings.
121 {"emailInterstitialTitle",
122 IDS_ONBOARDING_WELCOME_EMAIL_INTERSTITIAL_TITLE},
123 {"emailInterstitialContinue",
124 IDS_ONBOARDING_WELCOME_EMAIL_INTERSTITIAL_CONTINUE},
Scott Chen395516b2018-10-23 00:05:48125 };
Lei Zhang0a1c1c72018-12-13 20:41:33126 AddLocalizedStringsBulk(html_source, kLocalizedStrings,
127 base::size(kLocalizedStrings));
Scott Chen395516b2018-10-23 00:05:48128}
129
tmartinoe3f9c38e2016-09-13 14:05:32130WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
John Lee6eb43ae2019-02-28 21:25:26131 : content::WebUIController(web_ui), weak_ptr_factory_(this) {
tmartino7f0818b2016-09-27 23:39:07132 Profile* profile = Profile::FromWebUI(web_ui);
133
134 // This page is not shown to incognito or guest profiles. If one should end up
135 // here, we return, causing a 404-like page.
136 if (!profile ||
137 profile->GetProfileType() != Profile::ProfileType::REGULAR_PROFILE) {
138 return;
139 }
140
Scott Chenf68c9b22018-10-12 02:15:25141 StorePageSeen(profile);
tmartino6254f472016-11-21 01:22:56142
Jinho Bang7ab7a4e2018-01-15 14:36:07143 web_ui->AddMessageHandler(std::make_unique<WelcomeHandler>(web_ui));
tmartinoe3f9c38e2016-09-13 14:05:32144
145 content::WebUIDataSource* html_source =
146 content::WebUIDataSource::Create(url.host());
147
Dan Beam18cd5972019-02-07 03:54:29148 DarkModeHandler::Initialize(web_ui, html_source);
149
David Rogerf53a0752018-06-18 14:46:48150 bool is_dice =
151 AccountConsistencyModeManager::IsDiceEnabledForProfile(profile);
tmartinoe3f9c38e2016-09-13 14:05:32152
Scott Chenc6706672017-11-17 17:40:27153 // There are multiple possible configurations that affects the layout, but
154 // first add resources that are shared across all layouts.
tmartinoe3f9c38e2016-09-13 14:05:32155 html_source->AddResourcePath("logo.png", IDR_PRODUCT_LOGO_128);
156 html_source->AddResourcePath("logo2x.png", IDR_PRODUCT_LOGO_256);
Scott Chena1264452017-11-14 13:32:08157
Scott Chenf68c9b22018-10-12 02:15:25158 if (nux::IsNuxOnboardingEnabled(profile)) {
Scott Chenc74ed762018-09-21 00:23:16159 // Add Onboarding welcome strings.
Scott Chen395516b2018-10-23 00:05:48160 AddOnboardingStrings(html_source);
Scott Chenc74ed762018-09-21 00:23:16161
dpapadb1f8e9a2018-10-18 21:36:08162 // Add all Onboarding resources.
163 for (size_t i = 0; i < kOnboardingWelcomeResourcesSize; ++i) {
164 html_source->AddResourcePath(kOnboardingWelcomeResources[i].name,
165 kOnboardingWelcomeResources[i].value);
166 }
Scott Chenaac2e182018-10-23 01:49:44167
168 // chrome://welcome
Scott Chenc74ed762018-09-21 00:23:16169 html_source->SetDefaultResource(
170 IDR_WELCOME_ONBOARDING_WELCOME_WELCOME_HTML);
Scott Chenf68c9b22018-10-12 02:15:25171
Scott Chenaac2e182018-10-23 01:49:44172 // chrome://welcome/email-interstitial
173 html_source->AddResourcePath(
174 "email-interstitial",
175 IDR_WELCOME_ONBOARDING_WELCOME_EMAIL_INTERSTITIAL_HTML);
176
Scott Chene8414882018-10-30 03:14:35177#if defined(OS_WIN)
178 html_source->AddBoolean(
179 "is_win10", base::win::GetVersion() >= base::win::VERSION_WIN10);
180#endif
181
Hector Carmona207744f2018-10-25 23:49:54182 // Add the shared bookmark handler for onboarding modules.
183 web_ui->AddMessageHandler(
184 std::make_unique<nux::BookmarkHandler>(profile->GetPrefs()));
Hector Carmona207744f2018-10-25 23:49:54185
Scott Chenf68c9b22018-10-12 02:15:25186 // Add email provider bookmarking onboarding module.
Scott Chen3bb4c578362018-10-31 00:23:31187 web_ui->AddMessageHandler(std::make_unique<nux::EmailHandler>());
Hector Carmona207744f2018-10-25 23:49:54188 nux::EmailHandler::AddSources(html_source);
Scott Chenf68c9b22018-10-12 02:15:25189
190 // Add google apps bookmarking onboarding module.
Scott Chen3bb4c578362018-10-31 00:23:31191 web_ui->AddMessageHandler(std::make_unique<nux::GoogleAppsHandler>());
Scott Chenf68c9b22018-10-12 02:15:25192
John Leeeba59092019-02-05 23:44:58193 // Add NTP custom background onboarding module.
194 web_ui->AddMessageHandler(std::make_unique<nux::NtpBackgroundHandler>());
195
Scott Chenf68c9b22018-10-12 02:15:25196 // Add set-as-default onboarding module.
197 web_ui->AddMessageHandler(std::make_unique<nux::SetAsDefaultHandler>());
Scott Chen00858242018-11-01 23:40:47198
199 html_source->AddString(
Scott Chen7243c1a2018-11-06 01:33:41200 "newUserModules",
Scott Chen00858242018-11-01 23:40:47201 nux::GetNuxOnboardingModules(profile).FindKey("new-user")->GetString());
Scott Chen7243c1a2018-11-06 01:33:41202 html_source->AddString("returningUserModules",
Scott Chen00858242018-11-01 23:40:47203 nux::GetNuxOnboardingModules(profile)
204 .FindKey("returning-user")
205 ->GetString());
Scott Chen7243c1a2018-11-06 01:33:41206 html_source->AddBoolean("showEmailInterstitial",
207 nux::GetNuxOnboardingModules(profile)
208 .FindKey("show-email-interstitial")
209 ->GetBool());
John Lee6eb43ae2019-02-28 21:25:26210 html_source->SetRequestFilter(base::BindRepeating(
211 &HandleRequestCallback, weak_ptr_factory_.GetWeakPtr()));
Scott Chenc74ed762018-09-21 00:23:16212 } else if (kIsBranded && is_dice) {
213 // Use special layout if the application is branded and DICE is enabled.
Scott Chenc6706672017-11-17 17:40:27214 html_source->AddLocalizedString("headerText", IDS_WELCOME_HEADER);
Thomas Tangl441b06782018-09-27 19:37:07215 html_source->AddLocalizedString("acceptText",
216 IDS_PROFILES_DICE_SIGNIN_BUTTON);
Scott Chenc6706672017-11-17 17:40:27217 html_source->AddLocalizedString("secondHeaderText",
218 IDS_DICE_WELCOME_SECOND_HEADER);
219 html_source->AddLocalizedString("descriptionText",
220 IDS_DICE_WELCOME_DESCRIPTION);
221 html_source->AddLocalizedString("declineText",
222 IDS_DICE_WELCOME_DECLINE_BUTTON);
Mihai Sardarescu631f3b472018-05-04 15:08:15223 html_source->AddResourcePath("welcome_browser_proxy.html",
224 IDR_DICE_WELCOME_BROWSER_PROXY_HTML);
225 html_source->AddResourcePath("welcome_browser_proxy.js",
226 IDR_DICE_WELCOME_BROWSER_PROXY_JS);
Scott Chenc6706672017-11-17 17:40:27227 html_source->AddResourcePath("welcome_app.html", IDR_DICE_WELCOME_APP_HTML);
228 html_source->AddResourcePath("welcome_app.js", IDR_DICE_WELCOME_APP_JS);
Scott Chena1264452017-11-14 13:32:08229 html_source->AddResourcePath("welcome.css", IDR_DICE_WELCOME_CSS);
230 html_source->SetDefaultResource(IDR_DICE_WELCOME_HTML);
231 } else {
Mihai Sardarescu631f3b472018-05-04 15:08:15232 // Use default layout for non-DICE or unbranded build.
233 std::string value;
234 bool is_everywhere_variant =
235 (net::GetValueForKeyInQuery(url, "variant", &value) &&
236 value == "everywhere");
237
Scott Chenc6706672017-11-17 17:40:27238 if (kIsBranded) {
239 base::string16 subheader =
240 is_everywhere_variant
241 ? base::string16()
242 : l10n_util::GetStringUTF16(IDS_WELCOME_SUBHEADER);
243 html_source->AddString("subheaderText", subheader);
244 }
245
246 int header_id = is_everywhere_variant ? IDS_WELCOME_HEADER_AFTER_FIRST_RUN
247 : IDS_WELCOME_HEADER;
248 html_source->AddString("headerText", l10n_util::GetStringUTF16(header_id));
Thomas Tangl441b06782018-09-27 19:37:07249 html_source->AddLocalizedString("acceptText", IDS_WELCOME_ACCEPT_BUTTON);
Scott Chenc6706672017-11-17 17:40:27250 html_source->AddLocalizedString("descriptionText", IDS_WELCOME_DESCRIPTION);
251 html_source->AddLocalizedString("declineText", IDS_WELCOME_DECLINE_BUTTON);
Scott Chena1264452017-11-14 13:32:08252 html_source->AddResourcePath("welcome.js", IDR_WELCOME_JS);
253 html_source->AddResourcePath("welcome.css", IDR_WELCOME_CSS);
254 html_source->SetDefaultResource(IDR_WELCOME_HTML);
255 }
tmartinoe3f9c38e2016-09-13 14:05:32256
tmartinoe3f9c38e2016-09-13 14:05:32257 content::WebUIDataSource::Add(profile, html_source);
258}
259
260WelcomeUI::~WelcomeUI() {}
Hector Carmona8f2c3cb2018-07-12 21:07:58261
John Lee6eb43ae2019-02-28 21:25:26262void WelcomeUI::CreateBackgroundFetcher(
263 size_t background_index,
264 const content::WebUIDataSource::GotDataCallback& callback) {
265 background_fetcher_ =
266 std::make_unique<nux::NtpBackgroundFetcher>(background_index, callback);
267}
268
Scott Chenf68c9b22018-10-12 02:15:25269void WelcomeUI::StorePageSeen(Profile* profile) {
Hector Carmona8f2c3cb2018-07-12 21:07:58270 // Store that this profile has been shown the Welcome page.
271 profile->GetPrefs()->SetBoolean(prefs::kHasSeenWelcomePage, true);
272}