gecko-dev/dom/media/gmp/GMPParent.h
Chris Pearce 0901ad8256 Bug 1315850 - Ask the GMPService for the GMP thread in GMPParent::ChildTerminated. r=gerald
When we shutdown the browser while the GMPService is active we can end up
leaking a GMPParent, GeckoMediaPluginServiceParent, and a Runnable. I tracked
this down to the runnable dispatched to the GMP thread in
GMPParent::ChildTerminated(). The dispatch of this runnable is failing as we
are dispatching the runnable to a reference of the GMP thread which we have
previously acquired, but that thread is now shutdown. So the dispatch fails,
and if you look in nsThread::DispatchInternal() you'll see that we deliberately
leak the runnable if dispatch fails! The runnable leaking means that the
references it holds to the GMPParent and the GMP service parent leak.

The solution in this patch is to not cache a reference to the GMP thread on the
GMPParent; instead we re-request the GMP thread from the GMPService when we
want it. This means that in the case where the browser is shutting down,
GMPParent::GMPThread() will return null, and we'll not leak the runnable. We'll
then follow the (hacky) shutdown path added in bug 1163239.

We also need to change GMPParent::GMPThread() and GMPContentParent::GMPThread()
to return a reference to the GMP thread with a refcount held on it, in order
to ensure we don't race with the GMP service shutting down the GMP thread
while we're trying to dispatch to in on shutdown.

MozReview-Commit-ID: CXv9VZqTRzY

--HG--
extra : rebase_source : e507e48ee633cad8911287fb7296bbb1679a7bcb
2017-03-24 13:38:00 +13:00

238 lines
7.3 KiB
C++

/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
#ifndef GMPParent_h_
#define GMPParent_h_
#include "GMPProcessParent.h"
#include "GMPServiceParent.h"
#include "GMPDecryptorParent.h"
#include "GMPVideoDecoderParent.h"
#include "GMPVideoEncoderParent.h"
#include "GMPTimerParent.h"
#include "GMPStorageParent.h"
#include "mozilla/gmp/PGMPParent.h"
#include "nsCOMPtr.h"
#include "nscore.h"
#include "nsISupports.h"
#include "nsString.h"
#include "nsTArray.h"
#include "nsIFile.h"
#include "mozilla/MozPromise.h"
class nsIThread;
#ifdef MOZ_CRASHREPORTER
#include "nsExceptionHandler.h"
#endif
namespace mozilla {
namespace ipc {
class CrashReporterHost;
} // namespace ipc
namespace gmp {
class GMPCapability
{
public:
explicit GMPCapability() {}
GMPCapability(GMPCapability&& aOther)
: mAPIName(Move(aOther.mAPIName))
, mAPITags(Move(aOther.mAPITags))
{
}
explicit GMPCapability(const nsCString& aAPIName)
: mAPIName(aAPIName)
{}
explicit GMPCapability(const GMPCapability& aOther) = default;
nsCString mAPIName;
nsTArray<nsCString> mAPITags;
static bool Supports(const nsTArray<GMPCapability>& aCapabilities,
const nsCString& aAPI,
const nsTArray<nsCString>& aTags);
static bool Supports(const nsTArray<GMPCapability>& aCapabilities,
const nsCString& aAPI,
const nsCString& aTag);
};
enum GMPState {
GMPStateNotLoaded,
GMPStateLoaded,
GMPStateUnloading,
GMPStateClosing
};
class GMPContentParent;
class GMPParent final : public PGMPParent
{
public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPParent)
explicit GMPParent(AbstractThread* aMainThread);
RefPtr<GenericPromise> Init(GeckoMediaPluginServiceParent* aService, nsIFile* aPluginDir);
nsresult CloneFrom(const GMPParent* aOther);
void Crash();
nsresult LoadProcess();
// Called internally to close this if we don't need it
void CloseIfUnused();
// Notify all active de/encoders that we are closing, either because of
// normal shutdown or unexpected shutdown/crash.
void CloseActive(bool aDieWhenUnloaded);
// Tell the plugin to die after shutdown.
void MarkForDeletion();
bool IsMarkedForDeletion();
// Called by the GMPService to forcibly close active de/encoders at shutdown
void Shutdown();
// This must not be called while we're in the middle of abnormal ActorDestroy
void DeleteProcess();
GMPState State() const;
nsCOMPtr<nsIThread> GMPThread();
// A GMP can either be a single instance shared across all NodeIds (like
// in the OpenH264 case), or we can require a new plugin instance for every
// NodeIds running the plugin (as in the EME plugin case).
//
// A NodeId is a hash of the ($urlBarOrigin, $ownerDocOrigin) pair.
//
// Plugins are associated with an NodeIds by calling SetNodeId() before
// loading.
//
// If a plugin has no NodeId specified and it is loaded, it is assumed to
// be shared across NodeIds.
// Specifies that a GMP can only work with the specified NodeIds.
void SetNodeId(const nsACString& aNodeId);
const nsACString& GetNodeId() const { return mNodeId; }
const nsCString& GetDisplayName() const;
const nsCString& GetVersion() const;
uint32_t GetPluginId() const;
nsString GetPluginBaseName() const;
// Returns true if a plugin can be or is being used across multiple NodeIds.
bool CanBeSharedCrossNodeIds() const;
// A GMP can be used from a NodeId if it's already been set to work with
// that NodeId, or if it's not been set to work with any NodeId and has
// not yet been loaded (i.e. it's not shared across NodeIds).
bool CanBeUsedFrom(const nsACString& aNodeId) const;
already_AddRefed<nsIFile> GetDirectory() {
return nsCOMPtr<nsIFile>(mDirectory).forget();
}
void AbortAsyncShutdown();
// Called when the child process has died.
void ChildTerminated();
bool OpenPGMPContent();
void GetGMPContentParent(UniquePtr<MozPromiseHolder<GetGMPContentParentPromise>>&& aPromiseHolder);
already_AddRefed<GMPContentParent> ForgetGMPContentParent();
bool EnsureProcessLoaded(base::ProcessId* aID);
void IncrementGMPContentChildCount();
const nsTArray<GMPCapability>& GetCapabilities() const { return mCapabilities; }
private:
~GMPParent();
RefPtr<GeckoMediaPluginServiceParent> mService;
bool EnsureProcessLoaded();
RefPtr<GenericPromise> ReadGMPMetaData();
RefPtr<GenericPromise> ReadGMPInfoFile(nsIFile* aFile);
RefPtr<GenericPromise> ParseChromiumManifest(const nsAString& aJSON); // Main thread.
RefPtr<GenericPromise> ReadChromiumManifestFile(nsIFile* aFile); // GMP thread.
#ifdef MOZ_CRASHREPORTER
void WriteExtraDataForMinidump();
bool GetCrashID(nsString& aResult);
#endif
void ActorDestroy(ActorDestroyReason aWhy) override;
mozilla::ipc::IPCResult RecvInitCrashReporter(Shmem&& shmem, const NativeThreadId& aThreadId) override;
mozilla::ipc::IPCResult RecvPGMPStorageConstructor(PGMPStorageParent* actor) override;
PGMPStorageParent* AllocPGMPStorageParent() override;
bool DeallocPGMPStorageParent(PGMPStorageParent* aActor) override;
mozilla::ipc::IPCResult RecvPGMPTimerConstructor(PGMPTimerParent* actor) override;
PGMPTimerParent* AllocPGMPTimerParent() override;
bool DeallocPGMPTimerParent(PGMPTimerParent* aActor) override;
mozilla::ipc::IPCResult RecvPGMPContentChildDestroyed() override;
bool IsUsed()
{
return mGMPContentChildCount > 0 ||
!mGetContentParentPromises.IsEmpty();
}
void ResolveGetContentParentPromises();
void RejectGetContentParentPromises();
GMPState mState;
nsCOMPtr<nsIFile> mDirectory; // plugin directory on disk
nsString mName; // base name of plugin on disk, UTF-16 because used for paths
nsCString mDisplayName; // name of plugin displayed to users
nsCString mDescription; // description of plugin for display to users
nsCString mVersion;
#ifdef XP_WIN
nsCString mLibs;
#endif
nsString mAdapter;
uint32_t mPluginId;
nsTArray<GMPCapability> mCapabilities;
GMPProcessParent* mProcess;
bool mDeleteProcessOnlyOnUnload;
bool mAbnormalShutdownInProgress;
bool mIsBlockingDeletion;
bool mCanDecrypt;
nsTArray<RefPtr<GMPTimerParent>> mTimers;
nsTArray<RefPtr<GMPStorageParent>> mStorage;
// NodeId the plugin is assigned to, or empty if the the plugin is not
// assigned to a NodeId.
nsCString mNodeId;
// This is used for GMP content in the parent, there may be more of these in
// the content processes.
RefPtr<GMPContentParent> mGMPContentParent;
nsTArray<UniquePtr<MozPromiseHolder<GetGMPContentParentPromise>>> mGetContentParentPromises;
uint32_t mGMPContentChildCount;
int mChildPid;
// We hold a self reference to ourself while the child process is alive.
// This ensures that if the GMPService tries to shut us down and drops
// its reference to us, we stay alive long enough for the child process
// to terminate gracefully.
bool mHoldingSelfRef;
#ifdef MOZ_CRASHREPORTER
UniquePtr<ipc::CrashReporterHost> mCrashReporter;
#endif
const RefPtr<AbstractThread> mMainThread;
};
} // namespace gmp
} // namespace mozilla
#endif // GMPParent_h_