blob: c6646f94f42cd74fb3ac638d172e3bf168a69198 [file] [log] [blame]
[email protected]56e26542012-02-11 00:38:591// Copyright (c) 2012 The Chromium Authors. All rights reserved.
[email protected]0ebad8d2011-06-29 18:13:572// Use of this source code is governed by a BSD-style license that can be
3// found in the LICENSE file.
4
blundell2102f7c2015-07-09 10:00:535#ifndef COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_
6#define COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_
[email protected]0ebad8d2011-06-29 18:13:577
[email protected]645b4482012-07-25 22:59:088#include <map>
[email protected]0ebad8d2011-06-29 18:13:579#include <set>
10#include <string>
11
12#include "base/gtest_prod_util.h"
blundell2102f7c2015-07-09 10:00:5313#include "components/omnibox/browser/autocomplete_provider.h"
14#include "components/omnibox/browser/shortcuts_backend.h"
[email protected]0ebad8d2011-06-29 18:13:5715
blundellfb1215ee2015-06-19 20:18:1216class AutocompleteProviderClient;
[email protected]1cef2cce2013-10-14 20:41:5717class ShortcutsProviderTest;
[email protected]1cef2cce2013-10-14 20:41:5718
[email protected]0ebad8d2011-06-29 18:13:5719// Provider of recently autocompleted links. Provides autocomplete suggestions
20// from previously selected suggestions. The more often a user selects a
21// suggestion for a given search term the higher will be that suggestion's
22// ranking for future uses of that search term.
blundelld130d592015-06-21 19:29:1323class ShortcutsProvider : public AutocompleteProvider,
24 public ShortcutsBackend::ShortcutsBackendObserver {
[email protected]0ebad8d2011-06-29 18:13:5725 public:
blundellfb1215ee2015-06-19 20:18:1226 explicit ShortcutsProvider(AutocompleteProviderClient* client);
[email protected]0ebad8d2011-06-29 18:13:5727
28 // Performs the autocompletion synchronously. Since no asynch completion is
29 // performed |minimal_changes| is ignored.
jifcf322cd2015-06-17 11:01:1830 void Start(const AutocompleteInput& input, bool minimal_changes) override;
[email protected]0ebad8d2011-06-29 18:13:5731
Daniel Chenga542fca2014-10-21 09:51:2932 void DeleteMatch(const AutocompleteMatch& match) override;
[email protected]0ebad8d2011-06-29 18:13:5733
34 private:
[email protected]645b4482012-07-25 22:59:0835 friend class ClassifyTest;
rohitraob6175492016-02-03 16:37:5536 friend class ShortcutsProviderExtensionTest;
[email protected]f9dac2c72014-03-19 00:13:4337 friend class ShortcutsProviderTest;
[email protected]f13a2132014-04-22 08:13:5238 FRIEND_TEST_ALL_PREFIXES(ShortcutsProviderTest, CalculateScore);
[email protected]0ebad8d2011-06-29 18:13:5739
[email protected]b6775d782013-12-25 20:04:5340 typedef std::multimap<base::char16, base::string16> WordMap;
[email protected]645b4482012-07-25 22:59:0841
Daniel Chenga542fca2014-10-21 09:51:2942 ~ShortcutsProvider() override;
[email protected]649d1c02012-04-27 02:56:2143
[email protected]645b4482012-07-25 22:59:0844 // Returns a map mapping characters to groups of words from |text| that start
45 // with those characters, ordered lexicographically descending so that longer
46 // words appear before their prefixes (if any) within a particular
47 // equal_range().
[email protected]96920152013-12-04 21:00:1648 static WordMap CreateWordMapForString(const base::string16& text);
[email protected]645b4482012-07-25 22:59:0849
Gheorghe Comanici15b9b1042017-10-13 17:38:4450 // Finds all instances of the words from |find_words| within |text|, adds
51 // classifications to |original_class| according to the logic described below,
52 // and returns the result.
[email protected]0ebad8d2011-06-29 18:13:5753 //
Gheorghe Comanici15b9b1042017-10-13 17:38:4454 // - if |text_is_search_query| is false, the function adds
55 // ACMatchClassification::MATCH markers for all such instances.
56 //
57 // For example, given the |text|
58 // "Sports and News at sports.somesite.com - visit us!" and |original_class|
59 // {{0, NONE}, {18, URL}, {37, NONE}} (marking "sports.somesite.com" as a
60 // URL), calling with |find_text| set to "sp ew" would return
61 // {{0, MATCH}, {2, NONE}, {12, MATCH}, {14, NONE}, {18, URL|MATCH},
62 // {20, URL}, {37, NONE}}.
63 //
64 //
65 // - if |text_is_search_query| is true, applies the same logic, but uses
66 // NONE for the matching text and MATCH for the non-matching text. This is
67 // done to mimic the behavior of SearchProvider which decorates matches
68 // according to the approach used by Google Suggest.
69 //
70 // For example, given that |text| corresponds to a search query "panama
71 // canal" and |original class| is {{0, NONE}}, calling with |find_text| set
72 // to "canal" would return {{0,MATCH}, {7, NONE}}.
73 //
74 // |find_text| is provided as the original string used to create
75 // |find_words|. This is supplied because it's common for this to be a prefix
76 // of |text|, so we can quickly check for that and mark that entire substring
77 // as a match before proceeding with the more generic algorithm.
[email protected]645b4482012-07-25 22:59:0878 //
79 // |find_words| should be as constructed by CreateWordMapForString(find_text).
80 //
81 // |find_text| (and thus |find_words|) are expected to be lowercase. |text|
82 // will be lowercased in this function.
[email protected]0ebad8d2011-06-29 18:13:5783 static ACMatchClassifications ClassifyAllMatchesInString(
[email protected]96920152013-12-04 21:00:1684 const base::string16& find_text,
[email protected]645b4482012-07-25 22:59:0885 const WordMap& find_words,
[email protected]96920152013-12-04 21:00:1686 const base::string16& text,
Gheorghe Comanici15b9b1042017-10-13 17:38:4487 const bool text_is_search_query,
[email protected]0ebad8d2011-06-29 18:13:5788 const ACMatchClassifications& original_class);
89
a-v-yd3baa3e2016-04-27 07:24:1690 // ShortcutsBackendObserver:
91 void OnShortcutsLoaded() override;
92
93 // Performs the autocomplete matching and scoring.
94 void GetMatches(const AutocompleteInput& input);
95
96 // Returns an AutocompleteMatch corresponding to |shortcut|. Assigns it
97 // |relevance| score in the process, and highlights the description and
98 // contents against |input|, which should be the lower-cased version of
99 // the user's input. |input| and |fixed_up_input_text| are used to decide
100 // what can be inlined.
101 AutocompleteMatch ShortcutToACMatch(
102 const ShortcutsDatabase::Shortcut& shortcut,
103 int relevance,
104 const AutocompleteInput& input,
105 const base::string16& fixed_up_input_text,
106 const base::string16 term_string,
107 const WordMap& terms_map);
108
[email protected]0ebad8d2011-06-29 18:13:57109 // Returns iterator to first item in |shortcuts_map_| matching |keyword|.
110 // Returns shortcuts_map_.end() if there are no matches.
[email protected]f9dac2c72014-03-19 00:13:43111 ShortcutsBackend::ShortcutMap::const_iterator FindFirstMatch(
[email protected]96920152013-12-04 21:00:16112 const base::string16& keyword,
[email protected]f9dac2c72014-03-19 00:13:43113 ShortcutsBackend* backend);
[email protected]0ebad8d2011-06-29 18:13:57114
[email protected]f9dac2c72014-03-19 00:13:43115 int CalculateScore(const base::string16& terms,
sdefresne4946258f2015-02-27 20:10:45116 const ShortcutsDatabase::Shortcut& shortcut,
[email protected]f9dac2c72014-03-19 00:13:43117 int max_relevance);
[email protected]56e26542012-02-11 00:38:59118
[email protected]f13a2132014-04-22 08:13:52119 // The default max relevance unless overridden by a field trial.
120 static const int kShortcutsProviderDefaultMaxRelevance;
121
blundellfb1215ee2015-06-19 20:18:12122 AutocompleteProviderClient* client_;
[email protected]9c8242a72011-09-17 00:44:51123 bool initialized_;
[email protected]0ebad8d2011-06-29 18:13:57124};
125
blundell2102f7c2015-07-09 10:00:53126#endif // COMPONENTS_OMNIBOX_BROWSER_SHORTCUTS_PROVIDER_H_