history_url_provider.h revision 5d1f7b1de12d16ceb2c938c56701a3e8bfa558f7
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)#ifndef CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_
65821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_
75821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
85821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <string>
95821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include <vector>
105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/compiler_specific.h"
125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "base/synchronization/cancellation_flag.h"
135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/autocomplete_input.h"
145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/history_provider.h"
155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/history_provider_util.h"
165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#include "chrome/browser/autocomplete/url_prefix.h"
175d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)#include "chrome/browser/omnibox/omnibox_field_trial.h"
182a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/search_terms_data.h"
192a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)#include "chrome/browser/search_engines/template_url.h"
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
23c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)namespace base {
24c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)class MessageLoop;
25c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)}
26c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)
275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)namespace history {
285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryBackend;
295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class URLDatabase;
305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)}
315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// How history autocomplete works
335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// ==============================
345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Read down this diagram for temporal ordering.
365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   Main thread                History thread
385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   -----------                --------------
395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   AutocompleteController::Start
405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     -> HistoryURLProvider::Start
415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//       -> RunAutocompletePasses
425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         -> SuggestExactInput
435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         [params_ allocated]
445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         -> DoAutocomplete (for inline autocomplete)
455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//           -> URLDatabase::AutocompleteForPrefix (on in-memory DB)
465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         -> HistoryService::ScheduleAutocomplete
475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//         (return to controller) ----
485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                   /
495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                              HistoryBackend::ScheduleAutocomplete
505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                -> HistoryURLProvider::ExecuteWithDB
515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                  -> DoAutocomplete
525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                    -> URLDatabase::AutocompleteForPrefix
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//                                /
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   HistoryService::QueryComplete
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     [params_ destroyed]
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     -> AutocompleteProviderListener::OnProviderUpdate
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The autocomplete controller calls us, and must be called back, on the main
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// thread.  When called, we run two autocomplete passes.  The first pass runs
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronously on the main thread and queries the in-memory URL database.
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This pass promotes matches for inline autocomplete if applicable.  We do
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this synchronously so that users get consistent behavior when they type
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// quickly and hit enter, no matter how loaded the main history database is.
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Doing this synchronously also prevents inline autocomplete from being
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "flickery" in the AutocompleteEdit.  Because the in-memory DB does not have
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// redirect data, results other than the top match might change between the
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// two passes, so we can't just decide to use this pass' matches as the final
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// results.
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The second autocomplete pass uses the full history database, which must be
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// queried on the history thread.  Start() asks the history service schedule to
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callback on the history thread with a pointer to the main database.  When we
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are done doing queries, we schedule a task on the main thread that notifies
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the AutocompleteController that we're done.
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The communication between these threads is done using a
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HistoryURLProviderParams object.  This is allocated in the main thread, and
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// normally deleted in QueryComplete().  So that both autocomplete passes can
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use the same code, we also use this to hold results during the first
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// autocomplete pass.
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// While the second pass is running, the AutocompleteController may cancel the
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// request.  This can happen frequently when the user is typing quickly.  In
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this case, the main thread sets params_->cancel, which the background thread
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// checks periodically.  If it finds the flag set, it stops what it's doing
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// immediately and calls back to the main thread.  (We don't delete the params
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on the history thread, because we should only do that when we can safely
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NULL out params_, and that must be done on the main thread.)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to communicate autocomplete parameters between threads via the history
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// service.
925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct HistoryURLProviderParams {
935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProviderParams(const AutocompleteInput& input,
945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool trim_http,
952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const std::string& languages,
962a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           TemplateURL* default_search_provider,
972a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const SearchTermsData& search_terms_data);
985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~HistoryURLProviderParams();
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
100c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop* message_loop;
1015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A copy of the autocomplete input. We need the copy since this object will
1035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // live beyond the original query while it runs on the history thread.
1045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteInput input;
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should inline autocompletion be disabled? This is initalized from
1075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |input.prevent_inline_autocomplete()|, but set to false is the input
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // contains trailing white space.
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool prevent_inline_autocomplete;
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set when "http://" should be trimmed from the beginning of the URLs.
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool trim_http;
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set by the main thread to cancel this request.  If this flag is set when
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the query runs, the query will be abandoned.  This allows us to avoid
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // running queries that are no longer needed.  Since we don't care if we run
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the extra queries, the lack of signaling is not a problem.
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::CancellationFlag cancel_flag;
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set by ExecuteWithDB() on the history thread when the query could not be
1215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // performed because the history system failed to properly init the database.
1225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this is set when the main thread is called back, it avoids changing
1235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |matches_| at all, so it won't delete the default match
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // RunAutocompletePasses() creates.
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool failed;
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // List of matches written by the history thread.  We keep this separate list
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to avoid having the main thread read the provider's matches while the
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // history thread is manipulating them.  The provider copies this list back
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // to matches_ on the main thread in QueryComplete().
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ACMatches matches;
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Languages we should pass to gfx::GetCleanStringFromUrl.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string languages;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // When true, we should avoid calling SuggestExactInput().
1375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool dont_suggest_exact_input;
1385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1392a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The default search provider and search terms data necessary to cull results
1402a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that correspond to searches (on the default engine).  These can only be
1412a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // obtained on the UI thread, so we have to copy them into here to pass them
1422a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to the history thread.  We use a scoped_ptr<TemplateURL> for the DSP since
1432a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TemplateURLs can't be copied by value. We use a scoped_ptr<SearchTermsData>
1442a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // so that we can store a snapshot of the SearchTermsData accessible from the
1452a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // history thread.
1462a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TemplateURL> default_search_provider;
1472a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SearchTermsData> search_terms_data;
1482a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HistoryURLProviderParams);
1515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is an autocomplete provider and is also a pseudo-internal
1545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// component of the history system.  See comments above.
1555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryURLProvider : public HistoryProvider {
1565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1572a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Various values used in scoring, made public so other providers
1582a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // can insert results in appropriate ranges relative to these.
1592a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForBestInlineableResult;
1602a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForUnvisitedIntranetResult;
1612a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForWhatYouTypedResult;
1622a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kBaseScoreForNonInlineableResult;
1632a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProvider(AutocompleteProviderListener* listener, Profile* profile);
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1660f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // HistoryProvider:
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Start(const AutocompleteInput& input,
1685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     bool minimal_changes) OVERRIDE;
1695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Stop(bool clear_cached_results) OVERRIDE;
1705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1710f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Returns a match representing a navigation to |destination_url| given user
1720f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // input of |text|.  |trim_http| controls whether the match's |fill_into_edit|
1730f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // and |contents| should have any HTTP scheme stripped off, and should not be
1740f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // set to true if |text| contains an http prefix.
1750f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // NOTE: This does not set the relevance of the returned match, as different
1760f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  //       callers want different behavior. Callers must set this manually.
177a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  AutocompleteMatch SuggestExactInput(const base::string16& text,
1780f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                      const GURL& destination_url,
1790f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                      bool trim_http);
1800f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the history query on the history thread, called by the history
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // system. The history database MAY BE NULL in which case it is not
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // available and we should return no data. Also schedules returning the
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // results to the main thread
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void ExecuteWithDB(history::HistoryBackend* backend,
1865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     history::URLDatabase* db,
1875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     HistoryURLProviderParams* params);
1885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Actually runs the autocomplete job on the given database, which is
1905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // guaranteed not to be NULL.
1915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void DoAutocomplete(history::HistoryBackend* backend,
1925821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      history::URLDatabase* db,
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                      HistoryURLProviderParams* params);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Dispatches the results to the autocomplete controller. Called on the
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // main thread by ExecuteWithDB when the results are available.
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Frees params_gets_deleted on exit.
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void QueryComplete(HistoryURLProviderParams* params_gets_deleted);
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2015d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistoryURLProviderTest, HUPScoringExperiment);
2025d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum MatchType {
2045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NORMAL,
2055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WHAT_YOU_TYPED,
2065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINE_AUTOCOMPLETE,
2075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UNVISITED_INTRANET,  // An intranet site that has never been visited.
2085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class VisitClassifier;
2105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~HistoryURLProvider();
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines the relevance for a match, given its type.  If
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |match_type| is NORMAL, |match_number| is a number [0,
2155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // kMaxSuggestions) indicating the relevance of the match (higher ==
2165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // more relevant).  For other values of |match_type|, |match_number|
2175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is ignored.  Only called some of the time; for some matches,
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // relevancy scores are assigned consecutively decreasing (1416,
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // 1415, 1414, ...).
2205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  int CalculateRelevance(MatchType match_type, size_t match_number) const;
2215821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function that actually launches the two autocomplete passes.
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void RunAutocompletePasses(const AutocompleteInput& input,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                             bool fixup_input_and_run_pass_1);
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Given a |match| containing the "what you typed" suggestion created by
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // SuggestExactInput(), looks up its info in the DB.  If found, fills in the
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // title from the DB, promotes the match's priority to that of an inline
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete match (maybe it should be slightly better?), and places it on
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the front of |matches| (so we pick the right matches to throw away
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // when culling redirects to/from it).  Returns whether a match was promoted.
2325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool FixupExactSuggestion(history::URLDatabase* db,
2335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const AutocompleteInput& input,
2345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const VisitClassifier& classifier,
2355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            AutocompleteMatch* match,
2365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            history::HistoryMatches* matches) const;
2375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for FixupExactSuggestion, this returns true if the input
2395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // corresponds to some intranet URL where the user has previously visited the
2405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // host in question.  In this case the input should be treated as a URL.
2415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CanFindIntranetURL(history::URLDatabase* db,
2425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const AutocompleteInput& input) const;
2435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Determines if |match| is suitable for inline autocomplete.  If so, and if
2455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |params| is non-NULL, promotes the match.  Returns whether |match| is
2465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // suitable for inline autocomplete.
2478bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  bool PromoteMatchForInlineAutocomplete(const history::HistoryMatch& match,
2488bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                                         HistoryURLProviderParams* params);
2495821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2505821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sees if a shorter version of the best match should be created, and if so
2515821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // places it at the front of |matches|.  This can suggest history URLs that
2525821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // are prefixes of the best match (if they've been visited enough, compared to
2535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the best match), or create host-only suggestions even when they haven't
2545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // been visited before: if the user visited http://example.com/asdf once,
2555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // we'll suggest http://example.com/ even if they've never been to it.
2565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void PromoteOrCreateShorterSuggestion(
2575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::URLDatabase* db,
2585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const HistoryURLProviderParams& params,
2595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool have_what_you_typed_match,
2605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const AutocompleteMatch& what_you_typed_match,
2615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::HistoryMatches* matches);
2625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes results that have been rarely typed or visited, and not any time
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recently.  The exact parameters for this heuristic can be found in the
2652a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // function body. Also culls results corresponding to queries from the default
2662a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // search engine. These are low-quality, difficult-to-understand matches for
2672a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // users, and the SearchProvider should surface past queries in a better way
2682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // anyway.
2698bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)  void CullPoorMatches(const HistoryURLProviderParams& params,
2708bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)                       history::HistoryMatches* matches) const;
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes results that redirect to each other, leaving at most |max_results|
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // results.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CullRedirects(history::HistoryBackend* backend,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     history::HistoryMatches* matches,
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     size_t max_results) const;
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for CullRedirects, this removes all but the first
2795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // occurance of [any of the set of strings in |remove|] from the |matches|
2805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // list.
2815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
2825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The return value is the index of the item that is after the item in the
2835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input identified by |source_index|. If |source_index| or an item before
2845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is removed, the next item will be shifted, and this allows the caller to
2855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pick up on the next one when this happens.
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   size_t source_index,
2885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::vector<GURL>& remove) const;
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Converts a line from the database into an autocomplete match for display.
2915d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // If experimental scoring is enabled, the final relevance score might be
2925d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // different from the given |relevance|.
2935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteMatch HistoryMatchToACMatch(
2948bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const HistoryURLProviderParams& params,
2955821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      const history::HistoryMatch& history_match,
2965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MatchType match_type,
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int relevance);
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
299f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // Returns a set of classifications that highlight all the occurrences
300f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  // of |input_text| at word breaks in |description|.
301f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)  static ACMatchClassifications ClassifyDescription(
302a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& input_text,
303a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)      const base::string16& description);
304f2477e01787aa58f445919b809d89e252beef54fTorne (Richard Coles)
3055d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns a new relevance score for the given |match| based on the
3065d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // |old_relevance| score and |scoring_params_|.  The new relevance score is
3075d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // guaranteed to be less than or equal to |old_relevance|.  In other words,
3085d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // this function can only demote a score, never boost it.
3095d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Returns |old_relevance| score if experimental scoring is disabled
3105d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // or if the |match.promoted| is true.
3115d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  int CalculateRelevanceScoreUsingScoringParams(
3125d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      const history::HistoryMatch& match,
3135d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)      int old_relevance) const;
3145d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Params for the current query.  The provider should not free this directly;
3165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instead, it is passed as a parameter through the history backend, and the
3175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter itself is freed once it's no longer needed.  The only reason we
3185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keep this member is so we can set the cancel bit on it.
3195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProviderParams* params_;
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Params controlling experimental behavior of this provider.
3225d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  HUPScoringParams scoring_params_;
3235d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If true, HistoryURL provider should lookup and cull redirects.  If
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // false, it returns matches that may be redirects to each other and
3265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // simply hopes the default AutoCompleteController behavior to remove
3275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URLs that are likely duplicates (http://google.com <->
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // https://www.google.com/, etc.) will do a good enough job.
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool cull_redirects_;
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Used in PromoteOrCreateShorterSuggestion().  If true, we may create
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // shorter suggestions even when they haven't been visited before:
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // if the user visited http://example.com/asdf once, we'll suggest
3345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // http://example.com/ even if they've never been to it.
3355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool create_shorter_match_;
3365821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3375821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Whether to query the history URL database to match.  Even if
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // false, we still use the URL database to decide if the
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL-what-you-typed was visited before or not.  If false, the only
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // possible result that HistoryURL provider can return is
3415821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // URL-what-you-typed.  This variable is not part of params_ because
3425821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // it never changes after the HistoryURLProvider is initialized.
3435821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // It's used to aid the transition to get all URLs from history to
3445821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // be scored in the HistoryQuick provider only.
3455821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool search_url_database_;
3465821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3475821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3485821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_
349