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

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

Issue 9972012: Resolve the conflict that auto-launch has with the background mode feature (part 1). (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src/
Patch Set: gclient sync, ignore Created 8 years, 8 months 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 | Annotate | Revision Log
« no previous file with comments | « chrome/installer/util/auto_launch_util.h ('k') | no next file » | no next file with comments »
Toggle Intra-line Diffs ('i') | Expand Comments ('e') | Collapse Comments ('c') | Show Comments Hide Comments ('s')
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 "base/command_line.h" 7 #include "base/command_line.h"
8 #include "base/file_path.h" 8 #include "base/file_path.h"
9 #include "base/logging.h" 9 #include "base/logging.h"
10 #include "base/path_service.h" 10 #include "base/path_service.h"
11 #include "base/string_number_conversions.h" 11 #include "base/string_number_conversions.h"
12 #include "base/utf_string_conversions.h" 12 #include "base/utf_string_conversions.h"
13 #include "base/win/win_util.h" 13 #include "base/win/win_util.h"
14 #include "chrome/common/chrome_constants.h"
14 #include "chrome/common/chrome_switches.h" 15 #include "chrome/common/chrome_switches.h"
15 #include "chrome/common/chrome_version_info.h" 16 #include "chrome/common/chrome_version_info.h"
16 #include "chrome/installer/util/browser_distribution.h" 17 #include "chrome/installer/util/browser_distribution.h"
17 #include "chrome/installer/util/product.h" 18 #include "chrome/installer/util/product.h"
18 #include "chrome/installer/util/util_constants.h" 19 #include "chrome/installer/util/util_constants.h"
19 #include "crypto/sha2.h" 20 #include "crypto/sha2.h"
20 21
21 namespace auto_launch_util { 22 namespace auto_launch_util {
22 23
23 // The prefix of the Chrome Auto-launch key under the Run key. 24 // The prefix of the Chrome Auto-launch key under the Run key.
24 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch"; 25 const wchar_t kAutolaunchKeyValue[] = L"GoogleChromeAutoLaunch";
25 26
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.
34 };
35
26 // A helper function that takes a |profile_path| and builds a registry key 36 // A helper function that takes a |profile_path| and builds a registry key
27 // name to use when deciding where to read/write the auto-launch value 37 // name to use when deciding where to read/write the auto-launch value
28 // to/from. It takes into account the name of the profile (so that different 38 // to/from. It takes into account the name of the profile (so that different
29 // installations of Chrome don't conflict, and so the in the future different 39 // installations of Chrome don't conflict, and so the in the future different
30 // profiles can be auto-launched (or not) separately). 40 // profiles can be auto-launched (or not) separately).
31 string16 ProfileToKeyName(const string16& profile_directory) { 41 string16 ProfileToKeyName(const string16& profile_directory) {
32 FilePath path; 42 FilePath path;
33 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 43 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
34 if (command_line.HasSwitch(switches::kUserDataDir)) { 44 if (command_line.HasSwitch(switches::kUserDataDir)) {
35 path = command_line.GetSwitchValuePath(switches::kUserDataDir); 45 path = command_line.GetSwitchValuePath(switches::kUserDataDir);
36 } else { 46 } else {
37 // Get the path from the same source as the installer, to make sure there 47 // Get the path from the same source as the installer, to make sure there
38 // are no differences. 48 // are no differences.
39 BrowserDistribution* distribution = 49 BrowserDistribution* distribution =
40 BrowserDistribution::GetSpecificDistribution( 50 BrowserDistribution::GetSpecificDistribution(
41 BrowserDistribution::CHROME_BROWSER); 51 BrowserDistribution::CHROME_BROWSER);
42 installer::Product product(distribution); 52 installer::Product product(distribution);
43 path = product.GetUserDataPath(); 53 path = product.GetUserDataPath();
44 } 54 }
45 path = path.Append(profile_directory); 55 path = path.Append(profile_directory);
46 56
47 std::string input(path.AsUTF8Unsafe()); 57 std::string input(path.AsUTF8Unsafe());
48 uint8 hash[16]; 58 uint8 hash[16];
49 crypto::SHA256HashString(input, hash, sizeof(hash)); 59 crypto::SHA256HashString(input, hash, sizeof(hash));
50 std::string hash_string = base::HexEncode(hash, sizeof(hash)); 60 std::string hash_string = base::HexEncode(hash, sizeof(hash));
51 return string16(kAutolaunchKeyValue) + 61 return string16(kAutolaunchKeyValue) +
52 ASCIIToWide("_") + ASCIIToWide(hash_string); 62 ASCIIToWide("_") + ASCIIToWide(hash_string);
53 } 63 }
54 64
55 bool WillLaunchAtLogin(const FilePath& application_path, 65 // Returns whether the Chrome executable specified in |application_path| is set
56 const string16& profile_directory) { 66 // to auto-launch at computer startup with a given |command_line_switch|.
67 // NOTE: |application_path| is optional and should be blank in most cases (as
68 // it will default to the application path of the current executable).
69 // |profile_directory| is the name of the directory (leaf, not the full path)
70 // that contains the profile that should be opened at computer startup.
71 // |command_line_switch| is the switch we are optionally interested in and, if
72 // not blank, must be present for the function to return true. If blank, it acts
73 // like a wildcard.
74 bool WillLaunchAtLoginWithSwitch(const FilePath& application_path,
75 const string16& profile_directory,
76 const std::string& command_line_switch) {
57 string16 key_name(ProfileToKeyName(profile_directory)); 77 string16 key_name(ProfileToKeyName(profile_directory));
58 string16 autolaunch; 78 string16 autolaunch;
59 if (!base::win::ReadCommandFromAutoRun( 79 if (!base::win::ReadCommandFromAutoRun(
60 HKEY_CURRENT_USER, key_name, &autolaunch)) { 80 HKEY_CURRENT_USER, key_name, &autolaunch)) {
61 return false; 81 return false;
62 } 82 }
63 83
64 FilePath chrome_exe(application_path); 84 FilePath chrome_exe(application_path);
65 if (chrome_exe.empty()) { 85 if (chrome_exe.empty()) {
66 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { 86 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) {
67 NOTREACHED(); 87 NOTREACHED();
68 return false; 88 return false;
69 } 89 }
70 } 90 }
71 chrome_exe = chrome_exe.Append(installer::kChromeExe); 91 chrome_exe = chrome_exe.Append(installer::kChromeExe);
72 92
73 return autolaunch.find(chrome_exe.value()) != string16::npos; 93 if (autolaunch.find(chrome_exe.value()) == string16::npos)
94 return false;
95
96 return command_line_switch.empty() ||
97 autolaunch.find(ASCIIToUTF16(command_line_switch)) != string16::npos;
74 } 98 }
75 99
76 void SetWillLaunchAtLogin(bool auto_launch, 100 bool AutoStartRequested(const string16& profile_directory,
77 const FilePath& application_path, 101 bool window_requested,
78 const string16& profile_directory) { 102 const FilePath& application_path) {
103 if (window_requested) {
104 return WillLaunchAtLoginWithSwitch(application_path,
105 profile_directory,
106 switches::kAutoLaunchAtStartup);
107 } else {
108 // Background mode isn't profile specific, but is attached to the Run key
109 // for the Default profile.
110 return WillLaunchAtLoginWithSwitch(application_path,
111 ASCIIToUTF16(chrome::kInitialProfile),
112 switches::kNoStartupWindow);
113 }
114 }
115
116 bool HasDeprecatedBackgroundModeSwitch() {
Andrew T Wilson (Slow) 2012/04/18 23:07:51 I think this should probably be called RemoveDepre
Finnur 2012/04/19 17:53:51 Good idea. How about: CheckAndRemoveDeprecatedBac
117 // For backwards compatibility we need to provide a migration path from the
118 // previously used key "chromium" that the BackgroundMode used to set, as it
119 // is incompatible with the new key (can't have two Run keys with
120 // conflicting switches).
121 string16 chromium = ASCIIToUTF16("chromium");
122 string16 value;
123 if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) {
124 if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) !=
125 string16::npos) {
126 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium);
127 return true;
128 }
129 }
130
131 return false;
132 }
133
134 void SetWillLaunchAtLogin(const FilePath& application_path,
135 const string16& profile_directory,
136 FlagSetting foreground_mode,
137 FlagSetting background_mode) {
138 if (HasDeprecatedBackgroundModeSwitch()) {
139 // We've found the deprecated switch, we must migrate it (unless background
140 // mode is being turned off).
141 if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) &&
142 background_mode == FLAG_PRESERVE) {
143 // Preserve in this case also covers the deprecated value, so we must
144 // explicitly turn the flag on and the rest will be taken care of below.
145 background_mode = FLAG_ENABLE;
146 } else {
147 // Since the background mode is stored with the Default profile Run key
148 // we need to make an extra call to SetWillLaunchAtLogin.
Andrew T Wilson (Slow) 2012/04/18 23:07:51 What case does this cover? So, if someone passes i
Finnur 2012/04/19 17:53:51 Yes, this is the "profile_directory != kInitialPro
Andrew T Wilson (Slow) 2012/04/20 00:30:13 Let's put a NOTREACHED here since I don't think th
149 SetWillLaunchAtLogin(application_path,
150 ASCIIToUTF16(chrome::kInitialProfile),
151 FLAG_PRESERVE, // Foreground mode.
152 FLAG_ENABLE); // Background mode.
153 }
154 }
79 string16 key_name(ProfileToKeyName(profile_directory)); 155 string16 key_name(ProfileToKeyName(profile_directory));
80 156
157 // Check which feature should be enabled.
158 bool in_foreground =
159 foreground_mode == FLAG_ENABLE ||
160 (foreground_mode == FLAG_PRESERVE &&
161 WillLaunchAtLoginWithSwitch(application_path,
162 profile_directory,
163 switches::kAutoLaunchAtStartup));
164 bool in_background =
165 background_mode == FLAG_ENABLE ||
166 (background_mode == FLAG_PRESERVE &&
167 WillLaunchAtLoginWithSwitch(application_path,
168 profile_directory,
169 switches::kNoStartupWindow));
170
81 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. 171 // TODO(finnur): Convert this into a shortcut, instead of using the Run key.
82 if (auto_launch) { 172 if (in_foreground || in_background) {
83 FilePath path(application_path); 173 FilePath path(application_path);
84 if (path.empty()) { 174 if (path.empty()) {
85 if (!PathService::Get(base::DIR_EXE, &path)) { 175 if (!PathService::Get(base::DIR_EXE, &path)) {
86 NOTREACHED(); 176 NOTREACHED();
87 return; 177 return;
88 } 178 }
89 } 179 }
90 string16 cmd_line = ASCIIToUTF16("\""); 180 string16 cmd_line = ASCIIToUTF16("\"");
91 cmd_line += path.value(); 181 cmd_line += path.value();
92 cmd_line += ASCIIToUTF16("\\"); 182 cmd_line += ASCIIToUTF16("\\");
93 cmd_line += installer::kChromeExe; 183 cmd_line += installer::kChromeExe;
94 cmd_line += ASCIIToUTF16("\" --"); 184 cmd_line += ASCIIToUTF16("\"");
95 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
96 185
97 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); 186 if (in_background) {
98 if (command_line.HasSwitch(switches::kUserDataDir)) {
99 cmd_line += ASCIIToUTF16(" --"); 187 cmd_line += ASCIIToUTF16(" --");
100 cmd_line += ASCIIToUTF16(switches::kUserDataDir); 188 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow);
189 }
190 if (in_foreground) {
191 cmd_line += ASCIIToUTF16(" --");
192 cmd_line += ASCIIToUTF16(switches::kAutoLaunchAtStartup);
193
194 const CommandLine& command_line = *CommandLine::ForCurrentProcess();
195 if (command_line.HasSwitch(switches::kUserDataDir)) {
196 cmd_line += ASCIIToUTF16(" --");
197 cmd_line += ASCIIToUTF16(switches::kUserDataDir);
198 cmd_line += ASCIIToUTF16("=\"");
199 cmd_line +=
200 command_line.GetSwitchValuePath(switches::kUserDataDir).value();
201 cmd_line += ASCIIToUTF16("\"");
202 }
203
204 cmd_line += ASCIIToUTF16(" --");
205 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
101 cmd_line += ASCIIToUTF16("=\""); 206 cmd_line += ASCIIToUTF16("=\"");
102 cmd_line += 207 cmd_line += profile_directory;
103 command_line.GetSwitchValuePath(switches::kUserDataDir).value();
104 cmd_line += ASCIIToUTF16("\""); 208 cmd_line += ASCIIToUTF16("\"");
105 } 209 }
106 210
107 cmd_line += ASCIIToUTF16(" --");
108 cmd_line += ASCIIToUTF16(switches::kProfileDirectory);
109 cmd_line += ASCIIToUTF16("=\"");
110 cmd_line += profile_directory;
111 cmd_line += ASCIIToUTF16("\"");
112
113 base::win::AddCommandToAutoRun( 211 base::win::AddCommandToAutoRun(
114 HKEY_CURRENT_USER, key_name, cmd_line); 212 HKEY_CURRENT_USER, key_name, cmd_line);
115 } else { 213 } else {
116 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); 214 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name);
117 } 215 }
118 } 216 }
119 217
218 void DisableAllAutoStartFeatures(const string16& profile_directory) {
219 DisableForegroundStartAtLogin(profile_directory);
220 DisableBackgroundStartAtLogin();
221 }
222
223 void EnableForegroundStartAtLogin(const string16& profile_directory,
224 const FilePath& application_path) {
225 SetWillLaunchAtLogin(
226 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE);
227 }
228
229 void DisableForegroundStartAtLogin(const string16& profile_directory) {
230 SetWillLaunchAtLogin(
231 FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE);
232 }
233
234 void EnableBackgroundStartAtLogin() {
235 // Background mode isn't profile specific, but we specify the Default profile
236 // just to have a unique Run key to attach it to. FilePath is blank because
237 // this function is not called from the installer (see comments for
238 // EnableAutoStartAtLogin).
239 SetWillLaunchAtLogin(FilePath(),
240 ASCIIToUTF16(chrome::kInitialProfile),
241 FLAG_PRESERVE,
242 FLAG_ENABLE);
243 }
244
245 void DisableBackgroundStartAtLogin() {
246 SetWillLaunchAtLogin(FilePath(),
247 ASCIIToUTF16(chrome::kInitialProfile),
248 FLAG_PRESERVE,
249 FLAG_DISABLE);
250 }
251
120 } // namespace auto_launch_util 252 } // namespace auto_launch_util
OLDNEW
« no previous file with comments | « chrome/installer/util/auto_launch_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698