zero_suggest_provider.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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 27class AutocompleteInput; 28class GURL; 29class TemplateURLService; 30 31namespace base { 32class Value; 33} 34 35namespace net { 36class 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. 49class ZeroSuggestProvider : public AutocompleteProvider, 50 public net::URLFetcherDelegate { 51 public: 52 // Creates and returns an instance of this provider if the feature is enabled. 53 // Returns NULL if not enabled. 54 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, 55 Profile* profile); 56 57 // AutocompleteProvider: 58 virtual void Start(const AutocompleteInput& input, 59 bool /*minimal_changes*/) OVERRIDE; 60 virtual void Stop(bool clear_cached_results) OVERRIDE; 61 62 // net::URLFetcherDelegate 63 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 64 65 // Initiates a new fetch for the given |url|, limiting suggestions to those 66 // matching |user_text|. |user_text| may be non-empty if the user previously 67 // interacted with zero-suggest suggestions and then unfocused the omnibox. 68 // TODO(jered): Rip out |user_text| once the first match is decoupled from 69 // the current typing in the omnibox. 70 void StartZeroSuggest(const GURL& url, const string16& user_text); 71 72 private: 73 ZeroSuggestProvider(AutocompleteProviderListener* listener, 74 Profile* profile, 75 const std::string& url_prefix); 76 77 virtual ~ZeroSuggestProvider(); 78 79 // Update matches given the user has typed |user_text|. 80 void UpdateMatches(const string16& user_text); 81 82 // Fetches zero-suggest suggestions for |current_query_|. 83 void Run(); 84 85 // Parses results from the zero-suggest server and updates results. 86 // Returns true if results were updated. 87 bool ParseSuggestResults(base::Value* root_val); 88 89 // Converts the parsed results to a set of AutocompleteMatches and adds them 90 // to |matches_|. 91 void ConvertResultsToAutocompleteMatches(); 92 93 // Adds a URL suggestion for the current URL. This should be in the top 94 // position so that pressing enter has the effect of reloading the page. 95 void AddMatchForCurrentURL(); 96 97 // Adds a query suggestion from response position |result_index| with text 98 // |result| to |matches_|. Uses |search_provider| to build a search URL for 99 // this match. 100 void AddMatchForResult(const TemplateURL* search_provider, 101 size_t result_index, 102 const string16& result); 103 104 // Prefix of the URL from which to fetch zero-suggest suggestions. 105 const std::string url_prefix_; 106 107 // Used to build default search engine URLs for suggested queries. 108 TemplateURLService* template_url_service_; 109 110 // The URL for which a suggestion fetch is pending. 111 std::string current_query_; 112 113 // What the user has typed. 114 string16 user_text_; 115 116 // Fetcher used to retrieve results. 117 scoped_ptr<net::URLFetcher> fetcher_; 118 119 // Suggestions for the most recent query. 120 std::vector<string16> results_; 121 122 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); 123}; 124 125#endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 126