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/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/utf_string_conversions.h"
12#include "base/win/registry.h"
13#include "chrome/browser/background_mode_manager.h"
14#include "chrome/common/chrome_switches.h"
15#include "content/browser/browser_thread.h"
16#include "grit/chromium_strings.h"
17#include "grit/generated_resources.h"
18#include "ui/base/l10n/l10n_util.h"
19
20namespace {
21
22class DisableLaunchOnStartupTask : public Task {
23 public:
24  virtual void Run();
25};
26
27class EnableLaunchOnStartupTask : public Task {
28 public:
29  virtual void Run();
30};
31
32const HKEY kBackgroundModeRegistryRootKey = HKEY_CURRENT_USER;
33const wchar_t* kBackgroundModeRegistrySubkey =
34    L"Software\\Microsoft\\Windows\\CurrentVersion\\Run";
35const wchar_t* kBackgroundModeRegistryKeyName = L"chromium";
36
37void DisableLaunchOnStartupTask::Run() {
38  const wchar_t* key_name = kBackgroundModeRegistryKeyName;
39  base::win::RegKey read_key(kBackgroundModeRegistryRootKey,
40                             kBackgroundModeRegistrySubkey, KEY_READ);
41  base::win::RegKey write_key(kBackgroundModeRegistryRootKey,
42                              kBackgroundModeRegistrySubkey, KEY_WRITE);
43  if (read_key.ValueExists(key_name)) {
44    LONG result = write_key.DeleteValue(key_name);
45    DCHECK_EQ(ERROR_SUCCESS, result) <<
46        "Failed to deregister launch on login. error: " << result;
47  }
48}
49
50void EnableLaunchOnStartupTask::Run() {
51  // TODO(rickcam): Bug 53597: Make RegKey mockable.
52  // TODO(rickcam): Bug 53600: Use distinct registry keys per flavor+profile.
53  const wchar_t* key_name = kBackgroundModeRegistryKeyName;
54  base::win::RegKey read_key(kBackgroundModeRegistryRootKey,
55                             kBackgroundModeRegistrySubkey, KEY_READ);
56  base::win::RegKey write_key(kBackgroundModeRegistryRootKey,
57                              kBackgroundModeRegistrySubkey, KEY_WRITE);
58  FilePath executable;
59  if (!PathService::Get(base::FILE_EXE, &executable))
60    return;
61  std::wstring new_value = executable.value() +
62      L" --" + ASCIIToUTF16(switches::kNoStartupWindow);
63  if (read_key.ValueExists(key_name)) {
64    std::wstring current_value;
65    if ((read_key.ReadValue(key_name, &current_value) == ERROR_SUCCESS) &&
66        (current_value == new_value)) {
67      return;
68    }
69  }
70  LONG result = write_key.WriteValue(key_name, new_value.c_str());
71  DCHECK_EQ(ERROR_SUCCESS, result) <<
72      "Failed to register launch on login. error: " << result;
73}
74
75}  // namespace
76
77void BackgroundModeManager::EnableLaunchOnStartup(bool should_launch) {
78  // This functionality is only defined for default profile, currently.
79  if (CommandLine::ForCurrentProcess()->HasSwitch(switches::kUserDataDir))
80    return;
81  if (should_launch) {
82    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
83                            new EnableLaunchOnStartupTask());
84  } else {
85    BrowserThread::PostTask(BrowserThread::FILE, FROM_HERE,
86                            new DisableLaunchOnStartupTask());
87  }
88}
89
90void BackgroundModeManager::DisplayAppInstalledNotification(
91    const Extension* extension) {
92  // Create a status tray notification balloon explaining to the user that
93  // a background app has been installed.
94  CreateStatusTrayIcon();
95  status_icon_->DisplayBalloon(
96      l10n_util::GetStringUTF16(IDS_BACKGROUND_APP_INSTALLED_BALLOON_TITLE),
97      l10n_util::GetStringFUTF16(
98          IDS_BACKGROUND_APP_INSTALLED_BALLOON_BODY,
99          UTF8ToUTF16(extension->name()),
100          l10n_util::GetStringUTF16(IDS_PRODUCT_NAME)));
101}
102
103string16 BackgroundModeManager::GetPreferencesMenuLabel() {
104  return l10n_util::GetStringUTF16(IDS_OPTIONS);
105}
106