web_message_host_factory_wrapper.cc 3.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990
  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 "weblayer/browser/js_communication/web_message_host_factory_wrapper.h"
  5. #include "base/memory/raw_ptr.h"
  6. #include "components/js_injection/browser/web_message.h"
  7. #include "components/js_injection/browser/web_message_host.h"
  8. #include "components/js_injection/browser/web_message_reply_proxy.h"
  9. #include "content/public/browser/page.h"
  10. #include "content/public/browser/render_frame_host.h"
  11. #include "weblayer/browser/page_impl.h"
  12. #include "weblayer/public/js_communication/web_message.h"
  13. #include "weblayer/public/js_communication/web_message_host.h"
  14. #include "weblayer/public/js_communication/web_message_host_factory.h"
  15. #include "weblayer/public/js_communication/web_message_reply_proxy.h"
  16. namespace weblayer {
  17. namespace {
  18. // An implementation of js_injection::WebMessageHost that delegates to the
  19. // corresponding WebLayer type. This also serves as the WebMessageReplyProxy
  20. // implementation, which forwards to the js_injection implementation.
  21. class WebMessageHostWrapper : public js_injection::WebMessageHost,
  22. public WebMessageReplyProxy {
  23. public:
  24. WebMessageHostWrapper(weblayer::WebMessageHostFactory* factory,
  25. const std::string& origin_string,
  26. bool is_main_frame,
  27. js_injection::WebMessageReplyProxy* proxy)
  28. : proxy_(proxy),
  29. connection_(factory->CreateHost(origin_string, is_main_frame, this)) {}
  30. // js_injection::WebMessageHost:
  31. void OnPostMessage(
  32. std::unique_ptr<js_injection::WebMessage> message) override {
  33. std::unique_ptr<WebMessage> m = std::make_unique<WebMessage>();
  34. m->message = message->message;
  35. connection_->OnPostMessage(std::move(m));
  36. }
  37. void OnBackForwardCacheStateChanged() override {
  38. connection_->OnBackForwardCacheStateChanged();
  39. }
  40. // WebMessageReplyProxy:
  41. void PostWebMessage(std::unique_ptr<WebMessage> message) override {
  42. std::unique_ptr<js_injection::WebMessage> w =
  43. std::make_unique<js_injection::WebMessage>();
  44. w->message = std::move(message->message);
  45. proxy_->PostWebMessage(std::move(w));
  46. }
  47. bool IsInBackForwardCache() override {
  48. return proxy_->IsInBackForwardCache();
  49. }
  50. Page& GetPage() override {
  51. // In general WebLayer avoids exposing child frames. As such, GetPage()
  52. // returns the Page of the main frame.
  53. PageImpl* page =
  54. PageImpl::GetForPage(proxy_->GetPage().GetMainDocument().GetPage());
  55. // NavigationControllerImpl creates the PageImpl when navigation finishes so
  56. // that by the time this is called the Page should have been created.
  57. DCHECK(page);
  58. return *page;
  59. }
  60. private:
  61. raw_ptr<js_injection::WebMessageReplyProxy> proxy_;
  62. std::unique_ptr<weblayer::WebMessageHost> connection_;
  63. };
  64. } // namespace
  65. WebMessageHostFactoryWrapper::WebMessageHostFactoryWrapper(
  66. std::unique_ptr<weblayer::WebMessageHostFactory> factory)
  67. : factory_(std::move(factory)) {}
  68. WebMessageHostFactoryWrapper::~WebMessageHostFactoryWrapper() = default;
  69. std::unique_ptr<js_injection::WebMessageHost>
  70. WebMessageHostFactoryWrapper::CreateHost(
  71. const std::string& origin_string,
  72. bool is_main_frame,
  73. js_injection::WebMessageReplyProxy* proxy) {
  74. auto wrapper = std::make_unique<WebMessageHostWrapper>(
  75. factory_.get(), origin_string, is_main_frame, proxy);
  76. return wrapper;
  77. }
  78. } // namespace weblayer