blob: 6a97cda5de29efa0fe76e9320fa1cfd549ba5a4a [file] [log] [blame]
Salma Elmahallawya78c76732023-06-29 15:39:401
Avi Drissman4a8573c2022-09-09 19:35:542// Copyright 2012 The Chromium Authors
[email protected]a81712032011-08-31 16:10:043// Use of this source code is governed by a BSD-style license that can be
4// found in the LICENSE file.
5
dpapad538e0162020-12-17 23:06:236#include "chrome/browser/ui/webui/policy/policy_ui.h"
[email protected]a81712032011-08-31 16:10:047
Jinho Bang7ab7a4e2018-01-15 14:36:078#include <memory>
9
Yann Dagob206dca42023-10-30 17:24:4810#include "base/json/json_string_value_serializer.h"
Salma Elmahallawya78c76732023-06-29 15:39:4011#include "base/json/json_writer.h"
12#include "base/strings/stringprintf.h"
13#include "base/system/sys_info.h"
Anton Urusoveb2d3812017-08-24 20:01:0214#include "build/build_config.h"
Owen Mina41576152022-10-20 23:27:3715#include "build/chromeos_buildflags.h"
Elena Scobici1d50eea32023-07-17 20:32:0616#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
Elena Scobici518d19c2023-06-28 15:01:3817#include "chrome/browser/policy/value_provider/chrome_policies_value_provider.h"
[email protected]a81712032011-08-31 16:10:0418#include "chrome/browser/profiles/profile.h"
dpapad538e0162020-12-17 23:06:2319#include "chrome/browser/ui/webui/policy/policy_ui_handler.h"
Lei Zhang6af63c262019-11-22 02:07:0320#include "chrome/browser/ui/webui/webui_util.h"
Yann Dagoee874fcd2023-09-15 15:49:3121#include "chrome/common/channel_info.h"
[email protected]a81712032011-08-31 16:10:0422#include "chrome/common/url_constants.h"
Henrique Ferreirod78ab262023-09-13 11:21:4323#include "chrome/grit/branded_strings.h"
dpapad5a98fb22023-01-12 18:51:3424#include "components/grit/policy_resources.h"
25#include "components/grit/policy_resources_map.h"
Elena Scobicidb1e74e2023-06-08 22:52:2426#include "components/policy/core/common/features.h"
Elena Scobicie31fb4a2023-07-28 15:06:5827#include "components/policy/core/common/management/management_service.h"
Jayee9ba64c202023-07-24 15:01:3628#include "components/policy/core/common/policy_loader_common.h"
Jayee07bb468c2023-06-01 18:25:3129#include "components/policy/core/common/policy_pref_names.h"
Elena Scobicidb1e74e2023-06-08 22:52:2430#include "components/policy/core/common/policy_utils.h"
Elena Scobicie31fb4a2023-07-28 15:06:5831#include "components/policy/policy_constants.h"
Jayee07bb468c2023-06-01 18:25:3132#include "components/prefs/pref_registry_simple.h"
thestig4a2e88e2016-08-27 23:23:5133#include "components/strings/grit/components_strings.h"
Salma Elmahallawya78c76732023-06-29 15:39:4034#include "components/version_info/version_info.h"
35#include "components/version_ui/version_handler_helper.h"
[email protected]01ec4ec2012-01-18 04:13:4736#include "content/public/browser/web_ui.h"
Elena Scobicidb1e74e2023-06-08 22:52:2437#include "content/public/browser/web_ui_controller.h"
38#include "content/public/browser/web_ui_message_handler.h"
Salma Elmahallawya78c76732023-06-29 15:39:4039#include "content/public/common/user_agent.h"
Rebekah Potterf6b2f262022-05-27 21:43:1940#include "services/network/public/mojom/content_security_policy.mojom.h"
Rohit Raoe38fb482020-01-19 21:35:1441#include "ui/base/webui/web_ui_util.h"
[email protected]8a2166d92012-12-06 23:58:4542
[email protected]a8ac9bd22012-05-02 12:30:3743namespace {
David Jean93315a8f92023-06-16 08:03:2244
Salma Elmahallawy0d7b1c42023-01-24 01:03:5145// Returns the operating system information to be displayed on
46// chrome://policy/logs page.
47std::string GetOsInfo() {
Salma Elmahallawya78c76732023-06-29 15:39:4048#if BUILDFLAG(IS_ANDROID)
49 // The base format for the OS version and build.
Salma Elmahallawya44f0442023-01-26 22:00:1550 constexpr char kOSVersionAndBuildFormat[] = "Android %s %s";
Salma Elmahallawy0d7b1c42023-01-24 01:03:5151 return base::StringPrintf(
52 kOSVersionAndBuildFormat,
Salma Elmahallawya44f0442023-01-26 22:00:1553 (base::SysInfo::OperatingSystemVersion()).c_str(),
Salma Elmahallawy0d7b1c42023-01-24 01:03:5154 (content::GetAndroidOSInfo(content::IncludeAndroidBuildNumber::Include,
55 content::IncludeAndroidModel::Include))
Salma Elmahallawya44f0442023-01-26 22:00:1556 .c_str());
Salma Elmahallawya78c76732023-06-29 15:39:4057#else
58 return base::StringPrintf("%s %s",
59 base::SysInfo::OperatingSystemName().c_str(),
60 base::SysInfo::OperatingSystemVersion().c_str());
61#endif // BUILDFLAG (IS_ANDROID)
Salma Elmahallawy0d7b1c42023-01-24 01:03:5162}
63
64// Returns the version information to be displayed on the chrome://policy/logs
65// page.
Roman Sorokin9f655dca2023-02-01 14:09:3266base::Value::Dict GetVersionInfo() {
67 base::Value::Dict version_info;
Salma Elmahallawy0d7b1c42023-01-24 01:03:5168
Roman Sorokin9f655dca2023-02-01 14:09:3269 version_info.Set("revision", version_info::GetLastChange());
70 version_info.Set("version", version_info::GetVersionNumber());
71 version_info.Set("deviceOs", GetOsInfo());
72 version_info.Set("variations", version_ui::GetVariationsList());
Salma Elmahallawy0d7b1c42023-01-24 01:03:5173
74 return version_info;
75}
Salma Elmahallawy0d7b1c42023-01-24 01:03:5176
Rebekah Potter463261e2022-12-20 20:49:0177void CreateAndAddPolicyUIHtmlSource(Profile* profile) {
78 content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
79 profile, chrome::kChromeUIPolicyHost);
fhorschig07c5e0622015-12-03 16:48:5780 PolicyUIHandler::AddCommonLocalizedStringsToSource(source);
Henrique Limasbc7b1ba2019-05-22 22:35:5581
Lei Zhang6af63c262019-11-22 02:07:0382 static constexpr webui::LocalizedString kStrings[] = {
Yann Dago7a25b6f2020-05-14 01:09:2483 // Localized strings (alphabetical order).
Jérôme Gingras4b90f6fc2020-05-28 18:48:5784 {"copyPoliciesJSON", IDS_COPY_POLICIES_JSON},
Yann Dago7a25b6f2020-05-14 01:09:2485 {"exportPoliciesJSON", IDS_EXPORT_POLICIES_JSON},
86 {"filterPlaceholder", IDS_POLICY_FILTER_PLACEHOLDER},
87 {"hideExpandedStatus", IDS_POLICY_HIDE_EXPANDED_STATUS},
88 {"isAffiliatedYes", IDS_POLICY_IS_AFFILIATED_YES},
89 {"isAffiliatedNo", IDS_POLICY_IS_AFFILIATED_NO},
90 {"labelAssetId", IDS_POLICY_LABEL_ASSET_ID},
91 {"labelClientId", IDS_POLICY_LABEL_CLIENT_ID},
92 {"labelDirectoryApiId", IDS_POLICY_LABEL_DIRECTORY_API_ID},
Prateek R. Patileb8c8a2b2022-11-16 18:29:2293 {"labelError", IDS_POLICY_LABEL_ERROR},
Yann Dago4937e132023-01-23 14:19:2694 {"labelWarning", IDS_POLICY_HEADER_WARNING},
Yann Dago7a25b6f2020-05-14 01:09:2495 {"labelGaiaId", IDS_POLICY_LABEL_GAIA_ID},
96 {"labelIsAffiliated", IDS_POLICY_LABEL_IS_AFFILIATED},
Owen Mina41576152022-10-20 23:27:3797 {"labelLastCloudReportSentTimestamp",
98 IDS_POLICY_LABEL_LAST_CLOUD_REPORT_SENT_TIMESTAMP},
Yann Dago7a25b6f2020-05-14 01:09:2499 {"labelLocation", IDS_POLICY_LABEL_LOCATION},
100 {"labelMachineEnrollmentDomain",
101 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DOMAIN},
102 {"labelMachineEnrollmentMachineName",
103 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_MACHINE_NAME},
104 {"labelMachineEnrollmentToken", IDS_POLICY_LABEL_MACHINE_ENROLLMENT_TOKEN},
105 {"labelMachineEntrollmentDeviceId",
106 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DEVICE_ID},
107 {"labelIsOffHoursActive", IDS_POLICY_LABEL_IS_OFFHOURS_ACTIVE},
108 {"labelPoliciesPush", IDS_POLICY_LABEL_PUSH_POLICIES},
Igor Ruvinov129ad8c2021-11-05 19:35:40109 {"labelPrecedence", IDS_POLICY_LABEL_PRECEDENCE},
hmare6f303e43e2022-11-17 19:43:46110 {"labelProfileId", IDS_POLICY_LABEL_PROFILE_ID},
Yann Dago7a25b6f2020-05-14 01:09:24111 {"labelRefreshInterval", IDS_POLICY_LABEL_REFRESH_INTERVAL},
112 {"labelStatus", IDS_POLICY_LABEL_STATUS},
Sergiy Belozorovff0a9402022-03-08 17:58:24113 {"labelTimeSinceLastFetchAttempt",
114 IDS_POLICY_LABEL_TIME_SINCE_LAST_FETCH_ATTEMPT},
Yann Dago7a25b6f2020-05-14 01:09:24115 {"labelTimeSinceLastRefresh", IDS_POLICY_LABEL_TIME_SINCE_LAST_REFRESH},
116 {"labelUsername", IDS_POLICY_LABEL_USERNAME},
Jana Grill81c202952021-02-08 11:57:38117 {"labelManagedBy", IDS_POLICY_LABEL_MANAGED_BY},
Yann Dago7a25b6f2020-05-14 01:09:24118 {"labelVersion", IDS_POLICY_LABEL_VERSION},
Aryan Kaushik17637fd2023-08-24 08:02:36119 {"moreActions", IDS_POLICY_MORE_ACTIONS},
Yann Dago7a25b6f2020-05-14 01:09:24120 {"noPoliciesSet", IDS_POLICY_NO_POLICIES_SET},
121 {"offHoursActive", IDS_POLICY_OFFHOURS_ACTIVE},
122 {"offHoursNotActive", IDS_POLICY_OFFHOURS_NOT_ACTIVE},
Jérôme Gingras272c1f7d2020-05-27 16:35:50123 {"policyCopyValue", IDS_POLICY_COPY_VALUE},
Yann Dago7a25b6f2020-05-14 01:09:24124 {"policiesPushOff", IDS_POLICY_PUSH_POLICIES_OFF},
125 {"policiesPushOn", IDS_POLICY_PUSH_POLICIES_ON},
126 {"policyLearnMore", IDS_POLICY_LEARN_MORE},
127 {"reloadPolicies", IDS_POLICY_RELOAD_POLICIES},
128 {"showExpandedStatus", IDS_POLICY_SHOW_EXPANDED_STATUS},
129 {"showLess", IDS_POLICY_SHOW_LESS},
130 {"showMore", IDS_POLICY_SHOW_MORE},
131 {"showUnset", IDS_POLICY_SHOW_UNSET},
132 {"signinProfile", IDS_POLICY_SIGNIN_PROFILE},
133 {"status", IDS_POLICY_STATUS},
Prateek R. Patileb8c8a2b2022-11-16 18:29:22134 {"statusErrorManagedNoPolicy", IDS_POLICY_STATUS_ERROR_MANAGED_NO_POLICY},
Yann Dago4937e132023-01-23 14:19:26135 {"statusFlexOrgNoPolicy", IDS_POLICY_STATUS_FLEX_ORG_NO_POLICY},
Yann Dago7a25b6f2020-05-14 01:09:24136 {"statusDevice", IDS_POLICY_STATUS_DEVICE},
137 {"statusMachine", IDS_POLICY_STATUS_MACHINE},
Xiaohan Wange9439fd2022-01-18 21:00:31138#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
Yann Dago7a25b6f2020-05-14 01:09:24139 {"statusUpdater", IDS_POLICY_STATUS_UPDATER},
140#endif
141 {"statusUser", IDS_POLICY_STATUS_USER},
Owen Mina41576152022-10-20 23:27:37142#if !BUILDFLAG(IS_CHROMEOS)
143 {"uploadReport", IDS_UPLOAD_REPORT},
144#endif // !BUILDFLAG(IS_CHROMEOS)
Aryan Kaushik17637fd2023-08-24 08:02:36145 {"viewLogs", IDS_VIEW_POLICY_LOGS},
Henrique Limasbc7b1ba2019-05-22 22:35:55146 };
dpapadf61285c2021-02-09 21:56:39147 source->AddLocalizedStrings(kStrings);
Henrique Limasbc7b1ba2019-05-22 22:35:55148
Salma Elmahallawya44f0442023-01-26 22:00:15149 // Localized strings for chrome://policy/logs.
150 static constexpr webui::LocalizedString kPolicyLogsStrings[] = {
151 {"browserName", IDS_PRODUCT_NAME},
152 {"exportLogsJSON", IDS_EXPORT_POLICY_LOGS_JSON},
153 {"logsTitle", IDS_POLICY_LOGS_TITLE},
154 {"os", IDS_VERSION_UI_OS},
155 {"refreshLogs", IDS_REFRESH_POLICY_LOGS},
156 {"revision", IDS_VERSION_UI_REVISION},
157 {"versionInfoLabel", IDS_VERSION_INFO},
158 {"variations", IDS_VERSION_UI_VARIATIONS},
159 };
160 source->AddLocalizedStrings(kPolicyLogsStrings);
161
Salma Elmahallawy3059e682023-10-19 13:41:12162 std::string variations_json_value;
163 base::JSONWriter::Write(GetVersionInfo(), &variations_json_value);
Salma Elmahallawy0d7b1c42023-01-24 01:03:51164
Salma Elmahallawy3059e682023-10-19 13:41:12165 source->AddString("versionInfo", variations_json_value);
Salma Elmahallawy0d7b1c42023-01-24 01:03:51166
167 source->AddResourcePath("logs/", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
168 source->AddResourcePath("logs", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
Salma Elmahallawy0d7b1c42023-01-24 01:03:51169
Elena Scobici1d50eea32023-07-17 20:32:06170 // Test page should only load if testing is enabled and the profile is not
171 // managed by cloud.
Yann Dagof66aa5492023-12-05 18:21:03172 const bool allow_policy_test_page =
173 policy::utils::IsPolicyTestingEnabled(profile->GetPrefs(),
Yann Dagoee874fcd2023-09-15 15:49:31174 chrome::GetChannel()) &&
Elena Scobici1d50eea32023-07-17 20:32:06175 !policy::ManagementServiceFactory::GetForProfile(profile)
176 ->HasManagementAuthority(
Yann Dagof66aa5492023-12-05 18:21:03177 policy::EnterpriseManagementAuthority::CLOUD);
178 if (allow_policy_test_page) {
Elena Scobicidb1e74e2023-06-08 22:52:24179 // Localized strings for chrome://policy/test.
180 static constexpr webui::LocalizedString kPolicyTestStrings[] = {
181 {"testTitle", IDS_POLICY_TEST_TITLE},
Jayee7b9b6df2023-07-31 16:39:24182 {"testRestart", IDS_POLICY_TEST_RESTART_AND_APPLY},
Elena Scobici518d19c2023-06-28 15:01:38183 {"testApply", IDS_POLICY_TEST_APPLY},
184 {"testImport", IDS_POLICY_TEST_IMPORT},
185 {"testDesc", IDS_POLICY_TEST_DESC},
Elena Scobici3f4dc662023-07-17 20:27:29186 {"testRevertAppliedPolicies", IDS_POLICY_TEST_REVERT},
187 {"testClearPolicies", IDS_CLEAR},
Elena Scobici518d19c2023-06-28 15:01:38188 {"testTableName", IDS_POLICY_HEADER_NAME},
189 {"testTableSource", IDS_POLICY_HEADER_SOURCE},
Elena Scobici7b3bcbc2023-08-08 22:08:26190 {"testTableScope", IDS_POLICY_TEST_TABLE_SCOPE},
Elena Scobici518d19c2023-06-28 15:01:38191 {"testTableLevel", IDS_POLICY_HEADER_LEVEL},
192 {"testTableValue", IDS_POLICY_LABEL_VALUE},
193 {"testTableRemove", IDS_REMOVE},
194 {"testAdd", IDS_POLICY_TEST_ADD},
195 {"testNameSelect", IDS_POLICY_SELECT_NAME},
Elena Scobici277122b2023-08-01 17:44:12196 {"testTablePreset", IDS_POLICY_TEST_TABLE_PRESET},
197 {"testTablePresetCustom", IDS_POLICY_TEST_PRESET_CUSTOM},
198 {"testTablePresetLocalMachine", IDS_POLICY_TEST_PRESET_LOCAL_MACHINE},
199 {"testTablePresetCloudAccount", IDS_POLICY_TEST_PRESET_CLOUD_ACCOUNT},
Jayee826f507762023-08-07 16:57:37200 {"testUserAffiliated", IDS_POLICY_TEST_USER_AFFILIATED},
Elena Scobicidb1e74e2023-06-08 22:52:24201 };
Elena Scobici518d19c2023-06-28 15:01:38202
Elena Scobicidb1e74e2023-06-08 22:52:24203 source->AddLocalizedStrings(kPolicyTestStrings);
204 source->AddResourcePath("test/", IDR_POLICY_TEST_POLICY_TEST_HTML);
205 source->AddResourcePath("test", IDR_POLICY_TEST_POLICY_TEST_HTML);
Elena Scobici518d19c2023-06-28 15:01:38206
Elena Scobicie31fb4a2023-07-28 15:06:58207 // Create a string policy_names_to_types_str mapping policy names to their
208 // input types.
209 policy::Schema chrome_schema =
210 policy::Schema::Wrap(policy::GetChromeSchemaData());
Elena Scobici518d19c2023-06-28 15:01:38211 ChromePoliciesValueProvider value_provider(profile);
212 base::Value::List policy_names =
213 (*value_provider.GetNames().FindDict("chrome"))
214 .FindList("policyNames")
215 ->Clone();
Jayee9ba64c202023-07-24 15:01:36216
217 policy_names.EraseIf([&](auto& policy) {
218 return policy::IsPolicyNameSensitive(policy.GetString());
219 });
220
Yann Dagob206dca42023-10-30 17:24:48221 std::string policy_names_to_types;
222 JSONStringValueSerializer serializer(&policy_names_to_types);
223 serializer.Serialize(
224 policy::utils::GetPolicyNameToTypeMapping(policy_names, chrome_schema));
225 source->AddString("policyNamesToTypes", policy_names_to_types);
Elena Scobici518d19c2023-06-28 15:01:38226
Elena Scobicia8858f22023-07-24 14:46:19227 // Strings for policy levels, scopes and sources.
Elena Scobici518d19c2023-06-28 15:01:38228 static constexpr webui::LocalizedString kPolicyTestTypes[] = {
229 {"scopeUser", IDS_POLICY_SCOPE_USER},
Elena Scobici518d19c2023-06-28 15:01:38230 {"scopeDevice", IDS_POLICY_SCOPE_DEVICE},
231 {"levelRecommended", IDS_POLICY_LEVEL_RECOMMENDED},
232 {"levelMandatory", IDS_POLICY_LEVEL_MANDATORY},
233 {"sourceEnterpriseDefault", IDS_POLICY_SOURCE_ENTERPRISE_DEFAULT},
Elena Scobici518d19c2023-06-28 15:01:38234 {"sourceCommandLine", IDS_POLICY_SOURCE_COMMAND_LINE},
235 {"sourceCloud", IDS_POLICY_SOURCE_CLOUD},
Elena Scobicia8858f22023-07-24 14:46:19236 {"sourceActiveDirectory", IDS_POLICY_SOURCE_ACTIVE_DIRECTORY},
237 {"sourcePlatform", IDS_POLICY_SOURCE_PLATFORM},
Elena Scobici518d19c2023-06-28 15:01:38238 {"sourceMerged", IDS_POLICY_SOURCE_MERGED},
239 {"sourceCloudFromAsh", IDS_POLICY_SOURCE_CLOUD_FROM_ASH},
240 {"sourceRestrictedManagedGuestSessionOverride",
241 IDS_POLICY_SOURCE_RESTRICTED_MANAGED_GUEST_SESSION_OVERRIDE},
Elena Scobici518d19c2023-06-28 15:01:38242 };
243
244 source->AddLocalizedStrings(kPolicyTestTypes);
Elena Scobicidb1e74e2023-06-08 22:52:24245 }
246
Yann Dagof66aa5492023-12-05 18:21:03247 source->AddString("acceptedPaths",
248 allow_policy_test_page ? "/|/test|/logs" : "/|/logs");
dpapad5a98fb22023-01-12 18:51:34249 webui::SetupWebUIDataSource(
250 source, base::make_span(kPolicyResources, kPolicyResourcesSize),
251 IDR_POLICY_POLICY_HTML);
Rebekah Potterf6b2f262022-05-27 21:43:19252
Yann Dago4937e132023-01-23 14:19:26253 webui::EnableTrustedTypesCSP(source);
[email protected]a81712032011-08-31 16:10:04254}
255
[email protected]8a2166d92012-12-06 23:58:45256} // namespace
257
[email protected]01ec4ec2012-01-18 04:13:47258PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) {
Jinho Bang7ab7a4e2018-01-15 14:36:07259 web_ui->AddMessageHandler(std::make_unique<PolicyUIHandler>());
Rebekah Potter463261e2022-12-20 20:49:01260 CreateAndAddPolicyUIHtmlSource(Profile::FromWebUI(web_ui));
[email protected]a81712032011-08-31 16:10:04261}
262
Jérôme Gingras272c1f7d2020-05-27 16:35:50263PolicyUI::~PolicyUI() = default;
Jayee07bb468c2023-06-01 18:25:31264
265// static
266void PolicyUI::RegisterProfilePrefs(PrefRegistrySimple* registry) {
267 registry->RegisterBooleanPref(policy::policy_prefs::kPolicyTestPageEnabled,
268 true);
269}