help_app_kids_magazine_untrusted_ui.cc 3.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  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/help_app_ui/help_app_kids_magazine_untrusted_ui.h"
  5. #include "ash/webui/help_app_ui/url_constants.h"
  6. #include "base/strings/string_piece.h"
  7. #include "chromeos/grit/chromeos_help_app_kids_magazine_bundle_resources.h"
  8. #include "chromeos/grit/chromeos_help_app_kids_magazine_bundle_resources_map.h"
  9. #include "content/public/browser/web_contents.h"
  10. #include "content/public/browser/web_ui.h"
  11. #include "content/public/browser/web_ui_data_source.h"
  12. #include "content/public/common/url_constants.h"
  13. namespace ash {
  14. namespace {
  15. const char kKidsMagazinePathPrefix[] = "kids_magazine/";
  16. // Function to remove a prefix from an input string. Does nothing if the string
  17. // does not begin with the prefix.
  18. base::StringPiece StripPrefix(base::StringPiece input,
  19. base::StringPiece prefix) {
  20. if (input.find(prefix) == 0) {
  21. return input.substr(prefix.size());
  22. }
  23. return input;
  24. }
  25. content::WebUIDataSource* CreateHelpAppKidsMagazineUntrustedDataSource() {
  26. content::WebUIDataSource* source = content::WebUIDataSource::Create(
  27. kChromeUIHelpAppKidsMagazineUntrustedURL);
  28. // Set index.html as the default resource.
  29. source->SetDefaultResource(IDR_HELP_APP_KIDS_MAGAZINE_INDEX_HTML);
  30. source->DisableTrustedTypesCSP();
  31. for (size_t i = 0; i < kChromeosHelpAppKidsMagazineBundleResourcesSize; i++) {
  32. // While the JS and CSS file are stored in /kids_magazine/static/..., the
  33. // HTML file references /static/... directly. We need to strip the
  34. // "kids_magazine" prefix from the path.
  35. source->AddResourcePath(
  36. StripPrefix(kChromeosHelpAppKidsMagazineBundleResources[i].path,
  37. kKidsMagazinePathPrefix),
  38. kChromeosHelpAppKidsMagazineBundleResources[i].id);
  39. }
  40. // Add chrome://help-app and chrome-untrusted://help-app as frame ancestors.
  41. source->AddFrameAncestor(GURL(kChromeUIHelpAppURL));
  42. source->AddFrameAncestor(GURL(kChromeUIHelpAppUntrustedURL));
  43. source->OverrideContentSecurityPolicy(
  44. network::mojom::CSPDirectiveName::DefaultSrc, "");
  45. source->OverrideContentSecurityPolicy(
  46. network::mojom::CSPDirectiveName::ScriptSrc,
  47. "script-src 'self' https://www.gstatic.com;");
  48. return source;
  49. }
  50. } // namespace
  51. HelpAppKidsMagazineUntrustedUIConfig::HelpAppKidsMagazineUntrustedUIConfig()
  52. : WebUIConfig(content::kChromeUIUntrustedScheme,
  53. kChromeUIHelpAppKidsMagazineHost) {}
  54. HelpAppKidsMagazineUntrustedUIConfig::~HelpAppKidsMagazineUntrustedUIConfig() =
  55. default;
  56. std::unique_ptr<content::WebUIController>
  57. HelpAppKidsMagazineUntrustedUIConfig::CreateWebUIController(
  58. content::WebUI* web_ui) {
  59. return std::make_unique<HelpAppKidsMagazineUntrustedUI>(web_ui);
  60. }
  61. HelpAppKidsMagazineUntrustedUI::HelpAppKidsMagazineUntrustedUI(
  62. content::WebUI* web_ui)
  63. : ui::UntrustedWebUIController(web_ui) {
  64. content::WebUIDataSource* untrusted_source =
  65. CreateHelpAppKidsMagazineUntrustedDataSource();
  66. auto* browser_context = web_ui->GetWebContents()->GetBrowserContext();
  67. content::WebUIDataSource::Add(browser_context, untrusted_source);
  68. }
  69. HelpAppKidsMagazineUntrustedUI::~HelpAppKidsMagazineUntrustedUI() = default;
  70. } // namespace ash