16e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root// Copyright (c) 2012 The Chromium Authors. All rights reserved. 26e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root// Use of this source code is governed by a BSD-style license that can be 36e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root// found in the LICENSE file. 46e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 56e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/ui/startup/autolaunch_prompt.h" 66e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 76e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "base/command_line.h" 86e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "base/message_loop/message_loop.h" 96e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "base/prefs/pref_service.h" 106e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "base/strings/utf_string_conversions.h" 116e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/auto_launch_trial.h" 126e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/first_run/first_run.h" 136e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/infobars/infobar_service.h" 146e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/profiles/profile.h" 156e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/browser/ui/browser.h" 16eb8ac25fe5cbaf33f71532c2920a1faaca8e130fHui Shu#include "chrome/browser/ui/tabs/tab_strip_model.h" 173b1abba6bbc895d63da3e82e9b158c01bd12edddKenny Root#include "chrome/common/chrome_constants.h" 186e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/common/chrome_switches.h" 196e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/common/pref_names.h" 206e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "chrome/grit/chromium_strings.h" 2183c64e6b624a876436d2ef5d2f173b10407e27b4Mathias Agopian#include "chrome/grit/generated_resources.h" 2260e510af804b5b583d835a0f1f1e676d08b812bcMathias Agopian#include "chrome/installer/util/auto_launch_util.h" 23d685894212e6dbeac1fda4996903c1da115d49a6Ying Wang#include "components/infobars/core/confirm_infobar_delegate.h" 24d685894212e6dbeac1fda4996903c1da115d49a6Ying Wang#include "components/infobars/core/infobar.h" 256e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "components/pref_registry/pref_registry_syncable.h" 266e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "content/public/browser/browser_thread.h" 2764ef6b47e020da5370af8897638a0a778fa52b74Iliyan Malchev#include "content/public/browser/web_contents.h" 286e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "grit/theme_resources.h" 296e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root#include "ui/base/l10n/l10n_util.h" 306e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 316e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 326e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root// AutolaunchInfoBarDelegate -------------------------------------------------- 336e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 34a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wangnamespace { 35a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang 363b1abba6bbc895d63da3e82e9b158c01bd12edddKenny Root// The delegate for the infobar shown when Chrome is auto-launched. 37a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wangclass AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate { 38a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang public: 39eb8ac25fe5cbaf33f71532c2920a1faaca8e130fHui Shu // Creates an autolaunch infobar and delegate and adds the infobar to 40a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang // |infobar_service|. 41a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang static void Create(InfoBarService* infobar_service, Profile* profile); 42a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang 43a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang private: 443b1abba6bbc895d63da3e82e9b158c01bd12edddKenny Root explicit AutolaunchInfoBarDelegate(Profile* profile); 45a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang virtual ~AutolaunchInfoBarDelegate(); 463b1abba6bbc895d63da3e82e9b158c01bd12edddKenny Root 47a1b592e60dee58aaaf66ce430f3dbe36cf0448bbYing Wang void set_should_expire() { should_expire_ = true; } 486e7ac5f0bceddf51947fbf3b376e278df0735603Kenny Root 49 // ConfirmInfoBarDelegate: 50 virtual int GetIconID() const OVERRIDE; 51 virtual base::string16 GetMessageText() const OVERRIDE; 52 virtual base::string16 GetButtonLabel(InfoBarButton button) const OVERRIDE; 53 virtual bool Accept() OVERRIDE; 54 virtual bool Cancel() OVERRIDE; 55 virtual bool ShouldExpireInternal( 56 const NavigationDetails& details) const OVERRIDE; 57 58 // Weak pointer to the profile, not owned by us. 59 Profile* profile_; 60 61 // Whether the info-bar should be dismissed on the next navigation. 62 bool should_expire_; 63 64 // Used to delay the expiration of the info-bar. 65 base::WeakPtrFactory<AutolaunchInfoBarDelegate> weak_factory_; 66 67 DISALLOW_COPY_AND_ASSIGN(AutolaunchInfoBarDelegate); 68}; 69 70// static 71void AutolaunchInfoBarDelegate::Create(InfoBarService* infobar_service, 72 Profile* profile) { 73 infobar_service->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar( 74 scoped_ptr<ConfirmInfoBarDelegate>( 75 new AutolaunchInfoBarDelegate(profile)))); 76} 77 78AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate( 79 Profile* profile) 80 : ConfirmInfoBarDelegate(), 81 profile_(profile), 82 should_expire_(false), 83 weak_factory_(this) { 84 PrefService* prefs = profile->GetPrefs(); 85 prefs->SetInteger(prefs::kShownAutoLaunchInfobar, 86 prefs->GetInteger(prefs::kShownAutoLaunchInfobar) + 1); 87 88 // We want the info-bar to stick-around for a few seconds and then be hidden 89 // on the next navigation after that. 90 base::MessageLoop::current()->PostDelayedTask( 91 FROM_HERE, 92 base::Bind(&AutolaunchInfoBarDelegate::set_should_expire, 93 weak_factory_.GetWeakPtr()), 94 base::TimeDelta::FromSeconds(8)); 95} 96 97AutolaunchInfoBarDelegate::~AutolaunchInfoBarDelegate() { 98} 99 100int AutolaunchInfoBarDelegate::GetIconID() const { 101 return IDR_PRODUCT_LOGO_32; 102} 103 104base::string16 AutolaunchInfoBarDelegate::GetMessageText() const { 105 return l10n_util::GetStringUTF16(IDS_AUTO_LAUNCH_INFOBAR_TEXT); 106} 107 108base::string16 AutolaunchInfoBarDelegate::GetButtonLabel( 109 InfoBarButton button) const { 110 return l10n_util::GetStringUTF16((button == BUTTON_OK) ? 111 IDS_AUTO_LAUNCH_OK : IDS_AUTO_LAUNCH_REVERT); 112} 113 114bool AutolaunchInfoBarDelegate::Accept() { 115 return true; 116} 117 118bool AutolaunchInfoBarDelegate::Cancel() { 119 content::BrowserThread::PostTask( 120 content::BrowserThread::FILE, FROM_HERE, 121 base::Bind(&auto_launch_util::DisableForegroundStartAtLogin, 122 profile_->GetPath().BaseName().value())); 123 return true; 124} 125 126bool AutolaunchInfoBarDelegate::ShouldExpireInternal( 127 const NavigationDetails& details) const { 128 return should_expire_; 129} 130 131} // namespace 132 133 134// Functions ------------------------------------------------------------------ 135 136namespace chrome { 137 138bool ShowAutolaunchPrompt(Browser* browser) { 139 if (!auto_launch_trial::IsInAutoLaunchGroup()) 140 return false; 141 142 // Only supported on the main profile for now. 143 Profile* profile = browser->profile(); 144 if (profile->GetPath().BaseName() != 145 base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile))) { 146 return false; 147 } 148 149 int infobar_shown = 150 profile->GetPrefs()->GetInteger(prefs::kShownAutoLaunchInfobar); 151 const int kMaxTimesToShowInfoBar = 5; 152 if (infobar_shown >= kMaxTimesToShowInfoBar) 153 return false; 154 155 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 156 if (!command_line.HasSwitch(switches::kAutoLaunchAtStartup) && 157 !first_run::IsChromeFirstRun()) { 158 return false; 159 } 160 161 content::WebContents* web_contents = 162 browser->tab_strip_model()->GetActiveWebContents(); 163 AutolaunchInfoBarDelegate::Create( 164 InfoBarService::FromWebContents(web_contents), 165 Profile::FromBrowserContext(web_contents->GetBrowserContext())); 166 return true; 167} 168 169void RegisterAutolaunchUserPrefs(user_prefs::PrefRegistrySyncable* registry) { 170 registry->RegisterIntegerPref( 171 prefs::kShownAutoLaunchInfobar, 0, 172 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF); 173} 174 175} // namespace chrome 176