Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(574)

Side by Side Diff: chrome/browser/omnibox_search_hint.cc

Issue 15822005: Remove omnibox_search_hint.(cc,h). (Closed) Base URL: http://git.chromium.org/chromium/src.git@master
Patch Set: sync+rebase Created 7 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
« no previous file with comments | « chrome/browser/omnibox_search_hint.h ('k') | chrome/browser/ui/browser_tab_contents.cc » ('j') | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
OLDNEW
(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 #include "chrome/browser/omnibox_search_hint.h"
6
7 #include "base/bind.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 "base/prefs/pref_service.h"
13 #include "chrome/browser/autocomplete/autocomplete_match.h"
14 #include "chrome/browser/autocomplete/autocomplete_result.h"
15 #include "chrome/browser/infobars/confirm_infobar_delegate.h"
16 #include "chrome/browser/infobars/infobar_service.h"
17 #include "chrome/browser/omnibox/omnibox_log.h"
18 #include "chrome/browser/profiles/profile.h"
19 #include "chrome/browser/search_engines/template_url.h"
20 #include "chrome/browser/search_engines/template_url_service.h"
21 #include "chrome/browser/search_engines/template_url_service_factory.h"
22 #include "chrome/browser/ui/browser.h"
23 #include "chrome/browser/ui/browser_finder.h"
24 #include "chrome/browser/ui/browser_window.h"
25 #include "chrome/browser/ui/omnibox/location_bar.h"
26 #include "chrome/browser/ui/omnibox/omnibox_view.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.h"
38 #include "ui/base/l10n/l10n_util.h"
39
40 using content::NavigationController;
41 using content::NavigationEntry;
42
43
44 // HintInfoBarDelegate ---------------------------------------------------------
45
46 class HintInfoBarDelegate : public ConfirmInfoBarDelegate {
47 public:
48 // If the active entry for |web_contents| is a navigation to the user's
49 // default search engine, and the engine is on a small whitelist, creates a
50 // "you can search from the omnibox" hint delegate and adds it to the
51 // InfoBarService for |web_contents|.
52 static void Create(content::WebContents* web_contents,
53 OmniboxSearchHint* omnibox_hint);
54
55 private:
56 HintInfoBarDelegate(OmniboxSearchHint* omnibox_hint,
57 InfoBarService* infobar_service);
58 virtual ~HintInfoBarDelegate();
59
60 void AllowExpiry() { should_expire_ = true; }
61
62 // ConfirmInfoBarDelegate:
63 virtual void InfoBarDismissed() OVERRIDE;
64 virtual int GetIconID() const OVERRIDE;
65 virtual Type GetInfoBarType() const OVERRIDE;
66 virtual string16 GetMessageText() const OVERRIDE;
67 virtual int GetButtons() const OVERRIDE;
68 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
69 virtual bool Accept() OVERRIDE;
70 virtual bool ShouldExpireInternal(
71 const content::LoadCommittedDetails& details) const OVERRIDE;
72
73 // The omnibox hint that shows us.
74 OmniboxSearchHint* omnibox_hint_;
75
76 // Whether the user clicked one of the buttons.
77 bool action_taken_;
78
79 // Whether the info-bar should be dismissed on the next navigation.
80 bool should_expire_;
81
82 // Used to delay the expiration of the info-bar.
83 base::WeakPtrFactory<HintInfoBarDelegate> weak_factory_;
84
85 DISALLOW_COPY_AND_ASSIGN(HintInfoBarDelegate);
86 };
87
88 // static
89 void HintInfoBarDelegate::Create(content::WebContents* web_contents,
90 OmniboxSearchHint* omnibox_hint) {
91 // The URLs of search engines for which we want to trigger the infobar.
92 const char* const kSearchEngineURLs[] = {
93 "http://www.google.com/",
94 "http://www.yahoo.com/",
95 "http://www.bing.com/",
96 "http://www.altavista.com/",
97 "http://www.ask.com/",
98 "http://www.wolframalpha.com/",
99 };
100 CR_DEFINE_STATIC_LOCAL(std::set<std::string>, search_engine_urls, ());
101 if (search_engine_urls.empty()) {
102 for (size_t i = 0; i < arraysize(kSearchEngineURLs); ++i)
103 search_engine_urls.insert(kSearchEngineURLs[i]);
104 }
105
106 content::NavigationEntry* entry =
107 web_contents->GetController().GetActiveEntry();
108 if (search_engine_urls.find(entry->GetURL().spec()) ==
109 search_engine_urls.end()) {
110 // The search engine is not in our white-list, bail.
111 return;
112 }
113
114 Profile* profile =
115 Profile::FromBrowserContext(web_contents->GetBrowserContext());
116 const TemplateURL* const default_provider =
117 TemplateURLServiceFactory::GetForProfile(profile)->
118 GetDefaultSearchProvider();
119 if (!default_provider)
120 return;
121
122 if (default_provider->url_ref().GetHost() == entry->GetURL().host()) {
123 InfoBarService* infobar_service =
124 InfoBarService::FromWebContents(web_contents);
125 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>(
126 new HintInfoBarDelegate(omnibox_hint, infobar_service)));
127 }
128 }
129
130 HintInfoBarDelegate::HintInfoBarDelegate(OmniboxSearchHint* omnibox_hint,
131 InfoBarService* infobar_service)
132 : ConfirmInfoBarDelegate(infobar_service),
133 omnibox_hint_(omnibox_hint),
134 action_taken_(false),
135 should_expire_(false),
136 weak_factory_(this) {
137 // We want the info-bar to stick-around for few seconds and then be hidden
138 // on the next navigation after that.
139 base::MessageLoop::current()->PostDelayedTask(
140 FROM_HERE,
141 base::Bind(&HintInfoBarDelegate::AllowExpiry, weak_factory_.GetWeakPtr()),
142 base::TimeDelta::FromSeconds(8));
143 }
144
145 HintInfoBarDelegate::~HintInfoBarDelegate() {
146 if (!action_taken_)
147 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Ignored", 1);
148 }
149
150 void HintInfoBarDelegate::InfoBarDismissed() {
151 action_taken_ = true;
152 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Closed", 1);
153 // User closed the infobar, let's not bug him again with this in the future.
154 omnibox_hint_->DisableHint();
155 }
156
157 int HintInfoBarDelegate::GetIconID() const {
158 return IDR_INFOBAR_QUESTION_MARK;
159 }
160
161 InfoBarDelegate::Type HintInfoBarDelegate::GetInfoBarType() const {
162 return PAGE_ACTION_TYPE;
163 }
164
165 string16 HintInfoBarDelegate::GetMessageText() const {
166 return l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_TEXT);
167 }
168
169 int HintInfoBarDelegate::GetButtons() const {
170 return BUTTON_OK;
171 }
172
173 string16 HintInfoBarDelegate::GetButtonLabel(InfoBarButton button) const {
174 DCHECK_EQ(BUTTON_OK, button);
175 return l10n_util::GetStringUTF16(
176 IDS_OMNIBOX_SEARCH_HINT_INFOBAR_BUTTON_LABEL);
177 }
178
179 bool HintInfoBarDelegate::Accept() {
180 action_taken_ = true;
181 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.ShowMe", 1);
182 omnibox_hint_->DisableHint();
183 omnibox_hint_->ShowEnteringQuery();
184 return true;
185 }
186
187 bool HintInfoBarDelegate::ShouldExpireInternal(
188 const content::LoadCommittedDetails& details) const {
189 return should_expire_;
190 }
191
192
193 // OmniboxSearchHint ----------------------------------------------------------
194
195 DEFINE_WEB_CONTENTS_USER_DATA_KEY(OmniboxSearchHint);
196
197 OmniboxSearchHint::OmniboxSearchHint(content::WebContents* web_contents)
198 : web_contents_(web_contents) {
199 NavigationController* controller = &(web_contents->GetController());
200 notification_registrar_.Add(
201 this,
202 content::NOTIFICATION_NAV_ENTRY_COMMITTED,
203 content::Source<NavigationController>(controller));
204
205 Profile* profile =
206 Profile::FromBrowserContext(web_contents->GetBrowserContext());
207 // Listen for omnibox to figure-out when the user searches from the omnibox.
208 notification_registrar_.Add(this,
209 chrome::NOTIFICATION_OMNIBOX_OPENED_URL,
210 content::Source<Profile>(profile));
211 }
212
213 OmniboxSearchHint::~OmniboxSearchHint() {
214 }
215
216 void OmniboxSearchHint::Observe(int type,
217 const content::NotificationSource& source,
218 const content::NotificationDetails& details) {
219 if (type == content::NOTIFICATION_NAV_ENTRY_COMMITTED) {
220 HintInfoBarDelegate::Create(web_contents_, this);
221 } else if (type == chrome::NOTIFICATION_OMNIBOX_OPENED_URL) {
222 OmniboxLog* log = content::Details<OmniboxLog>(details).ptr();
223 AutocompleteMatch::Type type =
224 log->result.match_at(log->selected_index).type;
225 if (AutocompleteMatch::IsSearchType(type)) {
226 // The user performed a search from the omnibox, don't show the infobar
227 // again.
228 DisableHint();
229 }
230 }
231 }
232
233 void OmniboxSearchHint::ShowEnteringQuery() {
234 LocationBar* location_bar = chrome::FindBrowserWithWebContents(
235 web_contents_)->window()->GetLocationBar();
236 OmniboxView* omnibox_view = location_bar->GetLocationEntry();
237 location_bar->FocusLocation(true);
238 omnibox_view->SetUserText(
239 l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_OMNIBOX_TEXT));
240 omnibox_view->SelectAll(false);
241 // Entering text in the omnibox view triggers the suggestion popup that we
242 // don't want to show in this case.
243 omnibox_view->CloseOmniboxPopup();
244 }
245
246 void OmniboxSearchHint::DisableHint() {
247 // The NAV_ENTRY_COMMITTED notification was needed to show the infobar, the
248 // OMNIBOX_OPENED_URL notification was there to set the kShowOmniboxSearchHint
249 // prefs to false, none of them are needed anymore.
250 notification_registrar_.RemoveAll();
251 Profile* profile =
252 Profile::FromBrowserContext(web_contents_->GetBrowserContext());
253 profile->GetPrefs()->SetBoolean(prefs::kShowOmniboxSearchHint, false);
254 }
255
256 // static
257 bool OmniboxSearchHint::IsEnabled(Profile* profile) {
258 // The infobar can only be shown if the correct switch has been provided and
259 // the user did not dismiss the infobar before.
260 return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) &&
261 CommandLine::ForCurrentProcess()->HasSwitch(
262 switches::kSearchInOmniboxHint);
263 }
OLDNEW
« no previous file with comments | « chrome/browser/omnibox_search_hint.h ('k') | chrome/browser/ui/browser_tab_contents.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698