blob: c0d66c206b71b7d30578775317eb479215743475 [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"
Yann Dagoee874fcd2023-09-15 15:49:3120#include "chrome/common/channel_info.h"
[email protected]a81712032011-08-31 16:10:0421#include "chrome/common/url_constants.h"
Henrique Ferreirod78ab262023-09-13 11:21:4322#include "chrome/grit/branded_strings.h"
dpapad5a98fb22023-01-12 18:51:3423#include "components/grit/policy_resources.h"
24#include "components/grit/policy_resources_map.h"
Elena Scobicidb1e74e2023-06-08 22:52:2425#include "components/policy/core/common/features.h"
Elena Scobicie31fb4a2023-07-28 15:06:5826#include "components/policy/core/common/management/management_service.h"
Jayee9ba64c202023-07-24 15:01:3627#include "components/policy/core/common/policy_loader_common.h"
Salma Elmahallawya78c76732023-06-29 15:39:4028#include "components/policy/core/common/policy_logger.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 Elmahallawy0d7b1c42023-01-24 01:03:51162 source->AddBoolean(
163 "loggingEnabled",
164 policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled());
165
166 if (policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled()) {
167 std::string variations_json_value;
168 base::JSONWriter::Write(GetVersionInfo(), &variations_json_value);
169
170 source->AddString("versionInfo", variations_json_value);
171 }
172
173 source->AddResourcePath("logs/", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
174 source->AddResourcePath("logs", IDR_POLICY_LOGS_POLICY_LOGS_HTML);
Salma Elmahallawy0d7b1c42023-01-24 01:03:51175
Elena Scobici1d50eea32023-07-17 20:32:06176 // Test page should only load if testing is enabled and the profile is not
177 // managed by cloud.
Yann Dagoee874fcd2023-09-15 15:49:31178 if (policy::utils::IsPolicyTestingEnabled(profile->GetPrefs(),
179 chrome::GetChannel()) &&
Elena Scobici1d50eea32023-07-17 20:32:06180 !policy::ManagementServiceFactory::GetForProfile(profile)
181 ->HasManagementAuthority(
182 policy::EnterpriseManagementAuthority::CLOUD)) {
Elena Scobicidb1e74e2023-06-08 22:52:24183 // Localized strings for chrome://policy/test.
184 static constexpr webui::LocalizedString kPolicyTestStrings[] = {
185 {"testTitle", IDS_POLICY_TEST_TITLE},
Jayee7b9b6df2023-07-31 16:39:24186 {"testRestart", IDS_POLICY_TEST_RESTART_AND_APPLY},
Elena Scobici518d19c2023-06-28 15:01:38187 {"testApply", IDS_POLICY_TEST_APPLY},
188 {"testImport", IDS_POLICY_TEST_IMPORT},
189 {"testDesc", IDS_POLICY_TEST_DESC},
Elena Scobici3f4dc662023-07-17 20:27:29190 {"testRevertAppliedPolicies", IDS_POLICY_TEST_REVERT},
191 {"testClearPolicies", IDS_CLEAR},
Elena Scobici518d19c2023-06-28 15:01:38192 {"testTableName", IDS_POLICY_HEADER_NAME},
193 {"testTableSource", IDS_POLICY_HEADER_SOURCE},
Elena Scobici7b3bcbc2023-08-08 22:08:26194 {"testTableScope", IDS_POLICY_TEST_TABLE_SCOPE},
Elena Scobici518d19c2023-06-28 15:01:38195 {"testTableLevel", IDS_POLICY_HEADER_LEVEL},
196 {"testTableValue", IDS_POLICY_LABEL_VALUE},
197 {"testTableRemove", IDS_REMOVE},
198 {"testAdd", IDS_POLICY_TEST_ADD},
199 {"testNameSelect", IDS_POLICY_SELECT_NAME},
Elena Scobici277122b2023-08-01 17:44:12200 {"testTablePreset", IDS_POLICY_TEST_TABLE_PRESET},
201 {"testTablePresetCustom", IDS_POLICY_TEST_PRESET_CUSTOM},
202 {"testTablePresetLocalMachine", IDS_POLICY_TEST_PRESET_LOCAL_MACHINE},
203 {"testTablePresetCloudAccount", IDS_POLICY_TEST_PRESET_CLOUD_ACCOUNT},
Jayee826f507762023-08-07 16:57:37204 {"testUserAffiliated", IDS_POLICY_TEST_USER_AFFILIATED},
Elena Scobicidb1e74e2023-06-08 22:52:24205 };
Elena Scobici518d19c2023-06-28 15:01:38206
Elena Scobicidb1e74e2023-06-08 22:52:24207 source->AddLocalizedStrings(kPolicyTestStrings);
208 source->AddResourcePath("test/", IDR_POLICY_TEST_POLICY_TEST_HTML);
209 source->AddResourcePath("test", IDR_POLICY_TEST_POLICY_TEST_HTML);
Elena Scobici518d19c2023-06-28 15:01:38210
Elena Scobicie31fb4a2023-07-28 15:06:58211 // Create a string policy_names_to_types_str mapping policy names to their
212 // input types.
213 policy::Schema chrome_schema =
214 policy::Schema::Wrap(policy::GetChromeSchemaData());
Elena Scobici518d19c2023-06-28 15:01:38215 ChromePoliciesValueProvider value_provider(profile);
216 base::Value::List policy_names =
217 (*value_provider.GetNames().FindDict("chrome"))
218 .FindList("policyNames")
219 ->Clone();
Jayee9ba64c202023-07-24 15:01:36220
221 policy_names.EraseIf([&](auto& policy) {
222 return policy::IsPolicyNameSensitive(policy.GetString());
223 });
224
Elena Scobicie31fb4a2023-07-28 15:06:58225 std::string policy_names_to_types_str = "{";
Elena Scobici518d19c2023-06-28 15:01:38226 for (auto& policy_name : policy_names) {
Elena Scobicie31fb4a2023-07-28 15:06:58227 base::Value::Type policy_type =
228 chrome_schema.GetKnownProperty(policy_name.GetString()).type();
229 std::string policy_type_string;
230 switch (policy_type) {
231 case base::Value::Type::BOOLEAN:
232 policy_type_string = "boolean";
233 break;
234 case base::Value::Type::DICT:
235 policy_type_string = "dictionary";
236 break;
237 case base::Value::Type::INTEGER:
238 policy_type_string = "integer";
239 break;
240 case base::Value::Type::LIST:
241 policy_type_string = "list";
242 break;
243 case base::Value::Type::STRING:
244 policy_type_string = "string";
245 break;
246 default:
247 break;
248 }
249 policy_names_to_types_str +=
250 "\"" + policy_name.GetString() + "\":\"" + policy_type_string + "\",";
Elena Scobici518d19c2023-06-28 15:01:38251 }
Elena Scobicie31fb4a2023-07-28 15:06:58252 policy_names_to_types_str.pop_back(); // remove last divider
253 policy_names_to_types_str += "}";
254 source->AddString("policyNamesToTypes", policy_names_to_types_str);
Elena Scobici518d19c2023-06-28 15:01:38255
Elena Scobicia8858f22023-07-24 14:46:19256 // Strings for policy levels, scopes and sources.
Elena Scobici518d19c2023-06-28 15:01:38257 static constexpr webui::LocalizedString kPolicyTestTypes[] = {
258 {"scopeUser", IDS_POLICY_SCOPE_USER},
Elena Scobici518d19c2023-06-28 15:01:38259 {"scopeDevice", IDS_POLICY_SCOPE_DEVICE},
260 {"levelRecommended", IDS_POLICY_LEVEL_RECOMMENDED},
261 {"levelMandatory", IDS_POLICY_LEVEL_MANDATORY},
262 {"sourceEnterpriseDefault", IDS_POLICY_SOURCE_ENTERPRISE_DEFAULT},
Elena Scobici518d19c2023-06-28 15:01:38263 {"sourceCommandLine", IDS_POLICY_SOURCE_COMMAND_LINE},
264 {"sourceCloud", IDS_POLICY_SOURCE_CLOUD},
Elena Scobicia8858f22023-07-24 14:46:19265 {"sourceActiveDirectory", IDS_POLICY_SOURCE_ACTIVE_DIRECTORY},
266 {"sourcePlatform", IDS_POLICY_SOURCE_PLATFORM},
Elena Scobici518d19c2023-06-28 15:01:38267 {"sourceMerged", IDS_POLICY_SOURCE_MERGED},
268 {"sourceCloudFromAsh", IDS_POLICY_SOURCE_CLOUD_FROM_ASH},
269 {"sourceRestrictedManagedGuestSessionOverride",
270 IDS_POLICY_SOURCE_RESTRICTED_MANAGED_GUEST_SESSION_OVERRIDE},
Elena Scobici518d19c2023-06-28 15:01:38271 };
272
273 source->AddLocalizedStrings(kPolicyTestTypes);
Elena Scobicidb1e74e2023-06-08 22:52:24274 }
275
dpapad5a98fb22023-01-12 18:51:34276 webui::SetupWebUIDataSource(
277 source, base::make_span(kPolicyResources, kPolicyResourcesSize),
278 IDR_POLICY_POLICY_HTML);
Rebekah Potterf6b2f262022-05-27 21:43:19279
Yann Dago4937e132023-01-23 14:19:26280 webui::EnableTrustedTypesCSP(source);
[email protected]a81712032011-08-31 16:10:04281}
282
[email protected]8a2166d92012-12-06 23:58:45283} // namespace
284
[email protected]01ec4ec2012-01-18 04:13:47285PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) {
Jinho Bang7ab7a4e2018-01-15 14:36:07286 web_ui->AddMessageHandler(std::make_unique<PolicyUIHandler>());
Rebekah Potter463261e2022-12-20 20:49:01287 CreateAndAddPolicyUIHtmlSource(Profile::FromWebUI(web_ui));
[email protected]a81712032011-08-31 16:10:04288}
289
Jérôme Gingras272c1f7d2020-05-27 16:35:50290PolicyUI::~PolicyUI() = default;
Jayee07bb468c2023-06-01 18:25:31291
292// static
293void PolicyUI::RegisterProfilePrefs(PrefRegistrySimple* registry) {
294 registry->RegisterBooleanPref(policy::policy_prefs::kPolicyTestPageEnabled,
295 true);
296}