Roll src/third_party/WebKit 116cf7f:79abaa8 (svn 189234:189235)
[chromium-blink-merge.git] / chrome / installer / util / auto_launch_util.cc
blob08be65d79deb2cc186c50816433c9f3dad32348a
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.
5 #include "chrome/installer/util/auto_launch_util.h"
7 #include "base/command_line.h"
8 #include "base/files/file_path.h"
9 #include "base/logging.h"
10 #include "base/path_service.h"
11 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h"
13 #include "base/win/win_util.h"
14 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h"
17 #include "chrome/common/chrome_version_info.h"
18 #include "chrome/installer/util/util_constants.h"
19 #include "crypto/sha2.h"
21 using base::ASCIIToUTF16;
23 namespace auto_launch_util {
25 // The prefix of the Chrome Auto-launch key under the Run key.
26 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
28 // We use one Run key with flags specifying which feature we want to start up.
29 // When we change our Run key we need to specify what we want to do with each
30 // flag. This lists the possible actions we can take with the flags.
31 enum FlagSetting {
32 FLAG_DISABLE, // Disable the flag.
33 FLAG_ENABLE, // Enable the flag.
34 FLAG_PRESERVE, // Preserve the value that the flag has currently.
37 // A helper function that takes a |profile_directory| and builds a registry key
38 // name to use when deciding where to read/write the auto-launch value
39 // to/from. It takes into account the name of the profile (so that different
40 // installations of Chrome don't conflict, and so the in the future different
41 // profiles can be auto-launched (or not) separately).
42 base::string16 ProfileToKeyName(const base::string16& profile_directory) {
43 base::FilePath path;
44 const bool success = PathService::Get(chrome::DIR_USER_DATA, &path);
45 DCHECK(success);
46 path = path.Append(profile_directory);
48 std::string input(path.AsUTF8Unsafe());
49 uint8 hash[16];
50 crypto::SHA256HashString(input, hash, sizeof(hash));
51 std::string hash_string = base::HexEncode(hash, sizeof(hash));
52 return base::string16(kAutolaunchKeyValue) + ASCIIToUTF16("_") +
53 ASCIIToUTF16(hash_string);
56 // Returns whether the Chrome executable specified in |application_path| is set
57 // to auto-launch at computer startup with a given |command_line_switch|.
58 // NOTE: |application_path| is optional and should be blank in most cases (as
59 // it will default to the application path of the current executable).
60 // |profile_directory| is the name of the directory (leaf, not the full path)
61 // that contains the profile that should be opened at computer startup.
62 // |command_line_switch| is the switch we are optionally interested in and, if
63 // not blank, must be present for the function to return true. If blank, it acts
64 // like a wildcard.
65 bool WillLaunchAtLoginWithSwitch(const base::FilePath& application_path,
66 const base::string16& profile_directory,
67 const std::string& command_line_switch) {
68 base::string16 key_name(ProfileToKeyName(profile_directory));
69 base::string16 autolaunch;
70 if (!base::win::ReadCommandFromAutoRun(
71 HKEY_CURRENT_USER, key_name, &autolaunch)) {
72 return false;
75 base::FilePath chrome_exe(application_path);
76 if (chrome_exe.empty()) {
77 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) {
78 NOTREACHED();
79 return false;
82 chrome_exe = chrome_exe.Append(installer::kChromeExe);
84 if (autolaunch.find(chrome_exe.value()) == base::string16::npos)
85 return false;
87 return command_line_switch.empty() ||
88 autolaunch.find(ASCIIToUTF16(command_line_switch)) !=
89 base::string16::npos;
92 bool AutoStartRequested(const base::string16& profile_directory,
93 bool window_requested,
94 const base::FilePath& application_path) {
95 if (window_requested) {
96 return WillLaunchAtLoginWithSwitch(application_path,
97 profile_directory,
98 switches::kAutoLaunchAtStartup);
99 } else {
100 // Background mode isn't profile specific, but is attached to the Run key
101 // for the Default profile.
102 return WillLaunchAtLoginWithSwitch(application_path,
103 ASCIIToUTF16(chrome::kInitialProfile),
104 switches::kNoStartupWindow);
108 bool CheckAndRemoveDeprecatedBackgroundModeSwitch() {
109 // For backwards compatibility we need to provide a migration path from the
110 // previously used key "chromium" that the BackgroundMode used to set, as it
111 // is incompatible with the new key (can't have two Run keys with
112 // conflicting switches).
113 base::string16 chromium = ASCIIToUTF16("chromium");
114 base::string16 value;
115 if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) {
116 if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) !=
117 base::string16::npos) {
118 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium);
119 return true;
123 return false;
126 void SetWillLaunchAtLogin(const base::FilePath& application_path,
127 const base::string16& profile_directory,
128 FlagSetting foreground_mode,
129 FlagSetting background_mode) {
130 if (CheckAndRemoveDeprecatedBackgroundModeSwitch()) {
131 // We've found the deprecated switch, we must migrate it (unless background
132 // mode is being turned off).
133 if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) &&
134 background_mode == FLAG_PRESERVE) {
135 // Preserve in this case also covers the deprecated value, so we must
136 // explicitly turn the flag on and the rest will be taken care of below.
137 background_mode = FLAG_ENABLE;
138 } else {
139 // When we add support for multiple profiles for foreground mode we need
140 // to think about where to store the background mode switch. I think we
141 // need to store it with the Default profile (call SetWillLaunchAtLogin
142 // again specifying the Default profile), but concerns were raised in
143 // review.
144 NOTREACHED();
147 base::string16 key_name(ProfileToKeyName(profile_directory));
149 // Check which feature should be enabled.
150 bool in_foreground =
151 foreground_mode == FLAG_ENABLE ||
152 (foreground_mode == FLAG_PRESERVE &&
153 WillLaunchAtLoginWithSwitch(application_path,
154 profile_directory,
155 switches::kAutoLaunchAtStartup));
156 bool in_background =
157 background_mode == FLAG_ENABLE ||
158 (background_mode == FLAG_PRESERVE &&
159 WillLaunchAtLoginWithSwitch(application_path,
160 profile_directory,
161 switches::kNoStartupWindow));
163 // TODO(finnur): Convert this into a shortcut, instead of using the Run key.
164 if (in_foreground || in_background) {
165 base::FilePath path(application_path);
166 if (path.empty()) {
167 if (!PathService::Get(base::DIR_EXE, &path)) {
168 NOTREACHED();
169 return;
172 base::string16 cmd_line = ASCIIToUTF16("\"");
173 cmd_line += path.value();
174 cmd_line += ASCIIToUTF16("\\");
175 cmd_line += installer::kChromeExe;
176 cmd_line += ASCIIToUTF16("\"");
178 if (in_background) {
179 cmd_line += ASCIIToUTF16(" --");
180 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
182 if (in_foreground) {
183 cmd_line += ASCIIToUTF16(" --");
184 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
186 const base::CommandLine& command_line =
187 *base::CommandLine::ForCurrentProcess();
188 if (command_line.HasSwitch(switches::kUserDataDir)) {
189 cmd_line += ASCIIToUTF16(" --");
190 cmd_line += ASCIIToUTF16(switches::kUserDataDir);
191 cmd_line += ASCIIToUTF16("=\"");
192 cmd_line +=
193 command_line.GetSwitchValuePath(switches::kUserDataDir).value();
194 cmd_line += ASCIIToUTF16("\"");
197 cmd_line += ASCIIToUTF16(" --");
198 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
199 cmd_line += ASCIIToUTF16("=\"");
200 cmd_line += profile_directory;
201 cmd_line += ASCIIToUTF16("\"");
204 base::win::AddCommandToAutoRun(HKEY_CURRENT_USER, key_name, cmd_line);
205 } else {
206 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
210 void DisableAllAutoStartFeatures(const base::string16& profile_directory) {
211 DisableForegroundStartAtLogin(profile_directory);
212 DisableBackgroundStartAtLogin();
215 void EnableForegroundStartAtLogin(const base::string16& profile_directory,
216 const base::FilePath& application_path) {
217 SetWillLaunchAtLogin(
218 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE);
221 void DisableForegroundStartAtLogin(const base::string16& profile_directory) {
222 SetWillLaunchAtLogin(
223 base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
226 void EnableBackgroundStartAtLogin() {
227 // Background mode isn't profile specific, but we specify the Default profile
228 // just to have a unique Run key to attach it to. FilePath is blank because
229 // this function is not called from the installer (see comments for
230 // EnableAutoStartAtLogin).
231 SetWillLaunchAtLogin(base::FilePath(),
232 ASCIIToUTF16(chrome::kInitialProfile),
233 FLAG_PRESERVE,
234 FLAG_ENABLE);
237 void DisableBackgroundStartAtLogin() {
238 SetWillLaunchAtLogin(base::FilePath(),
239 ASCIIToUTF16(chrome::kInitialProfile),
240 FLAG_PRESERVE,
241 FLAG_DISABLE);
244 } // namespace auto_launch_util