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_BROWSER_AUTOCOMPLETE_RESULT_H_
6 #define COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_RESULT_H_
12 #include "base/basictypes.h"
13 #include "components/metrics/proto/omnibox_event.pb.h"
14 #include "components/omnibox/browser/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 std::string
& languages
,
70 const AutocompleteResult
& old_matches
,
71 TemplateURLService
* template_url_service
);
73 // Adds a new set of matches to the result set. Does not re-sort. Calls
74 // PossiblySwapContentsAndDescriptionForURLSuggestion(input)" on all added
75 // matches; see comments there for more information.
76 void AppendMatches(const AutocompleteInput
& input
,
77 const ACMatches
& matches
);
79 // Removes duplicates, puts the list in sorted order and culls to leave only
80 // the best kMaxMatches matches. Sets the default match to the best match
81 // and updates the alternate nav URL.
82 void SortAndCull(const AutocompleteInput
& input
,
83 const std::string
& languages
,
84 TemplateURLService
* template_url_service
);
86 // Returns true if at least one match was copied from the last result.
87 bool HasCopiedMatches() const;
89 // Vector-style accessors/operators.
92 const_iterator
begin() const;
94 const_iterator
end() const;
97 // Returns the match at the given index.
98 const AutocompleteMatch
& match_at(size_t index
) const;
99 AutocompleteMatch
* match_at(size_t index
);
101 // Get the default match for the query (not necessarily the first). Returns
102 // end() if there is no default match.
103 const_iterator
default_match() const { return default_match_
; }
105 // Returns true if the top match is a verbatim search or URL match (see
106 // IsVerbatimType() in autocomplete_match.h), and the next match is not also
107 // some kind of verbatim match.
108 bool TopMatchIsStandaloneVerbatimMatch() const;
110 const GURL
& alternate_nav_url() const { return alternate_nav_url_
; }
112 // Clears the matches for this result set.
115 void Swap(AutocompleteResult
* other
);
118 // Does a data integrity check on this result.
119 void Validate() const;
122 // Compute the "alternate navigation URL" for a given match. This is obtained
123 // by interpreting the user input directly as a URL. See comments on
124 // |alternate_nav_url_|.
125 static GURL
ComputeAlternateNavUrl(const AutocompleteInput
& input
,
126 const AutocompleteMatch
& match
);
128 // Sort |matches| by destination, taking into account demotions based on
129 // |page_classification| when resolving ties about which of several
130 // duplicates to keep. The matches are also deduplicated. If
131 // |set_duplicate_matches| is true, the duplicate matches are stored in the
132 // |duplicate_matches| vector of the corresponding AutocompleteMatch.
133 static void DedupMatchesByDestination(
134 metrics::OmniboxEventProto::PageClassification page_classification
,
135 bool set_duplicate_matches
,
139 friend class AutocompleteProviderTest
;
141 typedef std::map
<AutocompleteProvider
*, ACMatches
> ProviderToMatches
;
143 #if defined(OS_ANDROID)
144 // iterator::difference_type is not defined in the STL that we compile with on
146 typedef int matches_difference_type
;
148 typedef ACMatches::iterator::difference_type matches_difference_type
;
151 // Returns true if |matches| contains a match with the same destination as
153 static bool HasMatchByDestination(const AutocompleteMatch
& match
,
154 const ACMatches
& matches
);
156 // operator=() by another name.
157 void CopyFrom(const AutocompleteResult
& rhs
);
159 // Populates |provider_to_matches| from |matches_|.
160 void BuildProviderToMatches(ProviderToMatches
* provider_to_matches
) const;
162 // Copies matches into this result. |old_matches| gives the matches from the
163 // last result, and |new_matches| the results from this result.
164 void MergeMatchesByProvider(
165 metrics::OmniboxEventProto::PageClassification page_classification
,
166 const ACMatches
& old_matches
,
167 const ACMatches
& new_matches
);
171 const_iterator default_match_
;
173 // The "alternate navigation URL", if any, for this result set. This is a URL
174 // to try offering as a navigational option in case the user navigated to the
175 // URL of the default match but intended something else. For example, if the
176 // user's local intranet contains site "foo", and the user types "foo", we
177 // default to searching for "foo" when the user may have meant to navigate
178 // there. In cases like this, the default match will point to the "search for
179 // 'foo'" result, and this will contain "http://foo/".
180 GURL alternate_nav_url_
;
182 DISALLOW_COPY_AND_ASSIGN(AutocompleteResult
);
185 #endif // COMPONENTS_OMNIBOX_BROWSER_AUTOCOMPLETE_RESULT_H_