autolaunch_prompt_win.cc revision ca12bfac764ba476d6cd062bf1dde12cc64c3f40
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_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
31
32// AutolaunchInfoBarDelegate --------------------------------------------------
33
34namespace {
35
36// The delegate for the infobar shown when Chrome is auto-launched.
37class AutolaunchInfoBarDelegate : public ConfirmInfoBarDelegate {
38 public:
39  // Creates an autolaunch infobar delegate and adds it to |infobar_service|.
40  static void Create(InfoBarService* infobar_service, Profile* profile);
41
42 private:
43  AutolaunchInfoBarDelegate(InfoBarService* infobar_service,
44                            Profile* profile);
45  virtual ~AutolaunchInfoBarDelegate();
46
47  void set_should_expire() { should_expire_ = true; }
48
49  // ConfirmInfoBarDelegate:
50  virtual int GetIconID() const OVERRIDE;
51  virtual string16 GetMessageText() const OVERRIDE;
52  virtual string16 GetButtonLabel(InfoBarButton button) const OVERRIDE;
53  virtual bool Accept() OVERRIDE;
54  virtual bool Cancel() OVERRIDE;
55  virtual bool ShouldExpireInternal(
56      const content::LoadCommittedDetails& 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(scoped_ptr<InfoBarDelegate>(
74      new AutolaunchInfoBarDelegate(infobar_service, profile)));
75}
76
77AutolaunchInfoBarDelegate::AutolaunchInfoBarDelegate(
78    InfoBarService* infobar_service,
79    Profile* profile)
80    : ConfirmInfoBarDelegate(infobar_service),
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
104string16 AutolaunchInfoBarDelegate::GetMessageText() const {
105  return l10n_util::GetStringUTF16(IDS_AUTO_LAUNCH_INFOBAR_TEXT);
106}
107
108string16 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 content::LoadCommittedDetails& 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(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::kChromeFrame))
157    return false;
158
159  if (!command_line.HasSwitch(switches::kAutoLaunchAtStartup) &&
160      !first_run::IsChromeFirstRun()) {
161    return false;
162  }
163
164  content::WebContents* web_contents =
165      browser->tab_strip_model()->GetActiveWebContents();
166  AutolaunchInfoBarDelegate::Create(
167      InfoBarService::FromWebContents(web_contents),
168      Profile::FromBrowserContext(web_contents->GetBrowserContext()));
169  return true;
170}
171
172void RegisterAutolaunchUserPrefs(user_prefs::PrefRegistrySyncable* registry) {
173  registry->RegisterIntegerPref(
174      prefs::kShownAutoLaunchInfobar, 0,
175      user_prefs::PrefRegistrySyncable::UNSYNCABLE_PREF);
176}
177
178}  // namespace chrome
179