Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(49)

Side by Side Diff: chrome/installer/util/auto_launch_util.cc

Issue 1439803002: Remove logic for unused foreground auto-launch mode on Windows. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@d1_rm_auto_launch_trial
Patch Set: Created 5 years, 1 month ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 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 2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file. 3 // found in the LICENSE file.
4 4
5 #include "chrome/installer/util/auto_launch_util.h" 5 #include "chrome/installer/util/auto_launch_util.h"
6 6
7 #include <string>
8
9 #include "base/basictypes.h"
7 #include "base/command_line.h" 10 #include "base/command_line.h"
8 #include "base/files/file_path.h" 11 #include "base/files/file_path.h"
9 #include "base/logging.h" 12 #include "base/logging.h"
10 #include "base/path_service.h" 13 #include "base/path_service.h"
14 #include "base/strings/string16.h"
11 #include "base/strings/string_number_conversions.h" 15 #include "base/strings/string_number_conversions.h"
12 #include "base/strings/utf_string_conversions.h" 16 #include "base/strings/utf_string_conversions.h"
13 #include "base/win/win_util.h" 17 #include "base/win/win_util.h"
14 #include "chrome/common/chrome_constants.h" 18 #include "chrome/common/chrome_constants.h"
15 #include "chrome/common/chrome_paths.h" 19 #include "chrome/common/chrome_paths.h"
16 #include "chrome/common/chrome_switches.h" 20 #include "chrome/common/chrome_switches.h"
17 #include "chrome/installer/util/util_constants.h"
18 #include "crypto/sha2.h" 21 #include "crypto/sha2.h"
19 22
20 using base::ASCIIToUTF16;
21
22 namespace auto_launch_util { 23 namespace auto_launch_util {
23 24
24 // The prefix of the Chrome Auto-launch key under the Run key. 25 // The prefix of the Chrome Auto-launch key under the Run key.
grt (UTC plus 2) 2015/11/12 17:49:02 nit: put this and GetAutoLaunchKeyName() into the
gab 2015/11/12 18:44:26 Done.
25 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; 26 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
26 27
27 // We use one Run key with flags specifying which feature we want to start up. 28 // Builds a registry key name to use when deciding where to read/write the auto-
28 // When we change our Run key we need to specify what we want to do with each 29 // launch value to/from. It takes into account the path of the profile so that
29 // flag. This lists the possible actions we can take with the flags. 30 // different installations of Chrome don't conflict.
30 enum FlagSetting { 31 base::string16 GetAutoLaunchKeyName() {
31 FLAG_DISABLE, // Disable the flag.
32 FLAG_ENABLE, // Enable the flag.
33 FLAG_PRESERVE, // Preserve the value that the flag has currently.
34 };
35
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; 32 base::FilePath path;
43 const bool success = PathService::Get(chrome::DIR_USER_DATA, &path); 33 if (!PathService::Get(chrome::DIR_USER_DATA, &path))
44 DCHECK(success); 34 NOTREACHED();
45 path = path.Append(profile_directory); 35 // Background auto-launch is only supported for the Default profile at the
36 // moment, but keep the door opened to a multi-profile implementation by
37 // encoding the Default profile in the hash.
38 path = path.AppendASCII(chrome::kInitialProfile);
46 39
47 std::string input(path.AsUTF8Unsafe()); 40 std::string input(path.AsUTF8Unsafe());
48 uint8 hash[16]; 41 uint8 hash[16];
grt (UTC plus 2) 2015/11/12 17:49:02 nit: uint8_t and #include <stdint.h>
gab 2015/11/12 18:44:26 Done.
49 crypto::SHA256HashString(input, hash, sizeof(hash)); 42 crypto::SHA256HashString(input, hash, sizeof(hash));
grt (UTC plus 2) 2015/11/12 17:49:02 sizeof -> arraysize and #include "base/macros.h"
gab 2015/11/12 18:44:26 Done.
50 std::string hash_string = base::HexEncode(hash, sizeof(hash)); 43 std::string hash_string = base::HexEncode(hash, sizeof(hash));
grt (UTC plus 2) 2015/11/12 17:49:02 nit: inline HexEncode below
gab 2015/11/12 18:44:26 Done.
51 return base::string16(kAutolaunchKeyValue) + ASCIIToUTF16("_") + 44 return base::string16(kAutolaunchKeyValue) + base::ASCIIToUTF16("_") +
52 ASCIIToUTF16(hash_string); 45 base::ASCIIToUTF16(hash_string);
53 }
54
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;
72 }
73
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;
79 }
80 }
81 chrome_exe = chrome_exe.Append(installer::kChromeExe);
82
83 if (autolaunch.find(chrome_exe.value()) == base::string16::npos)
84 return false;
85
86 return command_line_switch.empty() ||
87 autolaunch.find(ASCIIToUTF16(command_line_switch)) !=
88 base::string16::npos;
89 }
90
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);
104 }
105 }
106
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;
119 }
120 }
121
122 return false;
123 }
124
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();
144 }
145 }
146 base::string16 key_name(ProfileToKeyName(profile_directory));
147
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));
161
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;
169 }
170 }
171 base::string16 cmd_line = ASCIIToUTF16("\"");
172 cmd_line += path.value();
173 cmd_line += ASCIIToUTF16("\\");
174 cmd_line += installer::kChromeExe;
175 cmd_line += ASCIIToUTF16("\"");
176
177 if (in_background) {
178 cmd_line += ASCIIToUTF16(" --");
179 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
180 }
181 if (in_foreground) {
182 cmd_line += ASCIIToUTF16(" --");
183 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
184
185 const base::CommandLine& command_line =
186 *base::CommandLine::ForCurrentProcess();
187
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("\"");
201 }
202
203 cmd_line += ASCIIToUTF16(" --");
204 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
205 cmd_line += ASCIIToUTF16("=\"");
206 cmd_line += profile_directory;
207 cmd_line += ASCIIToUTF16("\"");
208 }
209
210 base::win::AddCommandToAutoRun(HKEY_CURRENT_USER, key_name, cmd_line);
211 } else {
212 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
213 }
214 }
215
216 void DisableAllAutoStartFeatures(const base::string16& profile_directory) {
217 DisableForegroundStartAtLogin(profile_directory);
218 DisableBackgroundStartAtLogin();
219 }
220
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);
225 }
226
227 void DisableForegroundStartAtLogin(const base::string16& profile_directory) {
228 SetWillLaunchAtLogin(
229 base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
230 } 46 }
231 47
232 void EnableBackgroundStartAtLogin() { 48 void EnableBackgroundStartAtLogin() {
233 // Background mode isn't profile specific, but we specify the Default profile 49 base::CommandLine cmd_line(
234 // just to have a unique Run key to attach it to. FilePath is blank because 50 base::CommandLine::ForCurrentProcess()->GetProgram());
grt (UTC plus 2) 2015/11/12 17:49:02 i think it's better to use DIR_EXE + kChromeExe as
gab 2015/11/12 18:44:26 Good point, done.
235 // this function is not called from the installer (see comments for 51 cmd_line.AppendSwitch(switches::kNoStartupWindow);
236 // EnableAutoStartAtLogin). 52 base::win::AddCommandToAutoRun(HKEY_CURRENT_USER, GetAutoLaunchKeyName(),
237 SetWillLaunchAtLogin(base::FilePath(), 53 cmd_line.GetCommandLineString());
238 ASCIIToUTF16(chrome::kInitialProfile),
239 FLAG_PRESERVE,
240 FLAG_ENABLE);
241 } 54 }
242 55
243 void DisableBackgroundStartAtLogin() { 56 void DisableBackgroundStartAtLogin() {
244 SetWillLaunchAtLogin(base::FilePath(), 57 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER,
245 ASCIIToUTF16(chrome::kInitialProfile), 58 GetAutoLaunchKeyName());
246 FLAG_PRESERVE,
247 FLAG_DISABLE);
248 } 59 }
249 60
250 } // namespace auto_launch_util 61 } // namespace auto_launch_util
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698