history_contents_provider.h revision 21d179b334e59e9a3bfcaed4c4430bef1bc5759d
1// Copyright (c) 2010 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_HISTORY_CONTENTS_PROVIDER_H_
6#define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_CONTENTS_PROVIDER_H_
7#pragma once
8
9#include "chrome/browser/autocomplete/history_provider.h"
10#include "chrome/browser/history/history.h"
11
12namespace bookmark_utils {
13struct TitleMatch;
14}
15
16// HistoryContentsProvider is an AutocompleteProvider that provides results from
17// the contents (body and/or title) of previously visited pages.
18// HistoryContentsProvider gets results from two sources:
19// . HistoryService: this provides results for matches in the body/title of
20//   previously viewed pages. This is asynchronous.
21// . BookmarkModel: provides results for matches in the titles of bookmarks.
22//   This is synchronous.
23class HistoryContentsProvider : public HistoryProvider {
24 public:
25  HistoryContentsProvider(ACProviderListener* listener, Profile* profile);
26
27  // As necessary asks the history service for the relevant results. When
28  // done SetResults is invoked.
29  virtual void Start(const AutocompleteInput& input,
30                     bool minimal_changes) OVERRIDE;
31  virtual void Stop() OVERRIDE;
32
33  // Returns the total number of matches available in the database, up to
34  // kMaxMatchCount, whichever is smaller.
35  // Return value is incomplete if done() returns false.
36  size_t db_match_count() const { return results_.size(); }
37
38  // The maximum match count we'll report. If the db_match_count is greater
39  // than this, it will be clamped to this result.
40  static const size_t kMaxMatchCount = 50;
41
42 private:
43  ~HistoryContentsProvider();
44
45  void QueryComplete(HistoryService::Handle handle,
46                     history::QueryResults* results);
47
48  // Converts each MatchingPageResult in results_ to an AutocompleteMatch and
49  // adds it to matches_.
50  void ConvertResults();
51
52  // Creates and returns an AutocompleteMatch from a MatchingPageResult.
53  AutocompleteMatch ResultToMatch(const history::URLResult& result,
54                                  int score);
55
56  // Adds ACMatchClassifications to match from the offset positions in
57  // page_result.
58  void ClassifyDescription(const history::URLResult& result,
59                           AutocompleteMatch* match) const;
60
61  // Calculates and returns the relevance of a particular result. See the
62  // chart in autocomplete.h for the list of values this returns.
63  int CalculateRelevance(const history::URLResult& result);
64
65  // Queries the bookmarks for any bookmarks whose title matches input. All
66  // matches are added directly to results_.
67  void QueryBookmarks(const AutocompleteInput& input);
68
69  // Converts a BookmarkModel::TitleMatch to a QueryResult and adds it to
70  // results_.
71  void AddBookmarkTitleMatchToResults(const bookmark_utils::TitleMatch& match);
72
73  CancelableRequestConsumerT<int, 0> request_consumer_;
74
75  // The number of times we're returned each different type of result. These are
76  // used by CalculateRelevance. Initialized in Start.
77  int star_title_count_;
78  int star_contents_count_;
79  int title_count_;
80  int contents_count_;
81
82  // Current autocomplete input type.
83  AutocompleteInput::Type input_type_;
84
85  // Whether we should trim "http://" from results.
86  bool trim_http_;
87
88  // Results from most recent query. These are cached so we don't have to
89  // re-issue queries for "minor changes" (which don't affect this provider).
90  history::QueryResults results_;
91
92  // Whether results_ is valid (so we can tell invalid apart from empty).
93  bool have_results_;
94
95  // Current query string.
96  std::wstring query_;
97
98  DISALLOW_COPY_AND_ASSIGN(HistoryContentsProvider);
99};
100
101#endif  // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_CONTENTS_PROVIDER_H_
102