1
0
Fork 0
mirror of https://github.com/Atmosphere-NX/Atmosphere.git synced 2024-12-29 13:46:01 +00:00
Atmosphere/stratosphere/ams_mitm/source/sysupdater/sysupdater_async_impl.hpp

113 lines
3.7 KiB
C++
Raw Normal View History

/*
* Copyright (c) Atmosphère-NX
*
* This program is free software; you can redistribute it and/or modify it
* under the terms and conditions of the GNU General Public License,
* version 2, as published by the Free Software Foundation.
*
* This program is distributed in the hope it will be useful, but WITHOUT
* ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or
* FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for
* more details.
*
* You should have received a copy of the GNU General Public License
* along with this program. If not, see <http://www.gnu.org/licenses/>.
*/
#pragma once
#include <stratosphere.hpp>
#include "sysupdater_thread_allocator.hpp"
namespace ams::mitm::sysupdater {
class ErrorContextHolder {
private:
2021-10-10 08:14:06 +01:00
err::ErrorContext m_error_context;
public:
2021-10-10 08:14:06 +01:00
constexpr ErrorContextHolder() : m_error_context{} { /* ... */ }
virtual ~ErrorContextHolder() { /* ... */ }
template<typename T>
Result SaveErrorContextIfFailed(T &async, Result result) {
ON_RESULT_FAILURE { async.GetErrorContext(std::addressof(m_error_context)); };
R_RETURN(result);
}
template<typename T>
Result GetAndSaveErrorContext(T &async) {
R_RETURN(this->SaveErrorContextIfFailed(async, async.Get()));
}
template<typename T>
Result SaveInternalTaskErrorContextIfFailed(T &async, Result result) {
ON_RESULT_FAILURE { async.CreateErrorContext(std::addressof(m_error_context)); };
R_RETURN(result);
}
const err::ErrorContext &GetErrorContextImpl() {
2021-10-10 08:14:06 +01:00
return m_error_context;
}
};
class AsyncBase {
public:
virtual ~AsyncBase() { /* ... */ }
static Result ToAsyncResult(Result result);
Result Cancel() {
this->CancelImpl();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) {
*out = {};
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
private:
virtual void CancelImpl() = 0;
};
class AsyncResultBase : public AsyncBase {
public:
virtual ~AsyncResultBase() { /* ... */ }
Result Get() {
R_RETURN(ToAsyncResult(this->GetImpl()));
}
private:
virtual Result GetImpl() = 0;
};
static_assert(ns::impl::IsIAsyncResult<AsyncResultBase>);
/* NOTE: Based off of ns AsyncPrepareCardUpdateImpl. */
/* We don't implement the RequestServer::ManagedStop details, as we don't implement stoppable request list. */
class AsyncPrepareSdCardUpdateImpl : public AsyncResultBase, private ErrorContextHolder {
private:
2021-10-10 08:14:06 +01:00
Result m_result;
os::SystemEvent m_event;
util::optional<ThreadInfo> m_thread_info;
ncm::InstallTaskBase *m_task;
public:
2021-10-10 08:14:06 +01:00
AsyncPrepareSdCardUpdateImpl(ncm::InstallTaskBase *task) : m_result(ResultSuccess()), m_event(os::EventClearMode_ManualClear, true), m_thread_info(), m_task(task) { /* ... */ }
virtual ~AsyncPrepareSdCardUpdateImpl();
2021-10-10 08:14:06 +01:00
os::SystemEvent &GetEvent() { return m_event; }
virtual Result GetErrorContext(sf::Out<err::ErrorContext> out) override {
*out = ErrorContextHolder::GetErrorContextImpl();
2022-03-26 07:14:36 +00:00
R_SUCCEED();
}
Result Run();
private:
Result Execute();
virtual void CancelImpl() override;
2021-10-10 08:14:06 +01:00
virtual Result GetImpl() override { return m_result; }
};
}