zero_suggest_provider.h revision 5f1c94371a64b3196d4be9466099bb892df9b88e
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// 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.
10//
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.
13
14#ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
15#define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
16
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"
22#include "chrome/browser/history/history_types.h"
23#include "components/metrics/proto/omnibox_event.pb.h"
24
25class TemplateURLService;
26
27namespace base {
28class ListValue;
29class Value;
30}
31
32namespace net {
33class URLFetcher;
34}
35
36namespace user_prefs {
37class PrefRegistrySyncable;
38}
39
40// Autocomplete provider for searches based on the current URL.
41//
42// The controller will call StartZeroSuggest when the user focuses in the
43// omnibox. After construction, the autocomplete controller repeatedly calls
44// Start() with some user input, each time expecting to receive an updated
45// set of matches.
46//
47// TODO(jered): Consider deleting this class and building this functionality
48// into SearchProvider after dogfood and after we break the association between
49// omnibox text and suggestions.
50class ZeroSuggestProvider : public BaseSearchProvider {
51 public:
52  // Creates and returns an instance of this provider.
53  static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
54                                     TemplateURLService* template_url_service,
55                                     Profile* profile);
56
57  // Registers a preference used to cache zero suggest results.
58  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
59
60  // AutocompleteProvider:
61  virtual void Start(const AutocompleteInput& input,
62                     bool minimal_changes) OVERRIDE;
63  virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
64
65  // Sets |field_trial_triggered_| to false.
66  virtual void ResetSession() OVERRIDE;
67
68 protected:
69  // BaseSearchProvider:
70  virtual void ModifyProviderInfo(
71      metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE;
72
73 private:
74  ZeroSuggestProvider(AutocompleteProviderListener* listener,
75                      TemplateURLService* template_url_service,
76                      Profile* profile);
77
78  virtual ~ZeroSuggestProvider();
79
80  // BaseSearchProvider:
81  virtual bool StoreSuggestionResponse(const std::string& json_data,
82                                       const base::Value& parsed_data) OVERRIDE;
83  virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE;
84  virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE;
85  virtual SearchSuggestionParser::Results* GetResultsToFill(
86      bool is_keyword) OVERRIDE;
87  virtual bool ShouldAppendExtraParams(
88      const SearchSuggestionParser::SuggestResult& result) const OVERRIDE;
89  virtual void StopSuggest() OVERRIDE;
90  virtual void ClearAllResults() OVERRIDE;
91  virtual int GetDefaultResultRelevance() const OVERRIDE;
92  virtual void RecordDeletionResult(bool success) OVERRIDE;
93  virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE;
94  virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE;
95  virtual void UpdateMatches() OVERRIDE;
96
97  // Adds AutocompleteMatches for each of the suggestions in |results| to
98  // |map|.
99  void AddSuggestResultsToMap(
100      const SearchSuggestionParser::SuggestResults& results,
101      MatchMap* map);
102
103  // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
104  AutocompleteMatch NavigationToMatch(
105      const SearchSuggestionParser::NavigationResult& navigation);
106
107  // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
108  void Run(const GURL& suggest_url);
109
110  // Converts the parsed results to a set of AutocompleteMatches and adds them
111  // to |matches_|.  Also update the histograms for how many results were
112  // received.
113  void ConvertResultsToAutocompleteMatches();
114
115  // Returns an AutocompleteMatch for the current URL. The match should be in
116  // the top position so that pressing enter has the effect of reloading the
117  // page.
118  AutocompleteMatch MatchForCurrentURL();
119
120  // When the user is in the Most Visited field trial, we ask the TopSites
121  // service for the most visited URLs during Run().  It calls back to this
122  // function to return those |urls|.
123  void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
124
125  // Returns the relevance score for the verbatim result.
126  int GetVerbatimRelevance() const;
127
128  // Whether we can show zero suggest on |current_page_url| without
129  // sending |current_page_url| as a parameter to the server at |suggest_url|.
130  bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url,
131                                           const GURL& current_page_url) const;
132
133  // Checks whether we have a set of zero suggest results cached, and if so
134  // populates |matches_| with cached results.
135  void MaybeUseCachedSuggestions();
136
137  // The URL for which a suggestion fetch is pending.
138  std::string current_query_;
139
140  // The type of page the user is viewing (a search results page doing search
141  // term replacement, an arbitrary URL, etc.).
142  metrics::OmniboxEventProto::PageClassification current_page_classification_;
143
144  // Copy of OmniboxEditModel::permanent_text_.
145  base::string16 permanent_text_;
146
147  // Fetcher used to retrieve results.
148  scoped_ptr<net::URLFetcher> fetcher_;
149
150  // Suggestion for the current URL.
151  AutocompleteMatch current_url_match_;
152
153  // Contains suggest and navigation results as well as relevance parsed from
154  // the response for the most recent zero suggest input URL.
155  SearchSuggestionParser::Results results_;
156
157  // Whether we are currently showing cached zero suggest results.
158  bool results_from_cache_;
159
160  history::MostVisitedURLList most_visited_urls_;
161
162  // For callbacks that may be run after destruction.
163  base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
164
165  DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
166};
167
168#endif  // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
169