js_to_browser_messaging.h 2.5 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273
  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. #ifndef COMPONENTS_JS_INJECTION_BROWSER_JS_TO_BROWSER_MESSAGING_H_
  5. #define COMPONENTS_JS_INJECTION_BROWSER_JS_TO_BROWSER_MESSAGING_H_
  6. #include <string>
  7. #include <vector>
  8. #include "base/check.h"
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/js_injection/common/interfaces.mojom.h"
  11. #include "components/js_injection/common/origin_matcher.h"
  12. #include "mojo/public/cpp/bindings/associated_receiver_set.h"
  13. #include "mojo/public/cpp/bindings/associated_remote.h"
  14. #include "mojo/public/cpp/bindings/pending_associated_receiver.h"
  15. #include "mojo/public/cpp/bindings/pending_associated_remote.h"
  16. #include "third_party/blink/public/common/messaging/message_port_descriptor.h"
  17. namespace content {
  18. class RenderFrameHost;
  19. }
  20. namespace js_injection {
  21. class WebMessageHost;
  22. class WebMessageHostFactory;
  23. // Implementation of mojo::JsToBrowserMessaging interface. Receives
  24. // PostMessage() call from renderer JsBinding.
  25. //
  26. // This object is destroyed when the associated RenderFrameHost is destroyed.
  27. class JsToBrowserMessaging : public mojom::JsToBrowserMessaging {
  28. public:
  29. JsToBrowserMessaging(
  30. content::RenderFrameHost* rfh,
  31. mojo::PendingAssociatedReceiver<mojom::JsToBrowserMessaging> receiver,
  32. WebMessageHostFactory* factory,
  33. const OriginMatcher& origin_matcher);
  34. JsToBrowserMessaging(const JsToBrowserMessaging&) = delete;
  35. JsToBrowserMessaging& operator=(const JsToBrowserMessaging&) = delete;
  36. ~JsToBrowserMessaging() override;
  37. void OnBackForwardCacheStateChanged();
  38. // mojom::JsToBrowserMessaging implementation.
  39. void PostMessage(const std::u16string& message,
  40. std::vector<blink::MessagePortDescriptor> ports) override;
  41. void SetBrowserToJsMessaging(
  42. mojo::PendingAssociatedRemote<mojom::BrowserToJsMessaging>
  43. java_to_js_messaging) override;
  44. private:
  45. class ReplyProxyImpl;
  46. raw_ptr<content::RenderFrameHost> render_frame_host_;
  47. std::unique_ptr<ReplyProxyImpl> reply_proxy_;
  48. raw_ptr<WebMessageHostFactory> connection_factory_;
  49. OriginMatcher origin_matcher_;
  50. mojo::AssociatedReceiver<mojom::JsToBrowserMessaging> receiver_{this};
  51. std::unique_ptr<WebMessageHost> host_;
  52. #if DCHECK_IS_ON()
  53. std::string origin_string_;
  54. bool is_main_frame_;
  55. #endif
  56. };
  57. } // namespace js_injection
  58. #endif // COMPONENTS_JS_INJECTION_BROWSER_JS_TO_BROWSER_MESSAGING_H_