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