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

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

Issue 487693002: ShellUtil: Add generic methods to add/delete Windows file associations. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Minor comment change. Created 6 years, 2 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/shell_util.h ('k') | chrome/installer/util/shell_util_unittest.cc » ('j') | 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 // This file defines functions that integrate Chrome in Windows shell. These 5 // This file defines functions that integrate Chrome in Windows shell. These
6 // functions can be used by Chrome as well as Chrome installer. All of the 6 // functions can be used by Chrome as well as Chrome installer. All of the
7 // work is done by the local functions defined in anonymous namespace in 7 // work is done by the local functions defined in anonymous namespace in
8 // this class. 8 // this class.
9 9
10 #include "chrome/installer/util/shell_util.h" 10 #include "chrome/installer/util/shell_util.h"
(...skipping 173 matching lines...) Expand 10 before | Expand all | Expand 10 after
184 // class. 184 // class.
185 class RegistryEntry { 185 class RegistryEntry {
186 public: 186 public:
187 // A bit-field enum of places to look for this key in the Windows registry. 187 // A bit-field enum of places to look for this key in the Windows registry.
188 enum LookForIn { 188 enum LookForIn {
189 LOOK_IN_HKCU = 1 << 0, 189 LOOK_IN_HKCU = 1 << 0,
190 LOOK_IN_HKLM = 1 << 1, 190 LOOK_IN_HKLM = 1 << 1,
191 LOOK_IN_HKCU_THEN_HKLM = LOOK_IN_HKCU | LOOK_IN_HKLM, 191 LOOK_IN_HKCU_THEN_HKLM = LOOK_IN_HKCU | LOOK_IN_HKLM,
192 }; 192 };
193 193
194 // Details about a Windows application, to be entered into the registry for
195 // the purpose of file associations.
196 struct ApplicationInfo {
197 ApplicationInfo() : file_type_icon_index(0), application_icon_index(0) {}
198
199 // The ProgId used by Windows for file associations with this application.
200 // Must not be empty or start with a '.'.
201 base::string16 prog_id;
202 // The friendly name, and the path of the icon that will be used for files
203 // of these types when associated with this application by default. (They
204 // are NOT the name/icon that will represent the application under the Open
205 // With menu.)
206 base::string16 file_type_name;
207 // TODO(mgiuca): |file_type_icon_path| should be a base::FilePath.
208 base::string16 file_type_icon_path;
209 int file_type_icon_index;
210 // The command to execute when opening a file via this association. It
211 // should contain "%1" (to tell Windows to pass the filename as an
212 // argument).
213 // TODO(mgiuca): |command_line| should be a base::CommandLine.
214 base::string16 command_line;
215 // The AppUserModelId used by Windows 8 for this application. Distinct from
216 // |prog_id|.
217 base::string16 app_id;
218
219 // User-visible details about this application. Any of these may be empty.
220 base::string16 application_name;
221 // TODO(mgiuca): |application_icon_path| should be a base::FilePath.
222 base::string16 application_icon_path;
223 int application_icon_index;
224 base::string16 application_description;
225 base::string16 publisher_name;
226
227 // The CLSID for the application's DelegateExecute handler. May be empty.
228 base::string16 delegate_clsid;
229 };
230
194 // Returns the Windows browser client registration key for Chrome. For 231 // Returns the Windows browser client registration key for Chrome. For
195 // example: "Software\Clients\StartMenuInternet\Chromium[.user]". Strictly 232 // example: "Software\Clients\StartMenuInternet\Chromium[.user]". Strictly
196 // speaking, we should use the name of the executable (e.g., "chrome.exe"), 233 // speaking, we should use the name of the executable (e.g., "chrome.exe"),
197 // but that ship has sailed. The cost of switching now is re-prompting users 234 // but that ship has sailed. The cost of switching now is re-prompting users
198 // to make Chrome their default browser, which isn't polite. |suffix| is the 235 // to make Chrome their default browser, which isn't polite. |suffix| is the
199 // user-specific registration suffix; see GetUserSpecificDefaultBrowserSuffix 236 // user-specific registration suffix; see GetUserSpecificDefaultBrowserSuffix
200 // in shell_util.h for details. 237 // in shell_util.h for details.
201 static base::string16 GetBrowserClientKey(BrowserDistribution* dist, 238 static base::string16 GetBrowserClientKey(BrowserDistribution* dist,
202 const base::string16& suffix) { 239 const base::string16& suffix) {
203 DCHECK(suffix.empty() || suffix[0] == L'.'); 240 DCHECK(suffix.empty() || suffix[0] == L'.');
204 return base::string16(ShellUtil::kRegStartMenuInternet) 241 return base::string16(ShellUtil::kRegStartMenuInternet)
205 .append(1, L'\\') 242 .append(1, L'\\')
206 .append(dist->GetBaseAppName()) 243 .append(dist->GetBaseAppName())
207 .append(suffix); 244 .append(suffix);
208 } 245 }
209 246
210 // Returns the Windows Default Programs capabilities key for Chrome. For 247 // Returns the Windows Default Programs capabilities key for Chrome. For
211 // example: 248 // example:
212 // "Software\Clients\StartMenuInternet\Chromium[.user]\Capabilities". 249 // "Software\Clients\StartMenuInternet\Chromium[.user]\Capabilities".
213 static base::string16 GetCapabilitiesKey(BrowserDistribution* dist, 250 static base::string16 GetCapabilitiesKey(BrowserDistribution* dist,
214 const base::string16& suffix) { 251 const base::string16& suffix) {
215 return GetBrowserClientKey(dist, suffix).append(L"\\Capabilities"); 252 return GetBrowserClientKey(dist, suffix).append(L"\\Capabilities");
216 } 253 }
217 254
218 // This method returns a list of all the registry entries that 255 // This method returns a list of all the registry entries that
219 // are needed to register this installation's ProgId and AppId. 256 // are needed to register this installation's ProgId and AppId.
220 // These entries need to be registered in HKLM prior to Win8. 257 // These entries need to be registered in HKLM prior to Win8.
221 static void GetProgIdEntries(BrowserDistribution* dist, 258 static void GetChromeProgIdEntries(BrowserDistribution* dist,
222 const base::string16& chrome_exe, 259 const base::string16& chrome_exe,
223 const base::string16& suffix, 260 const base::string16& suffix,
224 ScopedVector<RegistryEntry>* entries) { 261 ScopedVector<RegistryEntry>* entries) {
225 base::string16 icon_path( 262 int chrome_icon_index =
226 ShellUtil::FormatIconLocation( 263 dist->GetIconIndex(BrowserDistribution::SHORTCUT_CHROME);
227 chrome_exe, 264
228 dist->GetIconIndex(BrowserDistribution::SHORTCUT_CHROME))); 265 ApplicationInfo app_info;
229 base::string16 open_cmd(ShellUtil::GetChromeShellOpenCmd(chrome_exe)); 266 app_info.prog_id = GetBrowserProgId(suffix);
267 app_info.file_type_name = dist->GetBrowserProgIdDesc();
268 // File types associated with Chrome are just given the Chrome icon.
269 app_info.file_type_icon_path = chrome_exe;
270 app_info.file_type_icon_index = chrome_icon_index;
271 app_info.command_line = ShellUtil::GetChromeShellOpenCmd(chrome_exe);
272 // For user-level installs: entries for the app id will be in HKCU; thus we
273 // do not need a suffix on those entries.
274 app_info.app_id = ShellUtil::GetBrowserModelId(
275 dist, InstallUtil::IsPerUserInstall(chrome_exe.c_str()));
276
277 // The command to execute when opening this application via the Metro UI.
230 base::string16 delegate_command( 278 base::string16 delegate_command(
231 ShellUtil::GetChromeDelegateCommand(chrome_exe)); 279 ShellUtil::GetChromeDelegateCommand(chrome_exe));
232 // For user-level installs: entries for the app id and DelegateExecute verb
233 // handler will be in HKCU; thus we do not need a suffix on those entries.
234 base::string16 app_id(
235 ShellUtil::GetBrowserModelId(
236 dist, InstallUtil::IsPerUserInstall(chrome_exe.c_str())));
237 base::string16 delegate_guid;
238 bool set_delegate_execute = 280 bool set_delegate_execute =
239 IsChromeMetroSupported() && 281 IsChromeMetroSupported() &&
240 dist->GetCommandExecuteImplClsid(&delegate_guid); 282 dist->GetCommandExecuteImplClsid(&app_info.delegate_clsid);
241 283
242 // DelegateExecute ProgId. Needed for Chrome Metro in Windows 8. 284 // TODO(grt): http://crbug.com/75152 Write a reference to a localized
285 // resource for name, description, and company.
286 app_info.application_name = dist->GetDisplayName();
287 app_info.application_icon_path = chrome_exe;
288 app_info.application_icon_index = chrome_icon_index;
289 app_info.application_description = dist->GetAppDescription();
290 app_info.publisher_name = dist->GetPublisherName();
291
292 GetProgIdEntries(app_info, entries);
293
294 // DelegateExecute ProgId. Needed for Chrome Metro in Windows 8. This is
295 // only needed for registring a web browser, not for general associations.
243 if (set_delegate_execute) { 296 if (set_delegate_execute) {
244 base::string16 model_id_shell(ShellUtil::kRegClasses); 297 base::string16 model_id_shell(ShellUtil::kRegClasses);
245 model_id_shell.push_back(base::FilePath::kSeparators[0]); 298 model_id_shell.push_back(base::FilePath::kSeparators[0]);
246 model_id_shell.append(app_id); 299 model_id_shell.append(app_info.app_id);
247 model_id_shell.append(ShellUtil::kRegExePath); 300 model_id_shell.append(ShellUtil::kRegExePath);
248 model_id_shell.append(ShellUtil::kRegShellPath); 301 model_id_shell.append(ShellUtil::kRegShellPath);
249 302
250 // <root hkey>\Software\Classes\<app_id>\.exe\shell @=open 303 // <root hkey>\Software\Classes\<app_id>\.exe\shell @=open
251 entries->push_back(new RegistryEntry(model_id_shell, 304 entries->push_back(new RegistryEntry(model_id_shell,
252 ShellUtil::kRegVerbOpen)); 305 ShellUtil::kRegVerbOpen));
253 306
254 // Each of Chrome's shortcuts has an appid; which, as of Windows 8, is 307 // Each of Chrome's shortcuts has an appid; which, as of Windows 8, is
255 // registered to handle some verbs. This registration has the side-effect 308 // registered to handle some verbs. This registration has the side-effect
256 // that these verbs now show up in the shortcut's context menu. We 309 // that these verbs now show up in the shortcut's context menu. We
257 // mitigate this side-effect by making the context menu entries 310 // mitigate this side-effect by making the context menu entries
258 // user readable/localized strings. See relevant MSDN article: 311 // user readable/localized strings. See relevant MSDN article:
gab 2014/10/02 13:03:59 s/user/use
Matt Giuca 2014/10/03 00:26:40 I believe it's trying to say "user-readable", not
gab 2014/10/03 15:26:25 Ah it's /"user readable"/"localized strings/; I wa
259 // http://msdn.microsoft.com/en-US/library/windows/desktop/cc144171.aspx 312 // http://msdn.microsoft.com/en-US/library/windows/desktop/cc144171.aspx
260 const struct { 313 const struct {
261 const wchar_t* verb; 314 const wchar_t* verb;
262 int name_id; 315 int name_id;
263 } verbs[] = { 316 } verbs[] = {
264 { ShellUtil::kRegVerbOpen, -1 }, 317 { ShellUtil::kRegVerbOpen, -1 },
265 { ShellUtil::kRegVerbOpenNewWindow, IDS_SHORTCUT_NEW_WINDOW_BASE }, 318 { ShellUtil::kRegVerbOpenNewWindow, IDS_SHORTCUT_NEW_WINDOW_BASE },
266 }; 319 };
267 for (size_t i = 0; i < arraysize(verbs); ++i) { 320 for (size_t i = 0; i < arraysize(verbs); ++i) {
268 base::string16 sub_path(model_id_shell); 321 base::string16 sub_path(model_id_shell);
(...skipping 10 matching lines...) Expand all
279 } 332 }
280 entries->push_back(new RegistryEntry( 333 entries->push_back(new RegistryEntry(
281 sub_path, L"CommandId", L"Browser.Launch")); 334 sub_path, L"CommandId", L"Browser.Launch"));
282 335
283 sub_path.push_back(base::FilePath::kSeparators[0]); 336 sub_path.push_back(base::FilePath::kSeparators[0]);
284 sub_path.append(ShellUtil::kRegCommand); 337 sub_path.append(ShellUtil::kRegCommand);
285 338
286 // <root hkey>\Software\Classes\<app_id>\.exe\shell\<verb>\command 339 // <root hkey>\Software\Classes\<app_id>\.exe\shell\<verb>\command
287 entries->push_back(new RegistryEntry(sub_path, delegate_command)); 340 entries->push_back(new RegistryEntry(sub_path, delegate_command));
288 entries->push_back(new RegistryEntry( 341 entries->push_back(new RegistryEntry(
289 sub_path, ShellUtil::kRegDelegateExecute, delegate_guid)); 342 sub_path, ShellUtil::kRegDelegateExecute, app_info.delegate_clsid));
290 } 343 }
291 } 344 }
345 }
346
347 // Gets the registry entries to register an application in the Windows
348 // registry. |app_info| provides all of the information needed.
349 static void GetProgIdEntries(const ApplicationInfo& app_info,
350 ScopedVector<RegistryEntry>* entries) {
351 // Basic sanity checks.
352 DCHECK(!app_info.prog_id.empty());
353 DCHECK_NE(L'.', app_info.prog_id[0]);
gab 2014/10/02 13:03:59 Another restriction worth enforcing here and in th
Matt Giuca 2014/10/03 00:26:40 Actually, I looked into this yesterday (since I fo
gab 2014/10/03 15:26:25 So I just dug further into this since I remembered
gab 2014/11/05 15:47:41 ping; just back from a month leave, has anything b
292 354
293 // File association ProgId 355 // File association ProgId
294 base::string16 chrome_html_prog_id(ShellUtil::kRegClasses); 356 base::string16 prog_id_path(ShellUtil::kRegClasses);
295 chrome_html_prog_id.push_back(base::FilePath::kSeparators[0]); 357 prog_id_path.push_back(base::FilePath::kSeparators[0]);
296 chrome_html_prog_id.append(GetBrowserProgId(suffix)); 358 prog_id_path.append(app_info.prog_id);
359 entries->push_back(
360 new RegistryEntry(prog_id_path, app_info.file_type_name));
297 entries->push_back(new RegistryEntry( 361 entries->push_back(new RegistryEntry(
298 chrome_html_prog_id, dist->GetBrowserProgIdDesc())); 362 prog_id_path + ShellUtil::kRegDefaultIcon,
363 ShellUtil::FormatIconLocation(app_info.file_type_icon_path,
364 app_info.file_type_icon_index)));
299 entries->push_back(new RegistryEntry( 365 entries->push_back(new RegistryEntry(
300 chrome_html_prog_id + ShellUtil::kRegDefaultIcon, icon_path)); 366 prog_id_path + ShellUtil::kRegShellOpen, app_info.command_line));
301 entries->push_back(new RegistryEntry( 367 if (!app_info.delegate_clsid.empty()) {
302 chrome_html_prog_id + ShellUtil::kRegShellOpen, open_cmd)); 368 entries->push_back(
303 if (set_delegate_execute) { 369 new RegistryEntry(prog_id_path + ShellUtil::kRegShellOpen,
304 entries->push_back(new RegistryEntry( 370 ShellUtil::kRegDelegateExecute,
305 chrome_html_prog_id + ShellUtil::kRegShellOpen, 371 app_info.delegate_clsid));
306 ShellUtil::kRegDelegateExecute, delegate_guid));
307 } 372 }
308 373
309 // The following entries are required as of Windows 8, but do not 374 // The following entries are required as of Windows 8, but do not
310 // depend on the DelegateExecute verb handler being set. 375 // depend on the DelegateExecute verb handler being set.
311 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 376 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
312 entries->push_back(new RegistryEntry( 377 if (!app_info.app_id.empty()) {
313 chrome_html_prog_id, ShellUtil::kRegAppUserModelId, app_id)); 378 entries->push_back(new RegistryEntry(
379 prog_id_path, ShellUtil::kRegAppUserModelId, app_info.app_id));
380 }
314 381
315 // Add \Software\Classes\ChromeHTML\Application entries 382 // Add \Software\Classes\<prog_id>\Application entries
316 base::string16 chrome_application(chrome_html_prog_id + 383 base::string16 application_path(prog_id_path +
317 ShellUtil::kRegApplication); 384 ShellUtil::kRegApplication);
318 entries->push_back(new RegistryEntry( 385 if (!app_info.app_id.empty()) {
319 chrome_application, ShellUtil::kRegAppUserModelId, app_id)); 386 entries->push_back(new RegistryEntry(
320 entries->push_back(new RegistryEntry( 387 application_path, ShellUtil::kRegAppUserModelId, app_info.app_id));
321 chrome_application, ShellUtil::kRegApplicationIcon, icon_path)); 388 }
322 // TODO(grt): http://crbug.com/75152 Write a reference to a localized 389 if (!app_info.application_icon_path.empty()) {
323 // resource for name, description, and company. 390 entries->push_back(new RegistryEntry(
324 entries->push_back(new RegistryEntry( 391 application_path,
325 chrome_application, ShellUtil::kRegApplicationName, 392 ShellUtil::kRegApplicationIcon,
326 dist->GetDisplayName())); 393 ShellUtil::FormatIconLocation(app_info.application_icon_path,
327 entries->push_back(new RegistryEntry( 394 app_info.application_icon_index)));
328 chrome_application, ShellUtil::kRegApplicationDescription, 395 }
329 dist->GetAppDescription())); 396 if (!app_info.application_name.empty()) {
330 entries->push_back(new RegistryEntry( 397 entries->push_back(new RegistryEntry(application_path,
331 chrome_application, ShellUtil::kRegApplicationCompany, 398 ShellUtil::kRegApplicationName,
332 dist->GetPublisherName())); 399 app_info.application_name));
400 }
401 if (!app_info.application_description.empty()) {
402 entries->push_back(
403 new RegistryEntry(application_path,
404 ShellUtil::kRegApplicationDescription,
405 app_info.application_description));
406 }
407 if (!app_info.publisher_name.empty()) {
408 entries->push_back(new RegistryEntry(application_path,
409 ShellUtil::kRegApplicationCompany,
410 app_info.publisher_name));
411 }
333 } 412 }
334 } 413 }
335 414
336 // This method returns a list of the registry entries needed to declare a 415 // This method returns a list of the registry entries needed to declare a
337 // capability of handling a protocol on Windows. 416 // capability of handling a protocol on Windows.
338 static void GetProtocolCapabilityEntries( 417 static void GetProtocolCapabilityEntries(
339 BrowserDistribution* dist, 418 BrowserDistribution* dist,
340 const base::string16& suffix, 419 const base::string16& suffix,
341 const base::string16& protocol, 420 const base::string16& protocol,
342 ScopedVector<RegistryEntry>* entries) { 421 ScopedVector<RegistryEntry>* entries) {
(...skipping 83 matching lines...) Expand 10 before | Expand all | Expand 10 after
426 } 505 }
427 506
428 // This method returns a list of the registry entries required for this 507 // This method returns a list of the registry entries required for this
429 // installation to be registered in the Windows shell. 508 // installation to be registered in the Windows shell.
430 // In particular: 509 // In particular:
431 // - App Paths 510 // - App Paths
432 // http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121 511 // http://msdn.microsoft.com/en-us/library/windows/desktop/ee872121
433 // - File Associations 512 // - File Associations
434 // http://msdn.microsoft.com/en-us/library/bb166549 513 // http://msdn.microsoft.com/en-us/library/bb166549
435 // These entries need to be registered in HKLM prior to Win8. 514 // These entries need to be registered in HKLM prior to Win8.
436 static void GetAppRegistrationEntries(const base::string16& chrome_exe, 515 static void GetChromeAppRegistrationEntries(
437 const base::string16& suffix, 516 const base::string16& chrome_exe,
438 ScopedVector<RegistryEntry>* entries) { 517 const base::string16& suffix,
518 ScopedVector<RegistryEntry>* entries) {
439 const base::FilePath chrome_path(chrome_exe); 519 const base::FilePath chrome_path(chrome_exe);
440 base::string16 app_path_key(ShellUtil::kAppPathsRegistryKey); 520 base::string16 app_path_key(ShellUtil::kAppPathsRegistryKey);
441 app_path_key.push_back(base::FilePath::kSeparators[0]); 521 app_path_key.push_back(base::FilePath::kSeparators[0]);
442 app_path_key.append(chrome_path.BaseName().value()); 522 app_path_key.append(chrome_path.BaseName().value());
443 entries->push_back(new RegistryEntry(app_path_key, chrome_exe)); 523 entries->push_back(new RegistryEntry(app_path_key, chrome_exe));
444 entries->push_back(new RegistryEntry(app_path_key, 524 entries->push_back(new RegistryEntry(app_path_key,
445 ShellUtil::kAppPathsRegistryPathName, chrome_path.DirName().value())); 525 ShellUtil::kAppPathsRegistryPathName, chrome_path.DirName().value()));
446 526
447 const base::string16 html_prog_id(GetBrowserProgId(suffix)); 527 const base::string16 html_prog_id(GetBrowserProgId(suffix));
448 for (int i = 0; ShellUtil::kPotentialFileAssociations[i] != NULL; i++) { 528 for (int i = 0; ShellUtil::kPotentialFileAssociations[i] != NULL; i++) {
449 base::string16 key(ShellUtil::kRegClasses); 529 GetAppExtRegistrationEntries(
450 key.push_back(base::FilePath::kSeparators[0]); 530 html_prog_id, ShellUtil::kPotentialFileAssociations[i], entries);
451 key.append(ShellUtil::kPotentialFileAssociations[i]);
452 key.push_back(base::FilePath::kSeparators[0]);
453 key.append(ShellUtil::kRegOpenWithProgids);
454 entries->push_back(
455 new RegistryEntry(key, html_prog_id, base::string16()));
456 } 531 }
457 } 532 }
458 533
534 // Gets the registry entries to register an application as a handler for a
535 // particular file extension. |prog_id| is the ProgId used by Windows for the
536 // application. |ext| is the file extension, which must begin with a '.'.
537 static void GetAppExtRegistrationEntries(
538 const base::string16& prog_id,
539 const base::string16& ext,
540 ScopedVector<RegistryEntry>* entries) {
541 // In HKEY_CURRENT_USER\Software\Classes\EXT\OpenWithProgids, create an
542 // empty value with this class's ProgId.
543 base::string16 key_name(ShellUtil::kRegClasses);
544 key_name.push_back(base::FilePath::kSeparators[0]);
545 key_name.append(ext);
546 key_name.push_back(base::FilePath::kSeparators[0]);
547 key_name.append(ShellUtil::kRegOpenWithProgids);
548 entries->push_back(new RegistryEntry(key_name, prog_id, base::string16()));
549 }
550
551 // Gets the registry entries to register an application as the default handler
552 // for a particular file extension. |prog_id| is the ProgId used by Windows
553 // for the application. |ext| is the file extension, which must begin with a
554 // '.'. If |overwrite_existing|, always sets the default handler; otherwise
555 // only sets if there is no existing default.
556 //
557 // This has no effect on Windows 8. Windows 8 ignores the default and lets the
558 // user choose. If there is only one handler for a file, it will automatically
559 // become the default. Otherwise, the first time the user opens a file, they
560 // are presented with the dialog to set the default handler. (This is roughly
561 // equivalent to being called with |overwrite_existing| false.)
562 static void GetAppDefaultRegistrationEntries(
563 const base::string16& prog_id,
564 const base::string16& ext,
565 bool overwrite_existing,
566 ScopedVector<RegistryEntry>* entries) {
567 // Set the default value of HKEY_CURRENT_USER\Software\Classes\EXT to this
568 // class's name.
569 base::string16 key_name(ShellUtil::kRegClasses);
570 key_name.push_back(base::FilePath::kSeparators[0]);
571 key_name.append(ext);
572 scoped_ptr<RegistryEntry> default_association(
573 new RegistryEntry(key_name, prog_id));
574 if (overwrite_existing ||
575 !default_association->KeyExistsInRegistry(
576 RegistryEntry::LOOK_IN_HKCU)) {
577 entries->push_back(default_association.release());
578 }
579 }
580
459 // This method returns a list of all the user level registry entries that 581 // This method returns a list of all the user level registry entries that
460 // are needed to make Chromium the default handler for a protocol on XP. 582 // are needed to make Chromium the default handler for a protocol on XP.
461 static void GetXPStyleUserProtocolEntries( 583 static void GetXPStyleUserProtocolEntries(
462 const base::string16& protocol, 584 const base::string16& protocol,
463 const base::string16& chrome_icon, 585 const base::string16& chrome_icon,
464 const base::string16& chrome_open, 586 const base::string16& chrome_open,
465 ScopedVector<RegistryEntry>* entries) { 587 ScopedVector<RegistryEntry>* entries) {
466 // Protocols associations. 588 // Protocols associations.
467 base::string16 url_key(ShellUtil::kRegClasses); 589 base::string16 url_key(ShellUtil::kRegClasses);
468 url_key.push_back(base::FilePath::kSeparators[0]); 590 url_key.push_back(base::FilePath::kSeparators[0]);
(...skipping 28 matching lines...) Expand all
497 // we register them anyways as some legacy apps are hardcoded to lookup those 619 // we register them anyways as some legacy apps are hardcoded to lookup those
498 // values. 620 // values.
499 static void GetXPStyleDefaultBrowserUserEntries( 621 static void GetXPStyleDefaultBrowserUserEntries(
500 BrowserDistribution* dist, 622 BrowserDistribution* dist,
501 const base::string16& chrome_exe, 623 const base::string16& chrome_exe,
502 const base::string16& suffix, 624 const base::string16& suffix,
503 ScopedVector<RegistryEntry>* entries) { 625 ScopedVector<RegistryEntry>* entries) {
504 // File extension associations. 626 // File extension associations.
505 base::string16 html_prog_id(GetBrowserProgId(suffix)); 627 base::string16 html_prog_id(GetBrowserProgId(suffix));
506 for (int i = 0; ShellUtil::kDefaultFileAssociations[i] != NULL; i++) { 628 for (int i = 0; ShellUtil::kDefaultFileAssociations[i] != NULL; i++) {
507 base::string16 ext_key(ShellUtil::kRegClasses); 629 GetAppDefaultRegistrationEntries(
508 ext_key.push_back(base::FilePath::kSeparators[0]); 630 html_prog_id, ShellUtil::kDefaultFileAssociations[i], true, entries);
509 ext_key.append(ShellUtil::kDefaultFileAssociations[i]);
510 entries->push_back(new RegistryEntry(ext_key, html_prog_id));
511 } 631 }
512 632
513 // Protocols associations. 633 // Protocols associations.
514 base::string16 chrome_open = ShellUtil::GetChromeShellOpenCmd(chrome_exe); 634 base::string16 chrome_open = ShellUtil::GetChromeShellOpenCmd(chrome_exe);
515 base::string16 chrome_icon = 635 base::string16 chrome_icon =
516 ShellUtil::FormatIconLocation( 636 ShellUtil::FormatIconLocation(
517 chrome_exe, 637 chrome_exe,
518 dist->GetIconIndex(BrowserDistribution::SHORTCUT_CHROME)); 638 dist->GetIconIndex(BrowserDistribution::SHORTCUT_CHROME));
519 for (int i = 0; ShellUtil::kBrowserProtocolAssociations[i] != NULL; i++) { 639 for (int i = 0; ShellUtil::kBrowserProtocolAssociations[i] != NULL; i++) {
520 GetXPStyleUserProtocolEntries(ShellUtil::kBrowserProtocolAssociations[i], 640 GetXPStyleUserProtocolEntries(ShellUtil::kBrowserProtocolAssociations[i],
(...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after
556 DCHECK(look_for_in); 676 DCHECK(look_for_in);
557 677
558 RegistryStatus status = DOES_NOT_EXIST; 678 RegistryStatus status = DOES_NOT_EXIST;
559 if (look_for_in & LOOK_IN_HKCU) 679 if (look_for_in & LOOK_IN_HKCU)
560 status = StatusInRegistryUnderRoot(HKEY_CURRENT_USER); 680 status = StatusInRegistryUnderRoot(HKEY_CURRENT_USER);
561 if (status == DOES_NOT_EXIST && (look_for_in & LOOK_IN_HKLM)) 681 if (status == DOES_NOT_EXIST && (look_for_in & LOOK_IN_HKLM))
562 status = StatusInRegistryUnderRoot(HKEY_LOCAL_MACHINE); 682 status = StatusInRegistryUnderRoot(HKEY_LOCAL_MACHINE);
563 return status == SAME_VALUE; 683 return status == SAME_VALUE;
564 } 684 }
565 685
686 // Checks if the current registry entry exists in \|key_path_|\|name_|,
687 // regardless of value. Same lookup rules as ExistsInRegistry.
688 // Unlike ExistsInRegistry, this returns true if some other value is present
689 // with the same key.
690 bool KeyExistsInRegistry(uint32 look_for_in) const {
691 DCHECK(look_for_in);
692
693 RegistryStatus status = DOES_NOT_EXIST;
694 if (look_for_in & LOOK_IN_HKCU)
695 status = StatusInRegistryUnderRoot(HKEY_CURRENT_USER);
696 if (status == DOES_NOT_EXIST && (look_for_in & LOOK_IN_HKLM))
697 status = StatusInRegistryUnderRoot(HKEY_LOCAL_MACHINE);
698 return status != DOES_NOT_EXIST;
699 }
700
566 private: 701 private:
567 // States this RegistryKey can be in compared to the registry. 702 // States this RegistryKey can be in compared to the registry.
568 enum RegistryStatus { 703 enum RegistryStatus {
569 // |name_| does not exist in the registry 704 // |name_| does not exist in the registry
570 DOES_NOT_EXIST, 705 DOES_NOT_EXIST,
571 // |name_| exists, but its value != |value_| 706 // |name_| exists, but its value != |value_|
572 DIFFERENT_VALUE, 707 DIFFERENT_VALUE,
573 // |name_| exists and its value is |value_| 708 // |name_| exists and its value is |value_|
574 SAME_VALUE, 709 SAME_VALUE,
575 }; 710 };
(...skipping 93 matching lines...) Expand 10 before | Expand all | Expand 10 after
669 // under a single registry root. Not doing so caused http://crbug.com/144910 for 804 // under a single registry root. Not doing so caused http://crbug.com/144910 for
670 // users who first-installed Chrome in that revision range (those users are 805 // users who first-installed Chrome in that revision range (those users are
671 // still impacted by http://crbug.com/144910). This method will keep returning 806 // still impacted by http://crbug.com/144910). This method will keep returning
672 // true for affected users (i.e. who have all the registrations, but over both 807 // true for affected users (i.e. who have all the registrations, but over both
673 // registry roots). 808 // registry roots).
674 bool IsChromeRegistered(BrowserDistribution* dist, 809 bool IsChromeRegistered(BrowserDistribution* dist,
675 const base::string16& chrome_exe, 810 const base::string16& chrome_exe,
676 const base::string16& suffix, 811 const base::string16& suffix,
677 uint32 look_for_in) { 812 uint32 look_for_in) {
678 ScopedVector<RegistryEntry> entries; 813 ScopedVector<RegistryEntry> entries;
679 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, &entries); 814 RegistryEntry::GetChromeProgIdEntries(dist, chrome_exe, suffix, &entries);
680 RegistryEntry::GetShellIntegrationEntries(dist, chrome_exe, suffix, &entries); 815 RegistryEntry::GetShellIntegrationEntries(dist, chrome_exe, suffix, &entries);
681 RegistryEntry::GetAppRegistrationEntries(chrome_exe, suffix, &entries); 816 RegistryEntry::GetChromeAppRegistrationEntries(chrome_exe, suffix, &entries);
682 return AreEntriesRegistered(entries, look_for_in); 817 return AreEntriesRegistered(entries, look_for_in);
683 } 818 }
684 819
685 // This method checks if Chrome is already registered on the local machine 820 // This method checks if Chrome is already registered on the local machine
686 // for the requested protocol. It just checks the one value required for this. 821 // for the requested protocol. It just checks the one value required for this.
687 // See RegistryEntry::ExistsInRegistry for the behavior of |look_for_in|. 822 // See RegistryEntry::ExistsInRegistry for the behavior of |look_for_in|.
688 bool IsChromeRegisteredForProtocol(BrowserDistribution* dist, 823 bool IsChromeRegisteredForProtocol(BrowserDistribution* dist,
689 const base::string16& suffix, 824 const base::string16& suffix,
690 const base::string16& protocol, 825 const base::string16& protocol,
691 uint32 look_for_in) { 826 uint32 look_for_in) {
(...skipping 1364 matching lines...) Expand 10 before | Expand all | Expand 10 after
2056 // Check if chrome is already registered with this suffix. 2191 // Check if chrome is already registered with this suffix.
2057 if (IsChromeRegistered(dist, chrome_exe, suffix, look_for_in)) 2192 if (IsChromeRegistered(dist, chrome_exe, suffix, look_for_in))
2058 return true; 2193 return true;
2059 2194
2060 bool result = true; 2195 bool result = true;
2061 if (root == HKEY_CURRENT_USER || IsUserAnAdmin()) { 2196 if (root == HKEY_CURRENT_USER || IsUserAnAdmin()) {
2062 // Do the full registration if we can do it at user-level or if the user is 2197 // Do the full registration if we can do it at user-level or if the user is
2063 // an admin. 2198 // an admin.
2064 ScopedVector<RegistryEntry> progid_and_appreg_entries; 2199 ScopedVector<RegistryEntry> progid_and_appreg_entries;
2065 ScopedVector<RegistryEntry> shell_entries; 2200 ScopedVector<RegistryEntry> shell_entries;
2066 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, 2201 RegistryEntry::GetChromeProgIdEntries(
2067 &progid_and_appreg_entries); 2202 dist, chrome_exe, suffix, &progid_and_appreg_entries);
2068 RegistryEntry::GetAppRegistrationEntries(chrome_exe, suffix, 2203 RegistryEntry::GetChromeAppRegistrationEntries(
2069 &progid_and_appreg_entries); 2204 chrome_exe, suffix, &progid_and_appreg_entries);
2070 RegistryEntry::GetShellIntegrationEntries( 2205 RegistryEntry::GetShellIntegrationEntries(
2071 dist, chrome_exe, suffix, &shell_entries); 2206 dist, chrome_exe, suffix, &shell_entries);
2072 result = (AddRegistryEntries(root, progid_and_appreg_entries) && 2207 result = (AddRegistryEntries(root, progid_and_appreg_entries) &&
2073 AddRegistryEntries(root, shell_entries)); 2208 AddRegistryEntries(root, shell_entries));
2074 } else if (elevate_if_not_admin && 2209 } else if (elevate_if_not_admin &&
2075 base::win::GetVersion() >= base::win::VERSION_VISTA && 2210 base::win::GetVersion() >= base::win::VERSION_VISTA &&
2076 ElevateAndRegisterChrome(dist, chrome_exe, suffix, base::string16())) { 2211 ElevateAndRegisterChrome(dist, chrome_exe, suffix, base::string16())) {
2077 // If the user is not an admin and OS is between Vista and Windows 7 2212 // If the user is not an admin and OS is between Vista and Windows 7
2078 // inclusively, try to elevate and register. This is only intended for 2213 // inclusively, try to elevate and register. This is only intended for
2079 // user-level installs as system-level installs should always be run with 2214 // user-level installs as system-level installs should always be run with
2080 // admin rights. 2215 // admin rights.
2081 result = true; 2216 result = true;
2082 } else { 2217 } else {
2083 // If we got to this point then all we can do is create ProgId and basic app 2218 // If we got to this point then all we can do is create ProgId and basic app
2084 // registrations under HKCU. 2219 // registrations under HKCU.
2085 ScopedVector<RegistryEntry> entries; 2220 ScopedVector<RegistryEntry> entries;
2086 RegistryEntry::GetProgIdEntries( 2221 RegistryEntry::GetChromeProgIdEntries(
2087 dist, chrome_exe, base::string16(), &entries); 2222 dist, chrome_exe, base::string16(), &entries);
2088 // Prefer to use |suffix|; unless Chrome's ProgIds are already registered 2223 // Prefer to use |suffix|; unless Chrome's ProgIds are already registered
2089 // with no suffix (as per the old registration style): in which case some 2224 // with no suffix (as per the old registration style): in which case some
2090 // other registry entries could refer to them and since we were not able to 2225 // other registry entries could refer to them and since we were not able to
2091 // set our HKLM entries above, we are better off not altering these here. 2226 // set our HKLM entries above, we are better off not altering these here.
2092 if (!AreEntriesRegistered(entries, RegistryEntry::LOOK_IN_HKCU)) { 2227 if (!AreEntriesRegistered(entries, RegistryEntry::LOOK_IN_HKCU)) {
2093 if (!suffix.empty()) { 2228 if (!suffix.empty()) {
2094 entries.clear(); 2229 entries.clear();
2095 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, &entries); 2230 RegistryEntry::GetChromeProgIdEntries(
2096 RegistryEntry::GetAppRegistrationEntries(chrome_exe, suffix, &entries); 2231 dist, chrome_exe, suffix, &entries);
2232 RegistryEntry::GetChromeAppRegistrationEntries(
2233 chrome_exe, suffix, &entries);
2097 } 2234 }
2098 result = AddRegistryEntries(HKEY_CURRENT_USER, entries); 2235 result = AddRegistryEntries(HKEY_CURRENT_USER, entries);
2099 } else { 2236 } else {
2100 // The ProgId is registered unsuffixed in HKCU, also register the app with 2237 // The ProgId is registered unsuffixed in HKCU, also register the app with
2101 // Windows in HKCU (this was not done in the old registration style and 2238 // Windows in HKCU (this was not done in the old registration style and
2102 // thus needs to be done after the above check for the unsuffixed 2239 // thus needs to be done after the above check for the unsuffixed
2103 // registration). 2240 // registration).
2104 entries.clear(); 2241 entries.clear();
2105 RegistryEntry::GetAppRegistrationEntries(chrome_exe, base::string16(), 2242 RegistryEntry::GetChromeAppRegistrationEntries(
2106 &entries); 2243 chrome_exe, base::string16(), &entries);
2107 result = AddRegistryEntries(HKEY_CURRENT_USER, entries); 2244 result = AddRegistryEntries(HKEY_CURRENT_USER, entries);
2108 } 2245 }
2109 } 2246 }
2110 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); 2247 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
2111 return result; 2248 return result;
2112 } 2249 }
2113 2250
2114 bool ShellUtil::RegisterChromeForProtocol(BrowserDistribution* dist, 2251 bool ShellUtil::RegisterChromeForProtocol(BrowserDistribution* dist,
2115 const base::string16& chrome_exe, 2252 const base::string16& chrome_exe,
2116 const base::string16& unique_suffix, 2253 const base::string16& unique_suffix,
(...skipping 168 matching lines...) Expand 10 before | Expand all | Expand 10 after
2285 // are any left...). 2422 // are any left...).
2286 if (free_bits >= 8 && next_byte_index < size) { 2423 if (free_bits >= 8 && next_byte_index < size) {
2287 free_bits -= 8; 2424 free_bits -= 8;
2288 bit_stream += bytes[next_byte_index++] << free_bits; 2425 bit_stream += bytes[next_byte_index++] << free_bits;
2289 } 2426 }
2290 } 2427 }
2291 2428
2292 DCHECK_EQ(ret.length(), encoded_length); 2429 DCHECK_EQ(ret.length(), encoded_length);
2293 return ret; 2430 return ret;
2294 } 2431 }
2432
2433 // static
2434 bool ShellUtil::AddFileAssociations(
2435 const base::string16& prog_id,
2436 const base::CommandLine& command_line,
2437 const base::string16& file_type_name,
2438 const base::FilePath& icon_path,
2439 const std::set<base::string16>& file_extensions) {
2440 ScopedVector<RegistryEntry> entries;
2441
2442 // Create a class for this app.
2443 RegistryEntry::ApplicationInfo app_info;
2444 app_info.prog_id = prog_id;
2445 app_info.file_type_name = file_type_name;
2446 app_info.file_type_icon_path = icon_path.value();
2447 app_info.file_type_icon_index = 0;
2448 app_info.command_line = command_line.GetCommandLineString();
2449 RegistryEntry::GetProgIdEntries(app_info, &entries);
2450
2451 // Associate each extension that the app can handle with the class. Set this
2452 // app as the default handler if and only if there is no existing default.
2453 for (std::set<base::string16>::const_iterator it = file_extensions.begin();
2454 it != file_extensions.end();
2455 ++it) {
2456 // Do not allow empty file extensions, or extensions beginning with a '.'.
2457 DCHECK(!it->empty());
2458 DCHECK_NE(L'.', (*it)[0]);
2459 base::string16 ext(1, L'.');
2460 ext.append(*it);
2461 RegistryEntry::GetAppExtRegistrationEntries(prog_id, ext, &entries);
2462
2463 // Regstering as the default will have no effect on Windows 8 (see
2464 // documentation for GetAppDefaultRegistrationEntries). However, if our app
2465 // is the only handler, it will automatically become the default, so the
2466 // same effect is achieved.
2467 RegistryEntry::GetAppDefaultRegistrationEntries(
2468 prog_id, ext, false, &entries);
2469 }
2470
2471 return AddRegistryEntries(HKEY_CURRENT_USER, entries);
2472 }
2473
2474 // static
2475 bool ShellUtil::DeleteFileAssociations(const base::string16& prog_id) {
2476 // Delete the key HKEY_CURRENT_USER\Software\Classes\PROGID.
2477 base::string16 key_path(ShellUtil::kRegClasses);
2478 key_path.push_back(base::FilePath::kSeparators[0]);
2479 key_path.append(prog_id);
2480 return InstallUtil::DeleteRegistryKey(
2481 HKEY_CURRENT_USER, key_path, WorkItem::kWow64Default);
2482
2483 // TODO(mgiuca): Remove the extension association entries. This requires that
2484 // the extensions associated with a particular prog_id are stored in that
2485 // prog_id's key.
2486 }
OLDNEW
« no previous file with comments | « chrome/installer/util/shell_util.h ('k') | chrome/installer/util/shell_util_unittest.cc » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698