Home | History | Annotate | Download | only in autocomplete
      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 
     12 namespace bookmark_utils {
     13 struct 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.
     23 class 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  private:
     34   ~HistoryContentsProvider();
     35 
     36   void QueryComplete(HistoryService::Handle handle,
     37                      history::QueryResults* results);
     38 
     39   // Converts each MatchingPageResult in results_ to an AutocompleteMatch and
     40   // adds it to matches_.
     41   void ConvertResults();
     42 
     43   // Creates and returns an AutocompleteMatch from a MatchingPageResult.
     44   AutocompleteMatch ResultToMatch(const history::URLResult& result,
     45                                   int score);
     46 
     47   // Adds ACMatchClassifications to match from the offset positions in
     48   // page_result.
     49   void ClassifyDescription(const history::URLResult& result,
     50                            AutocompleteMatch* match) const;
     51 
     52   // Calculates and returns the relevance of a particular result. See the
     53   // chart in autocomplete.h for the list of values this returns.
     54   int CalculateRelevance(const history::URLResult& result);
     55 
     56   // Queries the bookmarks for any bookmarks whose title matches input. All
     57   // matches are added directly to results_.
     58   void QueryBookmarks(const AutocompleteInput& input);
     59 
     60   // Converts a BookmarkModel::TitleMatch to a QueryResult and adds it to
     61   // results_.
     62   void AddBookmarkTitleMatchToResults(const bookmark_utils::TitleMatch& match);
     63 
     64   CancelableRequestConsumerT<int, 0> request_consumer_;
     65 
     66   // The number of times we're returned each different type of result. These are
     67   // used by CalculateRelevance. Initialized in Start.
     68   int star_title_count_;
     69   int star_contents_count_;
     70   int title_count_;
     71   int contents_count_;
     72 
     73   // Current autocomplete input type.
     74   AutocompleteInput::Type input_type_;
     75 
     76   // Whether we should trim "http://" from results.
     77   bool trim_http_;
     78 
     79   // Results from most recent query. These are cached so we don't have to
     80   // re-issue queries for "minor changes" (which don't affect this provider).
     81   history::QueryResults results_;
     82 
     83   // Whether results_ is valid (so we can tell invalid apart from empty).
     84   bool have_results_;
     85 
     86   // Current query string.
     87   string16 query_;
     88 
     89   DISALLOW_COPY_AND_ASSIGN(HistoryContentsProvider);
     90 };
     91 
     92 #endif  // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_CONTENTS_PROVIDER_H_
     93