| OLD | NEW |
| (Empty) |
| 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_AUTOCOMPLETE_CONTROLLER_H_ | |
| 6 #define CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_CONTROLLER_H_ | |
| 7 | |
| 8 #include "base/basictypes.h" | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/gtest_prod_util.h" | |
| 11 #include "base/memory/ref_counted.h" | |
| 12 #include "base/strings/string16.h" | |
| 13 #include "base/time/time.h" | |
| 14 #include "base/timer/timer.h" | |
| 15 #include "components/omnibox/autocomplete_input.h" | |
| 16 #include "components/omnibox/autocomplete_provider.h" | |
| 17 #include "components/omnibox/autocomplete_provider_client.h" | |
| 18 #include "components/omnibox/autocomplete_provider_listener.h" | |
| 19 #include "components/omnibox/autocomplete_result.h" | |
| 20 | |
| 21 class AutocompleteControllerDelegate; | |
| 22 class HistoryURLProvider; | |
| 23 class KeywordProvider; | |
| 24 class SearchProvider; | |
| 25 class TemplateURLService; | |
| 26 class ZeroSuggestProvider; | |
| 27 | |
| 28 // The AutocompleteController is the center of the autocomplete system. A | |
| 29 // class creates an instance of the controller, which in turn creates a set of | |
| 30 // AutocompleteProviders to serve it. The owning class can ask the controller | |
| 31 // to Start() a query; the controller in turn passes this call down to the | |
| 32 // providers, each of which keeps track of its own matches and whether it has | |
| 33 // finished processing the query. When a provider gets more matches or finishes | |
| 34 // processing, it notifies the controller, which merges the combined matches | |
| 35 // together and makes the result available to interested observers. | |
| 36 // | |
| 37 // The owner may also cancel the current query by calling Stop(), which the | |
| 38 // controller will in turn communicate to all the providers. No callbacks will | |
| 39 // happen after a request has been stopped. | |
| 40 // | |
| 41 // IMPORTANT: There is NO THREAD SAFETY built into this portion of the | |
| 42 // autocomplete system. All calls to and from the AutocompleteController should | |
| 43 // happen on the same thread. AutocompleteProviders are responsible for doing | |
| 44 // their own thread management when they need to return matches asynchronously. | |
| 45 // | |
| 46 // The coordinator for autocomplete queries, responsible for combining the | |
| 47 // matches from a series of providers into one AutocompleteResult. | |
| 48 class AutocompleteController : public AutocompleteProviderListener { | |
| 49 public: | |
| 50 typedef std::vector<scoped_refptr<AutocompleteProvider> > Providers; | |
| 51 | |
| 52 // |provider_types| is a bitmap containing AutocompleteProvider::Type values | |
| 53 // that will (potentially, depending on platform, flags, etc.) be | |
| 54 // instantiated. |template_url_service| is used to create URLs from the | |
| 55 // autocomplete results. | |
| 56 AutocompleteController(scoped_ptr<AutocompleteProviderClient> provider_client, | |
| 57 AutocompleteControllerDelegate* delegate, | |
| 58 int provider_types); | |
| 59 ~AutocompleteController() override; | |
| 60 | |
| 61 // Starts an autocomplete query, which continues until all providers are | |
| 62 // done or the query is Stop()ed. It is safe to Start() a new query without | |
| 63 // Stop()ing the previous one. | |
| 64 // | |
| 65 // See AutocompleteInput::AutocompleteInput(...) for more details regarding | |
| 66 // |input| params. | |
| 67 // | |
| 68 // The controller calls AutocompleteControllerDelegate::OnResultChanged() from | |
| 69 // inside this call at least once. If matches are available later on that | |
| 70 // result in changing the result set the delegate is notified again. When the | |
| 71 // controller is done the notification AUTOCOMPLETE_CONTROLLER_RESULT_READY is | |
| 72 // sent. | |
| 73 void Start(const AutocompleteInput& input); | |
| 74 | |
| 75 // Cancels the current query, ensuring there will be no future notifications | |
| 76 // fired. If new matches have come in since the most recent notification was | |
| 77 // fired, they will be discarded. | |
| 78 // | |
| 79 // If |clear_result| is true, the controller will also erase the result set. | |
| 80 void Stop(bool clear_result); | |
| 81 | |
| 82 // Asks the relevant provider to delete |match|, and ensures observers are | |
| 83 // notified of resulting changes immediately. This should only be called when | |
| 84 // no query is running. | |
| 85 void DeleteMatch(const AutocompleteMatch& match); | |
| 86 | |
| 87 // Removes any entries that were copied from the last result. This is used by | |
| 88 // the popup to ensure it's not showing an out-of-date query. | |
| 89 void ExpireCopiedEntries(); | |
| 90 | |
| 91 // AutocompleteProviderListener: | |
| 92 void OnProviderUpdate(bool updated_matches) override; | |
| 93 | |
| 94 // Called when an omnibox event log entry is generated. | |
| 95 // Populates provider_info with diagnostic information about the status | |
| 96 // of various providers. In turn, calls | |
| 97 // AutocompleteProvider::AddProviderInfo() so each provider can add | |
| 98 // provider-specific information, information we want to log for a particular | |
| 99 // provider but not others. | |
| 100 void AddProvidersInfo(ProvidersInfo* provider_info) const; | |
| 101 | |
| 102 // Called when a new omnibox session starts. | |
| 103 // We start a new session when the user first begins modifying the omnibox | |
| 104 // content; see |OmniboxEditModel::user_input_in_progress_|. | |
| 105 void ResetSession(); | |
| 106 | |
| 107 // Constructs the final destination URL for a given match using additional | |
| 108 // parameters otherwise not available at initial construction time. This | |
| 109 // method should be called from OmniboxEditModel::OpenMatch() before the user | |
| 110 // navigates to the selected match. | |
| 111 void UpdateMatchDestinationURLWithQueryFormulationTime( | |
| 112 base::TimeDelta query_formulation_time, | |
| 113 AutocompleteMatch* match) const; | |
| 114 | |
| 115 // Constructs the final destination URL for a given match using additional | |
| 116 // parameters otherwise not available at initial construction time. | |
| 117 void UpdateMatchDestinationURL( | |
| 118 const TemplateURLRef::SearchTermsArgs& search_terms_args, | |
| 119 AutocompleteMatch* match) const; | |
| 120 | |
| 121 HistoryURLProvider* history_url_provider() const { | |
| 122 return history_url_provider_; | |
| 123 } | |
| 124 KeywordProvider* keyword_provider() const { return keyword_provider_; } | |
| 125 SearchProvider* search_provider() const { return search_provider_; } | |
| 126 | |
| 127 // Deprecated. Do not use that method! It's provided temporarily as clank | |
| 128 // migrates. If you need to access the aucomplete input you should keep a | |
| 129 // local copy of it. | |
| 130 // TODO(beaudoin): Remove this method once clank no longer rely on it. | |
| 131 // crbug.com/367832 | |
| 132 const AutocompleteInput& input() const { return input_; } | |
| 133 | |
| 134 const AutocompleteResult& result() const { return result_; } | |
| 135 bool done() const { return done_; } | |
| 136 const Providers& providers() const { return providers_; } | |
| 137 | |
| 138 const base::TimeTicks& last_time_default_match_changed() const { | |
| 139 return last_time_default_match_changed_; | |
| 140 } | |
| 141 | |
| 142 private: | |
| 143 friend class AutocompleteProviderTest; | |
| 144 FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, | |
| 145 RedundantKeywordsIgnoredInResult); | |
| 146 FRIEND_TEST_ALL_PREFIXES(AutocompleteProviderTest, UpdateAssistedQueryStats); | |
| 147 FRIEND_TEST_ALL_PREFIXES(OmniboxViewTest, DoesNotUpdateAutocompleteOnBlur); | |
| 148 FRIEND_TEST_ALL_PREFIXES(OmniboxViewViewsTest, CloseOmniboxPopupOnTextDrag); | |
| 149 | |
| 150 // Updates |result_| to reflect the current provider state and fires | |
| 151 // notifications. If |regenerate_result| then we clear the result | |
| 152 // so when we incorporate the current provider state we end up | |
| 153 // implicitly removing all expired matches. (Normally we allow | |
| 154 // matches from the previous result set carry over. These stale | |
| 155 // results may outrank legitimate matches from the current result | |
| 156 // set. Sometimes we just want the current matches; the easier way | |
| 157 // to do this is to throw everything out and reconstruct the result | |
| 158 // set from the providers' current data.) | |
| 159 // If |force_notify_default_match_changed|, we tell NotifyChanged | |
| 160 // the default match has changed even if it hasn't. This is | |
| 161 // necessary in some cases; for instance, if the user typed a new | |
| 162 // character, the edit model needs to repaint (highlighting changed) | |
| 163 // even if the default match didn't change. | |
| 164 void UpdateResult(bool regenerate_result, | |
| 165 bool force_notify_default_match_changed); | |
| 166 | |
| 167 // Updates |result| to populate each match's |associated_keyword| if that | |
| 168 // match can show a keyword hint. |result| should be sorted by | |
| 169 // relevance before this is called. | |
| 170 void UpdateAssociatedKeywords(AutocompleteResult* result); | |
| 171 | |
| 172 // For each group of contiguous matches from the same TemplateURL, show the | |
| 173 // provider name as a description on the first match in the group. | |
| 174 void UpdateKeywordDescriptions(AutocompleteResult* result); | |
| 175 | |
| 176 // For each AutocompleteMatch returned by SearchProvider, updates the | |
| 177 // destination_url iff the provider's TemplateURL supports assisted query | |
| 178 // stats. | |
| 179 void UpdateAssistedQueryStats(AutocompleteResult* result); | |
| 180 | |
| 181 // Calls AutocompleteControllerDelegate::OnResultChanged() and if done sends | |
| 182 // AUTOCOMPLETE_CONTROLLER_RESULT_READY. | |
| 183 void NotifyChanged(bool notify_default_match); | |
| 184 | |
| 185 // Updates |done_| to be accurate with respect to current providers' statuses. | |
| 186 void CheckIfDone(); | |
| 187 | |
| 188 // Starts |expire_timer_|. | |
| 189 void StartExpireTimer(); | |
| 190 | |
| 191 // Starts |stop_timer_|. | |
| 192 void StartStopTimer(); | |
| 193 | |
| 194 // Helper function for Stop(). |due_to_user_inactivity| means this call was | |
| 195 // triggered by a user's idleness, i.e., not an explicit user action. | |
| 196 void StopHelper(bool clear_result, | |
| 197 bool due_to_user_inactivity); | |
| 198 | |
| 199 AutocompleteControllerDelegate* delegate_; | |
| 200 | |
| 201 // The client passed to the providers. | |
| 202 scoped_ptr<AutocompleteProviderClient> provider_client_; | |
| 203 | |
| 204 // A list of all providers. | |
| 205 Providers providers_; | |
| 206 | |
| 207 HistoryURLProvider* history_url_provider_; | |
| 208 | |
| 209 KeywordProvider* keyword_provider_; | |
| 210 | |
| 211 SearchProvider* search_provider_; | |
| 212 | |
| 213 ZeroSuggestProvider* zero_suggest_provider_; | |
| 214 | |
| 215 // Input passed to Start. | |
| 216 AutocompleteInput input_; | |
| 217 | |
| 218 // Data from the autocomplete query. | |
| 219 AutocompleteResult result_; | |
| 220 | |
| 221 // The most recent time the default match (inline match) changed. This may | |
| 222 // be earlier than the most recent keystroke if the recent keystrokes didn't | |
| 223 // change the suggested match in the omnibox. (For instance, if | |
| 224 // a user typed "mail.goog" and the match https://mail.google.com/ was | |
| 225 // the destination match ever since the user typed "ma" then this is | |
| 226 // the time that URL first appeared as the default match.) This may | |
| 227 // also be more recent than the last keystroke if there was an | |
| 228 // asynchronous provider that returned and changed the default | |
| 229 // match. See UpdateResult() for details on when we consider a | |
| 230 // match to have changed. | |
| 231 base::TimeTicks last_time_default_match_changed_; | |
| 232 | |
| 233 // Timer used to remove any matches copied from the last result. When run | |
| 234 // invokes |ExpireCopiedEntries|. | |
| 235 base::OneShotTimer<AutocompleteController> expire_timer_; | |
| 236 | |
| 237 // Timer used to tell the providers to Stop() searching for matches. | |
| 238 base::OneShotTimer<AutocompleteController> stop_timer_; | |
| 239 | |
| 240 // Amount of time (in ms) between when the user stops typing and | |
| 241 // when we send Stop() to every provider. This is intended to avoid | |
| 242 // the disruptive effect of belated omnibox updates, updates that | |
| 243 // come after the user has had to time to read the whole dropdown | |
| 244 // and doesn't expect it to change. | |
| 245 const base::TimeDelta stop_timer_duration_; | |
| 246 | |
| 247 // True if a query is not currently running. | |
| 248 bool done_; | |
| 249 | |
| 250 // Are we in Start()? This is used to avoid updating |result_| and sending | |
| 251 // notifications until Start() has been invoked on all providers. | |
| 252 bool in_start_; | |
| 253 | |
| 254 TemplateURLService* template_url_service_; | |
| 255 | |
| 256 DISALLOW_COPY_AND_ASSIGN(AutocompleteController); | |
| 257 }; | |
| 258 | |
| 259 #endif // CHROME_BROWSER_AUTOCOMPLETE_AUTOCOMPLETE_CONTROLLER_H_ | |
| OLD | NEW |