Skip to content

Commit

Permalink
Use feature markers during terminal-by-default handoff
Browse files Browse the repository at this point in the history
  • Loading branch information
lhecker committed May 24, 2022
1 parent c5fad74 commit 59d45c9
Show file tree
Hide file tree
Showing 9 changed files with 160 additions and 123 deletions.
1 change: 1 addition & 0 deletions .github/actions/spelling/expect/expect.txt
Original file line number Diff line number Diff line change
Expand Up @@ -1075,6 +1075,7 @@ ICore
IData
IDCANCEL
IDD
IDefault
IDesktop
IDevice
IDictionary
Expand Down
3 changes: 3 additions & 0 deletions src/cascadia/CascadiaPackage/Package.appxmanifest
Original file line number Diff line number Diff line change
Expand Up @@ -170,6 +170,7 @@
PublicFolder="Public">
<uap3:Properties>
<Clsid>{2EACA947-7F5F-4CFA-BA87-8F7FBEEFBE69}</Clsid>
<AllowDefaultTerminalHandoff />
</uap3:Properties>
</uap3:AppExtension>
</uap3:Extension>
Expand All @@ -181,6 +182,7 @@
PublicFolder="Public">
<uap3:Properties>
<Clsid>{E12CFF52-A866-4C77-9A90-F570A7AA2C6B}</Clsid>
<AllowDefaultTerminalHandoff />
</uap3:Properties>
</uap3:AppExtension>
</uap3:Extension>
Expand All @@ -189,6 +191,7 @@
<com:ProxyStub Id="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F" DisplayName="OpenConsoleHandoffProxy" Path="OpenConsoleProxy.dll"/>
<com:Interface Id="E686C757-9A35-4A1C-B3CE-0BCC8B5C69F4" ProxyStubClsid="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F"/>
<com:Interface Id="59D55CCE-FC8A-48B4-ACE8-0A9286C6557F" ProxyStubClsid="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F"/>
<com:Interface Id="746E6BC0-AB05-4E38-AB14-71E86763141F" ProxyStubClsid="3171DE52-6EFA-4AEF-8A9F-D02BD67E7A4F"/>
</com:ComInterface>
</com:Extension>
<com:Extension Category="windows.comServer">
Expand Down
2 changes: 1 addition & 1 deletion src/host/exe/CConsoleHandoff.h
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,7 @@ Author(s):
using namespace Microsoft::WRL;

struct __declspec(uuid(__CLSID_CConsoleHandoff))
CConsoleHandoff : public RuntimeClass<RuntimeClassFlags<ClassicCom>, IConsoleHandoff>
CConsoleHandoff : public RuntimeClass<RuntimeClassFlags<ClassicCom>, IConsoleHandoff, IDefaultTerminalMarker>
{
#pragma region IConsoleHandoff
STDMETHODIMP EstablishHandoff(HANDLE server,
Expand Down
1 change: 1 addition & 0 deletions src/host/globals.h
Original file line number Diff line number Diff line change
Expand Up @@ -75,6 +75,7 @@ class Globals
std::optional<CLSID> handoffTerminalClsid;
wil::unique_hfile handoffInboxConsoleHandle;
wil::unique_threadpool_wait handoffInboxConsoleExitWait;
bool defaultTerminalMarkerCheckRequired = false;

#ifdef UNIT_TESTING
void EnableConptyModeForTests(std::unique_ptr<Microsoft::Console::Render::VtEngine> vtRenderEngine);
Expand Down
6 changes: 6 additions & 0 deletions src/host/proxy/IConsoleHandoff.idl
Original file line number Diff line number Diff line change
Expand Up @@ -31,3 +31,9 @@ typedef const CONSOLE_PORTABLE_ATTACH_MSG* PCCONSOLE_PORTABLE_ATTACH_MSG;
[out, system_handle(sh_process)] HANDLE* process);
};

[
object,
uuid(746E6BC0-AB05-4E38-AB14-71E86763141F)
] interface IDefaultTerminalMarker : IUnknown
{
};
12 changes: 5 additions & 7 deletions src/host/srvinit.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -42,6 +42,8 @@ using namespace Microsoft::Console::Render;
const UINT CONSOLE_EVENT_FAILURE_ID = 21790;
const UINT CONSOLE_LPC_PORT_FAILURE_ID = 21791;

DEFINE_GUID(CLSID_SystemDelegationTerminal, 0xe12cff52, 0xa866, 0x4c77, 0x9a, 0x90, 0xf5, 0x70, 0xa7, 0xaa, 0x2c, 0x6b);

[[nodiscard]] HRESULT ConsoleServerInitialization(_In_ HANDLE Server, const ConsoleArguments* const args)
try
{
Expand All @@ -68,7 +70,7 @@ try
if (SUCCEEDED(Microsoft::Console::Internal::DefaultApp::CheckDefaultAppPolicy(isEnabled)) && isEnabled)
{
IID delegationClsid;
if (SUCCEEDED(DelegationConfig::s_GetDefaultConsoleId(delegationClsid)))
if (SUCCEEDED(DelegationConfig::s_GetDefaultConsoleId(delegationClsid, Globals.defaultTerminalMarkerCheckRequired)))
{
Globals.handoffConsoleClsid = delegationClsid;
TraceLoggingWrite(g_hConhostV2EventTraceProvider,
Expand Down Expand Up @@ -428,7 +430,7 @@ try
g.handoffTarget = true;

IID delegationClsid;
if (SUCCEEDED(DelegationConfig::s_GetDefaultConsoleId(delegationClsid)))
if (SUCCEEDED(DelegationConfig::s_GetDefaultConsoleId(delegationClsid, g.defaultTerminalMarkerCheckRequired)))
{
g.handoffConsoleClsid = delegationClsid;
}
Expand All @@ -439,11 +441,7 @@ try

if (!g.handoffTerminalClsid)
{
TraceLoggingWrite(g_hConhostV2EventTraceProvider,
"SrvInit_ReceiveHandoff_NoTerminal",
TraceLoggingLevel(WINEVENT_LEVEL_VERBOSE),
TraceLoggingKeyword(TIL_KEYWORD_TRACE));
return E_NOT_SET;
g.handoffTerminalClsid = CLSID_SystemDelegationTerminal;
}

TraceLoggingWrite(g_hConhostV2EventTraceProvider,
Expand Down
32 changes: 23 additions & 9 deletions src/propslib/DelegationConfig.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -29,6 +29,8 @@ using namespace ABI::Windows::ApplicationModel::AppExtensions;
#define DELEGATION_CONSOLE_KEY_NAME L"DelegationConsole"
#define DELEGATION_TERMINAL_KEY_NAME L"DelegationTerminal"

DEFINE_GUID(IID_IConsoleHandoff, 0xe686c757, 0x9a35, 0x4a1c, 0xb3, 0xce, 0x0b, 0xcc, 0x8b, 0x5c, 0x69, 0xf4);
DEFINE_GUID(IID_IDefaultTerminalMarker, 0x746e6bc0, 0xab05, 0x4e38, 0xab, 0x14, 0x71, 0xe8, 0x67, 0x63, 0x14, 0x1f);
DEFINE_GUID(CLSID_SystemDelegationConsole, 0x2eaca947, 0x7f5f, 0x4cfa, 0xba, 0x87, 0x8f, 0x7f, 0xbe, 0xef, 0xbe, 0x69);
DEFINE_GUID(CLSID_SystemDelegationTerminal, 0xe12cff52, 0xa866, 0x4c77, 0x9a, 0x90, 0xf5, 0x70, 0xa7, 0xaa, 0x2c, 0x6b);

Expand Down Expand Up @@ -127,6 +129,12 @@ HRESULT _lookupCatalog(PCWSTR extensionName, std::vector<T>& vec) noexcept
ComPtr<IMap<HSTRING, IInspectable*>> map;
RETURN_IF_FAILED(properties.As(&map));

{
boolean allowDefaultTerminalHandoff;
RETURN_IF_FAILED(map->HasKey(HStringReference(L"AllowDefaultTerminalHandoff").Get(), &allowDefaultTerminalHandoff));
extensionMetadata.allowDefaultTerminalHandoff = allowDefaultTerminalHandoff;
}

// Looking it up is going to get us an inspectable
ComPtr<IInspectable> inspectable;
RETURN_IF_FAILED(map->Lookup(HStringReference(L"Clsid").Get(), &inspectable));
Expand Down Expand Up @@ -225,7 +233,8 @@ try
// We also find the default here while we have the list of available ones so
// we can return the opaque structure instead of the raw IID.
IID defCon;
LOG_IF_FAILED(s_GetDefaultConsoleId(defCon));
bool isTerminalAsDefault;
LOG_IF_FAILED(s_GetDefaultConsoleId(defCon, isTerminalAsDefault));
IID defTerm;
LOG_IF_FAILED(s_GetDefaultTerminalId(defTerm));

Expand All @@ -238,7 +247,10 @@ try
{
if (pkg.console.clsid == defCon && pkg.terminal.clsid == defTerm)
{
chosenPackage = pkg;
if (!isTerminalAsDefault || pkg.console.allowDefaultTerminalHandoff)
{
chosenPackage = pkg;
}
break;
}
}
Expand Down Expand Up @@ -266,20 +278,22 @@ CATCH_RETURN()
return S_OK;
}

[[nodiscard]] HRESULT DelegationConfig::s_GetDefaultConsoleId(IID& iid) noexcept
[[nodiscard]] HRESULT DelegationConfig::s_GetDefaultConsoleId(IID& iid, bool& isTerminalAsDefault) noexcept
{
iid = { 0 };
isTerminalAsDefault = false;

auto hr = s_Get(DELEGATION_CONSOLE_KEY_NAME, iid);

auto defApp = false;
if (SUCCEEDED(Microsoft::Console::Internal::DefaultApp::CheckShouldTerminalBeDefault(defApp)) && defApp)
if (FAILED(hr))
{
if (FAILED(hr))
auto defApp = false;
if (SUCCEEDED(Microsoft::Console::Internal::DefaultApp::CheckShouldTerminalBeDefault(defApp)) && defApp)
{
// If we can't find a user-defined delegation console/terminal, use the hardcoded
// delegation console/terminal instead.
iid = CLSID_SystemDelegationConsole;
isTerminalAsDefault = true;
hr = S_OK;
}
}
Expand All @@ -293,10 +307,10 @@ CATCH_RETURN()

auto hr = s_Get(DELEGATION_TERMINAL_KEY_NAME, iid);

auto defApp = false;
if (SUCCEEDED(Microsoft::Console::Internal::DefaultApp::CheckShouldTerminalBeDefault(defApp)) && defApp)
if (FAILED(hr))
{
if (FAILED(hr))
auto defApp = false;
if (SUCCEEDED(Microsoft::Console::Internal::DefaultApp::CheckShouldTerminalBeDefault(defApp)) && defApp)
{
// If we can't find a user-defined delegation console/terminal, use the hardcoded
// delegation console/terminal instead.
Expand Down
19 changes: 8 additions & 11 deletions src/propslib/DelegationConfig.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -20,10 +20,10 @@ class DelegationConfig
public:
struct PkgVersion
{
unsigned short major;
unsigned short minor;
unsigned short build;
unsigned short revision;
unsigned short major = 0;
unsigned short minor = 0;
unsigned short build = 0;
unsigned short revision = 0;

bool operator==(const PkgVersion& other) const
{
Expand All @@ -36,12 +36,13 @@ class DelegationConfig

struct DelegationBase
{
CLSID clsid;
CLSID clsid{};
std::wstring name;
std::wstring author;
std::wstring pfn;
std::wstring logo;
PkgVersion version;
bool allowDefaultTerminalHandoff = false;

bool IsFromSamePackage(const DelegationBase& other) const
{
Expand All @@ -53,11 +54,7 @@ class DelegationConfig

bool operator==(const DelegationBase& other) const
{
return clsid == other.clsid &&
name == other.name &&
author == other.author &&
pfn == other.pfn &&
version == other.version;
return clsid == other.clsid;
}
};

Expand Down Expand Up @@ -85,7 +82,7 @@ class DelegationConfig

[[nodiscard]] static HRESULT s_SetDefaultByPackage(const DelegationPackage& pkg, const bool useRegExe = false) noexcept;

[[nodiscard]] static HRESULT s_GetDefaultConsoleId(IID& iid) noexcept;
[[nodiscard]] static HRESULT s_GetDefaultConsoleId(IID& iid, bool& isTerminalAsDefault) noexcept;
[[nodiscard]] static HRESULT s_GetDefaultTerminalId(IID& iid) noexcept;

private:
Expand Down
Loading

0 comments on commit 59d45c9

Please sign in to comment.