• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains the keyword autocomplete provider. The keyword provider
6 // is responsible for remembering/suggesting user "search keyword queries"
7 // (e.g.  "imdb Godzilla") and then fixing them up into valid URLs.  An
8 // instance of it gets created and managed by the autocomplete controller.
9 // KeywordProvider uses a TemplateURLModel to find the set of keywords.
10 //
11 // For more information on the autocomplete system in general, including how
12 // the autocomplete controller and autocomplete providers work, see
13 // chrome/browser/autocomplete.h.
14 
15 #ifndef CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_
16 #define CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_
17 #pragma once
18 
19 #include <string>
20 
21 #include "chrome/browser/autocomplete/autocomplete.h"
22 #include "content/common/notification_observer.h"
23 #include "content/common/notification_registrar.h"
24 
25 class Profile;
26 class TemplateURL;
27 class TemplateURLModel;
28 
29 // Autocomplete provider for keyword input.
30 //
31 // After construction, the autocomplete controller repeatedly calls Start()
32 // with some user input, each time expecting to receive a small set of the best
33 // matches (either synchronously or asynchronously).
34 //
35 // To construct these matches, the provider treats user input as a series of
36 // whitespace-delimited tokens and tries to match the first token as the prefix
37 // of a known "keyword".  A keyword is some string that maps to a search query
38 // URL; the rest of the user's input is taken as the input to the query.  For
39 // example, the keyword "bug" might map to the URL "http://b/issue?id=%s", so
40 // input like "bug 123" would become "http://b/issue?id=123".
41 //
42 // Because we do prefix matching, user input could match more than one keyword
43 // at once.  (Example: the input "f jazz" matches all keywords starting with
44 // "f".)  We return the best matches, up to three.
45 //
46 // The resulting matches are shown with content specified by the keyword
47 // (usually "Search [name] for %s"), description "(Keyword: [keyword])", and
48 // action "[keyword] %s".  If the user has typed a (possibly partial) keyword
49 // but no search terms, the suggested result is shown greyed out, with
50 // "<enter term(s)>" as the substituted input, and does nothing when selected.
51 class KeywordProvider : public AutocompleteProvider,
52                         public NotificationObserver {
53  public:
54   KeywordProvider(ACProviderListener* listener, Profile* profile);
55   // For testing.
56   KeywordProvider(ACProviderListener* listener, TemplateURLModel* model);
57 
58   // Returns the replacement string from the user input. The replacement
59   // string is the portion of the input that does not contain the keyword.
60   // For example, the replacement string for "b blah" is blah.
61   // If |trim_leading_whitespace| is true then leading whitespace in
62   // replacement string will be trimmed.
63   static string16 SplitReplacementStringFromInput(
64       const string16& input,
65       bool trim_leading_whitespace);
66 
67   // Returns the matching substituting keyword for |input|, or NULL if there
68   // is no keyword for the specified input.
69   static const TemplateURL* GetSubstitutingTemplateURLForInput(
70       Profile* profile,
71       const AutocompleteInput& input,
72       string16* remaining_input);
73 
74   // AutocompleteProvider
75   virtual void Start(const AutocompleteInput& input, bool minimal_changes);
76   virtual void Stop();
77 
78  private:
79   class ScopedEndExtensionKeywordMode;
80   friend class ScopedEndExtensionKeywordMode;
81 
82   virtual ~KeywordProvider();
83 
84   // Extracts the keyword from |input| into |keyword|. Any remaining characters
85   // after the keyword are placed in |remaining_input|. Returns true if |input|
86   // is valid and has a keyword. This makes use of SplitKeywordFromInput to
87   // extract the keyword and remaining string, and uses
88   // TemplateURLModel::CleanUserInputKeyword to remove unnecessary characters.
89   // In general use this instead of SplitKeywordFromInput.
90   // Leading whitespace in |*remaining_input| will be trimmed.
91   static bool ExtractKeywordFromInput(const AutocompleteInput& input,
92                                       string16* keyword,
93                                       string16* remaining_input);
94 
95   // Extracts the next whitespace-delimited token from input and returns it.
96   // Sets |remaining_input| to everything after the first token (skipping over
97   // the first intervening whitespace).
98   // If |trim_leading_whitespace| is true then leading whitespace in
99   // |*remaining_input| will be trimmed.
100   static string16 SplitKeywordFromInput(const string16& input,
101                                         bool trim_leading_whitespace,
102                                         string16* remaining_input);
103 
104   // Fills in the "destination_url" and "contents" fields of |match| with the
105   // provided user input and keyword data.
106   static void FillInURLAndContents(
107       const string16& remaining_input,
108       const TemplateURL* element,
109       AutocompleteMatch* match);
110 
111   // Determines the relevance for some input, given its type, whether the user
112   // typed the complete keyword, and whether the user is in "prefer keyword
113   // matches" mode, and whether the keyword supports replacement.
114   // If |allow_exact_keyword_match| is false, the relevance for complete
115   // keywords that support replacements is degraded.
116   static int CalculateRelevance(AutocompleteInput::Type type,
117                                 bool complete,
118                                 bool support_replacement,
119                                 bool prefer_keyword,
120                                 bool allow_exact_keyword_match);
121 
122   // Creates a fully marked-up AutocompleteMatch from the user's input.
123   // If |relevance| is negative, calculate a relevance based on heuristics.
124   AutocompleteMatch CreateAutocompleteMatch(
125       TemplateURLModel* model,
126       const string16& keyword,
127       const AutocompleteInput& input,
128       size_t prefix_length,
129       const string16& remaining_input,
130       int relevance);
131 
132   void EnterExtensionKeywordMode(const std::string& extension_id);
133   void MaybeEndExtensionKeywordMode();
134 
135   // NotificationObserver interface.
136   virtual void Observe(NotificationType type,
137                        const NotificationSource& source,
138                        const NotificationDetails& details);
139 
140   // Model for the keywords.  This is only non-null when testing, otherwise the
141   // TemplateURLModel from the Profile is used.
142   TemplateURLModel* model_;
143 
144   // Identifies the current input state. This is incremented each time the
145   // autocomplete edit's input changes in any way. It is used to tell whether
146   // suggest results from the extension are current.
147   int current_input_id_;
148 
149   // The input state at the time we last asked the extension for suggest
150   // results.
151   AutocompleteInput extension_suggest_last_input_;
152 
153   // We remember the last suggestions we've received from the extension in case
154   // we need to reset our matches without asking the extension again.
155   std::vector<AutocompleteMatch> extension_suggest_matches_;
156 
157   // If non-empty, holds the ID of the extension whose keyword is currently in
158   // the URL bar while the autocomplete popup is open.
159   std::string current_keyword_extension_id_;
160 
161   NotificationRegistrar registrar_;
162 
163   DISALLOW_COPY_AND_ASSIGN(KeywordProvider);
164 };
165 
166 #endif  // CHROME_BROWSER_AUTOCOMPLETE_KEYWORD_PROVIDER_H_
167