1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201
|
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.
//
// This file contains the zero-suggest autocomplete provider. This experimental
// provider is invoked when the user focuses in the omnibox prior to editing,
// and generates search query suggestions based on the current URL.
#ifndef COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
#define COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
#include <memory>
#include <string>
#include "base/compiler_specific.h"
#include "base/macros.h"
#include "components/history/core/browser/history_types.h"
#include "components/omnibox/browser/base_search_provider.h"
#include "components/omnibox/browser/search_provider.h"
#include "third_party/metrics_proto/omnibox_event.pb.h"
class AutocompleteProviderListener;
class HistoryURLProvider;
namespace base {
class Value;
}
namespace network {
class SimpleURLLoader;
}
namespace user_prefs {
class PrefRegistrySyncable;
}
// Autocomplete provider for searches based on the current URL.
//
// The controller will call Start() when the user focuses the omnibox. After
// construction, the autocomplete controller repeatedly calls Start() with some
// user input, each time expecting to receive an updated set of matches.
//
// TODO(jered): Consider deleting this class and building this functionality
// into SearchProvider after dogfood and after we break the association between
// omnibox text and suggestions.
class ZeroSuggestProvider : public BaseSearchProvider {
public:
// Creates and returns an instance of this provider.
static ZeroSuggestProvider* Create(AutocompleteProviderClient* client,
HistoryURLProvider* history_url_provider,
AutocompleteProviderListener* listener);
// Registers a preference used to cache zero suggest results.
static void RegisterProfilePrefs(user_prefs::PrefRegistrySyncable* registry);
// AutocompleteProvider:
void Start(const AutocompleteInput& input, bool minimal_changes) override;
void Stop(bool clear_cached_results,
bool due_to_user_inactivity) override;
void DeleteMatch(const AutocompleteMatch& match) override;
void AddProviderInfo(ProvidersInfo* provider_info) const override;
// Sets |field_trial_triggered_| to false.
void ResetSession() override;
private:
FRIEND_TEST_ALL_PREFIXES(ZeroSuggestProviderTest,
TestStartWillStopForSomeInput);
ZeroSuggestProvider(AutocompleteProviderClient* client,
HistoryURLProvider* history_url_provider,
AutocompleteProviderListener* listener);
~ZeroSuggestProvider() override;
// ZeroSuggestProvider is processing one of the following type of results
// at any time.
enum ResultType {
NONE,
DEFAULT_SERP, // The default search provider is queried for
// zero-suggest suggestions.
DEFAULT_SERP_FOR_URL, // The default search provider is queried for
// zero-suggest suggestions that are specific
// to the visited URL.
MOST_VISITED
};
// BaseSearchProvider:
const TemplateURL* GetTemplateURL(bool is_keyword) const override;
const AutocompleteInput GetInput(bool is_keyword) const override;
bool ShouldAppendExtraParams(
const SearchSuggestionParser::SuggestResult& result) const override;
void RecordDeletionResult(bool success) override;
// Called when loading is complete.
void OnURLLoadComplete(const network::SimpleURLLoader* source,
std::unique_ptr<std::string> response_body);
// The function updates |results_| with data parsed from |json_data|.
//
// * The update is not performed if |json_data| is invalid.
// * When the provider is using cached results and |json_data| is non-empty,
// this function updates the cached results.
// * When |results_| contains cached results, these are updated only if
// |json_cata| corresponds to an empty list. This is done to ensure that
// the display is cleared, as it may be showing cached results that should
// not be shown.
//
// The return value is true only when |results_| changed.
bool UpdateResults(const std::string& json_data);
// Adds AutocompleteMatches for each of the suggestions in |results| to
// |map|.
void AddSuggestResultsToMap(
const SearchSuggestionParser::SuggestResults& results,
MatchMap* map);
// Returns an AutocompleteMatch for a navigational suggestion |navigation|.
AutocompleteMatch NavigationToMatch(
const SearchSuggestionParser::NavigationResult& navigation);
// Converts the parsed results to a set of AutocompleteMatches and adds them
// to |matches_|. Also update the histograms for how many results were
// received.
void ConvertResultsToAutocompleteMatches();
// Returns an AutocompleteMatch for the current URL. The match should be in
// the top position so that pressing enter has the effect of reloading the
// page.
AutocompleteMatch MatchForCurrentURL();
// When the user is in the Most Visited field trial, we ask the TopSites
// service for the most visited URLs. It then calls back to this function to
// return those |urls|.
void OnMostVisitedUrlsAvailable(size_t request_num,
const history::MostVisitedURLList& urls);
// When the user is in the contextual omnibox suggestions field trial, we ask
// the ContextualSuggestionsService for a loader to retrieve recommendations.
// When the loader has started, the contextual suggestion service then calls
// back to this function with the |loader| to pass its ownership to |this|.
void OnContextualSuggestionsLoaderAvailable(
std::unique_ptr<network::SimpleURLLoader> loader);
// Whether zero suggest suggestions are allowed in the given context.
bool AllowZeroSuggestSuggestions(const GURL& current_page_url) const;
// Checks whether we have a set of zero suggest results cached, and if so
// populates |matches_| with cached results.
void MaybeUseCachedSuggestions();
// Returns the type of results that should be generated for the current
// context.
// Logs UMA metrics. Should be called exactly once, on Start(), otherwise the
// meaning of the data logged would change.
ResultType TypeOfResultToRun(const GURL& current_url,
const GURL& suggest_url);
// Used for efficiency when creating the verbatim match. Can be null.
HistoryURLProvider* history_url_provider_;
AutocompleteProviderListener* listener_;
// The result type that is currently being processed by provider.
// When the provider is not running, the result type is set to NONE.
ResultType result_type_running_;
// For reconciling asynchronous requests for most visited URLs.
size_t most_visited_request_num_ = 0;
// The URL for which a suggestion fetch is pending.
std::string current_query_;
// The title of the page for which a suggestion fetch is pending.
base::string16 current_title_;
// The type of page the user is viewing (a search results page doing search
// term replacement, an arbitrary URL, etc.).
metrics::OmniboxEventProto::PageClassification current_page_classification_;
// Copy of OmniboxEditModel::permanent_text_.
base::string16 permanent_text_;
// Loader used to retrieve results.
std::unique_ptr<network::SimpleURLLoader> loader_;
// Suggestion for the current URL.
AutocompleteMatch current_url_match_;
// Contains suggest and navigation results as well as relevance parsed from
// the response for the most recent zero suggest input URL.
SearchSuggestionParser::Results results_;
history::MostVisitedURLList most_visited_urls_;
// For callbacks that may be run after destruction.
base::WeakPtrFactory<ZeroSuggestProvider> weak_ptr_factory_;
DISALLOW_COPY_AND_ASSIGN(ZeroSuggestProvider);
};
#endif // COMPONENTS_OMNIBOX_BROWSER_ZERO_SUGGEST_PROVIDER_H_
|