zero_suggest_provider.h revision eb525c5499e34cc9c4b825d6d9e75bb07cc06ace
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 <map> 18#include <string> 19#include <vector> 20 21#include "base/basictypes.h" 22#include "base/compiler_specific.h" 23#include "base/memory/scoped_ptr.h" 24#include "base/strings/string16.h" 25#include "chrome/browser/autocomplete/autocomplete_provider.h" 26#include "chrome/browser/autocomplete/search_provider.h" 27#include "net/url_request/url_fetcher_delegate.h" 28 29class AutocompleteInput; 30class GURL; 31class TemplateURLService; 32 33namespace base { 34class ListValue; 35class Value; 36} 37 38namespace net { 39class URLFetcher; 40} 41 42// Autocomplete provider for searches based on the current URL. 43// 44// The controller will call StartZeroSuggest when the user focuses in the 45// omnibox. After construction, the autocomplete controller repeatedly calls 46// Start() with some user input, each time expecting to receive an updated 47// set of matches. 48// 49// TODO(jered): Consider deleting this class and building this functionality 50// into SearchProvider after dogfood and after we break the association between 51// omnibox text and suggestions. 52class ZeroSuggestProvider : public AutocompleteProvider, 53 public net::URLFetcherDelegate { 54 public: 55 // Creates and returns an instance of this provider. 56 static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, 57 Profile* profile); 58 59 // AutocompleteProvider: 60 virtual void Start(const AutocompleteInput& input, 61 bool /*minimal_changes*/) OVERRIDE; 62 virtual void Stop(bool clear_cached_results) OVERRIDE; 63 64 // Adds provider-specific information to omnibox event logs. 65 virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; 66 67 // Sets |field_trial_triggered_| to false. 68 virtual void ResetSession() OVERRIDE; 69 70 // net::URLFetcherDelegate 71 virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 72 73 // Initiates a new fetch for the given |url|. |user_text| is the user input 74 // and may be non-empty if the user previously interacted with 75 // zero-suggest suggestions and then unfocused the omnibox. |permanent_text| 76 // is the omnibox text for the current page. 77 // TODO(jered): Rip out |user_text| once the first match is decoupled from 78 // the current typing in the omnibox. 79 void StartZeroSuggest(const GURL& url, 80 const string16& permanent_text); 81 82 private: 83 ZeroSuggestProvider(AutocompleteProviderListener* listener, 84 Profile* profile); 85 86 virtual ~ZeroSuggestProvider(); 87 88 bool ShouldRunZeroSuggest(const GURL& url) const; 89 90 // The 4 functions below (that take classes defined in SearchProvider as 91 // arguments) were copied and trimmed from SearchProvider. 92 // TODO(hfung): Refactor them into a new base class common to both 93 // ZeroSuggestProvider and SearchProvider. 94 95 // From the OpenSearch formatted response |root_val|, populate query 96 // suggestions into |suggest_results|, navigation suggestions into 97 // |navigation_results|, and the verbatim relevance score into 98 // |verbatim_relevance|. 99 void FillResults(const base::Value& root_val, 100 int* verbatim_relevance, 101 SearchProvider::SuggestResults* suggest_results, 102 SearchProvider::NavigationResults* navigation_results); 103 104 // Creates AutocompleteMatches to search |template_url| for "<suggestion>" for 105 // all suggestions in |results|, and adds them to |map|. 106 void AddSuggestResultsToMap(const SearchProvider::SuggestResults& results, 107 const TemplateURL* template_url, 108 SearchProvider::MatchMap* map); 109 110 // Creates an AutocompleteMatch with the provided |relevance| and |type| to 111 // search |template_url| for |query_string|. |accepted_suggestion| will be 112 // used to generate Assisted Query Stats. 113 // 114 // Adds this match to |map|; if such a match already exists, whichever one 115 // has lower relevance is eliminated. 116 void AddMatchToMap(int relevance, 117 AutocompleteMatch::Type type, 118 const TemplateURL* template_url, 119 const string16& query_string, 120 int accepted_suggestion, 121 SearchProvider::MatchMap* map); 122 123 // Returns an AutocompleteMatch for a navigational suggestion |navigation|. 124 AutocompleteMatch NavigationToMatch( 125 const SearchProvider::NavigationResult& navigation); 126 127 // Fetches zero-suggest suggestions for |current_query_|. 128 void Run(); 129 130 // Parses results from the zero-suggest server and updates results. 131 void ParseSuggestResults(const base::Value& root_val); 132 133 // Converts the parsed results to a set of AutocompleteMatches and adds them 134 // to |matches_|. Also update the histograms for how many results were 135 // received. 136 void ConvertResultsToAutocompleteMatches(); 137 138 // Returns an AutocompleteMatch for the current URL. The match should be in 139 // the top position so that pressing enter has the effect of reloading the 140 // page. 141 AutocompleteMatch MatchForCurrentURL(); 142 143 // Used to build default search engine URLs for suggested queries. 144 TemplateURLService* template_url_service_; 145 146 // The URL for which a suggestion fetch is pending. 147 std::string current_query_; 148 149 // Copy of OmniboxEditModel::permanent_text_. 150 string16 permanent_text_; 151 152 // Fetcher used to retrieve results. 153 scoped_ptr<net::URLFetcher> fetcher_; 154 // Whether there's a pending request in flight. 155 bool have_pending_request_; 156 157 // Suggestion for the current URL. 158 AutocompleteMatch current_url_match_; 159 // Navigation suggestions for the most recent ZeroSuggest input URL. 160 SearchProvider::NavigationResults navigation_results_; 161 // Query suggestions for the most recent ZeroSuggest input URL. 162 SearchProvider::MatchMap query_matches_map_; 163 // The relevance score for the URL of the current page. 164 int verbatim_relevance_; 165 166 // Whether a field trial, if any, has triggered in the most recent 167 // autocomplete query. This field is set to true if the last request 168 // was a zero suggest request, the provider has completed and their 169 // corresponding response contained '"google:fieldtrialtriggered":true'. 170 bool field_trial_triggered_; 171 // Whether a zero suggest request triggered a field trial in the omnibox 172 // session. The user could have clicked on a suggestion when zero suggest 173 // triggered (same condition as field_trial_triggered_), or triggered zero 174 // suggest but kept typing. 175 bool field_trial_triggered_in_session_; 176 177 DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); 178}; 179 180#endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 181