Bug 1844169 - part8 : use macros to remove code duplication. r=jolin

Differential Revision: https://phabricator.services.mozilla.com/D191354
This commit is contained in:
alwu 2023-10-24 23:18:02 +00:00
Родитель d28c7e7af7
Коммит 1f55944f31
1 изменённых файлов: 57 добавлений и 135 удалений

Просмотреть файл

@ -17,6 +17,57 @@ namespace mozilla {
EME_LOG("MFCDMChild[%p]@%s: " msg, this, __func__, ##__VA_ARGS__) EME_LOG("MFCDMChild[%p]@%s: " msg, this, __func__, ##__VA_ARGS__)
#define SLOG(msg, ...) EME_LOG("MFCDMChild@%s: " msg, __func__, ##__VA_ARGS__) #define SLOG(msg, ...) EME_LOG("MFCDMChild@%s: " msg, __func__, ##__VA_ARGS__)
#define HANDLE_PENDING_PROMISE(method, callsite, promise, promiseId) \
do { \
promise->Then( \
self->mManagerThread, callsite, \
[self, promiseId, callsite]( \
PMFCDMChild::method##Promise::ResolveOrRejectValue&& result) { \
auto iter = self->mPendingGenericPromises.find(promiseId); \
if (iter == self->mPendingGenericPromises.end()) { \
return; \
} \
auto& promiseHolder = iter->second; \
if (result.IsResolve()) { \
if (NS_SUCCEEDED(result.ResolveValue())) { \
promiseHolder.ResolveIfExists(true, callsite); \
} else { \
promiseHolder.RejectIfExists(result.ResolveValue(), callsite); \
} \
} else { \
/* IPC die */ \
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, callsite); \
} \
self->mPendingGenericPromises.erase(iter); \
}); \
} while (0)
#define INVOKE_ASYNC(method, promiseId, param1) \
do { \
auto callsite = __func__; \
using ParamType = std::remove_reference<decltype(param1)>::type; \
mManagerThread->Dispatch(NS_NewRunnableFunction( \
callsite, [self = RefPtr{this}, callsite, promiseId, \
param_1 = std::forward<ParamType>(param1)] { \
auto p = self->Send##method(param_1); \
HANDLE_PENDING_PROMISE(method, callsite, p, promiseId); \
})); \
} while (0)
#define INVOKE_ASYNC2(method, promiseId, param1, param2) \
do { \
auto callsite = __func__; \
using ParamType1 = std::remove_reference<decltype(param1)>::type; \
using ParamType2 = std::remove_reference<decltype(param2)>::type; \
mManagerThread->Dispatch(NS_NewRunnableFunction( \
callsite, [self = RefPtr{this}, callsite, promiseId, \
param_1 = std::forward<ParamType1>(param1), \
param_2 = std::forward<ParamType2>(param2)] { \
auto p = self->Send##method(param_1, param_2); \
HANDLE_PENDING_PROMISE(method, callsite, p, promiseId); \
})); \
} while (0)
MFCDMChild::MFCDMChild(const nsAString& aKeySystem) MFCDMChild::MFCDMChild(const nsAString& aKeySystem)
: mKeySystem(aKeySystem), : mKeySystem(aKeySystem),
mManagerThread(RemoteDecoderManagerChild::GetManagerThread()), mManagerThread(RemoteDecoderManagerChild::GetManagerThread()),
@ -274,33 +325,7 @@ RefPtr<GenericPromise> MFCDMChild::LoadSession(
mPendingGenericPromises.end()); mPendingGenericPromises.end());
mPendingGenericPromises.emplace(aPromiseId, mPendingGenericPromises.emplace(aPromiseId,
MozPromiseHolder<GenericPromise>{}); MozPromiseHolder<GenericPromise>{});
mManagerThread->Dispatch(NS_NewRunnableFunction( INVOKE_ASYNC2(LoadSession, aPromiseId, aSessionType, nsString{aSessionId});
__func__, [self = RefPtr{this}, this, aSessionType,
sessionId = nsString{aSessionId}, aPromiseId] {
SendLoadSession(aSessionType, sessionId)
->Then(mManagerThread, __func__,
[self, this, aPromiseId](
PMFCDMChild::LoadSessionPromise::ResolveOrRejectValue&&
aResult) {
auto iter = mPendingGenericPromises.find(aPromiseId);
if (iter == mPendingGenericPromises.end()) {
return;
}
auto& promiseHolder = iter->second;
if (aResult.IsResolve()) {
if (NS_SUCCEEDED(aResult.ResolveValue())) {
promiseHolder.ResolveIfExists(true, __func__);
} else {
promiseHolder.RejectIfExists(aResult.ResolveValue(),
__func__);
}
} else {
// IPC died
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, __func__);
}
mPendingGenericPromises.erase(iter);
});
}));
return mPendingGenericPromises[aPromiseId].Ensure(__func__); return mPendingGenericPromises[aPromiseId].Ensure(__func__);
} }
@ -318,33 +343,8 @@ RefPtr<GenericPromise> MFCDMChild::UpdateSession(uint32_t aPromiseId,
mPendingGenericPromises.end()); mPendingGenericPromises.end());
mPendingGenericPromises.emplace(aPromiseId, mPendingGenericPromises.emplace(aPromiseId,
MozPromiseHolder<GenericPromise>{}); MozPromiseHolder<GenericPromise>{});
mManagerThread->Dispatch(NS_NewRunnableFunction( INVOKE_ASYNC2(UpdateSession, aPromiseId, nsString{aSessionId},
__func__, [self = RefPtr{this}, this, sessionId = nsString{aSessionId}, std::move(aResponse));
response = std::move(aResponse), aPromiseId] {
SendUpdateSession(sessionId, response)
->Then(mManagerThread, __func__,
[self, this, aPromiseId](
PMFCDMChild::UpdateSessionPromise::ResolveOrRejectValue&&
aResult) {
auto iter = mPendingGenericPromises.find(aPromiseId);
if (iter == mPendingGenericPromises.end()) {
return;
}
auto& promiseHolder = iter->second;
if (aResult.IsResolve()) {
if (NS_SUCCEEDED(aResult.ResolveValue())) {
promiseHolder.ResolveIfExists(true, __func__);
} else {
promiseHolder.RejectIfExists(aResult.ResolveValue(),
__func__);
}
} else {
// IPC died
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, __func__);
}
mPendingGenericPromises.erase(iter);
});
}));
return mPendingGenericPromises[aPromiseId].Ensure(__func__); return mPendingGenericPromises[aPromiseId].Ensure(__func__);
} }
@ -361,33 +361,7 @@ RefPtr<GenericPromise> MFCDMChild::CloseSession(uint32_t aPromiseId,
mPendingGenericPromises.end()); mPendingGenericPromises.end());
mPendingGenericPromises.emplace(aPromiseId, mPendingGenericPromises.emplace(aPromiseId,
MozPromiseHolder<GenericPromise>{}); MozPromiseHolder<GenericPromise>{});
mManagerThread->Dispatch(NS_NewRunnableFunction( INVOKE_ASYNC(CloseSession, aPromiseId, nsString{aSessionId});
__func__, [self = RefPtr{this}, this, sessionId = nsString{aSessionId},
aPromiseId] {
SendCloseSession(sessionId)->Then(
mManagerThread, __func__,
[self, this, aPromiseId](
PMFCDMChild::CloseSessionPromise::ResolveOrRejectValue&&
aResult) {
auto iter = mPendingGenericPromises.find(aPromiseId);
if (iter == mPendingGenericPromises.end()) {
return;
}
auto& promiseHolder = iter->second;
if (aResult.IsResolve()) {
if (NS_SUCCEEDED(aResult.ResolveValue())) {
promiseHolder.ResolveIfExists(true, __func__);
} else {
promiseHolder.RejectIfExists(aResult.ResolveValue(),
__func__);
}
} else {
// IPC died
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, __func__);
}
mPendingGenericPromises.erase(iter);
});
}));
return mPendingGenericPromises[aPromiseId].Ensure(__func__); return mPendingGenericPromises[aPromiseId].Ensure(__func__);
} }
@ -404,33 +378,7 @@ RefPtr<GenericPromise> MFCDMChild::RemoveSession(uint32_t aPromiseId,
mPendingGenericPromises.end()); mPendingGenericPromises.end());
mPendingGenericPromises.emplace(aPromiseId, mPendingGenericPromises.emplace(aPromiseId,
MozPromiseHolder<GenericPromise>{}); MozPromiseHolder<GenericPromise>{});
mManagerThread->Dispatch(NS_NewRunnableFunction( INVOKE_ASYNC(RemoveSession, aPromiseId, nsString{aSessionId});
__func__, [self = RefPtr{this}, this, sessionId = nsString{aSessionId},
aPromiseId] {
SendRemoveSession(sessionId)->Then(
mManagerThread, __func__,
[self, this, aPromiseId](
PMFCDMChild::RemoveSessionPromise::ResolveOrRejectValue&&
aResult) {
auto iter = mPendingGenericPromises.find(aPromiseId);
if (iter == mPendingGenericPromises.end()) {
return;
}
auto& promiseHolder = iter->second;
if (aResult.IsResolve()) {
if (NS_SUCCEEDED(aResult.ResolveValue())) {
promiseHolder.ResolveIfExists(true, __func__);
} else {
promiseHolder.RejectIfExists(aResult.ResolveValue(),
__func__);
}
} else {
// IPC died
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, __func__);
}
mPendingGenericPromises.erase(iter);
});
}));
return mPendingGenericPromises[aPromiseId].Ensure(__func__); return mPendingGenericPromises[aPromiseId].Ensure(__func__);
} }
@ -447,33 +395,7 @@ RefPtr<GenericPromise> MFCDMChild::SetServerCertificate(
mPendingGenericPromises.end()); mPendingGenericPromises.end());
mPendingGenericPromises.emplace(aPromiseId, mPendingGenericPromises.emplace(aPromiseId,
MozPromiseHolder<GenericPromise>{}); MozPromiseHolder<GenericPromise>{});
mManagerThread->Dispatch(NS_NewRunnableFunction( INVOKE_ASYNC(SetServerCertificate, aPromiseId, std::move(aCert));
__func__,
[self = RefPtr{this}, this, cert = std::move(aCert), aPromiseId] {
SendSetServerCertificate(cert)->Then(
mManagerThread, __func__,
[self, this, aPromiseId](
PMFCDMChild::SetServerCertificatePromise::ResolveOrRejectValue&&
aResult) {
auto iter = mPendingGenericPromises.find(aPromiseId);
if (iter == mPendingGenericPromises.end()) {
return;
}
auto& promiseHolder = iter->second;
if (aResult.IsResolve()) {
if (NS_SUCCEEDED(aResult.ResolveValue())) {
promiseHolder.ResolveIfExists(true, __func__);
} else {
promiseHolder.RejectIfExists(aResult.ResolveValue(),
__func__);
}
} else {
// IPC died
promiseHolder.RejectIfExists(NS_ERROR_FAILURE, __func__);
}
mPendingGenericPromises.erase(iter);
});
}));
return mPendingGenericPromises[aPromiseId].Ensure(__func__); return mPendingGenericPromises[aPromiseId].Ensure(__func__);
} }