OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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/omnibox_search_hint.h" | 5 #include "chrome/browser/omnibox_search_hint.h" |
6 | 6 |
7 #include "base/command_line.h" | 7 #include "base/command_line.h" |
8 #include "base/metrics/histogram.h" | 8 #include "base/metrics/histogram.h" |
9 #include "base/task.h" | 9 #include "base/task.h" |
10 // TODO(avi): remove when conversions not needed any more | 10 // TODO(avi): remove when conversions not needed any more |
11 #include "base/utf_string_conversions.h" | 11 #include "base/utf_string_conversions.h" |
12 #include "chrome/browser/autocomplete/autocomplete.h" | 12 #include "chrome/browser/autocomplete/autocomplete.h" |
13 #include "chrome/browser/autocomplete/autocomplete_edit.h" | 13 #include "chrome/browser/autocomplete/autocomplete_edit.h" |
14 #include "chrome/browser/autocomplete/autocomplete_match.h" | 14 #include "chrome/browser/autocomplete/autocomplete_match.h" |
15 #include "chrome/browser/prefs/pref_service.h" | 15 #include "chrome/browser/prefs/pref_service.h" |
16 #include "chrome/browser/profiles/profile.h" | 16 #include "chrome/browser/profiles/profile.h" |
17 #include "chrome/browser/search_engines/template_url.h" | 17 #include "chrome/browser/search_engines/template_url.h" |
18 #include "chrome/browser/search_engines/template_url_service.h" | 18 #include "chrome/browser/search_engines/template_url_service.h" |
19 #include "chrome/browser/search_engines/template_url_service_factory.h" | 19 #include "chrome/browser/search_engines/template_url_service_factory.h" |
20 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | 20 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" |
21 #include "chrome/browser/ui/browser_list.h" | 21 #include "chrome/browser/ui/browser_list.h" |
22 #include "chrome/browser/ui/browser_window.h" | 22 #include "chrome/browser/ui/browser_window.h" |
23 #include "chrome/browser/ui/omnibox/location_bar.h" | 23 #include "chrome/browser/ui/omnibox/location_bar.h" |
24 #include "chrome/browser/ui/omnibox/omnibox_view.h" | 24 #include "chrome/browser/ui/omnibox/omnibox_view.h" |
25 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | 25 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" |
| 26 #include "chrome/common/chrome_notification_types.h" |
26 #include "chrome/common/chrome_switches.h" | 27 #include "chrome/common/chrome_switches.h" |
27 #include "chrome/common/pref_names.h" | 28 #include "chrome/common/pref_names.h" |
28 #include "content/browser/tab_contents/navigation_details.h" | 29 #include "content/browser/tab_contents/navigation_details.h" |
| 30 #include "content/common/content_notification_types.h" |
29 #include "content/common/notification_details.h" | 31 #include "content/common/notification_details.h" |
30 #include "content/common/notification_source.h" | 32 #include "content/common/notification_source.h" |
31 #include "content/common/notification_type.h" | |
32 #include "grit/generated_resources.h" | 33 #include "grit/generated_resources.h" |
33 #include "grit/theme_resources_standard.h" | 34 #include "grit/theme_resources_standard.h" |
34 #include "ui/base/l10n/l10n_util.h" | 35 #include "ui/base/l10n/l10n_util.h" |
35 #include "ui/base/resource/resource_bundle.h" | 36 #include "ui/base/resource/resource_bundle.h" |
36 | 37 |
37 // The URLs of search engines for which we want to trigger the infobar. | 38 // The URLs of search engines for which we want to trigger the infobar. |
38 const char* kSearchEngineURLs[] = { | 39 const char* kSearchEngineURLs[] = { |
39 "http://www.google.com/", | 40 "http://www.google.com/", |
40 "http://www.yahoo.com/", | 41 "http://www.yahoo.com/", |
41 "http://www.bing.com/", | 42 "http://www.bing.com/", |
(...skipping 100 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
142 omnibox_hint_->ShowEnteringQuery(); | 143 omnibox_hint_->ShowEnteringQuery(); |
143 return true; | 144 return true; |
144 } | 145 } |
145 | 146 |
146 | 147 |
147 // OmniboxSearchHint ---------------------------------------------------------- | 148 // OmniboxSearchHint ---------------------------------------------------------- |
148 | 149 |
149 OmniboxSearchHint::OmniboxSearchHint(TabContentsWrapper* tab) : tab_(tab) { | 150 OmniboxSearchHint::OmniboxSearchHint(TabContentsWrapper* tab) : tab_(tab) { |
150 NavigationController* controller = &(tab->controller()); | 151 NavigationController* controller = &(tab->controller()); |
151 notification_registrar_.Add(this, | 152 notification_registrar_.Add(this, |
152 NotificationType::NAV_ENTRY_COMMITTED, | 153 content::NOTIFICATION_NAV_ENTRY_COMMITTED, |
153 Source<NavigationController>(controller)); | 154 Source<NavigationController>(controller)); |
154 // Fill the search_engine_urls_ map, used for faster look-up (overkill?). | 155 // Fill the search_engine_urls_ map, used for faster look-up (overkill?). |
155 for (size_t i = 0; | 156 for (size_t i = 0; |
156 i < sizeof(kSearchEngineURLs) / sizeof(kSearchEngineURLs[0]); ++i) { | 157 i < sizeof(kSearchEngineURLs) / sizeof(kSearchEngineURLs[0]); ++i) { |
157 search_engine_urls_[kSearchEngineURLs[i]] = 1; | 158 search_engine_urls_[kSearchEngineURLs[i]] = 1; |
158 } | 159 } |
159 | 160 |
160 // Listen for omnibox to figure-out when the user searches from the omnibox. | 161 // Listen for omnibox to figure-out when the user searches from the omnibox. |
161 notification_registrar_.Add(this, | 162 notification_registrar_.Add(this, |
162 NotificationType::OMNIBOX_OPENED_URL, | 163 chrome::NOTIFICATION_OMNIBOX_OPENED_URL, |
163 Source<Profile>(tab->profile())); | 164 Source<Profile>(tab->profile())); |
164 } | 165 } |
165 | 166 |
166 OmniboxSearchHint::~OmniboxSearchHint() { | 167 OmniboxSearchHint::~OmniboxSearchHint() { |
167 } | 168 } |
168 | 169 |
169 void OmniboxSearchHint::Observe(NotificationType type, | 170 void OmniboxSearchHint::Observe(int type, |
170 const NotificationSource& source, | 171 const NotificationSource& source, |
171 const NotificationDetails& details) { | 172 const NotificationDetails& details) { |
172 if (type == NotificationType::NAV_ENTRY_COMMITTED) { | 173 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) { |
173 NavigationEntry* entry = tab_->controller().GetActiveEntry(); | 174 NavigationEntry* entry = tab_->controller().GetActiveEntry(); |
174 if (search_engine_urls_.find(entry->url().spec()) == | 175 if (search_engine_urls_.find(entry->url().spec()) == |
175 search_engine_urls_.end()) { | 176 search_engine_urls_.end()) { |
176 // The search engine is not in our white-list, bail. | 177 // The search engine is not in our white-list, bail. |
177 return; | 178 return; |
178 } | 179 } |
179 const TemplateURL* const default_provider = | 180 const TemplateURL* const default_provider = |
180 TemplateURLServiceFactory::GetForProfile(tab_->profile())-> | 181 TemplateURLServiceFactory::GetForProfile(tab_->profile())-> |
181 GetDefaultSearchProvider(); | 182 GetDefaultSearchProvider(); |
182 if (!default_provider) | 183 if (!default_provider) |
183 return; | 184 return; |
184 | 185 |
185 const TemplateURLRef* const search_url = default_provider->url(); | 186 const TemplateURLRef* const search_url = default_provider->url(); |
186 if (search_url->GetHost() == entry->url().host()) | 187 if (search_url->GetHost() == entry->url().host()) |
187 ShowInfoBar(); | 188 ShowInfoBar(); |
188 } else if (type == NotificationType::OMNIBOX_OPENED_URL) { | 189 } else if (type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL) { |
189 AutocompleteLog* log = Details<AutocompleteLog>(details).ptr(); | 190 AutocompleteLog* log = Details<AutocompleteLog>(details).ptr(); |
190 AutocompleteMatch::Type type = | 191 AutocompleteMatch::Type type = |
191 log->result.match_at(log->selected_index).type; | 192 log->result.match_at(log->selected_index).type; |
192 if (type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || | 193 if (type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || |
193 type == AutocompleteMatch::SEARCH_HISTORY || | 194 type == AutocompleteMatch::SEARCH_HISTORY || |
194 type == AutocompleteMatch::SEARCH_SUGGEST) { | 195 type == AutocompleteMatch::SEARCH_SUGGEST) { |
195 // The user performed a search from the omnibox, don't show the infobar | 196 // The user performed a search from the omnibox, don't show the infobar |
196 // again. | 197 // again. |
197 DisableHint(); | 198 DisableHint(); |
198 } | 199 } |
(...skipping 27 matching lines...) Expand all Loading... |
226 } | 227 } |
227 | 228 |
228 // static | 229 // static |
229 bool OmniboxSearchHint::IsEnabled(Profile* profile) { | 230 bool OmniboxSearchHint::IsEnabled(Profile* profile) { |
230 // The infobar can only be shown if the correct switch has been provided and | 231 // The infobar can only be shown if the correct switch has been provided and |
231 // the user did not dismiss the infobar before. | 232 // the user did not dismiss the infobar before. |
232 return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) && | 233 return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) && |
233 CommandLine::ForCurrentProcess()->HasSwitch( | 234 CommandLine::ForCurrentProcess()->HasSwitch( |
234 switches::kSearchInOmniboxHint); | 235 switches::kSearchInOmniboxHint); |
235 } | 236 } |
OLD | NEW |