Skip to content

Commit

Permalink
Make number of text fragments look up configurable
Browse files Browse the repository at this point in the history
(cherry picked from commit fa583b9)

Bug: 1450935
Change-Id: Ia574e98c12052505f3558310fc8bd65cf08eee8c
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4584414
Reviewed-by: Peter Conn <peconn@chromium.org>
Reviewed-by: Jun Zou <junzou@chromium.org>
Commit-Queue: Tarun Bansal <tbansal@chromium.org>
Cr-Original-Commit-Position: refs/heads/main@{#1153148}
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/4598674
Reviewed-by: Tommy Nyquist <nyquist@chromium.org>
Cr-Commit-Position: refs/branch-heads/5790@{#495}
Cr-Branched-From: 1d71a33-refs/heads/main@{#1148114}
  • Loading branch information
tarunban authored and Chromium LUCI CQ committed Jun 8, 2023
1 parent 48bba4b commit 878dc3a
Show file tree
Hide file tree
Showing 2 changed files with 12 additions and 6 deletions.
Expand Up @@ -50,7 +50,7 @@ void TextFragmentLookupStateTracker::LookupTextFragment(
ExtractAllowedTextDirectives(text_directives);
// Increment lookup counter.
lookup_count_ += allowed_text_directives.size();
DCHECK_LE(lookup_count_, kMaxNumLookupPerPage);
DCHECK_LE(lookup_count_, max_lookups_per_page());

// Create and attach a `TextFinderManager` to the primary page.
content::Page& page = web_contents()->GetPrimaryPage();
Expand All @@ -69,17 +69,17 @@ 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_ >= kMaxNumLookupPerPage) {
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 <= kMaxNumLookupPerPage) {
if (lookup_count_ + cur_num <= max_lookups_per_page()) {
return text_directives;
} else {
// Throttled.
size_t allowed_num = kMaxNumLookupPerPage - lookup_count_;
size_t allowed_num = max_lookups_per_page() - lookup_count_;
return std::vector<std::string>(text_directives.begin(),
text_directives.begin() + allowed_num);
}
Expand All @@ -105,6 +105,12 @@ void TextFragmentLookupStateTracker::PrimaryPageChanged(content::Page& page) {
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
Expand Up @@ -11,8 +11,6 @@

namespace customtabs {

const size_t kMaxNumLookupPerPage = 15;

class TextFragmentLookupStateTracker
: public content::WebContentsObserver,
public content::WebContentsUserData<TextFragmentLookupStateTracker> {
Expand Down Expand Up @@ -50,6 +48,8 @@ class TextFragmentLookupStateTracker
// Implements `content::WebContentsObserver`:
void PrimaryPageChanged(content::Page& page) override;

size_t max_lookups_per_page() const;

WEB_CONTENTS_USER_DATA_KEY_DECL();

size_t lookup_count_ = 0;
Expand Down

0 comments on commit 878dc3a

Please sign in to comment.