autolaunch_prompt_win.cc revision c2e0dbddbe15c98d52c4786dac06cb8952a8ae6d
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/ui/startup/autolaunch_prompt.h" 6 7#include "base/command_line.h" 8#include "base/message_loop.h" 9#include "base/prefs/pref_service.h" 10#include "base/utf_string_conversions.h" 11#include "chrome/browser/auto_launch_trial.h" 12#include "chrome/browser/first_run/first_run.h" 13#include "chrome/browser/infobars/confirm_infobar_delegate.h" 14#include "chrome/browser/infobars/infobar_service.h" 15#include "chrome/browser/profiles/profile.h" 16#include "chrome/browser/ui/browser.h" 17#include "chrome/browser/ui/tabs/tab_strip_model.h" 18#include "chrome/common/chrome_constants.h" 19#include "chrome/common/chrome_switches.h" 20#include "chrome/common/pref_names.h" 21#include "chrome/installer/util/auto_launch_util.h" 22#include "components/user_prefs/pref_registry_syncable.h" 23#include "content/public/browser/browser_thread.h" 24#include "content/public/browser/navigation_details.h" 25#include "content/public/browser/web_contents.h" 26#include "grit/chromium_strings.h" 27#include "grit/generated_resources.h" 28#include "grit/theme_resources.h" 29#include "ui/base/l10n/l10n_util.h" 30#include "ui/base/resource/resource_bundle.h" 31 32using content::BrowserThread; 33 34 35// AutolaunchInfoBarDelegate -------------------------------------------------- 36 37namespace { 38 39// The delegate for the infobar shown when Chrome was auto-launched. 40class AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate { 41 public: 42 // Creates an autolaunch delegate and adds it to |infobar_service|. 43 static void Create(InfoBarService* infobar_service, 44 PrefService* prefs, 45 Profile* profile); 46 47 private: 48 AutolaunchInfoBarDelegate(InfoBarService* infobar_service, 49 PrefService* prefs, 50 Profile* profile); 51 virtual ~AutolaunchInfoBarDelegate(); 52 53 void AllowExpiry() { should_expire_ = true; } 54 55 // ConfirmInfoBarDelegate: 56 virtual gfx::Image* GetIcon() const OVERRIDE; 57 virtual string16 GetMessageText() const OVERRIDE; 58 virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; 59 virtual bool Accept() OVERRIDE; 60 virtual bool Cancel() OVERRIDE; 61 virtual bool ShouldExpireInternal( 62 const content::LoadCommittedDetails& details) const OVERRIDE; 63 64 // The prefs to use. 65 PrefService* prefs_; 66 67 // Whether the info-bar should be dismissed on the next navigation. 68 bool should_expire_; 69 70 // Weak pointer to the profile, not owned by us. 71 Profile* profile_; 72 73 // Used to delay the expiration of the info-bar. 74 base::WeakPtrFactory<AutolaunchInfoBarDelegate> weak_factory_; 75 76 DISALLOW_COPY_AND_ASSIGN(AutolaunchInfoBarDelegate); 77}; 78 79// static 80void AutolaunchInfoBarDelegate::Create(InfoBarService* infobar_service, 81 PrefService* prefs, 82 Profile* profile) { 83 infobar_service->AddInfoBar(scoped_ptr<InfoBarDelegate>( 84 new AutolaunchInfoBarDelegate(infobar_service, prefs, profile))); 85} 86 87AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate( 88 InfoBarService* infobar_service, 89 PrefService* prefs, 90 Profile* profile) 91 : ConfirmInfoBarDelegate(infobar_service), 92 prefs_(prefs), 93 should_expire_(false), 94 profile_(profile), 95 weak_factory_(this) { 96 int count = prefs_->GetInteger(prefs::kShownAutoLaunchInfobar); 97 prefs_->SetInteger(prefs::kShownAutoLaunchInfobar, count + 1); 98 99 // We want the info-bar to stick-around for a few seconds and then be hidden 100 // on the next navigation after that. 101 MessageLoop::current()->PostDelayedTask( 102 FROM_HERE, 103 base::Bind(&AutolaunchInfoBarDelegate::AllowExpiry, 104 weak_factory_.GetWeakPtr()), 105 base::TimeDelta::FromSeconds(8)); 106} 107 108AutolaunchInfoBarDelegate::~AutolaunchInfoBarDelegate() { 109} 110 111gfx::Image* AutolaunchInfoBarDelegate::GetIcon() const { 112 return &ResourceBundle::GetSharedInstance().GetNativeImageNamed( 113 IDR_PRODUCT_LOGO_32); 114} 115 116string16 AutolaunchInfoBarDelegate::GetMessageText() const { 117 return l10n_util::GetStringUTF16(IDS_AUTO_LAUNCH_INFOBAR_TEXT); 118} 119 120string16 AutolaunchInfoBarDelegate::GetButtonLabel( 121 InfoBarButton button) const { 122 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? 123 IDS_AUTO_LAUNCH_OK : IDS_AUTO_LAUNCH_REVERT); 124} 125 126bool AutolaunchInfoBarDelegate::Accept() { 127 return true; 128} 129 130bool AutolaunchInfoBarDelegate::Cancel() { 131 content::BrowserThread::PostTask( 132 content::BrowserThread::FILE, FROM_HERE, 133 base::Bind(&auto_launch_util::DisableForegroundStartAtLogin, 134 profile_->GetPath().BaseName().value())); 135 return true; 136} 137 138bool AutolaunchInfoBarDelegate::ShouldExpireInternal( 139 const content::LoadCommittedDetails& details) const { 140 return should_expire_; 141} 142 143} // namespace 144 145 146// Functions ------------------------------------------------------------------ 147 148namespace chrome { 149 150bool ShowAutolaunchPrompt(Browser* browser) { 151 if (!auto_launch_trial::IsInAutoLaunchGroup()) 152 return false; 153 154 // Only supported on the main profile for now. 155 Profile* profile = browser->profile(); 156 if (profile->GetPath().BaseName() != 157 base::FilePath(ASCIIToUTF16(chrome::kInitialProfile))) { 158 return false; 159 } 160 161 int infobar_shown = 162 profile->GetPrefs()->GetInteger(prefs::kShownAutoLaunchInfobar); 163 const int kMaxInfobarShown = 5; 164 if (infobar_shown >= kMaxInfobarShown) 165 return false; 166 167 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 168 if (command_line.HasSwitch(switches::kChromeFrame)) 169 return false; 170 171 if (!command_line.HasSwitch(switches::kAutoLaunchAtStartup) && 172 !first_run::IsChromeFirstRun()) { 173 return false; 174 } 175 176 content::WebContents* web_contents = 177 browser->tab_strip_model()->GetActiveWebContents(); 178 profile = Profile::FromBrowserContext(web_contents->GetBrowserContext()); 179 AutolaunchInfoBarDelegate::Create( 180 InfoBarService::FromWebContents(web_contents), profile->GetPrefs(), 181 profile); 182 return true; 183} 184 185void RegisterAutolaunchUserPrefs(user_prefs::PrefRegistrySyncable* registry) { 186 registry->RegisterIntegerPref( 187 prefs::kShownAutoLaunchInfobar, 188 0, 189 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 190} 191 192} // namespace chrome 193