| OLD | NEW |
| (Empty) | |
| 1 // Copyright (c) 2009 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/cocoa/keystone_infobar.h" |
| 6 |
| 7 #import <AppKit/AppKit.h> |
| 8 |
| 9 #include <string> |
| 10 |
| 11 #include "app/l10n_util.h" |
| 12 #include "app/resource_bundle.h" |
| 13 #include "base/command_line.h" |
| 14 #include "base/message_loop.h" |
| 15 #include "base/task.h" |
| 16 #import "chrome/browser/cocoa/keystone_glue.h" |
| 17 #include "chrome/browser/browser.h" |
| 18 #include "chrome/browser/browser_list.h" |
| 19 #include "chrome/browser/first_run.h" |
| 20 #include "chrome/browser/tab_contents/infobar_delegate.h" |
| 21 #include "chrome/browser/tab_contents/navigation_controller.h" |
| 22 #include "chrome/browser/tab_contents/tab_contents.h" |
| 23 #include "chrome/browser/profile.h" |
| 24 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/pref_names.h" |
| 26 #include "grit/chromium_strings.h" |
| 27 #include "grit/generated_resources.h" |
| 28 #include "grit/theme_resources.h" |
| 29 |
| 30 class SkBitmap; |
| 31 |
| 32 namespace { |
| 33 |
| 34 class KeystonePromotionInfoBarDelegate : public ConfirmInfoBarDelegate { |
| 35 public: |
| 36 KeystonePromotionInfoBarDelegate(TabContents* tab_contents) |
| 37 : ConfirmInfoBarDelegate(tab_contents), |
| 38 profile_(tab_contents->profile()), |
| 39 can_expire_(false), |
| 40 ALLOW_THIS_IN_INITIALIZER_LIST(method_factory_(this)) { |
| 41 const int kCanExpireOnNavigationAfterMilliseconds = 8 * 1000; |
| 42 MessageLoop::current()->PostDelayedTask( |
| 43 FROM_HERE, |
| 44 method_factory_.NewRunnableMethod( |
| 45 &KeystonePromotionInfoBarDelegate::SetCanExpire), |
| 46 kCanExpireOnNavigationAfterMilliseconds); |
| 47 } |
| 48 |
| 49 virtual ~KeystonePromotionInfoBarDelegate() {} |
| 50 |
| 51 // Inherited from InfoBarDelegate and overridden. |
| 52 |
| 53 virtual bool ShouldExpire( |
| 54 const NavigationController::LoadCommittedDetails& details) { |
| 55 return can_expire_; |
| 56 } |
| 57 |
| 58 virtual void InfoBarClosed() { |
| 59 delete this; |
| 60 } |
| 61 |
| 62 // Inherited from AlertInfoBarDelegate and overridden. |
| 63 |
| 64 virtual std::wstring GetMessageText() const { |
| 65 return l10n_util::GetStringF(IDS_PROMOTE_INFOBAR_TEXT, |
| 66 l10n_util::GetString(IDS_PRODUCT_NAME)); |
| 67 } |
| 68 |
| 69 virtual SkBitmap* GetIcon() const { |
| 70 return ResourceBundle::GetSharedInstance().GetBitmapNamed( |
| 71 IDR_PRODUCT_ICON_32); |
| 72 } |
| 73 |
| 74 // Inherited from ConfirmInfoBarDelegate and overridden. |
| 75 |
| 76 virtual int GetButtons() const { |
| 77 return BUTTON_OK | BUTTON_CANCEL | BUTTON_OK_DEFAULT; |
| 78 } |
| 79 |
| 80 virtual std::wstring GetButtonLabel(InfoBarButton button) const { |
| 81 return button == BUTTON_OK ? |
| 82 l10n_util::GetString(IDS_PROMOTE_INFOBAR_PROMOTE_BUTTON) : |
| 83 l10n_util::GetString(IDS_PROMOTE_INFOBAR_DONT_ASK_BUTTON); |
| 84 } |
| 85 |
| 86 virtual bool Accept() { |
| 87 [[KeystoneGlue defaultKeystoneGlue] promoteTicket]; |
| 88 return true; |
| 89 } |
| 90 |
| 91 virtual bool Cancel() { |
| 92 profile_->GetPrefs()->SetBoolean(prefs::kShowUpdatePromotionInfoBar, false); |
| 93 return true; |
| 94 } |
| 95 |
| 96 private: |
| 97 // Sets this info bar to be able to expire. Called a predetermined amount |
| 98 // of time after this object is created. |
| 99 void SetCanExpire() { |
| 100 can_expire_ = true; |
| 101 } |
| 102 |
| 103 // The TabContents' profile. |
| 104 Profile* profile_; // weak |
| 105 |
| 106 // Whether the info bar should be dismissed on the next navigation. |
| 107 bool can_expire_; |
| 108 |
| 109 // Used to delay the expiration of the info bar. |
| 110 ScopedRunnableMethodFactory<KeystonePromotionInfoBarDelegate> method_factory_; |
| 111 |
| 112 DISALLOW_COPY_AND_ASSIGN(KeystonePromotionInfoBarDelegate); |
| 113 }; |
| 114 |
| 115 } // namespace |
| 116 |
| 117 @interface KeystonePromotionInfoBar : NSObject |
| 118 - (void)checkAndShowInfoBarForProfile:(Profile*)profile; |
| 119 - (void)updateStatus:(NSNotification*)notification; |
| 120 - (void)removeObserver; |
| 121 @end // @interface KeystonePromotionInfoBar |
| 122 |
| 123 @implementation KeystonePromotionInfoBar |
| 124 |
| 125 - (void)dealloc { |
| 126 [self removeObserver]; |
| 127 [super dealloc]; |
| 128 } |
| 129 |
| 130 - (void)checkAndShowInfoBarForProfile:(Profile*)profile { |
| 131 // If this is the first run, the user clicked the "don't ask again" button |
| 132 // at some point in the past, or if the "don't ask about the default |
| 133 // browser" command-line switch is present, bail out. That command-line |
| 134 // switch is recycled here because it's likely that the set of users that |
| 135 // don't want to be nagged about the default browser also don't want to be |
| 136 // nagged about the update check. (Automated testers, I'm thinking of |
| 137 // you...) |
| 138 CommandLine* commandLine = CommandLine::ForCurrentProcess(); |
| 139 if (FirstRun::IsChromeFirstRun() || |
| 140 !profile->GetPrefs()->GetBoolean(prefs::kShowUpdatePromotionInfoBar) || |
| 141 commandLine->HasSwitch(switches::kNoDefaultBrowserCheck)) { |
| 142 return; |
| 143 } |
| 144 |
| 145 // If there is no Keystone glue (maybe because this application isn't |
| 146 // Keystone-enabled) or the application is on a read-only filesystem, |
| 147 // doing anything related to auto-update is pointless. Bail out. |
| 148 KeystoneGlue* keystoneGlue = [KeystoneGlue defaultKeystoneGlue]; |
| 149 if (!keystoneGlue || [keystoneGlue isOnReadOnlyFilesystem]) { |
| 150 return; |
| 151 } |
| 152 |
| 153 // Stay alive as long as needed. This is balanced by a release in |
| 154 // -updateStatus:. |
| 155 [self retain]; |
| 156 |
| 157 AutoupdateStatus recentStatus = [keystoneGlue recentStatus]; |
| 158 if (recentStatus == kAutoupdateNone || |
| 159 recentStatus == kAutoupdateRegistering) { |
| 160 NSNotificationCenter* center = [NSNotificationCenter defaultCenter]; |
| 161 [center addObserver:self |
| 162 selector:@selector(updateStatus:) |
| 163 name:kAutoupdateStatusNotification |
| 164 object:nil]; |
| 165 } else { |
| 166 [self updateStatus:[keystoneGlue recentNotification]]; |
| 167 } |
| 168 } |
| 169 |
| 170 - (void)updateStatus:(NSNotification*)notification { |
| 171 NSDictionary* dictionary = [notification userInfo]; |
| 172 AutoupdateStatus status = static_cast<AutoupdateStatus>( |
| 173 [[dictionary objectForKey:kAutoupdateStatusStatus] intValue]); |
| 174 |
| 175 if (status == kAutoupdateNone || status == kAutoupdateRegistering) { |
| 176 return; |
| 177 } |
| 178 |
| 179 [self removeObserver]; |
| 180 |
| 181 if (status != kAutoupdateRegisterFailed && |
| 182 [[KeystoneGlue defaultKeystoneGlue] needsPromotion]) { |
| 183 Browser* browser = BrowserList::GetLastActive(); |
| 184 if (browser) { |
| 185 TabContents* tabContents = browser->GetSelectedTabContents(); |
| 186 |
| 187 // Only show if no other info bars are showing, because that's how the |
| 188 // default browser info bar works. |
| 189 if (tabContents && tabContents->infobar_delegate_count() == 0) { |
| 190 tabContents->AddInfoBar( |
| 191 new KeystonePromotionInfoBarDelegate(tabContents)); |
| 192 } |
| 193 } |
| 194 } |
| 195 |
| 196 [self release]; |
| 197 } |
| 198 |
| 199 - (void)removeObserver { |
| 200 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 201 } |
| 202 |
| 203 @end // @implementation KeystonePromotionInfoBar |
| 204 |
| 205 // static |
| 206 void KeystoneInfoBar::PromotionInfoBar(Profile* profile) { |
| 207 KeystonePromotionInfoBar* promotionInfoBar = |
| 208 [[[KeystonePromotionInfoBar alloc] init] autorelease]; |
| 209 |
| 210 [promotionInfoBar checkAndShowInfoBarForProfile:profile]; |
| 211 } |
| OLD | NEW |