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.
5 #include "chrome/browser/omnibox_search_hint.h"
8 #include "base/command_line.h"
9 #include "base/memory/weak_ptr.h"
10 #include "base/message_loop.h"
11 #include "base/metrics/histogram.h"
12 #include "chrome/browser/autocomplete/autocomplete.h"
13 #include "chrome/browser/autocomplete/autocomplete_edit.h"
14 #include "chrome/browser/autocomplete/autocomplete_match.h"
15 #include "chrome/browser/infobars/infobar_tab_helper.h"
16 #include "chrome/browser/prefs/pref_service.h"
17 #include "chrome/browser/profiles/profile.h"
18 #include "chrome/browser/search_engines/template_url.h"
19 #include "chrome/browser/search_engines/template_url_service.h"
20 #include "chrome/browser/search_engines/template_url_service_factory.h"
21 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h"
22 #include "chrome/browser/ui/browser_list.h"
23 #include "chrome/browser/ui/browser_window.h"
24 #include "chrome/browser/ui/omnibox/location_bar.h"
25 #include "chrome/browser/ui/omnibox/omnibox_view.h"
26 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h"
27 #include "chrome/common/chrome_notification_types.h"
28 #include "chrome/common/chrome_switches.h"
29 #include "chrome/common/pref_names.h"
30 #include "content/public/browser/navigation_details.h"
31 #include "content/public/browser/navigation_entry.h"
32 #include "content/public/browser/notification_details.h"
33 #include "content/public/browser/notification_source.h"
34 #include "content/public/browser/notification_types.h"
35 #include "content/public/browser/web_contents.h"
36 #include "grit/generated_resources.h"
37 #include "grit/theme_resources_standard.h"
38 #include "ui/base/l10n/l10n_util.h"
39 #include "ui/base/resource/resource_bundle.h"
41 using content::NavigationController
;
42 using content::NavigationEntry
;
44 // The URLs of search engines for which we want to trigger the infobar.
45 const char* const kSearchEngineURLs
[] = {
46 "http://www.google.com/",
47 "http://www.yahoo.com/",
48 "http://www.bing.com/",
49 "http://www.altavista.com/",
50 "http://www.ask.com/",
51 "http://www.wolframalpha.com/",
55 // HintInfoBar ----------------------------------------------------------------
57 class HintInfoBar
: public ConfirmInfoBarDelegate
{
59 explicit HintInfoBar(OmniboxSearchHint
* omnibox_hint
);
62 virtual ~HintInfoBar();
64 void AllowExpiry() { should_expire_
= true; }
66 // ConfirmInfoBarDelegate:
67 virtual bool ShouldExpire(
68 const content::LoadCommittedDetails
& details
) const OVERRIDE
;
69 virtual void InfoBarDismissed() OVERRIDE
;
70 virtual gfx::Image
* GetIcon() const OVERRIDE
;
71 virtual Type
GetInfoBarType() const OVERRIDE
;
72 virtual string16
GetMessageText() const OVERRIDE
;
73 virtual int GetButtons() const OVERRIDE
;
74 virtual string16
GetButtonLabel(InfoBarButton button
) const OVERRIDE
;
75 virtual bool Accept() OVERRIDE
;
77 // The omnibox hint that shows us.
78 OmniboxSearchHint
* omnibox_hint_
;
80 // Whether the user clicked one of the buttons.
83 // Whether the info-bar should be dismissed on the next navigation.
86 // Used to delay the expiration of the info-bar.
87 base::WeakPtrFactory
<HintInfoBar
> weak_factory_
;
89 DISALLOW_COPY_AND_ASSIGN(HintInfoBar
);
92 HintInfoBar::HintInfoBar(OmniboxSearchHint
* omnibox_hint
)
93 : ConfirmInfoBarDelegate(omnibox_hint
->tab()->infobar_tab_helper()),
94 omnibox_hint_(omnibox_hint
),
96 should_expire_(false),
97 ALLOW_THIS_IN_INITIALIZER_LIST(weak_factory_(this)) {
98 // We want the info-bar to stick-around for few seconds and then be hidden
99 // on the next navigation after that.
100 MessageLoop::current()->PostDelayedTask(
102 base::Bind(&HintInfoBar::AllowExpiry
, weak_factory_
.GetWeakPtr()),
103 base::TimeDelta::FromSeconds(8));
106 HintInfoBar::~HintInfoBar() {
108 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Ignored", 1);
111 bool HintInfoBar::ShouldExpire(
112 const content::LoadCommittedDetails
& details
) const {
113 return details
.is_navigation_to_different_page() && should_expire_
;
116 void HintInfoBar::InfoBarDismissed() {
117 action_taken_
= true;
118 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Closed", 1);
119 // User closed the infobar, let's not bug him again with this in the future.
120 omnibox_hint_
->DisableHint();
123 gfx::Image
* HintInfoBar::GetIcon() const {
124 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed(
125 IDR_INFOBAR_QUESTION_MARK
);
128 InfoBarDelegate::Type
HintInfoBar::GetInfoBarType() const {
129 return PAGE_ACTION_TYPE
;
132 string16
HintInfoBar::GetMessageText() const {
133 return l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_TEXT
);
136 int HintInfoBar::GetButtons() const {
140 string16
HintInfoBar::GetButtonLabel(InfoBarButton button
) const {
141 DCHECK_EQ(BUTTON_OK
, button
);
142 return l10n_util::GetStringUTF16(
143 IDS_OMNIBOX_SEARCH_HINT_INFOBAR_BUTTON_LABEL
);
146 bool HintInfoBar::Accept() {
147 action_taken_
= true;
148 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.ShowMe", 1);
149 omnibox_hint_
->DisableHint();
150 omnibox_hint_
->ShowEnteringQuery();
155 // OmniboxSearchHint ----------------------------------------------------------
157 OmniboxSearchHint::OmniboxSearchHint(TabContentsWrapper
* tab
) : tab_(tab
) {
158 NavigationController
* controller
= &(tab
->web_contents()->GetController());
159 notification_registrar_
.Add(
161 content::NOTIFICATION_NAV_ENTRY_COMMITTED
,
162 content::Source
<NavigationController
>(controller
));
163 // Fill the search_engine_urls_ map, used for faster look-up (overkill?).
164 for (size_t i
= 0; i
< arraysize(kSearchEngineURLs
); ++i
)
165 search_engine_urls_
[kSearchEngineURLs
[i
]] = 1;
167 // Listen for omnibox to figure-out when the user searches from the omnibox.
168 notification_registrar_
.Add(this,
169 chrome::NOTIFICATION_OMNIBOX_OPENED_URL
,
170 content::Source
<Profile
>(tab
->profile()));
173 OmniboxSearchHint::~OmniboxSearchHint() {
176 void OmniboxSearchHint::Observe(int type
,
177 const content::NotificationSource
& source
,
178 const content::NotificationDetails
& details
) {
179 if (type
== content::NOTIFICATION_NAV_ENTRY_COMMITTED
) {
180 content::NavigationEntry
* entry
=
181 tab_
->web_contents()->GetController().GetActiveEntry();
182 if (search_engine_urls_
.find(entry
->GetURL().spec()) ==
183 search_engine_urls_
.end()) {
184 // The search engine is not in our white-list, bail.
187 const TemplateURL
* const default_provider
=
188 TemplateURLServiceFactory::GetForProfile(tab_
->profile())->
189 GetDefaultSearchProvider();
190 if (!default_provider
)
193 const TemplateURLRef
* const search_url
= default_provider
->url();
194 if (search_url
->GetHost() == entry
->GetURL().host())
196 } else if (type
== chrome::NOTIFICATION_OMNIBOX_OPENED_URL
) {
197 AutocompleteLog
* log
= content::Details
<AutocompleteLog
>(details
).ptr();
198 AutocompleteMatch::Type type
=
199 log
->result
.match_at(log
->selected_index
).type
;
200 if (type
== AutocompleteMatch::SEARCH_WHAT_YOU_TYPED
||
201 type
== AutocompleteMatch::SEARCH_HISTORY
||
202 type
== AutocompleteMatch::SEARCH_SUGGEST
) {
203 // The user performed a search from the omnibox, don't show the infobar
210 void OmniboxSearchHint::ShowInfoBar() {
211 tab_
->infobar_tab_helper()->AddInfoBar(new HintInfoBar(this));
214 void OmniboxSearchHint::ShowEnteringQuery() {
215 LocationBar
* location_bar
= BrowserList::GetLastActiveWithProfile(
216 tab_
->profile())->window()->GetLocationBar();
217 OmniboxView
* omnibox_view
= location_bar
->location_entry();
218 location_bar
->FocusLocation(true);
219 omnibox_view
->SetUserText(
220 l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_OMNIBOX_TEXT
));
221 omnibox_view
->SelectAll(false);
222 // Entering text in the omnibox view triggers the suggestion popup that we
223 // don't want to show in this case.
224 omnibox_view
->ClosePopup();
227 void OmniboxSearchHint::DisableHint() {
228 // The NAV_ENTRY_COMMITTED notification was needed to show the infobar, the
229 // OMNIBOX_OPENED_URL notification was there to set the kShowOmniboxSearchHint
230 // prefs to false, none of them are needed anymore.
231 notification_registrar_
.RemoveAll();
232 tab_
->profile()->GetPrefs()->SetBoolean(prefs::kShowOmniboxSearchHint
,
237 bool OmniboxSearchHint::IsEnabled(Profile
* profile
) {
238 // The infobar can only be shown if the correct switch has been provided and
239 // the user did not dismiss the infobar before.
240 return profile
->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint
) &&
241 CommandLine::ForCurrentProcess()->HasSwitch(
242 switches::kSearchInOmniboxHint
);