-
Notifications
You must be signed in to change notification settings - Fork 6.6k
/
text_fragment_lookup_state_tracker.cc
116 lines (95 loc) · 4.35 KB
/
text_fragment_lookup_state_tracker.cc
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
// Copyright 2023 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
#include "chrome/browser/android/customtabs/text_fragment_lookup_state_tracker.h"
#include "base/feature_list.h"
#include "base/metrics/field_trial_params.h"
#include "chrome/browser/companion/text_finder/text_finder.h"
#include "chrome/browser/companion/text_finder/text_finder_manager.h"
#include "chrome/browser/companion/text_finder/text_highlighter_manager.h"
#include "chrome/browser/flags/android/chrome_feature_list.h"
#include "chrome/common/chrome_render_frame.mojom.h"
#include "content/public/browser/browser_context.h"
#include "content/public/browser/page.h"
#include "content/public/browser/render_frame_host.h"
namespace customtabs {
namespace {
// Notifies text fragment look up completion.
// `state_key` is opaque id used by client to keep track of the request.
// `lookup_results` is the mapping between the text fragments that were looked
// up and whether they were found on the page or not.
void NotifyCallback(
TextFragmentLookupStateTracker::OnResultCallback cb,
const std::string& state_key,
const std::vector<std::pair<std::string, bool>>& lookup_results) {
std::move(cb).Run(state_key, lookup_results);
}
} // namespace
TextFragmentLookupStateTracker::~TextFragmentLookupStateTracker() = default;
TextFragmentLookupStateTracker::TextFragmentLookupStateTracker(
content::WebContents* web_contents)
: WebContentsObserver(web_contents),
content::WebContentsUserData<TextFragmentLookupStateTracker>(
*web_contents) {}
void TextFragmentLookupStateTracker::LookupTextFragment(
const std::string& state_key,
const std::vector<std::string>& text_directives,
OnResultCallback on_result_callback) {
CHECK(base::FeatureList::IsEnabled(
chrome::android::kCCTTextFragmentLookupApiEnabled));
const std::vector<std::string> allowed_text_directives =
ExtractAllowedTextDirectives(text_directives);
// Increment lookup counter.
lookup_count_ += allowed_text_directives.size();
DCHECK_LE(lookup_count_, max_lookups_per_page());
// Create and attach a `TextFinderManager` to the primary page.
content::Page& page = web_contents()->GetPrimaryPage();
companion::TextFinderManager* text_finder_manager =
companion::TextFinderManager::GetOrCreateForPage(page);
DCHECK(text_finder_manager);
companion::TextFinderManager::AllDoneCallback textfinder_finished_callback =
base::BindOnce(&NotifyCallback, std::move(on_result_callback), state_key);
text_finder_manager->CreateTextFinders(
allowed_text_directives, std::move(textfinder_finished_callback));
}
std::vector<std::string>
TextFragmentLookupStateTracker::ExtractAllowedTextDirectives(
const std::vector<std::string>& text_directives) const {
// Check if the lookup counter exceeds the max number.
if (lookup_count_ >= max_lookups_per_page()) {
return {};
}
// Extract the first allowed number of text directives.
size_t cur_num = text_directives.size();
if (lookup_count_ + cur_num <= max_lookups_per_page()) {
return text_directives;
} else {
// Throttled.
size_t allowed_num = max_lookups_per_page() - lookup_count_;
return std::vector<std::string>(text_directives.begin(),
text_directives.begin() + allowed_num);
}
}
void TextFragmentLookupStateTracker::FindScrollAndHighlight(
const std::string& text_directive) const {
CHECK(base::FeatureList::IsEnabled(
chrome::android::kCCTTextFragmentLookupApiEnabled));
// Create and attach a `TextHighlighterManager` to the primary page.
content::Page& page = web_contents()->GetPrimaryPage();
companion::TextHighlighterManager* text_highlighter_manager =
companion::TextHighlighterManager::GetOrCreateForPage(page);
DCHECK(text_highlighter_manager);
text_highlighter_manager->CreateTextHighlighterAndRemoveExistingInstance(
text_directive);
}
void TextFragmentLookupStateTracker::PrimaryPageChanged(content::Page& page) {
// Reset lookup counter.
lookup_count_ = 0;
}
size_t TextFragmentLookupStateTracker::max_lookups_per_page() const {
return GetFieldTrialParamByFeatureAsInt(
chrome::android::kCCTTextFragmentLookupApiEnabled, "max_lookups_per_page",
15);
}
WEB_CONTENTS_USER_DATA_KEY_IMPL(TextFragmentLookupStateTracker);
} // namespace customtabs