os_feedback_untrusted_ui.cc 4.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105
  1. // Copyright 2022 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/os_feedback_ui/os_feedback_untrusted_ui.h"
  5. #include <memory>
  6. #include "ash/webui/grit/ash_os_feedback_resources.h"
  7. #include "ash/webui/grit/ash_os_feedback_untrusted_resources.h"
  8. #include "ash/webui/grit/ash_os_feedback_untrusted_resources_map.h"
  9. #include "ash/webui/os_feedback_ui/url_constants.h"
  10. #include "base/containers/span.h"
  11. #include "chromeos/strings/grit/chromeos_strings.h"
  12. #include "content/public/browser/web_contents.h"
  13. #include "content/public/browser/web_ui.h"
  14. #include "content/public/browser/web_ui_data_source.h"
  15. #include "content/public/common/url_constants.h"
  16. #include "url/gurl.h"
  17. namespace ash {
  18. namespace feedback {
  19. namespace {
  20. void AddLocalizedStrings(content::WebUIDataSource* source) {
  21. static constexpr webui::LocalizedString kLocalizedStrings[] = {
  22. {"suggestedHelpContent", IDS_FEEDBACK_TOOL_SUGGESTED_HELP_CONTENT},
  23. {"popularHelpContent", IDS_FEEDBACK_TOOL_POPULAR_HELP_CONTENT},
  24. {"noMatchedResults", IDS_FEEDBACK_TOOL_NO_MATCHED_RESULTS},
  25. {"helpContentOfflineMessage",
  26. IDS_FEEDBACK_TOOL_HELP_CONTENT_OFFLINE_MESSAGE},
  27. {"helpContentOfflineAltText",
  28. IDS_FEEDBACK_TOOL_HELP_CONTENT_OFFLINE_ALT_TEXT},
  29. {"helpContentLabelTooltip", IDS_FEEDBACK_TOOL_HELP_CONTENT_LABEL_TOOLTIP},
  30. };
  31. source->AddLocalizedStrings(kLocalizedStrings);
  32. source->UseStringsJs();
  33. }
  34. } // namespace
  35. OsFeedbackUntrustedUIConfig::OsFeedbackUntrustedUIConfig()
  36. : WebUIConfig(content::kChromeUIUntrustedScheme,
  37. kChromeUIOSFeedbackUntrustedHost) {}
  38. OsFeedbackUntrustedUIConfig::~OsFeedbackUntrustedUIConfig() = default;
  39. std::unique_ptr<content::WebUIController>
  40. OsFeedbackUntrustedUIConfig::CreateWebUIController(content::WebUI* web_ui) {
  41. return std::make_unique<OsFeedbackUntrustedUI>(web_ui);
  42. }
  43. OsFeedbackUntrustedUI::OsFeedbackUntrustedUI(content::WebUI* web_ui)
  44. : ui::UntrustedWebUIController(web_ui) {
  45. content::WebUIDataSource* untrusted_source =
  46. content::WebUIDataSource::CreateAndAdd(
  47. web_ui->GetWebContents()->GetBrowserContext(),
  48. kChromeUIOSFeedbackUntrustedUrl);
  49. untrusted_source->AddResourcePaths(base::make_span(
  50. kAshOsFeedbackUntrustedResources, kAshOsFeedbackUntrustedResourcesSize));
  51. untrusted_source->AddResourcePath("help_content.js",
  52. IDR_ASH_OS_FEEDBACK_HELP_CONTENT_JS);
  53. untrusted_source->AddResourcePath("feedback_types.js",
  54. IDR_ASH_OS_FEEDBACK_FEEDBACK_TYPES_JS);
  55. untrusted_source->AddResourcePath(
  56. "file_path.mojom-lite.js",
  57. IDR_ASH_OS_FEEDBACK_MOJO_PUBLIC_MOJOM_BASE_FILE_PATH_MOJOM_LITE_JS);
  58. untrusted_source->AddResourcePath(
  59. "safe_base_name.mojom-lite.js",
  60. IDR_ASH_OS_FEEDBACK_MOJO_PUBLIC_MOJOM_BASE_SAFE_BASE_NAME_MOJOM_LITE_JS);
  61. untrusted_source->AddResourcePath(
  62. "help_resources_icons.js", IDR_ASH_OS_FEEDBACK_HELP_RESOURCES_ICONS_JS);
  63. untrusted_source->AddResourcePath(
  64. "mojom/os_feedback_ui.mojom-lite.js",
  65. IDR_ASH_OS_FEEDBACK_MOJOM_OS_FEEDBACK_UI_MOJOM_LITE_JS);
  66. untrusted_source->SetDefaultResource(
  67. IDR_ASH_OS_FEEDBACK_UNTRUSTED_UNTRUSTED_INDEX_HTML);
  68. AddLocalizedStrings(untrusted_source);
  69. // Allow the chrome://os-feedback WebUI to embed the corresponding
  70. // chrome-untrusted://os-feedback WebUI.
  71. untrusted_source->AddFrameAncestor(GURL(kChromeUIOSFeedbackUrl));
  72. // DisableTrustedTypesCSP to support TrustedTypePolicy named 'goog#html'.
  73. // It is the Closure templating system that renders our UI, as it does many
  74. // other web apps using it.
  75. untrusted_source->DisableTrustedTypesCSP();
  76. // TODO(b/194964287): Audit and tighten CSP.
  77. untrusted_source->OverrideContentSecurityPolicy(
  78. network::mojom::CSPDirectiveName::DefaultSrc, "");
  79. untrusted_source->OverrideContentSecurityPolicy(
  80. network::mojom::CSPDirectiveName::ScriptSrc,
  81. "script-src 'self' chrome-untrusted://resources;");
  82. }
  83. OsFeedbackUntrustedUI::~OsFeedbackUntrustedUI() = default;
  84. } // namespace feedback
  85. } // namespace ash