winget-cli

Unnamed repository; edit this file 'description' to name the repository.
Log | Files | Refs | README | LICENSE

SelfManagement.cpp (2137B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "winget/ExperimentalFeature.h"
      5 #include "winget/SelfManagement.h"
      6 #include "AppInstallerRuntime.h"
      7 
      8 namespace AppInstaller::SelfManagement
      9 {
     10     using namespace AppInstaller::Settings;
     11     using namespace std::string_view_literals;
     12     using namespace winrt::Windows::ApplicationModel;
     13     using namespace winrt::Windows::Management::Deployment;
     14     using namespace winrt::Windows::Services::Store;
     15 
     16     // Always use AppInstaller's package family name for wingetdev
     17     static constexpr std::wstring_view s_AppInstallerPfn = L"Microsoft.DesktopAppInstaller_8wekyb3d8bbwe"sv;
     18     constexpr std::wstring_view s_RemoteServerFileName = L"DotNet\\ConfigurationRemotingServer.exe";
     19 
     20     bool IsStubPreferred()
     21     {
     22         winrt::hstring packageFamilyName{ s_AppInstallerPfn };
     23 
     24         PackageManager packageManager;
     25         auto packageManager9 = packageManager.try_as<IPackageManager9>();
     26 
     27         if (!packageManager9)
     28         {
     29             // If the API isn't present, then the only option is full package.
     30             return false;
     31         }
     32 
     33         auto preference = packageManager9.GetPackageStubPreference(packageFamilyName);
     34 
     35         return preference == PackageStubPreference::Stub;
     36     }
     37 
     38     void SetStubPreferred(bool preferStub)
     39     {
     40         winrt::hstring packageFamilyName{ s_AppInstallerPfn };
     41 
     42         PackageManager packageManager;
     43         auto packageManager9 = packageManager.try_as<IPackageManager9>();
     44 
     45         THROW_HR_IF(HRESULT_FROM_WIN32(ERROR_OLD_WIN_VERSION), !packageManager9);
     46 
     47         packageManager9.SetPackageStubPreference(packageFamilyName, preferStub ? PackageStubPreference::Stub : PackageStubPreference::Full);
     48     }
     49 
     50     bool IsStubPackage()
     51     {
     52         // The right way to do it is to call FindPackage APIs from PackageManager, but that requires admin.
     53         std::filesystem::path serverPath = Runtime::GetPathTo(Runtime::PathName::SelfPackageRoot) / s_RemoteServerFileName;
     54         return !std::filesystem::exists(serverPath);
     55     }
     56 }