winget-cli

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

PinningData.h (4506B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #pragma once
      4 #include <winget/Pin.h>
      5 #include <winget/RepositorySearch.h>
      6 #include <map>
      7 #include <memory>
      8 #include <optional>
      9 #include <vector>
     10 
     11 namespace AppInstaller::Repository::Microsoft
     12 {
     13     struct PinningIndex;
     14 }
     15 
     16 namespace AppInstaller::Pinning
     17 {
     18     // Possible ways to consider pins when getting a package's available versions
     19     enum class PinBehavior
     20     {
     21         // Ignore pins, returns all available versions.
     22         IgnorePins,
     23         // Include available versions for packages with a Pinning pin.
     24         // Blocking pins and Gating pins still respected.
     25         IncludePinned,
     26         // Respect all the types of pins.
     27         ConsiderPins,
     28     };
     29 
     30     // The public representation of the pinning database.
     31     struct PinningData
     32     {
     33         // Creates an empty pinning data.
     34         PinningData();
     35 
     36         // Enum to make the pinning data disposition clear in the caller.
     37         enum class Disposition
     38         {
     39             // The data can only be read.
     40             ReadOnly,
     41             // The data can be read and written.
     42             ReadWrite,
     43         };
     44 
     45         // Creates a usable pinning data with the given read/write capability.
     46         PinningData(Disposition disposition);
     47 
     48         PinningData(const PinningData&);
     49         PinningData& operator=(const PinningData&);
     50         PinningData(PinningData&&) noexcept;
     51         PinningData& operator=(PinningData&&) noexcept;
     52         ~PinningData();
     53 
     54         // Determines if the pinning database is opened
     55         operator bool() const;
     56         bool IsDatabaseConnected() const;
     57 
     58         // Pass through functions to the index itself
     59         void AddOrUpdatePin(const Pin& pin);
     60         void RemovePin(const PinKey& pinKey);
     61         std::optional<Pin> GetPin(const PinKey& pinKey);
     62         std::vector<Pin> GetAllPins();
     63         bool ResetAllPins(std::string_view sourceId = {});
     64 
     65         // A type used for evaluating the pinning state for a given package.
     66         struct PinStateEvaluator
     67         {
     68             PinStateEvaluator(
     69                 PinBehavior behavior,
     70                 std::shared_ptr<AppInstaller::Repository::Microsoft::PinningIndex> database,
     71                 const std::shared_ptr<AppInstaller::Repository::IPackageVersion>& installedVersion);
     72 
     73             PinStateEvaluator(const PinStateEvaluator&);
     74             PinStateEvaluator& operator=(const PinStateEvaluator&);
     75             PinStateEvaluator(PinStateEvaluator&&) noexcept;
     76             PinStateEvaluator& operator=(PinStateEvaluator&&) noexcept;
     77 
     78             ~PinStateEvaluator();
     79 
     80             // Gets the latest available package version that fits within the pinning restrictions.
     81             // This should be the package object that contains available versions associated with the installed version for which this evaluator was created.
     82             std::shared_ptr<AppInstaller::Repository::IPackageVersion> GetLatestAvailableVersionForPins(const std::shared_ptr<AppInstaller::Repository::IPackageVersionCollection>& package);
     83 
     84             // Determines if the given version is an update to the installed version that this object was created with.
     85             // This should be a version associated with the installed version for which this evaluator was created.
     86             bool IsUpdate(const std::shared_ptr<AppInstaller::Repository::IPackageVersion>& availableVersion);
     87 
     88             // Determines the pin type to apply to the given version.
     89             PinType EvaluatePinType(const std::shared_ptr<AppInstaller::Repository::IPackageVersion>& packageVersion);
     90 
     91         private:
     92             PinBehavior m_behavior;
     93             std::shared_ptr<AppInstaller::Repository::Microsoft::PinningIndex> m_database;
     94             std::optional<Pin> m_installedPin;
     95             std::optional<Utility::VersionAndChannel> m_installedVersion;
     96             // Cache pins for available version to reduce database lookups.
     97             std::map<PinKey, std::optional<Pin>> m_availablePins;
     98         };
     99 
    100         // Creates an object for use in evaluating pinning data for a given package
    101         PinStateEvaluator CreatePinStateEvaluator(
    102             PinBehavior behavior,
    103             const std::shared_ptr<AppInstaller::Repository::IPackageVersion>& installedVersion);
    104 
    105     private:
    106         std::shared_ptr<AppInstaller::Repository::Microsoft::PinningIndex> m_database;
    107     };
    108 }