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

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

Issue 10451074: Always suffix ChromeHTML entries on Windows for user-level installs. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Created 8 years, 6 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
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 56 matching lines...) Expand 10 before | Expand all | Expand 10 after
67 return VerifyVersionInfo(&min_version_info, type_mask, condition_mask) != 0; 67 return VerifyVersionInfo(&min_version_info, type_mask, condition_mask) != 0;
68 } 68 }
69 69
70 // This class represents a single registry entry. The objective is to 70 // This class represents a single registry entry. The objective is to
71 // encapsulate all the registry entries required for registering Chrome at one 71 // encapsulate all the registry entries required for registering Chrome at one
72 // place. This class can not be instantiated outside the class and the objects 72 // place. This class can not be instantiated outside the class and the objects
73 // of this class type can be obtained only by calling a static method of this 73 // of this class type can be obtained only by calling a static method of this
74 // class. 74 // class.
75 class RegistryEntry { 75 class RegistryEntry {
76 public: 76 public:
77 // A bit-field enum of places to look for this key in the Windows registry.
78 enum LookForIn {
79 LOOK_IN_HKCU = 1 << 0,
80 LOOK_IN_HKLM = 1 << 1,
81 };
82
77 // Returns the Windows browser client registration key for Chrome. For 83 // Returns the Windows browser client registration key for Chrome. For
78 // example: "Software\Clients\StartMenuInternet\Chromium[.user]". Strictly 84 // example: "Software\Clients\StartMenuInternet\Chromium[.user]". Strictly
79 // speaking, we should use the name of the executable (e.g., "chrome.exe"), 85 // speaking, we should use the name of the executable (e.g., "chrome.exe"),
80 // but that ship has sailed. The cost of switching now is re-prompting users 86 // but that ship has sailed. The cost of switching now is re-prompting users
81 // to make Chrome their default browser, which isn't polite. |suffix| is the 87 // to make Chrome their default browser, which isn't polite. |suffix| is the
82 // user-specific registration suffix; see GetUserSpecificDefaultBrowserSuffix 88 // user-specific registration suffix; see GetUserSpecificDefaultBrowserSuffix
83 // in shell_util.h for details. 89 // in shell_util.h for details.
84 static string16 GetBrowserClientKey(BrowserDistribution* dist, 90 static string16 GetBrowserClientKey(BrowserDistribution* dist,
85 const string16& suffix) { 91 const string16& suffix) {
86 DCHECK(suffix.empty() || suffix[0] == L'.'); 92 DCHECK(suffix.empty() || suffix[0] == L'.');
87 return string16(ShellUtil::kRegStartMenuInternet) 93 return string16(ShellUtil::kRegStartMenuInternet)
88 .append(1, L'\\') 94 .append(1, L'\\')
89 .append(dist->GetApplicationName()) 95 .append(dist->GetApplicationName())
90 .append(suffix); 96 .append(suffix);
91 } 97 }
92 98
93 // Returns the Windows Default Programs capabilities key for Chrome. For 99 // Returns the Windows Default Programs capabilities key for Chrome. For
94 // example: 100 // example:
95 // "Software\Clients\StartMenuInternet\Chromium[.user]\Capabilities". 101 // "Software\Clients\StartMenuInternet\Chromium[.user]\Capabilities".
96 static string16 GetCapabilitiesKey(BrowserDistribution* dist, 102 static string16 GetCapabilitiesKey(BrowserDistribution* dist,
97 const string16& suffix) { 103 const string16& suffix) {
98 return GetBrowserClientKey(dist, suffix).append(L"\\Capabilities"); 104 return GetBrowserClientKey(dist, suffix).append(L"\\Capabilities");
99 } 105 }
100 106
107 // Returns true if Chrome is registered with the given |suffix| either in HKCU
108 // or HKLM.
109 // This function only checks for the existence of
110 // Software\Classes\ChromeHTML|suffix| and doesn't otherwise validate the
111 // registration of Chrome.
112 static bool IsChromeRegisteredWithSuffix(const string16& suffix) {
113 DCHECK(!suffix.empty());
114 string16 chrome_html_prog_id(ShellUtil::kRegClasses);
115 chrome_html_prog_id.push_back(FilePath::kSeparators[0]);
116 chrome_html_prog_id.append(ShellUtil::kChromeHTMLProgId);
117 chrome_html_prog_id.append(suffix);
118 return RegistryEntry(chrome_html_prog_id,
119 ShellUtil::kChromeHTMLProgIdDesc).ExistsInRegistry(
120 LOOK_IN_HKCU & LOOK_IN_HKLM);
121 }
122
101 // This method returns a list of all the registry entries that 123 // This method returns a list of all the registry entries that
102 // are needed to register Chromium ProgIds. 124 // are needed to register Chromium ProgIds.
103 // These entries should be registered in HKCU for user-level installs and in 125 // These entries should be registered in HKCU for user-level installs and in
104 // HKLM for system-level installs. 126 // HKLM for system-level installs.
105 // TODO (gab): Extract the Windows 8 only registrations out of this function. 127 // TODO (gab): Extract the Windows 8 only registrations out of this function.
106 static bool GetProgIdEntries(BrowserDistribution* dist, 128 static bool GetProgIdEntries(BrowserDistribution* dist,
107 const string16& chrome_exe, 129 const string16& chrome_exe,
108 const string16& suffix, 130 const string16& suffix,
109 std::list<RegistryEntry*>* entries) { 131 std::list<RegistryEntry*>* entries) {
110 string16 icon_path(ShellUtil::GetChromeIcon(dist, chrome_exe)); 132 string16 icon_path(ShellUtil::GetChromeIcon(dist, chrome_exe));
(...skipping 132 matching lines...) Expand 10 before | Expand all | Expand 10 after
243 // strings. 265 // strings.
244 entries->push_front(new RegistryEntry(install_info, kReinstallCommand, 266 entries->push_front(new RegistryEntry(install_info, kReinstallCommand,
245 quoted_exe_path + L" --" + ASCIIToWide(switches::kMakeDefaultBrowser))); 267 quoted_exe_path + L" --" + ASCIIToWide(switches::kMakeDefaultBrowser)));
246 entries->push_front(new RegistryEntry(install_info, L"HideIconsCommand", 268 entries->push_front(new RegistryEntry(install_info, L"HideIconsCommand",
247 quoted_exe_path + L" --" + ASCIIToWide(switches::kHideIcons))); 269 quoted_exe_path + L" --" + ASCIIToWide(switches::kHideIcons)));
248 entries->push_front(new RegistryEntry(install_info, L"ShowIconsCommand", 270 entries->push_front(new RegistryEntry(install_info, L"ShowIconsCommand",
249 quoted_exe_path + L" --" + ASCIIToWide(switches::kShowIcons))); 271 quoted_exe_path + L" --" + ASCIIToWide(switches::kShowIcons)));
250 entries->push_front(new RegistryEntry(install_info, L"IconsVisible", 1)); 272 entries->push_front(new RegistryEntry(install_info, L"IconsVisible", 1));
251 273
252 // Register with Default Programs. 274 // Register with Default Programs.
253 string16 app_name(dist->GetApplicationName().append(suffix)); 275 string16 reg_app_name(dist->GetApplicationName().append(suffix));
254 // Tell Windows where to find Chrome's Default Programs info. 276 // Tell Windows where to find Chrome's Default Programs info.
255 string16 capabilities(GetCapabilitiesKey(dist, suffix)); 277 string16 capabilities(GetCapabilitiesKey(dist, suffix));
256 entries->push_front(new RegistryEntry(ShellUtil::kRegRegisteredApplications, 278 entries->push_front(new RegistryEntry(ShellUtil::kRegRegisteredApplications,
257 app_name, capabilities)); 279 reg_app_name, capabilities));
258 // Write out Chrome's Default Programs info. 280 // Write out Chrome's Default Programs info.
259 // TODO(grt): http://crbug.com/75152 Write a reference to a localized 281 // TODO(grt): http://crbug.com/75152 Write a reference to a localized
260 // resource rather than this. 282 // resource rather than this.
261 entries->push_front(new RegistryEntry( 283 entries->push_front(new RegistryEntry(
262 capabilities, ShellUtil::kRegApplicationDescription, 284 capabilities, ShellUtil::kRegApplicationDescription,
263 dist->GetLongAppDescription())); 285 dist->GetLongAppDescription()));
264 entries->push_front(new RegistryEntry( 286 entries->push_front(new RegistryEntry(
265 capabilities, ShellUtil::kRegApplicationIcon, icon_path)); 287 capabilities, ShellUtil::kRegApplicationIcon, icon_path));
266 entries->push_front(new RegistryEntry( 288 entries->push_front(new RegistryEntry(
267 capabilities, ShellUtil::kRegApplicationName, app_name)); 289 capabilities, ShellUtil::kRegApplicationName,
290 dist->GetApplicationName()));
268 291
269 entries->push_front(new RegistryEntry(capabilities + L"\\Startmenu", 292 entries->push_front(new RegistryEntry(capabilities + L"\\Startmenu",
270 L"StartMenuInternet", app_name)); 293 L"StartMenuInternet", reg_app_name));
271 294
272 string16 html_prog_id(ShellUtil::kChromeHTMLProgId); 295 string16 html_prog_id(ShellUtil::kChromeHTMLProgId);
273 html_prog_id.append(suffix); 296 html_prog_id.append(suffix);
274 for (int i = 0; ShellUtil::kFileAssociations[i] != NULL; i++) { 297 for (int i = 0; ShellUtil::kFileAssociations[i] != NULL; i++) {
275 entries->push_front(new RegistryEntry( 298 entries->push_front(new RegistryEntry(
276 capabilities + L"\\FileAssociations", 299 capabilities + L"\\FileAssociations",
277 ShellUtil::kFileAssociations[i], html_prog_id)); 300 ShellUtil::kFileAssociations[i], html_prog_id));
278 } 301 }
279 for (int i = 0; ShellUtil::kPotentialProtocolAssociations[i] != NULL; 302 for (int i = 0; ShellUtil::kPotentialProtocolAssociations[i] != NULL;
280 i++) { 303 i++) {
(...skipping 89 matching lines...) Expand 10 before | Expand all | Expand 10 after
370 if (_is_string) { 393 if (_is_string) {
371 items->AddSetRegValueWorkItem(root, _key_path, _name, _value, true); 394 items->AddSetRegValueWorkItem(root, _key_path, _name, _value, true);
372 } else { 395 } else {
373 items->AddSetRegValueWorkItem(root, _key_path, _name, _int_value, true); 396 items->AddSetRegValueWorkItem(root, _key_path, _name, _int_value, true);
374 } 397 }
375 } 398 }
376 399
377 // Checks if the current registry entry exists in HKCU\|_key_path|\|_name| 400 // Checks if the current registry entry exists in HKCU\|_key_path|\|_name|
378 // and value is |_value|. If the key does NOT exist in HKCU, checks for 401 // and value is |_value|. If the key does NOT exist in HKCU, checks for
379 // the correct name and value in HKLM. 402 // the correct name and value in HKLM.
380 // This mimics Windows' behavior when searching in HKCR (HKCU takes precedence 403 // |look_for_in| specifies roots (HKCU and/or HKLM) in which to look for the
381 // over HKLM). For registrations outside of HKCR on versions of Windows up 404 // key, unspecified roots are not looked in and the key is assumed not to
382 // to Win7, Chrome's values go in HKLM. This function will make unnecessary 405 // exist in them.
383 // (but harmless) queries into HKCU in that case. Starting with Windows 8, 406 // |look_for_in| must at least specify one root to look into.
384 // Chrome's values go in HKCU for user-level installs, which takes precedence 407 // If |look_for_in| specifies both HKCU and HKLM, this method mimics Windows'
385 // over HKLM. 408 // behavior when searching in HKCR (HKCU takes precedence over HKLM). For
386 bool ExistsInRegistry() { 409 // registrations outside of HKCR on versions of Windows up to Win7, Chrome's
387 RegistryStatus hkcu_status = StatusInRegistryUnderRoot(HKEY_CURRENT_USER); 410 // values go in HKLM. This function will make unnecessary (but harmless)
411 // queries into HKCU in that case. Starting with Windows 8, Chrome's values go
412 // in HKCU for user-level installs, which takes precedence over HKLM.
413 bool ExistsInRegistry(uint32 look_for_in) const {
414 DCHECK(look_for_in);
415
416 RegistryStatus hkcu_status = DOES_NOT_EXIST;
417 if (look_for_in & LOOK_IN_HKCU)
418 hkcu_status = StatusInRegistryUnderRoot(HKEY_CURRENT_USER);
419
420 RegistryStatus hklm_status = DOES_NOT_EXIST;
421 if (hkcu_status == DOES_NOT_EXIST && (look_for_in & LOOK_IN_HKLM))
422 hklm_status = StatusInRegistryUnderRoot(HKEY_LOCAL_MACHINE);
423
388 return (hkcu_status == SAME_VALUE || 424 return (hkcu_status == SAME_VALUE ||
389 (hkcu_status == DOES_NOT_EXIST && 425 (hkcu_status == DOES_NOT_EXIST && hklm_status == SAME_VALUE));
390 StatusInRegistryUnderRoot(HKEY_LOCAL_MACHINE) == SAME_VALUE));
391 } 426 }
392 427
393 private: 428 private:
394 // States this RegistryKey can be in compared to the registry. 429 // States this RegistryKey can be in compared to the registry.
395 enum RegistryStatus { 430 enum RegistryStatus {
396 // |_name| does not exist in the registry 431 // |_name| does not exist in the registry
397 DOES_NOT_EXIST, 432 DOES_NOT_EXIST,
398 // |_name| exists, but its value != |_value| 433 // |_name| exists, but its value != |_value|
399 DIFFERENT_VALUE, 434 DIFFERENT_VALUE,
400 // |_name| exists and its value is |_value| 435 // |_name| exists and its value is |_value|
(...skipping 63 matching lines...) Expand 10 before | Expand all | Expand 10 after
464 499
465 // Apply all the registry changes and if there is a problem, rollback 500 // Apply all the registry changes and if there is a problem, rollback
466 if (!items->Do()) { 501 if (!items->Do()) {
467 items->Rollback(); 502 items->Rollback();
468 return false; 503 return false;
469 } 504 }
470 return true; 505 return true;
471 } 506 }
472 507
473 // Checks that all |entries| are present on this computer. 508 // Checks that all |entries| are present on this computer.
474 bool AreEntriesRegistered(const std::list<RegistryEntry*>& entries) { 509 // |look_for_in| is passed to RegistryEntry::ExistsInRegistry(). Documentation
510 // for it can be found there.
511 bool AreEntriesRegistered(const std::list<RegistryEntry*>& entries,
512 uint32 look_for_in) {
475 bool registered = true; 513 bool registered = true;
476 for (std::list<RegistryEntry*>::const_iterator itr = entries.begin(); 514 for (std::list<RegistryEntry*>::const_iterator itr = entries.begin();
477 registered && itr != entries.end(); ++itr) { 515 registered && itr != entries.end(); ++itr) {
478 // We do not need registered = registered && ... since the loop condition 516 // We do not need registered = registered && ... since the loop condition
479 // is set to exit early. 517 // is set to exit early.
480 registered = (*itr)->ExistsInRegistry(); 518 registered = (*itr)->ExistsInRegistry(look_for_in);
481 } 519 }
482 return registered; 520 return registered;
483 } 521 }
484 522
485 // Checks that all required registry entries for Chrome are already present 523 // Checks that all required registry entries for Chrome are already present
486 // on this computer. 524 // on this computer.
487 bool IsChromeRegistered(BrowserDistribution* dist, 525 bool IsChromeRegistered(BrowserDistribution* dist,
488 const string16& chrome_exe, 526 const string16& chrome_exe,
489 const string16& suffix) { 527 const string16& suffix) {
490 std::list<RegistryEntry*> entries; 528 std::list<RegistryEntry*> entries;
491 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries); 529 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
492 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, &entries); 530 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, &entries);
493 RegistryEntry::GetSystemEntries(dist, chrome_exe, suffix, &entries); 531 RegistryEntry::GetSystemEntries(dist, chrome_exe, suffix, &entries);
494 return AreEntriesRegistered(entries); 532 return AreEntriesRegistered(
533 entries, RegistryEntry::LOOK_IN_HKCU & RegistryEntry::LOOK_IN_HKLM);
495 } 534 }
496 535
497 // This method checks if Chrome is already registered on the local machine 536 // This method checks if Chrome is already registered on the local machine
498 // for the requested protocol. It just checks the one value required for this. 537 // for the requested protocol. It just checks the one value required for this.
499 bool IsChromeRegisteredForProtocol(BrowserDistribution* dist, 538 bool IsChromeRegisteredForProtocol(BrowserDistribution* dist,
500 const string16& suffix, 539 const string16& suffix,
501 const string16& protocol) { 540 const string16& protocol) {
502 std::list<RegistryEntry*> entries; 541 std::list<RegistryEntry*> entries;
503 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries); 542 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
504 RegistryEntry::GetProtocolCapabilityEntries(dist, suffix, protocol, &entries); 543 RegistryEntry::GetProtocolCapabilityEntries(dist, suffix, protocol, &entries);
505 return AreEntriesRegistered(entries); 544 return AreEntriesRegistered(
545 entries, RegistryEntry::LOOK_IN_HKCU & RegistryEntry::LOOK_IN_HKLM);
506 } 546 }
507 547
508 // This method registers Chrome on Vista by launching an elevated setup.exe. 548 // This method registers Chrome on Vista by launching an elevated setup.exe.
509 // That will show the user the standard Vista elevation prompt. If the user 549 // That will show the user the standard Vista elevation prompt. If the user
510 // accepts it the new process will make the necessary changes and return SUCCESS 550 // accepts it the new process will make the necessary changes and return SUCCESS
511 // that we capture and return. 551 // that we capture and return.
512 // If protocol is non-empty we will also register Chrome as being capable of 552 // If protocol is non-empty we will also register Chrome as being capable of
513 // handling the protocol. 553 // handling the protocol.
514 bool ElevateAndRegisterChrome(BrowserDistribution* dist, 554 bool ElevateAndRegisterChrome(BrowserDistribution* dist,
515 const string16& chrome_exe, 555 const string16& chrome_exe,
(...skipping 32 matching lines...) Expand 10 before | Expand all | Expand 10 after
548 } 588 }
549 589
550 DWORD ret_val = 0; 590 DWORD ret_val = 0;
551 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val); 591 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val);
552 if (ret_val == 0) 592 if (ret_val == 0)
553 return true; 593 return true;
554 } 594 }
555 return false; 595 return false;
556 } 596 }
557 597
558 // This method tries to figure out if another user has already registered her
559 // own copy of Chrome so that we can avoid overwriting it and append current
560 // user's login name to default browser registry entries. This function is
561 // not meant to detect all cases. It just tries to handle the most common case.
562 // All the conditions below have to be true for it to return true:
563 // - Software\Clients\StartMenuInternet\Chromium\"" key should have a valid
564 // value.
565 // - The value should not be same as given value in |chrome_exe|
566 // - Finally to handle the default install path (C:\Document and Settings\
567 // <user>\Local Settings\Application Data\Chromium\Application) the value
568 // of the above key should differ from |chrome_exe| only in user name.
569 bool AnotherUserHasDefaultBrowser(BrowserDistribution* dist,
570 const string16& chrome_exe) {
571 const string16 reg_key(
572 RegistryEntry::GetBrowserClientKey(dist, string16())
573 .append(ShellUtil::kRegShellOpen));
574 RegKey key(HKEY_LOCAL_MACHINE, reg_key.c_str(), KEY_READ);
575 string16 registry_chrome_exe;
576 if ((key.ReadValue(L"", &registry_chrome_exe) != ERROR_SUCCESS) ||
577 registry_chrome_exe.length() < 2)
578 return false;
579
580 registry_chrome_exe = registry_chrome_exe.substr(1,
581 registry_chrome_exe.length() - 2);
582 if ((registry_chrome_exe.size() == chrome_exe.size()) &&
583 (std::equal(chrome_exe.begin(), chrome_exe.end(),
584 registry_chrome_exe.begin(),
585 base::CaseInsensitiveCompare<wchar_t>()))) {
586 return false;
587 }
588
589 std::vector<string16> v1, v2;
590 base::SplitString(registry_chrome_exe, L'\\', &v1);
591 base::SplitString(chrome_exe, L'\\', &v2);
592 if (v1.empty() || v2.empty() || v1.size() != v2.size())
593 return false;
594
595 // Now check that only one of the values within two '\' chars differ.
596 std::vector<string16>::iterator itr1 = v1.begin();
597 std::vector<string16>::iterator itr2 = v2.begin();
598 bool one_mismatch = false;
599 for ( ; itr1 < v1.end() && itr2 < v2.end(); ++itr1, ++itr2) {
600 string16 s1 = *itr1;
601 string16 s2 = *itr2;
602 if ((s1.size() != s2.size()) ||
603 (!std::equal(s1.begin(), s1.end(),
604 s2.begin(), base::CaseInsensitiveCompare<wchar_t>()))) {
605 if (one_mismatch)
606 return false;
607 else
608 one_mismatch = true;
609 }
610 }
611 return true;
612 }
613
614 // Launches the Windows 7 and Windows 8 application association dialog, which 598 // Launches the Windows 7 and Windows 8 application association dialog, which
615 // is the only documented way to make a browser the default browser on 599 // is the only documented way to make a browser the default browser on
616 // Windows 8. 600 // Windows 8.
617 bool LaunchApplicationAssociationDialog(const string16& app_id) { 601 bool LaunchApplicationAssociationDialog(const string16& app_id) {
618 base::win::ScopedComPtr<IApplicationAssociationRegistrationUI> aarui; 602 base::win::ScopedComPtr<IApplicationAssociationRegistrationUI> aarui;
619 HRESULT hr = aarui.CreateInstance(CLSID_ApplicationAssociationRegistrationUI); 603 HRESULT hr = aarui.CreateInstance(CLSID_ApplicationAssociationRegistrationUI);
620 if (FAILED(hr)) 604 if (FAILED(hr))
621 return false; 605 return false;
622 hr = aarui->LaunchAdvancedAssociationUI(app_id.c_str()); 606 hr = aarui->LaunchAdvancedAssociationUI(app_id.c_str());
623 return SUCCEEDED(hr); 607 return SUCCEEDED(hr);
(...skipping 46 matching lines...) Expand 10 before | Expand all | Expand 10 after
670 key = ShellUtil::kRegClasses; 654 key = ShellUtil::kRegClasses;
671 key.push_back(FilePath::kSeparators[0]); 655 key.push_back(FilePath::kSeparators[0]);
672 key.append(ShellUtil::kChromeHTMLProgId); 656 key.append(ShellUtil::kChromeHTMLProgId);
673 key.append(suffix); 657 key.append(suffix);
674 key.append(ShellUtil::kRegShellOpen); 658 key.append(ShellUtil::kRegShellOpen);
675 InstallUtil::DeleteRegistryValue(root_key, key, 659 InstallUtil::DeleteRegistryValue(root_key, key,
676 ShellUtil::kRegDelegateExecute); 660 ShellUtil::kRegDelegateExecute);
677 } 661 }
678 } 662 }
679 663
664 // Returns true if the current user-level install has been registered without
665 // a suffix. This is used to support old installs where we used to not suffix
666 // user-level installs if they were the first to request the non-suffixed
667 // registry entries on the machine. This however created clashes with
668 // system-level installs when updating user-level installs to system-level
669 // installs. There is no downside to suffixing anyways (the name registered is
670 // not user visible).
671 // All the conditions below have to be true for it to return true:
672 // - Software\Clients\StartMenuInternet\Chromium\"" key should have a valid
673 // value.
674 // - The value should be same as given value in |chrome_exe|.
675 bool CurrentUserIsRegisteredWithNoSuffix(BrowserDistribution* dist,
676 const string16& chrome_exe) {
677 // Assert that a Chrome is registered under
678 // HKLM\Software\Clients\StartMenuInternet\Google Chrome
679 const string16 reg_key(
680 RegistryEntry::GetBrowserClientKey(dist, string16())
681 .append(ShellUtil::kRegShellOpen));
682 RegKey key(HKEY_LOCAL_MACHINE, reg_key.c_str(), KEY_READ);
683 string16 registry_chrome_exe;
684 if ((key.ReadValue(L"", &registry_chrome_exe) != ERROR_SUCCESS) ||
685 registry_chrome_exe.length() < 2) {
686 return false;
687 }
688
689 // Assert that the Chrome registered there is |chrome_exe| (i.e. this user's
690 // Chrome).
691 registry_chrome_exe = registry_chrome_exe.substr(1,
692 registry_chrome_exe.length() - 2);
693 if (registry_chrome_exe.size() != chrome_exe.size() ||
694 !std::equal(chrome_exe.begin(), chrome_exe.end(),
695 registry_chrome_exe.begin(),
696 base::CaseInsensitiveCompare<wchar_t>())) {
697 return false;
698 }
699
700 return true;
701 }
702
703 // Sets |suffix| to this user's username preceded by a dot. This suffix is then
704 // meant to be added to all registration that may conflict with another
705 // user-level Chrome install.
706 // Returns true unless the OS call to retrieve the username fails.
707 bool GetUserSpecificRegistrySuffix(string16* suffix) {
708 wchar_t user_name[256];
709 DWORD size = arraysize(user_name);
710 if (::GetUserName(user_name, &size) == 0 || size < 1) {
711 LOG(DFATAL) << "Failed to get username in " << __FUNCTION__ << ".";
712 return false;
713 }
714 suffix->reserve(size);
715 suffix->assign(1, L'.');
716 suffix->append(user_name, size - 1);
717 return true;
718 }
719
720 // Sets |suffix| to the current user's username on user-level installs.
721 // To support old-style user-level installs however, |suffix| is cleared if
722 // the user currently owns the non-suffixed HKLM registrations.
723 // |suffix| is also cleared on system-level installs.
724 // |suffix| should then be appended to all Chrome properties that may conflict
725 // with other Chrome user-level installs.
726 // Returns true unless one of the underlying calls fails.
727 bool GetInstallationSpecificSuffix(BrowserDistribution* dist,
728 const string16& chrome_exe,
729 string16* suffix) {
730 if (!InstallUtil::IsPerUserInstall(chrome_exe.c_str()) ||
731 CurrentUserIsRegisteredWithNoSuffix(dist, chrome_exe)) {
732 // No suffix on system-level installs and user-level installs already
733 // registered with no suffix.
734 *suffix = L"";
735 return true;
736 } else {
737 return GetUserSpecificRegistrySuffix(suffix);
738 }
739 }
740
680 } // namespace 741 } // namespace
681 742
682 const wchar_t* ShellUtil::kRegDefaultIcon = L"\\DefaultIcon"; 743 const wchar_t* ShellUtil::kRegDefaultIcon = L"\\DefaultIcon";
683 const wchar_t* ShellUtil::kRegShellPath = L"\\shell"; 744 const wchar_t* ShellUtil::kRegShellPath = L"\\shell";
684 const wchar_t* ShellUtil::kRegShellOpen = L"\\shell\\open\\command"; 745 const wchar_t* ShellUtil::kRegShellOpen = L"\\shell\\open\\command";
685 const wchar_t* ShellUtil::kRegStartMenuInternet = 746 const wchar_t* ShellUtil::kRegStartMenuInternet =
686 L"Software\\Clients\\StartMenuInternet"; 747 L"Software\\Clients\\StartMenuInternet";
687 const wchar_t* ShellUtil::kRegClasses = L"Software\\Classes"; 748 const wchar_t* ShellUtil::kRegClasses = L"Software\\Classes";
688 const wchar_t* ShellUtil::kRegRegisteredApplications = 749 const wchar_t* ShellUtil::kRegRegisteredApplications =
689 L"Software\\RegisteredApplications"; 750 L"Software\\RegisteredApplications";
(...skipping 226 matching lines...) Expand 10 before | Expand all | Expand 10 after
916 KEY_QUERY_VALUE) != ERROR_SUCCESS || 977 KEY_QUERY_VALUE) != ERROR_SUCCESS ||
917 key.ReadValue(kReinstallCommand, &command) != ERROR_SUCCESS) { 978 key.ReadValue(kReinstallCommand, &command) != ERROR_SUCCESS) {
918 continue; 979 continue;
919 } 980 }
920 if (!name.empty() && !command.empty() && 981 if (!name.empty() && !command.empty() &&
921 name.find(dist->GetApplicationName()) == string16::npos) 982 name.find(dist->GetApplicationName()) == string16::npos)
922 (*browsers)[name] = command; 983 (*browsers)[name] = command;
923 } 984 }
924 } 985 }
925 986
926 bool ShellUtil::GetUserSpecificDefaultBrowserSuffix(BrowserDistribution* dist, 987 string16 ShellUtil::GetCurrentInstallationSuffix() {
927 string16* entry) { 988 string16 tested_suffix;
928 wchar_t user_name[256]; 989 if (!GetUserSpecificRegistrySuffix(&tested_suffix) ||
929 DWORD size = arraysize(user_name); 990 !RegistryEntry::IsChromeRegisteredWithSuffix(tested_suffix)) {
930 if (::GetUserName(user_name, &size) == 0 || size < 1) 991 return L"";
931 return false; 992 }
932 entry->reserve(size); 993 return tested_suffix;
933 entry->assign(1, L'.'); 994 }
934 entry->append(user_name, size - 1);
935 995
936 return RegKey(HKEY_LOCAL_MACHINE, 996 bool ShellUtil::IsInstallationPresentInHKLM(BrowserDistribution* dist,
937 RegistryEntry::GetBrowserClientKey(dist, *entry).c_str(), 997 const string16& chrome_exe,
938 KEY_READ).Valid(); 998 const string16& suffix) {
999 std::list<RegistryEntry*> entries;
1000 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
1001 RegistryEntry::GetSystemEntries(dist, chrome_exe, suffix, &entries);
1002 return AreEntriesRegistered(entries, RegistryEntry::LOOK_IN_HKLM);
939 } 1003 }
940 1004
941 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist, 1005 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist,
942 int shell_change, 1006 int shell_change,
943 const string16& chrome_exe, 1007 const string16& chrome_exe,
944 bool elevate_if_not_admin) { 1008 bool elevate_if_not_admin) {
945 if (!dist->CanSetAsDefault()) 1009 if (!dist->CanSetAsDefault())
946 return false; 1010 return false;
947 1011
948 ShellUtil::RegisterChromeBrowser(dist, chrome_exe, L"", elevate_if_not_admin); 1012 ShellUtil::RegisterChromeBrowser(dist, chrome_exe, L"", elevate_if_not_admin);
949 1013
950 bool ret = true; 1014 bool ret = true;
951 // First use the new "recommended" way on Vista to make Chrome default 1015 // First use the new "recommended" way on Vista to make Chrome default
952 // browser. 1016 // browser.
953 string16 app_name = dist->GetApplicationName(); 1017 string16 app_name = dist->GetApplicationName();
954 string16 app_suffix; 1018 const string16 app_suffix(ShellUtil::GetCurrentInstallationSuffix());
955 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &app_suffix)) 1019 app_name += app_suffix;
956 app_name += app_suffix;
957 1020
958 if (base::win::GetVersion() >= base::win::VERSION_WIN8) { 1021 if (base::win::GetVersion() >= base::win::VERSION_WIN8) {
959 // On Windows 8, you can't set yourself as the default handler 1022 // On Windows 8, you can't set yourself as the default handler
960 // programatically. In other words IApplicationAssociationRegistration 1023 // programatically. In other words IApplicationAssociationRegistration
961 // has been rendered useless. What you can do is to launch 1024 // has been rendered useless. What you can do is to launch
962 // "Set Program Associations" section of the "Default Programs" 1025 // "Set Program Associations" section of the "Default Programs"
963 // control panel. This action does not require elevation and we 1026 // control panel. This action does not require elevation and we
964 // don't get to control window activation. More info at: 1027 // don't get to control window activation. More info at:
965 // http://msdn.microsoft.com/en-us/library/cc144154(VS.85).aspx 1028 // http://msdn.microsoft.com/en-us/library/cc144154(VS.85).aspx
966 return LaunchApplicationAssociationDialog(app_name.c_str()); 1029 return LaunchApplicationAssociationDialog(app_name.c_str());
(...skipping 29 matching lines...) Expand all
996 } 1059 }
997 } 1060 }
998 1061
999 // Now use the old way to associate Chrome with supported protocols and file 1062 // Now use the old way to associate Chrome with supported protocols and file
1000 // associations. This should not be required on Vista but since some 1063 // associations. This should not be required on Vista but since some
1001 // applications still read Software\Classes\http key directly, we have to do 1064 // applications still read Software\Classes\http key directly, we have to do
1002 // this on Vista also. 1065 // this on Vista also.
1003 1066
1004 std::list<RegistryEntry*> entries; 1067 std::list<RegistryEntry*> entries;
1005 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries); 1068 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
1006 string16 suffix; 1069 RegistryEntry::GetUserEntries(dist, chrome_exe, app_suffix, &entries);
1007 if (!GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
1008 suffix = L"";
1009 RegistryEntry::GetUserEntries(dist, chrome_exe, suffix, &entries);
1010 // Change the default browser for current user. 1070 // Change the default browser for current user.
1011 if ((shell_change & ShellUtil::CURRENT_USER) && 1071 if ((shell_change & ShellUtil::CURRENT_USER) &&
1012 !AddRegistryEntries(HKEY_CURRENT_USER, entries)) { 1072 !AddRegistryEntries(HKEY_CURRENT_USER, entries)) {
1013 ret = false; 1073 ret = false;
1014 LOG(ERROR) << "Could not make Chrome default browser (XP/current user)."; 1074 LOG(ERROR) << "Could not make Chrome default browser (XP/current user).";
1015 } 1075 }
1016 1076
1017 // Chrome as default browser at system level. 1077 // Chrome as default browser at system level.
1018 if ((shell_change & ShellUtil::SYSTEM_LEVEL) && 1078 if ((shell_change & ShellUtil::SYSTEM_LEVEL) &&
1019 !AddRegistryEntries(HKEY_LOCAL_MACHINE, entries)) { 1079 !AddRegistryEntries(HKEY_LOCAL_MACHINE, entries)) {
(...skipping 19 matching lines...) Expand all
1039 // First use the new "recommended" way on Vista to make Chrome default 1099 // First use the new "recommended" way on Vista to make Chrome default
1040 // protocol handler. 1100 // protocol handler.
1041 if (base::win::GetVersion() >= base::win::VERSION_VISTA) { 1101 if (base::win::GetVersion() >= base::win::VERSION_VISTA) {
1042 VLOG(1) << "Registering Chrome as default handler for " << protocol 1102 VLOG(1) << "Registering Chrome as default handler for " << protocol
1043 << " on Vista."; 1103 << " on Vista.";
1044 base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR; 1104 base::win::ScopedComPtr<IApplicationAssociationRegistration> pAAR;
1045 HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration, 1105 HRESULT hr = pAAR.CreateInstance(CLSID_ApplicationAssociationRegistration,
1046 NULL, CLSCTX_INPROC); 1106 NULL, CLSCTX_INPROC);
1047 if (SUCCEEDED(hr)) { 1107 if (SUCCEEDED(hr)) {
1048 string16 app_name = dist->GetApplicationName(); 1108 string16 app_name = dist->GetApplicationName();
1049 string16 suffix; 1109 app_name += ShellUtil::GetCurrentInstallationSuffix();
1050 if (ShellUtil::GetUserSpecificDefaultBrowserSuffix(dist, &suffix))
1051 app_name += suffix;
1052 1110
1053 hr = pAAR->SetAppAsDefault(app_name.c_str(), protocol.c_str(), 1111 hr = pAAR->SetAppAsDefault(app_name.c_str(), protocol.c_str(),
1054 AT_URLPROTOCOL); 1112 AT_URLPROTOCOL);
1055 } 1113 }
1056 if (!SUCCEEDED(hr)) { 1114 if (!SUCCEEDED(hr)) {
1057 ret = false; 1115 ret = false;
1058 LOG(ERROR) << "Could not make Chrome default protocol client (Vista):" 1116 LOG(ERROR) << "Could not make Chrome default protocol client (Vista):"
1059 << " HRESULT=" << hr << "."; 1117 << " HRESULT=" << hr << ".";
1060 } 1118 }
1061 } 1119 }
1062 1120
1063 // Now use the old way to associate Chrome with the desired protocol. This 1121 // Now use the old way to associate Chrome with the desired protocol. This
1064 // should not be required on Vista but since some applications still read 1122 // should not be required on Vista but since some applications still read
1065 // Software\Classes\http key directly, we have to do this on Vista also. 1123 // Software\Classes\http key directly, we have to do this on Vista also.
1066 1124
1067 std::list<RegistryEntry*> entries; 1125 std::list<RegistryEntry*> entries;
1068 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries); 1126 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
1069 string16 suffix; 1127 const string16 suffix(ShellUtil::GetCurrentInstallationSuffix());
1070 if (!GetUserSpecificDefaultBrowserSuffix(dist, &suffix)) 1128 const string16 chrome_open(ShellUtil::GetChromeShellOpenCmd(chrome_exe));
1071 suffix = L""; 1129 const string16 chrome_icon(ShellUtil::GetChromeIcon(dist, chrome_exe));
1072 string16 chrome_open = ShellUtil::GetChromeShellOpenCmd(chrome_exe);
1073 string16 chrome_icon = ShellUtil::GetChromeIcon(dist, chrome_exe);
1074 RegistryEntry::GetUserProtocolEntries(protocol, chrome_icon, chrome_open, 1130 RegistryEntry::GetUserProtocolEntries(protocol, chrome_icon, chrome_open,
1075 &entries); 1131 &entries);
1076 // Change the default protocol handler for current user. 1132 // Change the default protocol handler for current user.
1077 if (!AddRegistryEntries(HKEY_CURRENT_USER, entries)) { 1133 if (!AddRegistryEntries(HKEY_CURRENT_USER, entries)) {
1078 ret = false; 1134 ret = false;
1079 LOG(ERROR) << "Could not make Chrome default protocol client (XP)."; 1135 LOG(ERROR) << "Could not make Chrome default protocol client (XP).";
1080 } 1136 }
1081 1137
1082 return ret; 1138 return ret;
1083 } 1139 }
1084 1140
1085 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist, 1141 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist,
1086 const string16& chrome_exe, 1142 const string16& chrome_exe,
1087 const string16& unique_suffix, 1143 const string16& unique_suffix,
1088 bool elevate_if_not_admin) { 1144 bool elevate_if_not_admin) {
1089 if (!dist->CanSetAsDefault()) 1145 if (!dist->CanSetAsDefault())
1090 return false; 1146 return false;
1091 1147
1092 // First figure out we need to append a suffix to the registry entries to
1093 // make them unique.
1094 string16 suffix; 1148 string16 suffix;
1095 if (!unique_suffix.empty()) { 1149 if (!unique_suffix.empty()) {
1096 suffix = unique_suffix; 1150 suffix = unique_suffix;
1097 } else if (InstallUtil::IsPerUserInstall(chrome_exe.c_str()) && 1151 } else if (!GetInstallationSpecificSuffix(dist, chrome_exe, &suffix)) {
1098 !GetUserSpecificDefaultBrowserSuffix(dist, &suffix) && 1152 return false;
1099 !AnotherUserHasDefaultBrowser(dist, chrome_exe)) {
1100 suffix = L"";
1101 } 1153 }
1102 1154
1103 // TODO(grt): remove this on or after 2012-08-01; see impl for details. 1155 // TODO(grt): remove this on or after 2012-08-01; see impl for details.
1104 RemoveBadWindows8RegistrationIfNeeded(dist, chrome_exe, suffix); 1156 RemoveBadWindows8RegistrationIfNeeded(dist, chrome_exe, suffix);
1105 1157
1106 // Check if Chromium is already registered with this suffix. 1158 // Check if Chromium is already registered with this suffix.
1107 if (IsChromeRegistered(dist, chrome_exe, suffix)) 1159 if (IsChromeRegistered(dist, chrome_exe, suffix))
1108 return true; 1160 return true;
1109 1161
1110 // If user is an admin try to register and return the status. 1162 // If user is an admin try to register and return the status.
(...skipping 10 matching lines...) Expand all
1121 progids) && 1173 progids) &&
1122 AddRegistryEntries(HKEY_LOCAL_MACHINE, sys_entries); 1174 AddRegistryEntries(HKEY_LOCAL_MACHINE, sys_entries);
1123 } 1175 }
1124 1176
1125 // If user is not an admin and OS is Vista, try to elevate and register. 1177 // If user is not an admin and OS is Vista, try to elevate and register.
1126 if (elevate_if_not_admin && 1178 if (elevate_if_not_admin &&
1127 base::win::GetVersion() >= base::win::VERSION_VISTA && 1179 base::win::GetVersion() >= base::win::VERSION_VISTA &&
1128 ElevateAndRegisterChrome(dist, chrome_exe, suffix, L"")) 1180 ElevateAndRegisterChrome(dist, chrome_exe, suffix, L""))
1129 return true; 1181 return true;
1130 1182
1131 // If we got to this point then all we can do is create ProgIds under HKCU 1183 // If we got to this point then all we can do is create ProgIds under HKCU.
1132 // on XP as well as Vista.
1133 std::list<RegistryEntry*> entries; 1184 std::list<RegistryEntry*> entries;
1134 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries); 1185 STLElementDeleter<std::list<RegistryEntry*> > entries_deleter(&entries);
1135 RegistryEntry::GetProgIdEntries(dist, chrome_exe, L"", &entries); 1186 RegistryEntry::GetProgIdEntries(dist, chrome_exe, L"", &entries);
1187 // Prefer to use |suffix|; unless Chrome's ProgIds are already registered with
1188 // no suffix (as per the old registration style): in which case some other
1189 // registry entries could refer to them and since we were not able to set our
1190 // HKLM entries above, we are better off not altering these here.
1191 if (!suffix.empty() &&
1192 !AreEntriesRegistered(entries, RegistryEntry::LOOK_IN_HKCU)) {
1193 entries.clear();
1194 RegistryEntry::GetProgIdEntries(dist, chrome_exe, suffix, &entries);
1195 }
1136 return AddRegistryEntries(HKEY_CURRENT_USER, entries); 1196 return AddRegistryEntries(HKEY_CURRENT_USER, entries);
1137 } 1197 }
1138 1198
1139 bool ShellUtil::RegisterChromeForProtocol(BrowserDistribution* dist, 1199 bool ShellUtil::RegisterChromeForProtocol(BrowserDistribution* dist,
1140 const string16& chrome_exe, 1200 const string16& chrome_exe,
1141 const string16& unique_suffix, 1201 const string16& unique_suffix,
1142 const string16& protocol, 1202 const string16& protocol,
1143 bool elevate_if_not_admin) { 1203 bool elevate_if_not_admin) {
1144 if (!dist->CanSetAsDefault()) 1204 if (!dist->CanSetAsDefault())
1145 return false; 1205 return false;
1146 1206
1147 // Figure out we need to append a suffix to the registry entries to
1148 // make them unique.
1149 string16 suffix; 1207 string16 suffix;
1150 if (!unique_suffix.empty()) { 1208 if (!unique_suffix.empty()) {
1151 suffix = unique_suffix; 1209 suffix = unique_suffix;
1152 } else if (InstallUtil::IsPerUserInstall(chrome_exe.c_str()) && 1210 } else if (!GetInstallationSpecificSuffix(dist, chrome_exe, &suffix)) {
1153 !GetUserSpecificDefaultBrowserSuffix(dist, &suffix) && 1211 return false;
1154 !AnotherUserHasDefaultBrowser(dist, chrome_exe)) {
1155 suffix = L"";
1156 } 1212 }
1157 1213
1158 // Check if Chromium is already registered with this suffix. 1214 // Check if Chromium is already registered with this suffix.
1159 if (IsChromeRegisteredForProtocol(dist, suffix, protocol)) 1215 if (IsChromeRegisteredForProtocol(dist, suffix, protocol))
1160 return true; 1216 return true;
1161 1217
1162 if (IsUserAnAdmin()) { 1218 if (IsUserAnAdmin()) {
1163 // We can do this operation directly. 1219 // We can do this operation directly.
1164 // If we're not registered at all, try to register. If that fails 1220 // If we're not registered at all, try to register. If that fails
1165 // we should give up. 1221 // we should give up.
(...skipping 125 matching lines...) Expand 10 before | Expand all | Expand 10 after
1291 chrome_exe.c_str(), 1347 chrome_exe.c_str(),
1292 shortcut.c_str(), 1348 shortcut.c_str(),
1293 chrome_path.c_str(), 1349 chrome_path.c_str(),
1294 arguments.c_str(), 1350 arguments.c_str(),
1295 description.c_str(), 1351 description.c_str(),
1296 icon_path.c_str(), 1352 icon_path.c_str(),
1297 icon_index, 1353 icon_index,
1298 dist->GetBrowserAppId().c_str(), 1354 dist->GetBrowserAppId().c_str(),
1299 ConvertShellUtilShortcutOptionsToFileUtil(options)); 1355 ConvertShellUtilShortcutOptionsToFileUtil(options));
1300 } 1356 }
OLDNEW
« chrome/installer/setup/uninstall.cc ('K') | « chrome/installer/util/shell_util.h ('k') | no next file » | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698