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.
5 #include "chrome/browser/ui/startup/autolaunch_prompt.h"
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"
33 // AutolaunchInfoBarDelegate --------------------------------------------------
37 // The delegate for the infobar shown when Chrome is auto-launched.
38 class AutolaunchInfoBarDelegate
: public ConfirmInfoBarDelegate
{
40 // Creates an autolaunch infobar and delegate and adds the infobar to
42 static void Create(InfoBarService
* infobar_service
, Profile
* profile
);
45 explicit AutolaunchInfoBarDelegate(Profile
* profile
);
46 virtual ~AutolaunchInfoBarDelegate();
48 void set_should_expire() { should_expire_
= true; }
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
;
59 // Weak pointer to the profile, not owned by us.
62 // Whether the info-bar should be dismissed on the next navigation.
65 // Used to delay the expiration of the info-bar.
66 base::WeakPtrFactory
<AutolaunchInfoBarDelegate
> weak_factory_
;
68 DISALLOW_COPY_AND_ASSIGN(AutolaunchInfoBarDelegate
);
72 void AutolaunchInfoBarDelegate::Create(InfoBarService
* infobar_service
,
74 infobar_service
->AddInfoBar(ConfirmInfoBarDelegate::CreateInfoBar(
75 scoped_ptr
<ConfirmInfoBarDelegate
>(
76 new AutolaunchInfoBarDelegate(profile
))));
79 AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate(
81 : ConfirmInfoBarDelegate(),
83 should_expire_(false),
85 PrefService
* prefs
= profile
->GetPrefs();
86 prefs
->SetInteger(prefs::kShownAutoLaunchInfobar
,
87 prefs
->GetInteger(prefs::kShownAutoLaunchInfobar
) + 1);
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(
93 base::Bind(&AutolaunchInfoBarDelegate::set_should_expire
,
94 weak_factory_
.GetWeakPtr()),
95 base::TimeDelta::FromSeconds(8));
98 AutolaunchInfoBarDelegate::~AutolaunchInfoBarDelegate() {
101 int AutolaunchInfoBarDelegate::GetIconID() const {
102 return IDR_PRODUCT_LOGO_32
;
105 base::string16
AutolaunchInfoBarDelegate::GetMessageText() const {
106 return l10n_util::GetStringUTF16(IDS_AUTO_LAUNCH_INFOBAR_TEXT
);
109 base::string16
AutolaunchInfoBarDelegate::GetButtonLabel(
110 InfoBarButton button
) const {
111 return l10n_util::GetStringUTF16((button
== BUTTON_OK
) ?
112 IDS_AUTO_LAUNCH_OK
: IDS_AUTO_LAUNCH_REVERT
);
115 bool AutolaunchInfoBarDelegate::Accept() {
119 bool AutolaunchInfoBarDelegate::Cancel() {
120 content::BrowserThread::PostTask(
121 content::BrowserThread::FILE, FROM_HERE
,
122 base::Bind(&auto_launch_util::DisableForegroundStartAtLogin
,
123 profile_
->GetPath().BaseName().value()));
127 bool AutolaunchInfoBarDelegate::ShouldExpireInternal(
128 const content::LoadCommittedDetails
& details
) const {
129 return should_expire_
;
135 // Functions ------------------------------------------------------------------
139 bool ShowAutolaunchPrompt(Browser
* browser
) {
140 if (!auto_launch_trial::IsInAutoLaunchGroup())
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
))) {
151 profile
->GetPrefs()->GetInteger(prefs::kShownAutoLaunchInfobar
);
152 const int kMaxTimesToShowInfoBar
= 5;
153 if (infobar_shown
>= kMaxTimesToShowInfoBar
)
156 const CommandLine
& command_line
= *CommandLine::ForCurrentProcess();
157 if (!command_line
.HasSwitch(switches::kAutoLaunchAtStartup
) &&
158 !first_run::IsChromeFirstRun()) {
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()));
170 void RegisterAutolaunchUserPrefs(user_prefs::PrefRegistrySyncable
* registry
) {
171 registry
->RegisterIntegerPref(
172 prefs::kShownAutoLaunchInfobar
, 0,
173 user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF
);
176 } // namespace chrome