background_mode_manager_win.cc revision 72a454cd3513ac24fbdd0e0cb9ad70b86a99b801
1// Copyright (c) 2010 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/base_paths.h"
6#include "base/command_line.h"
7#include "base/file_path.h"
8#include "base/logging.h"
9#include "base/path_service.h"
10#include "base/task.h"
11#include "base/win/registry.h"
12#include "chrome/browser/background_mode_manager.h"
13#include "chrome/browser/browser_thread.h"
14#include "chrome/common/chrome_switches.h"
15#include "grit/generated_resources.h"
16#include "ui/base/l10n/l10n_util.h"
17
18namespace {
19
20class DisableLaunchOnStartupTask : public Task {
21 public:
22  virtual void Run();
23};
24
25class EnableLaunchOnStartupTask : public Task {
26 public:
27  virtual void Run();
28};
29
30const HKEY kBackgroundModeRegistryRootKey = HKEY_CURRENT_USER;
31const wchar_t* kBackgroundModeRegistrySubkey =
32    L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
33const wchar_t* kBackgroundModeRegistryKeyName = L"chromium";
34
35void DisableLaunchOnStartupTask::Run() {
36  const wchar_t* key_name = kBackgroundModeRegistryKeyName;
37  base::win::RegKey read_key(kBackgroundModeRegistryRootKey,
38                             kBackgroundModeRegistrySubkey, KEY_READ);
39  base::win::RegKey write_key(kBackgroundModeRegistryRootKey,
40                              kBackgroundModeRegistrySubkey, KEY_WRITE);
41  if (read_key.ValueExists(key_name)) {
42    LONG result = write_key.DeleteValue(key_name);
43    DCHECK_EQ(ERROR_SUCCESS, result) <<
44        "Failed to deregister launch on login. error: " << result;
45  }
46}
47
48void EnableLaunchOnStartupTask::Run() {
49  // TODO(rickcam): Bug 53597: Make RegKey mockable.
50  // TODO(rickcam): Bug 53600: Use distinct registry keys per flavor+profile.
51  const wchar_t* key_name = kBackgroundModeRegistryKeyName;
52  base::win::RegKey read_key(kBackgroundModeRegistryRootKey,
53                             kBackgroundModeRegistrySubkey, KEY_READ);
54  base::win::RegKey write_key(kBackgroundModeRegistryRootKey,
55                              kBackgroundModeRegistrySubkey, KEY_WRITE);
56  FilePath executable;
57  if (!PathService::Get(base::FILE_EXE, &executable))
58    return;
59  std::wstring new_value = executable.value() + L" --no-startup-window";
60  if (read_key.ValueExists(key_name)) {
61    std::wstring current_value;
62    if ((read_key.ReadValue(key_name, &current_value) == ERROR_SUCCESS) &&
63        (current_value == new_value)) {
64      return;
65    }
66  }
67  LONG result = write_key.WriteValue(key_name, new_value.c_str());
68  DCHECK_EQ(ERROR_SUCCESS, result) <<
69      "Failed to register launch on login. error: " << result;
70}
71
72}  // namespace
73
74void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
75  // This functionality is only defined for default profile, currently.
76  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir))
77    return;
78  if (should_launch) {
79    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
80                            new EnableLaunchOnStartupTask());
81  } else {
82    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
83                            new DisableLaunchOnStartupTask());
84  }
85}
86
87string16 BackgroundModeManager::GetPreferencesMenuLabel() {
88  return l10n_util::GetStringUTF16(IDS_OPTIONS);
89}
90