Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / zero_suggest_provider.h
index 68acf23..0f4c093 100644 (file)
@@ -4,12 +4,7 @@
 //
 // This file contains the zero-suggest autocomplete provider. This experimental
 // provider is invoked when the user focuses in the omnibox prior to editing,
-// and generates search query suggestions based on the current URL. To enable
-// this provider, point --experimental-zero-suggest-url-prefix at an
-// appropriate suggestion service.
-//
-// HUGE DISCLAIMER: This is just here for experimenting and will probably be
-// deleted entirely as we revise how suggestions work with the omnibox.
+// and generates search query suggestions based on the current URL.
 
 #ifndef CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
 #define CHROME_BROWSER_AUTOCOMPLETE_ZERO_SUGGEST_PROVIDER_H_
 #include "base/memory/scoped_ptr.h"
 #include "chrome/browser/autocomplete/base_search_provider.h"
 #include "chrome/browser/autocomplete/search_provider.h"
+#include "chrome/browser/history/history_types.h"
+#include "components/metrics/proto/omnibox_event.pb.h"
+#include "net/url_request/url_fetcher_delegate.h"
 
+class AutocompleteProviderListener;
 class TemplateURLService;
 
 namespace base {
@@ -31,6 +30,10 @@ namespace net {
 class URLFetcher;
 }
 
+namespace user_prefs {
+class PrefRegistrySyncable;
+}
+
 // Autocomplete provider for searches based on the current URL.
 //
 // The controller will call StartZeroSuggest when the user focuses in the
@@ -41,21 +44,33 @@ class URLFetcher;
 // TODO(jered): Consider deleting this class and building this functionality
 // into SearchProvider after dogfood and after we break the association between
 // omnibox text and suggestions.
-class ZeroSuggestProvider : public BaseSearchProvider {
+class ZeroSuggestProvider : public BaseSearchProvider,
+                            public net::URLFetcherDelegate {
  public:
   // Creates and returns an instance of this provider.
   static ZeroSuggestProvider* Create(AutocompleteProviderListener* listener,
+                                     TemplateURLService* template_url_service,
                                      Profile* profile);
 
+  // Registers a preference used to cache zero suggest results.
+  static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
+
   // AutocompleteProvider:
   virtual void Start(const AutocompleteInput& input,
                      bool minimal_changes) OVERRIDE;
+  virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE;
 
   // Sets |field_trial_triggered_| to false.
   virtual void ResetSession() OVERRIDE;
 
+ protected:
+  // BaseSearchProvider:
+  virtual void ModifyProviderInfo(
+      metrics::OmniboxEventProto_ProviderInfo* provider_info) const OVERRIDE;
+
  private:
   ZeroSuggestProvider(AutocompleteProviderListener* listener,
+                      TemplateURLService* template_url_service,
                       Profile* profile);
 
   virtual ~ZeroSuggestProvider();
@@ -63,24 +78,30 @@ class ZeroSuggestProvider : public BaseSearchProvider {
   // BaseSearchProvider:
   virtual const TemplateURL* GetTemplateURL(bool is_keyword) const OVERRIDE;
   virtual const AutocompleteInput GetInput(bool is_keyword) const OVERRIDE;
-  virtual Results* GetResultsToFill(bool is_keyword) OVERRIDE;
   virtual bool ShouldAppendExtraParams(
-      const SuggestResult& result) const OVERRIDE;
+      const SearchSuggestionParser::SuggestResult& result) const OVERRIDE;
   virtual void StopSuggest() OVERRIDE;
   virtual void ClearAllResults() OVERRIDE;
-  virtual int GetDefaultResultRelevance() const OVERRIDE;
   virtual void RecordDeletionResult(bool success) OVERRIDE;
-  virtual void LogFetchComplete(bool success, bool is_keyword) OVERRIDE;
-  virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const OVERRIDE;
-  virtual void UpdateMatches() OVERRIDE;
+
+  // net::URLFetcherDelegate:
+  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
+
+  // Optionally, cache the received |json_data| and return true if we want
+  // to stop processing results at this point. The |parsed_data| is the parsed
+  // version of |json_data| used to determine if we received an empty result.
+  bool StoreSuggestionResponse(const std::string& json_data,
+                               const base::Value& parsed_data);
 
   // Adds AutocompleteMatches for each of the suggestions in |results| to
   // |map|.
-  void AddSuggestResultsToMap(const SuggestResults& results,
-                              MatchMap* map);
+  void AddSuggestResultsToMap(
+      const SearchSuggestionParser::SuggestResults& results,
+      MatchMap* map);
 
   // Returns an AutocompleteMatch for a navigational suggestion |navigation|.
-  AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
+  AutocompleteMatch NavigationToMatch(
+      const SearchSuggestionParser::NavigationResult& navigation);
 
   // Fetches zero-suggest suggestions by sending a request using |suggest_url|.
   void Run(const GURL& suggest_url);
@@ -105,19 +126,21 @@ class ZeroSuggestProvider : public BaseSearchProvider {
 
   // Whether we can show zero suggest on |current_page_url| without
   // sending |current_page_url| as a parameter to the server at |suggest_url|.
-  bool CanShowZeroSuggestWithoutSendingURL(
-      const GURL& suggest_url,
-      const GURL& current_page_url) const;
+  bool CanShowZeroSuggestWithoutSendingURL(const GURL& suggest_url,
+                                           const GURL& current_page_url) const;
 
-  // Used to build default search engine URLs for suggested queries.
-  TemplateURLService* template_url_service_;
+  // Checks whether we have a set of zero suggest results cached, and if so
+  // populates |matches_| with cached results.
+  void MaybeUseCachedSuggestions();
+
+  AutocompleteProviderListener* listener_;
 
   // The URL for which a suggestion fetch is pending.
   std::string current_query_;
 
   // The type of page the user is viewing (a search results page doing search
   // term replacement, an arbitrary URL, etc.).
-  AutocompleteInput::PageClassification current_page_classification_;
+  metrics::OmniboxEventProto::PageClassification current_page_classification_;
 
   // Copy of OmniboxEditModel::permanent_text_.
   base::string16 permanent_text_;
@@ -130,7 +153,10 @@ class ZeroSuggestProvider : public BaseSearchProvider {
 
   // Contains suggest and navigation results as well as relevance parsed from
   // the response for the most recent zero suggest input URL.
-  Results results_;
+  SearchSuggestionParser::Results results_;
+
+  // Whether we are currently showing cached zero suggest results.
+  bool results_from_cache_;
 
   history::MostVisitedURLList most_visited_urls_;