Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(159)

Side by Side Diff: chrome/browser/autocomplete/autocomplete_result.h

Issue 10699032: autocomplete: Extract AutocompleteResult from autocomplete.*. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: move comments Created 8 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
(Empty)
1 // Copyright (c) 2012 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 #ifndef CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_H_
6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_H_
7 #pragma once
8
9 #include <stddef.h>
10
11 #include <map>
12
13 #include "base/basictypes.h"
14 #include "chrome/browser/autocomplete/autocomplete_types.h"
15 #include "googleurl/src/gurl.h"
16
17 class AutocompleteInput;
18 struct AutocompleteMatch;
19 class AutocompleteProvider;
20
21 // All matches from all providers for a particular query. This also tracks
22 // what the default match should be if the user doesn't manually select another
23 // match.
24 class AutocompleteResult {
25 public:
26 typedef ACMatches::const_iterator const_iterator;
27 typedef ACMatches::iterator iterator;
28
29 // The "Selection" struct is the information we need to select the same match
30 // in one result set that was selected in another.
31 struct Selection {
32 Selection()
33 : provider_affinity(NULL),
34 is_history_what_you_typed_match(false) {
35 }
36
37 // Clear the selection entirely.
38 void Clear();
39
40 // True when the selection is empty.
41 bool empty() const {
42 return destination_url.is_empty() && !provider_affinity &&
43 !is_history_what_you_typed_match;
44 }
45
46 // The desired destination URL.
47 GURL destination_url;
48
49 // The desired provider. If we can't find a match with the specified
50 // |destination_url|, we'll use the best match from this provider.
51 const AutocompleteProvider* provider_affinity;
52
53 // True when this is the HistoryURLProvider's "what you typed" match. This
54 // can't be tracked using |destination_url| because its URL changes on every
55 // keystroke, so if this is set, we'll preserve the selection by simply
56 // choosing the new "what you typed" entry and ignoring |destination_url|.
57 bool is_history_what_you_typed_match;
58 };
59
60 // Max number of matches we'll show from the various providers.
61 static const size_t kMaxMatches;
62
63 // The lowest score a match can have and still potentially become the default
64 // match for the result set.
65 static const int kLowestDefaultScore;
66
67 AutocompleteResult();
68 ~AutocompleteResult();
69
70 // operator=() by another name.
71 void CopyFrom(const AutocompleteResult& rhs);
72
73 // Copies matches from |old_matches| to provide a consistant result set. See
74 // comments in code for specifics.
75 void CopyOldMatches(const AutocompleteInput& input,
76 const AutocompleteResult& old_matches);
77
78 // Adds a single match. The match is inserted at the appropriate position
79 // based on relevancy and display order. This is ONLY for use after
80 // SortAndCull() has been invoked, and preserves default_match_.
81 void AddMatch(const AutocompleteMatch& match);
82
83 // Adds a new set of matches to the result set. Does not re-sort.
84 void AppendMatches(const ACMatches& matches);
85
86 // Removes duplicates, puts the list in sorted order and culls to leave only
87 // the best kMaxMatches matches. Sets the default match to the best match
88 // and updates the alternate nav URL.
89 void SortAndCull(const AutocompleteInput& input);
90
91 // Returns true if at least one match was copied from the last result.
92 bool HasCopiedMatches() const;
93
94 // Vector-style accessors/operators.
95 size_t size() const;
96 bool empty() const;
97 const_iterator begin() const;
98 iterator begin();
99 const_iterator end() const;
100 iterator end();
101
102 // Returns the match at the given index.
103 const AutocompleteMatch& match_at(size_t index) const;
104 AutocompleteMatch* match_at(size_t index);
105
106 // Get the default match for the query (not necessarily the first). Returns
107 // end() if there is no default match.
108 const_iterator default_match() const { return default_match_; }
109
110 const GURL& alternate_nav_url() const { return alternate_nav_url_; }
111
112 // Clears the matches for this result set.
113 void Reset();
114
115 void Swap(AutocompleteResult* other);
116
117 #ifndef NDEBUG
118 // Does a data integrity check on this result.
119 void Validate() const;
120 #endif
121
122 private:
123 typedef std::map<AutocompleteProvider*, ACMatches> ProviderToMatches;
124
125 #if defined(OS_ANDROID)
126 // iterator::difference_type is not defined in the STL that we compile with on
127 // Android.
128 typedef int matches_difference_type;
129 #else
130 typedef ACMatches::iterator::difference_type matches_difference_type;
131 #endif
132
133 // Populates |provider_to_matches| from |matches_|.
134 void BuildProviderToMatches(ProviderToMatches* provider_to_matches) const;
135
136 // Returns true if |matches| contains a match with the same destination as
137 // |match|.
138 static bool HasMatchByDestination(const AutocompleteMatch& match,
139 const ACMatches& matches);
140
141 // Copies matches into this result. |old_matches| gives the matches from the
142 // last result, and |new_matches| the results from this result.
143 void MergeMatchesByProvider(const ACMatches& old_matches,
144 const ACMatches& new_matches);
145
146 ACMatches matches_;
147
148 const_iterator default_match_;
149
150 // The "alternate navigation URL", if any, for this result set. This is a URL
151 // to try offering as a navigational option in case the user navigated to the
152 // URL of the default match but intended something else. For example, if the
153 // user's local intranet contains site "foo", and the user types "foo", we
154 // default to searching for "foo" when the user may have meant to navigate
155 // there. In cases like this, the default match will point to the "search for
156 // 'foo'" result, and this will contain "http://foo/".
157 GURL alternate_nav_url_;
158
159 DISALLOW_COPY_AND_ASSIGN(AutocompleteResult);
160 };
161
162 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_RESULT_H_
OLDNEW
« no previous file with comments | « chrome/browser/autocomplete/autocomplete_provider.h ('k') | chrome/browser/autocomplete/autocomplete_result.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698