onscreen_content_provider.h 5.5 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  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_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_
  5. #define COMPONENTS_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_
  6. #include <map>
  7. #include <memory>
  8. #include <vector>
  9. #include "base/memory/weak_ptr.h"
  10. #include "components/content_capture/browser/content_capture_frame.h"
  11. #include "components/content_capture/common/content_capture.mojom.h"
  12. #include "content/public/browser/global_routing_id.h"
  13. #include "content/public/browser/web_contents_observer.h"
  14. #include "content/public/browser/web_contents_user_data.h"
  15. namespace content {
  16. class WebContents;
  17. class NavigationEntry;
  18. } // namespace content
  19. namespace content_capture {
  20. class ContentCaptureReceiver;
  21. class ContentCaptureConsumer;
  22. // This class has an instance per WebContents, it is the base class of
  23. // ContentCaptureReceiverManager implementation which shall overrides the pure
  24. // virtual methods to get the messages from each receivers, this class creates
  25. // the ContentCaptureReceiver and associates it with RenderFrameHost, it also
  26. // binds ContentCaptureReceiver with its peer ContentCaptureSender in renderer.
  27. // The ContentSession here is used to specify which frame the message came from.
  28. class OnscreenContentProvider
  29. : public content::WebContentsObserver,
  30. public content::WebContentsUserData<OnscreenContentProvider> {
  31. public:
  32. ~OnscreenContentProvider() override;
  33. static OnscreenContentProvider* Create(content::WebContents* web_contents);
  34. // Binds the |request| with the |render_frame_host| associated
  35. // ContentCaptureReceiver.
  36. static void BindContentCaptureReceiver(
  37. mojo::PendingAssociatedReceiver<mojom::ContentCaptureReceiver>
  38. pending_receiver,
  39. content::RenderFrameHost* render_frame_host);
  40. void AddConsumer(ContentCaptureConsumer& consumer);
  41. void RemoveConsumer(ContentCaptureConsumer& consumer);
  42. // The methods called by ContentCaptureReceiver.
  43. void DidCaptureContent(ContentCaptureReceiver* content_capture_receiver,
  44. const ContentCaptureFrame& data);
  45. void DidUpdateContent(ContentCaptureReceiver* content_capture_receiver,
  46. const ContentCaptureFrame& data);
  47. void DidRemoveContent(ContentCaptureReceiver* content_capture_receiver,
  48. const std::vector<int64_t>& data);
  49. void DidRemoveSession(ContentCaptureReceiver* content_capture_receiver);
  50. void DidUpdateTitle(ContentCaptureReceiver* content_capture_receiver);
  51. void DidUpdateFavicon(ContentCaptureReceiver* content_capture_receiver);
  52. // content::WebContentsObserver:
  53. void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
  54. void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
  55. void ReadyToCommitNavigation(
  56. content::NavigationHandle* navigation_handle) override;
  57. void TitleWasSet(content::NavigationEntry* entry) override;
  58. void DidUpdateFaviconURL(
  59. content::RenderFrameHost* render_frame_host,
  60. const std::vector<blink::mojom::FaviconURLPtr>& candidates) override;
  61. size_t GetFrameMapSizeForTesting() const { return frame_map_.size(); }
  62. base::WeakPtr<OnscreenContentProvider> GetWeakPtr() {
  63. return weak_ptr_factory_.GetWeakPtr();
  64. }
  65. void NotifyFaviconURLUpdatedForTesting(
  66. content::RenderFrameHost* render_frame_host,
  67. const std::vector<blink::mojom::FaviconURLPtr>& candidates) {
  68. NotifyFaviconURLUpdated(render_frame_host, candidates);
  69. }
  70. #ifdef UNIT_TEST
  71. ContentCaptureReceiver* ContentCaptureReceiverForFrameForTesting(
  72. content::RenderFrameHost* render_frame_host) const {
  73. return ContentCaptureReceiverForFrame(render_frame_host);
  74. }
  75. const std::vector<ContentCaptureConsumer*>& GetConsumersForTesting() const {
  76. return consumers_;
  77. }
  78. #endif
  79. private:
  80. friend class content::WebContentsUserData<OnscreenContentProvider>;
  81. WEB_CONTENTS_USER_DATA_KEY_DECL();
  82. using content::WebContentsUserData<
  83. OnscreenContentProvider>::CreateForWebContents;
  84. explicit OnscreenContentProvider(content::WebContents* web_contents);
  85. ContentCaptureReceiver* ContentCaptureReceiverForFrame(
  86. content::RenderFrameHost* render_frame_host) const;
  87. // Builds ContentCaptureSession and returns in |session|, |ancestor_only|
  88. // specifies if only ancestor should be returned in |session|.
  89. void BuildContentCaptureSession(
  90. ContentCaptureReceiver* content_capture_receiver,
  91. bool ancestor_only,
  92. ContentCaptureSession* session);
  93. // Builds ContentCaptureSession for |content_capture_receiver| into |session|,
  94. // return true if succeed, this method returns the session that has been
  95. // reported and shall be used for removing session.
  96. bool BuildContentCaptureSessionLastSeen(
  97. ContentCaptureReceiver* content_capture_receiver,
  98. ContentCaptureSession* session);
  99. bool BuildContentCaptureSessionForMainFrame(ContentCaptureSession* session);
  100. bool ShouldCapture(const GURL& url);
  101. void NotifyFaviconURLUpdated(
  102. content::RenderFrameHost* render_frame_host,
  103. const std::vector<blink::mojom::FaviconURLPtr>& candidates);
  104. std::map<content::GlobalRenderFrameHostId,
  105. std::unique_ptr<ContentCaptureReceiver>>
  106. frame_map_;
  107. std::vector<ContentCaptureConsumer*> consumers_;
  108. base::WeakPtrFactory<OnscreenContentProvider> weak_ptr_factory_{this};
  109. };
  110. } // namespace content_capture
  111. #endif // COMPONENTS_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_