File: shortcuts_provider.h

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 (122 lines) | stat: -rw-r--r-- 5,107 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
// 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.

#ifndef COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_
#define COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_

#include <map>
#include <string>

#include "base/gtest_prod_util.h"
#include "base/memory/raw_ptr.h"
#include "components/omnibox/browser/autocomplete_provider.h"
#include "components/omnibox/browser/shortcuts_backend.h"

class AutocompleteProviderClient;
class ShortcutsProviderTest;

// Provider of recently autocompleted links. Provides autocomplete suggestions
// from previously selected suggestions. The more often a user selects a
// suggestion for a given search term the higher will be that suggestion's
// ranking for future uses of that search term.
class ShortcutsProvider : public AutocompleteProvider,
                          public ShortcutsBackend::ShortcutsBackendObserver {
 public:
  // ShortcutMatch holds sufficient information about a single match from the
  // shortcut database to allow for destination deduping and relevance sorting.
  // After those stages the top matches are converted to the more heavyweight
  // AutocompleteMatch struct.  Avoiding constructing the larger struct for
  // every such match can save significant time when there are many shortcut
  // matches to process.
  // TODO(manukh): We should probably merge `ShortcutMatch` into
  //   `ShortcutsDatabase::Shortcut`. There's a 4-deep hierarchy of structs:
  //   - `AutocompleteMatch` are created from `ShortcutMatch`es
  //   - `ShortcutMatch`es own `ShortcutsDatabase::Shortcut`s
  //   - `ShortcutsDatabase::Shortcut`s own
  //     `ShortcutsDatabase::Shortcut::MatchCore`s
  struct ShortcutMatch {
    ShortcutMatch(int relevance,
                  int aggregate_number_of_hits,
                  base::Time most_recent_access_time,
                  size_t shortest_text_length,
                  const GURL& stripped_destination_url,
                  const ShortcutsDatabase::Shortcut* shortcut);

    ShortcutMatch(const ShortcutMatch& other);
    ShortcutMatch& operator=(const ShortcutMatch& other);

    int relevance;
    // The sum of `number_of_hits` of all deduped shortcuts.
    int aggregate_number_of_hits;
    base::Time most_recent_access_time;
    size_t shortest_text_length;
    GURL stripped_destination_url;
    raw_ptr<const ShortcutsDatabase::Shortcut> shortcut;
    std::u16string contents;
    AutocompleteMatch::Type type;
  };

  explicit ShortcutsProvider(AutocompleteProviderClient* client);

  // Performs the autocompletion synchronously. Since no asynch completion is
  // performed |minimal_changes| is ignored.
  void Start(const AutocompleteInput& input, bool minimal_changes) override;

  void DeleteMatch(const AutocompleteMatch& match) override;

 private:
  friend class ClassifyTest;
  friend class ShortcutsProviderExtensionTest;
  friend class ShortcutsProviderTest;

  ~ShortcutsProvider() override;

  // ShortcutsBackendObserver:
  void OnShortcutsLoaded() override;

  // Performs the autocomplete matching and scoring. Populates matches results
  // with scoring signals for ML models if enabled. Only populates signals for
  // ULR matches for now.
  void DoAutocomplete(const AutocompleteInput& input,
                      bool populate_scoring_signals);

  // Creates a shortcut match by aggregating the scoring factors from a vector
  // of `shortcuts`. Specifically:
  // - Considers the shortest shortcut when computing fraction typed.
  // - Considers the most recent shortcut when considering last visit.
  // - Considers the sum of `number_of_hits`.
  // - Considers the shortest contents when picking a shortcut.
  // Returns the shortcut match with the aggregated score.
  ShortcutMatch CreateScoredShortcutMatch(
      size_t input_length,
      const GURL& stripped_destination_url,
      const std::vector<const ShortcutsDatabase::Shortcut*>& shortcuts,
      int max_relevance);

  // Returns an AutocompleteMatch corresponding to `shortcut_match`. Highlights
  // the description and contents against `input`, which should be the
  // normalized version of the user's input. `input` and `fixed_up_input_text`
  // are used to decide what can be inlined.
  AutocompleteMatch ShortcutMatchToACMatch(
      const ShortcutMatch& shortcut_match,
      int relevance,
      const AutocompleteInput& input,
      const std::u16string& fixed_up_input_text,
      const std::u16string& lower_input);

  // Returns iterator to first item in |shortcuts_map_| matching |keyword|.
  // Returns shortcuts_map_.end() if there are no matches.
  ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch(
      const std::u16string& keyword,
      ShortcutsBackend* backend);

  // The default max relevance unless overridden by a field trial.
  static const int kShortcutsProviderDefaultMaxRelevance;

  raw_ptr<AutocompleteProviderClient> client_ = nullptr;
  scoped_refptr<ShortcutsBackend> backend_;
  bool initialized_{};
};

#endif  // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_