| OLD | NEW |
| 1 // Copyright (c) 2010 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 "app/l10n_util.h" | 7 #include "app/l10n_util.h" |
| 8 #include "app/resource_bundle.h" | 8 #include "app/resource_bundle.h" |
| 9 #include "base/command_line.h" | 9 #include "base/command_line.h" |
| 10 #include "base/metrics/histogram.h" | 10 #include "base/metrics/histogram.h" |
| 11 #include "base/task.h" | 11 #include "base/task.h" |
| (...skipping 23 matching lines...) Expand all Loading... |
| 35 // The URLs of search engines for which we want to trigger the infobar. | 35 // The URLs of search engines for which we want to trigger the infobar. |
| 36 const char* kSearchEngineURLs[] = { | 36 const char* kSearchEngineURLs[] = { |
| 37 "http://www.google.com/", | 37 "http://www.google.com/", |
| 38 "http://www.yahoo.com/", | 38 "http://www.yahoo.com/", |
| 39 "http://www.bing.com/", | 39 "http://www.bing.com/", |
| 40 "http://www.altavista.com/", | 40 "http://www.altavista.com/", |
| 41 "http://www.ask.com/", | 41 "http://www.ask.com/", |
| 42 "http://www.wolframalpha.com/", | 42 "http://www.wolframalpha.com/", |
| 43 }; | 43 }; |
| 44 | 44 |
| 45 |
| 46 // HintInfoBar ---------------------------------------------------------------- |
| 47 |
| 45 class HintInfoBar : public ConfirmInfoBarDelegate { | 48 class HintInfoBar : public ConfirmInfoBarDelegate { |
| 46 public: | 49 public: |
| 47 explicit HintInfoBar(OmniboxSearchHint* omnibox_hint) | 50 explicit HintInfoBar(OmniboxSearchHint* omnibox_hint); |
| 48 : ConfirmInfoBarDelegate(omnibox_hint->tab()), | |
| 49 omnibox_hint_(omnibox_hint), | |
| 50 action_taken_(false), | |
| 51 should_expire_(false), | |
| 52 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { | |
| 53 // We want the info-bar to stick-around for few seconds and then be hidden | |
| 54 // on the next navigation after that. | |
| 55 MessageLoop::current()->PostDelayedTask(FROM_HERE, | |
| 56 method_factory_.NewRunnableMethod(&HintInfoBar::Expire), | |
| 57 8000); // 8 seconds. | |
| 58 } | |
| 59 | |
| 60 virtual bool ShouldExpire( | |
| 61 const NavigationController::LoadCommittedDetails& details) const { | |
| 62 return should_expire_; | |
| 63 } | |
| 64 | |
| 65 // Overridden from ConfirmInfoBarDelegate: | |
| 66 virtual void InfoBarClosed() { | |
| 67 if (!action_taken_) | |
| 68 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Ignored", 1); | |
| 69 delete this; | |
| 70 } | |
| 71 | |
| 72 virtual void InfoBarDismissed() { | |
| 73 action_taken_ = true; | |
| 74 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Closed", 1); | |
| 75 // User closed the infobar, let's not bug him again with this in the future. | |
| 76 omnibox_hint_->DisableHint(); | |
| 77 } | |
| 78 | |
| 79 virtual string16 GetMessageText() const { | |
| 80 return l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_TEXT); | |
| 81 } | |
| 82 | |
| 83 virtual SkBitmap* GetIcon() const { | |
| 84 return ResourceBundle::GetSharedInstance().GetBitmapNamed( | |
| 85 IDR_INFOBAR_QUESTION_MARK); | |
| 86 } | |
| 87 | |
| 88 virtual int GetButtons() const { | |
| 89 return BUTTON_OK; | |
| 90 } | |
| 91 | |
| 92 virtual string16 GetButtonLabel(InfoBarButton button) const { | |
| 93 return l10n_util::GetStringUTF16( | |
| 94 IDS_OMNIBOX_SEARCH_HINT_INFOBAR_BUTTON_LABEL); | |
| 95 } | |
| 96 | |
| 97 virtual Type GetInfoBarType() { return PAGE_ACTION_TYPE; } | |
| 98 | |
| 99 virtual bool Accept() { | |
| 100 action_taken_ = true; | |
| 101 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.ShowMe", 1); | |
| 102 omnibox_hint_->DisableHint(); | |
| 103 omnibox_hint_->ShowEnteringQuery(); | |
| 104 return true; | |
| 105 } | |
| 106 | |
| 107 void Expire() { | |
| 108 should_expire_ = true; | |
| 109 } | |
| 110 | 51 |
| 111 private: | 52 private: |
| 53 virtual ~HintInfoBar(); |
| 54 |
| 55 void AllowExpiry() { should_expire_ = true; } |
| 56 |
| 57 // ConfirmInfoBarDelegate: |
| 58 virtual bool ShouldExpire( |
| 59 const NavigationController::LoadCommittedDetails& details) const; |
| 60 virtual void InfoBarDismissed(); |
| 61 virtual void InfoBarClosed(); |
| 62 virtual SkBitmap* GetIcon() const; |
| 63 virtual Type GetInfoBarType() const; |
| 64 virtual string16 GetMessageText() const; |
| 65 virtual int GetButtons() const; |
| 66 virtual string16 GetButtonLabel(InfoBarButton button) const; |
| 67 virtual bool Accept(); |
| 68 |
| 112 // The omnibox hint that shows us. | 69 // The omnibox hint that shows us. |
| 113 OmniboxSearchHint* omnibox_hint_; | 70 OmniboxSearchHint* omnibox_hint_; |
| 114 | 71 |
| 115 // Whether the user clicked one of the buttons. | 72 // Whether the user clicked one of the buttons. |
| 116 bool action_taken_; | 73 bool action_taken_; |
| 117 | 74 |
| 118 // Whether the info-bar should be dismissed on the next navigation. | 75 // Whether the info-bar should be dismissed on the next navigation. |
| 119 bool should_expire_; | 76 bool should_expire_; |
| 120 | 77 |
| 121 // Used to delay the expiration of the info-bar. | 78 // Used to delay the expiration of the info-bar. |
| 122 ScopedRunnableMethodFactory<HintInfoBar> method_factory_; | 79 ScopedRunnableMethodFactory<HintInfoBar> method_factory_; |
| 123 | 80 |
| 124 DISALLOW_COPY_AND_ASSIGN(HintInfoBar); | 81 DISALLOW_COPY_AND_ASSIGN(HintInfoBar); |
| 125 }; | 82 }; |
| 126 | 83 |
| 84 HintInfoBar::HintInfoBar(OmniboxSearchHint* omnibox_hint) |
| 85 : ConfirmInfoBarDelegate(omnibox_hint->tab()), |
| 86 omnibox_hint_(omnibox_hint), |
| 87 action_taken_(false), |
| 88 should_expire_(false), |
| 89 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 90 // We want the info-bar to stick-around for few seconds and then be hidden |
| 91 // on the next navigation after that. |
| 92 MessageLoop::current()->PostDelayedTask(FROM_HERE, |
| 93 method_factory_.NewRunnableMethod(&HintInfoBar::AllowExpiry), |
| 94 8000); // 8 seconds. |
| 95 } |
| 96 HintInfoBar::~HintInfoBar() { |
| 97 } |
| 98 |
| 99 bool HintInfoBar::ShouldExpire( |
| 100 const NavigationController::LoadCommittedDetails& details) const { |
| 101 return should_expire_; |
| 102 } |
| 103 |
| 104 void HintInfoBar::InfoBarDismissed() { |
| 105 action_taken_ = true; |
| 106 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Closed", 1); |
| 107 // User closed the infobar, let's not bug him again with this in the future. |
| 108 omnibox_hint_->DisableHint(); |
| 109 } |
| 110 |
| 111 void HintInfoBar::InfoBarClosed() { |
| 112 if (!action_taken_) |
| 113 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.Ignored", 1); |
| 114 delete this; |
| 115 } |
| 116 |
| 117 SkBitmap* HintInfoBar::GetIcon() const { |
| 118 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 119 IDR_INFOBAR_QUESTION_MARK); |
| 120 } |
| 121 |
| 122 InfoBarDelegate::Type HintInfoBar::GetInfoBarType() const { |
| 123 return PAGE_ACTION_TYPE; |
| 124 } |
| 125 |
| 126 string16 HintInfoBar::GetMessageText() const { |
| 127 return l10n_util::GetStringUTF16(IDS_OMNIBOX_SEARCH_HINT_INFOBAR_TEXT); |
| 128 } |
| 129 |
| 130 int HintInfoBar::GetButtons() const { |
| 131 return BUTTON_OK; |
| 132 } |
| 133 |
| 134 string16 HintInfoBar::GetButtonLabel(InfoBarButton button) const { |
| 135 return l10n_util::GetStringUTF16( |
| 136 IDS_OMNIBOX_SEARCH_HINT_INFOBAR_BUTTON_LABEL); |
| 137 } |
| 138 |
| 139 bool HintInfoBar::Accept() { |
| 140 action_taken_ = true; |
| 141 UMA_HISTOGRAM_COUNTS("OmniboxSearchHint.ShowMe", 1); |
| 142 omnibox_hint_->DisableHint(); |
| 143 omnibox_hint_->ShowEnteringQuery(); |
| 144 return true; |
| 145 } |
| 146 |
| 147 |
| 148 // OmniboxSearchHint ---------------------------------------------------------- |
| 149 |
| 127 OmniboxSearchHint::OmniboxSearchHint(TabContents* tab) : tab_(tab) { | 150 OmniboxSearchHint::OmniboxSearchHint(TabContents* tab) : tab_(tab) { |
| 128 NavigationController* controller = &(tab->controller()); | 151 NavigationController* controller = &(tab->controller()); |
| 129 notification_registrar_.Add(this, | 152 notification_registrar_.Add(this, |
| 130 NotificationType::NAV_ENTRY_COMMITTED, | 153 NotificationType::NAV_ENTRY_COMMITTED, |
| 131 Source<NavigationController>(controller)); | 154 Source<NavigationController>(controller)); |
| 132 // 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?). |
| 133 for (size_t i = 0; | 156 for (size_t i = 0; |
| 134 i < sizeof(kSearchEngineURLs) / sizeof(kSearchEngineURLs[0]); ++i) { | 157 i < sizeof(kSearchEngineURLs) / sizeof(kSearchEngineURLs[0]); ++i) { |
| 135 search_engine_urls_[kSearchEngineURLs[i]] = 1; | 158 search_engine_urls_[kSearchEngineURLs[i]] = 1; |
| 136 } | 159 } |
| (...skipping 66 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 203 } | 226 } |
| 204 | 227 |
| 205 // static | 228 // static |
| 206 bool OmniboxSearchHint::IsEnabled(Profile* profile) { | 229 bool OmniboxSearchHint::IsEnabled(Profile* profile) { |
| 207 // The infobar can only be shown if the correct switch has been provided and | 230 // The infobar can only be shown if the correct switch has been provided and |
| 208 // the user did not dismiss the infobar before. | 231 // the user did not dismiss the infobar before. |
| 209 return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) && | 232 return profile->GetPrefs()->GetBoolean(prefs::kShowOmniboxSearchHint) && |
| 210 CommandLine::ForCurrentProcess()->HasSwitch( | 233 CommandLine::ForCurrentProcess()->HasSwitch( |
| 211 switches::kSearchInOmniboxHint); | 234 switches::kSearchInOmniboxHint); |
| 212 } | 235 } |
| OLD | NEW |