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/history_provider.h"
14116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/history/core/browser/history_match.h"
155f1c94371a64b3196d4be9466099bb892df9b88eTorne (Richard Coles)#include "components/omnibox/autocomplete_input.h"
166e8cce623b6e4fe0c9e4af605d675dd9d0338c38Torne (Richard Coles)#include "components/omnibox/omnibox_field_trial.h"
17116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch#include "components/search_engines/template_url.h"
185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
19116680a4aac90f2aa7413d9095a592090648e557Ben Murdochclass AutocompleteProviderListener;
205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class Profile;
21f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)class SearchTermsData;
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
41f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//       -> SuggestExactInput
42f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//       [params_ allocated]
43f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//       -> DoAutocomplete (for inline autocomplete)
44f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//         -> URLDatabase::AutocompleteForPrefix (on in-memory DB)
45f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//       -> HistoryService::ScheduleAutocomplete
46f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//       (return to controller) ----
47f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                                 /
48f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                            HistoryBackend::ScheduleAutocomplete
49f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                              -> HistoryURLProvider::ExecuteWithDB
50f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                                -> DoAutocomplete
51f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                                  -> URLDatabase::AutocompleteForPrefix
52f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)//                              /
535821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//   HistoryService::QueryComplete
545821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     [params_ destroyed]
555821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//     -> AutocompleteProviderListener::OnProviderUpdate
565821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
575821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The autocomplete controller calls us, and must be called back, on the main
585821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// thread.  When called, we run two autocomplete passes.  The first pass runs
595821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// synchronously on the main thread and queries the in-memory URL database.
605821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This pass promotes matches for inline autocomplete if applicable.  We do
615821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this synchronously so that users get consistent behavior when they type
625821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// quickly and hit enter, no matter how loaded the main history database is.
635821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Doing this synchronously also prevents inline autocomplete from being
645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// "flickery" in the AutocompleteEdit.  Because the in-memory DB does not have
655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// redirect data, results other than the top match might change between the
665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// two passes, so we can't just decide to use this pass' matches as the final
675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// results.
685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
695821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The second autocomplete pass uses the full history database, which must be
705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// queried on the history thread.  Start() asks the history service schedule to
715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// callback on the history thread with a pointer to the main database.  When we
725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// are done doing queries, we schedule a task on the main thread that notifies
735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// the AutocompleteController that we're done.
745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// The communication between these threads is done using a
765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// HistoryURLProviderParams object.  This is allocated in the main thread, and
775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// normally deleted in QueryComplete().  So that both autocomplete passes can
785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// use the same code, we also use this to hold results during the first
795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// autocomplete pass.
805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)//
815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// While the second pass is running, the AutocompleteController may cancel the
825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// request.  This can happen frequently when the user is typing quickly.  In
835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// this case, the main thread sets params_->cancel, which the background thread
845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// checks periodically.  If it finds the flag set, it stops what it's doing
855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// immediately and calls back to the main thread.  (We don't delete the params
865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// on the history thread, because we should only do that when we can safely
875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// NULL out params_, and that must be done on the main thread.)
885821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// Used to communicate autocomplete parameters between threads via the history
905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// service.
915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)struct HistoryURLProviderParams {
926d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // See comments on |promote_type| below.
936d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  enum PromoteType {
946d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    WHAT_YOU_TYPED_MATCH,
956d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    FRONT_HISTORY_MATCH,
966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)    NEITHER,
976d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  };
986d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProviderParams(const AutocompleteInput& input,
1005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                           bool trim_http,
101f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                           const AutocompleteMatch& what_you_typed_match,
1022a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const std::string& languages,
1032a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           TemplateURL* default_search_provider,
1042a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)                           const SearchTermsData& search_terms_data);
1055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~HistoryURLProviderParams();
1065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
107c2e0dbddbe15c98d52c4786dac06cb8952a8ae6dTorne (Richard Coles)  base::MessageLoop* message_loop;
1085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // A copy of the autocomplete input. We need the copy since this object will
1105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // live beyond the original query while it runs on the history thread.
1115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteInput input;
1125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Should inline autocompletion be disabled? This is initalized from
1145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // |input.prevent_inline_autocomplete()|, but set to false is the input
1155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // contains trailing white space.
1165821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool prevent_inline_autocomplete;
1175821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set when "http://" should be trimmed from the beginning of the URLs.
1195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool trim_http;
1205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
121f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // A match corresponding to what the user typed.
122f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  AutocompleteMatch what_you_typed_match;
123f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
1245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set by the main thread to cancel this request.  If this flag is set when
1255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the query runs, the query will be abandoned.  This allows us to avoid
1265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // running queries that are no longer needed.  Since we don't care if we run
1275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // the extra queries, the lack of signaling is not a problem.
1285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  base::CancellationFlag cancel_flag;
1295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Set by ExecuteWithDB() on the history thread when the query could not be
1315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // performed because the history system failed to properly init the database.
1325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // If this is set when the main thread is called back, it avoids changing
133f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |matches_| at all, so it won't delete the default match Start() creates.
1345821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool failed;
1355821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1366d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // List of matches written by DoAutocomplete().  Upon its return the provider
1376d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // converts this list to ACMatches and places them in |matches_|.
1386d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  history::HistoryMatches matches;
1396d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1406d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // True if the suggestion for exactly what the user typed appears as a known
1416d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // URL in the user's history.  In this case, this will also be the first match
1426d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // in |matches|.
1436d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  //
1446d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // NOTE: There are some complications related to keeping things consistent
1456d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // between passes and how we deal with intranet URLs, which are too complex to
1466d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // explain here; see the implementations of DoAutocomplete() and
1476d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // FixupExactSuggestion() for specific comments.
1486d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool exact_suggestion_is_in_history;
1496d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
1506d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Tells the provider whether to promote the what you typed match, the first
1516d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // element of |matches|, or neither as the first AutocompleteMatch.  If
1526d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // |exact_suggestion_is_in_history| is true (and thus "the what you typed
1536d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // match" and "the first element of |matches|" represent the same thing), this
1546d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // will be set to WHAT_YOU_TYPED_MATCH.
1556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  //
1566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // NOTE: The second pass of DoAutocomplete() checks what the first pass set
1576d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // this to.  See comments in DoAutocomplete().
1586d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  PromoteType promote_type;
1596d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
160116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // True if |what_you_typed_match| is eligible for display.  If this is true,
161116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // PromoteMatchesIfNecessary() may choose to place |what_you_typed_match| on
162116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // |matches_| even when |promote_type| is not WHAT_YOU_TYPED_MATCH.
1636d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool have_what_you_typed_match;
1645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1655821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Languages we should pass to gfx::GetCleanStringFromUrl.
1665821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  std::string languages;
1675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1682a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // The default search provider and search terms data necessary to cull results
1692a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // that correspond to searches (on the default engine).  These can only be
1702a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // obtained on the UI thread, so we have to copy them into here to pass them
1712a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // to the history thread.  We use a scoped_ptr<TemplateURL> for the DSP since
1722a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // TemplateURLs can't be copied by value. We use a scoped_ptr<SearchTermsData>
1732a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // so that we can store a snapshot of the SearchTermsData accessible from the
1742a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // history thread.
1752a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<TemplateURL> default_search_provider;
1762a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  scoped_ptr<SearchTermsData> search_terms_data;
1772a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1785821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
1795821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HistoryURLProviderParams);
1805821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
1815821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1825821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// This class is an autocomplete provider and is also a pseudo-internal
1835821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)// component of the history system.  See comments above.
1845821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)class HistoryURLProvider : public HistoryProvider {
1855821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) public:
1862a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // Various values used in scoring, made public so other providers
1872a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // can insert results in appropriate ranges relative to these.
1882a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForBestInlineableResult;
1892a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForUnvisitedIntranetResult;
1902a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kScoreForWhatYouTypedResult;
1912a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  static const int kBaseScoreForNonInlineableResult;
1922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)
1935821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProvider(AutocompleteProviderListener* listener, Profile* profile);
1945821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
1950f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // HistoryProvider:
1965821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Start(const AutocompleteInput& input,
1975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     bool minimal_changes) OVERRIDE;
1985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  virtual void Stop(bool clear_cached_results) OVERRIDE;
1995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2000f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // Returns a match representing a navigation to |destination_url| given user
2010f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // input of |text|.  |trim_http| controls whether the match's |fill_into_edit|
2020f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // and |contents| should have any HTTP scheme stripped off, and should not be
2030f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)  // set to true if |text| contains an http prefix.
2046d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // NOTES: This does not set the relevance of the returned match, as different
2056d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  //        callers want different behavior. Callers must set this manually.
2066d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  //        This function should only be called on the UI thread.
207a3f6a49ab37290eeeb8db0f41ec0f1cb74a68be7Torne (Richard Coles)  AutocompleteMatch SuggestExactInput(const base::string16& text,
2080f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                      const GURL& destination_url,
2090f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)                                      bool trim_http);
2100f1bc08d4cfcc34181b0b5cbf065c40f687bf740Torne (Richard Coles)
2115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Runs the history query on the history thread, called by the history
2125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // system. The history database MAY BE NULL in which case it is not
2135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // available and we should return no data. Also schedules returning the
2145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // results to the main thread
215116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  void ExecuteWithDB(HistoryURLProviderParams* params,
216116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                     history::HistoryBackend* backend,
217116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch                     history::URLDatabase* db);
2185821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2195821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles) private:
2205d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  FRIEND_TEST_ALL_PREFIXES(HistoryURLProviderTest, HUPScoringExperiment);
2215d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
2225821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  enum MatchType {
2235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    NORMAL,
2245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    WHAT_YOU_TYPED,
2255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    INLINE_AUTOCOMPLETE,
2265821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)    UNVISITED_INTRANET,  // An intranet site that has never been visited.
2275821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  };
2285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  class VisitClassifier;
2295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  ~HistoryURLProvider();
2315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
232f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Determines the relevance for a match, given its type.  If |match_type| is
233f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // NORMAL, |match_number| is a number indicating the relevance of the match
234f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // (higher == more relevant).  For other values of |match_type|,
235f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |match_number| is ignored.  Only called some of the time; for some matches,
236f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // relevancy scores are assigned consecutively decreasing (1416, 1415, ...).
237f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static int CalculateRelevance(MatchType match_type, int match_number);
238f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
239f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Returns a set of classifications that highlight all the occurrences of
240f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // |input_text| at word breaks in |description|.
241f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  static ACMatchClassifications ClassifyDescription(
242f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const base::string16& input_text,
243f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)      const base::string16& description);
244f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
245f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Actually runs the autocomplete job on the given database, which is
246f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // guaranteed not to be NULL.  Used by both autocomplete passes, and therefore
247f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // called on multiple different threads (though not simultaneously).
248f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void DoAutocomplete(history::HistoryBackend* backend,
249f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      history::URLDatabase* db,
250f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)                      HistoryURLProviderParams* params);
251f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
252116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // May promote the what you typed match, the first history match in
253116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // parmas->matches, or both to the front of |matches_|, depending on the
254116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  // values of params->promote_type and params->have_what_you_typed_match.
2556d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void PromoteMatchesIfNecessary(const HistoryURLProviderParams& params);
2566d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)
257f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Dispatches the results to the autocomplete controller. Called on the
258f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // main thread by ExecuteWithDB when the results are available.
259f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Frees params_gets_deleted on exit.
260f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  void QueryComplete(HistoryURLProviderParams* params_gets_deleted);
261f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)
262f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // Looks up the info for params->what_you_typed_match in the DB.  If found,
263f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  // fills in the title, promotes the match's priority to that of an inline
2645821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // autocomplete match (maybe it should be slightly better?), and places it on
2656d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // the front of params->matches (so we pick the right matches to throw away
2666d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // when culling redirects to/from it).  Returns whether a match was promoted.
2675821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool FixupExactSuggestion(history::URLDatabase* db,
2685821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                            const VisitClassifier& classifier,
2696d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)                            HistoryURLProviderParams* params) const;
2705821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2715821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for FixupExactSuggestion, this returns true if the input
2725821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // corresponds to some intranet URL where the user has previously visited the
2735821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // host in question.  In this case the input should be treated as a URL.
2745821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  bool CanFindIntranetURL(history::URLDatabase* db,
2755821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                          const AutocompleteInput& input) const;
2765821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2775821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Sees if a shorter version of the best match should be created, and if so
2786d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // places it at the front of params->matches.  This can suggest history URLs
2796d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // that are prefixes of the best match (if they've been visited enough,
2806d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // compared to the best match), or create host-only suggestions even when they
2816d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // haven't been visited before: if the user visited http://example.com/asdf
2826d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // once, we'll suggest http://example.com/ even if they've never been to it.
2836d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Returns true if a match was successfully created/promoted that we're
2846d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // willing to inline autocomplete.
2856d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  bool PromoteOrCreateShorterSuggestion(
2865821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      history::URLDatabase* db,
2875821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      bool have_what_you_typed_match,
2886d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      HistoryURLProviderParams* params);
2895821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2905821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes results that have been rarely typed or visited, and not any time
2915821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // recently.  The exact parameters for this heuristic can be found in the
2922a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // function body. Also culls results corresponding to queries from the default
2932a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // search engine. These are low-quality, difficult-to-understand matches for
2942a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // users, and the SearchProvider should surface past queries in a better way
2952a99a7e74a7f215066514fe81d2bfa6639d9edddTorne (Richard Coles)  // anyway.
2966d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  void CullPoorMatches(HistoryURLProviderParams* params) const;
2975821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
2985821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Removes results that redirect to each other, leaving at most |max_results|
2995821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // results.
3005821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  void CullRedirects(history::HistoryBackend* backend,
3015821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     history::HistoryMatches* matches,
3025821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                     size_t max_results) const;
3035821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3045821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Helper function for CullRedirects, this removes all but the first
3055821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // occurance of [any of the set of strings in |remove|] from the |matches|
3065821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // list.
3075821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  //
3085821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // The return value is the index of the item that is after the item in the
3095821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // input identified by |source_index|. If |source_index| or an item before
3105821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // is removed, the next item will be shifted, and this allows the caller to
3115821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // pick up on the next one when this happens.
3125821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  size_t RemoveSubsequentMatchesOf(history::HistoryMatches* matches,
3135821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   size_t source_index,
3145821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)                                   const std::vector<GURL>& remove) const;
3155821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3166d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // Converts a specified |match_number| from params.matches into an
3176d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // autocomplete match for display.  If experimental scoring is enabled, the
3186d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // final relevance score might be different from the given |relevance|.
3196d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)  // NOTE: This function should only be called on the UI thread.
3205821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  AutocompleteMatch HistoryMatchToACMatch(
3218bcbed890bc3ce4d7a057a8f32cab53fa534672eTorne (Richard Coles)      const HistoryURLProviderParams& params,
3226d86b77056ed63eb6871182f42a9fd5f07550f90Torne (Richard Coles)      size_t match_number,
3235821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      MatchType match_type,
3245821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)      int relevance);
3255821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
326116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch  AutocompleteProviderListener* listener_;
327116680a4aac90f2aa7413d9095a592090648e557Ben Murdoch
3285821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // Params for the current query.  The provider should not free this directly;
3295821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // instead, it is passed as a parameter through the history backend, and the
3305821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // parameter itself is freed once it's no longer needed.  The only reason we
3315821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  // keep this member is so we can set the cancel bit on it.
3325821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)  HistoryURLProviderParams* params_;
3335821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3345d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  // Params controlling experimental behavior of this provider.
3355d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)  HUPScoringParams scoring_params_;
3365d1f7b1de12d16ceb2c938c56701a3e8bfa558f7Torne (Richard Coles)
337f8ee788a64d60abd8f2d742a5fdedde054ecd910Torne (Richard Coles)  DISALLOW_COPY_AND_ASSIGN(HistoryURLProvider);
3385821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)};
3395821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)
3405821806d5e7f356e8fa4b058a389a808ea183019Torne (Richard Coles)#endif  // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_URL_PROVIDER_H_
341