1 // Copyright 2014 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.
5 #ifndef COMPONENTS_OMNIBOX_AUTOCOMPLETE_RESULT_H_
6 #define COMPONENTS_OMNIBOX_AUTOCOMPLETE_RESULT_H_
12 #include "base/basictypes.h"
13 #include "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/omnibox/autocomplete_match.h"
17 class AutocompleteInput
;
18 class AutocompleteProvider
;
19 class TemplateURLService
;
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
24 class AutocompleteResult
{
26 typedef ACMatches::const_iterator const_iterator
;
27 typedef ACMatches::iterator iterator
;
29 // The "Selection" struct is the information we need to select the same match
30 // in one result set that was selected in another.
33 : provider_affinity(NULL
),
34 is_history_what_you_typed_match(false) {
37 // Clear the selection entirely.
40 // True when the selection is empty.
42 return destination_url
.is_empty() && !provider_affinity
&&
43 !is_history_what_you_typed_match
;
46 // The desired destination URL.
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
;
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
;
60 // Max number of matches we'll show from the various providers.
61 static const size_t kMaxMatches
;
64 ~AutocompleteResult();
66 // Copies matches from |old_matches| to provide a consistant result set. See
67 // comments in code for specifics.
68 void CopyOldMatches(const AutocompleteInput
& input
,
69 const AutocompleteResult
& old_matches
,
70 TemplateURLService
* template_url_service
);
72 // Adds a new set of matches to the result set. Does not re-sort. Calls
73 // PossiblySwapContentsAndDescriptionForURLSuggestion(input)" on all added
74 // matches; see comments there for more information.
75 void AppendMatches(const AutocompleteInput
& input
,
76 const ACMatches
& matches
);
78 // Removes duplicates, puts the list in sorted order and culls to leave only
79 // the best kMaxMatches matches. Sets the default match to the best match
80 // and updates the alternate nav URL.
81 void SortAndCull(const AutocompleteInput
& input
,
82 TemplateURLService
* template_url_service
);
84 // Returns true if at least one match was copied from the last result.
85 bool HasCopiedMatches() const;
87 // Vector-style accessors/operators.
90 const_iterator
begin() const;
92 const_iterator
end() const;
95 // Returns the match at the given index.
96 const AutocompleteMatch
& match_at(size_t index
) const;
97 AutocompleteMatch
* match_at(size_t index
);
99 // Get the default match for the query (not necessarily the first). Returns
100 // end() if there is no default match.
101 const_iterator
default_match() const { return default_match_
; }
103 // Returns true if the top match is a verbatim search or URL match (see
104 // IsVerbatimType() in autocomplete_match.h), and the next match is not also
105 // some kind of verbatim match.
106 bool TopMatchIsStandaloneVerbatimMatch() const;
108 const GURL
& alternate_nav_url() const { return alternate_nav_url_
; }
110 // Clears the matches for this result set.
113 void Swap(AutocompleteResult
* other
);
116 // Does a data integrity check on this result.
117 void Validate() const;
120 // Compute the "alternate navigation URL" for a given match. This is obtained
121 // by interpreting the user input directly as a URL. See comments on
122 // |alternate_nav_url_|.
123 static GURL
ComputeAlternateNavUrl(const AutocompleteInput
& input
,
124 const AutocompleteMatch
& match
);
126 // Sort |matches| by destination, taking into account demotions based on
127 // |page_classification| when resolving ties about which of several
128 // duplicates to keep. The matches are also deduplicated. If
129 // |set_duplicate_matches| is true, the duplicate matches are stored in the
130 // |duplicate_matches| vector of the corresponding AutocompleteMatch.
131 static void DedupMatchesByDestination(
132 metrics::OmniboxEventProto::PageClassification page_classification
,
133 bool set_duplicate_matches
,
137 friend class AutocompleteProviderTest
;
139 typedef std::map
<AutocompleteProvider
*, ACMatches
> ProviderToMatches
;
141 #if defined(OS_ANDROID)
142 // iterator::difference_type is not defined in the STL that we compile with on
144 typedef int matches_difference_type
;
146 typedef ACMatches::iterator::difference_type matches_difference_type
;
149 // Returns true if |matches| contains a match with the same destination as
151 static bool HasMatchByDestination(const AutocompleteMatch
& match
,
152 const ACMatches
& matches
);
154 // operator=() by another name.
155 void CopyFrom(const AutocompleteResult
& rhs
);
157 // Populates |provider_to_matches| from |matches_|.
158 void BuildProviderToMatches(ProviderToMatches
* provider_to_matches
) const;
160 // Copies matches into this result. |old_matches| gives the matches from the
161 // last result, and |new_matches| the results from this result.
162 void MergeMatchesByProvider(
163 metrics::OmniboxEventProto::PageClassification page_classification
,
164 const ACMatches
& old_matches
,
165 const ACMatches
& new_matches
);
169 const_iterator default_match_
;
171 // The "alternate navigation URL", if any, for this result set. This is a URL
172 // to try offering as a navigational option in case the user navigated to the
173 // URL of the default match but intended something else. For example, if the
174 // user's local intranet contains site "foo", and the user types "foo", we
175 // default to searching for "foo" when the user may have meant to navigate
176 // there. In cases like this, the default match will point to the "search for
177 // 'foo'" result, and this will contain "http://foo/".
178 GURL alternate_nav_url_
;
180 DISALLOW_COPY_AND_ASSIGN(AutocompleteResult
);
183 #endif // COMPONENTS_OMNIBOX_AUTOCOMPLETE_RESULT_H_