web_frames_manager_java_script_feature.h 3.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  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 IOS_WEB_JS_MESSAGING_WEB_FRAMES_MANAGER_JAVA_SCRIPT_FEATURE_H_
  5. #define IOS_WEB_JS_MESSAGING_WEB_FRAMES_MANAGER_JAVA_SCRIPT_FEATURE_H_
  6. #import <WebKit/WebKit.h>
  7. #include "base/memory/weak_ptr.h"
  8. #include "base/supports_user_data.h"
  9. #import "ios/web/js_messaging/scoped_wk_script_message_handler.h"
  10. #import "ios/web/public/js_messaging/java_script_feature.h"
  11. namespace web {
  12. class BrowserState;
  13. class WebState;
  14. // A feature which notifies the native application code of the creation and
  15. // destruction of webpage frames based on JavaScript messages from the webpage.
  16. class WebFramesManagerJavaScriptFeature : public base::SupportsUserData::Data,
  17. public JavaScriptFeature {
  18. public:
  19. WebFramesManagerJavaScriptFeature(BrowserState* browser_state);
  20. ~WebFramesManagerJavaScriptFeature() override;
  21. // Returns the WebFramesManagerJavaScriptFeature associated with
  22. // |browser_state|, creating one if necessary. |browser_state| must not be
  23. // null.
  24. static WebFramesManagerJavaScriptFeature* FromBrowserState(
  25. BrowserState* browser_state);
  26. // Broadcasts a (not encrypted) JavaScript message to get the identifiers
  27. // and keys of existing frames.
  28. void RegisterExistingFrames(WebState* web_state);
  29. // Configures message handlers for the creation and destruction of frames.
  30. // |user_content_controller| is used directly (instead of using the built-in
  31. // JavaScriptFeature message handling) because constructing WebFrame instances
  32. // requires access to the WKScriptMessage's WKFrameInfo instance.
  33. void ConfigureHandlers(WKUserContentController* user_content_controller);
  34. private:
  35. friend class WebFramesManagerJavaScriptFeatureTest;
  36. WebFramesManagerJavaScriptFeature(const WebFramesManagerJavaScriptFeature&) =
  37. delete;
  38. WebFramesManagerJavaScriptFeature& operator=(
  39. const WebFramesManagerJavaScriptFeature&) = delete;
  40. // Handles a message from JavaScript to register a new WebFrame.
  41. void FrameAvailableMessageReceived(WKScriptMessage* script_message);
  42. // Handles a message from JavaScript to remove a WebFrame.
  43. void FrameUnavailableMessageReceived(WKScriptMessage* script_message);
  44. // The browser state associated with this instance of the feature.
  45. BrowserState* browser_state_;
  46. // This feature uses ScopedWKScriptMessageHandler directly instead of the
  47. // message handling built into JavaScriptFeature because creating WebFrames
  48. // requires a pointer to the WKFrameInfo object (which is intentionally hidden
  49. // from JavaScriptFeature::ScriptMessageReceived).
  50. std::unique_ptr<ScopedWKScriptMessageHandler> frame_available_handler_;
  51. std::unique_ptr<ScopedWKScriptMessageHandler> frame_unavailable_handler_;
  52. base::WeakPtrFactory<WebFramesManagerJavaScriptFeature> weak_factory_;
  53. };
  54. } // namespace web
  55. #endif // IOS_WEB_JS_MESSAGING_WEB_FRAMES_MANAGER_JAVA_SCRIPT_FEATURE_H_