• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 // Copyright 2022 The Chromium Authors
2 // Use of this source code is governed by a BSD-style license that can be
3 // found in the LICENSE file.
4 
5 #include "base/win/default_apps_util.h"
6 
7 #include <shobjidl.h>
8 #include <wrl/client.h>
9 
10 #include "base/logging.h"
11 #include "base/strings/strcat.h"
12 #include "base/strings/string_util.h"
13 #include "base/win/windows_version.h"
14 
15 namespace {
16 
17 // Returns the target used as a activate parameter when opening the settings
18 // pointing to the page that is the most relevant to a user trying to change the
19 // default handler for `protocol`.
GetTargetForDefaultAppsSettings(base::WStringPiece protocol)20 std::wstring GetTargetForDefaultAppsSettings(base::WStringPiece protocol) {
21   static constexpr base::WStringPiece kSystemSettingsDefaultAppsPrefix(
22       L"SystemSettings_DefaultApps_");
23   if (base::EqualsCaseInsensitiveASCII(protocol, L"http"))
24     return base::StrCat({kSystemSettingsDefaultAppsPrefix, L"Browser"});
25   if (base::EqualsCaseInsensitiveASCII(protocol, L"mailto"))
26     return base::StrCat({kSystemSettingsDefaultAppsPrefix, L"Email"});
27   return L"SettingsPageAppsDefaultsProtocolView";
28 }
29 
30 }  // namespace
31 
32 namespace base::win {
33 
LaunchDefaultAppsSettingsModernDialog(base::WStringPiece protocol)34 bool LaunchDefaultAppsSettingsModernDialog(base::WStringPiece protocol) {
35   // The appModelId looks arbitrary but it is the same in Win8 and Win10. There
36   // is no easy way to retrieve the appModelId from the registry.
37   static constexpr wchar_t kControlPanelAppModelId[] =
38       L"windows.immersivecontrolpanel_cw5n1h2txyewy"
39       L"!microsoft.windows.immersivecontrolpanel";
40 
41   Microsoft::WRL::ComPtr<IApplicationActivationManager> activator;
42   HRESULT hr = ::CoCreateInstance(CLSID_ApplicationActivationManager, nullptr,
43                                   CLSCTX_ALL, IID_PPV_ARGS(&activator));
44   if (FAILED(hr))
45     return false;
46 
47   DWORD pid = 0;
48   CoAllowSetForegroundWindow(activator.Get(), nullptr);
49   hr = activator->ActivateApplication(
50       kControlPanelAppModelId, L"page=SettingsPageAppsDefaults", AO_NONE, &pid);
51   if (FAILED(hr))
52     return false;
53   if (protocol.empty())
54     return true;
55 
56   hr = activator->ActivateApplication(
57       kControlPanelAppModelId,
58       base::StrCat({L"page=SettingsPageAppsDefaults&target=",
59                     GetTargetForDefaultAppsSettings(protocol)})
60           .c_str(),
61       AO_NONE, &pid);
62   return SUCCEEDED(hr);
63 }
64 
65 }  // namespace base::win
66