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