winget-cli

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

PredefinedWriteableSourceFactory.cpp (4329B)


      1 // Copyright (c) Microsoft Corporation.
      2 // Licensed under the MIT License.
      3 #include "pch.h"
      4 #include "Microsoft/ARPHelper.h"
      5 #include "Microsoft/PredefinedWriteableSourceFactory.h"
      6 #include "Microsoft/SQLiteIndex.h"
      7 #include "Microsoft/SQLiteIndexSource.h"
      8 #include <winget/ManifestInstaller.h>
      9 
     10 #include <winget/Registry.h>
     11 #include <AppInstallerArchitecture.h>
     12 
     13 using namespace std::string_literals;
     14 using namespace std::string_view_literals;
     15 
     16 namespace AppInstaller::Repository::Microsoft
     17 {
     18     namespace
     19     {
     20         // The factory for the predefined installing source.
     21         struct PredefinedWriteableSourceFactoryImpl : public ISourceFactory
     22         {
     23             std::string_view TypeName() const override final
     24             {
     25                 return PredefinedWriteableSourceFactory::Type();
     26             }
     27 
     28             std::shared_ptr<ISourceReference> Create(const SourceDetails& details) override final;
     29 
     30             bool Add(SourceDetails&, IProgressCallback&) override final
     31             {
     32                 // Add should never be needed, as this is predefined.
     33                 THROW_HR(E_NOTIMPL);
     34             }
     35 
     36             bool Update(const SourceDetails&, IProgressCallback&) override final
     37             {
     38                 // Update could be used later, but not for now.
     39                 THROW_HR(E_NOTIMPL);
     40             }
     41 
     42             bool Remove(const SourceDetails&, IProgressCallback&) override final
     43             {
     44                 // Similar to add, remove should never be needed.
     45                 THROW_HR(E_NOTIMPL);
     46             }
     47         };
     48 
     49         struct PredefinedWriteableSourceReference : public ISourceReference
     50         {
     51             PredefinedWriteableSourceReference(const SourceDetails& details) : m_details(details)
     52             {
     53                 m_details.Identifier = "*PredefinedWriteableSource";
     54             }
     55 
     56             std::string GetIdentifier() override { return m_details.Identifier; }
     57 
     58             SourceDetails& GetDetails() override { return m_details; };
     59 
     60             std::shared_ptr<ISource> Open(IProgressCallback&) override
     61             {
     62                 // Installing is the only type right now so just return the Installing source to all callers.
     63                 // Since the source is writeable, it must be shared by all callers that try to open it
     64                 // since queries on one instance would not see what was written on another instance.
     65                 std::call_once(g_InstallingSourceOnceFlag,
     66                     [&]()
     67                     {
     68                         // Create an in memory index without paths or dependencies
     69                         SQLiteIndex index = SQLiteIndex::CreateNew(SQLITE_MEMORY_DB_CONNECTION_TARGET, SQLite::Version::Latest(), SQLiteIndex::CreateOptions::SupportPathless | SQLiteIndex::CreateOptions::DisableDependenciesSupport);
     70 
     71                         g_sharedSource = std::make_shared<SQLiteIndexWriteableSource>(m_details, std::move(index), true);
     72                     });
     73 
     74                 return g_sharedSource;
     75             }
     76 
     77         private:
     78             SourceDetails m_details;
     79             static std::shared_ptr<ISource> g_sharedSource;
     80             static std::once_flag g_InstallingSourceOnceFlag;
     81         };
     82 
     83         std::shared_ptr<ISource> PredefinedWriteableSourceReference::g_sharedSource = nullptr;
     84         std::once_flag PredefinedWriteableSourceReference::g_InstallingSourceOnceFlag;
     85 
     86         std::shared_ptr<ISourceReference> PredefinedWriteableSourceFactoryImpl::Create(const SourceDetails& details)
     87         {
     88             THROW_HR_IF(E_INVALIDARG, details.Type != PredefinedWriteableSourceFactory::Type());
     89 
     90             return std::make_shared<PredefinedWriteableSourceReference>(details);
     91         }
     92     }
     93 
     94     std::string_view PredefinedWriteableSourceFactory::TypeToString(WriteableType type)
     95     {
     96         switch (type)
     97         {
     98         case AppInstaller::Repository::Microsoft::PredefinedWriteableSourceFactory::WriteableType::Installing:
     99             return "Installing"sv;
    100         default:
    101             return "Unknown"sv;
    102         }
    103     }
    104 
    105     std::unique_ptr<ISourceFactory> PredefinedWriteableSourceFactory::Create()
    106     {
    107         return std::make_unique<PredefinedWriteableSourceFactoryImpl>();
    108     }
    109 }