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, 76e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)// and generates search query suggestions based on the current URL. 85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/basictypes.h" 135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h" 145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/memory/scoped_ptr.h" 151320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/history/core/browser/history_types.h" 166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)#include "components/metrics/proto/omnibox_event.pb.h" 171320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/omnibox/base_search_provider.h" 181320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci#include "components/omnibox/search_provider.h" 196e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "net/url_request/url_fetcher_delegate.h" 205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 216e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class AutocompleteProviderListener; 221320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucciclass Profile; 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. 486e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)class ZeroSuggestProvider : public BaseSearchProvider, 496e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) public net::URLFetcherDelegate { 505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public: 51868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Creates and returns an instance of this provider. 525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener, 535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) TemplateURLService* template_url_service, 545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) Profile* profile); 555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 56010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Registers a preference used to cache zero suggest results. 57010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry); 58010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // AutocompleteProvider: 605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual void Start(const AutocompleteInput& input, 61effb81e5f8246d0db0270817048dc992db66e9fbBen Murdoch bool minimal_changes) OVERRIDE; 6203b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) virtual void Stop(bool clear_cached_results) OVERRIDE; 63010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 6403b57e008b61dfcb1fbad3aea950ae0e001748b0Torne (Richard Coles) virtual void AddProviderInfo(ProvidersInfo* provider_info) const OVERRIDE; 65868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 66868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Sets |field_trial_triggered_| to false. 67868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) virtual void ResetSession() OVERRIDE; 68868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private: 705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) ZeroSuggestProvider(AutocompleteProviderListener* listener, 715f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) TemplateURLService* template_url_service, 72868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) Profile* profile); 735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) virtual ~ZeroSuggestProvider(); 755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // BaseSearchProvider: 7723730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE; 78a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE; 795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) virtual bool ShouldAppendExtraParams( 805f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const SearchSuggestionParser::SuggestResult& result) const OVERRIDE; 81a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) virtual void RecordDeletionResult(bool success) OVERRIDE; 826e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 836e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // net::URLFetcherDelegate: 846e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE; 856e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 866e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // Optionally, cache the received |json_data| and return true if we want 876e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // to stop processing results at this point. The |parsed_data| is the parsed 886e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) // version of |json_data| used to determine if we received an empty result. 896e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) bool StoreSuggestionResponse(const std::string& json_data, 906e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) const base::Value& parsed_data); 915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) 925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // Adds AutocompleteMatches for each of the suggestions in |results| to 935d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles) // |map|. 945f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) void AddSuggestResultsToMap( 955f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const SearchSuggestionParser::SuggestResults& results, 965f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) MatchMap* map); 97868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 98868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns an AutocompleteMatch for a navigational suggestion |navigation|. 995f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) AutocompleteMatch NavigationToMatch( 1005f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) const SearchSuggestionParser::NavigationResult& navigation); 101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 102f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) // Fetches zero-suggest suggestions by sending a request using |suggest_url|. 103f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles) void Run(const GURL& suggest_url); 1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 105eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // Converts the parsed results to a set of AutocompleteMatches and adds them 106eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // to |matches_|. Also update the histograms for how many results were 107eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch // received. 108eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch void ConvertResultsToAutocompleteMatches(); 1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 110868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Returns an AutocompleteMatch for the current URL. The match should be in 111868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // the top position so that pressing enter has the effect of reloading the 112868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // page. 113868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AutocompleteMatch MatchForCurrentURL(); 1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 115d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // When the user is in the Most Visited field trial, we ask the TopSites 116d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // service for the most visited URLs during Run(). It calls back to this 117d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // function to return those |urls|. 118d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls); 119d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 120a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Returns the relevance score for the verbatim result. 121a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) int GetVerbatimRelevance() const; 122a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 12323730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // Whether we can show zero suggest on |current_page_url| without 12423730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) // sending |current_page_url| as a parameter to the server at |suggest_url|. 125010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url, 126010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) const GURL& current_page_url) const; 127010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 128010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Checks whether we have a set of zero suggest results cached, and if so 129010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // populates |matches_| with cached results. 130010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) void MaybeUseCachedSuggestions(); 13123730a6e56a168d1879203e4b3819bb36e3d8f1fTorne (Richard Coles) 1326e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) AutocompleteProviderListener* listener_; 1331320f92c476a1ad9d19dba2a48c72b75566198e9Primiano Tucci Profile* profile_; 1346e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles) 1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // The URL for which a suggestion fetch is pending. 1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) std::string current_query_; 1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 138d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // The type of page the user is viewing (a search results page doing search 139d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch // term replacement, an arbitrary URL, etc.). 1406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles) metrics::OmniboxEventProto::PageClassification current_page_classification_; 141d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch 1427d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles) // Copy of OmniboxEditModel::permanent_text_. 143a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles) base::string16 permanent_text_; 1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) // Fetcher used to retrieve results. 1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) scoped_ptr<net::URLFetcher> fetcher_; 147868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 148868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) // Suggestion for the current URL. 149868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) AutocompleteMatch current_url_match_; 150a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) 151a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // Contains suggest and navigation results as well as relevance parsed from 152a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles) // the response for the most recent zero suggest input URL. 1535f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles) SearchSuggestionParser::Results results_; 154868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles) 155010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) // Whether we are currently showing cached zero suggest results. 156010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) bool results_from_cache_; 157010d83a9304c5a91596085d917d248abff47903aTorne (Richard Coles) 158d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) history::MostVisitedURLList most_visited_urls_; 159d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 160d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) // For callbacks that may be run after destruction. 161d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_; 162d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles) 1635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider); 1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}; 1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) 1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_ 167