Upstream version 9.38.198.0
[platform/framework/web/crosswalk.git] / src / chrome / browser / autocomplete / base_search_provider.h
index a3a788a..66f26aa 100644 (file)
 
 #include "base/memory/scoped_vector.h"
 #include "base/strings/string16.h"
-#include "chrome/browser/autocomplete/autocomplete_input.h"
-#include "chrome/browser/autocomplete/autocomplete_match.h"
-#include "chrome/browser/autocomplete/autocomplete_provider.h"
 #include "components/metrics/proto/omnibox_event.pb.h"
-#include "net/url_request/url_fetcher_delegate.h"
+#include "components/omnibox/autocomplete_input.h"
+#include "components/omnibox/autocomplete_match.h"
+#include "components/omnibox/autocomplete_provider.h"
+#include "components/omnibox/search_suggestion_parser.h"
 
-class AutocompleteProviderListener;
 class GURL;
 class Profile;
+class SearchTermsData;
 class SuggestionDeletionHandler;
 class TemplateURL;
+class TemplateURLService;
 
 namespace base {
 class DictionaryValue;
@@ -37,8 +38,7 @@ class Value;
 // Base functionality for receiving suggestions from a search engine.
 // This class is abstract and should only be used as a base for other
 // autocomplete providers utilizing its functionality.
-class BaseSearchProvider : public AutocompleteProvider,
-                           public net::URLFetcherDelegate {
+class BaseSearchProvider : public AutocompleteProvider {
  public:
   // ID used in creating URLFetcher for default provider's suggest results.
   static const int kDefaultProviderURLFetcherID;
@@ -49,7 +49,7 @@ class BaseSearchProvider : public AutocompleteProvider,
   // ID used in creating URLFetcher for deleting suggestion results.
   static const int kDeletionURLFetcherID;
 
-  BaseSearchProvider(AutocompleteProviderListener* listener,
+  BaseSearchProvider(TemplateURLService* template_url_service,
                      Profile* profile,
                      AutocompleteProvider::Type type);
 
@@ -57,7 +57,8 @@ class BaseSearchProvider : public AutocompleteProvider,
   static bool ShouldPrefetch(const AutocompleteMatch& match);
 
   // Returns a simpler AutocompleteMatch suitable for persistence like in
-  // ShortcutsDatabase.
+  // ShortcutsDatabase.  This wrapper function uses a number of default values
+  // that may or may not be appropriate for your needs.
   // NOTE: Use with care. Most likely you want the other CreateSearchSuggestion
   // with protected access.
   static AutocompleteMatch CreateSearchSuggestion(
@@ -76,8 +77,6 @@ class BaseSearchProvider : public AutocompleteProvider,
     return field_trial_triggered_in_session_;
   }
 
-  void set_in_app_list() { in_app_list_ = true; }
-
  protected:
   // The following keys are used to record additional information on matches.
 
@@ -101,239 +100,10 @@ class BaseSearchProvider : public AutocompleteProvider,
 
   virtual ~BaseSearchProvider();
 
-  // The Result classes are intermediate representations of AutocompleteMatches,
-  // simply containing relevance-ranked search and navigation suggestions.
-  // They may be cached to provide some synchronous matches while requests for
-  // new suggestions from updated input are in flight.
-  // TODO(msw) Extend these classes to generate their corresponding matches and
-  //           other requisite data, in order to consolidate and simplify the
-  //           highly fragmented SearchProvider logic for each Result type.
-  class Result {
-   public:
-    Result(bool from_keyword_provider,
-           int relevance,
-           bool relevance_from_server,
-           AutocompleteMatchType::Type type,
-           const std::string& deletion_url);
-    virtual ~Result();
-
-    bool from_keyword_provider() const { return from_keyword_provider_; }
-
-    const base::string16& match_contents() const { return match_contents_; }
-    const ACMatchClassifications& match_contents_class() const {
-      return match_contents_class_;
-    }
-
-    AutocompleteMatchType::Type type() const { return type_; }
-    int relevance() const { return relevance_; }
-    void set_relevance(int relevance) { relevance_ = relevance; }
-
-    bool relevance_from_server() const { return relevance_from_server_; }
-    void set_relevance_from_server(bool relevance_from_server) {
-      relevance_from_server_ = relevance_from_server;
-    }
-
-    const std::string& deletion_url() const { return deletion_url_; }
-
-    // Returns if this result is inlineable against the current input |input|.
-    // Non-inlineable results are stale.
-    virtual bool IsInlineable(const base::string16& input) const = 0;
-
-    // Returns the default relevance value for this result (which may
-    // be left over from a previous omnibox input) given the current
-    // input and whether the current input caused a keyword provider
-    // to be active.
-    virtual int CalculateRelevance(const AutocompleteInput& input,
-                                   bool keyword_provider_requested) const = 0;
-
-   protected:
-    // The contents to be displayed and its style info.
-    base::string16 match_contents_;
-    ACMatchClassifications match_contents_class_;
-
-    // True if the result came from the keyword provider.
-    bool from_keyword_provider_;
-
-    AutocompleteMatchType::Type type_;
-
-    // The relevance score.
-    int relevance_;
-
-   private:
-    // Whether this result's relevance score was fully or partly calculated
-    // based on server information, and thus is assumed to be more accurate.
-    // This is ultimately used in
-    // SearchProvider::ConvertResultsToAutocompleteMatches(), see comments
-    // there.
-    bool relevance_from_server_;
-
-    // Optional deletion URL provided with suggestions. Fetching this URL
-    // should result in some reasonable deletion behaviour on the server,
-    // e.g. deleting this term out of a user's server-side search history.
-    std::string deletion_url_;
-  };
-
-  class SuggestResult : public Result {
-   public:
-    SuggestResult(const base::string16& suggestion,
-                  AutocompleteMatchType::Type type,
-                  const base::string16& match_contents,
-                  const base::string16& match_contents_prefix,
-                  const base::string16& annotation,
-                  const base::string16& answer_contents,
-                  const base::string16& answer_type,
-                  const std::string& suggest_query_params,
-                  const std::string& deletion_url,
-                  bool from_keyword_provider,
-                  int relevance,
-                  bool relevance_from_server,
-                  bool should_prefetch,
-                  const base::string16& input_text);
-    virtual ~SuggestResult();
-
-    const base::string16& suggestion() const { return suggestion_; }
-    const base::string16& match_contents_prefix() const {
-      return match_contents_prefix_;
-    }
-    const base::string16& annotation() const { return annotation_; }
-    const std::string& suggest_query_params() const {
-      return suggest_query_params_;
-    }
-
-    const base::string16& answer_contents() const { return answer_contents_; }
-    const base::string16& answer_type() const { return answer_type_; }
-
-    bool should_prefetch() const { return should_prefetch_; }
-
-    // Fills in |match_contents_class_| to reflect how |match_contents_| should
-    // be displayed and bolded against the current |input_text|.  If
-    // |allow_bolding_all| is false and |match_contents_class_| would have all
-    // of |match_contents_| bolded, do nothing.
-    void ClassifyMatchContents(const bool allow_bolding_all,
-                               const base::string16& input_text);
-
-    // Result:
-    virtual bool IsInlineable(const base::string16& input) const OVERRIDE;
-    virtual int CalculateRelevance(
-        const AutocompleteInput& input,
-        bool keyword_provider_requested) const OVERRIDE;
-
-   private:
-    // The search terms to be used for this suggestion.
-    base::string16 suggestion_;
-
-    // The contents to be displayed as prefix of match contents.
-    // Used for postfix suggestions to display a leading ellipsis (or some
-    // equivalent character) to indicate omitted text.
-    // Only used to pass this information to about:omnibox's "Additional Info".
-    base::string16 match_contents_prefix_;
-
-    // Optional annotation for the |match_contents_| for disambiguation.
-    // This may be displayed in the autocomplete match contents, but is defined
-    // separately to facilitate different formatting.
-    base::string16 annotation_;
-
-    // Optional additional parameters to be added to the search URL.
-    std::string suggest_query_params_;
-
-    // Optional formatted Answers result.
-    base::string16 answer_contents_;
-
-    // Type of optional formatted Answers result.
-    base::string16 answer_type_;
-
-    // Should this result be prefetched?
-    bool should_prefetch_;
-  };
-
-  class NavigationResult : public Result {
-   public:
-    // |provider| is necessary to use StringForURLDisplay() in order to
-    // compute |formatted_url_|.
-    NavigationResult(const AutocompleteProvider& provider,
-                     const GURL& url,
-                     AutocompleteMatchType::Type type,
-                     const base::string16& description,
-                     const std::string& deletion_url,
-                     bool from_keyword_provider,
-                     int relevance,
-                     bool relevance_from_server,
-                     const base::string16& input_text,
-                     const std::string& languages);
-    virtual ~NavigationResult();
-
-    const GURL& url() const { return url_; }
-    const base::string16& description() const { return description_; }
-    const base::string16& formatted_url() const { return formatted_url_; }
-
-    // Fills in |match_contents_| and |match_contents_class_| to reflect how
-    // the URL should be displayed and bolded against the current |input_text|
-    // and user |languages|.  If |allow_bolding_nothing| is false and
-    // |match_contents_class_| would result in an entirely unbolded
-    // |match_contents_|, do nothing.
-    void CalculateAndClassifyMatchContents(const bool allow_bolding_nothing,
-                                           const base::string16& input_text,
-                                           const std::string& languages);
-
-    // Result:
-    virtual bool IsInlineable(const base::string16& input) const OVERRIDE;
-    virtual int CalculateRelevance(
-        const AutocompleteInput& input,
-        bool keyword_provider_requested) const OVERRIDE;
-
-   private:
-    // The suggested url for navigation.
-    GURL url_;
-
-    // The properly formatted ("fixed up") URL string with equivalent meaning
-    // to the one in |url_|.
-    base::string16 formatted_url_;
-
-    // The suggested navigational result description; generally the site name.
-    base::string16 description_;
-  };
-
-  typedef std::vector<SuggestResult> SuggestResults;
-  typedef std::vector<NavigationResult> NavigationResults;
   typedef std::pair<base::string16, std::string> MatchKey;
   typedef std::map<MatchKey, AutocompleteMatch> MatchMap;
   typedef ScopedVector<SuggestionDeletionHandler> SuggestionDeletionHandlers;
 
-  // A simple structure bundling most of the information (including
-  // both SuggestResults and NavigationResults) returned by a call to
-  // the suggest server.
-  //
-  // This has to be declared after the typedefs since it relies on some of them.
-  struct Results {
-    Results();
-    ~Results();
-
-    // Clears |suggest_results| and |navigation_results| and resets
-    // |verbatim_relevance| to -1 (implies unset).
-    void Clear();
-
-    // Returns whether any of the results (including verbatim) have
-    // server-provided scores.
-    bool HasServerProvidedScores() const;
-
-    // Query suggestions sorted by relevance score.
-    SuggestResults suggest_results;
-
-    // Navigational suggestions sorted by relevance score.
-    NavigationResults navigation_results;
-
-    // The server supplied verbatim relevance scores. Negative values
-    // indicate that there is no suggested score; a value of 0
-    // suppresses the verbatim result.
-    int verbatim_relevance;
-
-    // The JSON metadata associated with this server response.
-    std::string metadata;
-
-   private:
-    DISALLOW_COPY_AND_ASSIGN(Results);
-  };
-
   // Returns an AutocompleteMatch with the given |autocomplete_provider|
   // for the search |suggestion|, which represents a search via |template_url|.
   // If |template_url| is NULL, returns a match with an invalid destination URL.
@@ -344,27 +114,21 @@ class BaseSearchProvider : public AutocompleteProvider,
   //
   // |input| is also necessary for various other details, like whether we should
   // allow inline autocompletion and what the transition type should be.
-  // |accepted_suggestion| and |omnibox_start_margin| are used to generate
-  // Assisted Query Stats.
+  // |in_keyword_mode| helps guarantee a non-keyword suggestion does not
+  // appear as the default match when the user is in keyword mode.
+  // |accepted_suggestion| is used to generate Assisted Query Stats.
   // |append_extra_query_params| should be set if |template_url| is the default
   // search engine, so the destination URL will contain any
   // command-line-specified query params.
-  // |from_app_list| should be set if the search was made from the app list.
   static AutocompleteMatch CreateSearchSuggestion(
       AutocompleteProvider* autocomplete_provider,
       const AutocompleteInput& input,
-      const SuggestResult& suggestion,
+      const bool in_keyword_mode,
+      const SearchSuggestionParser::SuggestResult& suggestion,
       const TemplateURL* template_url,
       const SearchTermsData& search_terms_data,
       int accepted_suggestion,
-      int omnibox_start_margin,
-      bool append_extra_query_params,
-      bool from_app_list);
-
-  // Parses JSON response received from the provider, stripping XSSI
-  // protection if needed. Returns the parsed data if successful, NULL
-  // otherwise.
-  static scoped_ptr<base::Value> DeserializeJsonData(std::string json_data);
+      bool append_extra_query_params);
 
   // Returns whether the requirements for requesting zero suggest results
   // are met. The requirements are
@@ -381,6 +145,7 @@ class BaseSearchProvider : public AutocompleteProvider,
      const GURL& suggest_url,
      const TemplateURL* template_url,
      metrics::OmniboxEventProto::PageClassification page_classification,
+     const SearchTermsData& search_terms_data,
      Profile* profile);
 
   // Returns whether we can send the URL of the current page in any suggest
@@ -404,11 +169,9 @@ class BaseSearchProvider : public AutocompleteProvider,
       const GURL& suggest_url,
       const TemplateURL* template_url,
       metrics::OmniboxEventProto::PageClassification page_classification,
+      const SearchTermsData& search_terms_data,
       Profile* profile);
 
-  // net::URLFetcherDelegate:
-  virtual void OnURLFetchComplete(const net::URLFetcher* source) OVERRIDE;
-
   // If the |deletion_url| is valid, then set |match.deletable| to true and
   // save the |deletion_url| into the |match|'s additional info under
   // the key |kDeletionUrlKey|.
@@ -421,34 +184,26 @@ class BaseSearchProvider : public AutocompleteProvider,
   // |metadata| and |accepted_suggestion| are used for generating an
   // AutocompleteMatch.
   // |mark_as_deletable| indicates whether the match should be marked deletable.
+  // |in_keyword_mode| helps guarantee a non-keyword suggestion does not
+  // appear as the default match when the user is in keyword mode.
   // NOTE: Any result containing a deletion URL is always marked deletable.
-  void AddMatchToMap(const SuggestResult& result,
+  void AddMatchToMap(const SearchSuggestionParser::SuggestResult& result,
                      const std::string& metadata,
                      int accepted_suggestion,
                      bool mark_as_deletable,
+                     bool in_keyword_mode,
                      MatchMap* map);
 
   // Parses results from the suggest server and updates the appropriate suggest
-  // and navigation result lists in |results|. |is_keyword_result| indicates
-  // whether the response was received from the keyword provider.
+  // and navigation result lists in |results|. |default_result_relevance| is
+  // the relevance to use if it was not explicitly set by the server.
+  // |is_keyword_result| indicates whether the response was received from the
+  // keyword provider.
   // Returns whether the appropriate result list members were updated.
   bool ParseSuggestResults(const base::Value& root_val,
+                           int default_result_relevance,
                            bool is_keyword_result,
-                           Results* results);
-
-  // Prefetches any images in Answers results.
-  void PrefetchAnswersImages(const base::DictionaryValue* answers_json);
-
-  // Called at the end of ParseSuggestResults to rank the |results|.
-  virtual void SortResults(bool is_keyword,
-                           const base::ListValue* relevances,
-                           Results* results);
-
-  // 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.
-  virtual bool StoreSuggestionResponse(const std::string& json_data,
-                                       const base::Value& parsed_data);
+                           SearchSuggestionParser::Results* results);
 
   // Returns the TemplateURL corresponding to the keyword or default
   // provider based on the value of |is_keyword|.
@@ -458,12 +213,10 @@ class BaseSearchProvider : public AutocompleteProvider,
   // based on the value of |is_keyword|.
   virtual const AutocompleteInput GetInput(bool is_keyword) const = 0;
 
-  // Returns a pointer to a Results object, which will hold suggest results.
-  virtual Results* GetResultsToFill(bool is_keyword) = 0;
-
   // Returns whether the destination URL corresponding to the given |result|
   // should contain command-line-specified query params.
-  virtual bool ShouldAppendExtraParams(const SuggestResult& result) const = 0;
+  virtual bool ShouldAppendExtraParams(
+      const SearchSuggestionParser::SuggestResult& result) const = 0;
 
   // Stops the suggest query.
   // NOTE: This does not update |done_|.  Callers must do so.
@@ -472,25 +225,15 @@ class BaseSearchProvider : public AutocompleteProvider,
   // Clears the current results.
   virtual void ClearAllResults() = 0;
 
-  // Returns the relevance to use if it was not explicitly set by the server.
-  virtual int GetDefaultResultRelevance() const = 0;
-
   // Records in UMA whether the deletion request resulted in success.
   virtual void RecordDeletionResult(bool success) = 0;
 
-  // Records UMA statistics about a suggest server response.
-  virtual void LogFetchComplete(bool succeeded, bool is_keyword) = 0;
-
   // Modify provider-specific UMA statistics.
   virtual void ModifyProviderInfo(
       metrics::OmniboxEventProto_ProviderInfo* provider_info) const;
 
-  // Returns whether the |fetcher| is for the keyword provider.
-  virtual bool IsKeywordFetcher(const net::URLFetcher* fetcher) const = 0;
-
-  // Updates |matches_| from the latest results; applies calculated relevances
-  // if suggested relevances cause undesriable behavior. Updates |done_|.
-  virtual void UpdateMatches() = 0;
+  TemplateURLService* template_url_service_;
+  Profile* profile_;
 
   // Whether a field trial, if any, has triggered in the most recent
   // autocomplete query. This field is set to true only if the suggestion
@@ -502,10 +245,6 @@ class BaseSearchProvider : public AutocompleteProvider,
   // session.
   bool field_trial_triggered_in_session_;
 
-  // 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_;
-
  private:
   friend class SearchProviderTest;
   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch);
@@ -524,11 +263,6 @@ class BaseSearchProvider : public AutocompleteProvider,
   // causes us to auto-cancel all such requests on shutdown.
   SuggestionDeletionHandlers deletion_handlers_;
 
-  // True if this provider's results are being displayed in the app list. By
-  // default this is false, meaning that the results will be shown in the
-  // omnibox.
-  bool in_app_list_;
-
   DISALLOW_COPY_AND_ASSIGN(BaseSearchProvider);
 };