Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 // | 4 // |
| 5 // 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 13 matching lines...) Expand all Loading... | |
| 24 #include "base/files/file_util.h" | 24 #include "base/files/file_util.h" |
| 25 #include "base/lazy_instance.h" | 25 #include "base/lazy_instance.h" |
| 26 #include "base/logging.h" | 26 #include "base/logging.h" |
| 27 #include "base/macros.h" | 27 #include "base/macros.h" |
| 28 #include "base/md5.h" | 28 #include "base/md5.h" |
| 29 #include "base/memory/scoped_vector.h" | 29 #include "base/memory/scoped_vector.h" |
| 30 #include "base/metrics/sparse_histogram.h" | 30 #include "base/metrics/sparse_histogram.h" |
| 31 #include "base/path_service.h" | 31 #include "base/path_service.h" |
| 32 #include "base/strings/string16.h" | 32 #include "base/strings/string16.h" |
| 33 #include "base/strings/string_number_conversions.h" | 33 #include "base/strings/string_number_conversions.h" |
| 34 #include "base/strings/string_piece.h" | |
| 34 #include "base/strings/string_split.h" | 35 #include "base/strings/string_split.h" |
| 35 #include "base/strings/string_util.h" | 36 #include "base/strings/string_util.h" |
| 36 #include "base/strings/stringprintf.h" | 37 #include "base/strings/stringprintf.h" |
| 37 #include "base/strings/utf_string_conversions.h" | 38 #include "base/strings/utf_string_conversions.h" |
| 38 #include "base/synchronization/cancellation_flag.h" | 39 #include "base/synchronization/cancellation_flag.h" |
| 39 #include "base/values.h" | 40 #include "base/values.h" |
| 40 #include "base/win/registry.h" | 41 #include "base/win/registry.h" |
| 41 #include "base/win/scoped_co_mem.h" | 42 #include "base/win/scoped_co_mem.h" |
| 42 #include "base/win/scoped_comptr.h" | 43 #include "base/win/scoped_comptr.h" |
| 43 #include "base/win/shortcut.h" | 44 #include "base/win/shortcut.h" |
| (...skipping 656 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 700 } | 701 } |
| 701 | 702 |
| 702 DWORD ret_val = 0; | 703 DWORD ret_val = 0; |
| 703 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val); | 704 InstallUtil::ExecuteExeAsAdmin(cmd, &ret_val); |
| 704 if (ret_val == 0) | 705 if (ret_val == 0) |
| 705 return true; | 706 return true; |
| 706 } | 707 } |
| 707 return false; | 708 return false; |
| 708 } | 709 } |
| 709 | 710 |
| 711 // Returns the target used as a activate parameter when opening the settings | |
| 712 // pointing to the page that is the most relevant to a user trying to change the | |
| 713 // default handler for |protocol|. | |
| 714 base::string16 GetTargetForDefaultAppsSettings(const wchar_t* protocol) { | |
| 715 static const wchar_t kSystemSettingsDefaultAppsFormat[] = | |
| 716 L"SystemSettings_DefaultApps_%ls"; | |
| 717 | |
| 718 if (base::EqualsCaseInsensitiveASCII(protocol, L"http")) { | |
|
grt (UTC plus 2)
2016/04/18 19:00:56
nit: omit braces for single-line conditionals
Patrick Monette
2016/04/19 14:36:13
Done.
| |
| 719 return base::StringPrintf(kSystemSettingsDefaultAppsFormat, L"Browser"); | |
| 720 } else if (base::EqualsCaseInsensitiveASCII(protocol, L"mailto")) { | |
|
grt (UTC plus 2)
2016/04/18 19:00:56
nit: omit "else" when the "if" block "return"s:
Patrick Monette
2016/04/19 14:36:13
Nice choice of variable name there. Done.
| |
| 721 return base::StringPrintf(kSystemSettingsDefaultAppsFormat, L"Email"); | |
| 722 } | |
| 723 return L"SettingsPageAppsDefaultsProtocolView"; | |
| 724 } | |
| 725 | |
| 710 // Launches the Windows 'settings' modern app with the 'default apps' view | 726 // Launches the Windows 'settings' modern app with the 'default apps' view |
| 711 // focused. This only works for Windows 8 and Windows 10. The appModelId | 727 // 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 | 728 // 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. | 729 // retrieve the appModelId from the registry. |
| 714 bool LaunchDefaultAppsSettingsModernDialog() { | 730 bool LaunchDefaultAppsSettingsModernDialog(const wchar_t* protocol) { |
| 731 DCHECK(protocol); | |
| 732 static const wchar_t kControlPanelAppModelId[] = | |
| 733 L"windows.immersivecontrolpanel_cw5n1h2txyewy" | |
| 734 L"!microsoft.windows.immersivecontrolpanel"; | |
| 735 | |
| 715 base::win::ScopedComPtr<IApplicationActivationManager> activator; | 736 base::win::ScopedComPtr<IApplicationActivationManager> activator; |
| 716 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); | 737 HRESULT hr = activator.CreateInstance(CLSID_ApplicationActivationManager); |
| 717 if (SUCCEEDED(hr)) { | 738 if (SUCCEEDED(hr)) { |
| 718 DWORD pid = 0; | 739 DWORD pid = 0; |
| 719 CoAllowSetForegroundWindow(activator.get(), nullptr); | 740 CoAllowSetForegroundWindow(activator.get(), nullptr); |
| 720 hr = activator->ActivateApplication( | 741 hr = activator->ActivateApplication(kControlPanelAppModelId, |
| 721 L"windows.immersivecontrolpanel_cw5n1h2txyewy" | 742 L"page=SettingsPageAppsDefaults", |
| 722 L"!microsoft.windows.immersivecontrolpanel", | 743 AO_NONE, &pid); |
| 723 L"page=SettingsPageAppsDefaults", AO_NONE, &pid); | |
| 724 if (SUCCEEDED(hr)) { | 744 if (SUCCEEDED(hr)) { |
| 725 hr = activator->ActivateApplication( | 745 hr = activator->ActivateApplication( |
| 726 L"windows.immersivecontrolpanel_cw5n1h2txyewy" | 746 kControlPanelAppModelId, |
| 727 L"!microsoft.windows.immersivecontrolpanel", | 747 base::StringPrintf(L"page=SettingsPageAppsDefaults&target=%ls", |
| 728 L"page=SettingsPageAppsDefaults" | 748 GetTargetForDefaultAppsSettings(protocol)) |
| 729 L"&target=SystemSettings_DefaultApps_Browser", AO_NONE, &pid); | 749 .c_str(), |
| 750 AO_NONE, &pid); | |
| 730 } | 751 } |
| 731 if (SUCCEEDED(hr)) | 752 if (SUCCEEDED(hr)) |
| 732 return true; | 753 return true; |
| 733 UMA_HISTOGRAM_SPARSE_SLOWLY("DefaultBrowser.ActivateSettings.ErrorHresult", | 754 UMA_HISTOGRAM_SPARSE_SLOWLY("DefaultBrowser.ActivateSettings.ErrorHresult", |
| 734 hr); | 755 hr); |
| 735 } | 756 } |
| 736 return false; | 757 return false; |
| 737 } | 758 } |
| 738 | 759 |
| 739 // Launches the Windows 7 and Windows 8 dialog for picking the application to | 760 // Launches the Windows 7 and Windows 8 dialog for picking the application to |
| (...skipping 1068 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1808 return ProbeProtocolHandlers(chrome_exe, | 1829 return ProbeProtocolHandlers(chrome_exe, |
| 1809 protocols, | 1830 protocols, |
| 1810 arraysize(protocols)); | 1831 arraysize(protocols)); |
| 1811 } | 1832 } |
| 1812 | 1833 |
| 1813 // static | 1834 // static |
| 1814 bool ShellUtil::CanMakeChromeDefaultUnattended() { | 1835 bool ShellUtil::CanMakeChromeDefaultUnattended() { |
| 1815 return base::win::GetVersion() < base::win::VERSION_WIN8; | 1836 return base::win::GetVersion() < base::win::VERSION_WIN8; |
| 1816 } | 1837 } |
| 1817 | 1838 |
| 1839 // static | |
| 1840 ShellUtil::InteractiveSetDefaultMode ShellUtil::GetInteractiveSetDefaultMode() { | |
| 1841 DCHECK(!CanMakeChromeDefaultUnattended()); | |
| 1842 | |
| 1843 if (base::win::GetVersion() == base::win::VERSION_WIN8) | |
| 1844 return InteractiveSetDefaultMode::INTENT_PICKER; | |
| 1845 | |
| 1846 return InteractiveSetDefaultMode::SYSTEM_SETTINGS; | |
| 1847 } | |
| 1848 | |
| 1818 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist, | 1849 bool ShellUtil::MakeChromeDefault(BrowserDistribution* dist, |
| 1819 int shell_change, | 1850 int shell_change, |
| 1820 const base::FilePath& chrome_exe, | 1851 const base::FilePath& chrome_exe, |
| 1821 bool elevate_if_not_admin) { | 1852 bool elevate_if_not_admin) { |
| 1822 DCHECK(!(shell_change & SYSTEM_LEVEL) || IsUserAnAdmin()); | 1853 DCHECK(!(shell_change & SYSTEM_LEVEL) || IsUserAnAdmin()); |
| 1823 | 1854 |
| 1824 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); | 1855 BrowserDistribution* distribution = BrowserDistribution::GetDistribution(); |
| 1825 if (distribution->GetDefaultBrowserControlPolicy() != | 1856 if (distribution->GetDefaultBrowserControlPolicy() != |
| 1826 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { | 1857 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { |
| 1827 return false; | 1858 return false; |
| (...skipping 53 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1881 | 1912 |
| 1882 // Send Windows notification event so that it can update icons for | 1913 // Send Windows notification event so that it can update icons for |
| 1883 // file associations. | 1914 // file associations. |
| 1884 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); | 1915 SHChangeNotify(SHCNE_ASSOCCHANGED, SHCNF_IDLIST, NULL, NULL); |
| 1885 return ret; | 1916 return ret; |
| 1886 } | 1917 } |
| 1887 | 1918 |
| 1888 bool ShellUtil::ShowMakeChromeDefaultSystemUI( | 1919 bool ShellUtil::ShowMakeChromeDefaultSystemUI( |
| 1889 BrowserDistribution* dist, | 1920 BrowserDistribution* dist, |
| 1890 const base::FilePath& chrome_exe) { | 1921 const base::FilePath& chrome_exe) { |
| 1891 DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN8); | 1922 DCHECK(!CanMakeChromeDefaultUnattended()); |
| 1892 if (dist->GetDefaultBrowserControlPolicy() != | 1923 if (dist->GetDefaultBrowserControlPolicy() != |
| 1893 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { | 1924 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { |
| 1894 return false; | 1925 return false; |
| 1895 } | 1926 } |
| 1896 | 1927 |
| 1897 if (!RegisterChromeBrowser(dist, chrome_exe, base::string16(), true)) | 1928 if (!RegisterChromeBrowser(dist, chrome_exe, base::string16(), true)) |
| 1898 return false; | 1929 return false; |
| 1899 | 1930 |
| 1900 bool succeeded = true; | 1931 bool succeeded = true; |
| 1901 bool is_default = (GetChromeDefaultState() == IS_DEFAULT); | 1932 bool is_default = (GetChromeDefaultState() == IS_DEFAULT); |
| 1902 if (!is_default) { | 1933 if (!is_default) { |
| 1903 if (base::win::GetVersion() < base::win::VERSION_WIN10) { | 1934 switch (GetInteractiveSetDefaultMode()) { |
| 1904 // On Windows 8, you can't set yourself as the default handler | 1935 case INTENT_PICKER: { |
| 1905 // programatically. In other words IApplicationAssociationRegistration | 1936 // On Windows 8, you can't set yourself as the default handler |
| 1906 // has been rendered useless. What you can do is to launch | 1937 // programatically. In other words IApplicationAssociationRegistration |
| 1907 // "Set Program Associations" section of the "Default Programs" | 1938 // 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 | 1939 // "Set Program Associations" section of the "Default Programs" |
| 1909 // open webpages?" dialog. We choose the latter. | 1940 // control panel, which is a mess, or pop the concise "How you want to |
| 1910 ScopedUserProtocolEntry user_protocol_entry; | 1941 // open webpages?" dialog. We choose the latter. |
| 1911 succeeded = LaunchSelectDefaultProtocolHandlerDialog(L"http"); | 1942 ScopedUserProtocolEntry user_protocol_entry(L"http"); |
| 1912 } else { | 1943 succeeded = LaunchSelectDefaultProtocolHandlerDialog(L"http"); |
| 1913 // On Windows 10, you can't even launch the associations dialog. | 1944 } break; |
| 1914 // So we launch the settings dialog. Quoting from MSDN: "The Open With | 1945 case SYSTEM_SETTINGS: |
| 1915 // dialog box can no longer be used to change the default program used to | 1946 // On Windows 10, you can't even launch the associations dialog. |
| 1916 // open a file extension. You can only use SHOpenWithDialog to open | 1947 // So we launch the settings dialog. Quoting from MSDN: "The Open With |
| 1917 // a single file." | 1948 // dialog box can no longer be used to change the default program used |
| 1918 succeeded = LaunchDefaultAppsSettingsModernDialog(); | 1949 // to open a file extension. You can only use SHOpenWithDialog to open |
| 1950 // a single file." | |
| 1951 succeeded = LaunchDefaultAppsSettingsModernDialog(L"http"); | |
| 1952 break; | |
| 1919 } | 1953 } |
| 1920 is_default = (succeeded && GetChromeDefaultState() == IS_DEFAULT); | 1954 is_default = (succeeded && GetChromeDefaultState() == IS_DEFAULT); |
| 1921 } | 1955 } |
| 1922 if (succeeded && is_default) | 1956 if (succeeded && is_default) |
| 1923 RegisterChromeAsDefaultXPStyle(dist, CURRENT_USER, chrome_exe); | 1957 RegisterChromeAsDefaultXPStyle(dist, CURRENT_USER, chrome_exe); |
| 1924 return succeeded; | 1958 return succeeded; |
| 1925 } | 1959 } |
| 1926 | 1960 |
| 1927 bool ShellUtil::MakeChromeDefaultProtocolClient( | 1961 bool ShellUtil::MakeChromeDefaultProtocolClient( |
| 1928 BrowserDistribution* dist, | 1962 BrowserDistribution* dist, |
| (...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 1970 if (!RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol)) | 2004 if (!RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol)) |
| 1971 ret = false; | 2005 ret = false; |
| 1972 | 2006 |
| 1973 return ret; | 2007 return ret; |
| 1974 } | 2008 } |
| 1975 | 2009 |
| 1976 bool ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI( | 2010 bool ShellUtil::ShowMakeChromeDefaultProtocolClientSystemUI( |
| 1977 BrowserDistribution* dist, | 2011 BrowserDistribution* dist, |
| 1978 const base::FilePath& chrome_exe, | 2012 const base::FilePath& chrome_exe, |
| 1979 const base::string16& protocol) { | 2013 const base::string16& protocol) { |
| 1980 DCHECK_GE(base::win::GetVersion(), base::win::VERSION_WIN8); | 2014 DCHECK(!CanMakeChromeDefaultUnattended()); |
| 1981 if (dist->GetDefaultBrowserControlPolicy() != | 2015 if (dist->GetDefaultBrowserControlPolicy() != |
| 1982 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { | 2016 BrowserDistribution::DEFAULT_BROWSER_FULL_CONTROL) { |
| 1983 return false; | 2017 return false; |
| 1984 } | 2018 } |
| 1985 | 2019 |
| 1986 if (!RegisterChromeForProtocol( | 2020 if (!RegisterChromeForProtocol( |
| 1987 dist, chrome_exe, base::string16(), protocol, true)) | 2021 dist, chrome_exe, base::string16(), protocol, true)) |
| 1988 return false; | 2022 return false; |
| 1989 | 2023 |
| 1990 bool succeeded = true; | 2024 bool succeeded = true; |
| 1991 bool is_default = ( | 2025 bool is_default = ( |
| 1992 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); | 2026 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); |
| 1993 if (!is_default) { | 2027 if (!is_default) { |
| 1994 // On Windows 8, you can't set yourself as the default handler | 2028 switch (GetInteractiveSetDefaultMode()) { |
| 1995 // programatically. In other words IApplicationAssociationRegistration | 2029 case INTENT_PICKER: { |
| 1996 // has been rendered useless. What you can do is to launch | 2030 // On Windows 8, you can't set yourself as the default handler |
| 1997 // "Set Program Associations" section of the "Default Programs" | 2031 // programatically. In other words IApplicationAssociationRegistration |
| 1998 // control panel, which is a mess, or pop the concise "How you want to open | 2032 // has been rendered useless. What you can do is to launch |
| 1999 // links of this type (protocol)?" dialog. We choose the latter. | 2033 // "Set Program Associations" section of the "Default Programs" |
| 2000 succeeded = LaunchSelectDefaultProtocolHandlerDialog(protocol.c_str()); | 2034 // control panel, which is a mess, or pop the concise "How you want to |
| 2035 // open | |
| 2036 // links of this type (protocol)?" dialog. We choose the latter. | |
| 2037 ScopedUserProtocolEntry user_protocol_entry(protocol.c_str()); | |
| 2038 succeeded = LaunchSelectDefaultProtocolHandlerDialog(protocol.c_str()); | |
| 2039 } break; | |
| 2040 case SYSTEM_SETTINGS: | |
| 2041 // On Windows 10, you can't even launch the associations dialog. | |
| 2042 // So we launch the settings dialog. | |
| 2043 succeeded = LaunchDefaultAppsSettingsModernDialog(protocol.c_str()); | |
| 2044 break; | |
| 2045 } | |
| 2001 is_default = (succeeded && | 2046 is_default = (succeeded && |
| 2002 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); | 2047 GetChromeDefaultProtocolClientState(protocol) == IS_DEFAULT); |
| 2003 } | 2048 } |
| 2004 if (succeeded && is_default) | 2049 if (succeeded && is_default) |
| 2005 RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol); | 2050 RegisterChromeAsDefaultProtocolClientXPStyle(dist, chrome_exe, protocol); |
| 2006 return succeeded; | 2051 return succeeded; |
| 2007 } | 2052 } |
| 2008 | 2053 |
| 2009 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist, | 2054 bool ShellUtil::RegisterChromeBrowser(BrowserDistribution* dist, |
| 2010 const base::FilePath& chrome_exe, | 2055 const base::FilePath& chrome_exe, |
| (...skipping 329 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 2340 itr != entries.end(); ++itr) | 2385 itr != entries.end(); ++itr) |
| 2341 (*itr)->AddToWorkItemList(root, items.get()); | 2386 (*itr)->AddToWorkItemList(root, items.get()); |
| 2342 | 2387 |
| 2343 // Apply all the registry changes and if there is a problem, rollback | 2388 // Apply all the registry changes and if there is a problem, rollback |
| 2344 if (!items->Do()) { | 2389 if (!items->Do()) { |
| 2345 items->Rollback(); | 2390 items->Rollback(); |
| 2346 return false; | 2391 return false; |
| 2347 } | 2392 } |
| 2348 return true; | 2393 return true; |
| 2349 } | 2394 } |
| OLD | NEW |