File: page_text_agent.h

package info (click to toggle)
chromium 138.0.7204.183-1~deb12u1
  • links: PTS, VCS
  • area: main
  • in suites: bookworm-proposed-updates
  • size: 6,080,960 kB
  • sloc: cpp: 34,937,079; ansic: 7,176,967; javascript: 4,110,704; python: 1,419,954; asm: 946,768; xml: 739,971; pascal: 187,324; sh: 89,623; perl: 88,663; objc: 79,944; sql: 50,304; cs: 41,786; fortran: 24,137; makefile: 21,811; php: 13,980; tcl: 13,166; yacc: 8,925; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (95 lines) | stat: -rw-r--r-- 3,790 bytes parent folder | download | duplicates (7)
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
// Copyright 2021 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_OPTIMIZATION_GUIDE_CONTENT_RENDERER_PAGE_TEXT_AGENT_H_
#define COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_RENDERER_PAGE_TEXT_AGENT_H_

#include <stdint.h>

#include <map>
#include <optional>
#include <string>
#include <utility>

#include "base/functional/callback.h"
#include "base/memory/ref_counted_memory.h"
#include "base/memory/scoped_refptr.h"
#include "base/memory/weak_ptr.h"
#include "components/optimization_guide/content/mojom/page_text_service.mojom.h"
#include "content/public/renderer/render_frame_observer.h"
#include "content/public/renderer/render_frame_observer_tracker.h"
#include "mojo/public/cpp/bindings/associated_receiver_set.h"
#include "mojo/public/cpp/bindings/pending_remote.h"
#include "third_party/blink/public/web/web_meaningful_layout.h"

namespace content {
class RenderFrame;
}  // namespace content

namespace optimization_guide {

// PageTextAgent is the interface between ChromeRenderFrameObserver and
// mojom::PageTextService. It currently supports requesting and getting text
// dumps during |content::RenderFrameObserver::DidMeaningfulLayout|, and
// |DidFinishLoad| for subframes.
class PageTextAgent
    : public mojom::PageTextService,
      public content::RenderFrameObserver,
      public content::RenderFrameObserverTracker<PageTextAgent> {
 public:
  explicit PageTextAgent(content::RenderFrame* frame);
  ~PageTextAgent() override;

  // This should be called during |DidMeaningfulLayout| to determine whether
  // this class would like to get a page dump. If so, the returned callback
  // should be ran with the text, and |max_size| will be updated to a bigger
  // value iff this class wants more text than that.
  base::OnceCallback<void(scoped_refptr<const base::RefCountedString16>)>
  MaybeRequestTextDumpOnLayoutEvent(blink::WebMeaningfulLayout event,
                                    uint32_t* max_size);

  // Bind to mojo pipes. Public for testing.
  void Bind(mojo::PendingAssociatedReceiver<mojom::PageTextService> receiver);

  // mojom::PageTextService:
  void RequestPageTextDump(
      mojom::PageTextDumpRequestPtr request,
      mojo::PendingRemote<mojom::PageTextConsumer> consumer) override;

  // content::RenderFrameObserver:
  void OnDestruct() override {}
  void DidObserveLoadingBehavior(blink::LoadingBehaviorFlag behavior) override;
  void DidStartNavigation(
      const GURL& url,
      std::optional<blink::WebNavigationType> navigation_type) override;
  void DidFinishLoad() override;

  PageTextAgent(const PageTextAgent&) = delete;
  PageTextAgent& operator=(const PageTextAgent&) = delete;

 private:
  // Called when the text dump is done and it can be sent to |consumer|.
  void OnPageTextDump(mojo::PendingRemote<mojom::PageTextConsumer> consumer,
                      scoped_refptr<const base::RefCountedString16> content);

  // Keeps track of the text dump events that have been requested. Entries are
  // only present between being added in |RequestPageTextDump| and
  // |MaybeRequestTextDumpOnLayoutEvent| where relevant data to make the
  // response is moved into the callback.
  using RequestAndConsumer =
      std::pair<mojom::PageTextDumpRequestPtr,
                mojo::PendingRemote<mojom::PageTextConsumer>>;
  std::map<mojom::TextDumpEvent, RequestAndConsumer> requests_by_event_;

  // Set when an AMP page is detected from loading behavior flags.
  bool is_amp_page_ = false;

  mojo::AssociatedReceiverSet<mojom::PageTextService> receivers_;

  base::WeakPtrFactory<PageTextAgent> weak_factory_{this};
};

}  // namespace optimization_guide

#endif  // COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_RENDERER_PAGE_TEXT_AGENT_H_