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 zerosuggest 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 --zerosuggest-url-prefix at an appropriate suggestion |
| 9 // service. |
| 10 |
| 11 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZEROSUGGEST_PROVIDER_H_ |
| 12 #define CHROME_BROWSER_AUTOCOMPLETE_ZEROSUGGEST_PROVIDER_H_ |
| 13 |
| 14 #include <string> |
| 15 #include <vector> |
| 16 |
| 17 #include "base/basictypes.h" |
| 18 #include "base/compiler_specific.h" |
| 19 #include "base/memory/scoped_ptr.h" |
| 20 #include "base/string16.h" |
| 21 #include "base/timer.h" |
| 22 #include "chrome/browser/autocomplete/autocomplete_provider.h" |
| 23 #include "net/url_request/url_fetcher_delegate.h" |
| 24 |
| 25 class AutocompleteInput; |
| 26 class GURL; |
| 27 class TemplateURLService; |
| 28 |
| 29 namespace base { |
| 30 class Value; |
| 31 } |
| 32 |
| 33 namespace net { |
| 34 class URLFetcher; |
| 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 class ZerosuggestProvider : public AutocompleteProvider, |
| 44 public net::URLFetcherDelegate { |
| 45 public: |
| 46 ZerosuggestProvider(AutocompleteProviderListener* listener, |
| 47 Profile* profile, |
| 48 const std::string& url_prefix); |
| 49 |
| 50 // AutocompleteProvider: |
| 51 virtual void Start(const AutocompleteInput& input, |
| 52 bool /*minimal_changes*/) OVERRIDE; |
| 53 virtual void Stop(bool clear_cached_results) OVERRIDE; |
| 54 |
| 55 // net::URLFetcherDelegate |
| 56 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; |
| 57 |
| 58 // Sets a timer to initiate a new fetch for the given |url|, limiting |
| 59 // suggestions to those matching |user_text|. |user_text| may be non-empty |
| 60 // if the user previously interacted with zerosuggest suggestions and then |
| 61 // unfocused the omnibox. |
| 62 void StartZerosuggest(const GURL& url, const string16& user_text); |
| 63 |
| 64 private: |
| 65 virtual ~ZerosuggestProvider(); |
| 66 |
| 67 // Update matches given the user has typed |user_text|. |
| 68 void UpdateMatches(const string16& user_text); |
| 69 |
| 70 // Fetches zerosuggest suggestions for |current_query_|. |
| 71 void Run(); |
| 72 |
| 73 // Parses results from the zerosuggest server and updates results. |
| 74 // Returns true if results were updated. |
| 75 bool ParseSuggestResults(base::Value* root_val); |
| 76 |
| 77 // Converts the parsed results to a set of AutocompleteMatches and adds them |
| 78 // to |matches_|. |
| 79 void ConvertResultsToAutocompleteMatches(); |
| 80 |
| 81 // Adds a URL suggestion for the current URL. This should be in the top |
| 82 // position so that pressing enter has the effect of reloading the page. |
| 83 void AddMatchForCurrentURL(); |
| 84 |
| 85 // Adds a query suggestion from response position |result_index| with text |
| 86 // |result| to |matches_|. Uses |search_provider| to build a search URL for |
| 87 // this match. |
| 88 void AddMatchForResult(const TemplateURL* search_provider, |
| 89 size_t result_index, |
| 90 const string16& result); |
| 91 |
| 92 // Timer which must elapse before a fetch begins. |
| 93 base::OneShotTimer<ZerosuggestProvider> timer_; |
| 94 |
| 95 // Prefix of the URL from which to fetch zerosuggest suggestions. |
| 96 const std::string url_prefix_; |
| 97 |
| 98 // Used to build default search engine URLs for suggested queries. |
| 99 TemplateURLService* template_url_service_; |
| 100 |
| 101 // The URL for which a suggestion fetch is pending. |
| 102 std::string current_query_; |
| 103 |
| 104 // Current query as UTF-16. |
| 105 string16 current_query_text_; |
| 106 |
| 107 // What the user has typed. |
| 108 string16 user_text_; |
| 109 |
| 110 // Fetcher used to retrieve results. |
| 111 scoped_ptr<net::URLFetcher> fetcher_; |
| 112 |
| 113 // Suggestions for the most recent query. |
| 114 std::vector<string16> results_; |
| 115 |
| 116 DISALLOW_COPY_AND_ASSIGN(ZerosuggestProvider); |
| 117 }; |
| 118 |
| 119 #endif // CHROME_BROWSER_AUTOCOMPLETE_ZEROSUGGEST_PROVIDER_H_ |
OLD | NEW |