OLD | NEW |
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/files/file_path.h" | 8 #include "base/files/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" |
(...skipping 20 matching lines...) Expand all Loading... |
31 FLAG_DISABLE, // Disable the flag. | 31 FLAG_DISABLE, // Disable the flag. |
32 FLAG_ENABLE, // Enable the flag. | 32 FLAG_ENABLE, // Enable the flag. |
33 FLAG_PRESERVE, // Preserve the value that the flag has currently. | 33 FLAG_PRESERVE, // Preserve the value that the flag has currently. |
34 }; | 34 }; |
35 | 35 |
36 // 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 |
37 // 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 |
38 // 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 |
39 // 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 |
40 // profiles can be auto-launched (or not) separately). | 40 // profiles can be auto-launched (or not) separately). |
41 string16 ProfileToKeyName(const string16& profile_directory) { | 41 base::string16 ProfileToKeyName(const base::string16& profile_directory) { |
42 base::FilePath path; | 42 base::FilePath path; |
43 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); | 43 const CommandLine& command_line = *CommandLine::ForCurrentProcess(); |
44 if (command_line.HasSwitch(switches::kUserDataDir)) { | 44 if (command_line.HasSwitch(switches::kUserDataDir)) { |
45 path = command_line.GetSwitchValuePath(switches::kUserDataDir); | 45 path = command_line.GetSwitchValuePath(switches::kUserDataDir); |
46 } else { | 46 } else { |
47 // 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 |
48 // are no differences. | 48 // are no differences. |
49 BrowserDistribution* distribution = | 49 BrowserDistribution* distribution = |
50 BrowserDistribution::GetSpecificDistribution( | 50 BrowserDistribution::GetSpecificDistribution( |
51 BrowserDistribution::CHROME_BROWSER); | 51 BrowserDistribution::CHROME_BROWSER); |
52 installer::Product product(distribution); | 52 installer::Product product(distribution); |
53 std::vector<base::FilePath> data_dir_paths; | 53 std::vector<base::FilePath> data_dir_paths; |
54 product.GetUserDataPaths(&data_dir_paths); | 54 product.GetUserDataPaths(&data_dir_paths); |
55 if (!data_dir_paths.empty()) | 55 if (!data_dir_paths.empty()) |
56 path = data_dir_paths[0]; | 56 path = data_dir_paths[0]; |
57 } | 57 } |
58 path = path.Append(profile_directory); | 58 path = path.Append(profile_directory); |
59 | 59 |
60 std::string input(path.AsUTF8Unsafe()); | 60 std::string input(path.AsUTF8Unsafe()); |
61 uint8 hash[16]; | 61 uint8 hash[16]; |
62 crypto::SHA256HashString(input, hash, sizeof(hash)); | 62 crypto::SHA256HashString(input, hash, sizeof(hash)); |
63 std::string hash_string = base::HexEncode(hash, sizeof(hash)); | 63 std::string hash_string = base::HexEncode(hash, sizeof(hash)); |
64 return string16(kAutolaunchKeyValue) + | 64 return base::string16(kAutolaunchKeyValue) + ASCIIToWide("_") + |
65 ASCIIToWide("_") + ASCIIToWide(hash_string); | 65 ASCIIToWide(hash_string); |
66 } | 66 } |
67 | 67 |
68 // Returns whether the Chrome executable specified in |application_path| is set | 68 // Returns whether the Chrome executable specified in |application_path| is set |
69 // to auto-launch at computer startup with a given |command_line_switch|. | 69 // to auto-launch at computer startup with a given |command_line_switch|. |
70 // NOTE: |application_path| is optional and should be blank in most cases (as | 70 // NOTE: |application_path| is optional and should be blank in most cases (as |
71 // it will default to the application path of the current executable). | 71 // it will default to the application path of the current executable). |
72 // |profile_directory| is the name of the directory (leaf, not the full path) | 72 // |profile_directory| is the name of the directory (leaf, not the full path) |
73 // that contains the profile that should be opened at computer startup. | 73 // that contains the profile that should be opened at computer startup. |
74 // |command_line_switch| is the switch we are optionally interested in and, if | 74 // |command_line_switch| is the switch we are optionally interested in and, if |
75 // not blank, must be present for the function to return true. If blank, it acts | 75 // not blank, must be present for the function to return true. If blank, it acts |
76 // like a wildcard. | 76 // like a wildcard. |
77 bool WillLaunchAtLoginWithSwitch(const base::FilePath& application_path, | 77 bool WillLaunchAtLoginWithSwitch(const base::FilePath& application_path, |
78 const string16& profile_directory, | 78 const base::string16& profile_directory, |
79 const std::string& command_line_switch) { | 79 const std::string& command_line_switch) { |
80 string16 key_name(ProfileToKeyName(profile_directory)); | 80 base::string16 key_name(ProfileToKeyName(profile_directory)); |
81 string16 autolaunch; | 81 base::string16 autolaunch; |
82 if (!base::win::ReadCommandFromAutoRun( | 82 if (!base::win::ReadCommandFromAutoRun( |
83 HKEY_CURRENT_USER, key_name, &autolaunch)) { | 83 HKEY_CURRENT_USER, key_name, &autolaunch)) { |
84 return false; | 84 return false; |
85 } | 85 } |
86 | 86 |
87 base::FilePath chrome_exe(application_path); | 87 base::FilePath chrome_exe(application_path); |
88 if (chrome_exe.empty()) { | 88 if (chrome_exe.empty()) { |
89 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { | 89 if (!PathService::Get(base::DIR_EXE, &chrome_exe)) { |
90 NOTREACHED(); | 90 NOTREACHED(); |
91 return false; | 91 return false; |
92 } | 92 } |
93 } | 93 } |
94 chrome_exe = chrome_exe.Append(installer::kChromeExe); | 94 chrome_exe = chrome_exe.Append(installer::kChromeExe); |
95 | 95 |
96 if (autolaunch.find(chrome_exe.value()) == string16::npos) | 96 if (autolaunch.find(chrome_exe.value()) == base::string16::npos) |
97 return false; | 97 return false; |
98 | 98 |
99 return command_line_switch.empty() || | 99 return command_line_switch.empty() || |
100 autolaunch.find(ASCIIToUTF16(command_line_switch)) != string16::npos; | 100 autolaunch.find(ASCIIToUTF16(command_line_switch)) != |
| 101 base::string16::npos; |
101 } | 102 } |
102 | 103 |
103 bool AutoStartRequested(const string16& profile_directory, | 104 bool AutoStartRequested(const base::string16& profile_directory, |
104 bool window_requested, | 105 bool window_requested, |
105 const base::FilePath& application_path) { | 106 const base::FilePath& application_path) { |
106 if (window_requested) { | 107 if (window_requested) { |
107 return WillLaunchAtLoginWithSwitch(application_path, | 108 return WillLaunchAtLoginWithSwitch(application_path, |
108 profile_directory, | 109 profile_directory, |
109 switches::kAutoLaunchAtStartup); | 110 switches::kAutoLaunchAtStartup); |
110 } else { | 111 } else { |
111 // Background mode isn't profile specific, but is attached to the Run key | 112 // Background mode isn't profile specific, but is attached to the Run key |
112 // for the Default profile. | 113 // for the Default profile. |
113 return WillLaunchAtLoginWithSwitch(application_path, | 114 return WillLaunchAtLoginWithSwitch(application_path, |
114 ASCIIToUTF16(chrome::kInitialProfile), | 115 ASCIIToUTF16(chrome::kInitialProfile), |
115 switches::kNoStartupWindow); | 116 switches::kNoStartupWindow); |
116 } | 117 } |
117 } | 118 } |
118 | 119 |
119 bool CheckAndRemoveDeprecatedBackgroundModeSwitch() { | 120 bool CheckAndRemoveDeprecatedBackgroundModeSwitch() { |
120 // For backwards compatibility we need to provide a migration path from the | 121 // For backwards compatibility we need to provide a migration path from the |
121 // previously used key "chromium" that the BackgroundMode used to set, as it | 122 // previously used key "chromium" that the BackgroundMode used to set, as it |
122 // is incompatible with the new key (can't have two Run keys with | 123 // is incompatible with the new key (can't have two Run keys with |
123 // conflicting switches). | 124 // conflicting switches). |
124 string16 chromium = ASCIIToUTF16("chromium"); | 125 base::string16 chromium = ASCIIToUTF16("chromium"); |
125 string16 value; | 126 base::string16 value; |
126 if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) { | 127 if (base::win::ReadCommandFromAutoRun(HKEY_CURRENT_USER, chromium, &value)) { |
127 if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) != | 128 if (value.find(ASCIIToUTF16(switches::kNoStartupWindow)) != |
128 string16::npos) { | 129 base::string16::npos) { |
129 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium); | 130 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, chromium); |
130 return true; | 131 return true; |
131 } | 132 } |
132 } | 133 } |
133 | 134 |
134 return false; | 135 return false; |
135 } | 136 } |
136 | 137 |
137 void SetWillLaunchAtLogin(const base::FilePath& application_path, | 138 void SetWillLaunchAtLogin(const base::FilePath& application_path, |
138 const string16& profile_directory, | 139 const base::string16& profile_directory, |
139 FlagSetting foreground_mode, | 140 FlagSetting foreground_mode, |
140 FlagSetting background_mode) { | 141 FlagSetting background_mode) { |
141 if (CheckAndRemoveDeprecatedBackgroundModeSwitch()) { | 142 if (CheckAndRemoveDeprecatedBackgroundModeSwitch()) { |
142 // We've found the deprecated switch, we must migrate it (unless background | 143 // We've found the deprecated switch, we must migrate it (unless background |
143 // mode is being turned off). | 144 // mode is being turned off). |
144 if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) && | 145 if (profile_directory == ASCIIToUTF16(chrome::kInitialProfile) && |
145 background_mode == FLAG_PRESERVE) { | 146 background_mode == FLAG_PRESERVE) { |
146 // Preserve in this case also covers the deprecated value, so we must | 147 // Preserve in this case also covers the deprecated value, so we must |
147 // explicitly turn the flag on and the rest will be taken care of below. | 148 // explicitly turn the flag on and the rest will be taken care of below. |
148 background_mode = FLAG_ENABLE; | 149 background_mode = FLAG_ENABLE; |
149 } else { | 150 } else { |
150 // When we add support for multiple profiles for foreground mode we need | 151 // When we add support for multiple profiles for foreground mode we need |
151 // to think about where to store the background mode switch. I think we | 152 // to think about where to store the background mode switch. I think we |
152 // need to store it with the Default profile (call SetWillLaunchAtLogin | 153 // need to store it with the Default profile (call SetWillLaunchAtLogin |
153 // again specifying the Default profile), but concerns were raised in | 154 // again specifying the Default profile), but concerns were raised in |
154 // review. | 155 // review. |
155 NOTREACHED(); | 156 NOTREACHED(); |
156 } | 157 } |
157 } | 158 } |
158 string16 key_name(ProfileToKeyName(profile_directory)); | 159 base::string16 key_name(ProfileToKeyName(profile_directory)); |
159 | 160 |
160 // Check which feature should be enabled. | 161 // Check which feature should be enabled. |
161 bool in_foreground = | 162 bool in_foreground = |
162 foreground_mode == FLAG_ENABLE || | 163 foreground_mode == FLAG_ENABLE || |
163 (foreground_mode == FLAG_PRESERVE && | 164 (foreground_mode == FLAG_PRESERVE && |
164 WillLaunchAtLoginWithSwitch(application_path, | 165 WillLaunchAtLoginWithSwitch(application_path, |
165 profile_directory, | 166 profile_directory, |
166 switches::kAutoLaunchAtStartup)); | 167 switches::kAutoLaunchAtStartup)); |
167 bool in_background = | 168 bool in_background = |
168 background_mode == FLAG_ENABLE || | 169 background_mode == FLAG_ENABLE || |
169 (background_mode == FLAG_PRESERVE && | 170 (background_mode == FLAG_PRESERVE && |
170 WillLaunchAtLoginWithSwitch(application_path, | 171 WillLaunchAtLoginWithSwitch(application_path, |
171 profile_directory, | 172 profile_directory, |
172 switches::kNoStartupWindow)); | 173 switches::kNoStartupWindow)); |
173 | 174 |
174 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. | 175 // TODO(finnur): Convert this into a shortcut, instead of using the Run key. |
175 if (in_foreground || in_background) { | 176 if (in_foreground || in_background) { |
176 base::FilePath path(application_path); | 177 base::FilePath path(application_path); |
177 if (path.empty()) { | 178 if (path.empty()) { |
178 if (!PathService::Get(base::DIR_EXE, &path)) { | 179 if (!PathService::Get(base::DIR_EXE, &path)) { |
179 NOTREACHED(); | 180 NOTREACHED(); |
180 return; | 181 return; |
181 } | 182 } |
182 } | 183 } |
183 string16 cmd_line = ASCIIToUTF16("\""); | 184 base::string16 cmd_line = ASCIIToUTF16("\""); |
184 cmd_line += path.value(); | 185 cmd_line += path.value(); |
185 cmd_line += ASCIIToUTF16("\\"); | 186 cmd_line += ASCIIToUTF16("\\"); |
186 cmd_line += installer::kChromeExe; | 187 cmd_line += installer::kChromeExe; |
187 cmd_line += ASCIIToUTF16("\""); | 188 cmd_line += ASCIIToUTF16("\""); |
188 | 189 |
189 if (in_background) { | 190 if (in_background) { |
190 cmd_line += ASCIIToUTF16(" --"); | 191 cmd_line += ASCIIToUTF16(" --"); |
191 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow); | 192 cmd_line += ASCIIToUTF16(switches::kNoStartupWindow); |
192 } | 193 } |
193 if (in_foreground) { | 194 if (in_foreground) { |
(...skipping 17 matching lines...) Expand all Loading... |
211 cmd_line += ASCIIToUTF16("\""); | 212 cmd_line += ASCIIToUTF16("\""); |
212 } | 213 } |
213 | 214 |
214 base::win::AddCommandToAutoRun( | 215 base::win::AddCommandToAutoRun( |
215 HKEY_CURRENT_USER, key_name, cmd_line); | 216 HKEY_CURRENT_USER, key_name, cmd_line); |
216 } else { | 217 } else { |
217 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); | 218 base::win::RemoveCommandFromAutoRun(HKEY_CURRENT_USER, key_name); |
218 } | 219 } |
219 } | 220 } |
220 | 221 |
221 void DisableAllAutoStartFeatures(const string16& profile_directory) { | 222 void DisableAllAutoStartFeatures(const base::string16& profile_directory) { |
222 DisableForegroundStartAtLogin(profile_directory); | 223 DisableForegroundStartAtLogin(profile_directory); |
223 DisableBackgroundStartAtLogin(); | 224 DisableBackgroundStartAtLogin(); |
224 } | 225 } |
225 | 226 |
226 void EnableForegroundStartAtLogin(const string16& profile_directory, | 227 void EnableForegroundStartAtLogin(const base::string16& profile_directory, |
227 const base::FilePath& application_path) { | 228 const base::FilePath& application_path) { |
228 SetWillLaunchAtLogin( | 229 SetWillLaunchAtLogin( |
229 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE); | 230 application_path, profile_directory, FLAG_ENABLE, FLAG_PRESERVE); |
230 } | 231 } |
231 | 232 |
232 void DisableForegroundStartAtLogin(const string16& profile_directory) { | 233 void DisableForegroundStartAtLogin(const base::string16& profile_directory) { |
233 SetWillLaunchAtLogin( | 234 SetWillLaunchAtLogin( |
234 base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE); | 235 base::FilePath(), profile_directory, FLAG_DISABLE, FLAG_PRESERVE); |
235 } | 236 } |
236 | 237 |
237 void EnableBackgroundStartAtLogin() { | 238 void EnableBackgroundStartAtLogin() { |
238 // Background mode isn't profile specific, but we specify the Default profile | 239 // Background mode isn't profile specific, but we specify the Default profile |
239 // just to have a unique Run key to attach it to. FilePath is blank because | 240 // just to have a unique Run key to attach it to. FilePath is blank because |
240 // this function is not called from the installer (see comments for | 241 // this function is not called from the installer (see comments for |
241 // EnableAutoStartAtLogin). | 242 // EnableAutoStartAtLogin). |
242 SetWillLaunchAtLogin(base::FilePath(), | 243 SetWillLaunchAtLogin(base::FilePath(), |
243 ASCIIToUTF16(chrome::kInitialProfile), | 244 ASCIIToUTF16(chrome::kInitialProfile), |
244 FLAG_PRESERVE, | 245 FLAG_PRESERVE, |
245 FLAG_ENABLE); | 246 FLAG_ENABLE); |
246 } | 247 } |
247 | 248 |
248 void DisableBackgroundStartAtLogin() { | 249 void DisableBackgroundStartAtLogin() { |
249 SetWillLaunchAtLogin(base::FilePath(), | 250 SetWillLaunchAtLogin(base::FilePath(), |
250 ASCIIToUTF16(chrome::kInitialProfile), | 251 ASCIIToUTF16(chrome::kInitialProfile), |
251 FLAG_PRESERVE, | 252 FLAG_PRESERVE, |
252 FLAG_DISABLE); | 253 FLAG_DISABLE); |
253 } | 254 } |
254 | 255 |
255 } // namespace auto_launch_util | 256 } // namespace auto_launch_util |
OLD | NEW |