1 // Copyright 2020 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/ui/webui/settings/chromeos/main_section.h"
6 
7 #include "ash/public/cpp/resources/grit/ash_public_unscaled_resources.h"
8 #include "base/feature_list.h"
9 #include "base/i18n/message_formatter.h"
10 #include "base/i18n/number_formatting.h"
11 #include "base/no_destructor.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "chrome/browser/browser_process.h"
14 #include "chrome/browser/browser_process_platform_part.h"
15 #include "chrome/browser/chromeos/policy/browser_policy_connector_chromeos.h"
16 #include "chrome/browser/chromeos/policy/minimum_version_policy_handler.h"
17 #include "chrome/browser/chromeos/profiles/profile_helper.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/ui/webui/metrics_handler.h"
20 #include "chrome/browser/ui/webui/policy_indicator_localized_strings_provider.h"
21 #include "chrome/browser/ui/webui/settings/browser_lifetime_handler.h"
22 #include "chrome/browser/ui/webui/settings/chromeos/os_settings_features_util.h"
23 #include "chrome/browser/ui/webui/webui_util.h"
24 #include "chrome/common/url_constants.h"
25 #include "chrome/common/webui_url_constants.h"
26 #include "chrome/grit/browser_resources.h"
27 #include "chrome/grit/chromium_strings.h"
28 #include "chrome/grit/generated_resources.h"
29 #include "chrome/grit/os_settings_resources.h"
30 #include "chromeos/constants/chromeos_features.h"
31 #include "components/strings/grit/components_strings.h"
32 #include "components/user_manager/user_manager.h"
33 #include "content/public/browser/web_ui_data_source.h"
34 #include "ui/base/l10n/l10n_util.h"
35 #include "ui/base/webui/web_ui_util.h"
36 #include "ui/chromeos/devicetype_utils.h"
37 
38 namespace chromeos {
39 namespace settings {
40 namespace {
41 
AddSearchInSettingsStrings(content::WebUIDataSource * html_source)42 void AddSearchInSettingsStrings(content::WebUIDataSource* html_source) {
43   static constexpr webui::LocalizedString kLocalizedStrings[] = {
44       {"searchPrompt", IDS_SETTINGS_SEARCH_PROMPT},
45       {"searchNoResults", IDS_SEARCH_NO_RESULTS},
46       {"searchResults", IDS_SEARCH_RESULTS},
47       {"searchResultSelected", IDS_OS_SEARCH_RESULT_ROW_A11Y_RESULT_SELECTED},
48       {"clearSearch", IDS_CLEAR_SEARCH},
49   };
50   AddLocalizedStringsBulk(html_source, kLocalizedStrings);
51 
52   html_source->AddString(
53       "searchNoOsResultsHelp",
54       l10n_util::GetStringFUTF16(
55           IDS_SETTINGS_SEARCH_NO_RESULTS_HELP,
56           base::ASCIIToUTF16(chrome::kOsSettingsSearchHelpURL)));
57 
58   // TODO(crbug/1080777): Remove this flag and JS codepaths effected.
59   html_source->AddBoolean("newOsSettingsSearch", true);
60 }
61 
AddUpdateRequiredEolStrings(content::WebUIDataSource * html_source)62 void AddUpdateRequiredEolStrings(content::WebUIDataSource* html_source) {
63   policy::BrowserPolicyConnectorChromeOS* connector =
64       g_browser_process->platform_part()->browser_policy_connector_chromeos();
65   policy::MinimumVersionPolicyHandler* handler =
66       connector->GetMinimumVersionPolicyHandler();
67   bool device_managed = connector->IsEnterpriseManaged();
68 
69   // |eol_return_banner_text| contains the update required end of life banner
70   // text which is left empty when the banner should not be shown.
71   base::string16 eol_return_banner_text;
72   if (device_managed && handler->ShouldShowUpdateRequiredEolBanner()) {
73     base::Optional<int> days = handler->GetTimeRemainingInDays();
74     // We only need to show the banner if less than equal to one week remains to
75     // reach the update required deadline.
76     if (days && days.value() <= 7) {
77       // |days| could have value equal to zero if we are very close to the
78       // deadline.
79       int days_remaining = days.value() ? days.value() : 1;
80       base::string16 domain_name =
81           base::UTF8ToUTF16(connector->GetEnterpriseDisplayDomain());
82       base::string16 link_url =
83           base::UTF8ToUTF16(chrome::kChromeUIManagementURL);
84       if (days_remaining == 7) {
85         eol_return_banner_text = l10n_util::GetStringFUTF16(
86             IDS_SETTINGS_UPDATE_REQUIRED_EOL_BANNER_ONE_WEEK, domain_name,
87             ui::GetChromeOSDeviceName(), link_url);
88       } else {
89         eol_return_banner_text =
90             base::i18n::MessageFormatter::FormatWithNumberedArgs(
91                 l10n_util::GetStringUTF16(
92                     IDS_SETTINGS_UPDATE_REQUIRED_EOL_BANNER_DAYS),
93                 days_remaining,
94                 base::UTF8ToUTF16(connector->GetEnterpriseDisplayDomain()),
95                 ui::GetChromeOSDeviceName(),
96                 base::UTF8ToUTF16(chrome::kChromeUIManagementURL));
97       }
98     }
99   }
100   html_source->AddString("updateRequiredEolBannerText", eol_return_banner_text);
101 }
102 
103 }  // namespace
104 
MainSection(Profile * profile,SearchTagRegistry * search_tag_registry)105 MainSection::MainSection(Profile* profile,
106                          SearchTagRegistry* search_tag_registry)
107     : OsSettingsSection(profile, search_tag_registry) {}
108 
109 MainSection::~MainSection() = default;
110 
AddLoadTimeData(content::WebUIDataSource * html_source)111 void MainSection::AddLoadTimeData(content::WebUIDataSource* html_source) {
112   static constexpr webui::LocalizedString kLocalizedStrings[] = {
113       {"add", IDS_ADD},
114       {"advancedPageTitle", IDS_SETTINGS_ADVANCED},
115       {"back", IDS_ACCNAME_BACK},
116       {"basicPageTitle", IDS_SETTINGS_BASIC},
117       {"cancel", IDS_CANCEL},
118       {"clear", IDS_SETTINGS_CLEAR},
119       {"close", IDS_CLOSE},
120       {"confirm", IDS_CONFIRM},
121       {"continue", IDS_SETTINGS_CONTINUE},
122       {"controlledByExtension", IDS_SETTINGS_CONTROLLED_BY_EXTENSION},
123       {"custom", IDS_SETTINGS_CUSTOM},
124       {"delete", IDS_SETTINGS_DELETE},
125       {"deviceOff", IDS_SETTINGS_DEVICE_OFF},
126       {"deviceOn", IDS_SETTINGS_DEVICE_ON},
127       {"disable", IDS_DISABLE},
128       {"done", IDS_DONE},
129       {"edit", IDS_SETTINGS_EDIT},
130       {"extensionsLinkTooltip", IDS_SETTINGS_MENU_EXTENSIONS_LINK_TOOLTIP},
131       {"learnMore", IDS_LEARN_MORE},
132       {"menu", IDS_MENU},
133       {"menuButtonLabel", IDS_SETTINGS_MENU_BUTTON_LABEL},
134       {"moreActions", IDS_SETTINGS_MORE_ACTIONS},
135       {"ok", IDS_OK},
136       {"restart", IDS_SETTINGS_RESTART},
137       {"save", IDS_SAVE},
138       {"searchResultBubbleText", IDS_SEARCH_RESULT_BUBBLE_TEXT},
139       {"searchResultsBubbleText", IDS_SEARCH_RESULTS_BUBBLE_TEXT},
140       {"settings", IDS_SETTINGS_SETTINGS},
141       {"settingsAltPageTitle", IDS_SETTINGS_ALT_PAGE_TITLE},
142       {"subpageArrowRoleDescription", IDS_SETTINGS_SUBPAGE_BUTTON},
143       {"subpageBackButtonAriaLabel",
144        IDS_SETTINGS_SUBPAGE_BACK_BUTTON_ARIA_LABEL},
145       {"subpageBackButtonAriaRoleDescription",
146        IDS_SETTINGS_SUBPAGE_BACK_BUTTON_ARIA_ROLE_DESCRIPTION},
147       {"notValidWebAddress", IDS_SETTINGS_NOT_VALID_WEB_ADDRESS},
148       {"notValidWebAddressForContentType",
149        IDS_SETTINGS_NOT_VALID_WEB_ADDRESS_FOR_CONTENT_TYPE},
150 
151       // Common font related strings shown in a11y and appearance sections.
152       {"quickBrownFox", IDS_SETTINGS_QUICK_BROWN_FOX},
153       {"verySmall", IDS_SETTINGS_VERY_SMALL_FONT},
154       {"small", IDS_SETTINGS_SMALL_FONT},
155       {"medium", IDS_SETTINGS_MEDIUM_FONT},
156       {"large", IDS_SETTINGS_LARGE_FONT},
157       {"veryLarge", IDS_SETTINGS_VERY_LARGE_FONT},
158   };
159   AddLocalizedStringsBulk(html_source, kLocalizedStrings);
160 
161   // This handler is for chrome://os-settings.
162   html_source->AddBoolean("isOSSettings", true);
163 
164   html_source->AddBoolean("isGuest", features::IsGuestModeActive());
165   html_source->AddBoolean(
166       "isKioskModeActive",
167       user_manager::UserManager::Get()->IsLoggedInAsAnyKioskApp());
168   html_source->AddBoolean("isSupervised", profile()->IsSupervised());
169 
170   html_source->AddBoolean("isDeepLinkingEnabled",
171                           chromeos::features::IsDeepLinkingEnabled());
172 
173   // Add the System Web App resources for Settings.
174   html_source->AddResourcePath("icon-192.png", IDR_SETTINGS_LOGO_192);
175 
176   AddSearchInSettingsStrings(html_source);
177   AddChromeOSUserStrings(html_source);
178   AddUpdateRequiredEolStrings(html_source);
179 
180   policy_indicator::AddLocalizedStrings(html_source);
181 }
182 
AddHandlers(content::WebUI * web_ui)183 void MainSection::AddHandlers(content::WebUI* web_ui) {
184   // Add the metrics handler to write uma stats.
185   web_ui->AddMessageHandler(std::make_unique<MetricsHandler>());
186 
187   web_ui->AddMessageHandler(
188       std::make_unique<::settings::BrowserLifetimeHandler>());
189 }
190 
GetSectionNameMessageId() const191 int MainSection::GetSectionNameMessageId() const {
192   NOTIMPLEMENTED();
193   return 0;
194 }
195 
GetSection() const196 mojom::Section MainSection::GetSection() const {
197   NOTIMPLEMENTED();
198   return mojom::Section::kMinValue;
199 }
200 
GetSectionIcon() const201 mojom::SearchResultIcon MainSection::GetSectionIcon() const {
202   NOTIMPLEMENTED();
203   return mojom::SearchResultIcon::kMinValue;
204 }
205 
GetSectionPath() const206 std::string MainSection::GetSectionPath() const {
207   NOTIMPLEMENTED();
208   return std::string();
209 }
210 
LogMetric(mojom::Setting setting,base::Value & value) const211 bool MainSection::LogMetric(mojom::Setting setting, base::Value& value) const {
212   NOTIMPLEMENTED();
213   return false;
214 }
215 
RegisterHierarchy(HierarchyGenerator * generator) const216 void MainSection::RegisterHierarchy(HierarchyGenerator* generator) const {
217   // MainSection is a container for common resources/functionality shared
218   // between sections and does not have its own subpages/settings.
219   NOTIMPLEMENTED();
220 }
221 
AddChromeOSUserStrings(content::WebUIDataSource * html_source)222 void MainSection::AddChromeOSUserStrings(
223     content::WebUIDataSource* html_source) {
224   const user_manager::User* user =
225       ProfileHelper::Get()->GetUserByProfile(profile());
226   const user_manager::User* primary_user =
227       user_manager::UserManager::Get()->GetPrimaryUser();
228   std::string primary_user_email = primary_user->GetAccountId().GetUserEmail();
229 
230   html_source->AddString("primaryUserEmail", primary_user_email);
231   html_source->AddBoolean("isActiveDirectoryUser",
232                           user && user->IsActiveDirectoryUser());
233   html_source->AddBoolean(
234       "isSecondaryUser",
235       user && user->GetAccountId() != primary_user->GetAccountId());
236   html_source->AddString(
237       "secondaryUserBannerText",
238       l10n_util::GetStringFUTF16(IDS_SETTINGS_SECONDARY_USER_BANNER,
239                                  base::ASCIIToUTF16(primary_user_email)));
240 }
241 
242 }  // namespace settings
243 }  // namespace chromeos
244