| // 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 "base/types/expected.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. If conversion fails, the returned base::expected contains a |
| // descriptive error message. |
| base::expected<void, std::string> 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_ |