File: page_content_proto_util.h

package info (click to toggle)
chromium 140.0.7339.127-1
  • links: PTS, VCS
  • area: main
  • in suites: forky, sid
  • size: 6,192,880 kB
  • sloc: cpp: 35,093,808; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,503; pascal: 187,748; perl: 88,691; sh: 88,248; objc: 79,953; sql: 52,714; cs: 44,599; fortran: 24,137; makefile: 22,114; tcl: 15,277; php: 13,980; yacc: 9,000; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (89 lines) | stat: -rw-r--r-- 3,843 bytes parent folder | download | duplicates (3)
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
// Copyright 2024 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_BROWSER_PAGE_CONTENT_PROTO_UTIL_H_
#define COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_PROTO_UTIL_H_

#include "base/containers/flat_map.h"
#include "base/containers/flat_set.h"
#include "base/functional/callback.h"
#include "components/optimization_guide/content/browser/page_content_proto_provider.h"
#include "components/optimization_guide/proto/common_types.pb.h"
#include "components/optimization_guide/proto/features/model_prototyping.pb.h"
#include "content/public/browser/global_routing_id.h"
#include "third_party/blink/public/mojom/content_extraction/ai_page_content.mojom-forward.h"
#include "ui/gfx/geometry/point.h"
#include "url/origin.h"

namespace optimization_guide {

struct RenderFrameInfo {
 public:
  RenderFrameInfo();
  RenderFrameInfo(const RenderFrameInfo& other);
  ~RenderFrameInfo();

  content::GlobalRenderFrameHostToken global_frame_token;
  url::Origin source_origin;
  GURL url;
  std::string serialized_server_token;
  std::optional<optimization_guide::proto::MediaData> media_data;
};

struct TargetNodeInfo {
  optimization_guide::proto::DocumentIdentifier document_identifier;
  raw_ptr<const optimization_guide::proto::ContentNode> node = nullptr;
};

using AIPageContentMap = base::flat_map<content::GlobalRenderFrameHostToken,
                                        blink::mojom::AIPageContentPtr>;

// A set of frame tokens that have been seen during conversion.
using FrameTokenSet = base::flat_set<content::GlobalRenderFrameHostToken>;

// A callback to get the RenderFrameInfo for a given frame token.
using GetRenderFrameInfo =
    base::RepeatingCallback<std::optional<RenderFrameInfo>(int child_process_id,
                                                           blink::FrameToken)>;

// Converts the mojom data structure for AIPageContent to its equivalent proto
// mapping.
// Returns false if the conversion failed because the renderer provided invalid
// inputs.
bool ConvertAIPageContentToProto(
    blink::mojom::AIPageContentOptionsPtr main_frame_options,
    content::GlobalRenderFrameHostToken main_frame_token,
    const AIPageContentMap& page_content_map,
    GetRenderFrameInfo get_render_frame_info,
    FrameTokenSet& frame_token_set,
    optimization_guide::AIPageContentResult& page_content);

// Hit test given coordinate with the provided annotated page content and
// returns the target node and containing document info at the coordinate if
// there's a match. Returns std::nullopt otherwise.
std::optional<optimization_guide::TargetNodeInfo> FindNodeAtPoint(
    const optimization_guide::proto::AnnotatedPageContent&
        annotated_page_content,
    const gfx::Point& coordinate);

// Returns the target node and containing document info if there's a matching
// node from the annotated page content with the same dom node id and under a
// frame node with matching document identifier. Returns std::nullopt otherwise.
std::optional<optimization_guide::TargetNodeInfo> FindNodeWithID(
    const optimization_guide::proto::AnnotatedPageContent&
        annotated_page_content,
    const std::string_view document_identifier,
    const int dom_node_id);


// Returns the URL to use for frame metadata given the Document's
// `committed_url` and `committed_origin`. The `committed_url` may not be a
// valid origin (for example about:blank or data: URLs) but the origin will be
// the web origin of the Document's content.
GURL GetURLForFrameMetadata(const GURL& committed_url,
                            const url::Origin& committed_origin);

}  // namespace optimization_guide

#endif  // COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_PROTO_UTIL_H_