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.
5 // This file contains the zero-suggest autocomplete provider. This experimental
6 // provider is invoked when the user focuses in the omnibox prior to editing,
7 // and generates search query suggestions based on the current URL. To enable
8 // this provider, point --experimental-zero-suggest-url-prefix at an
9 // appropriate suggestion service.
11 // HUGE DISCLAIMER: This is just here for experimenting and will probably be
12 // deleted entirely as we revise how suggestions work with the omnibox.
14 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
15 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
17 #include "base/basictypes.h"
18 #include "base/compiler_specific.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "chrome/browser/autocomplete/base_search_provider.h"
21 #include "chrome/browser/autocomplete/search_provider.h"
23 class TemplateURLService
;
34 // Autocomplete provider for searches based on the current URL.
36 // The controller will call StartZeroSuggest when the user focuses in the
37 // omnibox. After construction, the autocomplete controller repeatedly calls
38 // Start() with some user input, each time expecting to receive an updated
41 // TODO(jered): Consider deleting this class and building this functionality
42 // into SearchProvider after dogfood and after we break the association between
43 // omnibox text and suggestions.
44 class ZeroSuggestProvider
: public BaseSearchProvider
{
46 // Creates and returns an instance of this provider.
47 static ZeroSuggestProvider
* Create(AutocompleteProviderListener
* listener
,
50 // AutocompleteProvider:
51 virtual void Start(const AutocompleteInput
& input
,
52 bool minimal_changes
) OVERRIDE
;
54 // Sets |field_trial_triggered_| to false.
55 virtual void ResetSession() OVERRIDE
;
58 ZeroSuggestProvider(AutocompleteProviderListener
* listener
,
61 virtual ~ZeroSuggestProvider();
63 // BaseSearchProvider:
64 virtual const TemplateURL
* GetTemplateURL(bool is_keyword
) const OVERRIDE
;
65 virtual const AutocompleteInput
GetInput(bool is_keyword
) const OVERRIDE
;
66 virtual Results
* GetResultsToFill(bool is_keyword
) OVERRIDE
;
67 virtual bool ShouldAppendExtraParams(
68 const SuggestResult
& result
) const OVERRIDE
;
69 virtual void StopSuggest() OVERRIDE
;
70 virtual void ClearAllResults() OVERRIDE
;
71 virtual int GetDefaultResultRelevance() const OVERRIDE
;
72 virtual void RecordDeletionResult(bool success
) OVERRIDE
;
73 virtual void LogFetchComplete(bool success
, bool is_keyword
) OVERRIDE
;
74 virtual bool IsKeywordFetcher(const net::URLFetcher
* fetcher
) const OVERRIDE
;
75 virtual void UpdateMatches() OVERRIDE
;
77 // Adds AutocompleteMatches for each of the suggestions in |results| to
79 void AddSuggestResultsToMap(const SuggestResults
& results
,
82 // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
83 AutocompleteMatch
NavigationToMatch(const NavigationResult
& navigation
);
85 // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
86 void Run(const GURL
& suggest_url
);
88 // Converts the parsed results to a set of AutocompleteMatches and adds them
89 // to |matches_|. Also update the histograms for how many results were
91 void ConvertResultsToAutocompleteMatches();
93 // Returns an AutocompleteMatch for the current URL. The match should be in
94 // the top position so that pressing enter has the effect of reloading the
96 AutocompleteMatch
MatchForCurrentURL();
98 // When the user is in the Most Visited field trial, we ask the TopSites
99 // service for the most visited URLs during Run(). It calls back to this
100 // function to return those |urls|.
101 void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList
& urls
);
103 // Returns the relevance score for the verbatim result.
104 int GetVerbatimRelevance() const;
106 // Whether we can show zero suggest on |current_page_url| without
107 // sending |current_page_url| as a parameter to the server at |suggest_url|.
108 bool CanShowZeroSuggestWithoutSendingURL(
109 const GURL
& suggest_url
,
110 const GURL
& current_page_url
) const;
112 // Used to build default search engine URLs for suggested queries.
113 TemplateURLService
* template_url_service_
;
115 // The URL for which a suggestion fetch is pending.
116 std::string current_query_
;
118 // The type of page the user is viewing (a search results page doing search
119 // term replacement, an arbitrary URL, etc.).
120 AutocompleteInput::PageClassification current_page_classification_
;
122 // Copy of OmniboxEditModel::permanent_text_.
123 base::string16 permanent_text_
;
125 // Fetcher used to retrieve results.
126 scoped_ptr
<net::URLFetcher
> fetcher_
;
128 // Suggestion for the current URL.
129 AutocompleteMatch current_url_match_
;
131 // Contains suggest and navigation results as well as relevance parsed from
132 // the response for the most recent zero suggest input URL.
135 history::MostVisitedURLList most_visited_urls_
;
137 // For callbacks that may be run after destruction.
138 base::WeakPtrFactory
<ZeroSuggestProvider
> weak_ptr_factory_
;
140 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider
);
143 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_