Upstream version 5.34.104.0
[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. 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
23 class TemplateURLService;
24
25 namespace base {
26 class ListValue;
27 class Value;
28 }
29
30 namespace net {
31 class URLFetcher;
32 }
33
34 // Autocomplete provider for searches based on the current URL.
35 //
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
39 // set of matches.
40 //
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 {
45  public:
46   // Creates and returns an instance of this provider.
47   static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
48                                      Profile* profile);
49
50   // AutocompleteProvider:
51   virtual void Start(const AutocompleteInput& input,
52                      bool /*minimal_changes*/) OVERRIDE;
53
54   // Sets |field_trial_triggered_| to false.
55   virtual void ResetSession() OVERRIDE;
56
57   // net::URLFetcherDelegate
58   virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
59
60   // Initiates a new fetch for the given |url| of classification
61   // |page_classification|. |permanent_text| is the omnibox text
62   // for the current page.
63   void StartZeroSuggest(
64       const GURL& curent_page_url,
65       AutocompleteInput::PageClassification page_classification,
66       const base::string16& permanent_text);
67
68  private:
69   ZeroSuggestProvider(AutocompleteProviderListener* listener,
70                       Profile* profile);
71
72   virtual ~ZeroSuggestProvider();
73
74   // BaseSearchProvider:
75   virtual const TemplateURL* GetTemplateURL(
76       const SuggestResult& result) const OVERRIDE;
77   virtual const AutocompleteInput GetInput(
78       const SuggestResult& result) const OVERRIDE;
79   virtual bool ShouldAppendExtraParams(
80       const SuggestResult& result) const OVERRIDE;
81   virtual void StopSuggest() OVERRIDE;
82   virtual void ClearAllResults() OVERRIDE;
83
84   // The 4 functions below (that take classes defined in SearchProvider as
85   // arguments) were copied and trimmed from SearchProvider.
86   // TODO(hfung): Refactor them into a new base class common to both
87   // ZeroSuggestProvider and SearchProvider.
88
89   // From the OpenSearch formatted response |root_val|, populate query
90   // suggestions into |suggest_results|, navigation suggestions into
91   // |navigation_results|, and the verbatim relevance score into
92   // |verbatim_relevance|.
93   void FillResults(const base::Value& root_val,
94                    int* verbatim_relevance,
95                    SuggestResults* suggest_results,
96                    NavigationResults* navigation_results);
97
98   // Adds AutocompleteMatches for each of the suggestions in |results| to
99   // |map|.
100   void AddSuggestResultsToMap(const SuggestResults& results,
101                               MatchMap* map);
102
103   // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
104   AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
105
106   // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
107   void Run(const GURL& suggest_url);
108
109   // Parses results from the zero-suggest server and updates results.
110   void ParseSuggestResults(const base::Value& root_val);
111
112   // Converts the parsed results to a set of AutocompleteMatches and adds them
113   // to |matches_|.  Also update the histograms for how many results were
114   // received.
115   void ConvertResultsToAutocompleteMatches();
116
117   // Returns an AutocompleteMatch for the current URL. The match should be in
118   // the top position so that pressing enter has the effect of reloading the
119   // page.
120   AutocompleteMatch MatchForCurrentURL();
121
122   // When the user is in the Most Visited field trial, we ask the TopSites
123   // service for the most visited URLs during Run().  It calls back to this
124   // function to return those |urls|.
125   void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
126
127   // Used to build default search engine URLs for suggested queries.
128   TemplateURLService* template_url_service_;
129
130   // The URL for which a suggestion fetch is pending.
131   std::string current_query_;
132
133   // The type of page the user is viewing (a search results page doing search
134   // term replacement, an arbitrary URL, etc.).
135   AutocompleteInput::PageClassification current_page_classification_;
136
137   // Copy of OmniboxEditModel::permanent_text_.
138   base::string16 permanent_text_;
139
140   // Fetcher used to retrieve results.
141   scoped_ptr<net::URLFetcher> fetcher_;
142   // Whether there's a pending request in flight.
143   bool have_pending_request_;
144
145   // Suggestion for the current URL.
146   AutocompleteMatch current_url_match_;
147   // Navigation suggestions for the most recent ZeroSuggest input URL.
148   NavigationResults navigation_results_;
149   // Query suggestions for the most recent ZeroSuggest input URL.
150   MatchMap query_matches_map_;
151   // The relevance score for the URL of the current page.
152   int verbatim_relevance_;
153
154   history::MostVisitedURLList most_visited_urls_;
155
156   // For callbacks that may be run after destruction.
157   base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
158
159   DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
160 };
161
162 #endif  // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_