File: onscreen_content_provider.h

package info (click to toggle)
chromium 139.0.7258.127-2
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,122,156 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (144 lines) | stat: -rw-r--r-- 5,810 bytes parent folder | download | duplicates (6)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
// Copyright 2019 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifndef COMPONENTS_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_
#define COMPONENTS_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_

#include <map>
#include <memory>
#include <vector>

#include "base/memory/raw_ptr.h"
#include "base/memory/weak_ptr.h"
#include "components/content_capture/browser/content_capture_frame.h"
#include "components/content_capture/common/content_capture.mojom.h"
#include "content/public/browser/global_routing_id.h"
#include "content/public/browser/web_contents_observer.h"
#include "content/public/browser/web_contents_user_data.h"

namespace content {
class WebContents;
class NavigationEntry;
}  // namespace content

namespace content_capture {

class ContentCaptureReceiver;
class ContentCaptureConsumer;

// This class has an instance per WebContents, it is the base class of
// ContentCaptureReceiverManager implementation which shall overrides the pure
// virtual methods to get the messages from each receivers, this class creates
// the ContentCaptureReceiver and associates it with RenderFrameHost, it also
// binds ContentCaptureReceiver with its peer ContentCaptureSender in renderer.
// The ContentSession here is used to specify which frame the message came from.
class OnscreenContentProvider
    : public content::WebContentsObserver,
      public content::WebContentsUserData<OnscreenContentProvider> {
 public:
  ~OnscreenContentProvider() override;
  static OnscreenContentProvider* Create(content::WebContents* web_contents);

  // Binds the |request| with the |render_frame_host| associated
  // ContentCaptureReceiver.
  static void BindContentCaptureReceiver(
      mojo::PendingAssociatedReceiver<mojom::ContentCaptureReceiver>
          pending_receiver,
      content::RenderFrameHost* render_frame_host);

  void AddConsumer(ContentCaptureConsumer& consumer);
  void RemoveConsumer(ContentCaptureConsumer& consumer);

  // The methods called by ContentCaptureReceiver.
  void FlushCaptureContent(ContentCaptureReceiver* content_capture_receiver,
                           const ContentCaptureFrame& data);
  void DidCaptureContent(ContentCaptureReceiver* content_capture_receiver,
                         const ContentCaptureFrame& data);
  void DidUpdateContent(ContentCaptureReceiver* content_capture_receiver,
                        const ContentCaptureFrame& data);
  void DidRemoveContent(ContentCaptureReceiver* content_capture_receiver,
                        const std::vector<int64_t>& data);
  void DidRemoveSession(ContentCaptureReceiver* content_capture_receiver);
  void DidUpdateTitle(ContentCaptureReceiver* content_capture_receiver);
  void DidUpdateFavicon(ContentCaptureReceiver* content_capture_receiver);

  // content::WebContentsObserver:
  void RenderFrameCreated(content::RenderFrameHost* render_frame_host) override;
  void RenderFrameDeleted(content::RenderFrameHost* render_frame_host) override;
  void ReadyToCommitNavigation(
      content::NavigationHandle* navigation_handle) override;
  void TitleWasSet(content::NavigationEntry* entry) override;
  void DidUpdateFaviconURL(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::FaviconURLPtr>& candidates) override;

  size_t GetFrameMapSizeForTesting() const { return frame_map_.size(); }

  base::WeakPtr<OnscreenContentProvider> GetWeakPtr() {
    return weak_ptr_factory_.GetWeakPtr();
  }

  void NotifyFaviconURLUpdatedForTesting(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::FaviconURLPtr>& candidates) {
    NotifyFaviconURLUpdated(render_frame_host, candidates);
  }

#ifdef UNIT_TEST
  ContentCaptureReceiver* ContentCaptureReceiverForFrameForTesting(
      content::RenderFrameHost* render_frame_host) const {
    return ContentCaptureReceiverForFrame(render_frame_host);
  }

  const std::vector<raw_ptr<ContentCaptureConsumer, VectorExperimental>>&
  GetConsumersForTesting() const {
    return consumers_;
  }
#endif

 private:
  friend class content::WebContentsUserData<OnscreenContentProvider>;
  WEB_CONTENTS_USER_DATA_KEY_DECL();
  using content::WebContentsUserData<
      OnscreenContentProvider>::CreateForWebContents;

  explicit OnscreenContentProvider(content::WebContents* web_contents);

  ContentCaptureReceiver* ContentCaptureReceiverForFrame(
      content::RenderFrameHost* render_frame_host) const;

  // Builds ContentCaptureSession and returns in |session|, |ancestor_only|
  // specifies if only ancestor should be returned in |session|.
  void BuildContentCaptureSession(
      ContentCaptureReceiver* content_capture_receiver,
      bool ancestor_only,
      ContentCaptureSession* session);

  // Builds ContentCaptureSession for |content_capture_receiver| into |session|,
  // return true if succeed, this method returns the session that has been
  // reported and shall be used for removing session.
  bool BuildContentCaptureSessionLastSeen(
      ContentCaptureReceiver* content_capture_receiver,
      ContentCaptureSession* session);

  bool BuildContentCaptureSessionForMainFrame(ContentCaptureSession* session);

  bool ShouldCapture(const GURL& url);

  void NotifyFaviconURLUpdated(
      content::RenderFrameHost* render_frame_host,
      const std::vector<blink::mojom::FaviconURLPtr>& candidates);

  std::map<content::GlobalRenderFrameHostId,
           std::unique_ptr<ContentCaptureReceiver>>
      frame_map_;

  std::vector<raw_ptr<ContentCaptureConsumer, VectorExperimental>> consumers_;

  base::WeakPtrFactory<OnscreenContentProvider> weak_ptr_factory_{this};
};

}  // namespace content_capture

#endif  // COMPONENTS_CONTENT_CAPTURE_BROWSER_ONSCREEN_CONTENT_PROVIDER_H_