winget-cli

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

RepositorySource.h (14382B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <winget/RepositorySearch.h>
      5 #include <winget/PackageTrackingCatalog.h>
      6 #include <AppInstallerProgress.h>
      7 #include <winget/Certificates.h>
      8 #include <winget/Authentication.h>
      9 
     10 #include <chrono>
     11 #include <filesystem>
     12 #include <memory>
     13 #include <optional>
     14 #include <string>
     15 #include <string_view>
     16 #include <vector>
     17 
     18 
     19 namespace AppInstaller::Repository
     20 {
     21     // The interval is of 100 nano seconds precision.This is used by file date period and the Windows::Foundation::TimeSpan exposed in COM api.
     22     using TimeSpan = std::chrono::duration<int64_t, std::ratio_multiply<std::ratio<100>, std::nano>>;
     23 
     24     struct ISourceReference;
     25     struct ISource;
     26 
     27     // Defines the origin of the source details.
     28     enum class SourceOrigin
     29     {
     30         Default,
     31         User,
     32         Predefined,
     33         GroupPolicy,
     34         Metadata,
     35         PackageTracking,
     36     };
     37 
     38     // Defines the trust level of the source.
     39     enum class SourceTrustLevel : uint32_t
     40     {
     41         None        = 0x00000000,
     42         Trusted     = 0x00000001,
     43         StoreOrigin = 0x00000002,
     44     };
     45 
     46     DEFINE_ENUM_FLAG_OPERATORS(SourceTrustLevel);
     47 
     48     // Converts a string_view to the corresponding SourceTrustLevel enum.
     49     SourceTrustLevel ConvertToSourceTrustLevelEnum(std::string_view trustLevel);
     50 
     51     // Converts a vector of trust level strings to the corresponding SourceTrustLevel enum flag.
     52     SourceTrustLevel ConvertToSourceTrustLevelFlag(std::vector<std::string> values);
     53 
     54     // Converts a SourceTrustLevel flag to a list of trust level strings.
     55     std::vector<std::string_view> SourceTrustLevelFlagToList(SourceTrustLevel trustLevel);
     56 
     57     // Converts a SourceTrustLevel enum to the corresponding string.
     58     std::string_view SourceTrustLevelEnumToString(SourceTrustLevel trustLevel);
     59 
     60     // Gets the full trust level string name for display.
     61     std::string GetSourceTrustLevelForDisplay(SourceTrustLevel trustLevel);
     62 
     63     std::string_view ToString(SourceOrigin origin);
     64 
     65     // Fields that require user agreements.
     66     enum class ImplicitAgreementFieldEnum : int
     67     {
     68         None = 0x0,
     69         Market = 0x1,
     70     };
     71 
     72     DEFINE_ENUM_FLAG_OPERATORS(ImplicitAgreementFieldEnum);
     73 
     74     // A predefined source.
     75     // These sources are not under the direct control of the user, such as packages installed on the system.
     76     enum class PredefinedSource
     77     {
     78         // Default behavior. Contains ARP packages installed as for user and for machine, MSIX packages for current user.
     79         Installed,
     80         // Only contains packages installed as for user
     81         InstalledUser,
     82         // Only contains packages installed as for machine
     83         InstalledMachine,
     84         ARP,
     85         MSIX,
     86         Installing,
     87         // Same as `Installed`, but creating the source reference for this is sufficient to cause the cache to be updated
     88         // on next Open of any `Installed` or `InstalledForceCacheUpdate`.
     89         InstalledForceCacheUpdate,
     90     };
     91 
     92     // A well known source.
     93     // These come with the app and can be disabled but not removed.
     94     enum class WellKnownSource
     95     {
     96         WinGet,
     97         MicrosoftStore,
     98         DesktopFrameworks,
     99     };
    100 
    101     // Search behavior for composite sources.
    102     // Only relevant for composite sources with an installed source, not for aggregates of multiple available sources.
    103     // Installed and available packages in the result are always correlated when possible.
    104     enum class CompositeSearchBehavior
    105     {
    106         // Search only installed packages.
    107         Installed,
    108         // Search both installed and available packages.
    109         AllPackages,
    110         // Search only available packages.
    111         AvailablePackages,
    112     };
    113 
    114     // Interface for source configurations. Source configurations are used to get a source reference without opening the source.
    115     struct SourceDetails
    116     {
    117         // The name of the source.
    118         std::string Name;
    119 
    120         // The type of the source.
    121         std::string Type;
    122 
    123         // The argument used when adding the source.
    124         std::string Arg;
    125 
    126         // The source's extra data string.
    127         std::string Data;
    128 
    129         // The source's unique identifier.
    130         std::string Identifier;
    131 
    132         // The origin of the source.
    133         SourceOrigin Origin = SourceOrigin::Default;
    134 
    135         // The trust level of the source
    136         SourceTrustLevel TrustLevel = SourceTrustLevel::None;
    137 
    138         // The last time that this source was updated.
    139         std::chrono::system_clock::time_point LastUpdateTime = {};
    140 
    141         // Stores the earliest time that a background update should be attempted.
    142         std::chrono::system_clock::time_point DoNotUpdateBefore = {};
    143 
    144         // Whether the source supports InstalledSource correlation.
    145         bool SupportInstalledSearchCorrelation = true;
    146 
    147         // The configuration of how the server certificate will be validated.
    148         Certificates::PinningConfiguration CertificatePinningConfiguration;
    149 
    150         // This value is used as an alternative to the `Arg` value if it is failing to function properly.
    151         // The alternate location must point to identical data or inconsistencies may arise.
    152         std::string AlternateArg;
    153 
    154         // Whether the source should be hidden by default unless explicitly declared.
    155         bool Explicit = false;
    156     };
    157 
    158     // Check if a source matches a well known source
    159     std::optional<WellKnownSource> CheckForWellKnownSource(const SourceDetails& sourceDetails);
    160 
    161     // Individual source agreement entry. Label will be highlighted in the display as the key of the agreement entry.
    162     struct SourceAgreement
    163     {
    164         SourceAgreement() = default;
    165 
    166         SourceAgreement(std::string label, std::string text, std::string url) :
    167             Label(std::move(label)), Text(std::move(text)), Url(std::move(url)) {}
    168 
    169         std::string Label;
    170         std::string Text;
    171         std::string Url;
    172     };
    173 
    174     // Interface for retrieving information about a source after opening the source.
    175     struct SourceInformation
    176     {
    177         // Identifier of the source agreements. This is used to identify if source agreements have changed.
    178         std::string SourceAgreementsIdentifier;
    179 
    180         // List of source agreements that require user to accept.
    181         std::vector<SourceAgreement> SourceAgreements;
    182 
    183         // Unsupported match fields in search request. If this field is in the filters, the request may fail.
    184         std::vector<std::string> UnsupportedPackageMatchFields;
    185 
    186         // Required match fields in search request. If this field is not found in the filters, the request may fail(except Market).
    187         std::vector<std::string> RequiredPackageMatchFields;
    188 
    189         // Unsupported query parameters in get manifest request.
    190         std::vector<std::string> UnsupportedQueryParameters;
    191 
    192         // Required query parameters in get manifest request.
    193         std::vector<std::string> RequiredQueryParameters;
    194 
    195         // Source authentication info.
    196         Authentication::AuthenticationInfo Authentication;
    197     };
    198 
    199     // Allows calling code to inquire about specific features of an ISource implementation.
    200     // The default state of any new flag is false.
    201     enum class SourceFeatureFlag
    202     {
    203         // If true, the manifests for this source may contain more data than is available from just the
    204         // version information found from a search.
    205         ManifestMayContainAdditionalSystemReferenceStrings,
    206     };
    207 
    208     // Represents a source which would be interacted from outside of repository lib.
    209     struct Source
    210     {
    211         // Default constructor with an empty source.
    212         Source();
    213 
    214         // Constructor to get a named source, passing empty string will get all available sources.
    215         Source(std::string_view name);
    216 
    217         // Constructor to get a PredefinedSource. Like installed source, etc.
    218         Source(PredefinedSource source);
    219 
    220         // Constructor to get a source coming with winget. Like winget community source, etc.
    221         Source(WellKnownSource source);
    222 
    223         // Constructor for a source to be added.
    224         Source(std::string_view name, std::string_view arg, std::string_view type, SourceTrustLevel trustLevel, bool isExplicit);
    225 
    226         // Constructor for creating a composite source from a list of available sources.
    227         Source(const std::vector<Source>& availableSources);
    228 
    229         // Constructor for creating a composite source from an installed source and available source(may be composite already).
    230         Source(
    231             const Source& installedSource,
    232             const Source& availableSource,
    233             CompositeSearchBehavior searchBehavior = CompositeSearchBehavior::Installed);
    234 
    235         // Constructor for creating a Source object from an existing ISource.
    236         // Should only be used internally by ISource implementations to return the value from IPackageVersion::GetSource.
    237         Source(std::shared_ptr<ISource> source);
    238 
    239         // Bool operator to check if a source reference is successfully acquired.
    240         // Theoretically, the constructor could just throw when CreateSource returns empty.
    241         // To avoid putting try catch everywhere, we use bool operator here.
    242         operator bool() const;
    243 
    244         // Determines if the sources are equivalent.
    245         // Currently only works for individual sources, not composites.
    246         bool operator==(const Source& other) const;
    247         bool operator!=(const Source& other) const;
    248 
    249         // Gets the source's identifier; a unique identifier independent of the name
    250         // that will not change between a remove/add or between additional adds.
    251         // Must be suitable for filesystem names unless the source is internal to winget,
    252         // in which case the identifier should begin with a '*' character.
    253         std::string GetIdentifier() const;
    254 
    255         // Get the source's configuration details from settings.
    256         SourceDetails GetDetails() const;
    257 
    258         // Get the source's information.
    259         SourceInformation GetInformation() const;
    260 
    261         // Query the value of the given feature flag.
    262         // The default state of any new flag is false.
    263         bool QueryFeatureFlag(SourceFeatureFlag flag) const;
    264 
    265         // Returns true if the origin type can contain available packages.
    266         bool ContainsAvailablePackages() const;
    267 
    268         // Set custom header. Must be set before Open to have effect.
    269         bool SetCustomHeader(std::optional<std::string> header);
    270 
    271         // Set caller. Must be set before Open to have effect.
    272         void SetCaller(std::string caller);
    273 
    274         // Set authentication arguments. Must be set before Open to have effect.
    275         void SetAuthenticationArguments(Authentication::AuthenticationArguments args);
    276 
    277         // Set background update check interval.
    278         void SetBackgroundUpdateInterval(TimeSpan interval);
    279 
    280         // Indicates that we are only interested in the PackageTrackingCatalog for the source.
    281         // Must be set before Open to have effect, and will prevent the underlying source from being updated or opened.
    282         void InstalledPackageInformationOnly(bool value);
    283 
    284         // Determines if this source refers to the given well known source.
    285         bool IsWellKnownSource(WellKnownSource wellKnownSource);
    286 
    287         // Execute a search on the source.
    288         SearchResult Search(const SearchRequest& request) const;
    289 
    290         /* Source agreements */
    291 
    292         // Get required agreement fields info.
    293         ImplicitAgreementFieldEnum GetAgreementFieldsFromSourceInformation() const;
    294 
    295         // Checks the source agreements and returns if agreements are satisfied.
    296         bool CheckSourceAgreements() const;
    297 
    298         // Saves the accepted source agreements in metadata.
    299         void SaveAcceptedSourceAgreements() const;
    300 
    301         /* Composite sources */
    302 
    303         // Gets a value indicating whether this source is a composite of other sources,
    304         // and thus the packages may come from disparate sources as well.
    305         bool IsComposite() const;
    306 
    307         // Gets the available sources if the source is composite.
    308         std::vector<Source> GetAvailableSources() const;
    309 
    310         /* Writable sources */
    311 
    312         // Adds a package version to the source.
    313         void AddPackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath);
    314 
    315         // Removes a package version from the source.
    316         void RemovePackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath);
    317 
    318         /* Source operations */
    319 
    320         // Opens the source. This function should throw upon open failure rather than returning an empty pointer.
    321         std::vector<SourceDetails> Open(IProgressCallback& progress);
    322 
    323         // Add source. Source add command.
    324         bool Add(IProgressCallback& progress);
    325 
    326         // Update Source. Source update command.
    327         std::vector<SourceDetails> Update(IProgressCallback& progress);
    328 
    329         // Remove source. Source remove command.
    330         bool Remove(IProgressCallback& progress);
    331 
    332         // Gets the tracking catalog for the current source.
    333         PackageTrackingCatalog GetTrackingCatalog() const;
    334 
    335         // Drop source. Source reset command.
    336         static bool DropSource(std::string_view name);
    337 
    338         // Get a list of all available SourceDetails.
    339         static std::vector<SourceDetails> GetCurrentSources();
    340 
    341         // Get a default source type is the source type used when adding a source without specifying a type.
    342         static std::string_view GetDefaultSourceType();
    343 
    344     private:
    345         void InitializeSourceReference(std::string_view name);
    346 
    347         std::vector<std::shared_ptr<ISourceReference>> m_sourceReferences;
    348         std::shared_ptr<ISource> m_source;
    349         bool m_isSourceToBeAdded = false;
    350         bool m_isComposite = false;
    351         std::optional<TimeSpan> m_backgroundUpdateInterval;
    352         bool m_installedPackageInformationOnly = false;
    353         mutable std::shared_ptr<PackageTrackingCatalog> m_trackingCatalog;
    354     };
    355 }