blob: 2c73f1913b44a208c9a2f4d8974b4a617468d360 [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
Salma Elmahallawya78c76732023-06-29 15:39:4010#include "base/json/json_writer.h"
11#include "base/strings/stringprintf.h"
12#include "base/system/sys_info.h"
Anton Urusoveb2d3812017-08-24 20:01:0213#include "build/build_config.h"
Owen Mina41576152022-10-20 23:27:3714#include "build/chromeos_buildflags.h"
Elena Scobici1d50eea32023-07-17 20:32:0615#include "chrome/browser/enterprise/browser_management/management_service_factory.h"
Elena Scobici518d19c2023-06-28 15:01:3816#include "chrome/browser/policy/value_provider/chrome_policies_value_provider.h"
[email protected]a81712032011-08-31 16:10:0417#include "chrome/browser/profiles/profile.h"
dpapad538e0162020-12-17 23:06:2318#include "chrome/browser/ui/webui/policy/policy_ui_handler.h"
Lei Zhang6af63c262019-11-22 02:07:0319#include "chrome/browser/ui/webui/webui_util.h"
[email protected]a81712032011-08-31 16:10:0420#include "chrome/common/url_constants.h"
Salma Elmahallawya44f0442023-01-26 22:00:1521#include "chrome/grit/chromium_strings.h"
dpapad5a98fb22023-01-12 18:51:3422#include "components/grit/policy_resources.h"
23#include "components/grit/policy_resources_map.h"
Elena Scobicidb1e74e2023-06-08 22:52:2424#include "components/policy/core/common/features.h"
Elena Scobicie31fb4a2023-07-28 15:06:5825#include "components/policy/core/common/management/management_service.h"
Jayee9ba64c202023-07-24 15:01:3626#include "components/policy/core/common/policy_loader_common.h"
Salma Elmahallawya78c76732023-06-29 15:39:4027#include "components/policy/core/common/policy_logger.h"
Jayee07bb468c2023-06-01 18:25:3128#include "components/policy/core/common/policy_pref_names.h"
Elena Scobicidb1e74e2023-06-08 22:52:2429#include "components/policy/core/common/policy_utils.h"
Elena Scobicie31fb4a2023-07-28 15:06:5830#include "components/policy/policy_constants.h"
Jayee07bb468c2023-06-01 18:25:3131#include "components/prefs/pref_registry_simple.h"
thestig4a2e88e2016-08-27 23:23:5132#include "components/strings/grit/components_strings.h"
Salma Elmahallawya78c76732023-06-29 15:39:4033#include "components/version_info/version_info.h"
34#include "components/version_ui/version_handler_helper.h"
[email protected]01ec4ec2012-01-18 04:13:4735#include "content/public/browser/web_ui.h"
Elena Scobicidb1e74e2023-06-08 22:52:2436#include "content/public/browser/web_ui_controller.h"
37#include "content/public/browser/web_ui_message_handler.h"
Salma Elmahallawya78c76732023-06-29 15:39:4038#include "content/public/common/user_agent.h"
Rebekah Potterf6b2f262022-05-27 21:43:1939#include "services/network/public/mojom/content_security_policy.mojom.h"
Rohit Raoe38fb482020-01-19 21:35:1440#include "ui/base/webui/web_ui_util.h"
[email protected]8a2166d92012-12-06 23:58:4541
[email protected]a8ac9bd22012-05-02 12:30:3742namespace {
David Jean93315a8f92023-06-16 08:03:2243
Salma Elmahallawy0d7b1c42023-01-24 01:03:5144// Returns the operating system information to be displayed on
45// chrome://policy/logs page.
46std::string GetOsInfo() {
Salma Elmahallawya78c76732023-06-29 15:39:4047#if BUILDFLAG(IS_ANDROID)
48 // The base format for the OS version and build.
Salma Elmahallawya44f0442023-01-26 22:00:1549 constexpr char kOSVersionAndBuildFormat[] = "Android %s %s";
Salma Elmahallawy0d7b1c42023-01-24 01:03:5150 return base::StringPrintf(
51 kOSVersionAndBuildFormat,
Salma Elmahallawya44f0442023-01-26 22:00:1552 (base::SysInfo::OperatingSystemVersion()).c_str(),
Salma Elmahallawy0d7b1c42023-01-24 01:03:5153 (content::GetAndroidOSInfo(content::IncludeAndroidBuildNumber::Include,
54 content::IncludeAndroidModel::Include))
Salma Elmahallawya44f0442023-01-26 22:00:1555 .c_str());
Salma Elmahallawya78c76732023-06-29 15:39:4056#else
57 return base::StringPrintf("%s %s",
58 base::SysInfo::OperatingSystemName().c_str(),
59 base::SysInfo::OperatingSystemVersion().c_str());
60#endif // BUILDFLAG (IS_ANDROID)
Salma Elmahallawy0d7b1c42023-01-24 01:03:5161}
62
63// Returns the version information to be displayed on the chrome://policy/logs
64// page.
Roman Sorokin9f655dca2023-02-01 14:09:3265base::Value::Dict GetVersionInfo() {
66 base::Value::Dict version_info;
Salma Elmahallawy0d7b1c42023-01-24 01:03:5167
Roman Sorokin9f655dca2023-02-01 14:09:3268 version_info.Set("revision", version_info::GetLastChange());
69 version_info.Set("version", version_info::GetVersionNumber());
70 version_info.Set("deviceOs", GetOsInfo());
71 version_info.Set("variations", version_ui::GetVariationsList());
Salma Elmahallawy0d7b1c42023-01-24 01:03:5172
73 return version_info;
74}
Salma Elmahallawy0d7b1c42023-01-24 01:03:5175
Rebekah Potter463261e2022-12-20 20:49:0176void CreateAndAddPolicyUIHtmlSource(Profile* profile) {
77 content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
78 profile, chrome::kChromeUIPolicyHost);
fhorschig07c5e0622015-12-03 16:48:5779 PolicyUIHandler::AddCommonLocalizedStringsToSource(source);
Henrique Limasbc7b1ba2019-05-22 22:35:5580
Lei Zhang6af63c262019-11-22 02:07:0381 static constexpr webui::LocalizedString kStrings[] = {
Yann Dago7a25b6f2020-05-14 01:09:2482 // Localized strings (alphabetical order).
Jérôme Gingras4b90f6fc2020-05-28 18:48:5783 {"copyPoliciesJSON", IDS_COPY_POLICIES_JSON},
Yann Dago7a25b6f2020-05-14 01:09:2484 {"exportPoliciesJSON", IDS_EXPORT_POLICIES_JSON},
85 {"filterPlaceholder", IDS_POLICY_FILTER_PLACEHOLDER},
86 {"hideExpandedStatus", IDS_POLICY_HIDE_EXPANDED_STATUS},
87 {"isAffiliatedYes", IDS_POLICY_IS_AFFILIATED_YES},
88 {"isAffiliatedNo", IDS_POLICY_IS_AFFILIATED_NO},
89 {"labelAssetId", IDS_POLICY_LABEL_ASSET_ID},
90 {"labelClientId", IDS_POLICY_LABEL_CLIENT_ID},
91 {"labelDirectoryApiId", IDS_POLICY_LABEL_DIRECTORY_API_ID},
Prateek R. Patileb8c8a2b2022-11-16 18:29:2292 {"labelError", IDS_POLICY_LABEL_ERROR},
Yann Dago4937e132023-01-23 14:19:2693 {"labelWarning", IDS_POLICY_HEADER_WARNING},
Yann Dago7a25b6f2020-05-14 01:09:2494 {"labelGaiaId", IDS_POLICY_LABEL_GAIA_ID},
95 {"labelIsAffiliated", IDS_POLICY_LABEL_IS_AFFILIATED},
Owen Mina41576152022-10-20 23:27:3796 {"labelLastCloudReportSentTimestamp",
97 IDS_POLICY_LABEL_LAST_CLOUD_REPORT_SENT_TIMESTAMP},
Yann Dago7a25b6f2020-05-14 01:09:2498 {"labelLocation", IDS_POLICY_LABEL_LOCATION},
99 {"labelMachineEnrollmentDomain",
100 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DOMAIN},
101 {"labelMachineEnrollmentMachineName",
102 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_MACHINE_NAME},
103 {"labelMachineEnrollmentToken", IDS_POLICY_LABEL_MACHINE_ENROLLMENT_TOKEN},
104 {"labelMachineEntrollmentDeviceId",
105 IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DEVICE_ID},
106 {"labelIsOffHoursActive", IDS_POLICY_LABEL_IS_OFFHOURS_ACTIVE},
107 {"labelPoliciesPush", IDS_POLICY_LABEL_PUSH_POLICIES},
Igor Ruvinov129ad8c2021-11-05 19:35:40108 {"labelPrecedence", IDS_POLICY_LABEL_PRECEDENCE},
hmare6f303e43e2022-11-17 19:43:46109 {"labelProfileId", IDS_POLICY_LABEL_PROFILE_ID},
Yann Dago7a25b6f2020-05-14 01:09:24110 {"labelRefreshInterval", IDS_POLICY_LABEL_REFRESH_INTERVAL},
111 {"labelStatus", IDS_POLICY_LABEL_STATUS},
Sergiy Belozorovff0a9402022-03-08 17:58:24112 {"labelTimeSinceLastFetchAttempt",
113 IDS_POLICY_LABEL_TIME_SINCE_LAST_FETCH_ATTEMPT},
Yann Dago7a25b6f2020-05-14 01:09:24114 {"labelTimeSinceLastRefresh", IDS_POLICY_LABEL_TIME_SINCE_LAST_REFRESH},
115 {"labelUsername", IDS_POLICY_LABEL_USERNAME},
Jana Grill81c202952021-02-08 11:57:38116 {"labelManagedBy", IDS_POLICY_LABEL_MANAGED_BY},
Yann Dago7a25b6f2020-05-14 01:09:24117 {"labelVersion", IDS_POLICY_LABEL_VERSION},
118 {"noPoliciesSet", IDS_POLICY_NO_POLICIES_SET},
119 {"offHoursActive", IDS_POLICY_OFFHOURS_ACTIVE},
120 {"offHoursNotActive", IDS_POLICY_OFFHOURS_NOT_ACTIVE},
Jérôme Gingras272c1f7d2020-05-27 16:35:50121 {"policyCopyValue", IDS_POLICY_COPY_VALUE},
Yann Dago7a25b6f2020-05-14 01:09:24122 {"policiesPushOff", IDS_POLICY_PUSH_POLICIES_OFF},
123 {"policiesPushOn", IDS_POLICY_PUSH_POLICIES_ON},
124 {"policyLearnMore", IDS_POLICY_LEARN_MORE},
125 {"reloadPolicies", IDS_POLICY_RELOAD_POLICIES},
126 {"showExpandedStatus", IDS_POLICY_SHOW_EXPANDED_STATUS},
127 {"showLess", IDS_POLICY_SHOW_LESS},
128 {"showMore", IDS_POLICY_SHOW_MORE},
129 {"showUnset", IDS_POLICY_SHOW_UNSET},
130 {"signinProfile", IDS_POLICY_SIGNIN_PROFILE},
131 {"status", IDS_POLICY_STATUS},
Prateek R. Patileb8c8a2b2022-11-16 18:29:22132 {"statusErrorManagedNoPolicy", IDS_POLICY_STATUS_ERROR_MANAGED_NO_POLICY},
Yann Dago4937e132023-01-23 14:19:26133 {"statusFlexOrgNoPolicy", IDS_POLICY_STATUS_FLEX_ORG_NO_POLICY},
Yann Dago7a25b6f2020-05-14 01:09:24134 {"statusDevice", IDS_POLICY_STATUS_DEVICE},
135 {"statusMachine", IDS_POLICY_STATUS_MACHINE},
Xiaohan Wange9439fd2022-01-18 21:00:31136#if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING)
Yann Dago7a25b6f2020-05-14 01:09:24137 {"statusUpdater", IDS_POLICY_STATUS_UPDATER},
138#endif
139 {"statusUser", IDS_POLICY_STATUS_USER},
Owen Mina41576152022-10-20 23:27:37140#if !BUILDFLAG(IS_CHROMEOS)
141 {"uploadReport", IDS_UPLOAD_REPORT},
142#endif // !BUILDFLAG(IS_CHROMEOS)
Henrique Limasbc7b1ba2019-05-22 22:35:55143 };
dpapadf61285c2021-02-09 21:56:39144 source->AddLocalizedStrings(kStrings);
Henrique Limasbc7b1ba2019-05-22 22:35:55145
Salma Elmahallawya44f0442023-01-26 22:00:15146 // Localized strings for chrome://policy/logs.
147 static constexpr webui::LocalizedString kPolicyLogsStrings[] = {
148 {"browserName", IDS_PRODUCT_NAME},
149 {"exportLogsJSON", IDS_EXPORT_POLICY_LOGS_JSON},
150 {"logsTitle", IDS_POLICY_LOGS_TITLE},
151 {"os", IDS_VERSION_UI_OS},
152 {"refreshLogs", IDS_REFRESH_POLICY_LOGS},
153 {"revision", IDS_VERSION_UI_REVISION},
154 {"versionInfoLabel", IDS_VERSION_INFO},
155 {"variations", IDS_VERSION_UI_VARIATIONS},
156 };
157 source->AddLocalizedStrings(kPolicyLogsStrings);
158
Salma Elmahallawy0d7b1c42023-01-24 01:03:51159 source->AddBoolean(
160 "loggingEnabled",
161 policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled());
162
163 if (policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled()) {
164 std::string variations_json_value;
165 base::JSONWriter::Write(GetVersionInfo(), &variations_json_value);
166
167 source->AddString("versionInfo", variations_json_value);
168 }
169
170 source->AddResourcePath("logs/", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
171 source->AddResourcePath("logs", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
Salma Elmahallawy0d7b1c42023-01-24 01:03:51172
Elena Scobici1d50eea32023-07-17 20:32:06173 // Test page should only load if testing is enabled and the profile is not
174 // managed by cloud.
175 if (policy::utils::IsPolicyTestingEnabled(profile->GetPrefs()) &&
176 !policy::ManagementServiceFactory::GetForProfile(profile)
177 ->HasManagementAuthority(
178 policy::EnterpriseManagementAuthority::CLOUD)) {
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
Elena Scobicie31fb4a2023-07-28 15:06:58221 std::string policy_names_to_types_str = "{";
Elena Scobici518d19c2023-06-28 15:01:38222 for (auto& policy_name : policy_names) {
Elena Scobicie31fb4a2023-07-28 15:06:58223 base::Value::Type policy_type =
224 chrome_schema.GetKnownProperty(policy_name.GetString()).type();
225 std::string policy_type_string;
226 switch (policy_type) {
227 case base::Value::Type::BOOLEAN:
228 policy_type_string = "boolean";
229 break;
230 case base::Value::Type::DICT:
231 policy_type_string = "dictionary";
232 break;
233 case base::Value::Type::INTEGER:
234 policy_type_string = "integer";
235 break;
236 case base::Value::Type::LIST:
237 policy_type_string = "list";
238 break;
239 case base::Value::Type::STRING:
240 policy_type_string = "string";
241 break;
242 default:
243 break;
244 }
245 policy_names_to_types_str +=
246 "\"" + policy_name.GetString() + "\":\"" + policy_type_string + "\",";
Elena Scobici518d19c2023-06-28 15:01:38247 }
Elena Scobicie31fb4a2023-07-28 15:06:58248 policy_names_to_types_str.pop_back(); // remove last divider
249 policy_names_to_types_str += "}";
250 source->AddString("policyNamesToTypes", policy_names_to_types_str);
Elena Scobici518d19c2023-06-28 15:01:38251
Elena Scobicia8858f22023-07-24 14:46:19252 // Strings for policy levels, scopes and sources.
Elena Scobici518d19c2023-06-28 15:01:38253 static constexpr webui::LocalizedString kPolicyTestTypes[] = {
254 {"scopeUser", IDS_POLICY_SCOPE_USER},
Elena Scobici518d19c2023-06-28 15:01:38255 {"scopeDevice", IDS_POLICY_SCOPE_DEVICE},
256 {"levelRecommended", IDS_POLICY_LEVEL_RECOMMENDED},
257 {"levelMandatory", IDS_POLICY_LEVEL_MANDATORY},
258 {"sourceEnterpriseDefault", IDS_POLICY_SOURCE_ENTERPRISE_DEFAULT},
Elena Scobici518d19c2023-06-28 15:01:38259 {"sourceCommandLine", IDS_POLICY_SOURCE_COMMAND_LINE},
260 {"sourceCloud", IDS_POLICY_SOURCE_CLOUD},
Elena Scobicia8858f22023-07-24 14:46:19261 {"sourceActiveDirectory", IDS_POLICY_SOURCE_ACTIVE_DIRECTORY},
262 {"sourcePlatform", IDS_POLICY_SOURCE_PLATFORM},
Elena Scobici518d19c2023-06-28 15:01:38263 {"sourceMerged", IDS_POLICY_SOURCE_MERGED},
264 {"sourceCloudFromAsh", IDS_POLICY_SOURCE_CLOUD_FROM_ASH},
265 {"sourceRestrictedManagedGuestSessionOverride",
266 IDS_POLICY_SOURCE_RESTRICTED_MANAGED_GUEST_SESSION_OVERRIDE},
Elena Scobici518d19c2023-06-28 15:01:38267 };
268
269 source->AddLocalizedStrings(kPolicyTestTypes);
Elena Scobicidb1e74e2023-06-08 22:52:24270 }
271
dpapad5a98fb22023-01-12 18:51:34272 webui::SetupWebUIDataSource(
273 source, base::make_span(kPolicyResources, kPolicyResourcesSize),
274 IDR_POLICY_POLICY_HTML);
Rebekah Potterf6b2f262022-05-27 21:43:19275
Yann Dago4937e132023-01-23 14:19:26276 webui::EnableTrustedTypesCSP(source);
[email protected]a81712032011-08-31 16:10:04277}
278
[email protected]8a2166d92012-12-06 23:58:45279} // namespace
280
[email protected]01ec4ec2012-01-18 04:13:47281PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) {
Jinho Bang7ab7a4e2018-01-15 14:36:07282 web_ui->AddMessageHandler(std::make_unique<PolicyUIHandler>());
Rebekah Potter463261e2022-12-20 20:49:01283 CreateAndAddPolicyUIHtmlSource(Profile::FromWebUI(web_ui));
[email protected]a81712032011-08-31 16:10:04284}
285
Jérôme Gingras272c1f7d2020-05-27 16:35:50286PolicyUI::~PolicyUI() = default;
Jayee07bb468c2023-06-01 18:25:31287
288// static
289void PolicyUI::RegisterProfilePrefs(PrefRegistrySimple* registry) {
290 registry->RegisterBooleanPref(policy::policy_prefs::kPolicyTestPageEnabled,
291 true);
292}