zero_suggest_provider.h revision a1401311d1ab56c4ed0a474bd38c108f75cb0cd9
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)
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Autocomplete provider for searches based on the current URL.
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The controller will call StartZeroSuggest when the user focuses in the
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// omnibox. After construction, the autocomplete controller repeatedly calls
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Start() with some user input, each time expecting to receive an updated
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// set of matches.
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// TODO(jered): Consider deleting this class and building this functionality
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// into SearchProvider after dogfood and after we break the association between
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// omnibox text and suggestions.
445d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)class ZeroSuggestProvider : public BaseSearchProvider {
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
46868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Creates and returns an instance of this provider.
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                     Profile* profile);
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // AutocompleteProvider:
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Start(const AutocompleteInput& input,
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     bool /*minimal_changes*/) OVERRIDE;
53868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
54868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Sets |field_trial_triggered_| to false.
55868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  virtual void ResetSession() OVERRIDE;
56868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // net::URLFetcherDelegate
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
60d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // Initiates a new fetch for the given |url| of classification
61d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // |page_classification|. |permanent_text| is the omnibox text
62d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // for the current page.
63d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  void StartZeroSuggest(
64f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)      const GURL& curent_page_url,
65d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch      AutocompleteInput::PageClassification page_classification,
66a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& permanent_text);
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ZeroSuggestProvider(AutocompleteProviderListener* listener,
70868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)                      Profile* profile);
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual ~ZeroSuggestProvider();
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
745d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // BaseSearchProvider:
755d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual const TemplateURL* GetTemplateURL(
765d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const SuggestResult& result) const OVERRIDE;
77a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE;
785d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual bool ShouldAppendExtraParams(
795d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const SuggestResult& result) const OVERRIDE;
805d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void StopSuggest() OVERRIDE;
815d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  virtual void ClearAllResults() OVERRIDE;
82a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual int GetDefaultResultRelevance() const OVERRIDE;
83a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  virtual void RecordDeletionResult(bool success) OVERRIDE;
845d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
855d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Adds AutocompleteMatches for each of the suggestions in |results| to
865d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |map|.
875d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  void AddSuggestResultsToMap(const SuggestResults& results,
885d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)                              MatchMap* map);
89868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
90868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
92868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
93f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
94f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  void Run(const GURL& suggest_url);
955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
96eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // Converts the parsed results to a set of AutocompleteMatches and adds them
97eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // to |matches_|.  Also update the histograms for how many results were
98eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  // received.
99eb525c5499e34cc9c4b825d6d9e75bb07cc06aceBen Murdoch  void ConvertResultsToAutocompleteMatches();
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
101868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Returns an AutocompleteMatch for the current URL. The match should be in
102868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // the top position so that pressing enter has the effect of reloading the
103868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // page.
104868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AutocompleteMatch MatchForCurrentURL();
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
106d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // When the user is in the Most Visited field trial, we ask the TopSites
107d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // service for the most visited URLs during Run().  It calls back to this
108d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // function to return those |urls|.
109d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  void OnMostVisitedUrlsAvailable(const history::MostVisitedURLList& urls);
110d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
111a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Returns the relevance score for the verbatim result.
112a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  int GetVerbatimRelevance() const;
113a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used to build default search engine URLs for suggested queries.
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  TemplateURLService* template_url_service_;
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The URL for which a suggestion fetch is pending.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string current_query_;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
120d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // The type of page the user is viewing (a search results page doing search
121d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  // term replacement, an arbitrary URL, etc.).
122d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch  AutocompleteInput::PageClassification current_page_classification_;
123d3868032626d59662ff73b372b5d584c1d144c53Ben Murdoch
1247d4cd473f85ac64c3747c96c277f9e506a0d2246Torne (Richard Coles)  // Copy of OmniboxEditModel::permanent_text_.
125a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  base::string16 permanent_text_;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Fetcher used to retrieve results.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  scoped_ptr<net::URLFetcher> fetcher_;
129868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Whether there's a pending request in flight.
130868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  bool have_pending_request_;
131868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
132868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  // Suggestion for the current URL.
133868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)  AutocompleteMatch current_url_match_;
134a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)
135a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // Contains suggest and navigation results as well as relevance parsed from
136a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  // the response for the most recent zero suggest input URL.
137a1401311d1ab56c4ed0a474bd38c108f75cb0cd9Torne (Richard Coles)  Results results_;
138868fa2fe829687343ffae624259930155e16dbd8Torne (Richard Coles)
139d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  history::MostVisitedURLList most_visited_urls_;
140d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
141d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  // For callbacks that may be run after destruction.
142d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)  base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
143d0247b1b59f9c528cb6df88b4f2b9afaf80d181eTorne (Richard Coles)
1445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
1455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
148