Update To 11.40.268.0
[platform/framework/web/crosswalk.git] / src / components / omnibox / search_provider.h
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved.
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 //
5 // This file contains the Search autocomplete provider.  This provider is
6 // responsible for all autocomplete entries that start with "Search <engine>
7 // for ...", including searching for the current input string, search
8 // history, and search suggestions.  An instance of it gets created and
9 // managed by the autocomplete controller.
10
11 #ifndef COMPONENTS_OMNIBOX_SEARCH_PROVIDER_H_
12 #define COMPONENTS_OMNIBOX_SEARCH_PROVIDER_H_
13
14 #include <string>
15 #include <vector>
16
17 #include "base/basictypes.h"
18 #include "base/compiler_specific.h"
19 #include "base/memory/scoped_ptr.h"
20 #include "base/time/time.h"
21 #include "base/timer/timer.h"
22 #include "components/metrics/proto/omnibox_input_type.pb.h"
23 #include "components/omnibox/answers_cache.h"
24 #include "components/omnibox/base_search_provider.h"
25 #include "components/search_engines/template_url.h"
26 #include "net/url_request/url_fetcher_delegate.h"
27
28 class AutocompleteProviderClient;
29 class AutocompleteProviderListener;
30 class AutocompleteResult;
31 class SearchProviderTest;
32 class TemplateURLService;
33
34 namespace history {
35 struct KeywordSearchTermVisit;
36 }
37
38 namespace net {
39 class URLFetcher;
40 }
41
42 // Autocomplete provider for searches and suggestions from a search engine.
43 //
44 // After construction, the autocomplete controller repeatedly calls Start()
45 // with some user input, each time expecting to receive a small set of the best
46 // matches (either synchronously or asynchronously).
47 //
48 // Initially the provider creates a match that searches for the current input
49 // text.  It also starts a task to query the Suggest servers.  When that data
50 // comes back, the provider creates and returns matches for the best
51 // suggestions.
52 class SearchProvider : public BaseSearchProvider,
53                        public net::URLFetcherDelegate {
54  public:
55   SearchProvider(AutocompleteProviderListener* listener,
56                  TemplateURLService* template_url_service,
57                  scoped_ptr<AutocompleteProviderClient> client);
58
59   // Extracts the suggest response metadata which SearchProvider previously
60   // stored for |match|.
61   static std::string GetSuggestMetadata(const AutocompleteMatch& match);
62
63   // Answers prefetch handling - register displayed answers. Takes the top
64   // match for Autocomplete and registers the contained answer data, if any.
65   void RegisterDisplayedAnswers(const AutocompleteResult& result);
66
67   // AutocompleteProvider:
68   void ResetSession() override;
69
70   // This URL may be sent with suggest requests; see comments on CanSendURL().
71   void set_current_page_url(const GURL& current_page_url) {
72     current_page_url_ = current_page_url;
73   }
74
75  protected:
76   ~SearchProvider() override;
77
78  private:
79   friend class SearchProviderTest;
80   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, CanSendURL);
81   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest,
82                            DontInlineAutocompleteAsynchronously);
83   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInline);
84   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineDomainClassify);
85   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, NavigationInlineSchemeSubstring);
86   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestRelevanceExperiment);
87   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, TestDeleteMatch);
88   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SuggestQueryUsesToken);
89   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, SessionToken);
90   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, AnswersCache);
91   FRIEND_TEST_ALL_PREFIXES(SearchProviderTest, RemoveExtraAnswers);
92   FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, GetDestinationURL);
93   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, ClearPrefetchedResults);
94   FRIEND_TEST_ALL_PREFIXES(InstantExtendedPrefetchTest, SetPrefetchQuery);
95
96   // Manages the providers (TemplateURLs) used by SearchProvider. Two providers
97   // may be used:
98   // . The default provider. This corresponds to the user's default search
99   //   engine. This is always used, except for the rare case of no default
100   //   engine.
101   // . The keyword provider. This is used if the user has typed in a keyword.
102   class Providers {
103    public:
104     explicit Providers(TemplateURLService* template_url_service);
105
106     // Returns true if the specified providers match the two providers cached
107     // by this class.
108     bool equal(const base::string16& default_provider,
109                const base::string16& keyword_provider) const {
110       return (default_provider == default_provider_) &&
111           (keyword_provider == keyword_provider_);
112     }
113
114     // Resets the cached providers.
115     void set(const base::string16& default_provider,
116              const base::string16& keyword_provider) {
117       default_provider_ = default_provider;
118       keyword_provider_ = keyword_provider;
119     }
120
121     TemplateURLService* template_url_service() { return template_url_service_; }
122     const base::string16& default_provider() const { return default_provider_; }
123     const base::string16& keyword_provider() const { return keyword_provider_; }
124
125     // NOTE: These may return NULL even if the provider members are nonempty!
126     const TemplateURL* GetDefaultProviderURL() const;
127     const TemplateURL* GetKeywordProviderURL() const;
128
129     // Returns true if there is a valid keyword provider.
130     bool has_keyword_provider() const { return !keyword_provider_.empty(); }
131
132    private:
133     TemplateURLService* template_url_service_;
134
135     // Cached across the life of a query so we behave consistently even if the
136     // user changes their default while the query is running.
137     base::string16 default_provider_;
138     base::string16 keyword_provider_;
139
140     DISALLOW_COPY_AND_ASSIGN(Providers);
141   };
142
143   class CompareScoredResults;
144
145   typedef std::vector<history::KeywordSearchTermVisit> HistoryResults;
146
147   // Calculates the relevance score for the keyword verbatim result (if the
148   // input matches one of the profile's keyword).
149   static int CalculateRelevanceForKeywordVerbatim(
150       metrics::OmniboxInputType::Type type,
151       bool prefer_keyword);
152
153   // A helper function for UpdateAllOldResults().
154   static void UpdateOldResults(bool minimal_changes,
155                                SearchSuggestionParser::Results* results);
156
157   // Returns the first match in |matches| which might be chosen as default.
158   static ACMatches::iterator FindTopMatch(ACMatches* matches);
159
160   // AutocompleteProvider:
161   void Start(const AutocompleteInput& input, bool minimal_changes) override;
162   void Stop(bool clear_cached_results) override;
163
164   // BaseSearchProvider:
165   const TemplateURL* GetTemplateURL(bool is_keyword) const override;
166   const AutocompleteInput GetInput(bool is_keyword) const override;
167   bool ShouldAppendExtraParams(
168       const SearchSuggestionParser::SuggestResult& result) const override;
169   void RecordDeletionResult(bool success) override;
170
171   // net::URLFetcherDelegate:
172   void OnURLFetchComplete(const net::URLFetcher* source) override;
173
174   // Stops the suggest query.
175   // NOTE: This does not update |done_|.  Callers must do so.
176   void StopSuggest();
177
178   // Clears the current results.
179   void ClearAllResults();
180
181   // Recalculates the match contents class of |results| to better display
182   // against the current input and user's language.
183   void UpdateMatchContentsClass(const base::string16& input_text,
184                                 SearchSuggestionParser::Results* results);
185
186   // Called after ParseSuggestResults to rank the |results|.
187   void SortResults(bool is_keyword, SearchSuggestionParser::Results* results);
188
189   // Records UMA statistics about a suggest server response.
190   void LogFetchComplete(bool success, bool is_keyword);
191
192   // Updates |matches_| from the latest results; applies calculated relevances
193   // if suggested relevances cause undesirable behavior. Updates |done_|.
194   void UpdateMatches();
195
196   // Called when timer_ expires.
197   void Run();
198
199   // Runs the history query, if necessary. The history query is synchronous.
200   // This does not update |done_|.
201   void DoHistoryQuery(bool minimal_changes);
202
203   // Returns the time to delay before sending the Suggest request.
204   base::TimeDelta GetSuggestQueryDelay() const;
205
206   // Determines whether an asynchronous subcomponent query should run for the
207   // current input.  If so, starts it if necessary; otherwise stops it.
208   // NOTE: This function does not update |done_|.  Callers must do so.
209   void StartOrStopSuggestQuery(bool minimal_changes);
210
211   // Returns true when the current query can be sent to the Suggest service.
212   // This will be false e.g. when Suggest is disabled, the query contains
213   // potentially private data, etc.
214   bool IsQuerySuitableForSuggest() const;
215
216   // Remove existing keyword results if the user is no longer in keyword mode,
217   // and, if |minimal_changes| is false, revise the existing results to
218   // indicate they were received before the last keystroke.
219   void UpdateAllOldResults(bool minimal_changes);
220
221   // Given new asynchronous results, ensure that we don't clobber the current
222   // top results, which were determined synchronously on the last keystroke.
223   void PersistTopSuggestions(SearchSuggestionParser::Results* results);
224
225   // Apply calculated relevance scores to the current results.
226   void ApplyCalculatedSuggestRelevance(
227       SearchSuggestionParser::SuggestResults* list);
228   void ApplyCalculatedNavigationRelevance(
229       SearchSuggestionParser::NavigationResults* list);
230
231   // Starts a new URLFetcher requesting suggest results from |template_url|;
232   // callers own the returned URLFetcher, which is NULL for invalid providers.
233   net::URLFetcher* CreateSuggestFetcher(int id,
234                                         const TemplateURL* template_url,
235                                         const AutocompleteInput& input);
236
237   // Converts the parsed results to a set of AutocompleteMatches, |matches_|.
238   void ConvertResultsToAutocompleteMatches();
239
240   // Remove answer contents from each match in |matches| other than the first
241   // that appears.
242   static void RemoveExtraAnswers(ACMatches* matches);
243
244   // Returns an iterator to the first match in |matches_| which might
245   // be chosen as default.
246   ACMatches::const_iterator FindTopMatch() const;
247
248   // Checks if suggested relevances violate an expected constraint.
249   // See UpdateMatches() for the use and explanation of this constraint
250   // and other constraints enforced without the use of helper functions.
251   bool IsTopMatchSearchWithURLInput() const;
252
253   // Converts an appropriate number of navigation results in
254   // |navigation_results| to matches and adds them to |matches|.
255   void AddNavigationResultsToMatches(
256       const SearchSuggestionParser::NavigationResults& navigation_results,
257       ACMatches* matches);
258
259   // Adds a match for each result in |raw_default_history_results_| or
260   // |raw_keyword_history_results_| to |map|. |is_keyword| indicates
261   // which one of the two.
262   void AddRawHistoryResultsToMap(bool is_keyword,
263                                  int did_not_accept_suggestion,
264                                  MatchMap* map);
265
266   // Adds a match for each transformed result in |results| to |map|.
267   void AddTransformedHistoryResultsToMap(
268       const SearchSuggestionParser::SuggestResults& results,
269       int did_not_accept_suggestion,
270       MatchMap* map);
271
272   // Calculates relevance scores for all |results|.
273   SearchSuggestionParser::SuggestResults ScoreHistoryResultsHelper(
274       const HistoryResults& results,
275       bool base_prevent_inline_autocomplete,
276       bool input_multiple_words,
277       const base::string16& input_text,
278       bool is_keyword);
279
280   // Calculates relevance scores for |results|, adjusting for boundary
281   // conditions around multi-word queries. (See inline comments in function
282   // definition for more details.)
283   void ScoreHistoryResults(
284       const HistoryResults& results,
285       bool is_keyword,
286       SearchSuggestionParser::SuggestResults* scored_results);
287
288   // Adds matches for |results| to |map|.
289   void AddSuggestResultsToMap(
290       const SearchSuggestionParser::SuggestResults& results,
291       const std::string& metadata,
292       MatchMap* map);
293
294   // Gets the relevance score for the verbatim result.  This value may be
295   // provided by the suggest server or calculated locally; if
296   // |relevance_from_server| is non-NULL, it will be set to indicate which of
297   // those is true.
298   int GetVerbatimRelevance(bool* relevance_from_server) const;
299
300   // Calculates the relevance score for the verbatim result from the
301   // default search engine.  This version takes into account context:
302   // i.e., whether the user has entered a keyword-based search or not.
303   int CalculateRelevanceForVerbatim() const;
304
305   // Calculates the relevance score for the verbatim result from the default
306   // search engine *ignoring* whether the input is a keyword-based search
307   // or not.  This function should only be used to determine the minimum
308   // relevance score that the best result from this provider should have.
309   // For normal use, prefer the above function.
310   int CalculateRelevanceForVerbatimIgnoringKeywordModeState() const;
311
312   // Gets the relevance score for the keyword verbatim result.
313   // |relevance_from_server| is handled as in GetVerbatimRelevance().
314   // TODO(mpearson): Refactor so this duplication isn't necessary or
315   // restructure so one static function takes all the parameters it needs
316   // (rather than looking at internal state).
317   int GetKeywordVerbatimRelevance(bool* relevance_from_server) const;
318
319   // |time| is the time at which this query was last seen.  |is_keyword|
320   // indicates whether the results correspond to the keyword provider or default
321   // provider. |use_aggressive_method| says whether this function can use a
322   // method that gives high scores (1200+) rather than one that gives lower
323   // scores.  When using the aggressive method, scores may exceed 1300
324   // unless |prevent_search_history_inlining| is set.
325   int CalculateRelevanceForHistory(const base::Time& time,
326                                    bool is_keyword,
327                                    bool use_aggressive_method,
328                                    bool prevent_search_history_inlining) const;
329
330   // Returns an AutocompleteMatch for a navigational suggestion.
331   AutocompleteMatch NavigationToMatch(
332       const SearchSuggestionParser::NavigationResult& navigation);
333
334   // Updates the value of |done_| from the internal state.
335   void UpdateDone();
336
337   // Obtains a session token, regenerating if necessary.
338   std::string GetSessionToken();
339
340   // Answers prefetch handling - finds the previously displayed answer matching
341   // the current top-scoring history result. If there is a previous answer,
342   // returns the query data associated with it. Otherwise, returns an empty
343   // AnswersQueryData.
344   AnswersQueryData FindAnswersPrefetchData();
345
346   AutocompleteProviderListener* listener_;
347
348   // The number of suggest results that haven't yet arrived. If it's greater
349   // than 0, it indicates that one of the URLFetchers is still running.
350   int suggest_results_pending_;
351
352   // Maintains the TemplateURLs used.
353   Providers providers_;
354
355   // The user's input.
356   AutocompleteInput input_;
357
358   // Input when searching against the keyword provider.
359   AutocompleteInput keyword_input_;
360
361   // Searches in the user's history that begin with the input text.
362   HistoryResults raw_keyword_history_results_;
363   HistoryResults raw_default_history_results_;
364
365   // Scored searches in the user's history - based on |keyword_history_results_|
366   // or |default_history_results_| as appropriate.
367   SearchSuggestionParser::SuggestResults transformed_keyword_history_results_;
368   SearchSuggestionParser::SuggestResults transformed_default_history_results_;
369
370   // A timer to start a query to the suggest server after the user has stopped
371   // typing for long enough.
372   base::OneShotTimer<SearchProvider> timer_;
373
374   // The time at which we sent a query to the suggest server.
375   base::TimeTicks time_suggest_request_sent_;
376
377   // Fetchers used to retrieve results for the keyword and default providers.
378   scoped_ptr<net::URLFetcher> keyword_fetcher_;
379   scoped_ptr<net::URLFetcher> default_fetcher_;
380
381   // Results from the default and keyword search providers.
382   SearchSuggestionParser::Results default_results_;
383   SearchSuggestionParser::Results keyword_results_;
384
385   // The top query suggestion, left blank if none.
386   base::string16 top_query_suggestion_match_contents_;
387   // The top navigation suggestion, left blank/invalid if none.
388   GURL top_navigation_suggestion_;
389
390   GURL current_page_url_;
391
392   // Session token management.
393   std::string current_token_;
394   base::TimeTicks token_expiration_time_;
395
396   // Answers prefetch management.
397   AnswersCache answers_cache_;  // Cache for last answers seen.
398   AnswersQueryData prefetch_data_;  // Data to use for query prefetching.
399
400   DISALLOW_COPY_AND_ASSIGN(SearchProvider);
401 };
402
403 #endif  // COMPONENTS_OMNIBOX_SEARCH_PROVIDER_H_