| OLD | NEW |
| (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/chromeos/extensions/offers_private_api.h" | |
| 6 | |
| 7 #include <string> | |
| 8 | |
| 9 #include "base/compiler_specific.h" | |
| 10 #include "base/logging.h" | |
| 11 #include "base/memory/scoped_ptr.h" | |
| 12 #include "base/values.h" | |
| 13 #include "chrome/browser/chromeos/system/statistics_provider.h" | |
| 14 #include "chrome/browser/extensions/extension_host.h" | |
| 15 #include "chrome/browser/extensions/extension_process_manager.h" | |
| 16 #include "chrome/browser/extensions/extension_tab_util.h" | |
| 17 #include "chrome/browser/extensions/extension_window_controller.h" | |
| 18 #include "chrome/browser/infobars/infobar_tab_helper.h" | |
| 19 #include "chrome/browser/profiles/profile.h" | |
| 20 #include "chrome/browser/tab_contents/confirm_infobar_delegate.h" | |
| 21 #include "chrome/browser/ui/browser.h" | |
| 22 #include "chrome/browser/ui/browser_list.h" | |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents_wrapper.h" | |
| 24 #include "chrome/common/chrome_notification_types.h" | |
| 25 #include "chrome/common/extensions/extension.h" | |
| 26 #include "content/public/browser/notification_details.h" | |
| 27 #include "content/public/browser/notification_observer.h" | |
| 28 #include "content/public/browser/notification_registrar.h" | |
| 29 #include "content/public/browser/notification_source.h" | |
| 30 #include "content/public/browser/web_contents.h" | |
| 31 #include "grit/generated_resources.h" | |
| 32 #include "ui/base/l10n/l10n_util.h" | |
| 33 | |
| 34 namespace { | |
| 35 | |
| 36 // For a given coupon type, returns the coupon code value from the underlying | |
| 37 // system. | |
| 38 std::string GetValueForCouponType(std::string& type) { | |
| 39 // Possible ECHO code type and corresponding key name in StatisticsProvider. | |
| 40 const std::string kCouponType = "COUPON_CODE"; | |
| 41 const std::string kCouponCodeKey = "ubind_attribute"; | |
| 42 const std::string kGroupType = "GROUP_CODE"; | |
| 43 const std::string kGroupCodeKey = "gbind_attribute"; | |
| 44 | |
| 45 chromeos::system::StatisticsProvider* provider = | |
| 46 chromeos::system::StatisticsProvider::GetInstance(); | |
| 47 std::string result; | |
| 48 if (type == kCouponType) | |
| 49 provider->GetMachineStatistic(kCouponCodeKey, &result); | |
| 50 else if (type == kGroupType) | |
| 51 provider->GetMachineStatistic(kGroupCodeKey, &result); | |
| 52 return result; | |
| 53 } | |
| 54 | |
| 55 } // namespace | |
| 56 | |
| 57 | |
| 58 GetCouponCodeFunction::GetCouponCodeFunction() { | |
| 59 } | |
| 60 | |
| 61 GetCouponCodeFunction::~GetCouponCodeFunction() { | |
| 62 } | |
| 63 | |
| 64 bool GetCouponCodeFunction::RunImpl() { | |
| 65 std::string type; | |
| 66 EXTENSION_FUNCTION_VALIDATE(args_->GetString(0, &type)); | |
| 67 result_.reset(Value::CreateStringValue(GetValueForCouponType(type))); | |
| 68 return true; | |
| 69 } | |
| 70 | |
| 71 // Confirmation InfoBar displayed to ask user for consent for performing | |
| 72 // the ECHO protocol transaction. Used by GetUserConsentFunction. | |
| 73 class OffersConsentInfoBarDelegate | |
| 74 : public ConfirmInfoBarDelegate, | |
| 75 public content::NotificationObserver { | |
| 76 public: | |
| 77 OffersConsentInfoBarDelegate(Browser* browser, | |
| 78 InfoBarTabHelper* infobar_helper, | |
| 79 const GURL& url, | |
| 80 GetUserConsentFunction* consent_receiver); | |
| 81 private: | |
| 82 virtual ~OffersConsentInfoBarDelegate(); | |
| 83 | |
| 84 // ConfirmInfoBarDelegate: | |
| 85 virtual string16 GetMessageText() const OVERRIDE; | |
| 86 virtual int GetButtons() const OVERRIDE; | |
| 87 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; | |
| 88 virtual bool Accept() OVERRIDE; | |
| 89 virtual bool Cancel() OVERRIDE; | |
| 90 virtual void InfoBarDismissed() OVERRIDE; | |
| 91 | |
| 92 // content::NotificationObserver: | |
| 93 virtual void Observe(int type, | |
| 94 const content::NotificationSource& source, | |
| 95 const content::NotificationDetails& details) OVERRIDE; | |
| 96 | |
| 97 content::NotificationRegistrar registrar_; | |
| 98 Browser* browser_; | |
| 99 | |
| 100 // The extension host we are showing the InfoBar for. The delegate needs to | |
| 101 // own this since the InfoBar gets deleted and recreated when you switch tabs | |
| 102 // and come back (and we don't want the user's interaction with the InfoBar to | |
| 103 // get lost at that point). | |
| 104 scoped_ptr<ExtensionHost> extension_host_; | |
| 105 | |
| 106 scoped_refptr<GetUserConsentFunction> consent_receiver_; | |
| 107 }; | |
| 108 | |
| 109 OffersConsentInfoBarDelegate::OffersConsentInfoBarDelegate( | |
| 110 Browser* browser, | |
| 111 InfoBarTabHelper* infobar_helper, | |
| 112 const GURL& url, | |
| 113 GetUserConsentFunction* consent_receiver) | |
| 114 : ConfirmInfoBarDelegate(infobar_helper), | |
| 115 browser_(browser), | |
| 116 consent_receiver_(consent_receiver) { | |
| 117 ExtensionProcessManager* manager = | |
| 118 browser->profile()->GetExtensionProcessManager(); | |
| 119 extension_host_.reset(manager->CreateInfobarHost(url, browser)); | |
| 120 extension_host_->SetAssociatedWebContents(infobar_helper->web_contents()); | |
| 121 | |
| 122 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE, | |
| 123 content::Source<Profile>(browser->profile())); | |
| 124 registrar_.Add(this, chrome::NOTIFICATION_EXTENSION_UNLOADED, | |
| 125 content::Source<Profile>(browser->profile())); | |
| 126 } | |
| 127 | |
| 128 OffersConsentInfoBarDelegate::~OffersConsentInfoBarDelegate() { | |
| 129 } | |
| 130 | |
| 131 string16 OffersConsentInfoBarDelegate::GetMessageText() const { | |
| 132 return l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_LABEL); | |
| 133 } | |
| 134 | |
| 135 int OffersConsentInfoBarDelegate::GetButtons() const { | |
| 136 return BUTTON_OK | BUTTON_CANCEL; | |
| 137 } | |
| 138 | |
| 139 string16 OffersConsentInfoBarDelegate::GetButtonLabel( | |
| 140 InfoBarButton button) const { | |
| 141 DCHECK(button == BUTTON_OK || button == BUTTON_CANCEL); | |
| 142 if (button == BUTTON_OK) | |
| 143 return l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_ENABLE_BUTTON); | |
| 144 return l10n_util::GetStringUTF16(IDS_OFFERS_CONSENT_INFOBAR_DISABLE_BUTTON); | |
| 145 } | |
| 146 | |
| 147 bool OffersConsentInfoBarDelegate::Accept() { | |
| 148 consent_receiver_->SetConsent(true); | |
| 149 return true; | |
| 150 } | |
| 151 | |
| 152 bool OffersConsentInfoBarDelegate::Cancel() { | |
| 153 consent_receiver_->SetConsent(false); | |
| 154 return true; | |
| 155 } | |
| 156 | |
| 157 void OffersConsentInfoBarDelegate::InfoBarDismissed() { | |
| 158 // Assume no consent if the user closes the Info Bar without | |
| 159 // pressing any of the buttons. | |
| 160 consent_receiver_->SetConsent(false); | |
| 161 } | |
| 162 | |
| 163 void OffersConsentInfoBarDelegate::Observe( | |
| 164 int type, | |
| 165 const content::NotificationSource& source, | |
| 166 const content::NotificationDetails& details) { | |
| 167 if (type == chrome::NOTIFICATION_EXTENSION_HOST_VIEW_SHOULD_CLOSE || | |
| 168 type == chrome::NOTIFICATION_EXTENSION_UNLOADED) { | |
| 169 if (extension_host_.get() == content::Details<ExtensionHost>(details).ptr()) | |
| 170 RemoveSelf(); | |
| 171 } | |
| 172 } | |
| 173 | |
| 174 GetUserConsentFunction::GetUserConsentFunction() { | |
| 175 } | |
| 176 | |
| 177 void GetUserConsentFunction::SetConsent(bool is_consent) { | |
| 178 result_.reset(Value::CreateBooleanValue(is_consent)); | |
| 179 SendResponse(true); | |
| 180 Release(); | |
| 181 } | |
| 182 | |
| 183 GetUserConsentFunction::~GetUserConsentFunction() { | |
| 184 } | |
| 185 | |
| 186 bool GetUserConsentFunction::RunImpl() { | |
| 187 AddRef(); // Balanced in SetConsent(). | |
| 188 ShowConsentInfoBar(); | |
| 189 return true; | |
| 190 } | |
| 191 | |
| 192 void GetUserConsentFunction::ShowConsentInfoBar() { | |
| 193 const Extension* extension = GetExtension(); | |
| 194 GURL url = extension->GetResourceURL(""); | |
| 195 | |
| 196 Browser* browser = GetCurrentBrowser(); | |
| 197 | |
| 198 TabContentsWrapper* tab_contents = NULL; | |
| 199 tab_contents = browser->GetSelectedTabContentsWrapper(); | |
| 200 tab_contents->infobar_tab_helper()->AddInfoBar( | |
| 201 new OffersConsentInfoBarDelegate(browser, | |
| 202 tab_contents->infobar_tab_helper(), | |
| 203 url, | |
| 204 this)); | |
| 205 DCHECK(browser->extension_window_controller()); | |
| 206 } | |
| OLD | NEW |