Add new certificateProvider extension API.
[chromium-blink-merge.git] / chrome / installer / util / auto_launch_util.cc
blob2c1081a10c5300b9eb4ce3f0cae2014bbb81a80a
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/installer/util/util_constants.h"
18 #include "crypto/sha2.h"
20 using base::ASCIIToUTF16;
22 namespace auto_launch_util {
24 // The prefix of the Chrome Auto-launch key under the Run key.
25 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
27 // We use one Run key with flags specifying which feature we want to start up.
28 // When we change our Run key we need to specify what we want to do with each
29 // flag. This lists the possible actions we can take with the flags.
30 enum FlagSetting {
31 FLAG_DISABLE, // Disable the flag.
32 FLAG_ENABLE, // Enable the flag.
33 FLAG_PRESERVE, // Preserve the value that the flag has currently.
36 // A helper function that takes a |profile_directory| and builds a registry key
37 // name to use when deciding where to read/write the auto-launch value
38 // to/from. It takes into account the name of the profile (so that different
39 // installations of Chrome don't conflict, and so the in the future different
40 // profiles can be auto-launched (or not) separately).
41 base::string16 ProfileToKeyName(const base::string16& profile_directory) {
42 base::FilePath path;
43 const bool success = PathService::Get(chrome::DIR_USER_DATA, &path);
44 DCHECK(success);
45 path = path.Append(profile_directory);
47 std::string input(path.AsUTF8Unsafe());
48 uint8 hash[16];
49 crypto::SHA256HashString(input, hash, sizeof(hash));
50 std::string hash_string = base::HexEncode(hash, sizeof(hash));
51 return base::string16(kAutolaunchKeyValue) + ASCIIToUTF16("_") +
52 ASCIIToUTF16(hash_string);
55 // Returns whether the Chrome executable specified in |application_path| is set
56 // to auto-launch at computer startup with a given |command_line_switch|.
57 // NOTE: |application_path| is optional and should be blank in most cases (as
58 // it will default to the application path of the current executable).
59 // |profile_directory| is the name of the directory (leaf, not the full path)
60 // that contains the profile that should be opened at computer startup.
61 // |command_line_switch| is the switch we are optionally interested in and, if
62 // not blank, must be present for the function to return true. If blank, it acts
63 // like a wildcard.
64 bool WillLaunchAtLoginWithSwitch(const base::FilePath& application_path,
65 const base::string16& profile_directory,
66 const std::string& command_line_switch) {
67 base::string16 key_name(ProfileToKeyName(profile_directory));
68 base::string16 autolaunch;
69 if (!base::win::ReadCommandFromAutoRun(
70 HKEY_CURRENT_USER, key_name, &autolaunch)) {
71 return false;
74 base::FilePath chrome_exe(application_path);
75 if (chrome_exe.empty()) {
76 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) {
77 NOTREACHED();
78 return false;
81 chrome_exe = chrome_exe.Append(installer::kChromeExe);
83 if (autolaunch.find(chrome_exe.value()) == base::string16::npos)
84 return false;
86 return command_line_switch.empty() ||
87 autolaunch.find(ASCIIToUTF16(command_line_switch)) !=
88 base::string16::npos;
91 bool AutoStartRequested(const base::string16& profile_directory,
92 bool window_requested,
93 const base::FilePath& application_path) {
94 if (window_requested) {
95 return WillLaunchAtLoginWithSwitch(application_path,
96 profile_directory,
97 switches::kAutoLaunchAtStartup);
98 } else {
99 // Background mode isn't profile specific, but is attached to the Run key
100 // for the Default profile.
101 return WillLaunchAtLoginWithSwitch(application_path,
102 ASCIIToUTF16(chrome::kInitialProfile),
103 switches::kNoStartupWindow);
107 bool CheckAndRemoveDeprecatedBackgroundModeSwitch() {
108 // For backwards compatibility we need to provide a migration path from the
109 // previously used key "chromium" that the BackgroundMode used to set, as it
110 // is incompatible with the new key (can't have two Run keys with
111 // conflicting switches).
112 base::string16 chromium = ASCIIToUTF16("chromium");
113 base::string16 value;
114 if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) {
115 if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) !=
116 base::string16::npos) {
117 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium);
118 return true;
122 return false;
125 void SetWillLaunchAtLogin(const base::FilePath& application_path,
126 const base::string16& profile_directory,
127 FlagSetting foreground_mode,
128 FlagSetting background_mode) {
129 if (CheckAndRemoveDeprecatedBackgroundModeSwitch()) {
130 // We've found the deprecated switch, we must migrate it (unless background
131 // mode is being turned off).
132 if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) &&
133 background_mode == FLAG_PRESERVE) {
134 // Preserve in this case also covers the deprecated value, so we must
135 // explicitly turn the flag on and the rest will be taken care of below.
136 background_mode = FLAG_ENABLE;
137 } else {
138 // When we add support for multiple profiles for foreground mode we need
139 // to think about where to store the background mode switch. I think we
140 // need to store it with the Default profile (call SetWillLaunchAtLogin
141 // again specifying the Default profile), but concerns were raised in
142 // review.
143 NOTREACHED();
146 base::string16 key_name(ProfileToKeyName(profile_directory));
148 // Check which feature should be enabled.
149 bool in_foreground =
150 foreground_mode == FLAG_ENABLE ||
151 (foreground_mode == FLAG_PRESERVE &&
152 WillLaunchAtLoginWithSwitch(application_path,
153 profile_directory,
154 switches::kAutoLaunchAtStartup));
155 bool in_background =
156 background_mode == FLAG_ENABLE ||
157 (background_mode == FLAG_PRESERVE &&
158 WillLaunchAtLoginWithSwitch(application_path,
159 profile_directory,
160 switches::kNoStartupWindow));
162 // TODO(finnur): Convert this into a shortcut, instead of using the Run key.
163 if (in_foreground || in_background) {
164 base::FilePath path(application_path);
165 if (path.empty()) {
166 if (!PathService::Get(base::DIR_EXE, &path)) {
167 NOTREACHED();
168 return;
171 base::string16 cmd_line = ASCIIToUTF16("\"");
172 cmd_line += path.value();
173 cmd_line += ASCIIToUTF16("\\");
174 cmd_line += installer::kChromeExe;
175 cmd_line += ASCIIToUTF16("\"");
177 if (in_background) {
178 cmd_line += ASCIIToUTF16(" --");
179 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
181 if (in_foreground) {
182 cmd_line += ASCIIToUTF16(" --");
183 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
185 const base::CommandLine& command_line =
186 *base::CommandLine::ForCurrentProcess();
188 // Propagate --user-data-dir if it was specified on the command line.
189 // Retrieve the value from the PathService since some sanitation may have
190 // taken place. There is no need to add it to the command line in the
191 // event that the dir was overridden by Group Policy since the GP override
192 // will be in force when Chrome is launched.
193 if (command_line.HasSwitch(switches::kUserDataDir)) {
194 cmd_line += ASCIIToUTF16(" --");
195 cmd_line += ASCIIToUTF16(switches::kUserDataDir);
196 cmd_line += ASCIIToUTF16("=\"");
197 base::FilePath user_data_dir;
198 PathService::Get(chrome::DIR_USER_DATA, &user_data_dir);
199 cmd_line += user_data_dir.value();
200 cmd_line += ASCIIToUTF16("\"");
203 cmd_line += ASCIIToUTF16(" --");
204 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
205 cmd_line += ASCIIToUTF16("=\"");
206 cmd_line += profile_directory;
207 cmd_line += ASCIIToUTF16("\"");
210 base::win::AddCommandToAutoRun(HKEY_CURRENT_USER, key_name, cmd_line);
211 } else {
212 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
216 void DisableAllAutoStartFeatures(const base::string16& profile_directory) {
217 DisableForegroundStartAtLogin(profile_directory);
218 DisableBackgroundStartAtLogin();
221 void EnableForegroundStartAtLogin(const base::string16& profile_directory,
222 const base::FilePath& application_path) {
223 SetWillLaunchAtLogin(
224 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE);
227 void DisableForegroundStartAtLogin(const base::string16& profile_directory) {
228 SetWillLaunchAtLogin(
229 base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
232 void EnableBackgroundStartAtLogin() {
233 // Background mode isn't profile specific, but we specify the Default profile
234 // just to have a unique Run key to attach it to. FilePath is blank because
235 // this function is not called from the installer (see comments for
236 // EnableAutoStartAtLogin).
237 SetWillLaunchAtLogin(base::FilePath(),
238 ASCIIToUTF16(chrome::kInitialProfile),
239 FLAG_PRESERVE,
240 FLAG_ENABLE);
243 void DisableBackgroundStartAtLogin() {
244 SetWillLaunchAtLogin(base::FilePath(),
245 ASCIIToUTF16(chrome::kInitialProfile),
246 FLAG_PRESERVE,
247 FLAG_DISABLE);
250 } // namespace auto_launch_util