File: builtin_provider.cc

package info (click to toggle)
chromium 139.0.7258.127-1
  • links: PTS, VCS
  • area: main
  • in suites:
  • size: 6,122,068 kB
  • sloc: cpp: 35,100,771; ansic: 7,163,530; javascript: 4,103,002; python: 1,436,920; asm: 946,517; xml: 746,709; pascal: 187,653; perl: 88,691; sh: 88,436; objc: 79,953; sql: 51,488; cs: 44,583; fortran: 24,137; makefile: 22,147; tcl: 15,277; php: 13,980; yacc: 8,984; ruby: 7,485; awk: 3,720; lisp: 3,096; lex: 1,327; ada: 727; jsp: 228; sed: 36
file content (206 lines) | stat: -rw-r--r-- 8,984 bytes parent folder | download | duplicates (6)
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
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
// Copyright 2012 The Chromium Authors
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#ifdef UNSAFE_BUFFERS_BUILD
// TODO(crbug.com/390223051): Remove C-library calls to fix the errors.
#pragma allow_unsafe_libc_calls
#endif

#include "components/omnibox/browser/builtin_provider.h"

#include <stddef.h>

#include <string>

#include "base/strings/strcat.h"
#include "base/strings/string_util.h"
#include "base/strings/utf_string_conversions.h"
#include "components/omnibox/browser/autocomplete_input.h"
#include "components/omnibox/browser/autocomplete_match.h"
#include "components/omnibox/browser/autocomplete_match_classification.h"
#include "components/omnibox/browser/autocomplete_provider_client.h"
#include "components/url_formatter/url_fixer.h"
#include "third_party/metrics_proto/omnibox_focus_type.pb.h"
#include "third_party/metrics_proto/omnibox_input_type.pb.h"
#include "url/url_constants.h"

const int BuiltinProvider::kRelevance = 860;

BuiltinProvider::BuiltinProvider(AutocompleteProviderClient* client)
    : AutocompleteProvider(AutocompleteProvider::TYPE_BUILTIN),
      client_(client) {
  builtins_ = client_->GetBuiltinURLs();
  template_url_service_ = client->GetTemplateURLService();
}

void BuiltinProvider::Start(const AutocompleteInput& input,
                            bool minimal_changes) {
  matches_.clear();
  if (input.IsZeroSuggest() ||
      (input.type() == metrics::OmniboxInputType::EMPTY)) {
    return;
  }

  if (input.type() != metrics::OmniboxInputType::QUERY) {
    DoBuiltinAutocompletion(input.text());
    UpdateRelevanceScores(input);
  }
}

BuiltinProvider::~BuiltinProvider() = default;

void BuiltinProvider::DoBuiltinAutocompletion(const std::u16string& text) {
  const size_t kAboutSchemeLength = strlen(url::kAboutScheme);
  const std::u16string kAbout =
      base::StrCat({url::kAboutScheme16, url::kStandardSchemeSeparator16});
  const std::u16string embedderAbout = base::StrCat(
      {base::UTF8ToUTF16(client_->GetEmbedderRepresentationOfAboutScheme()),
       url::kStandardSchemeSeparator16});

  const int kUrl = ACMatchClassification::URL;
  const int kMatch = kUrl | ACMatchClassification::MATCH;

  bool starting_about = base::StartsWith(embedderAbout, text,
                                         base::CompareCase::INSENSITIVE_ASCII);
  if (starting_about ||
      base::StartsWith(kAbout, text, base::CompareCase::INSENSITIVE_ASCII)) {
    // Highlight the input portion matching |embedderAbout|; or if the user
    // has input "about:" (with optional slashes), highlight the whole
    // |embedderAbout|.
    TermMatches style_matches;
    if (starting_about)
      style_matches.emplace_back(0, 0, text.length());
    else if (text.length() > kAboutSchemeLength)
      style_matches.emplace_back(0, 0, embedderAbout.length());
    ACMatchClassifications styles =
        ClassifyTermMatches(style_matches, std::string::npos, kMatch, kUrl);
    // Include some common builtin URLs as the user types the scheme.
    for (const std::u16string& url :
         client_->GetBuiltinsToProvideAsUserTypes()) {
      AddBuiltinMatch(url, std::u16string(), styles);
    }
  } else {
    // Match input about: or |embedderAbout| URL input against builtin URLs.
    GURL url = url_formatter::FixupURL(base::UTF16ToUTF8(text), std::string());
    const bool text_ends_with_slash =
        base::EndsWith(text, u"/", base::CompareCase::SENSITIVE);
    // BuiltinProvider doesn't know how to suggest valid ?query or #fragment
    // extensions to builtin URLs.
    if (url.SchemeIs(client_->GetEmbedderRepresentationOfAboutScheme()) &&
        url.has_host() && !url.has_query() && !url.has_ref()) {
      // Suggest about:blank for substrings, taking URL fixup into account.
      // Chrome does not support trailing slashes or paths for about:blank.
      const std::u16string blank_host = u"blank";
      const std::u16string host = base::UTF8ToUTF16(url.host());
      if (base::StartsWith(text, url::kAboutScheme16,
                           base::CompareCase::INSENSITIVE_ASCII) &&
          base::StartsWith(blank_host, host,
                           base::CompareCase::INSENSITIVE_ASCII) &&
          (url.path().length() <= 1) && !text_ends_with_slash) {
        std::u16string match(url::kAboutBlankURL16);
        const size_t corrected_length = kAboutSchemeLength + 1 + host.length();
        TermMatches style_matches = {{0, 0, corrected_length}};
        ACMatchClassifications styles =
            ClassifyTermMatches(style_matches, match.length(), kMatch, kUrl);
        AddBuiltinMatch(match, match.substr(corrected_length), styles);
      }

      // Include the path for sub-pages (e.g. "chrome://settings/browser").
      std::u16string host_and_path = base::UTF8ToUTF16(url.host() + url.path());
      base::TrimString(host_and_path, u"/", &host_and_path);
      size_t match_length = embedderAbout.length() + host_and_path.length();
      for (Builtins::const_iterator i(builtins_.begin());
           (i != builtins_.end()) && (matches_.size() < provider_max_matches_);
           ++i) {
        if (base::StartsWith(*i, host_and_path,
                             base::CompareCase::INSENSITIVE_ASCII)) {
          std::u16string match_string = embedderAbout + *i;
          TermMatches style_matches = {{0, 0, match_length}};
          ACMatchClassifications styles = ClassifyTermMatches(
              style_matches, match_string.length(), kMatch, kUrl);
          // FixupURL() may have dropped a trailing slash on the user's input.
          // Ensure that in that case, we don't inline autocomplete unless the
          // autocompletion restores the slash.  This prevents us from e.g.
          // trying to add a 'y' to an input like "chrome://histor/".
          std::u16string inline_autocompletion(
              match_string.substr(match_length));
          if (text_ends_with_slash &&
              !base::StartsWith(match_string.substr(match_length), u"/",
                                base::CompareCase::INSENSITIVE_ASCII))
            inline_autocompletion = std::u16string();
          AddBuiltinMatch(match_string, inline_autocompletion, styles);
        }
      }
    }
  }
}

void BuiltinProvider::UpdateRelevanceScores(const AutocompleteInput& input) {
  // Provide a relevance score for each match.
  for (size_t i = 0; i < matches_.size(); ++i) {
    matches_[i].relevance += matches_.size() - (i + 1);
  }

  // If allowing completions is okay and there's a match that's considered
  // appropriate to be the default match, mark it as such and give it a high
  // enough score to beat url-what-you-typed.
  size_t default_match_index;

  // None of the built in site URLs contain whitespaces so we can safely prevent
  // autocompletion when the input has a trailing whitespace in order to avoid
  // autocompleting e.g. 'chrome://s ettings' when the input is 'chrome://s '.
  bool input_allowed_to_have_default_match =
      !input.prevent_inline_autocomplete() &&
      (input.text().empty() || !base::IsUnicodeWhitespace(input.text().back()));
  if (input_allowed_to_have_default_match &&
      HasMatchThatShouldBeDefault(&default_match_index)) {
    matches_[default_match_index].relevance = 1250;
    matches_[default_match_index].allowed_to_be_default_match = true;
  }
}

void BuiltinProvider::AddBuiltinMatch(const std::u16string& match_string,
                                      const std::u16string& inline_completion,
                                      const ACMatchClassifications& styles) {
  AutocompleteMatch match(this, kRelevance, false,
                          AutocompleteMatchType::NAVSUGGEST);
  match.suggest_type = omnibox::TYPE_NAVIGATION;
  match.fill_into_edit = match_string;
  match.inline_autocompletion = inline_completion;
  match.destination_url = GURL(match_string);
  match.contents = match_string;
  match.contents_class = styles;
  matches_.push_back(match);
}

bool BuiltinProvider::HasMatchThatShouldBeDefault(size_t* index) const {
  if (matches_.size() == 0)
    return false;

  // If there's only one possible completion of the user's input and it's not
  // empty, it should be allowed to be the default match.
  if (matches_.size() == 1) {
    *index = 0;
    return !matches_[0].inline_autocompletion.empty();
  }

  // If there's a non-empty completion that is a prefix of all of the others,
  // it should be allowed to be the default match.
  size_t shortest = 0;
  for (size_t i = 1; i < matches_.size(); ++i) {
    if (matches_[i].contents.length() < matches_[shortest].contents.length())
      shortest = i;
  }
  if (matches_[shortest].inline_autocompletion.empty())
    return false;

  for (size_t i = 0; i < matches_.size(); ++i) {
    if (!base::StartsWith(matches_[i].contents, matches_[shortest].contents,
                          base::CompareCase::INSENSITIVE_ASCII)) {
      return false;
    }
  }
  *index = shortest;
  return true;
}