js_communication_host.h 4.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117
  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_COMMUNICATION_HOST_H_
  5. #define COMPONENTS_JS_INJECTION_BROWSER_JS_COMMUNICATION_HOST_H_
  6. #include <memory>
  7. #include <string>
  8. #include <vector>
  9. #include "base/memory/raw_ptr.h"
  10. #include "components/js_injection/common/interfaces.mojom.h"
  11. #include "content/public/browser/global_routing_id.h"
  12. #include "content/public/browser/web_contents_observer.h"
  13. #include "third_party/abseil-cpp/absl/types/optional.h"
  14. namespace content {
  15. class RenderFrameHost;
  16. } // namespace content
  17. namespace js_injection {
  18. class OriginMatcher;
  19. struct DocumentStartJavaScript;
  20. struct JsObject;
  21. class JsToBrowserMessaging;
  22. class WebMessageHostFactory;
  23. // This class is 1:1 with WebContents, when AddWebMessageListener() is called,
  24. // it stores the information in this class and send them to renderer side
  25. // JsCommunication if there is any. When RenderFrameCreated() gets called, it
  26. // needs to configure that new RenderFrame with the information stores in this
  27. // class.
  28. class JsCommunicationHost : public content::WebContentsObserver {
  29. public:
  30. explicit JsCommunicationHost(content::WebContents* web_contents);
  31. JsCommunicationHost(const JsCommunicationHost&) = delete;
  32. JsCommunicationHost& operator=(const JsCommunicationHost&) = delete;
  33. ~JsCommunicationHost() override;
  34. // Captures the result of adding script. There are two possibilities when
  35. // adding script: there was an error, in which case |error_message| is set,
  36. // otherwise the add was successful and |script_id| is set.
  37. struct AddScriptResult {
  38. AddScriptResult();
  39. AddScriptResult(const AddScriptResult&);
  40. AddScriptResult& operator=(const AddScriptResult&);
  41. ~AddScriptResult();
  42. absl::optional<std::string> error_message;
  43. absl::optional<int> script_id;
  44. };
  45. // Native side AddDocumentStartJavaScript, returns an error message if the
  46. // parameters didn't pass necessary checks.
  47. AddScriptResult AddDocumentStartJavaScript(
  48. const std::u16string& script,
  49. const std::vector<std::string>& allowed_origin_rules);
  50. bool RemoveDocumentStartJavaScript(int script_id);
  51. // Adds a new WebMessageHostFactory. For any urls that match
  52. // |allowed_origin_rules|, |js_object_name| is registered as a JS object that
  53. // can be used by script on the page to send and receive messages. Returns
  54. // an empty string on success. On failure, the return string gives the error
  55. // message.
  56. std::u16string AddWebMessageHostFactory(
  57. std::unique_ptr<WebMessageHostFactory> factory,
  58. const std::u16string& js_object_name,
  59. const std::vector<std::string>& allowed_origin_rules);
  60. // Returns the factory previously registered under the specified name.
  61. void RemoveWebMessageHostFactory(const std::u16string& js_object_name);
  62. struct RegisteredFactory {
  63. std::u16string js_name;
  64. OriginMatcher allowed_origin_rules;
  65. raw_ptr<WebMessageHostFactory> factory = nullptr;
  66. };
  67. // Returns the registered factories.
  68. std::vector<RegisteredFactory> GetWebMessageHostFactories();
  69. // content::WebContentsObserver implementations
  70. void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
  71. void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
  72. void RenderFrameHostStateChanged(
  73. content::RenderFrameHost* render_frame_host,
  74. content::RenderFrameHost::LifecycleState old_state,
  75. content::RenderFrameHost::LifecycleState new_state) override;
  76. private:
  77. void NotifyFrameForWebMessageListener(
  78. content::RenderFrameHost* render_frame_host);
  79. void NotifyFrameForAllDocumentStartJavaScripts(
  80. content::RenderFrameHost* render_frame_host);
  81. void NotifyFrameForAddDocumentStartJavaScript(
  82. const DocumentStartJavaScript* script,
  83. content::RenderFrameHost* render_frame_host);
  84. void NotifyFrameForRemoveDocumentStartJavaScript(
  85. int32_t script_id,
  86. content::RenderFrameHost* render_frame_host);
  87. int32_t next_script_id_ = 0;
  88. std::vector<DocumentStartJavaScript> scripts_;
  89. std::vector<std::unique_ptr<JsObject>> js_objects_;
  90. std::map<content::GlobalRenderFrameHostId,
  91. std::vector<std::unique_ptr<JsToBrowserMessaging>>>
  92. js_to_browser_messagings_;
  93. };
  94. } // namespace js_injection
  95. #endif // COMPONENTS_JS_INJECTION_BROWSER_JS_COMMUNICATION_HOST_H_