| 1 | // Copyright (c) .NET Foundation and contributors. All rights reserved. Licensed under the Microsoft Reciprocal License. See LICENSE.TXT file in the project root for full license information. |
| 2 | |
| 3 | namespace WixToolset.BootstrapperApplicationApi |
| 4 | { |
| 5 | using System; |
| 6 | using System.ComponentModel; |
| 7 | using System.Runtime.InteropServices; |
| 8 | using System.Security; |
| 9 | |
| 10 | /// <summary> |
| 11 | /// Default implementation of <see cref="IEngine"/>. |
| 12 | /// </summary> |
| 13 | public sealed class Engine : IEngine |
| 14 | { |
| 15 | private IBootstrapperEngine engine; |
| 16 | |
| 17 | internal Engine(IBootstrapperEngine engine) |
| 18 | { |
| 19 | this.engine = engine; |
| 20 | } |
| 21 | |
| 22 | /// <inheritdoc/> |
| 23 | public int PackageCount |
| 24 | { |
| 25 | get |
| 26 | { |
| 27 | int count; |
| 28 | this.engine.GetPackageCount(out count); |
| 29 | |
| 30 | return count; |
| 31 | } |
| 32 | } |
| 33 | |
| 34 | /// <inheritdoc/> |
| 35 | public void Apply(IntPtr hwndParent) |
| 36 | { |
| 37 | this.engine.Apply(hwndParent); |
| 38 | } |
| 39 | |
| 40 | /// <inheritdoc/> |
| 41 | public void CloseSplashScreen() |
| 42 | { |
| 43 | this.engine.CloseSplashScreen(); |
| 44 | } |
| 45 | |
| 46 | /// <inheritdoc/> |
| 47 | public int CompareVersions(string version1, string version2) |
| 48 | { |
| 49 | this.engine.CompareVersions(version1, version2, out var result); |
| 50 | return result; |
| 51 | } |
| 52 | |
| 53 | /// <inheritdoc/> |
| 54 | public bool ContainsVariable(string name) |
| 55 | { |
| 56 | return BalUtil.BalVariableExistsFromEngine(this.engine, name); |
| 57 | } |
| 58 | |
| 59 | /// <inheritdoc/> |
| 60 | public void Detect() |
| 61 | { |
| 62 | this.Detect(IntPtr.Zero); |
| 63 | } |
| 64 | |
| 65 | /// <inheritdoc/> |
| 66 | public void Detect(IntPtr hwndParent) |
| 67 | { |
| 68 | this.engine.Detect(hwndParent); |
| 69 | } |
| 70 | |
| 71 | /// <inheritdoc/> |
| 72 | public bool Elevate(IntPtr hwndParent) |
| 73 | { |
| 74 | int ret = this.engine.Elevate(hwndParent); |
| 75 | |
| 76 | if (NativeMethods.S_OK == ret || NativeMethods.E_ALREADYINITIALIZED == ret) |
| 77 | { |
| 78 | return true; |
| 79 | } |
| 80 | else if (NativeMethods.E_CANCELLED == ret) |
| 81 | { |
| 82 | return false; |
| 83 | } |
| 84 | else |
| 85 | { |
| 86 | throw new Win32Exception(ret); |
| 87 | } |
| 88 | } |
| 89 | |
| 90 | /// <inheritdoc/> |
| 91 | public string EscapeString(string input) |
| 92 | { |
| 93 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 94 | try |
| 95 | { |
| 96 | int ret = BalUtil.BalEscapeStringFromEngine(this.engine, input, ref handle); |
| 97 | if (ret != NativeMethods.S_OK) |
| 98 | { |
| 99 | throw new Win32Exception(ret); |
| 100 | } |
| 101 | |
| 102 | return handle.ToUniString(); |
| 103 | } |
| 104 | finally |
| 105 | { |
| 106 | handle.Dispose(); |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | /// <inheritdoc/> |
| 111 | public bool EvaluateCondition(string condition) |
| 112 | { |
| 113 | bool value; |
| 114 | this.engine.EvaluateCondition(condition, out value); |
| 115 | |
| 116 | return value; |
| 117 | } |
| 118 | |
| 119 | /// <inheritdoc/> |
| 120 | public string FormatString(string format) |
| 121 | { |
| 122 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 123 | try |
| 124 | { |
| 125 | int ret = BalUtil.BalFormatStringFromEngine(this.engine, format, ref handle); |
| 126 | if (ret != NativeMethods.S_OK) |
| 127 | { |
| 128 | throw new Win32Exception(ret); |
| 129 | } |
| 130 | |
| 131 | return handle.ToUniString(); |
| 132 | } |
| 133 | finally |
| 134 | { |
| 135 | handle.Dispose(); |
| 136 | } |
| 137 | } |
| 138 | |
| 139 | /// <inheritdoc/> |
| 140 | public long GetVariableNumeric(string name) |
| 141 | { |
| 142 | int ret = this.engine.GetVariableNumeric(name, out long value); |
| 143 | if (NativeMethods.S_OK != ret) |
| 144 | { |
| 145 | throw new Win32Exception(ret); |
| 146 | } |
| 147 | |
| 148 | return value; |
| 149 | } |
| 150 | |
| 151 | /// <inheritdoc/> |
| 152 | public SecureString GetVariableSecureString(string name) |
| 153 | { |
| 154 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 155 | try |
| 156 | { |
| 157 | int ret = BalUtil.BalGetStringVariableFromEngine(this.engine, name, ref handle); |
| 158 | if (ret != NativeMethods.S_OK) |
| 159 | { |
| 160 | throw new Win32Exception(ret); |
| 161 | } |
| 162 | |
| 163 | return handle.ToSecureString(); |
| 164 | } |
| 165 | finally |
| 166 | { |
| 167 | handle.Dispose(); |
| 168 | } |
| 169 | } |
| 170 | |
| 171 | /// <inheritdoc/> |
| 172 | public string GetVariableString(string name) |
| 173 | { |
| 174 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 175 | try |
| 176 | { |
| 177 | int ret = BalUtil.BalGetStringVariableFromEngine(this.engine, name, ref handle); |
| 178 | if (ret != NativeMethods.S_OK) |
| 179 | { |
| 180 | throw new Win32Exception(ret); |
| 181 | } |
| 182 | |
| 183 | return handle.ToUniString(); |
| 184 | } |
| 185 | finally |
| 186 | { |
| 187 | handle.Dispose(); |
| 188 | } |
| 189 | } |
| 190 | |
| 191 | /// <inheritdoc/> |
| 192 | public string GetVariableVersion(string name) |
| 193 | { |
| 194 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 195 | try |
| 196 | { |
| 197 | int ret = BalUtil.BalGetVersionVariableFromEngine(this.engine, name, ref handle); |
| 198 | if (ret != NativeMethods.S_OK) |
| 199 | { |
| 200 | throw new Win32Exception(ret); |
| 201 | } |
| 202 | |
| 203 | return handle.ToUniString(); |
| 204 | } |
| 205 | finally |
| 206 | { |
| 207 | handle.Dispose(); |
| 208 | } |
| 209 | } |
| 210 | |
| 211 | /// <inheritdoc/> |
| 212 | public string GetRelatedBundleVariable(string bundleId, string name) |
| 213 | { |
| 214 | StrUtil.StrHandle handle = new StrUtil.StrHandle(); |
| 215 | try |
| 216 | { |
| 217 | int ret = BalUtil.BalGetRelatedBundleVariableFromEngine(this.engine, bundleId, name, ref handle); |
| 218 | if (ret != NativeMethods.S_OK) |
| 219 | { |
| 220 | throw new Win32Exception(ret); |
| 221 | } |
| 222 | |
| 223 | return handle.ToUniString(); |
| 224 | } |
| 225 | finally |
| 226 | { |
| 227 | handle.Dispose(); |
| 228 | } |
| 229 | } |
| 230 | |
| 231 | /// <inheritdoc/> |
| 232 | public void LaunchApprovedExe(IntPtr hwndParent, string approvedExeForElevationId, string arguments) |
| 233 | { |
| 234 | this.LaunchApprovedExe(hwndParent, approvedExeForElevationId, arguments, 0); |
| 235 | } |
| 236 | |
| 237 | /// <inheritdoc/> |
| 238 | public void LaunchApprovedExe(IntPtr hwndParent, string approvedExeForElevationId, string arguments, int waitForInputIdleTimeout) |
| 239 | { |
| 240 | this.engine.LaunchApprovedExe(hwndParent, approvedExeForElevationId, arguments, waitForInputIdleTimeout); |
| 241 | } |
| 242 | /// <inheritdoc/> |
| 243 | |
| 244 | public void Log(LogLevel level, string message) |
| 245 | { |
| 246 | this.engine.Log(level, message); |
| 247 | } |
| 248 | |
| 249 | /// <inheritdoc/> |
| 250 | public void Plan(LaunchAction action) |
| 251 | { |
| 252 | this.engine.Plan(action); |
| 253 | } |
| 254 | |
| 255 | /// <inheritdoc/> |
| 256 | public void SetUpdate(string localSource, string downloadSource, long size, UpdateHashType hashType, string hash, string updatePackageId) |
| 257 | { |
| 258 | this.engine.SetUpdate(localSource, downloadSource, size, hashType, hash, updatePackageId); |
| 259 | } |
| 260 | |
| 261 | /// <inheritdoc/> |
| 262 | public void SetUpdateSource(string url) |
| 263 | { |
| 264 | this.engine.SetUpdateSource(url); |
| 265 | } |
| 266 | |
| 267 | /// <inheritdoc/> |
| 268 | public void SetLocalSource(string packageOrContainerId, string payloadId, string path) |
| 269 | { |
| 270 | this.engine.SetLocalSource(packageOrContainerId, payloadId, path); |
| 271 | } |
| 272 | |
| 273 | /// <inheritdoc/> |
| 274 | public void SetDownloadSource(string packageOrContainerId, string payloadId, string url, string user, string password) |
| 275 | { |
| 276 | this.engine.SetDownloadSource(packageOrContainerId, payloadId, url, user, password); |
| 277 | } |
| 278 | |
| 279 | /// <inheritdoc/> |
| 280 | public void SetVariableNumeric(string name, long value) |
| 281 | { |
| 282 | this.engine.SetVariableNumeric(name, value); |
| 283 | } |
| 284 | |
| 285 | /// <inheritdoc/> |
| 286 | public void SetVariableString(string name, SecureString value, bool formatted) |
| 287 | { |
| 288 | IntPtr pValue = Marshal.SecureStringToCoTaskMemUnicode(value); |
| 289 | try |
| 290 | { |
| 291 | this.engine.SetVariableString(name, pValue, formatted); |
| 292 | } |
| 293 | finally |
| 294 | { |
| 295 | Marshal.FreeCoTaskMem(pValue); |
| 296 | } |
| 297 | } |
| 298 | |
| 299 | /// <inheritdoc/> |
| 300 | public void SetVariableString(string name, string value, bool formatted) |
| 301 | { |
| 302 | IntPtr pValue = Marshal.StringToCoTaskMemUni(value); |
| 303 | try |
| 304 | { |
| 305 | this.engine.SetVariableString(name, pValue, formatted); |
| 306 | } |
| 307 | finally |
| 308 | { |
| 309 | Marshal.FreeCoTaskMem(pValue); |
| 310 | } |
| 311 | } |
| 312 | |
| 313 | /// <inheritdoc/> |
| 314 | public void SetVariableVersion(string name, string value) |
| 315 | { |
| 316 | IntPtr pValue = Marshal.StringToCoTaskMemUni(value); |
| 317 | try |
| 318 | { |
| 319 | this.engine.SetVariableVersion(name, pValue); |
| 320 | } |
| 321 | finally |
| 322 | { |
| 323 | Marshal.FreeCoTaskMem(pValue); |
| 324 | } |
| 325 | } |
| 326 | |
| 327 | /// <inheritdoc/> |
| 328 | public int SendEmbeddedError(int errorCode, string message, int uiHint) |
| 329 | { |
| 330 | int result = 0; |
| 331 | this.engine.SendEmbeddedError(errorCode, message, uiHint, out result); |
| 332 | return result; |
| 333 | } |
| 334 | |
| 335 | /// <inheritdoc/> |
| 336 | public int SendEmbeddedProgress(int progressPercentage, int overallPercentage) |
| 337 | { |
| 338 | int result = 0; |
| 339 | this.engine.SendEmbeddedProgress(progressPercentage, overallPercentage, out result); |
| 340 | return result; |
| 341 | } |
| 342 | |
| 343 | /// <inheritdoc/> |
| 344 | public void Quit(int exitCode) |
| 345 | { |
| 346 | this.engine.Quit(exitCode); |
| 347 | } |
| 348 | } |
| 349 | } |