Merge Chromium + Blink git repositories
[chromium-blink-merge.git] / chrome / browser / ui / startup / autolaunch_prompt_win.cc
blob039133f468ac7dcd995585075d45b7bcdca03d20
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/infobar_service.h"
14 #include "chrome/browser/profiles/profile.h"
15 #include "chrome/browser/ui/browser.h"
16 #include "chrome/browser/ui/tabs/tab_strip_model.h"
17 #include "chrome/common/chrome_constants.h"
18 #include "chrome/common/chrome_switches.h"
19 #include "chrome/common/pref_names.h"
20 #include "chrome/grit/chromium_strings.h"
21 #include "chrome/grit/generated_resources.h"
22 #include "chrome/installer/util/auto_launch_util.h"
23 #include "components/infobars/core/confirm_infobar_delegate.h"
24 #include "components/infobars/core/infobar.h"
25 #include "components/pref_registry/pref_registry_syncable.h"
26 #include "content/public/browser/browser_thread.h"
27 #include "content/public/browser/web_contents.h"
28 #include "grit/theme_resources.h"
29 #include "ui/base/l10n/l10n_util.h"
32 // AutolaunchInfoBarDelegate --------------------------------------------------
34 namespace {
36 // The delegate for the infobar shown when Chrome is auto-launched.
37 class AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate {
38 public:
39 // Creates an autolaunch infobar and delegate and adds the infobar to
40 // |infobar_service|.
41 static void Create(InfoBarService* infobar_service, Profile* profile);
43 private:
44 explicit AutolaunchInfoBarDelegate(Profile* profile);
45 ~AutolaunchInfoBarDelegate() override;
47 void set_should_expire() { should_expire_ = true; }
49 // ConfirmInfoBarDelegate:
50 int GetIconId() const override;
51 bool ShouldExpire(const NavigationDetails& details) const override;
52 base::string16 GetMessageText() const override;
53 base::string16 GetButtonLabel(InfoBarButton button) const override;
54 bool Accept() override;
55 bool Cancel() override;
57 // Weak pointer to the profile, not owned by us.
58 Profile* profile_;
60 // Whether the info-bar should be dismissed on the next navigation.
61 bool should_expire_;
63 // Used to delay the expiration of the info-bar.
64 base::WeakPtrFactory<AutolaunchInfoBarDelegate> weak_factory_;
66 DISALLOW_COPY_AND_ASSIGN(AutolaunchInfoBarDelegate);
69 // static
70 void AutolaunchInfoBarDelegate::Create(InfoBarService* infobar_service,
71 Profile* profile) {
72 infobar_service->AddInfoBar(
73 infobar_service->CreateConfirmInfoBar(scoped_ptr<ConfirmInfoBarDelegate>(
74 new AutolaunchInfoBarDelegate(profile))));
77 AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate(
78 Profile* profile)
79 : ConfirmInfoBarDelegate(),
80 profile_(profile),
81 should_expire_(false),
82 weak_factory_(this) {
83 PrefService* prefs = profile->GetPrefs();
84 prefs->SetInteger(prefs::kShownAutoLaunchInfobar,
85 prefs->GetInteger(prefs::kShownAutoLaunchInfobar) + 1);
87 // We want the info-bar to stick-around for a few seconds and then be hidden
88 // on the next navigation after that.
89 base::MessageLoop::current()->PostDelayedTask(
90 FROM_HERE,
91 base::Bind(&AutolaunchInfoBarDelegate::set_should_expire,
92 weak_factory_.GetWeakPtr()),
93 base::TimeDelta::FromSeconds(8));
96 AutolaunchInfoBarDelegate::~AutolaunchInfoBarDelegate() {
99 int AutolaunchInfoBarDelegate::GetIconId() const {
100 return IDR_PRODUCT_LOGO_32;
103 bool AutolaunchInfoBarDelegate::ShouldExpire(
104 const NavigationDetails& details) const {
105 return should_expire_ && ConfirmInfoBarDelegate::ShouldExpire(details);
108 base::string16 AutolaunchInfoBarDelegate::GetMessageText() const {
109 return l10n_util::GetStringUTF16(IDS_AUTO_LAUNCH_INFOBAR_TEXT);
112 base::string16 AutolaunchInfoBarDelegate::GetButtonLabel(
113 InfoBarButton button) const {
114 return l10n_util::GetStringUTF16((button == BUTTON_OK) ?
115 IDS_AUTO_LAUNCH_OK : IDS_AUTO_LAUNCH_REVERT);
118 bool AutolaunchInfoBarDelegate::Accept() {
119 return true;
122 bool AutolaunchInfoBarDelegate::Cancel() {
123 content::BrowserThread::PostTask(
124 content::BrowserThread::FILE, FROM_HERE,
125 base::Bind(&auto_launch_util::DisableForegroundStartAtLogin,
126 profile_->GetPath().BaseName().value()));
127 return true;
130 } // namespace
133 // Functions ------------------------------------------------------------------
135 namespace chrome {
137 bool ShowAutolaunchPrompt(Browser* browser) {
138 if (!auto_launch_trial::IsInAutoLaunchGroup())
139 return false;
141 // Only supported on the main profile for now.
142 Profile* profile = browser->profile();
143 if (profile->GetPath().BaseName() !=
144 base::FilePath(base::ASCIIToUTF16(chrome::kInitialProfile))) {
145 return false;
148 int infobar_shown =
149 profile->GetPrefs()->GetInteger(prefs::kShownAutoLaunchInfobar);
150 const int kMaxTimesToShowInfoBar = 5;
151 if (infobar_shown >= kMaxTimesToShowInfoBar)
152 return false;
154 const base::CommandLine& command_line =
155 *base::CommandLine::ForCurrentProcess();
156 if (!command_line.HasSwitch(switches::kAutoLaunchAtStartup) &&
157 !first_run::IsChromeFirstRun()) {
158 return false;
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;
169 void RegisterAutolaunchUserPrefs(user_prefs::PrefRegistrySyncable* registry) {
170 registry->RegisterIntegerPref(prefs::kShownAutoLaunchInfobar, 0);
173 } // namespace chrome