File: glic_pin_candidate_comparator.cc

package info (click to toggle)
chromium 140.0.7339.185-1
  • links: PTS, VCS
  • area: main
  • in suites: forky
  • size: 6,193,740 kB
  • sloc: cpp: 35,093,945; ansic: 7,161,670; javascript: 4,199,694; python: 1,441,797; asm: 949,904; xml: 747,515; 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 (108 lines) | stat: -rw-r--r-- 3,742 bytes parent folder | download | duplicates (4)
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
// Copyright 2025 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/glic/host/context/glic_pin_candidate_comparator.h"

#include <tuple>

#include "base/i18n/char_iterator.h"
#include "base/i18n/string_search.h"
#include "base/strings/string_split.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "content/public/browser/web_contents.h"

namespace glic {

namespace {

bool ContainsWhitespace(std::u16string_view text) {
  for (base::i18n::UTF16CharIterator iter(text); !iter.end(); iter.Advance()) {
    if (base::IsUnicodeWhitespace(iter.get())) {
      return true;
    }
  }
  return false;
}

SearchResult Search(std::u16string_view title, const std::u16string& query) {
  SearchResult result;
  size_t index;
  size_t length;
  if (base::i18n::StringSearchIgnoringCaseAndAccents(query, title, &index,
                                                     &length)) {
    result.matches_prefix = index == 0u;
    result.contained = true;
    if (ContainsWhitespace(query)) {
      // Cannot match a word prefix if `query` contains whitespace.
      return result;
    }
    std::vector<std::u16string_view> words =
        base::SplitStringPiece(title, base::kWhitespaceUTF16,
                               base::WhitespaceHandling::TRIM_WHITESPACE,
                               base::SplitResult::SPLIT_WANT_NONEMPTY);
    for (auto& word : words) {
      if (base::i18n::StringSearchIgnoringCaseAndAccents(query, word, &index,
                                                         &length)) {
        if (index == 0u) {
          result.matches_word_prefix = true;
          break;
        }
      }
    }
  }
  return result;
}

}  // namespace

GlicPinCandidateComparator::GlicPinCandidateComparator(
    const base::optional_ref<const std::string> query) {
  if (query) {
    query_ = base::UTF8ToUTF16(*query);
  }
}

GlicPinCandidateComparator::~GlicPinCandidateComparator() = default;

bool GlicPinCandidateComparator::operator()(content::WebContents* a,
                                            content::WebContents* b) {
  if (query_.empty()) {
    return a->GetLastActiveTimeTicks() > b->GetLastActiveTimeTicks();
  }

  const SearchResult a_results = GetSearchResults(a->GetTitle());
  const SearchResult b_results = GetSearchResults(b->GetTitle());

  // Use last active time as a tie-breaker when other search result criteria are
  // equal.
  auto a_tie_breaker = a->GetLastActiveTimeTicks();
  auto b_tie_breaker = b->GetLastActiveTimeTicks();

  // Compare WebContents based on a tuple of search result criteria and
  // last active time. The comparison order prioritizes:
  // 1. `matches_prefix`: Whether the query matches the beginning of the title.
  // 2. `matches_word_prefix`: Whether the query matches the beginning of a word
  //    in the title.
  // 3. `contained`: Whether the query is contained anywhere in the title.
  // 4. `last_active_time`: Used as a tie-breaker, more recent first.
  return std::tie(a_results.matches_prefix, a_results.matches_word_prefix,
                  a_results.contained, a_tie_breaker) >
         std::tie(b_results.matches_prefix, b_results.matches_word_prefix,
                  b_results.contained, b_tie_breaker);
}

const SearchResult GlicPinCandidateComparator::GetSearchResults(
    std::u16string_view title) {
  std::u16string title_str(title);
  auto it = search_results_cache_.find(title_str);
  if (it == search_results_cache_.end()) {
    it = search_results_cache_
             .insert({std::move(title_str), Search(title, query_)})
             .first;
  }
  return it->second;
}

}  // namespace glic