File size: 9,464 Bytes
8c763fb | 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 | // Copyright (c) Microsoft Corporation.
// Licensed under the MIT License.
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Globalization;
using System.Management.Automation.Host;
using System.Security;
using Dbg = System.Management.Automation.Diagnostics;
using InternalHostUserInterface = System.Management.Automation.Internal.Host.InternalHostUserInterface;
namespace System.Management.Automation.Remoting
{
/// <summary>
/// The ServerRemoteHostUserInterface class.
/// </summary>
internal class ServerRemoteHostUserInterface : PSHostUserInterface, IHostUISupportsMultipleChoiceSelection
{
/// <summary>
/// Server method executor.
/// </summary>
private readonly ServerMethodExecutor _serverMethodExecutor;
/// <summary>
/// Constructor for ServerRemoteHostUserInterface.
/// </summary>
internal ServerRemoteHostUserInterface(ServerRemoteHost remoteHost)
{
Dbg.Assert(remoteHost != null, "Expected remoteHost != null");
ServerRemoteHost = remoteHost;
Dbg.Assert(!remoteHost.HostInfo.IsHostUINull, "Expected !remoteHost.HostInfo.IsHostUINull");
_serverMethodExecutor = remoteHost.ServerMethodExecutor;
// Use HostInfo to duplicate host-RawUI as null or non-null based on the client's host-RawUI.
RawUI = remoteHost.HostInfo.IsHostRawUINull ? null : new ServerRemoteHostRawUserInterface(this);
}
/// <summary>
/// Raw ui.
/// </summary>
public override PSHostRawUserInterface RawUI { get; }
/// <summary>
/// Server remote host.
/// </summary>
internal ServerRemoteHost ServerRemoteHost { get; }
/// <summary>
/// Read line.
/// </summary>
public override string ReadLine()
{
return _serverMethodExecutor.ExecuteMethod<string>(RemoteHostMethodId.ReadLine);
}
/// <summary>
/// Prompt for choice.
/// </summary>
public override int PromptForChoice(string caption, string message, Collection<ChoiceDescription> choices, int defaultChoice)
{
return _serverMethodExecutor.ExecuteMethod<int>(RemoteHostMethodId.PromptForChoice, new object[] { caption, message, choices, defaultChoice });
}
/// <summary>
/// Prompt for choice. User can select multiple choices.
/// </summary>
/// <param name="caption"></param>
/// <param name="message"></param>
/// <param name="choices"></param>
/// <param name="defaultChoices"></param>
/// <returns></returns>
public Collection<int> PromptForChoice(string caption,
string message,
Collection<ChoiceDescription> choices,
IEnumerable<int> defaultChoices)
{
return _serverMethodExecutor.ExecuteMethod<Collection<int>>(RemoteHostMethodId.PromptForChoiceMultipleSelection,
new object[] { caption, message, choices, defaultChoices });
}
/// <summary>
/// Prompt.
/// </summary>
public override Dictionary<string, PSObject> Prompt(string caption, string message, Collection<FieldDescription> descriptions)
{
// forward the call to the client host
Dictionary<string, PSObject> results = _serverMethodExecutor.ExecuteMethod<Dictionary<string, PSObject>>(
RemoteHostMethodId.Prompt, new object[] { caption, message, descriptions });
// attempt to do the requested type casts on the server (it is okay to fail the cast and return the original object)
foreach (FieldDescription description in descriptions)
{
Type requestedType = InternalHostUserInterface.GetFieldType(description);
if (requestedType != null)
{
PSObject valueFromClient;
if (results.TryGetValue(description.Name, out valueFromClient))
{
object conversionResult;
if (LanguagePrimitives.TryConvertTo(valueFromClient, requestedType, CultureInfo.InvariantCulture, out conversionResult))
{
if (conversionResult != null)
{
results[description.Name] = PSObject.AsPSObject(conversionResult);
}
else
{
results[description.Name] = null;
}
}
}
}
}
return results;
}
/// <summary>
/// Write.
/// </summary>
public override void Write(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.Write1, new object[] { message });
}
/// <summary>
/// Write.
/// </summary>
public override void Write(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.Write2, new object[] { foregroundColor, backgroundColor, message });
}
/// <summary>
/// Write line.
/// </summary>
public override void WriteLine()
{
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine1);
}
/// <summary>
/// Write line.
/// </summary>
public override void WriteLine(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine2, new object[] { message });
}
/// <summary>
/// Write line.
/// </summary>
public override void WriteLine(ConsoleColor foregroundColor, ConsoleColor backgroundColor, string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteLine3, new object[] { foregroundColor, backgroundColor, message });
}
/// <summary>
/// Write error line.
/// </summary>
public override void WriteErrorLine(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteErrorLine, new object[] { message });
}
/// <summary>
/// Write debug line.
/// </summary>
public override void WriteDebugLine(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteDebugLine, new object[] { message });
}
/// <summary>
/// Write progress.
/// </summary>
public override void WriteProgress(long sourceId, ProgressRecord record)
{
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteProgress, new object[] { sourceId, record });
}
/// <summary>
/// Write verbose line.
/// </summary>
public override void WriteVerboseLine(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteVerboseLine, new object[] { message });
}
/// <summary>
/// Write warning line.
/// </summary>
public override void WriteWarningLine(string message)
{
message = GetOutputString(message, supportsVirtualTerminal: true);
_serverMethodExecutor.ExecuteVoidMethod(RemoteHostMethodId.WriteWarningLine, new object[] { message });
}
/// <summary>
/// Read line as secure string.
/// </summary>
public override SecureString ReadLineAsSecureString()
{
return _serverMethodExecutor.ExecuteMethod<SecureString>(RemoteHostMethodId.ReadLineAsSecureString);
}
/// <summary>
/// Prompt for credential.
/// </summary>
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName)
{
return _serverMethodExecutor.ExecuteMethod<PSCredential>(RemoteHostMethodId.PromptForCredential1,
new object[] { caption, message, userName, targetName });
}
/// <summary>
/// Prompt for credential.
/// </summary>
public override PSCredential PromptForCredential(string caption, string message, string userName, string targetName, PSCredentialTypes allowedCredentialTypes, PSCredentialUIOptions options)
{
return _serverMethodExecutor.ExecuteMethod<PSCredential>(RemoteHostMethodId.PromptForCredential2,
new object[] { caption, message, userName, targetName, allowedCredentialTypes, options });
}
}
}
|