Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / search_provider.h
index 7e5dfdf..7140c5c 100644 (file)
 #include "base/time/time.h"
 #include "base/timer/timer.h"
 #include "chrome/browser/autocomplete/base_search_provider.h"
-#include "chrome/browser/history/history_types.h"
-#include "chrome/browser/search_engines/template_url.h"
 #include "components/metrics/proto/omnibox_input_type.pb.h"
+#include "components/search_engines/template_url.h"
+#include "net/url_request/url_fetcher_delegate.h"
 
+class AutocompleteProviderListener;
+class AutocompleteResult;
 class Profile;
 class SearchProviderTest;
 class TemplateURLService;
 
+namespace history {
+struct KeywordSearchTermVisit;
+}
+
 namespace net {
 class URLFetcher;
 }
@@ -39,14 +45,21 @@ class URLFetcher;
 // text.  It also starts a task to query the Suggest servers.  When that data
 // comes back, the provider creates and returns matches for the best
 // suggestions.
-class SearchProvider : public BaseSearchProvider {
+class SearchProvider : public BaseSearchProvider,
+                       public net::URLFetcherDelegate {
  public:
-  SearchProvider(AutocompleteProviderListener* listener, Profile* profile);
+  SearchProvider(AutocompleteProviderListener* listener,
+                 TemplateURLService* template_url_service,
+                 Profile* profile);
 
   // Extracts the suggest response metadata which SearchProvider previously
   // stored for |match|.
   static std::string GetSuggestMetadata(const AutocompleteMatch& match);
 
+  // Answers prefetch handling - register displayed answers. Takes the top
+  // match for Autocomplete and registers the contained answer data, if any.
+  void RegisterDisplayedAnswers(const AutocompleteResult& result);
+
   // AutocompleteProvider:
   virtual void ResetSession() OVERRIDE;
 
@@ -69,6 +82,7 @@ class SearchProvider : public BaseSearchProvider {
   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch);
   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestQueryUsesToken);
   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SessionToken);
+  FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, AnswersCache);
   FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, GetDestinationURL);
   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, ClearPrefetchedResults);
   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, SetPrefetchQuery);
@@ -122,19 +136,20 @@ class SearchProvider : public BaseSearchProvider {
 
   class CompareScoredResults;
 
+  struct AnswersQueryData {
+    base::string16 full_query_text;
+    base::string16 query_type;
+  };
+
   typedef std::vector<history::KeywordSearchTermVisit> HistoryResults;
 
   // Removes non-inlineable results until either the top result can inline
   // autocomplete the current input or verbatim outscores the top result.
-  static void RemoveStaleResults(const base::string16& input,
-                                 int verbatim_relevance,
-                                 SuggestResults* suggest_results,
-                                 NavigationResults* navigation_results);
-
-  // Recalculates the match contents class of |results| to better display
-  // against the current input and user's language.
-  void UpdateMatchContentsClass(const base::string16& input_text,
-                                Results* results);
+  static void RemoveStaleResults(
+      const base::string16& input,
+      int verbatim_relevance,
+      SearchSuggestionParser::SuggestResults* suggest_results,
+      SearchSuggestionParser::NavigationResults* navigation_results);
 
   // Calculates the relevance score for the keyword verbatim result (if the
   // input matches one of the profile's keyword).
@@ -147,21 +162,31 @@ class SearchProvider : public BaseSearchProvider {
                      bool minimal_changes) OVERRIDE;
 
   // BaseSearchProvider:
-  virtual void SortResults(bool is_keyword,
-                           const base::ListValue* relevances,
-                           Results* results) OVERRIDE;
   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;
+
+  // Recalculates the match contents class of |results| to better display
+  // against the current input and user's language.
+  void UpdateMatchContentsClass(const base::string16& input_text,
+                                SearchSuggestionParser::Results* results);
+
+  // Called after ParseSuggestResults to rank the |results|.
+  void SortResults(bool is_keyword, SearchSuggestionParser::Results* results);
+
+  // Records UMA statistics about a suggest server response.
+  void LogFetchComplete(bool success, bool is_keyword);
+
+  // Updates |matches_| from the latest results; applies calculated relevances
+  // if suggested relevances cause undesirable behavior. Updates |done_|.
+  void UpdateMatches();
 
   // Called when timer_ expires.
   void Run();
@@ -186,8 +211,10 @@ class SearchProvider : public BaseSearchProvider {
 
   // Apply calculated relevance scores to the current results.
   void ApplyCalculatedRelevance();
-  void ApplyCalculatedSuggestRelevance(SuggestResults* list);
-  void ApplyCalculatedNavigationRelevance(NavigationResults* list);
+  void ApplyCalculatedSuggestRelevance(
+      SearchSuggestionParser::SuggestResults* list);
+  void ApplyCalculatedNavigationRelevance(
+      SearchSuggestionParser::NavigationResults* list);
 
   // Starts a new URLFetcher requesting suggest results from |template_url|;
   // callers own the returned URLFetcher, which is NULL for invalid providers.
@@ -202,15 +229,15 @@ class SearchProvider : public BaseSearchProvider {
   // be chosen as default.
   ACMatches::const_iterator FindTopMatch() const;
 
-  // Checks if suggested relevances violate certain expected constraints.
-  // See UpdateMatches() for the use and explanation of these constraints.
-  bool HasKeywordDefaultMatchInKeywordMode() const;
+  // Checks if suggested relevances violate an expected constraint.
+  // See UpdateMatches() for the use and explanation of this constraint
+  // and other constraints enforced without the use of helper functions.
   bool IsTopMatchSearchWithURLInput() const;
 
   // Converts an appropriate number of navigation results in
   // |navigation_results| to matches and adds them to |matches|.
   void AddNavigationResultsToMatches(
-      const NavigationResults& navigation_results,
+      const SearchSuggestionParser::NavigationResults& navigation_results,
       ACMatches* matches);
 
   // Adds a match for each result in |results| to |map|. |is_keyword| indicates
@@ -221,16 +248,18 @@ class SearchProvider : public BaseSearchProvider {
                               MatchMap* map);
 
   // Calculates relevance scores for all |results|.
-  SuggestResults ScoreHistoryResults(const HistoryResults& results,
-                                     bool base_prevent_inline_autocomplete,
-                                     bool input_multiple_words,
-                                     const base::string16& input_text,
-                                     bool is_keyword);
+  SearchSuggestionParser::SuggestResults ScoreHistoryResults(
+      const HistoryResults& results,
+      bool base_prevent_inline_autocomplete,
+      bool input_multiple_words,
+      const base::string16& input_text,
+      bool is_keyword);
 
   // Adds matches for |results| to |map|.
-  void AddSuggestResultsToMap(const SuggestResults& results,
-                              const std::string& metadata,
-                              MatchMap* map);
+  void AddSuggestResultsToMap(
+      const SearchSuggestionParser::SuggestResults& results,
+      const std::string& metadata,
+      MatchMap* map);
 
   // Gets the relevance score for the verbatim result.  This value may be
   // provided by the suggest server or calculated locally; if
@@ -269,7 +298,8 @@ class SearchProvider : public BaseSearchProvider {
                                    bool prevent_search_history_inlining) const;
 
   // Returns an AutocompleteMatch for a navigational suggestion.
-  AutocompleteMatch NavigationToMatch(const NavigationResult& navigation);
+  AutocompleteMatch NavigationToMatch(
+      const SearchSuggestionParser::NavigationResult& navigation);
 
   // Updates the value of |done_| from the internal state.
   void UpdateDone();
@@ -277,10 +307,20 @@ class SearchProvider : public BaseSearchProvider {
   // Obtains a session token, regenerating if necessary.
   std::string GetSessionToken();
 
+  // Answers prefetch handling - finds previously displayed answer matching the
+  // current |input| and sets |prefetch_data_|.
+  void DoAnswersQuery(const AutocompleteInput& input);
+
   // The amount of time to wait before sending a new suggest request after the
   // previous one.  Non-const because some unittests modify this value.
   static int kMinimumTimeBetweenSuggestQueriesMs;
 
+  AutocompleteProviderListener* listener_;
+
+  // The number of suggest results that haven't yet arrived. If it's greater
+  // than 0, it indicates that one of the URLFetchers is still running.
+  int suggest_results_pending_;
+
   // Maintains the TemplateURLs used.
   Providers providers_;
 
@@ -306,8 +346,8 @@ class SearchProvider : public BaseSearchProvider {
   scoped_ptr<net::URLFetcher> default_fetcher_;
 
   // Results from the default and keyword search providers.
-  Results default_results_;
-  Results keyword_results_;
+  SearchSuggestionParser::Results default_results_;
+  SearchSuggestionParser::Results keyword_results_;
 
   GURL current_page_url_;
 
@@ -315,6 +355,10 @@ class SearchProvider : public BaseSearchProvider {
   std::string current_token_;
   base::TimeTicks token_expiration_time_;
 
+  // Answers prefetch management.
+  AnswersQueryData prefetch_data_;     // Data to use for query prefetching.
+  AnswersQueryData last_answer_seen_;  // Last answer seen.
+
   DISALLOW_COPY_AND_ASSIGN(SearchProvider);
 };