summaryrefslogtreecommitdiffstats
path: root/chrome/browser/background_mode_manager_mac.mm
blob: 02e93f4eef9280f96c0cf39d4e1e4b83a2e5c771 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
// Copyright (c) 2010 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#include "base/command_line.h"
#include "base/mac/mac_util.h"
#include "chrome/browser/background_mode_manager.h"
#include "chrome/browser/browser_thread.h"
#include "chrome/common/app_mode_common_mac.h"
#include "chrome/common/chrome_switches.h"
#include "grit/generated_resources.h"
#include "ui/base/l10n/l10n_util.h"

namespace {

class DisableLaunchOnStartupTask : public Task {
 public:
  virtual void Run();
};

class EnableLaunchOnStartupTask : public Task {
 public:
  virtual void Run();
};

const CFStringRef kLaunchOnStartupResetAllowedPrefsKey =
    CFSTR("LaunchOnStartupResetAllowed");

void DisableLaunchOnStartupTask::Run() {
  Boolean key_exists_and_has_valid_format;  // ignored
  if (!CFPreferencesGetAppBooleanValue(kLaunchOnStartupResetAllowedPrefsKey,
                                       app_mode::kAppPrefsID,
                                       &key_exists_and_has_valid_format))
    return;

  CFPreferencesSetAppValue(kLaunchOnStartupResetAllowedPrefsKey,
                           kCFBooleanFalse,
                           app_mode::kAppPrefsID);

  // Check if Chrome is not a login Item, or is a Login Item but w/o 'hidden'
  // flag - most likely user has modified the setting, don't override it.
  bool is_hidden = false;
  if (!base::mac::CheckLoginItemStatus(&is_hidden) || !is_hidden)
    return;

  base::mac::RemoveFromLoginItems();
}

void EnableLaunchOnStartupTask::Run() {
  // Return if Chrome is already a Login Item (avoid overriding user choice).
  if (base::mac::CheckLoginItemStatus(NULL))
    return;

  base::mac::AddToLoginItems(true);  // Hide on startup.
  CFPreferencesSetAppValue(kLaunchOnStartupResetAllowedPrefsKey,
                           kCFBooleanTrue,
                           app_mode::kAppPrefsID);
}

}  // namespace

void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
  // This functionality is only defined for default profile, currently.
  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir))
    return;

  if (should_launch) {
    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
                            new EnableLaunchOnStartupTask());
  } else {
    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
                            new DisableLaunchOnStartupTask());
  }
}

string16 BackgroundModeManager::GetPreferencesMenuLabel() {
  return l10n_util::GetStringUTF16(IDS_OPTIONS);
}