zero_suggest_provider.h revision 116680a4aac90f2aa7413d9095a592090648e557
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                                     Profile* profile);
55
56  // Registers a preference used to cache zero suggest results.
57  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
58
59  // AutocompleteProvider:
60  virtual void Start(const AutocompleteInput& input,
61                     bool minimal_changes) OVERRIDE;
62  virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
63
64  // Sets |field_trial_triggered_| to false.
65  virtual void ResetSession() OVERRIDE;
66
67 protected:
68  // BaseSearchProvider:
69  virtual void ModifyProviderInfo(
70      metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE;
71
72 private:
73  ZeroSuggestProvider(AutocompleteProviderListener* listener,
74                      Profile* profile);
75
76  virtual ~ZeroSuggestProvider();
77
78  // BaseSearchProvider:
79  virtual bool StoreSuggestionResponse(const std::string& json_data,
80                                       const base::Value& parsed_data) OVERRIDE;
81  virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE;
82  virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE;
83  virtual Results* GetResultsToFill(bool is_keyword) OVERRIDE;
84  virtual bool ShouldAppendExtraParams(
85      const SuggestResult& result) const OVERRIDE;
86  virtual void StopSuggest() OVERRIDE;
87  virtual void ClearAllResults() OVERRIDE;
88  virtual int GetDefaultResultRelevance() const OVERRIDE;
89  virtual void RecordDeletionResult(bool success) OVERRIDE;
90  virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE;
91  virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE;
92  virtual void UpdateMatches() OVERRIDE;
93
94  // Adds AutocompleteMatches for each of the suggestions in |results| to
95  // |map|.
96  void AddSuggestResultsToMap(const SuggestResults& results, MatchMap* map);
97
98  // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
99  AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
100
101  // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
102  void Run(const GURL& suggest_url);
103
104  // Converts the parsed results to a set of AutocompleteMatches and adds them
105  // to |matches_|.  Also update the histograms for how many results were
106  // received.
107  void ConvertResultsToAutocompleteMatches();
108
109  // Returns an AutocompleteMatch for the current URL. The match should be in
110  // the top position so that pressing enter has the effect of reloading the
111  // page.
112  AutocompleteMatch MatchForCurrentURL();
113
114  // When the user is in the Most Visited field trial, we ask the TopSites
115  // service for the most visited URLs during Run().  It calls back to this
116  // function to return those |urls|.
117  void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
118
119  // Returns the relevance score for the verbatim result.
120  int GetVerbatimRelevance() const;
121
122  // Whether we can show zero suggest on |current_page_url| without
123  // sending |current_page_url| as a parameter to the server at |suggest_url|.
124  bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url,
125                                           const GURL& current_page_url) const;
126
127  // Checks whether we have a set of zero suggest results cached, and if so
128  // populates |matches_| with cached results.
129  void MaybeUseCachedSuggestions();
130
131  // Used to build default search engine URLs for suggested queries.
132  TemplateURLService* template_url_service_;
133
134  // The URL for which a suggestion fetch is pending.
135  std::string current_query_;
136
137  // The type of page the user is viewing (a search results page doing search
138  // term replacement, an arbitrary URL, etc.).
139  metrics::OmniboxEventProto::PageClassification current_page_classification_;
140
141  // Copy of OmniboxEditModel::permanent_text_.
142  base::string16 permanent_text_;
143
144  // Fetcher used to retrieve results.
145  scoped_ptr<net::URLFetcher> fetcher_;
146
147  // Suggestion for the current URL.
148  AutocompleteMatch current_url_match_;
149
150  // Contains suggest and navigation results as well as relevance parsed from
151  // the response for the most recent zero suggest input URL.
152  Results results_;
153
154  // Whether we are currently showing cached zero suggest results.
155  bool results_from_cache_;
156
157  history::MostVisitedURLList most_visited_urls_;
158
159  // For callbacks that may be run after destruction.
160  base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
161
162  DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
163};
164
165#endif  // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
166