diff options
author | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 20:40:10 +0000 |
---|---|---|
committer | jcampan@chromium.org <jcampan@chromium.org@0039d316-1c4b-4281-b951-d872f2087c98> | 2009-07-23 20:40:10 +0000 |
commit | c8865483c216fbcaede15c4cd45e369643c8639b (patch) | |
tree | 075c9828944f3dff7c6ac6e0475eb878eef1e31c /chrome/browser/omnibox_search_hint.cc | |
parent | 351355117f4cb4ee805961920a7c9d2ad0ae944c (diff) | |
download | chromium_src-c8865483c216fbcaede15c4cd45e369643c8639b.zip chromium_src-c8865483c216fbcaede15c4cd45e369643c8639b.tar.gz chromium_src-c8865483c216fbcaede15c4cd45e369643c8639b.tar.bz2 |
This CL adds an infobar instructing users they can do search directly from the location bar when they navigate to their default search engine.The infobar is dismissed and not shown again if the user does a search from the omnibox or dismiss the infobar.This is part of a UX experiment and is behind a switch.BUG=NoneTEST=Start Chrome with a fresh profile. Navigate to www.google.com. An info bar should be shown. Click the 'show me' button, the location bar should display a message explaining search can be made from it.
Review URL: http://codereview.chromium.org/159242
git-svn-id: svn://svn.chromium.org/chrome/trunk/src@21438 0039d316-1c4b-4281-b951-d872f2087c98
Diffstat (limited to 'chrome/browser/omnibox_search_hint.cc')
-rw-r--r-- | chrome/browser/omnibox_search_hint.cc | 204 |
1 files changed, 204 insertions, 0 deletions
diff --git a/chrome/browser/omnibox_search_hint.cc b/chrome/browser/omnibox_search_hint.cc new file mode 100644 index 0000000..9d36690 --- /dev/null +++ b/chrome/browser/omnibox_search_hint.cc @@ -0,0 +1,204 @@ +// Copyright (c) 2009 The Chromium Authors. All rights reserved. +// Use of this source code is governed by a BSD-style license that can be +// found in the LICENSE file. + +#include "chrome/browser/omnibox_search_hint.h" + +#include "app/animation.h" +#include "app/l10n_util.h" +#include "app/resource_bundle.h" +#include "base/command_line.h" +#include "base/task.h" +#include "chrome/browser/browser_list.h" +#include "chrome/browser/browser_window.h" +#include "chrome/browser/location_bar.h" +#include "chrome/browser/autocomplete/autocomplete_edit_view.h" +#include "chrome/browser/profile.h" +#include "chrome/browser/search_engines/template_url_model.h" +#include "chrome/browser/tab_contents/infobar_delegate.h" +#include "chrome/browser/tab_contents/tab_contents.h" +#include "chrome/common/chrome_switches.h" +#include "chrome/common/notification_service.h" +#include "chrome/common/notification_type.h" +#include "chrome/common/pref_names.h" +#include "grit/generated_resources.h" + +// The URLs of search engines for which we want to trigger the infobar. +const char* kSearchEngineURLs[] = { + "http://www.google.com/", + "http://www.yahoo.com/", + "http://www.bing.com/", + "http://www.altavista.com/", + "http://www.ask.com/", + "http://www.wolframalpha.com/", +}; + +class HintInfoBar : public ConfirmInfoBarDelegate { + public: + explicit HintInfoBar(OmniboxSearchHint* omnibox_hint) + : ConfirmInfoBarDelegate(omnibox_hint->tab()), + omnibox_hint_(omnibox_hint), + action_taken_(false), + should_expire_(false), + ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { + // We want the info-bar to stick-around for few seconds and then be hidden + // on the next navigation after that. + MessageLoop::current()->PostDelayedTask(FROM_HERE, + method_factory_.NewRunnableMethod(&HintInfoBar::Expire), + 8000); // 8 seconds. + } + + virtual bool ShouldExpire( + const NavigationController::LoadCommittedDetails& details) const { + return should_expire_; + } + + // Overridden from ConfirmInfoBarDelegate: + virtual void InfoBarClosed() { + if (!action_taken_) + UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Ignored", 1); + delete this; + } + + virtual void InfoBarDismissed() { + action_taken_ = true; + UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Closed", 1); + // User closed the infobar, let's not bug him again with this in the future. + omnibox_hint_->DisableHint(); + } + + virtual std::wstring GetMessageText() const { + return l10n_util::GetString(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_TEXT); + } + + virtual SkBitmap* GetIcon() const { + return ResourceBundle::GetSharedInstance().GetBitmapNamed( + IDR_INFOBAR_QUESTION_MARK); + } + + virtual int GetButtons() const { + return BUTTON_OK; + } + + virtual std::wstring GetButtonLabel(InfoBarButton button) const { + return l10n_util::GetString(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_BUTTON_LABEL); + } + + virtual Type GetInfoBarType() { + return INFO_TYPE; + } + + virtual bool Accept() { + action_taken_ = true; + UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.ShowMe", 1); + omnibox_hint_->DisableHint(); + omnibox_hint_->ShowEnteringQuery(); + return true; + } + + void Expire() { + should_expire_ = true; + } + + private: + // The omnibox hint that shows us. + OmniboxSearchHint* omnibox_hint_; + + // Whether the user clicked one of the buttons. + bool action_taken_; + + // Whether the info-bar should be dismissed on the next navigation. + bool should_expire_; + + // Used to delay the expiration of the info-bar. + ScopedRunnableMethodFactory<HintInfoBar> method_factory_; + + DISALLOW_COPY_AND_ASSIGN(HintInfoBar); +}; + +OmniboxSearchHint::OmniboxSearchHint(TabContents* tab) : tab_(tab) { + NavigationController* controller = &(tab->controller()); + notification_registrar_.Add(this, + NotificationType::NAV_ENTRY_COMMITTED, + Source<NavigationController>(controller)); + // Fill the search_engine_urls_ map, used for faster look-up (overkill?). + for (size_t i = 0; + i < sizeof(kSearchEngineURLs) / sizeof(kSearchEngineURLs[0]); ++i) { + search_engine_urls_[kSearchEngineURLs[i]] = 1; + } + + // Listen for omnibox to figure-out when the user searches from the omnibox. + notification_registrar_.Add(this, + NotificationType::OMNIBOX_OPENED_URL, + Source<Profile>(tab->profile())); +} + +OmniboxSearchHint::~OmniboxSearchHint() { +} + +void OmniboxSearchHint::Observe(NotificationType type, + const NotificationSource& source, + const NotificationDetails& details) { + if (type == NotificationType::NAV_ENTRY_COMMITTED) { + NavigationEntry* entry = tab_->controller().GetActiveEntry(); + if (search_engine_urls_.find(entry->url().spec()) == + search_engine_urls_.end()) { + // The search engine is not in our white-list, bail. + return; + } + const TemplateURL* const default_provider = + tab_->profile()->GetTemplateURLModel()->GetDefaultSearchProvider(); + if (!default_provider) + return; + + const TemplateURLRef* const search_url = default_provider->url(); + if (search_url->GetHost() == entry->url().host()) + ShowInfoBar(); + } else if (type == NotificationType::OMNIBOX_OPENED_URL) { + AutocompleteLog* log = Details<AutocompleteLog>(details).ptr(); + AutocompleteMatch::Type type = + log->result.match_at(log->selected_index).type; + if (type == AutocompleteMatch::SEARCH_WHAT_YOU_TYPED || + type == AutocompleteMatch::SEARCH_HISTORY || + type == AutocompleteMatch::SEARCH_SUGGEST) { + // The user performed a search from the omnibox, don't show the infobar + // again. + DisableHint(); + } + } +} + +void OmniboxSearchHint::ShowInfoBar() { + tab_->AddInfoBar(new HintInfoBar(this)); +} + +void OmniboxSearchHint::ShowEnteringQuery() { + LocationBar* location_bar = BrowserList::GetLastActive()->window()-> + GetLocationBar(); + AutocompleteEditView* edit_view = location_bar->location_entry(); + location_bar->FocusLocation(); + edit_view->SetUserText( + l10n_util::GetString(IDS_OMNIBOX_SEARCH_HINT_OMNIBOX_TEXT)); + edit_view->SelectAll(false); + // Entering text in the autocomplete edit view triggers the suggestion popup + // that we don't want to show in this case. + edit_view->ClosePopup(); +} + +void OmniboxSearchHint::DisableHint() { + // The NAV_ENTRY_COMMITTED notification was needed to show the infobar, the + // OMNIBOX_OPENED_URL notification was there to set the kShowOmniboxSearchHint + // prefs to false, none of them are needed anymore. + notification_registrar_.RemoveAll(); + tab_->profile()->GetPrefs()->SetBoolean(prefs::kShowOmniboxSearchHint, + false); +} + +// static +bool OmniboxSearchHint::IsEnabled(Profile* profile) { + // The infobar can only be shown if the correct switch has been provided and + // the user did not dismiss the infobar before. + return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) && + CommandLine::ForCurrentProcess()->HasSwitch( + switches::kSearchInOmniboxHint); +} |