@@ -21,22 +21,39 @@ internal sealed class NativeWinGetHelper : IWinGetManagerHelper
2121 public PackageManager WinGetManager = null ! ;
2222 public static PackageManager ? ExternalWinGetManager ;
2323 private readonly WinGet Manager ;
24+ private readonly Func < WinGet , IWinGetManagerHelper > _bundledHelperFactory ;
2425
2526 public string ActivationMode { get ; private set ; } = string . Empty ;
2627 public string ActivationSource { get ; private set ; } = string . Empty ;
2728 private bool _isBundledActivation ;
2829
30+ internal static IReadOnlyList < string > PreferredActivationModes =>
31+ [
32+ "packaged COM registration" ,
33+ "lower-trust COM registration" ,
34+ "bundled in-proc COM" ,
35+ ] ;
36+
2937 public NativeWinGetHelper ( WinGet manager )
38+ : this ( manager , bundledHelperFactory : null , skipInitialization : false ) { }
39+
40+ internal NativeWinGetHelper (
41+ WinGet manager ,
42+ Func < WinGet , IWinGetManagerHelper > ? bundledHelperFactory ,
43+ bool skipInitialization
44+ )
3045 {
3146 Manager = manager ;
47+ _bundledHelperFactory =
48+ bundledHelperFactory ?? ( static manager => new BundledWinGetHelper ( manager ) ) ;
3249 if ( CoreTools . IsAdministrator ( ) )
3350 {
3451 Logger . Info (
3552 "Running elevated, WinGet class registration is likely to fail unless using lower trust class registration is allowed in settings"
3653 ) ;
3754 }
3855
39- if ( TryInitializeBundledFactory ( ) )
56+ if ( skipInitialization )
4057 {
4158 return ;
4259 }
@@ -46,36 +63,47 @@ public NativeWinGetHelper(WinGet manager)
4663 return ;
4764 }
4865
49- InitializeLowerTrustFactory ( ) ;
66+ if ( TryInitializeLowerTrustFactory ( ) )
67+ {
68+ return ;
69+ }
70+
71+ InitializeBundledFactory ( ) ;
5072 }
5173
52- private bool TryInitializeBundledFactory ( )
74+ internal void UseBundledActivationForTesting ( )
75+ {
76+ _isBundledActivation = true ;
77+ ActivationMode = "bundled in-proc COM" ;
78+ ActivationSource = "test" ;
79+ }
80+
81+ private bool TryInitializeLowerTrustFactory ( )
5382 {
5483 try
5584 {
56- var factory = new WindowsPackageManagerBundledFactory ( ) ;
85+ var factory = new WindowsPackageManagerStandardFactory ( allowLowerTrustRegistration : true ) ;
5786 var winGetManager = factory . CreatePackageManager ( ) ;
5887 ApplyFactory (
5988 factory ,
6089 winGetManager ,
61- "bundled in-proc COM" ,
62- factory . LibraryPath ,
63- "Connected to WinGet API using bundled in-proc activation."
90+ "lower-trust COM registration " ,
91+ "system COM registration (allow lower trust)" ,
92+ "Connected to WinGet API using lower-trust COM activation."
6493 ) ;
65- _isBundledActivation = true ;
6694 return true ;
6795 }
6896 catch ( WinGetComActivationException ex )
6997 {
7098 Logger . Warn (
71- $ "Bundled WinGet in-proc activation failed ({ ex . HResultHex } : { ex . Reason } ), attempting packaged COM activation..."
99+ $ "Lower-trust WinGet COM activation failed ({ ex . HResultHex } : { ex . Reason } ), attempting bundled in-proc activation..."
72100 ) ;
73101 return false ;
74102 }
75103 catch ( Exception ex )
76104 {
77105 Logger . Warn (
78- $ "Bundled WinGet in-proc activation failed ({ ex . Message } ), attempting packaged COM activation..."
106+ $ "Lower-trust WinGet COM activation failed ({ ex . Message } ), attempting bundled in-proc activation..."
79107 ) ;
80108 return false ;
81109 }
@@ -112,17 +140,18 @@ private bool TryInitializeStandardFactory()
112140 }
113141 }
114142
115- private void InitializeLowerTrustFactory ( )
143+ private void InitializeBundledFactory ( )
116144 {
117- var factory = new WindowsPackageManagerStandardFactory ( allowLowerTrustRegistration : true ) ;
145+ var factory = new WindowsPackageManagerBundledFactory ( ) ;
118146 var winGetManager = factory . CreatePackageManager ( ) ;
119147 ApplyFactory (
120148 factory ,
121149 winGetManager ,
122- "lower-trust COM registration " ,
123- "system COM registration (allow lower trust)" ,
124- "Connected to WinGet API using lower-trust COM activation."
150+ "bundled in-proc COM" ,
151+ factory . LibraryPath ,
152+ "Connected to WinGet API using bundled in-proc activation."
125153 ) ;
154+ _isBundledActivation = true ;
126155 }
127156
128157 private void ApplyFactory (
@@ -273,7 +302,7 @@ public IReadOnlyList<Package> GetAvailableUpdates_UnSafe()
273302 if ( _isBundledActivation )
274303 {
275304 Logger . Info ( "WinGet is using bundled in-proc COM — falling back to CLI for update detection" ) ;
276- return new BundledWinGetHelper ( Manager ) . GetAvailableUpdates_UnSafe ( ) ;
305+ return _bundledHelperFactory ( Manager ) . GetAvailableUpdates_UnSafe ( ) ;
277306 }
278307
279308 var logger = Manager . TaskLogger . CreateNew ( LoggableTaskType . ListUpdates ) ;
@@ -344,6 +373,12 @@ var nativePackage in TaskRecycler<IReadOnlyList<CatalogPackage>>.RunOrAttach(
344373
345374 public IReadOnlyList < Package > GetInstalledPackages_UnSafe ( )
346375 {
376+ if ( _isBundledActivation )
377+ {
378+ Logger . Info ( "WinGet is using bundled in-proc COM — falling back to CLI for installed package detection" ) ;
379+ return _bundledHelperFactory ( Manager ) . GetInstalledPackages_UnSafe ( ) ;
380+ }
381+
347382 var logger = Manager . TaskLogger . CreateNew ( LoggableTaskType . ListInstalledPackages ) ;
348383 List < Package > packages = [ ] ;
349384 foreach (
0 commit comments