Bug 1375412: Strengthen ownership of a11y::LazyInstantiator when posting runnable back to main thread; r=eeejay

MozReview-Commit-ID: A0IdEfAs4q9
This commit is contained in:
Aaron Klotz 2017-06-23 13:03:33 -06:00
parent ea272c6f03
commit eb1c707d34
2 changed files with 40 additions and 7 deletions

View File

@ -10,7 +10,6 @@
#include "mozilla/a11y/Accessible.h"
#include "mozilla/Assertions.h"
#include "mozilla/mscom/MainThreadRuntime.h"
#include "mozilla/mscom/Ptr.h"
#include "mozilla/mscom/Registration.h"
#include "mozilla/UniquePtr.h"
#include "nsAccessibilityService.h"
@ -264,9 +263,11 @@ LazyInstantiator::ShouldInstantiate(const DWORD aClientTid)
// Call GatherTelemetry on a background thread because it does I/O on
// the executable file to retrieve version information.
nsCOMPtr<nsIRunnable> runnable(
NewRunnableMethod<nsCOMPtr<nsIFile>>(this,
NewRunnableMethod<nsCOMPtr<nsIFile>, RefPtr<AccumulateRunnable>>(
this,
&LazyInstantiator::GatherTelemetry,
clientExe));
clientExe,
new AccumulateRunnable(this)));
NS_NewThread(getter_AddRefs(mTelemetryThread), runnable);
}
#endif // defined(MOZ_TELEMETRY_REPORTING)
@ -328,7 +329,8 @@ LazyInstantiator::AppendVersionInfo(nsIFile* aClientExe,
}
void
LazyInstantiator::GatherTelemetry(nsIFile* aClientExe)
LazyInstantiator::GatherTelemetry(nsIFile* aClientExe,
AccumulateRunnable* aRunnable)
{
MOZ_ASSERT(!NS_IsMainThread());
@ -338,10 +340,11 @@ LazyInstantiator::GatherTelemetry(nsIFile* aClientExe)
AppendVersionInfo(aClientExe, value);
}
aRunnable->SetData(value);
// Now that we've (possibly) obtained version info, send the resulting
// string back to the main thread to accumulate in telemetry.
NS_DispatchToMainThread(NewNonOwningRunnableMethod<nsString>(this,
&LazyInstantiator::AccumulateTelemetry, value));
NS_DispatchToMainThread(aRunnable);
}
void

View File

@ -8,6 +8,7 @@
#define mozilla_a11y_LazyInstantiator_h
#include "IUnknownImpl.h"
#include "mozilla/mscom/Ptr.h"
#include "mozilla/RefPtr.h"
#include "nsString.h"
#if defined(MOZ_TELEMETRY_REPORTING)
@ -86,8 +87,37 @@ private:
bool GetClientExecutableName(const DWORD aClientTid, nsIFile** aOutClientExe);
#if defined(MOZ_TELEMETRY_REPORTING)
class AccumulateRunnable final : public Runnable
{
public:
explicit AccumulateRunnable(LazyInstantiator* aObj)
: Runnable("mozilla::a11y::LazyInstantiator::AccumulateRunnable")
, mObj(aObj)
{
MOZ_ASSERT(NS_IsMainThread());
aObj->AddRef();
}
void SetData(const nsAString& aData)
{
mData = aData;
}
NS_IMETHOD Run() override
{
mObj->AccumulateTelemetry(mData);
return NS_OK;
}
private:
mscom::STAUniquePtr<LazyInstantiator> mObj;
nsString mData;
};
friend class AccumulateRunnable;
void AppendVersionInfo(nsIFile* aClientExe, nsAString& aStrToAppend);
void GatherTelemetry(nsIFile* aClientExe);
void GatherTelemetry(nsIFile* aClientExe, AccumulateRunnable* aRunnable);
void AccumulateTelemetry(const nsString& aValue);
#endif // defined(MOZ_TELEMETRY_REPORTING)