sample_page_handler.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  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. #ifndef ASH_WEBUI_SAMPLE_SYSTEM_WEB_APP_UI_SAMPLE_PAGE_HANDLER_H_
  5. #define ASH_WEBUI_SAMPLE_SYSTEM_WEB_APP_UI_SAMPLE_PAGE_HANDLER_H_
  6. #include <memory>
  7. #include "ash/webui/sample_system_web_app_ui/mojom/sample_system_web_app_ui.mojom.h"
  8. #include "base/memory/weak_ptr.h"
  9. #include "base/one_shot_event.h"
  10. #include "mojo/public/cpp/bindings/pending_receiver.h"
  11. #include "mojo/public/cpp/bindings/pending_remote.h"
  12. #include "mojo/public/cpp/bindings/receiver.h"
  13. #include "mojo/public/cpp/bindings/remote.h"
  14. #include "mojo/public/mojom/base/string16.mojom.h"
  15. namespace ash {
  16. // Implements the PageHandler interface. For illustration purposes this
  17. // class also interacts with a remote Page. There are five examples of
  18. // interaction.
  19. // 1. The UI fetches "preferences" (an example of "get")
  20. // 2. The UI sends a message, (an example of "set")
  21. // 3. The UI asks the PageHandler to execute an action (DoSomething).
  22. // 4. The PageHandler notifies the page about an event.
  23. class PageHandler : public mojom::sample_swa::PageHandler {
  24. public:
  25. PageHandler();
  26. ~PageHandler() override;
  27. PageHandler(const PageHandler&) = delete;
  28. PageHandler& operator=(const PageHandler&) = delete;
  29. // Called when a child page wants to bind its interface in `page_`, so they
  30. // can communicate via Mojo.
  31. void CreateParentPage(
  32. mojo::PendingRemote<mojom::sample_swa::ChildUntrustedPage>
  33. child_untrusted_page,
  34. mojo::PendingReceiver<mojom::sample_swa::ParentTrustedPage>
  35. parent_trusted_page);
  36. void BindInterface(
  37. mojo::PendingReceiver<mojom::sample_swa::PageHandler> pending_receiver,
  38. mojo::PendingRemote<mojom::sample_swa::Page> pending_page);
  39. private:
  40. // Shows how the page can retrieve information from the browser process.
  41. void GetPreferences(GetPreferencesCallback callback) override;
  42. // Shows how the page can send information to the browser process.
  43. void Send(const std::string& message) override;
  44. // Handles the page requesting an action from the controller.
  45. void DoSomething() override;
  46. // Called as a reaction to DoSomething; invoked when DoSomething is done.
  47. void OnSomethingDone();
  48. // Called after `page_` is bound. This requests `page_` to bind child page's
  49. // Mojo pipes in JavaScript.
  50. void BindChildPageInJavaScript(
  51. mojo::PendingRemote<mojom::sample_swa::ChildUntrustedPage>
  52. child_trusted_page,
  53. mojo::PendingReceiver<mojom::sample_swa::ParentTrustedPage>
  54. parent_trusted_page);
  55. mojo::Receiver<mojom::sample_swa::PageHandler> receiver_{this};
  56. mojo::Remote<mojom::sample_swa::Page> page_;
  57. std::string message_;
  58. // The event signaling page handler is bound. This is the earliest time `this`
  59. // can send Mojo messages to JavaScript.
  60. base::OneShotEvent on_page_handler_bound_;
  61. base::WeakPtrFactory<PageHandler> weak_ptr_factory_{this};
  62. };
  63. } // namespace ash
  64. #endif // ASH_WEBUI_SAMPLE_SYSTEM_WEB_APP_UI_SAMPLE_PAGE_HANDLER_H_