OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/autocomplete/autocomplete_controller.h" | 5 #include "chrome/browser/autocomplete/autocomplete_controller.h" |
6 | 6 |
7 #include <set> | 7 #include <set> |
8 #include <string> | 8 #include <string> |
9 | 9 |
| 10 #include "base/command_line.h" |
10 #include "base/format_macros.h" | 11 #include "base/format_macros.h" |
11 #include "base/logging.h" | 12 #include "base/logging.h" |
12 #include "base/metrics/histogram.h" | 13 #include "base/metrics/histogram.h" |
13 #include "base/string_number_conversions.h" | 14 #include "base/string_number_conversions.h" |
14 #include "base/stringprintf.h" | 15 #include "base/stringprintf.h" |
15 #include "base/time.h" | 16 #include "base/time.h" |
16 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" | 17 #include "chrome/browser/autocomplete/autocomplete_controller_delegate.h" |
17 #include "chrome/browser/autocomplete/builtin_provider.h" | 18 #include "chrome/browser/autocomplete/builtin_provider.h" |
18 #include "chrome/browser/autocomplete/extension_app_provider.h" | 19 #include "chrome/browser/autocomplete/extension_app_provider.h" |
19 #include "chrome/browser/autocomplete/history_contents_provider.h" | 20 #include "chrome/browser/autocomplete/history_contents_provider.h" |
20 #include "chrome/browser/autocomplete/history_quick_provider.h" | 21 #include "chrome/browser/autocomplete/history_quick_provider.h" |
21 #include "chrome/browser/autocomplete/history_url_provider.h" | 22 #include "chrome/browser/autocomplete/history_url_provider.h" |
22 #include "chrome/browser/autocomplete/keyword_provider.h" | 23 #include "chrome/browser/autocomplete/keyword_provider.h" |
23 #include "chrome/browser/autocomplete/search_provider.h" | 24 #include "chrome/browser/autocomplete/search_provider.h" |
24 #include "chrome/browser/autocomplete/shortcuts_provider.h" | 25 #include "chrome/browser/autocomplete/shortcuts_provider.h" |
| 26 #include "chrome/browser/autocomplete/zerosuggest_provider.h" |
25 #include "chrome/browser/profiles/profile.h" | 27 #include "chrome/browser/profiles/profile.h" |
26 #include "chrome/browser/search_engines/template_url.h" | 28 #include "chrome/browser/search_engines/template_url.h" |
27 #include "chrome/common/chrome_notification_types.h" | 29 #include "chrome/common/chrome_notification_types.h" |
| 30 #include "chrome/common/chrome_switches.h" |
28 #include "content/public/browser/notification_service.h" | 31 #include "content/public/browser/notification_service.h" |
29 #include "grit/generated_resources.h" | 32 #include "grit/generated_resources.h" |
30 #include "grit/theme_resources.h" | 33 #include "grit/theme_resources.h" |
31 #include "ui/base/l10n/l10n_util.h" | 34 #include "ui/base/l10n/l10n_util.h" |
32 | 35 |
33 namespace { | 36 namespace { |
34 | 37 |
35 // Converts the given type to an integer based on the AQS specification. | 38 // Converts the given type to an integer based on the AQS specification. |
36 // For more details, See http://goto.google.com/binary-clients-logging . | 39 // For more details, See http://goto.google.com/binary-clients-logging . |
37 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) { | 40 int AutocompleteMatchToAssistedQueryType(const AutocompleteMatch::Type& type) { |
(...skipping 31 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
69 | 72 |
70 } // namespace | 73 } // namespace |
71 | 74 |
72 const int AutocompleteController::kNoItemSelected = -1; | 75 const int AutocompleteController::kNoItemSelected = -1; |
73 | 76 |
74 AutocompleteController::AutocompleteController( | 77 AutocompleteController::AutocompleteController( |
75 Profile* profile, | 78 Profile* profile, |
76 AutocompleteControllerDelegate* delegate) | 79 AutocompleteControllerDelegate* delegate) |
77 : delegate_(delegate), | 80 : delegate_(delegate), |
78 keyword_provider_(NULL), | 81 keyword_provider_(NULL), |
| 82 zerosuggest_provider_(NULL), |
79 done_(true), | 83 done_(true), |
80 in_start_(false), | 84 in_start_(false), |
81 profile_(profile) { | 85 profile_(profile) { |
82 search_provider_ = new SearchProvider(this, profile); | 86 search_provider_ = new SearchProvider(this, profile); |
83 providers_.push_back(search_provider_); | 87 providers_.push_back(search_provider_); |
84 #if !defined(OS_ANDROID) | 88 #if !defined(OS_ANDROID) |
85 // History quick provider is enabled on all platforms other than Android. | 89 // History quick provider is enabled on all platforms other than Android. |
86 bool hqp_enabled = true; | 90 bool hqp_enabled = true; |
87 providers_.push_back(new HistoryQuickProvider(this, profile)); | 91 providers_.push_back(new HistoryQuickProvider(this, profile)); |
88 // Search provider/"tab to search" is enabled on all platforms other than | 92 // Search provider/"tab to search" is enabled on all platforms other than |
89 // Android. | 93 // Android. |
90 keyword_provider_ = new KeywordProvider(this, profile); | 94 keyword_provider_ = new KeywordProvider(this, profile); |
91 providers_.push_back(keyword_provider_); | 95 providers_.push_back(keyword_provider_); |
92 #else | 96 #else |
93 // TODO(mrossetti): Remove the following and permanently modify the | 97 // TODO(mrossetti): Remove the following and permanently modify the |
94 // HistoryURLProvider to not search titles once HQP is turned on permanently. | 98 // HistoryURLProvider to not search titles once HQP is turned on permanently. |
95 // TODO(jcivelli): Enable the History Quick Provider and figure out why it | 99 // TODO(jcivelli): Enable the History Quick Provider and figure out why it |
96 // reports the wrong results for some pages. | 100 // reports the wrong results for some pages. |
97 bool hqp_enabled = false; | 101 bool hqp_enabled = false; |
98 #endif // !OS_ANDROID | 102 #endif // !OS_ANDROID |
99 providers_.push_back(new HistoryURLProvider(this, profile)); | 103 providers_.push_back(new HistoryURLProvider(this, profile)); |
100 providers_.push_back(new ShortcutsProvider(this, profile)); | 104 providers_.push_back(new ShortcutsProvider(this, profile)); |
101 providers_.push_back(new HistoryContentsProvider(this, profile, hqp_enabled)); | 105 providers_.push_back(new HistoryContentsProvider(this, profile, hqp_enabled)); |
102 providers_.push_back(new BuiltinProvider(this, profile)); | 106 providers_.push_back(new BuiltinProvider(this, profile)); |
103 providers_.push_back(new ExtensionAppProvider(this, profile)); | 107 providers_.push_back(new ExtensionAppProvider(this, profile)); |
| 108 // Create Zerosuggest if its switch is present. |
| 109 CommandLine* cl = CommandLine::ForCurrentProcess(); |
| 110 if (cl->HasSwitch(switches::kExperimentalZerosuggestURLPrefix)) { |
| 111 zerosuggest_provider_ = new ZerosuggestProvider(this, profile, |
| 112 cl->GetSwitchValueASCII(switches::kExperimentalZerosuggestURLPrefix)); |
| 113 providers_.push_back(zerosuggest_provider_); |
| 114 } |
104 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) | 115 for (ACProviders::iterator i(providers_.begin()); i != providers_.end(); ++i) |
105 (*i)->AddRef(); | 116 (*i)->AddRef(); |
106 } | 117 } |
107 | 118 |
108 AutocompleteController::~AutocompleteController() { | 119 AutocompleteController::~AutocompleteController() { |
109 // The providers may have tasks outstanding that hold refs to them. We need | 120 // The providers may have tasks outstanding that hold refs to them. We need |
110 // to ensure they won't call us back if they outlive us. (Practically, | 121 // to ensure they won't call us back if they outlive us. (Practically, |
111 // calling Stop() should also cancel those tasks and make it so that we hold | 122 // calling Stop() should also cancel those tasks and make it so that we hold |
112 // the only refs.) We also don't want to bother notifying anyone of our | 123 // the only refs.) We also don't want to bother notifying anyone of our |
113 // result changes here, because the notification observer is in the midst of | 124 // result changes here, because the notification observer is in the midst of |
(...skipping 68 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
182 expire_timer_.Stop(); | 193 expire_timer_.Stop(); |
183 done_ = true; | 194 done_ = true; |
184 if (clear_result && !result_.empty()) { | 195 if (clear_result && !result_.empty()) { |
185 result_.Reset(); | 196 result_.Reset(); |
186 // NOTE: We pass in false since we're trying to only clear the popup, not | 197 // NOTE: We pass in false since we're trying to only clear the popup, not |
187 // touch the edit... this is all a mess and should be cleaned up :( | 198 // touch the edit... this is all a mess and should be cleaned up :( |
188 NotifyChanged(false); | 199 NotifyChanged(false); |
189 } | 200 } |
190 } | 201 } |
191 | 202 |
| 203 void AutocompleteController::StartZerosuggest( |
| 204 const GURL& url, |
| 205 const string16& user_text) { |
| 206 if (zerosuggest_provider_ != NULL) |
| 207 zerosuggest_provider_->StartZerosuggest(url, user_text); |
| 208 } |
| 209 |
| 210 void AutocompleteController::StopZerosuggest() { |
| 211 if (zerosuggest_provider_ != NULL) |
| 212 zerosuggest_provider_->Stop(false); |
| 213 } |
| 214 |
192 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) { | 215 void AutocompleteController::DeleteMatch(const AutocompleteMatch& match) { |
193 DCHECK(match.deletable); | 216 DCHECK(match.deletable); |
194 match.provider->DeleteMatch(match); // This may synchronously call back to | 217 match.provider->DeleteMatch(match); // This may synchronously call back to |
195 // OnProviderUpdate(). | 218 // OnProviderUpdate(). |
196 // If DeleteMatch resulted in a callback to OnProviderUpdate and we're | 219 // If DeleteMatch resulted in a callback to OnProviderUpdate and we're |
197 // not done, we might attempt to redisplay the deleted match. Make sure | 220 // not done, we might attempt to redisplay the deleted match. Make sure |
198 // we aren't displaying it by removing any old entries. | 221 // we aren't displaying it by removing any old entries. |
199 ExpireCopiedEntries(); | 222 ExpireCopiedEntries(); |
200 } | 223 } |
201 | 224 |
(...skipping 198 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
400 } | 423 } |
401 done_ = true; | 424 done_ = true; |
402 } | 425 } |
403 | 426 |
404 void AutocompleteController::StartExpireTimer() { | 427 void AutocompleteController::StartExpireTimer() { |
405 if (result_.HasCopiedMatches()) | 428 if (result_.HasCopiedMatches()) |
406 expire_timer_.Start(FROM_HERE, | 429 expire_timer_.Start(FROM_HERE, |
407 base::TimeDelta::FromMilliseconds(kExpireTimeMS), | 430 base::TimeDelta::FromMilliseconds(kExpireTimeMS), |
408 this, &AutocompleteController::ExpireCopiedEntries); | 431 this, &AutocompleteController::ExpireCopiedEntries); |
409 } | 432 } |
OLD | NEW |