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.
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
) {
44 const bool success
= PathService::Get(chrome::DIR_USER_DATA
, &path
);
46 path
= path
.Append(profile_directory
);
48 std::string
input(path
.AsUTF8Unsafe());
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
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
)) {
75 base::FilePath
chrome_exe(application_path
);
76 if (chrome_exe
.empty()) {
77 if (!PathService::Get(base::DIR_EXE
, &chrome_exe
)) {
82 chrome_exe
= chrome_exe
.Append(installer::kChromeExe
);
84 if (autolaunch
.find(chrome_exe
.value()) == base::string16::npos
)
87 return command_line_switch
.empty() ||
88 autolaunch
.find(ASCIIToUTF16(command_line_switch
)) !=
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
,
98 switches::kAutoLaunchAtStartup
);
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
);
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
;
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
147 base::string16
key_name(ProfileToKeyName(profile_directory
));
149 // Check which feature should be enabled.
151 foreground_mode
== FLAG_ENABLE
||
152 (foreground_mode
== FLAG_PRESERVE
&&
153 WillLaunchAtLoginWithSwitch(application_path
,
155 switches::kAutoLaunchAtStartup
));
157 background_mode
== FLAG_ENABLE
||
158 (background_mode
== FLAG_PRESERVE
&&
159 WillLaunchAtLoginWithSwitch(application_path
,
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
);
167 if (!PathService::Get(base::DIR_EXE
, &path
)) {
172 base::string16 cmd_line
= ASCIIToUTF16("\"");
173 cmd_line
+= path
.value();
174 cmd_line
+= ASCIIToUTF16("\\");
175 cmd_line
+= installer::kChromeExe
;
176 cmd_line
+= ASCIIToUTF16("\"");
179 cmd_line
+= ASCIIToUTF16(" --");
180 cmd_line
+= ASCIIToUTF16(switches::kNoStartupWindow
);
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("=\"");
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
);
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
),
237 void DisableBackgroundStartAtLogin() {
238 SetWillLaunchAtLogin(base::FilePath(),
239 ASCIIToUTF16(chrome::kInitialProfile
),
244 } // namespace auto_launch_util