1// Copyright (c) 2011 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 "base/command_line.h"
6#include "base/mac/mac_util.h"
7#include "chrome/browser/background_mode_manager.h"
8#include "chrome/browser/browser_process.h"
9#include "chrome/browser/prefs/pref_service.h"
10#include "chrome/common/app_mode_common_mac.h"
11#include "chrome/common/chrome_switches.h"
12#include "chrome/common/pref_names.h"
13#include "content/browser/browser_thread.h"
14#include "grit/generated_resources.h"
15#include "ui/base/l10n/l10n_util.h"
16
17namespace {
18
19class DisableLaunchOnStartupTask : public Task {
20 public:
21  virtual void Run();
22};
23
24class EnableLaunchOnStartupTask : public Task {
25 public:
26  virtual void Run();
27};
28
29class SetUserCreatedLoginItemPrefTask : public Task {
30 public:
31  virtual void Run();
32};
33
34void DisableLaunchOnStartupTask::Run() {
35  // Check if Chrome is not a login Item, or is a Login Item but w/o 'hidden'
36  // flag - most likely user has modified the setting, don't override it.
37  bool is_hidden = false;
38  if (!base::mac::CheckLoginItemStatus(&is_hidden) || !is_hidden)
39    return;
40
41  base::mac::RemoveFromLoginItems();
42}
43
44void EnableLaunchOnStartupTask::Run() {
45  // Return if Chrome is already a Login Item (avoid overriding user choice).
46  if (base::mac::CheckLoginItemStatus(NULL)) {
47    // Call back to the UI thread to set our preference so we don't delete the
48    // user's login item when we disable launch on startup. There's a race
49    // condition here if the user disables launch on startup before our callback
50    // is run, but the user can manually disable "Open At Login" via the dock if
51    // this happens.
52    BrowserThread::PostTask(BrowserThread::UI, FROM_HERE,
53                            new SetUserCreatedLoginItemPrefTask());
54    return;
55  }
56
57  base::mac::AddToLoginItems(true);  // Hide on startup.
58}
59
60void SetUserCreatedLoginItemPrefTask::Run() {
61  PrefService* service = g_browser_process->local_state();
62  service->SetBoolean(prefs::kUserCreatedLoginItem, true);
63}
64
65}  // namespace
66
67void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
68  // This functionality is only defined for default profile, currently.
69  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir))
70    return;
71
72  if (should_launch) {
73    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
74                            new EnableLaunchOnStartupTask());
75  } else {
76    PrefService* service = g_browser_process->local_state();
77    if (service->GetBoolean(prefs::kUserCreatedLoginItem)) {
78      // We didn't create the login item, so nothing to do here.
79      service->ClearPref(prefs::kUserCreatedLoginItem);
80      return;
81    }
82    // Call to the File thread to remove the login item since it requires
83    // accessing the disk.
84    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
85                            new DisableLaunchOnStartupTask());
86  }
87}
88
89void BackgroundModeManager::DisplayAppInstalledNotification(
90    const Extension* extension) {
91  // TODO(atwilson): Display a platform-appropriate notification here.
92  // http://crbug.com/74970
93}
94
95string16 BackgroundModeManager::GetPreferencesMenuLabel() {
96  return l10n_util::GetStringUTF16(IDS_OPTIONS);
97}
98