Bug 1287747 -GetFilesHelper must release callbacks and promises on the main-thread only, r=smaug

This commit is contained in:
Andrea Marchesini 2016-09-11 09:56:54 +02:00
Родитель d8f4f2f4dc
Коммит b5d19337d3
2 изменённых файлов: 53 добавлений и 1 удалений

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

@ -11,6 +11,53 @@
namespace mozilla {
namespace dom {
namespace {
// This class is used in the DTOR of GetFilesHelper to release resources in the
// correct thread.
class ReleaseRunnable final : public Runnable
{
public:
static void
MaybeReleaseOnMainThread(nsTArray<RefPtr<Promise>>& aPromises,
nsTArray<RefPtr<GetFilesCallback>>& aCallbacks)
{
if (NS_IsMainThread()) {
return;
}
if (!aPromises.IsEmpty() || !aCallbacks.IsEmpty()) {
RefPtr<ReleaseRunnable> runnable =
new ReleaseRunnable(aPromises, aCallbacks);
NS_DispatchToMainThread(runnable);
}
}
NS_IMETHOD
Run() override
{
MOZ_ASSERT(NS_IsMainThread());
mPromises.Clear();
mCallbacks.Clear();
return NS_OK;
}
private:
ReleaseRunnable(nsTArray<RefPtr<Promise>>& aPromises,
nsTArray<RefPtr<GetFilesCallback>>& aCallbacks)
{
mPromises.SwapElements(aPromises);
mCallbacks.SwapElements(aCallbacks);
}
nsTArray<RefPtr<Promise>> mPromises;
nsTArray<RefPtr<GetFilesCallback>> mCallbacks;
};
} // anonymous
///////////////////////////////////////////////////////////////////////////////
// GetFilesHelper Base class
@ -81,6 +128,11 @@ GetFilesHelper::GetFilesHelper(nsIGlobalObject* aGlobal, bool aRecursiveFlag)
{
}
GetFilesHelper::~GetFilesHelper()
{
ReleaseRunnable::MaybeReleaseOnMainThread(mPromises, mCallbacks);
}
void
GetFilesHelper::AddPromise(Promise* aPromise)
{

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

@ -99,7 +99,7 @@ public:
protected:
GetFilesHelper(nsIGlobalObject* aGlobal, bool aRecursiveFlag);
virtual ~GetFilesHelper() {}
virtual ~GetFilesHelper();
void
SetDirectoryPath(const nsAString& aDirectoryPath)