ISource.h (4784B)
1 // Copyright (c) Microsoft Corporation. 2 // Licensed under the MIT License. 3 #pragma once 4 #include "Public/winget/RepositorySource.h" 5 #include <memory> 6 7 namespace AppInstaller::Repository 8 { 9 // To allow for runtime casting from ISource to the specific types, this enum contains all of the ISource implementations. 10 enum class ISourceType 11 { 12 TestSource, 13 ConfigurableTestSource, 14 RestSource, 15 SQLiteIndexSource, 16 CompositeSource, 17 IMutablePackageSource, 18 OpenExceptionProxy, 19 }; 20 21 // Internal interface for interacting with a source from outside of the repository lib. 22 struct ISource 23 { 24 virtual ~ISource() = default; 25 26 // Gets the source's identifier; a unique identifier independent of the name 27 // that will not change between a remove/add or between additional adds. 28 // Must be suitable for filesystem names unless the source is internal to winget, 29 // in which case the identifier should begin with a '*' character. 30 virtual const std::string& GetIdentifier() const = 0; 31 32 // Get the source's configuration from settings. Source details can be used during opening the source. 33 virtual const SourceDetails& GetDetails() const = 0; 34 35 // Get the source's information after the source is opened. 36 virtual SourceInformation GetInformation() const { return {}; }; 37 38 // Query the value of the given feature flag. 39 // The default state of any new flag is false. 40 virtual bool QueryFeatureFlag(SourceFeatureFlag) const { return false; } 41 42 // Execute a search on the source. 43 virtual SearchResult Search(const SearchRequest& request) const = 0; 44 45 // Gets this object as the requested type, or null if it is not the requested type. 46 virtual void* CastTo(ISourceType type) = 0; 47 }; 48 49 // Does the equivalent of a dynamic_pointer_cast, but without it to allow RTTI to be disabled. 50 template <typename SourceType> 51 std::shared_ptr<SourceType> SourceCast(const std::shared_ptr<ISource>& source) 52 { 53 if (!source) 54 { 55 return {}; 56 } 57 58 void* castResult = source->CastTo(SourceType::SourceType); 59 60 if (!castResult) 61 { 62 return {}; 63 } 64 65 return std::shared_ptr<SourceType>(source, reinterpret_cast<SourceType*>(castResult)); 66 } 67 68 // Internal interface to represent source information; basically SourceDetails but with methods to enable differential behaviors. 69 struct ISourceReference 70 { 71 virtual ~ISourceReference() = default; 72 73 // Gets the source's identifier; a unique identifier independent of the name 74 // that will not change between a remove/add or between additional adds. 75 // Must be suitable for filesystem names unless the source is internal to winget, 76 // in which case the identifier should begin with a '*' character. 77 virtual std::string GetIdentifier() = 0; 78 79 // Get the source's configuration details from settings. 80 virtual SourceDetails& GetDetails() = 0; 81 82 // Get the source's information. 83 virtual SourceInformation GetInformation() { return {}; } 84 85 // Set custom header. Returns false if custom header is not supported. 86 virtual bool SetCustomHeader(std::optional<std::string>) { return false; } 87 88 // Set caller. 89 virtual void SetCaller(std::string) {} 90 91 // Set authentication arguments. 92 virtual void SetAuthenticationArguments(Authentication::AuthenticationArguments) {} 93 94 // Determine if the source needs to be updated before being opened. 95 virtual bool ShouldUpdateBeforeOpen(const std::optional<TimeSpan>&) { return false; } 96 97 // Opens the source. This function should throw upon open failure rather than returning an empty pointer. 98 virtual std::shared_ptr<ISource> Open(IProgressCallback& progress) = 0; 99 }; 100 101 // Internal interface extension to ISource for databases that can be updated after creation, like InstallingPackages 102 struct IMutablePackageSource 103 { 104 static constexpr AppInstaller::Repository::ISourceType SourceType = AppInstaller::Repository::ISourceType::IMutablePackageSource; 105 106 virtual ~IMutablePackageSource() = default; 107 108 // Adds a package version to the source. 109 virtual void AddPackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) = 0; 110 111 // Removes a package version from the source. 112 virtual void RemovePackageVersion(const Manifest::Manifest& manifest, const std::filesystem::path& relativePath) = 0; 113 }; 114 }