OLD | NEW |
---|---|
(Empty) | |
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 <string> | |
18 #include <vector> | |
19 | |
20 #include "base/basictypes.h" | |
21 #include "base/compiler_specific.h" | |
22 #include "base/memory/scoped_ptr.h" | |
23 #include "base/string16.h" | |
24 #include "chrome/browser/autocomplete/autocomplete_provider.h" | |
25 #include "net/url_request/url_fetcher_delegate.h" | |
26 | |
27 class AutocompleteInput; | |
28 class GURL; | |
29 class TemplateURLService; | |
30 | |
31 namespace base { | |
32 class Value; | |
33 } | |
34 | |
35 namespace net { | |
36 class URLFetcher; | |
37 } | |
38 | |
39 // Autocomplete provider for searches based on the current URL. | |
40 // | |
41 // The controller will call StartZeroSuggest when the user focuses in the | |
42 // omnibox. After construction, the autocomplete controller repeatedly calls | |
43 // Start() with some user input, each time expecting to receive an updated | |
44 // set of matches. | |
45 // | |
46 // TODO(jered): Consider deleting this class and building this functionality | |
47 // into SearchProvider after dogfood and after we break the association between | |
48 // omnibox text and suggestions. | |
49 class ZeroSuggestProvider : public AutocompleteProvider, | |
50 public net::URLFetcherDelegate { | |
51 public: | |
52 ZeroSuggestProvider(AutocompleteProviderListener* listener, | |
53 Profile* profile, | |
54 const std::string& url_prefix); | |
55 | |
56 // AutocompleteProvider: | |
57 virtual void Start(const AutocompleteInput& input, | |
58 bool /*minimal_changes*/) OVERRIDE; | |
59 virtual void Stop(bool clear_cached_results) OVERRIDE; | |
60 | |
61 // net::URLFetcherDelegate | |
62 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; | |
63 | |
64 // Initiates a new fetch for the given |url|, limiting suggestions to those | |
65 // matching |user_text|. |user_text| may be non-empty if the user previously | |
66 // interacted with zero-suggest suggestions and then unfocused the omnibox. | |
67 // TODO(jered): Rip out |user_text| once the first match is decoupled from | |
68 // the current typing in the omnibox. | |
69 void StartZeroSuggest(const GURL& url, const string16& user_text); | |
70 | |
71 private: | |
72 virtual ~ZeroSuggestProvider(); | |
73 | |
74 // Update matches given the user has typed |user_text|. | |
75 void UpdateMatches(const string16& user_text); | |
76 | |
77 // Fetches zero-suggest suggestions for |current_query_|. | |
78 void Run(); | |
79 | |
80 // Parses results from the zero-suggest server and updates results. | |
81 // Returns true if results were updated. | |
82 bool ParseSuggestResults(base::Value* root_val); | |
83 | |
84 // Converts the parsed results to a set of AutocompleteMatches and adds them | |
85 // to |matches_|. | |
86 void ConvertResultsToAutocompleteMatches(); | |
87 | |
88 // Adds a URL suggestion for the current URL. This should be in the top | |
89 // position so that pressing enter has the effect of reloading the page. | |
90 void AddMatchForCurrentURL(); | |
91 | |
92 // Adds a query suggestion from response position |result_index| with text | |
93 // |result| to |matches_|. Uses |search_provider| to build a search URL for | |
94 // this match. | |
95 void AddMatchForResult(const TemplateURL* search_provider, | |
96 size_t result_index, | |
97 const string16& result); | |
98 | |
99 // Prefix of the URL from which to fetch zero-suggest suggestions. | |
100 const std::string url_prefix_; | |
101 | |
102 // Used to build default search engine URLs for suggested queries. | |
103 TemplateURLService* template_url_service_; | |
104 | |
105 // The URL for which a suggestion fetch is pending. | |
106 std::string current_query_; | |
107 | |
108 // Current query as UTF-16. | |
Peter Kasting
2012/08/15 21:52:44
Nit: Don't keep both these members just to reduce
Jered
2012/08/15 23:13:00
Done.
| |
109 string16 current_query_text_; | |
110 | |
111 // What the user has typed. | |
112 string16 user_text_; | |
113 | |
114 // Fetcher used to retrieve results. | |
115 scoped_ptr<net::URLFetcher> fetcher_; | |
116 | |
117 // Suggestions for the most recent query. | |
118 std::vector<string16> results_; | |
119 | |
120 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); | |
121 }; | |
122 | |
123 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ | |
OLD | NEW |