Bug 1436694 - Make a PostTask variant that returns a MozPromise. r=jib, r=jya

MozReview-Commit-ID: 76mLpntaU5v

--HG--
extra : rebase_source : bd0ac6102854b5d50d711c16c94c6cfd07274f69
This commit is contained in:
Andreas Pehrson 2018-02-15 15:18:12 +01:00
Родитель 030279dd8e
Коммит fc712f4a1a
2 изменённых файлов: 31 добавлений и 8 удалений

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

@ -2149,6 +2149,20 @@ MediaManager::PostTask(already_AddRefed<Runnable> task)
Get()->mMediaThread->message_loop()->PostTask(Move(task));
}
template<typename MozPromiseType, typename FunctionType>
/* static */ RefPtr<MozPromiseType>
MediaManager::PostTask(const char* aName, FunctionType&& aFunction)
{
MozPromiseHolder<MozPromiseType> holder;
RefPtr<MozPromiseType> promise = holder.Ensure(aName);
MediaManager::PostTask(NS_NewRunnableFunction(aName,
[h = Move(holder), func = Forward<FunctionType>(aFunction)]() mutable
{
func(h);
}));
return promise;
}
/* static */ nsresult
MediaManager::NotifyRecordingStatusChange(nsPIDOMWindowInner* aWindow)
{
@ -4050,14 +4064,11 @@ SourceListener::SetEnabledFor(TrackID aTrackID, bool aEnable)
return DeviceOperationPromise::CreateAndResolve(NS_OK, __func__);
}
RefPtr<DeviceOperationPromise::Private> promise =
new DeviceOperationPromise::Private(__func__);
MediaManager::PostTask(NewTaskFrom([self, device = state.mDevice,
aEnable, promise]() mutable {
promise->Resolve(aEnable ? device->Start() : device->Stop(), __func__);
}));
RefPtr<DeviceOperationPromise> result = promise.get();
return result;
return MediaManager::PostTask<DeviceOperationPromise>(__func__,
[self, device = state.mDevice, aEnable]
(MozPromiseHolder<DeviceOperationPromise>& h) {
h.Resolve(aEnable ? device->Start() : device->Stop(), __func__);
});
}, [](bool aDummy) {
// Timer was canceled by us. We signal this with NS_ERROR_ABORT.
return DeviceOperationPromise::CreateAndResolve(NS_ERROR_ABORT, __func__);

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

@ -148,6 +148,18 @@ public:
static MediaManager* GetIfExists();
static void StartupInit();
static void PostTask(already_AddRefed<Runnable> task);
/**
* Posts an async operation to the media manager thread.
* FunctionType must be a function that takes a `MozPromiseHolder&`.
*
* The returned promise is resolved or rejected by aFunction on the media
* manager thread.
*/
template<typename MozPromiseType, typename FunctionType>
static RefPtr<MozPromiseType>
PostTask(const char* aName, FunctionType&& aFunction);
#ifdef DEBUG
static bool IsInMediaThread();
#endif