print_management_ui.cc 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143
  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. #include "ash/webui/print_management/print_management_ui.h"
  5. #include "ash/webui/grit/ash_print_management_resources.h"
  6. #include "ash/webui/grit/ash_print_management_resources_map.h"
  7. #include "ash/webui/print_management/mojom/printing_manager.mojom.h"
  8. #include "ash/webui/print_management/url_constants.h"
  9. #include "chromeos/strings/grit/chromeos_strings.h"
  10. #include "content/public/browser/web_contents.h"
  11. #include "content/public/browser/web_ui.h"
  12. #include "content/public/browser/web_ui_data_source.h"
  13. #include "services/network/public/mojom/content_security_policy.mojom.h"
  14. #include "ui/base/webui/web_ui_util.h"
  15. #include "ui/resources/grit/webui_generated_resources.h"
  16. namespace ash {
  17. namespace printing {
  18. namespace printing_manager {
  19. namespace {
  20. void SetUpWebUIDataSource(content::WebUIDataSource* source,
  21. base::span<const webui::ResourcePath> resources,
  22. int default_resource) {
  23. for (const auto& resource : resources) {
  24. source->AddResourcePath(resource.path, resource.id);
  25. }
  26. source->SetDefaultResource(default_resource);
  27. source->AddResourcePath("test_loader.html", IDR_WEBUI_HTML_TEST_LOADER_HTML);
  28. source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER_JS);
  29. source->AddResourcePath("test_loader_util.js",
  30. IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
  31. }
  32. void AddPrintManagementStrings(content::WebUIDataSource* html_source) {
  33. static constexpr webui::LocalizedString kLocalizedStrings[] = {
  34. {"completionStatusCanceled",
  35. IDS_PRINT_MANAGEMENT_COMPLETION_STATUS_CANCELED},
  36. {"completionStatusPrinted",
  37. IDS_PRINT_MANAGEMENT_COMPLETION_STATUS_PRINTED},
  38. {"fileNameColumn", IDS_PRINT_MANAGEMENT_FILE_NAME_COLUMN},
  39. {"printerNameColumn", IDS_PRINT_MANAGEMENT_PRINTER_NAME_COLUMN},
  40. {"dateColumn", IDS_PRINT_MANAGEMENT_DATE_COLUMN},
  41. {"statusColumn", IDS_PRINT_MANAGEMENT_STATUS_COLUMN},
  42. {"printJobTitle", IDS_PRINT_MANAGEMENT_TITLE},
  43. {"clearAllHistoryLabel",
  44. IDS_PRINT_MANAGEMENT_CLEAR_ALL_HISTORY_BUTTON_TEXT},
  45. {"clearHistoryConfirmationText",
  46. IDS_PRINT_MANAGEMENT_CLEAR_ALL_HISTORY_CONFIRMATION_TEXT},
  47. {"cancelButtonLabel", IDS_PRINT_MANAGEMENT_CANCEL_BUTTON_LABEL},
  48. {"clearButtonLabel", IDS_PRINT_MANAGEMENT_CLEAR_BUTTON_LABEL},
  49. {"historyHeader", IDS_PRINT_MANAGEMENT_HISTORY_HEADER_LABEL},
  50. {"printJobHistoryExpirationPeriod",
  51. IDS_PRINT_MANAGEMENT_HISTORY_TOOL_TIP_MULTIPLE_DAYS_EXPIRATION},
  52. {"printJobHistoryIndefinitePeriod",
  53. IDS_PRINT_MANAGEMENT_HISTORY_TOOL_TIP_INDEFINITE},
  54. {"printJobHistorySingleDay",
  55. IDS_PRINT_MANAGEMENT_HISTORY_TOOL_TIP_SINGLE_DAY_EXPIRATION},
  56. {"printedPageLabel", IDS_PRINT_MANAGEMENT_PRINTED_PAGES_ARIA_LABEL},
  57. {"printedPagesFraction",
  58. IDS_PRINT_MANAGEMENT_PRINTED_PAGES_PROGRESS_FRACTION},
  59. {"completePrintJobLabel", IDS_PRINT_MANAGEMENT_COMPLETED_JOB_ARIA_LABEL},
  60. {"ongoingPrintJobLabel", IDS_PRINT_MANAGEMENT_ONGOING_JOB_ARIA_LABEL},
  61. {"stoppedOngoingPrintJobLabel",
  62. IDS_PRINT_MANAGEMENT_STOPPED_ONGOING_JOB_ARIA_LABEL},
  63. {"paperJam", IDS_PRINT_MANAGEMENT_PAPER_JAM_ERROR_STATUS},
  64. {"outOfPaper", IDS_PRINT_MANAGEMENT_OUT_OF_PAPER_ERROR_STATUS},
  65. {"outOfInk", IDS_PRINT_MANAGEMENT_OUT_OF_INK_ERROR_STATUS},
  66. {"doorOpen", IDS_PRINT_MANAGEMENT_DOOR_OPEN_ERROR_STATUS},
  67. {"printerUnreachable",
  68. IDS_PRINT_MANAGEMENT_PRINTER_UNREACHABLE_ERROR_STATUS},
  69. {"trayMissing", IDS_PRINT_MANAGEMENT_TRAY_MISSING_ERROR_STATUS},
  70. {"outputFull", IDS_PRINT_MANAGEMENT_OUTPUT_FULL_ERROR_STATUS},
  71. {"stopped", IDS_PRINT_MANAGEMENT_STOPPED_ERROR_STATUS},
  72. {"clientUnauthorized",
  73. IDS_PRINT_MANAGEMENT_CLIENT_UNAUTHORIZED_ERROR_STATUS},
  74. {"filterFailed", IDS_PRINT_MANAGEMENT_FILTERED_FAILED_ERROR_STATUS},
  75. {"unknownPrinterError", IDS_PRINT_MANAGEMENT_UNKNOWN_ERROR_STATUS},
  76. {"paperJamStopped", IDS_PRINT_MANAGEMENT_PAPER_JAM_STOPPED_ERROR_STATUS},
  77. {"outOfPaperStopped",
  78. IDS_PRINT_MANAGEMENT_OUT_OF_PAPER_STOPPED_ERROR_STATUS},
  79. {"outOfInkStopped", IDS_PRINT_MANAGEMENT_OUT_OF_INK_STOPPED_ERROR_STATUS},
  80. {"doorOpenStopped", IDS_PRINT_MANAGEMENT_DOOR_OPEN_STOPPED_ERROR_STATUS},
  81. {"trayMissingStopped",
  82. IDS_PRINT_MANAGEMENT_TRAY_MISSING_STOPPED_ERROR_STATUS},
  83. {"outputFullStopped",
  84. IDS_PRINT_MANAGEMENT_OUTPUT_FULL_STOPPED_ERROR_STATUS},
  85. {"stoppedGeneric", IDS_PRINT_MANAGEMENT_GENERIC_STOPPED_ERROR_STATUS},
  86. {"unknownPrinterErrorStopped",
  87. IDS_PRINT_MANAGEMENT_UNKNOWN_STOPPED_ERROR_STATUS},
  88. {"noPrintJobInProgress",
  89. IDS_PRINT_MANAGEMENT_NO_PRINT_JOBS_IN_PROGRESS_MESSAGE},
  90. {"clearAllPrintJobPolicyIndicatorToolTip",
  91. IDS_PRINT_MANAGEMENT_CLEAR_ALL_POLICY_PRINT_JOB_INDICATOR_MESSAGE},
  92. {"cancelPrintJobButtonLabel",
  93. IDS_PRINT_MANAGEMENT_CANCEL_PRINT_JOB_BUTTON_LABEL},
  94. {"cancelledPrintJob",
  95. IDS_PRINT_MANAGEMENT_CANCELED_PRINT_JOB_ARIA_ANNOUNCEMENT},
  96. {"collapsedPrintingText", IDS_PRINT_MANAGEMENT_COLLAPSE_PRINTING_STATUS}};
  97. html_source->AddLocalizedStrings(kLocalizedStrings);
  98. html_source->UseStringsJs();
  99. }
  100. } // namespace
  101. PrintManagementUI::PrintManagementUI(
  102. content::WebUI* web_ui,
  103. BindPrintingMetadataProviderCallback callback)
  104. : ui::MojoWebUIController(web_ui),
  105. bind_pending_receiver_callback_(std::move(callback)) {
  106. content::WebUIDataSource* html_source =
  107. content::WebUIDataSource::CreateAndAdd(
  108. web_ui->GetWebContents()->GetBrowserContext(),
  109. kChromeUIPrintManagementHost);
  110. html_source->OverrideContentSecurityPolicy(
  111. network::mojom::CSPDirectiveName::ScriptSrc,
  112. "script-src chrome://resources chrome://test 'self';");
  113. html_source->DisableTrustedTypesCSP();
  114. const auto resources = base::make_span(kAshPrintManagementResources,
  115. kAshPrintManagementResourcesSize);
  116. SetUpWebUIDataSource(html_source, resources, IDR_PRINT_MANAGEMENT_INDEX_HTML);
  117. html_source->AddResourcePath("printing_manager.mojom-lite.js",
  118. IDR_PRINTING_MANAGER_MOJO_LITE_JS);
  119. AddPrintManagementStrings(html_source);
  120. }
  121. PrintManagementUI::~PrintManagementUI() = default;
  122. void PrintManagementUI::BindInterface(
  123. mojo::PendingReceiver<mojom::PrintingMetadataProvider> receiver) {
  124. bind_pending_receiver_callback_.Run(std::move(receiver));
  125. }
  126. WEB_UI_CONTROLLER_TYPE_IMPL(PrintManagementUI)
  127. } // namespace printing_manager
  128. } // namespace printing
  129. } // namespace ash