Avi Drissman | 4a8573c | 2022-09-09 19:35:54 | [diff] [blame] | 1 | // Copyright 2012 The Chromium Authors |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 2 | // Use of this source code is governed by a BSD-style license that can be |
| 3 | // found in the LICENSE file. |
| 4 | |
dpapad | 538e016 | 2020-12-17 23:06:23 | [diff] [blame] | 5 | #include "chrome/browser/ui/webui/policy/policy_ui.h" |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 6 | |
Jinho Bang | 7ab7a4e | 2018-01-15 14:36:07 | [diff] [blame] | 7 | #include <memory> |
| 8 | |
Anton Urusov | eb2d381 | 2017-08-24 20:01:02 | [diff] [blame] | 9 | #include "build/build_config.h" |
Owen Min | a4157615 | 2022-10-20 23:27:37 | [diff] [blame] | 10 | #include "build/chromeos_buildflags.h" |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 11 | #include "chrome/browser/profiles/profile.h" |
dpapad | 538e016 | 2020-12-17 23:06:23 | [diff] [blame] | 12 | #include "chrome/browser/ui/webui/policy/policy_ui_handler.h" |
Lei Zhang | 6af63c26 | 2019-11-22 02:07:03 | [diff] [blame] | 13 | #include "chrome/browser/ui/webui/webui_util.h" |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 14 | #include "chrome/common/url_constants.h" |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 15 | #include "chrome/grit/chromium_strings.h" |
dpapad | 5a98fb2 | 2023-01-12 18:51:34 | [diff] [blame] | 16 | #include "components/grit/policy_resources.h" |
| 17 | #include "components/grit/policy_resources_map.h" |
thestig | 4a2e88e | 2016-08-27 23:23:51 | [diff] [blame] | 18 | #include "components/strings/grit/components_strings.h" |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 19 | #include "content/public/browser/web_ui.h" |
Rebekah Potter | f6b2f26 | 2022-05-27 21:43:19 | [diff] [blame] | 20 | #include "services/network/public/mojom/content_security_policy.mojom.h" |
Rohit Rao | e38fb48 | 2020-01-19 21:35:14 | [diff] [blame] | 21 | #include "ui/base/webui/web_ui_util.h" |
[email protected] | 8a2166d9 | 2012-12-06 23:58:45 | [diff] [blame] | 22 | |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 23 | #if BUILDFLAG(IS_ANDROID) |
| 24 | #include "base/json/json_writer.h" |
| 25 | #include "base/strings/stringprintf.h" |
| 26 | #include "base/system/sys_info.h" |
| 27 | #include "components/policy/core/common/policy_logger.h" |
| 28 | #include "components/version_info/version_info.h" |
| 29 | #include "components/version_ui/version_handler_helper.h" |
| 30 | #include "content/public/common/user_agent.h" |
| 31 | #endif // BUILDFLAG(IS_ANDROID) |
| 32 | |
[email protected] | a8ac9bd2 | 2012-05-02 12:30:37 | [diff] [blame] | 33 | namespace { |
[email protected] | 0ec4898e | 2011-12-30 21:09:24 | [diff] [blame] | 34 | |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 35 | #if BUILDFLAG(IS_ANDROID) |
| 36 | // Returns the operating system information to be displayed on |
| 37 | // chrome://policy/logs page. |
| 38 | std::string GetOsInfo() { |
| 39 | // The base format for the OS version and build |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 40 | constexpr char kOSVersionAndBuildFormat[] = "Android %s %s"; |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 41 | return base::StringPrintf( |
| 42 | kOSVersionAndBuildFormat, |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 43 | (base::SysInfo::OperatingSystemVersion()).c_str(), |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 44 | (content::GetAndroidOSInfo(content::IncludeAndroidBuildNumber::Include, |
| 45 | content::IncludeAndroidModel::Include)) |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 46 | .c_str()); |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 47 | } |
| 48 | |
| 49 | // Returns the version information to be displayed on the chrome://policy/logs |
| 50 | // page. |
| 51 | base::Value GetVersionInfo() { |
| 52 | base::Value version_info(base::Value::Type::DICT); |
| 53 | |
| 54 | version_info.SetStringPath("revision", version_info::GetLastChange()); |
| 55 | version_info.SetStringPath("version", version_info::GetVersionNumber()); |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 56 | version_info.SetStringPath("deviceOs", GetOsInfo()); |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 57 | version_info.SetPath("variations", |
| 58 | base::Value(version_ui::GetVariationsList())); |
| 59 | |
| 60 | return version_info; |
| 61 | } |
| 62 | #endif |
| 63 | |
Rebekah Potter | 463261e | 2022-12-20 20:49:01 | [diff] [blame] | 64 | void CreateAndAddPolicyUIHtmlSource(Profile* profile) { |
| 65 | content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd( |
| 66 | profile, chrome::kChromeUIPolicyHost); |
fhorschig | 07c5e062 | 2015-12-03 16:48:57 | [diff] [blame] | 67 | PolicyUIHandler::AddCommonLocalizedStringsToSource(source); |
Henrique Limas | bc7b1ba | 2019-05-22 22:35:55 | [diff] [blame] | 68 | |
Lei Zhang | 6af63c26 | 2019-11-22 02:07:03 | [diff] [blame] | 69 | static constexpr webui::LocalizedString kStrings[] = { |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 70 | // Localized strings (alphabetical order). |
Jérôme Gingras | 4b90f6fc | 2020-05-28 18:48:57 | [diff] [blame] | 71 | {"copyPoliciesJSON", IDS_COPY_POLICIES_JSON}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 72 | {"exportPoliciesJSON", IDS_EXPORT_POLICIES_JSON}, |
| 73 | {"filterPlaceholder", IDS_POLICY_FILTER_PLACEHOLDER}, |
| 74 | {"hideExpandedStatus", IDS_POLICY_HIDE_EXPANDED_STATUS}, |
| 75 | {"isAffiliatedYes", IDS_POLICY_IS_AFFILIATED_YES}, |
| 76 | {"isAffiliatedNo", IDS_POLICY_IS_AFFILIATED_NO}, |
| 77 | {"labelAssetId", IDS_POLICY_LABEL_ASSET_ID}, |
| 78 | {"labelClientId", IDS_POLICY_LABEL_CLIENT_ID}, |
| 79 | {"labelDirectoryApiId", IDS_POLICY_LABEL_DIRECTORY_API_ID}, |
Prateek R. Patil | eb8c8a2b | 2022-11-16 18:29:22 | [diff] [blame] | 80 | {"labelError", IDS_POLICY_LABEL_ERROR}, |
Yann Dago | 4937e13 | 2023-01-23 14:19:26 | [diff] [blame] | 81 | {"labelWarning", IDS_POLICY_HEADER_WARNING}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 82 | {"labelGaiaId", IDS_POLICY_LABEL_GAIA_ID}, |
| 83 | {"labelIsAffiliated", IDS_POLICY_LABEL_IS_AFFILIATED}, |
Owen Min | a4157615 | 2022-10-20 23:27:37 | [diff] [blame] | 84 | {"labelLastCloudReportSentTimestamp", |
| 85 | IDS_POLICY_LABEL_LAST_CLOUD_REPORT_SENT_TIMESTAMP}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 86 | {"labelLocation", IDS_POLICY_LABEL_LOCATION}, |
| 87 | {"labelMachineEnrollmentDomain", |
| 88 | IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DOMAIN}, |
| 89 | {"labelMachineEnrollmentMachineName", |
| 90 | IDS_POLICY_LABEL_MACHINE_ENROLLMENT_MACHINE_NAME}, |
| 91 | {"labelMachineEnrollmentToken", IDS_POLICY_LABEL_MACHINE_ENROLLMENT_TOKEN}, |
| 92 | {"labelMachineEntrollmentDeviceId", |
| 93 | IDS_POLICY_LABEL_MACHINE_ENROLLMENT_DEVICE_ID}, |
| 94 | {"labelIsOffHoursActive", IDS_POLICY_LABEL_IS_OFFHOURS_ACTIVE}, |
| 95 | {"labelPoliciesPush", IDS_POLICY_LABEL_PUSH_POLICIES}, |
Igor Ruvinov | 129ad8c | 2021-11-05 19:35:40 | [diff] [blame] | 96 | {"labelPrecedence", IDS_POLICY_LABEL_PRECEDENCE}, |
hmare | 6f303e43e | 2022-11-17 19:43:46 | [diff] [blame] | 97 | {"labelProfileId", IDS_POLICY_LABEL_PROFILE_ID}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 98 | {"labelRefreshInterval", IDS_POLICY_LABEL_REFRESH_INTERVAL}, |
| 99 | {"labelStatus", IDS_POLICY_LABEL_STATUS}, |
Sergiy Belozorov | ff0a940 | 2022-03-08 17:58:24 | [diff] [blame] | 100 | {"labelTimeSinceLastFetchAttempt", |
| 101 | IDS_POLICY_LABEL_TIME_SINCE_LAST_FETCH_ATTEMPT}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 102 | {"labelTimeSinceLastRefresh", IDS_POLICY_LABEL_TIME_SINCE_LAST_REFRESH}, |
| 103 | {"labelUsername", IDS_POLICY_LABEL_USERNAME}, |
Jana Grill | 81c20295 | 2021-02-08 11:57:38 | [diff] [blame] | 104 | {"labelManagedBy", IDS_POLICY_LABEL_MANAGED_BY}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 105 | {"labelVersion", IDS_POLICY_LABEL_VERSION}, |
| 106 | {"noPoliciesSet", IDS_POLICY_NO_POLICIES_SET}, |
| 107 | {"offHoursActive", IDS_POLICY_OFFHOURS_ACTIVE}, |
| 108 | {"offHoursNotActive", IDS_POLICY_OFFHOURS_NOT_ACTIVE}, |
Jérôme Gingras | 272c1f7d | 2020-05-27 16:35:50 | [diff] [blame] | 109 | {"policyCopyValue", IDS_POLICY_COPY_VALUE}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 110 | {"policiesPushOff", IDS_POLICY_PUSH_POLICIES_OFF}, |
| 111 | {"policiesPushOn", IDS_POLICY_PUSH_POLICIES_ON}, |
| 112 | {"policyLearnMore", IDS_POLICY_LEARN_MORE}, |
| 113 | {"reloadPolicies", IDS_POLICY_RELOAD_POLICIES}, |
| 114 | {"showExpandedStatus", IDS_POLICY_SHOW_EXPANDED_STATUS}, |
| 115 | {"showLess", IDS_POLICY_SHOW_LESS}, |
| 116 | {"showMore", IDS_POLICY_SHOW_MORE}, |
| 117 | {"showUnset", IDS_POLICY_SHOW_UNSET}, |
| 118 | {"signinProfile", IDS_POLICY_SIGNIN_PROFILE}, |
| 119 | {"status", IDS_POLICY_STATUS}, |
Prateek R. Patil | eb8c8a2b | 2022-11-16 18:29:22 | [diff] [blame] | 120 | {"statusErrorManagedNoPolicy", IDS_POLICY_STATUS_ERROR_MANAGED_NO_POLICY}, |
Yann Dago | 4937e13 | 2023-01-23 14:19:26 | [diff] [blame] | 121 | {"statusFlexOrgNoPolicy", IDS_POLICY_STATUS_FLEX_ORG_NO_POLICY}, |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 122 | {"statusDevice", IDS_POLICY_STATUS_DEVICE}, |
| 123 | {"statusMachine", IDS_POLICY_STATUS_MACHINE}, |
Xiaohan Wang | e9439fd | 2022-01-18 21:00:31 | [diff] [blame] | 124 | #if BUILDFLAG(IS_WIN) && BUILDFLAG(GOOGLE_CHROME_BRANDING) |
Yann Dago | 7a25b6f | 2020-05-14 01:09:24 | [diff] [blame] | 125 | {"statusUpdater", IDS_POLICY_STATUS_UPDATER}, |
| 126 | #endif |
| 127 | {"statusUser", IDS_POLICY_STATUS_USER}, |
Owen Min | a4157615 | 2022-10-20 23:27:37 | [diff] [blame] | 128 | #if !BUILDFLAG(IS_CHROMEOS) |
| 129 | {"uploadReport", IDS_UPLOAD_REPORT}, |
| 130 | #endif // !BUILDFLAG(IS_CHROMEOS) |
Henrique Limas | bc7b1ba | 2019-05-22 22:35:55 | [diff] [blame] | 131 | }; |
dpapad | f61285c | 2021-02-09 21:56:39 | [diff] [blame] | 132 | source->AddLocalizedStrings(kStrings); |
Henrique Limas | bc7b1ba | 2019-05-22 22:35:55 | [diff] [blame] | 133 | |
Salma Elmahallawy | a44f044 | 2023-01-26 22:00:15 | [diff] [blame^] | 134 | // Localized strings for chrome://policy/logs. |
| 135 | static constexpr webui::LocalizedString kPolicyLogsStrings[] = { |
| 136 | {"browserName", IDS_PRODUCT_NAME}, |
| 137 | {"exportLogsJSON", IDS_EXPORT_POLICY_LOGS_JSON}, |
| 138 | {"logsTitle", IDS_POLICY_LOGS_TITLE}, |
| 139 | {"os", IDS_VERSION_UI_OS}, |
| 140 | {"refreshLogs", IDS_REFRESH_POLICY_LOGS}, |
| 141 | {"revision", IDS_VERSION_UI_REVISION}, |
| 142 | {"versionInfoLabel", IDS_VERSION_INFO}, |
| 143 | {"variations", IDS_VERSION_UI_VARIATIONS}, |
| 144 | }; |
| 145 | source->AddLocalizedStrings(kPolicyLogsStrings); |
| 146 | |
Salma Elmahallawy | 0d7b1c4 | 2023-01-24 01:03:51 | [diff] [blame] | 147 | #if BUILDFLAG(IS_ANDROID) |
| 148 | source->AddBoolean( |
| 149 | "loggingEnabled", |
| 150 | policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled()); |
| 151 | |
| 152 | if (policy::PolicyLogger::GetInstance()->IsPolicyLoggingEnabled()) { |
| 153 | std::string variations_json_value; |
| 154 | base::JSONWriter::Write(GetVersionInfo(), &variations_json_value); |
| 155 | |
| 156 | source->AddString("versionInfo", variations_json_value); |
| 157 | } |
| 158 | |
| 159 | source->AddResourcePath("logs/", IDR_POLICY_LOGS_POLICY_LOGS_HTML); |
| 160 | source->AddResourcePath("logs", IDR_POLICY_LOGS_POLICY_LOGS_HTML); |
| 161 | #endif // BUILDFLAG(IS_ANDROID) |
| 162 | |
dpapad | 5a98fb2 | 2023-01-12 18:51:34 | [diff] [blame] | 163 | webui::SetupWebUIDataSource( |
| 164 | source, base::make_span(kPolicyResources, kPolicyResourcesSize), |
| 165 | IDR_POLICY_POLICY_HTML); |
Rebekah Potter | f6b2f26 | 2022-05-27 21:43:19 | [diff] [blame] | 166 | |
Yann Dago | 4937e13 | 2023-01-23 14:19:26 | [diff] [blame] | 167 | webui::EnableTrustedTypesCSP(source); |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 168 | } |
| 169 | |
[email protected] | 8a2166d9 | 2012-12-06 23:58:45 | [diff] [blame] | 170 | } // namespace |
| 171 | |
[email protected] | 01ec4ec | 2012-01-18 04:13:47 | [diff] [blame] | 172 | PolicyUI::PolicyUI(content::WebUI* web_ui) : WebUIController(web_ui) { |
Jinho Bang | 7ab7a4e | 2018-01-15 14:36:07 | [diff] [blame] | 173 | web_ui->AddMessageHandler(std::make_unique<PolicyUIHandler>()); |
Rebekah Potter | 463261e | 2022-12-20 20:49:01 | [diff] [blame] | 174 | CreateAndAddPolicyUIHtmlSource(Profile::FromWebUI(web_ui)); |
[email protected] | a8171203 | 2011-08-31 16:10:04 | [diff] [blame] | 175 | } |
| 176 | |
Jérôme Gingras | 272c1f7d | 2020-05-27 16:35:50 | [diff] [blame] | 177 | PolicyUI::~PolicyUI() = default; |