history_quick_provider.h revision 5821806d5e7f356e8fa4b058a389a808ea183019
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_HISTORY_QUICK_PROVIDER_H_ 6#define CHROME_BROWSER_AUTOCOMPLETE_HISTORY_QUICK_PROVIDER_H_ 7 8#include <string> 9 10#include "base/basictypes.h" 11#include "base/compiler_specific.h" 12#include "chrome/browser/autocomplete/autocomplete_input.h" 13#include "chrome/browser/autocomplete/autocomplete_match.h" 14#include "chrome/browser/autocomplete/history_provider.h" 15#include "chrome/browser/history/history_types.h" 16#include "chrome/browser/history/in_memory_url_index.h" 17 18class Profile; 19class TermMatches; 20 21namespace history { 22struct ScoredHistoryMatch; 23} // namespace history 24 25// This class is an autocomplete provider (a pseudo-internal component of 26// the history system) which quickly (and synchronously) provides matching 27// results from recently or frequently visited sites in the profile's 28// history. 29class HistoryQuickProvider : public HistoryProvider { 30 public: 31 HistoryQuickProvider(AutocompleteProviderListener* listener, 32 Profile* profile); 33 34 // AutocompleteProvider. |minimal_changes| is ignored since there is no asynch 35 // completion performed. 36 virtual void Start(const AutocompleteInput& input, 37 bool minimal_changes) OVERRIDE; 38 39 virtual void DeleteMatch(const AutocompleteMatch& match) OVERRIDE; 40 41 // Disable this provider. For unit testing purposes only. This is required 42 // because this provider is closely associated with the HistoryURLProvider 43 // and in order to properly test the latter the HistoryQuickProvider must 44 // be disabled. 45 // TODO(mrossetti): Eliminate this once the HUP has been refactored. 46 static void set_disabled(bool disabled) { disabled_ = disabled; } 47 48 private: 49 friend class HistoryQuickProviderTest; 50 FRIEND_TEST_ALL_PREFIXES(HistoryQuickProviderTest, Spans); 51 FRIEND_TEST_ALL_PREFIXES(HistoryQuickProviderTest, Relevance); 52 53 virtual ~HistoryQuickProvider(); 54 55 // Performs the autocomplete matching and scoring. 56 void DoAutocomplete(); 57 58 // Creates an AutocompleteMatch from |history_match|, assigning it 59 // the score |score|. 60 AutocompleteMatch QuickMatchToACMatch( 61 const history::ScoredHistoryMatch& history_match, 62 int score); 63 64 // Returns the index that should be used for history lookups. 65 history::InMemoryURLIndex* GetIndex(); 66 67 // Fill and return an ACMatchClassifications structure given the term 68 // matches (|matches|) to highlight where terms were found. 69 static ACMatchClassifications SpansFromTermMatch( 70 const history::TermMatches& matches, 71 size_t text_length, 72 bool is_url); 73 74 // Only for use in unittests. Takes ownership of |index|. 75 void set_index(history::InMemoryURLIndex* index) { 76 index_for_testing_.reset(index); 77 } 78 79 AutocompleteInput autocomplete_input_; 80 std::string languages_; 81 82 // True if we're allowed to reorder results depending on 83 // inlineability in order to assign higher relevance scores. 84 // Consider a case where ScoredHistoryMatch provides results x and 85 // y, where x is not inlineable and has a score of 3000 and y is 86 // inlineable and has a score of 2500. If reorder_for_inlining_ is 87 // false, then x gets demoted to a non-inlineable score (1199) and y 88 // gets demoted to a lower score (1198) because we try to preserve 89 // the order. On the other hand, if reorder_for_inlining_ is true, 90 // then y keeps its score of 2500 and x gets demoted to 2499 in 91 // order to follow y. There will not be any problems with an 92 // unexpected inline because the non-inlineable result x scores 93 // lower than the inlineable one. 94 // TODO(mpearson): remove this variable after we're done experimenting. 95 // (This member is meant to only exist for experimentation purposes. 96 // Once we know which behavior is better, we should rip out this variable 97 // and make the best behavior the default.) 98 bool reorder_for_inlining_; 99 100 // Only used for testing. 101 scoped_ptr<history::InMemoryURLIndex> index_for_testing_; 102 103 // This provider is disabled when true. 104 static bool disabled_; 105 106 DISALLOW_COPY_AND_ASSIGN(HistoryQuickProvider); 107}; 108 109#endif // CHROME_BROWSER_AUTOCOMPLETE_HISTORY_QUICK_PROVIDER_H_ 110