blob: 43eb3a9bf2a4cfe0ddbea7fde7c68131ee2b6ccc [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
Dan Beame3e79472019-03-15 01:23:337#include "base/bind.h"
Scott Chenf68c9b22018-10-12 02:15:258#include "base/metrics/histogram_macros.h"
Dan Beame3e79472019-03-15 01:23:339#include "base/stl_util.h"
Hector Carmonac98557a992018-06-27 17:51:4310#include "build/build_config.h"
David Rogerf53a0752018-06-18 14:46:4811#include "chrome/browser/signin/account_consistency_mode_manager.h"
Lei Zhang0a1c1c72018-12-13 20:41:3312#include "chrome/browser/ui/webui/localized_string.h"
Hector Carmona207744f2018-10-25 23:49:5413#include "chrome/browser/ui/webui/welcome/nux/bookmark_handler.h"
Scott Chenf68c9b22018-10-12 02:15:2514#include "chrome/browser/ui/webui/welcome/nux/constants.h"
Scott Chenf68c9b22018-10-12 02:15:2515#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"
Colin Blundell3517170e2019-07-11 08:16:3428#include "components/signin/public/base/signin_pref_names.h"
Scott Chen395516b2018-10-23 00:05:4829#include "components/strings/grit/components_strings.h"
Scott Chenf68c9b22018-10-12 02:15:2530#include "content/public/browser/web_contents.h"
tmartino7f0818b2016-09-27 23:39:0731#include "net/base/url_util.h"
tmartinoe3f9c38e2016-09-13 14:05:3232#include "ui/base/l10n/l10n_util.h"
tmartinoe3f9c38e2016-09-13 14:05:3233
Scott Chene8414882018-10-30 03:14:3534#if defined(OS_WIN)
35#include "base/win/windows_version.h"
36#endif
37
Scott Chenc6706672017-11-17 17:40:2738namespace {
Dan Beama896f662019-03-19 18:26:3939
Scott Chen532ec2c2018-09-26 21:50:0540const bool kIsBranded =
Scott Chenc6706672017-11-17 17:40:2741#if defined(GOOGLE_CHROME_BUILD)
Scott Chen532ec2c2018-09-26 21:50:0542 true;
Scott Chenc6706672017-11-17 17:40:2743#else
Scott Chen532ec2c2018-09-26 21:50:0544 false;
Scott Chenc6706672017-11-17 17:40:2745#endif
Dan Beama896f662019-03-19 18:26:3946
47const char kPreviewBackgroundPath[] = "preview-background.jpg";
Scott Chenc6706672017-11-17 17:40:2748
dpapad28dccc442019-04-09 22:06:4049bool ShouldHandleRequestCallback(base::WeakPtr<WelcomeUI> weak_ptr,
50 const std::string& path) {
Dan Beama896f662019-03-19 18:26:3951 if (!base::StartsWith(path, kPreviewBackgroundPath,
John Lee6eb43ae2019-02-28 21:25:2652 base::CompareCase::SENSITIVE)) {
53 return false;
54 }
55
56 std::string index_param = path.substr(path.find_first_of("?") + 1);
57 int background_index = -1;
58 if (!base::StringToInt(index_param, &background_index) ||
59 background_index < 0) {
60 return false;
61 }
62
dpapad28dccc442019-04-09 22:06:4063 return !weak_ptr ? false : true;
64}
John Lee6eb43ae2019-02-28 21:25:2665
dpapad28dccc442019-04-09 22:06:4066void HandleRequestCallback(
67 base::WeakPtr<WelcomeUI> weak_ptr,
68 const std::string& path,
69 const content::WebUIDataSource::GotDataCallback& callback) {
70 DCHECK(ShouldHandleRequestCallback(weak_ptr, path));
71
72 std::string index_param = path.substr(path.find_first_of("?") + 1);
73 int background_index = -1;
74 CHECK(base::StringToInt(index_param, &background_index) ||
75 background_index < 0);
76
77 DCHECK(weak_ptr);
78 weak_ptr->CreateBackgroundFetcher(background_index, callback);
John Lee6eb43ae2019-02-28 21:25:2679}
80
Scott Chen395516b2018-10-23 00:05:4881void AddOnboardingStrings(content::WebUIDataSource* html_source) {
82 static constexpr LocalizedString kLocalizedStrings[] = {
83 // Shared strings.
Scott Chen395516b2018-10-23 00:05:4884 {"acceptText", IDS_WELCOME_ACCEPT_BUTTON},
Scott Chen395516b2018-10-23 00:05:4885 {"bookmarkAdded", IDS_ONBOARDING_WELCOME_BOOKMARK_ADDED},
Hector Carmona6d779282018-10-30 23:48:4086 {"bookmarksAdded", IDS_ONBOARDING_WELCOME_BOOKMARKS_ADDED},
Scott Chen395516b2018-10-23 00:05:4887 {"bookmarkRemoved", IDS_ONBOARDING_WELCOME_BOOKMARK_REMOVED},
Hector Carmona6d779282018-10-30 23:48:4088 {"bookmarksRemoved", IDS_ONBOARDING_WELCOME_BOOKMARKS_REMOVED},
Scott Chen5524784e2019-04-02 02:54:5889 {"defaultBrowserChanged", IDS_ONBOARDING_DEFAULT_BROWSER_CHANGED},
Scott Chenebfdce732018-10-26 02:34:2690 {"getStarted", IDS_ONBOARDING_WELCOME_GET_STARTED},
91 {"headerText", IDS_WELCOME_HEADER},
92 {"next", IDS_ONBOARDING_WELCOME_NEXT},
93 {"noThanks", IDS_NO_THANKS},
94 {"skip", IDS_ONBOARDING_WELCOME_SKIP},
Scott Chen395516b2018-10-23 00:05:4895
96 // Sign-in view strings.
97 {"signInHeader", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_HEADER},
98 {"signInSubHeader", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_SUB_HEADER},
99 {"signIn", IDS_ONBOARDING_WELCOME_SIGNIN_VIEW_SIGNIN},
100
Scott Chen395516b2018-10-23 00:05:48101 // Google apps module strings.
102 {"googleAppsDescription",
103 IDS_ONBOARDING_WELCOME_NUX_GOOGLE_APPS_DESCRIPTION},
104
John Lee0c3ede532019-01-30 02:58:50105 // New Tab Page background module strings.
106 {"ntpBackgroundDescription",
107 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_DESCRIPTION},
John Leecea68d42019-02-12 20:02:57108 {"ntpBackgroundDefault",
109 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_DEFAULT_TITLE},
John Lee10f19872019-03-11 21:28:50110 {"ntpBackgroundPreviewUpdated",
111 IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_PREVIEW_UPDATED},
John Leeb5d871e2019-03-12 03:30:18112 {"ntpBackgroundReset", IDS_ONBOARDING_WELCOME_NTP_BACKGROUND_RESET},
John Lee0c3ede532019-01-30 02:58:50113
Scott Chen395516b2018-10-23 00:05:48114 // Set as default module strings.
115 {"setDefaultHeader", IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_HEADER},
116 {"setDefaultSubHeader",
117 IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SUB_HEADER},
118 {"setDefaultSkip", IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SKIP},
119 {"setDefaultConfirm",
120 IDS_ONBOARDING_WELCOME_NUX_SET_AS_DEFAULT_SET_AS_DEFAULT},
Scott Chen618035d2018-10-24 00:48:15121
122 // Landing view strings.
123 {"landingTitle", IDS_ONBOARDING_WELCOME_LANDING_TITLE},
124 {"landingDescription", IDS_ONBOARDING_WELCOME_LANDING_DESCRIPTION},
125 {"landingNewUser", IDS_ONBOARDING_WELCOME_LANDING_NEW_USER},
126 {"landingExistingUser", IDS_ONBOARDING_WELCOME_LANDING_EXISTING_USER},
Scott Chen395516b2018-10-23 00:05:48127 };
Lei Zhang0a1c1c72018-12-13 20:41:33128 AddLocalizedStringsBulk(html_source, kLocalizedStrings,
129 base::size(kLocalizedStrings));
Scott Chen395516b2018-10-23 00:05:48130}
131
Dan Beama896f662019-03-19 18:26:39132} // namespace
133
tmartinoe3f9c38e2016-09-13 14:05:32134WelcomeUI::WelcomeUI(content::WebUI* web_ui, const GURL& url)
John Lee6eb43ae2019-02-28 21:25:26135 : content::WebUIController(web_ui), weak_ptr_factory_(this) {
tmartino7f0818b2016-09-27 23:39:07136 Profile* profile = Profile::FromWebUI(web_ui);
137
138 // This page is not shown to incognito or guest profiles. If one should end up
139 // here, we return, causing a 404-like page.
Ramin Halavati85ce9272019-05-14 10:29:30140 if (!profile || !profile->IsRegularProfile()) {
tmartino7f0818b2016-09-27 23:39:07141 return;
142 }
143
Scott Chenf68c9b22018-10-12 02:15:25144 StorePageSeen(profile);
tmartino6254f472016-11-21 01:22:56145
Jinho Bang7ab7a4e2018-01-15 14:36:07146 web_ui->AddMessageHandler(std::make_unique<WelcomeHandler>(web_ui));
tmartinoe3f9c38e2016-09-13 14:05:32147
148 content::WebUIDataSource* html_source =
149 content::WebUIDataSource::Create(url.host());
150
Scott Chenc6706672017-11-17 17:40:27151 // There are multiple possible configurations that affects the layout, but
152 // first add resources that are shared across all layouts.
tmartinoe3f9c38e2016-09-13 14:05:32153 html_source->AddResourcePath("logo.png", IDR_PRODUCT_LOGO_128);
154 html_source->AddResourcePath("logo2x.png", IDR_PRODUCT_LOGO_256);
Scott Chena1264452017-11-14 13:32:08155
Dan Beama896f662019-03-19 18:26:39156 if (nux::IsNuxOnboardingEnabled(profile)) {
Scott Chenc74ed762018-09-21 00:23:16157 // Add Onboarding welcome strings.
Scott Chen395516b2018-10-23 00:05:48158 AddOnboardingStrings(html_source);
Scott Chenc74ed762018-09-21 00:23:16159
dpapadb1f8e9a2018-10-18 21:36:08160 // Add all Onboarding resources.
161 for (size_t i = 0; i < kOnboardingWelcomeResourcesSize; ++i) {
162 html_source->AddResourcePath(kOnboardingWelcomeResources[i].name,
163 kOnboardingWelcomeResources[i].value);
164 }
Scott Chenaac2e182018-10-23 01:49:44165
Hector Carmona1c97020b762019-06-11 16:39:29166#if defined(GOOGLE_CHROME_BUILD)
167 // Load unscaled images.
168 html_source->AddResourcePath("images/module_icons/google_dark.svg",
169 IDR_WELCOME_MODULE_ICONS_GOOGLE_DARK);
170 html_source->AddResourcePath("images/module_icons/google_light.svg",
171 IDR_WELCOME_MODULE_ICONS_GOOGLE_LIGHT);
172 html_source->AddResourcePath("images/module_icons/set_default_dark.svg",
173 IDR_WELCOME_MODULE_ICONS_SET_DEFAULT_DARK);
174 html_source->AddResourcePath("images/module_icons/set_default_light.svg",
175 IDR_WELCOME_MODULE_ICONS_SET_DEFAULT_LIGHT);
176 html_source->AddResourcePath("images/module_icons/wallpaper_dark.svg",
177 IDR_WELCOME_MODULE_ICONS_WALLPAPER_DARK);
178 html_source->AddResourcePath("images/module_icons/wallpaper_light.svg",
179 IDR_WELCOME_MODULE_ICONS_WALLPAPER_LIGHT);
180 html_source->AddResourcePath("images/ntp_thumbnails/art.jpg",
181 IDR_WELCOME_NTP_THUMBNAILS_ART);
182 html_source->AddResourcePath("images/ntp_thumbnails/cityscape.jpg",
183 IDR_WELCOME_NTP_THUMBNAILS_CITYSCAPE);
184 html_source->AddResourcePath("images/ntp_thumbnails/earth.jpg",
185 IDR_WELCOME_NTP_THUMBNAILS_EARTH);
186 html_source->AddResourcePath("images/ntp_thumbnails/geometric_shapes.jpg",
187 IDR_WELCOME_NTP_THUMBNAILS_GEOMETRIC_SHAPES);
188 html_source->AddResourcePath("images/ntp_thumbnails/landscape.jpg",
189 IDR_WELCOME_NTP_THUMBNAILS_LANDSCAPE);
190 html_source->AddResourcePath("images/set_default_dark.svg",
191 IDR_WELCOME_SET_DEFAULT_DARK);
192 html_source->AddResourcePath("images/set_default_light.svg",
193 IDR_WELCOME_SET_DEFAULT_LIGHT);
194#endif // defined(GOOGLE_CHROME_BUILD)
195
Scott Chenaac2e182018-10-23 01:49:44196 // chrome://welcome
Scott Chenc74ed762018-09-21 00:23:16197 html_source->SetDefaultResource(
198 IDR_WELCOME_ONBOARDING_WELCOME_WELCOME_HTML);
Scott Chenf68c9b22018-10-12 02:15:25199
Scott Chene8414882018-10-30 03:14:35200#if defined(OS_WIN)
201 html_source->AddBoolean(
Bruce Dawsonaed9bea2019-04-20 02:30:09202 "is_win10", base::win::GetVersion() >= base::win::Version::WIN10);
Scott Chene8414882018-10-30 03:14:35203#endif
204
Hector Carmona207744f2018-10-25 23:49:54205 // Add the shared bookmark handler for onboarding modules.
206 web_ui->AddMessageHandler(
207 std::make_unique<nux::BookmarkHandler>(profile->GetPrefs()));
Hector Carmona207744f2018-10-25 23:49:54208
Scott Chenf68c9b22018-10-12 02:15:25209 // Add google apps bookmarking onboarding module.
Scott Chen3bb4c578362018-10-31 00:23:31210 web_ui->AddMessageHandler(std::make_unique<nux::GoogleAppsHandler>());
Scott Chenf68c9b22018-10-12 02:15:25211
John Leeeba59092019-02-05 23:44:58212 // Add NTP custom background onboarding module.
213 web_ui->AddMessageHandler(std::make_unique<nux::NtpBackgroundHandler>());
214
Scott Chenf68c9b22018-10-12 02:15:25215 // Add set-as-default onboarding module.
216 web_ui->AddMessageHandler(std::make_unique<nux::SetAsDefaultHandler>());
Scott Chen00858242018-11-01 23:40:47217
218 html_source->AddString(
Scott Chen7243c1a2018-11-06 01:33:41219 "newUserModules",
Scott Chen00858242018-11-01 23:40:47220 nux::GetNuxOnboardingModules(profile).FindKey("new-user")->GetString());
Scott Chen7243c1a2018-11-06 01:33:41221 html_source->AddString("returningUserModules",
Scott Chen00858242018-11-01 23:40:47222 nux::GetNuxOnboardingModules(profile)
223 .FindKey("returning-user")
224 ->GetString());
Hector Carmona7f52fab2019-04-18 23:31:22225 html_source->AddBoolean("signinAllowed", profile->GetPrefs()->GetBoolean(
226 prefs::kSigninAllowed));
dpapad28dccc442019-04-09 22:06:40227 html_source->SetRequestFilter(
228 base::BindRepeating(&ShouldHandleRequestCallback,
229 weak_ptr_factory_.GetWeakPtr()),
230 base::BindRepeating(&HandleRequestCallback,
231 weak_ptr_factory_.GetWeakPtr()));
Hector Carmonae3b2605c2019-04-11 22:28:35232 html_source->SetJsonPath("strings.js");
Dan Beama896f662019-03-19 18:26:39233 } else if (kIsBranded &&
234 AccountConsistencyModeManager::IsDiceEnabledForProfile(profile)) {
Scott Chenc74ed762018-09-21 00:23:16235 // Use special layout if the application is branded and DICE is enabled.
Scott Chenc6706672017-11-17 17:40:27236 html_source->AddLocalizedString("headerText", IDS_WELCOME_HEADER);
Thomas Tangl441b06782018-09-27 19:37:07237 html_source->AddLocalizedString("acceptText",
238 IDS_PROFILES_DICE_SIGNIN_BUTTON);
Scott Chenc6706672017-11-17 17:40:27239 html_source->AddLocalizedString("secondHeaderText",
240 IDS_DICE_WELCOME_SECOND_HEADER);
241 html_source->AddLocalizedString("descriptionText",
242 IDS_DICE_WELCOME_DESCRIPTION);
243 html_source->AddLocalizedString("declineText",
244 IDS_DICE_WELCOME_DECLINE_BUTTON);
Mihai Sardarescu631f3b472018-05-04 15:08:15245 html_source->AddResourcePath("welcome_browser_proxy.html",
246 IDR_DICE_WELCOME_BROWSER_PROXY_HTML);
247 html_source->AddResourcePath("welcome_browser_proxy.js",
248 IDR_DICE_WELCOME_BROWSER_PROXY_JS);
Scott Chenc6706672017-11-17 17:40:27249 html_source->AddResourcePath("welcome_app.html", IDR_DICE_WELCOME_APP_HTML);
250 html_source->AddResourcePath("welcome_app.js", IDR_DICE_WELCOME_APP_JS);
Scott Chena1264452017-11-14 13:32:08251 html_source->AddResourcePath("welcome.css", IDR_DICE_WELCOME_CSS);
252 html_source->SetDefaultResource(IDR_DICE_WELCOME_HTML);
253 } else {
Mihai Sardarescu631f3b472018-05-04 15:08:15254 // Use default layout for non-DICE or unbranded build.
255 std::string value;
256 bool is_everywhere_variant =
257 (net::GetValueForKeyInQuery(url, "variant", &value) &&
258 value == "everywhere");
259
Scott Chenc6706672017-11-17 17:40:27260 if (kIsBranded) {
261 base::string16 subheader =
262 is_everywhere_variant
263 ? base::string16()
264 : l10n_util::GetStringUTF16(IDS_WELCOME_SUBHEADER);
265 html_source->AddString("subheaderText", subheader);
266 }
267
268 int header_id = is_everywhere_variant ? IDS_WELCOME_HEADER_AFTER_FIRST_RUN
269 : IDS_WELCOME_HEADER;
270 html_source->AddString("headerText", l10n_util::GetStringUTF16(header_id));
Thomas Tangl441b06782018-09-27 19:37:07271 html_source->AddLocalizedString("acceptText", IDS_WELCOME_ACCEPT_BUTTON);
Scott Chenc6706672017-11-17 17:40:27272 html_source->AddLocalizedString("descriptionText", IDS_WELCOME_DESCRIPTION);
273 html_source->AddLocalizedString("declineText", IDS_WELCOME_DECLINE_BUTTON);
Scott Chena1264452017-11-14 13:32:08274 html_source->AddResourcePath("welcome.js", IDR_WELCOME_JS);
275 html_source->AddResourcePath("welcome.css", IDR_WELCOME_CSS);
276 html_source->SetDefaultResource(IDR_WELCOME_HTML);
277 }
tmartinoe3f9c38e2016-09-13 14:05:32278
tmartinoe3f9c38e2016-09-13 14:05:32279 content::WebUIDataSource::Add(profile, html_source);
280}
281
282WelcomeUI::~WelcomeUI() {}
Hector Carmona8f2c3cb2018-07-12 21:07:58283
John Lee6eb43ae2019-02-28 21:25:26284void WelcomeUI::CreateBackgroundFetcher(
285 size_t background_index,
286 const content::WebUIDataSource::GotDataCallback& callback) {
287 background_fetcher_ =
288 std::make_unique<nux::NtpBackgroundFetcher>(background_index, callback);
289}
290
Scott Chenf68c9b22018-10-12 02:15:25291void WelcomeUI::StorePageSeen(Profile* profile) {
Hector Carmona8f2c3cb2018-07-12 21:07:58292 // Store that this profile has been shown the Welcome page.
293 profile->GetPrefs()->SetBoolean(prefs::kHasSeenWelcomePage, true);
294}