bookmark_provider.h revision 116680a4aac90f2aa7413d9095a592090648e557
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#ifndef CHROME_BROWSER_AUTOCOMPLETE_BOOKMARK_PROVIDER_H_
6#define CHROME_BROWSER_AUTOCOMPLETE_BOOKMARK_PROVIDER_H_
7
8#include <string>
9
10#include "chrome/browser/autocomplete/autocomplete_match.h"
11#include "chrome/browser/autocomplete/autocomplete_provider.h"
12#include "components/autocomplete/autocomplete_input.h"
13#include "components/query_parser/snippet.h"
14
15class BookmarkModel;
16class Profile;
17
18namespace bookmarks {
19struct BookmarkMatch;
20}
21
22// This class is an autocomplete provider which quickly (and synchronously)
23// provides autocomplete suggestions based on the titles of bookmarks. Page
24// titles, URLs, and typed and visit counts of the bookmarks are not currently
25// taken into consideration as those factors will have been used by the
26// HistoryQuickProvider (HQP) while identifying suggestions.
27//
28// TODO(mrossetti): Propose a way to coordinate with the HQP the taking of typed
29// and visit counts and last visit dates, etc. into consideration while scoring.
30class BookmarkProvider : public AutocompleteProvider {
31 public:
32  explicit BookmarkProvider(Profile* profile);
33
34  // When |minimal_changes| is true short circuit any additional searching and
35  // leave the previous matches for this provider unchanged, otherwise perform
36  // a complete search for |input| across all bookmark titles.
37  virtual void Start(const AutocompleteInput& input,
38                     bool minimal_changes) OVERRIDE;
39
40  // Sets the BookmarkModel for unit tests.
41  void set_bookmark_model_for_testing(BookmarkModel* bookmark_model) {
42    bookmark_model_ = bookmark_model;
43  }
44
45 private:
46  FRIEND_TEST_ALL_PREFIXES(BookmarkProviderTest, InlineAutocompletion);
47
48  virtual ~BookmarkProvider();
49
50  // Performs the actual matching of |input| over the bookmarks and fills in
51  // |matches_|.
52  void DoAutocomplete(const AutocompleteInput& input);
53
54  // Compose an AutocompleteMatch based on |match| that has 1) the URL of
55  // |match|'s bookmark, and 2) the bookmark's title, not the URL's page
56  // title, as the description.  |input| is used to compute the match's
57  // inline_autocompletion.  |fixed_up_input_text| is used in that way as well;
58  // it's passed separately so this function doesn't have to compute it.
59  AutocompleteMatch BookmarkMatchToACMatch(
60      const AutocompleteInput& input,
61      const base::string16& fixed_up_input_text,
62      const bookmarks::BookmarkMatch& match);
63
64  // Converts |positions| into ACMatchClassifications and returns the
65  // classifications. |text_length| is used to determine the need to add an
66  // 'unhighlighted' classification span so the tail of the source string
67  // properly highlighted.
68  static ACMatchClassifications ClassificationsFromMatch(
69      const query_parser::Snippet::MatchPositions& positions,
70      size_t text_length,
71      bool is_url);
72
73  Profile* profile_;
74  BookmarkModel* bookmark_model_;
75
76  // True if we should use matches in the URL for scoring.
77  const bool score_using_url_matches_;
78
79  // Languages used during the URL formatting.
80  std::string languages_;
81
82  DISALLOW_COPY_AND_ASSIGN(BookmarkProvider);
83};
84
85#endif  // CHROME_BROWSER_AUTOCOMPLETE_BOOKMARK_PROVIDER_H_
86