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

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

Issue 1896513002: Fix registerProtocolHandler implementation on Windows 8 and 10. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Created 4 years, 8 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch
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 62 matching lines...) Expand 10 before | Expand all | Expand 10 after
73 // Defaut Programs). These registrations can be in HKCU as of Windows 8. 73 // Defaut Programs). These registrations can be in HKCU as of Windows 8.
74 // Note: Shell registration implies ProgId registration. 74 // Note: Shell registration implies ProgId registration.
75 CONFIRM_SHELL_REGISTRATION, 75 CONFIRM_SHELL_REGISTRATION,
76 // Same as CONFIRM_SHELL_REGISTRATION, but only look in HKLM (used when 76 // Same as CONFIRM_SHELL_REGISTRATION, but only look in HKLM (used when
77 // uninstalling to know whether elevation is required to clean up the 77 // uninstalling to know whether elevation is required to clean up the
78 // registry). 78 // registry).
79 CONFIRM_SHELL_REGISTRATION_IN_HKLM, 79 CONFIRM_SHELL_REGISTRATION_IN_HKLM,
80 }; 80 };
81 81
82 const wchar_t kReinstallCommand[] = L"ReinstallCommand"; 82 const wchar_t kReinstallCommand[] = L"ReinstallCommand";
83 const wchar_t kSystemSettingsDefaultAppsFormat[] =
84 L"SystemSettings_DefaultApps_%ls";
83 85
84 // Returns the current (or installed) browser's ProgId (e.g. 86 // Returns the current (or installed) browser's ProgId (e.g.
85 // "ChromeHTML|suffix|"). 87 // "ChromeHTML|suffix|").
86 // |suffix| can be the empty string. 88 // |suffix| can be the empty string.
87 base::string16 GetBrowserProgId(const base::string16& suffix) { 89 base::string16 GetBrowserProgId(const base::string16& suffix) {
88 BrowserDistribution* dist = BrowserDistribution::GetDistribution(); 90 BrowserDistribution* dist = BrowserDistribution::GetDistribution();
89 base::string16 chrome_html(dist->GetBrowserProgIdPrefix()); 91 base::string16 chrome_html(dist->GetBrowserProgIdPrefix());
90 chrome_html.append(suffix); 92 chrome_html.append(suffix);
91 93
92 // ProgIds cannot be longer than 39 characters. 94 // ProgIds cannot be longer than 39 characters.
(...skipping 609 matching lines...) Expand 10 before | Expand all | Expand 10 after
702 DWORD ret_val = 0; 704 DWORD ret_val = 0;
703 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val); 705 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val);
704 if (ret_val == 0) 706 if (ret_val == 0)
705 return true; 707 return true;
706 } 708 }
707 return false; 709 return false;
708 } 710 }
709 711
710 // Launches the Windows 'settings' modern app with the 'default apps' view 712 // Launches the Windows 'settings' modern app with the 'default apps' view
711 // focused. This only works for Windows 8 and Windows 10. The appModelId 713 // focused. This only works for Windows 8 and Windows 10. The appModelId
712 // looks arbitrary but it is the same in Win8 and Win10 previews. There 714 // looks arbitrary but it is the same in Win8 and Win10. There is no easy way to
713 // is no easy way to retrieve the appModelId from the registry. 715 // retrieve the appModelId from the registry.
714 bool LaunchDefaultAppsSettingsModernDialog() { 716 bool LaunchDefaultAppsSettingsModernDialog(const wchar_t* target) {
grt (UTC plus 2) 2016/04/15 18:47:26 how about having this take the protocol rather tha
Patrick Monette 2016/04/15 21:35:42 Done.
717 DCHECK(target);
718 static const wchar_t kControlPanelAppModelId[] =
719 L"windows.immersivecontrolpanel_cw5n1h2txyewy"
720 L"!microsoft.windows.immersivecontrolpanel";
721
715 base::win::ScopedComPtr<IApplicationActivationManager> activator; 722 base::win::ScopedComPtr<IApplicationActivationManager> activator;
716 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); 723 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager);
717 if (SUCCEEDED(hr)) { 724 if (SUCCEEDED(hr)) {
718 DWORD pid = 0; 725 DWORD pid = 0;
719 CoAllowSetForegroundWindow(activator.get(), nullptr); 726 CoAllowSetForegroundWindow(activator.get(), nullptr);
720 hr = activator->ActivateApplication( 727 hr = activator->ActivateApplication(kControlPanelAppModelId,
721 L"windows.immersivecontrolpanel_cw5n1h2txyewy" 728 L"page=SettingsPageAppsDefaults",
722 L"!microsoft.windows.immersivecontrolpanel", 729 AO_NONE, &pid);
723 L"page=SettingsPageAppsDefaults", AO_NONE, &pid);
724 if (SUCCEEDED(hr)) { 730 if (SUCCEEDED(hr)) {
725 hr = activator->ActivateApplication( 731 hr = activator->ActivateApplication(
726 L"windows.immersivecontrolpanel_cw5n1h2txyewy" 732 kControlPanelAppModelId,
727 L"!microsoft.windows.immersivecontrolpanel", 733 base::StringPrintf(L"page=SettingsPageAppsDefaults&target=%ls",
728 L"page=SettingsPageAppsDefaults" 734 target)
729 L"&target=SystemSettings_DefaultApps_Browser", AO_NONE, &pid); 735 .c_str(),
736 AO_NONE, &pid);
730 } 737 }
731 if (SUCCEEDED(hr)) 738 if (SUCCEEDED(hr))
732 return true; 739 return true;
733 UMA_HISTOGRAM_SPARSE_SLOWLY("DefaultBrowser.ActivateSettings.ErrorHresult", 740 UMA_HISTOGRAM_SPARSE_SLOWLY("DefaultBrowser.ActivateSettings.ErrorHresult",
734 hr); 741 hr);
735 } 742 }
736 return false; 743 return false;
737 } 744 }
738 745
739 // Launches the Windows 7 and Windows 8 dialog for picking the application to 746 // Launches the Windows 7 and Windows 8 dialog for picking the application to
(...skipping 596 matching lines...) Expand 10 before | Expand all | Expand 10 after
1336 return false; 1343 return false;
1337 } 1344 }
1338 if (base::IsDirectoryEmpty(shortcut_folder) && 1345 if (base::IsDirectoryEmpty(shortcut_folder) &&
1339 !base::DeleteFile(shortcut_folder, true)) { 1346 !base::DeleteFile(shortcut_folder, true)) {
1340 LOG(ERROR) << "Cannot remove folder " << shortcut_folder.value(); 1347 LOG(ERROR) << "Cannot remove folder " << shortcut_folder.value();
1341 return false; 1348 return false;
1342 } 1349 }
1343 return true; 1350 return true;
1344 } 1351 }
1345 1352
1353 // Returns the target used as a activate parameter when opening the settings
1354 // pointing to the page that is the most relevant to a user trying to change the
1355 // default handler for |protocol|.
1356 base::string16 GetTargetForDefaultAppsSettings(const base::string16& protocol) {
1357 if (protocol == L"mailto")
1358 return base::StringPrintf(kSystemSettingsDefaultAppsFormat, L"Email");
1359
1360 return L"SettingsPageAppsDefaultsProtocolView";
1361 }
1362
1363 // Creates the registry key <root hkey>\Software\Classes\<protocol>\URL Protocol
1364 // so that Windows allows the handler for this |protocol| to be be changed.
1365 bool CreateKeyForProtocol(const wchar_t* protocol) {
1366 RegKey reg_key(HKEY_CURRENT_USER, ShellUtil::kRegClasses, KEY_ALL_ACCESS);
1367 DCHECK(reg_key.Valid());
1368 if (reg_key.CreateKey(protocol, KEY_CREATE_SUB_KEY | KEY_SET_VALUE) !=
1369 ERROR_SUCCESS)
1370 return false;
1371
1372 return reg_key.WriteValue(ShellUtil::kRegUrlProtocol, L"") == ERROR_SUCCESS;
1373 }
1374
1346 } // namespace 1375 } // namespace
1347 1376
1348 const wchar_t* ShellUtil::kRegDefaultIcon = L"\\DefaultIcon"; 1377 const wchar_t* ShellUtil::kRegDefaultIcon = L"\\DefaultIcon";
1349 const wchar_t* ShellUtil::kRegShellPath = L"\\shell"; 1378 const wchar_t* ShellUtil::kRegShellPath = L"\\shell";
1350 const wchar_t* ShellUtil::kRegShellOpen = L"\\shell\\open\\command"; 1379 const wchar_t* ShellUtil::kRegShellOpen = L"\\shell\\open\\command";
1351 const wchar_t* ShellUtil::kRegStartMenuInternet = 1380 const wchar_t* ShellUtil::kRegStartMenuInternet =
1352 L"Software\\Clients\\StartMenuInternet"; 1381 L"Software\\Clients\\StartMenuInternet";
1353 const wchar_t* ShellUtil::kRegClasses = L"Software\\Classes"; 1382 const wchar_t* ShellUtil::kRegClasses = L"Software\\Classes";
1354 const wchar_t* ShellUtil::kRegRegisteredApplications = 1383 const wchar_t* ShellUtil::kRegRegisteredApplications =
1355 L"Software\\RegisteredApplications"; 1384 L"Software\\RegisteredApplications";
(...skipping 452 matching lines...) Expand 10 before | Expand all | Expand 10 after
1808 return ProbeProtocolHandlers(chrome_exe, 1837 return ProbeProtocolHandlers(chrome_exe,
1809 protocols, 1838 protocols,
1810 arraysize(protocols)); 1839 arraysize(protocols));
1811 } 1840 }
1812 1841
1813 // static 1842 // static
1814 bool ShellUtil::CanMakeChromeDefaultUnattended() { 1843 bool ShellUtil::CanMakeChromeDefaultUnattended() {
1815 return base::win::GetVersion() < base::win::VERSION_WIN8; 1844 return base::win::GetVersion() < base::win::VERSION_WIN8;
1816 } 1845 }
1817 1846
1847 // static
1848 ShellUtil::SetDefaultInteractiveMode ShellUtil::GetSetDefaultInteractiveMode() {
1849 DCHECK(!CanMakeChromeDefaultUnattended());
1850
1851 if (base::win::GetVersion() == base::win::VERSION_WIN8) {
grt (UTC plus 2) 2016/04/15 18:47:26 nit: omit braces
Patrick Monette 2016/04/15 21:35:42 Done.
1852 return SetDefaultInteractiveMode::INTENT_PICKER;
1853 }
1854
1855 return SetDefaultInteractiveMode::SYSTEM_SETTINGS;
1856 }
1857
1818 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist, 1858 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist,
1819 int shell_change, 1859 int shell_change,
1820 const base::FilePath& chrome_exe, 1860 const base::FilePath& chrome_exe,
1821 bool elevate_if_not_admin) { 1861 bool elevate_if_not_admin) {
1822 DCHECK(!(shell_change & SYSTEM_LEVEL) || IsUserAnAdmin()); 1862 DCHECK(!(shell_change & SYSTEM_LEVEL) || IsUserAnAdmin());
1823 1863
1824 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); 1864 BrowserDistribution* distribution = BrowserDistribution::GetDistribution();
1825 if (distribution->GetDefaultBrowserControlPolicy() != 1865 if (distribution->GetDefaultBrowserControlPolicy() !=
1826 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { 1866 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) {
1827 return false; 1867 return false;
(...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after
1881 1921
1882 // Send Windows notification event so that it can update icons for 1922 // Send Windows notification event so that it can update icons for
1883 // file associations. 1923 // file associations.
1884 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); 1924 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL);
1885 return ret; 1925 return ret;
1886 } 1926 }
1887 1927
1888 bool ShellUtil::ShowMakeChromeDefaultSystemUI( 1928 bool ShellUtil::ShowMakeChromeDefaultSystemUI(
1889 BrowserDistribution* dist, 1929 BrowserDistribution* dist,
1890 const base::FilePath& chrome_exe) { 1930 const base::FilePath& chrome_exe) {
1891 DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN8); 1931 DCHECK(!CanMakeChromeDefaultUnattended());
1892 if (dist->GetDefaultBrowserControlPolicy() != 1932 if (dist->GetDefaultBrowserControlPolicy() !=
1893 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { 1933 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) {
1894 return false; 1934 return false;
1895 } 1935 }
1896 1936
1897 if (!RegisterChromeBrowser(dist, chrome_exe, base::string16(), true)) 1937 if (!RegisterChromeBrowser(dist, chrome_exe, base::string16(), true))
1898 return false; 1938 return false;
1899 1939
1900 bool succeeded = true; 1940 bool succeeded = true;
1901 bool is_default = (GetChromeDefaultState() == IS_DEFAULT); 1941 bool is_default = (GetChromeDefaultState() == IS_DEFAULT);
1902 if (!is_default) { 1942 if (!is_default) {
1903 if (base::win::GetVersion() < base::win::VERSION_WIN10) { 1943 switch (GetSetDefaultInteractiveMode()) {
1904 // On Windows 8, you can't set yourself as the default handler 1944 case INTENT_PICKER: {
1905 // programatically. In other words IApplicationAssociationRegistration 1945 // On Windows 8, you can't set yourself as the default handler
1906 // has been rendered useless. What you can do is to launch 1946 // programatically. In other words IApplicationAssociationRegistration
1907 // "Set Program Associations" section of the "Default Programs" 1947 // has been rendered useless. What you can do is to launch
1908 // control panel, which is a mess, or pop the concise "How you want to 1948 // "Set Program Associations" section of the "Default Programs"
1909 // open webpages?" dialog. We choose the latter. 1949 // control panel, which is a mess, or pop the concise "How you want to
1910 ScopedUserProtocolEntry user_protocol_entry; 1950 // open webpages?" dialog. We choose the latter.
1911 succeeded = LaunchSelectDefaultProtocolHandlerDialog(L"http"); 1951 ScopedUserProtocolEntry user_protocol_entry;
1912 } else { 1952 succeeded = LaunchSelectDefaultProtocolHandlerDialog(L"http");
1913 // On Windows 10, you can't even launch the associations dialog. 1953 } break;
grt (UTC plus 2) 2016/04/15 18:47:26 is this what git cl format likes?
Patrick Monette 2016/04/15 21:35:42 Yes.
1914 // So we launch the settings dialog. Quoting from MSDN: "The Open With 1954 case SYSTEM_SETTINGS:
1915 // dialog box can no longer be used to change the default program used to 1955 // On Windows 10, you can't even launch the associations dialog.
1916 // open a file extension. You can only use SHOpenWithDialog to open 1956 // So we launch the settings dialog. Quoting from MSDN: "The Open With
1917 // a single file." 1957 // dialog box can no longer be used to change the default program used
1918 succeeded = LaunchDefaultAppsSettingsModernDialog(); 1958 // to open a file extension. You can only use SHOpenWithDialog to open
1959 // a single file."
1960 succeeded = LaunchDefaultAppsSettingsModernDialog(
1961 base::StringPrintf(kSystemSettingsDefaultAppsFormat, L"Browser")
grt (UTC plus 2) 2016/04/15 18:47:26 how about using GetTargetForDefaultAppsSettings(L"
Patrick Monette 2016/04/15 21:35:42 Done.
1962 .c_str());
1963 break;
1919 } 1964 }
1920 is_default = (succeeded && GetChromeDefaultState() == IS_DEFAULT); 1965 is_default = (succeeded && GetChromeDefaultState() == IS_DEFAULT);
1921 } 1966 }
1922 if (succeeded && is_default) 1967 if (succeeded && is_default)
1923 RegisterChromeAsDefaultXPStyle(dist, CURRENT_USER, chrome_exe); 1968 RegisterChromeAsDefaultXPStyle(dist, CURRENT_USER, chrome_exe);
1924 return succeeded; 1969 return succeeded;
1925 } 1970 }
1926 1971
1927 bool ShellUtil::MakeChromeDefaultProtocolClient( 1972 bool ShellUtil::MakeChromeDefaultProtocolClient(
1928 BrowserDistribution* dist, 1973 BrowserDistribution* dist,
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after
1970 if (!RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol)) 2015 if (!RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol))
1971 ret = false; 2016 ret = false;
1972 2017
1973 return ret; 2018 return ret;
1974 } 2019 }
1975 2020
1976 bool ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI( 2021 bool ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI(
1977 BrowserDistribution* dist, 2022 BrowserDistribution* dist,
1978 const base::FilePath& chrome_exe, 2023 const base::FilePath& chrome_exe,
1979 const base::string16& protocol) { 2024 const base::string16& protocol) {
1980 DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN8); 2025 DCHECK(!CanMakeChromeDefaultUnattended());
1981 if (dist->GetDefaultBrowserControlPolicy() != 2026 if (dist->GetDefaultBrowserControlPolicy() !=
1982 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { 2027 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) {
1983 return false; 2028 return false;
1984 } 2029 }
1985 2030
1986 if (!RegisterChromeForProtocol( 2031 if (!RegisterChromeForProtocol(
1987 dist, chrome_exe, base::string16(), protocol, true)) 2032 dist, chrome_exe, base::string16(), protocol, true))
1988 return false; 2033 return false;
1989 2034
2035 // This registry key is required so Windows give the user the possibility to
2036 // choose a default handler for |protocol|.
2037 if (!CreateKeyForProtocol(protocol.c_str())) {
grt (UTC plus 2) 2016/04/15 18:47:26 is this doing the same thing that the ScopedUserPr
Patrick Monette 2016/04/15 21:35:42 Good point! I generalized it for all protocols so
2038 DVLOG(1) << "Failed to create a valid key for the protocol " << protocol;
2039 return false;
2040 }
2041
1990 bool succeeded = true; 2042 bool succeeded = true;
1991 bool is_default = ( 2043 bool is_default = (
1992 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); 2044 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT);
1993 if (!is_default) { 2045 if (!is_default) {
1994 // On Windows 8, you can't set yourself as the default handler 2046 switch (GetSetDefaultInteractiveMode()) {
1995 // programatically. In other words IApplicationAssociationRegistration 2047 case INTENT_PICKER: {
1996 // has been rendered useless. What you can do is to launch 2048 // On Windows 8, you can't set yourself as the default handler
1997 // "Set Program Associations" section of the "Default Programs" 2049 // programatically. In other words IApplicationAssociationRegistration
1998 // control panel, which is a mess, or pop the concise "How you want to open 2050 // has been rendered useless. What you can do is to launch
1999 // links of this type (protocol)?" dialog. We choose the latter. 2051 // "Set Program Associations" section of the "Default Programs"
2000 succeeded = LaunchSelectDefaultProtocolHandlerDialog(protocol.c_str()); 2052 // control panel, which is a mess, or pop the concise "How you want to
2053 // open
2054 // links of this type (protocol)?" dialog. We choose the latter.
2055 ScopedUserProtocolEntry user_protocol_entry;
grt (UTC plus 2) 2016/04/15 18:47:26 as-is, this is specifically for http, so it isn't
Patrick Monette 2016/04/15 21:35:41 Done.
2056 succeeded = LaunchSelectDefaultProtocolHandlerDialog(protocol.c_str());
2057 } break;
2058 case SYSTEM_SETTINGS:
2059 // On Windows 10, you can't even launch the associations dialog.
2060 // So we launch the settings dialog.
2061 succeeded = LaunchDefaultAppsSettingsModernDialog(
2062 GetTargetForDefaultAppsSettings(protocol).c_str());
2063 break;
2064 }
2001 is_default = (succeeded && 2065 is_default = (succeeded &&
2002 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); 2066 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT);
2003 } 2067 }
2004 if (succeeded && is_default) 2068 if (succeeded && is_default)
2005 RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol); 2069 RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol);
2006 return succeeded; 2070 return succeeded;
2007 } 2071 }
2008 2072
2009 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist, 2073 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist,
2010 const base::FilePath& chrome_exe, 2074 const base::FilePath& chrome_exe,
(...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after
2340 itr != entries.end(); ++itr) 2404 itr != entries.end(); ++itr)
2341 (*itr)->AddToWorkItemList(root, items.get()); 2405 (*itr)->AddToWorkItemList(root, items.get());
2342 2406
2343 // Apply all the registry changes and if there is a problem, rollback 2407 // Apply all the registry changes and if there is a problem, rollback
2344 if (!items->Do()) { 2408 if (!items->Do()) {
2345 items->Rollback(); 2409 items->Rollback();
2346 return false; 2410 return false;
2347 } 2411 }
2348 return true; 2412 return true;
2349 } 2413 }
OLDNEW
« chrome/installer/util/shell_util.h ('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