sample_system_web_app_untrusted_ui.cc 2.4 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364
  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/sample_system_web_app_ui/sample_system_web_app_untrusted_ui.h"
  5. #include "ash/webui/grit/ash_sample_system_web_app_untrusted_resources_map.h"
  6. #include "ash/webui/sample_system_web_app_ui/sample_system_web_app_ui.h"
  7. #include "ash/webui/sample_system_web_app_ui/url_constants.h"
  8. #include "content/public/browser/web_contents.h"
  9. #include "content/public/browser/web_ui.h"
  10. #include "content/public/browser/web_ui_data_source.h"
  11. #include "content/public/common/url_constants.h"
  12. #include "url/gurl.h"
  13. namespace ash {
  14. SampleSystemWebAppUntrustedUI::SampleSystemWebAppUntrustedUI(
  15. content::WebUI* web_ui)
  16. : ui::UntrustedWebUIController(web_ui) {
  17. content::WebUIDataSource* untrusted_source =
  18. content::WebUIDataSource::CreateAndAdd(
  19. web_ui->GetWebContents()->GetBrowserContext(),
  20. kChromeUISampleSystemWebAppUntrustedURL);
  21. untrusted_source->AddResourcePaths(
  22. base::make_span(kAshSampleSystemWebAppUntrustedResources,
  23. kAshSampleSystemWebAppUntrustedResourcesSize));
  24. untrusted_source->AddFrameAncestor(GURL(kChromeUISampleSystemWebAppURL));
  25. }
  26. SampleSystemWebAppUntrustedUI::~SampleSystemWebAppUntrustedUI() = default;
  27. void SampleSystemWebAppUntrustedUI::BindInterface(
  28. mojo::PendingReceiver<mojom::sample_swa::UntrustedPageInterfacesFactory>
  29. factory) {
  30. if (untrusted_page_factory_.is_bound())
  31. untrusted_page_factory_.reset();
  32. untrusted_page_factory_.Bind(std::move(factory));
  33. }
  34. void SampleSystemWebAppUntrustedUI::CreateParentPage(
  35. mojo::PendingRemote<mojom::sample_swa::ChildUntrustedPage>
  36. child_untrusted_page,
  37. mojo::PendingReceiver<mojom::sample_swa::ParentTrustedPage>
  38. parent_trusted_page) {
  39. // Find the parent frame's controller.
  40. auto* chrome_frame = web_ui()->GetWebContents()->GetPrimaryMainFrame();
  41. if (!chrome_frame)
  42. return;
  43. CHECK(chrome_frame->GetWebUI());
  44. auto* sample_ui_controller =
  45. chrome_frame->GetWebUI()->GetController()->GetAs<SampleSystemWebAppUI>();
  46. CHECK(sample_ui_controller);
  47. sample_ui_controller->CreateParentPage(std::move(child_untrusted_page),
  48. std::move(parent_trusted_page));
  49. }
  50. WEB_UI_CONTROLLER_TYPE_IMPL(SampleSystemWebAppUntrustedUI)
  51. } // namespace ash