zero_suggest_provider.h revision 010d83a9304c5a91596085d917d248abff47903a
15821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Copyright (c) 2012 The Chromium Authors. All rights reserved. 25821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Use of this source code is governed by a BSD-style license that can be 35821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// found in the LICENSE file. 45821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 55821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This file contains the zero-suggest autocomplete provider. This experimental 65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// provider is invoked when the user focuses in the omnibox prior to editing, 75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// and generates search query suggestions based on the current URL. To enable 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this provider, point --experimental-zero-suggest-url-prefix at an 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// appropriate suggestion service. 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HUGE DISCLAIMER: This is just here for experimenting and will probably be 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// deleted entirely as we revise how suggestions work with the omnibox. 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/autocomplete/base_search_provider.h" 21868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)#include "chrome/browser/autocomplete/search_provider.h" 225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class TemplateURLService; 245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace base { 26868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)class ListValue; 275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Value; 285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace net { 315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class URLFetcher; 325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)} 335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 34010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)namespace user_prefs { 35010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)class PrefRegistrySyncable; 36010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles)} 37010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Autocomplete provider for searches based on the current URL. 395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The controller will call StartZeroSuggest when the user focuses in the 415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// omnibox. After construction, the autocomplete controller repeatedly calls 425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Start() with some user input, each time expecting to receive an updated 435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// set of matches. 445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// 455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(jered): Consider deleting this class and building this functionality 465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// into SearchProvider after dogfood and after we break the association between 475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// omnibox text and suggestions. 485d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ZeroSuggestProvider : public BaseSearchProvider { 495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 50868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Creates and returns an instance of this provider. 515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile); 535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 54010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Registers a preference used to cache zero suggest results. 55010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AutocompleteProvider: 585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Start(const AutocompleteInput& input, 59effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch bool minimal_changes) OVERRIDE; 60010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 61868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 62868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Sets |field_trial_triggered_| to false. 63868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual void ResetSession() OVERRIDE; 64868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 65010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) protected: 66010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // BaseSearchProvider: 67010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual void ModifyProviderInfo( 68010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE; 69010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ZeroSuggestProvider(AutocompleteProviderListener* listener, 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Profile* profile); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~ZeroSuggestProvider(); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // BaseSearchProvider: 77010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual bool StoreSuggestionResponse(const std::string& json_data, 78010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const base::Value& parsed_data) OVERRIDE; 7923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; 80a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; 8123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual Results* GetResultsToFill(bool is_keyword) OVERRIDE; 825d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual bool ShouldAppendExtraParams( 835d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) const SuggestResult& result) const OVERRIDE; 845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void StopSuggest() OVERRIDE; 855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual void ClearAllResults() OVERRIDE; 86a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual int GetDefaultResultRelevance() const OVERRIDE; 87a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void RecordDeletionResult(bool success) OVERRIDE; 8823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE; 8923730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE; 9023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual void UpdateMatches() OVERRIDE; 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Adds AutocompleteMatches for each of the suggestions in |results| to 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |map|. 94010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) void AddSuggestResultsToMap(const SuggestResults& results, MatchMap* map); 95868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 96868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns an AutocompleteMatch for a navigational suggestion |navigation|. 975d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) AutocompleteMatch NavigationToMatch(const NavigationResult& navigation); 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 99f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Fetches zero-suggest suggestions by sending a request using |suggest_url|. 100f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void Run(const GURL& suggest_url); 1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 102eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Converts the parsed results to a set of AutocompleteMatches and adds them 103eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // to |matches_|. Also update the histograms for how many results were 104eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // received. 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void ConvertResultsToAutocompleteMatches(); 1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 107868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns an AutocompleteMatch for the current URL. The match should be in 108868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // the top position so that pressing enter has the effect of reloading the 109868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // page. 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AutocompleteMatch MatchForCurrentURL(); 1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 112d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // When the user is in the Most Visited field trial, we ask the TopSites 113d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // service for the most visited URLs during Run(). It calls back to this 114d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // function to return those |urls|. 115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls); 116d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 117a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Returns the relevance score for the verbatim result. 118a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int GetVerbatimRelevance() const; 119a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12023730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // Whether we can show zero suggest on |current_page_url| without 12123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // sending |current_page_url| as a parameter to the server at |suggest_url|. 122010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url, 123010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const GURL& current_page_url) const; 124010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Checks whether we have a set of zero suggest results cached, and if so 126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // populates |matches_| with cached results. 127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) void MaybeUseCachedSuggestions(); 12823730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Used to build default search engine URLs for suggested queries. 1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) TemplateURLService* template_url_service_; 1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The URL for which a suggestion fetch is pending. 1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string current_query_; 1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 135d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // The type of page the user is viewing (a search results page doing search 136d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // term replacement, an arbitrary URL, etc.). 137d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch AutocompleteInput::PageClassification current_page_classification_; 138d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 1397d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Copy of OmniboxEditModel::permanent_text_. 140a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 permanent_text_; 1415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Fetcher used to retrieve results. 1435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<net::URLFetcher> fetcher_; 144868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 145868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Suggestion for the current URL. 146868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AutocompleteMatch current_url_match_; 147a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 148a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Contains suggest and navigation results as well as relevance parsed from 149a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // the response for the most recent zero suggest input URL. 150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) Results results_; 151868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 152010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Whether we are currently showing cached zero suggest results. 153010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) bool results_from_cache_; 154010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 155d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) history::MostVisitedURLList most_visited_urls_; 156d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 157d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // For callbacks that may be run after destruction. 158d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_; 159d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 1605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); 1615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 164