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
|
// 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() = default;
content::GlobalRenderFrameHostToken global_frame_token;
url::Origin source_origin;
GURL url;
std::string serialized_server_token;
};
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);
} // namespace optimization_guide
#endif // COMPONENTS_OPTIMIZATION_GUIDE_CONTENT_BROWSER_PAGE_CONTENT_PROTO_UTIL_H_
|