help_app_ui.cc 6.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. // Copyright 2019 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/help_app_ui/help_app_ui.h"
  5. #include <utility>
  6. #include "ash/constants/ash_features.h"
  7. #include "ash/webui/grit/ash_help_app_resources.h"
  8. #include "ash/webui/help_app_ui/help_app_manager.h"
  9. #include "ash/webui/help_app_ui/help_app_manager_factory.h"
  10. #include "ash/webui/help_app_ui/help_app_page_handler.h"
  11. #include "ash/webui/help_app_ui/help_app_untrusted_ui.h"
  12. #include "ash/webui/help_app_ui/search/search_handler.h"
  13. #include "ash/webui/help_app_ui/url_constants.h"
  14. #include "ash/webui/web_applications/webui_test_prod_util.h"
  15. #include "chromeos/ash/components/local_search_service/public/cpp/local_search_service_proxy.h"
  16. #include "chromeos/ash/components/local_search_service/public/cpp/local_search_service_proxy_factory.h"
  17. #include "chromeos/ash/components/local_search_service/public/mojom/types.mojom.h"
  18. #include "chromeos/constants/chromeos_features.h"
  19. #include "chromeos/strings/grit/chromeos_strings.h"
  20. #include "components/content_settings/core/common/content_settings_types.h"
  21. #include "content/public/browser/web_contents.h"
  22. #include "content/public/browser/web_ui.h"
  23. #include "content/public/browser/web_ui_data_source.h"
  24. #include "content/public/common/url_constants.h"
  25. #include "services/network/public/mojom/content_security_policy.mojom.h"
  26. #include "ui/webui/webui_allowlist.h"
  27. namespace ash {
  28. namespace {
  29. content::WebUIDataSource* CreateHostDataSource() {
  30. auto* source = content::WebUIDataSource::Create(kChromeUIHelpAppHost);
  31. // TODO(b/218419680): Remove index_dark_light_html when the dark/light flag is
  32. // no longer needed.
  33. if (chromeos::features::IsDarkLightModeEnabled()) {
  34. source->SetDefaultResource(IDR_HELP_APP_HOST_INDEX_DARK_LIGHT_HTML);
  35. } else {
  36. source->SetDefaultResource(IDR_HELP_APP_HOST_INDEX_HTML);
  37. }
  38. source->AddResourcePath("app_icon_192.png", IDR_HELP_APP_ICON_192);
  39. source->AddResourcePath("app_icon_512.png", IDR_HELP_APP_ICON_512);
  40. source->AddResourcePath("browser_proxy.js", IDR_HELP_APP_BROWSER_PROXY_JS);
  41. source->AddLocalizedString("appTitle", IDS_HELP_APP_EXPLORE);
  42. return source;
  43. }
  44. } // namespace
  45. HelpAppUI::HelpAppUI(content::WebUI* web_ui,
  46. std::unique_ptr<HelpAppUIDelegate> delegate)
  47. : MojoWebUIController(web_ui), delegate_(std::move(delegate)) {
  48. content::BrowserContext* browser_context =
  49. web_ui->GetWebContents()->GetBrowserContext();
  50. content::WebUIDataSource* host_source = CreateHostDataSource();
  51. content::WebUIDataSource::Add(browser_context, host_source);
  52. // We need a CSP override to use the chrome-untrusted:// scheme in the host.
  53. std::string csp =
  54. std::string("frame-src ") + kChromeUIHelpAppUntrustedURL + ";";
  55. host_source->OverrideContentSecurityPolicy(
  56. network::mojom::CSPDirectiveName::FrameSrc, csp);
  57. // Add ability to request chrome-untrusted: URLs.
  58. web_ui->AddRequestableScheme(content::kChromeUIUntrustedScheme);
  59. // Register common permissions for chrome-untrusted:// pages.
  60. // TODO(https://crbug.com/1113568): Remove this after common permissions are
  61. // granted by default.
  62. auto* permissions_allowlist = WebUIAllowlist::GetOrCreate(browser_context);
  63. const url::Origin untrusted_origin =
  64. url::Origin::Create(GURL(kChromeUIHelpAppUntrustedURL));
  65. permissions_allowlist->RegisterAutoGrantedPermissions(
  66. untrusted_origin, {
  67. ContentSettingsType::COOKIES,
  68. ContentSettingsType::IMAGES,
  69. ContentSettingsType::JAVASCRIPT,
  70. ContentSettingsType::SOUND,
  71. });
  72. if (MaybeConfigureTestableDataSource(host_source)) {
  73. host_source->OverrideContentSecurityPolicy(
  74. network::mojom::CSPDirectiveName::TrustedTypes,
  75. std::string("trusted-types test-harness;"));
  76. }
  77. // Register common permissions for chrome-untrusted:// pages.
  78. // TODO(https://crbug.com/1113568): Remove this after common permissions are
  79. // granted by default.
  80. auto* magazine_permissions_allowlist =
  81. WebUIAllowlist::GetOrCreate(browser_context);
  82. const url::Origin magazine_untrusted_origin =
  83. url::Origin::Create(GURL(kChromeUIHelpAppKidsMagazineUntrustedURL));
  84. magazine_permissions_allowlist->RegisterAutoGrantedPermissions(
  85. magazine_untrusted_origin, {
  86. ContentSettingsType::COOKIES,
  87. ContentSettingsType::IMAGES,
  88. ContentSettingsType::JAVASCRIPT,
  89. ContentSettingsType::SOUND,
  90. });
  91. }
  92. HelpAppUI::~HelpAppUI() = default;
  93. void HelpAppUI::BindInterface(
  94. mojo::PendingReceiver<help_app::mojom::PageHandlerFactory> receiver) {
  95. page_factory_receiver_.reset();
  96. page_factory_receiver_.Bind(std::move(receiver));
  97. }
  98. void HelpAppUI::BindInterface(
  99. mojo::PendingReceiver<chromeos::local_search_service::mojom::Index>
  100. index_receiver) {
  101. if (base::FeatureList::IsEnabled(features::kEnableLocalSearchService)) {
  102. auto* const factory = chromeos::local_search_service::
  103. LocalSearchServiceProxyFactory::GetForBrowserContext(
  104. web_ui()->GetWebContents()->GetBrowserContext());
  105. factory->SetLocalState(delegate_->GetLocalState());
  106. factory->GetIndex(chromeos::local_search_service::IndexId::kHelpApp,
  107. chromeos::local_search_service::Backend::kInvertedIndex,
  108. std::move(index_receiver));
  109. }
  110. }
  111. void HelpAppUI::BindInterface(
  112. mojo::PendingReceiver<help_app::mojom::SearchHandler> receiver) {
  113. if (base::FeatureList::IsEnabled(features::kHelpAppLauncherSearch)) {
  114. help_app::HelpAppManagerFactory::GetForBrowserContext(
  115. web_ui()->GetWebContents()->GetBrowserContext())
  116. ->search_handler()
  117. ->BindInterface(std::move(receiver));
  118. }
  119. }
  120. void HelpAppUI::CreatePageHandler(
  121. mojo::PendingReceiver<help_app::mojom::PageHandler> receiver) {
  122. page_handler_ =
  123. std::make_unique<HelpAppPageHandler>(this, std::move(receiver));
  124. }
  125. bool HelpAppUI::IsJavascriptErrorReportingEnabled() {
  126. // JavaScript errors are reported via CrashReportPrivate.reportError. Don't
  127. // send duplicate reports via WebUI.
  128. return false;
  129. }
  130. WEB_UI_CONTROLLER_TYPE_IMPL(HelpAppUI)
  131. } // namespace ash