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