os_feedback_ui.cc 7.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166
  1. // Copyright 2021 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_ui.h"
  5. #include <memory>
  6. #include <utility>
  7. #include "ash/webui/grit/ash_os_feedback_resources.h"
  8. #include "ash/webui/grit/ash_os_feedback_resources_map.h"
  9. #include "ash/webui/os_feedback_ui/backend/feedback_service_provider.h"
  10. #include "ash/webui/os_feedback_ui/backend/help_content_provider.h"
  11. #include "ash/webui/os_feedback_ui/backend/os_feedback_delegate.h"
  12. #include "ash/webui/os_feedback_ui/mojom/os_feedback_ui.mojom.h"
  13. #include "ash/webui/os_feedback_ui/url_constants.h"
  14. #include "chromeos/strings/grit/chromeos_strings.h"
  15. #include "content/public/browser/web_contents.h"
  16. #include "content/public/browser/web_ui.h"
  17. #include "content/public/browser/web_ui_data_source.h"
  18. #include "content/public/common/url_constants.h"
  19. #include "mojo/public/cpp/bindings/pending_receiver.h"
  20. #include "ui/resources/grit/webui_generated_resources.h"
  21. #include "ui/webui/mojo_web_ui_controller.h"
  22. #include "ui/webui/webui_allowlist.h"
  23. namespace ash {
  24. namespace {
  25. void SetUpWebUIDataSource(content::WebUIDataSource* source,
  26. base::span<const webui::ResourcePath> resources,
  27. int default_resource) {
  28. source->AddResourcePaths(resources);
  29. source->SetDefaultResource(default_resource);
  30. source->AddResourcePath("test_loader.html", IDR_WEBUI_HTML_TEST_LOADER_HTML);
  31. source->AddResourcePath("test_loader.js", IDR_WEBUI_JS_TEST_LOADER_JS);
  32. source->AddResourcePath("test_loader_util.js",
  33. IDR_WEBUI_JS_TEST_LOADER_UTIL_JS);
  34. }
  35. void AddLocalizedStrings(content::WebUIDataSource* source) {
  36. static constexpr webui::LocalizedString kLocalizedStrings[] = {
  37. {"backButtonLabel", IDS_FEEDBACK_TOOL_BACK_BUTTON_LABEL},
  38. {"continueButtonLabel", IDS_FEEDBACK_TOOL_CONTINUE_BUTTON_LABEL},
  39. {"descriptionHint", IDS_FEEDBACK_TOOL_DESCRIPTION_HINT},
  40. {"descriptionLabel", IDS_FEEDBACK_TOOL_DESCRIPTION_LABEL},
  41. {"descriptionRequired", IDS_FEEDBACK_TOOL_DESCRIPTION_REQUIRED},
  42. {"feedbackHelpLinkLabel", IDS_FEEDBACK_TOOL_FEEDBACK_HELP_LINK_LABEL},
  43. {"pageTitle", IDS_FEEDBACK_TOOL_PAGE_TITLE},
  44. {"privacyNote", IDS_FEEDBACK_TOOL_PRIVACY_NOTE},
  45. {"sendButtonLabel", IDS_FEEDBACK_TOOL_SEND_BUTTON_LABEL},
  46. // The help content strings are needed for browser tests.
  47. {"suggestedHelpContent", IDS_FEEDBACK_TOOL_SUGGESTED_HELP_CONTENT},
  48. {"popularHelpContent", IDS_FEEDBACK_TOOL_POPULAR_HELP_CONTENT},
  49. {"helpContentOfflineMessage",
  50. IDS_FEEDBACK_TOOL_HELP_CONTENT_OFFLINE_MESSAGE},
  51. {"helpContentOfflineAltText",
  52. IDS_FEEDBACK_TOOL_HELP_CONTENT_OFFLINE_ALT_TEXT},
  53. {"helpContentLabelTooltip", IDS_FEEDBACK_TOOL_HELP_CONTENT_LABEL_TOOLTIP},
  54. {"noMatchedResults", IDS_FEEDBACK_TOOL_NO_MATCHED_RESULTS},
  55. {"attachFilesLabel", IDS_FEEDBACK_TOOL_ATTACH_FILES_LABEL},
  56. {"attachScreenshotLabel", IDS_FEEDBACK_TOOL_SCREENSHOT_LABEL},
  57. {"attachScreenshotCheckboxAriaLabel",
  58. IDS_FEEDBACK_TOOL_ATTACH_SCREENSHOT_CHECKBOX_ARIA_LABEL},
  59. {"previewImageAriaLabel", IDS_FEEDBACK_TOOL_PREVIEW_IMAGE_ARIA_LABEL},
  60. {"addFileLabel", IDS_FEEDBACK_TOOL_ADD_FILE_LABEL},
  61. {"replaceFileLabel", IDS_FEEDBACK_TOOL_REPLACE_FILE_LABEL},
  62. {"replaceFileArialLabel", IDS_FEEDBACK_TOOL_REPLACE_FILE_ARIA_LABEL},
  63. {"attachFileLabelTooltip", IDS_FEEDBACK_TOOL_ATTACH_FILE_LABEL_TOOLTIP},
  64. {"attachFileCheckboxArialLabel",
  65. IDS_FEEDBACK_TOOL_ATTACH_FILE_CHECKBOX_ARIA_LABEL},
  66. {"userEmailLabel", IDS_FEEDBACK_TOOL_USER_EMAIL_LABEL},
  67. {"userEmailAriaLabel", IDS_FEEDBACK_TOOL_USER_EMAIL_ARIA_LABEL},
  68. {"shareDiagnosticDataLabel",
  69. IDS_FEEDBACK_TOOL_SHARE_DIAGNOSTIC_DATA_LABEL},
  70. {"sharePageUrlLabel", IDS_FEEDBACK_TOOL_SHARE_PAGE_URL_LABEL},
  71. {"confirmationTitleOnline", IDS_FEEDBACK_TOOL_PAGE_TITLE_AFTER_SENT},
  72. {"confirmationTitleOffline", IDS_FEEDBACK_TOOL_PAGE_TITLE_SEND_OFFLINE},
  73. {"exploreAppDescription",
  74. IDS_FEEDBACK_TOOL_RESOURCES_EXPLORE_APP_DESCRIPTION},
  75. {"exploreAppLabel", IDS_FEEDBACK_TOOL_RESOURCES_EXPLORE_APP_LABEL},
  76. {"diagnosticsAppLabel",
  77. IDS_FEEDBACK_TOOL_RESOURCES_DIAGNOSTICS_APP_LABEL},
  78. {"diagnosticsAppDescription",
  79. IDS_FEEDBACK_TOOL_RESOURCES_DIAGNOSTICS_APP_DESCRIPTION},
  80. {"askCommunityLabel", IDS_FEEDBACK_TOOL_RESOURCES_ASK_COMMUNITY_LABEL},
  81. {"askCommunityDescription",
  82. IDS_FEEDBACK_TOOL_RESOURCES_ASK_COMMUNITY_DESCRIPTION},
  83. {"userConsentLabel", IDS_FEEDBACK_TOOL_USER_CONSENT_LABEL},
  84. {"includeSystemInfoAndMetricsCheckboxLabel",
  85. IDS_FEEDBACK_TOOL_INCLUDE_SYSTEM_INFO_AND_METRICS_CHECKBOX_LABEL},
  86. {"anonymousUser", IDS_FEEDBACK_TOOL_ANONYMOUS_EMAIL_OPTION},
  87. {"thankYouNoteOffline", IDS_FEEDBACK_TOOL_THANK_YOU_NOTE_OFFLINE},
  88. {"thankYouNoteOnline", IDS_FEEDBACK_TOOL_THANK_YOU_NOTE_ONLINE},
  89. {"helpResourcesLabel", IDS_FEEDBACK_TOOL_HELP_RESOURCES_LABEL},
  90. {"buttonNewReport", IDS_FEEDBACK_TOOL_SEND_NEW_REPORT_BUTTON_LABEL},
  91. {"buttonDone", IDS_FEEDBACK_TOOL_DONE_BUTTON_LABEL},
  92. {"fileTooBigErrorMessage", IDS_FEEDBACK_TOOL_FILE_TOO_BIG_ERROR_MESSAGE},
  93. {"bluetoothLogsInfo", IDS_FEEDBACK_TOOL_BLUETOOTH_LOGS_CHECKBOX},
  94. };
  95. source->AddLocalizedStrings(kLocalizedStrings);
  96. source->UseStringsJs();
  97. }
  98. } // namespace
  99. OSFeedbackUI::OSFeedbackUI(
  100. content::WebUI* web_ui,
  101. std::unique_ptr<OsFeedbackDelegate> feedback_delegate)
  102. : MojoWebUIController(web_ui) {
  103. auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
  104. content::WebUIDataSource* source = content::WebUIDataSource::CreateAndAdd(
  105. browser_context, kChromeUIOSFeedbackHost);
  106. // Add ability to request chrome-untrusted://os-feedback URLs.
  107. web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme);
  108. // We need a CSP override to use the chrome-untrusted:// scheme in the host.
  109. const std::string csp =
  110. std::string("frame-src ") + kChromeUIOSFeedbackUntrustedUrl + ";";
  111. source->OverrideContentSecurityPolicy(
  112. network::mojom::CSPDirectiveName::FrameSrc, csp);
  113. source->OverrideContentSecurityPolicy(
  114. network::mojom::CSPDirectiveName::ScriptSrc,
  115. "script-src chrome://resources chrome://test chrome://webui-test "
  116. "'self';");
  117. source->DisableTrustedTypesCSP();
  118. const auto resources =
  119. base::make_span(kAshOsFeedbackResources, kAshOsFeedbackResourcesSize);
  120. SetUpWebUIDataSource(source, resources, IDR_ASH_OS_FEEDBACK_INDEX_HTML);
  121. AddLocalizedStrings(source);
  122. // Register common permissions for chrome-untrusted:// pages.
  123. // TODO(https://crbug.com/1113568): Remove this after common permissions are
  124. // granted by default.
  125. auto* webui_allowlist = WebUIAllowlist::GetOrCreate(browser_context);
  126. const url::Origin untrusted_origin =
  127. url::Origin::Create(GURL(kChromeUIOSFeedbackUntrustedUrl));
  128. webui_allowlist->RegisterAutoGrantedPermission(
  129. untrusted_origin, ContentSettingsType::JAVASCRIPT);
  130. help_content_provider_ = std::make_unique<feedback::HelpContentProvider>(
  131. feedback_delegate->GetApplicationLocale(), browser_context);
  132. feedback_service_provider_ =
  133. std::make_unique<feedback::FeedbackServiceProvider>(
  134. std::move(feedback_delegate));
  135. }
  136. OSFeedbackUI::~OSFeedbackUI() = default;
  137. void OSFeedbackUI::BindInterface(
  138. mojo::PendingReceiver<os_feedback_ui::mojom::FeedbackServiceProvider>
  139. receiver) {
  140. feedback_service_provider_->BindInterface(std::move(receiver));
  141. }
  142. void OSFeedbackUI::BindInterface(
  143. mojo::PendingReceiver<os_feedback_ui::mojom::HelpContentProvider>
  144. receiver) {
  145. help_content_provider_->BindInterface(std::move(receiver));
  146. }
  147. WEB_UI_CONTROLLER_TYPE_IMPL(OSFeedbackUI)
  148. } // namespace ash