author Gerald Squelart <>
Wed, 15 Jul 2015 14:39:00 -0400
changeset 273938 407e4f2de1a3908103cf4fbac83945c4a953b81b
parent 273789 93c229409478347feeb3ee2a7607c5c676deb90f
child 273940 603c57935f3d5124faa9f88cc19d909a7925e69d
permissions -rw-r--r--
Bug 1183433 - Remove G-M-PServiceParent::AbortAsyncShutdown declaration, as it's not defined nor used anywhere. r=cpearce, a=lmandel

/* -*- 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 */

#ifndef GMPServiceParent_h_
#define GMPServiceParent_h_

#include "GMPService.h"
#include "mozilla/gmp/PGMPServiceParent.h"
#include "mozIGeckoMediaPluginChromeService.h"
#include "nsClassHashtable.h"
#include "nsDataHashtable.h"
#include "mozilla/Atomics.h"
#include "nsThreadUtils.h"

template <class> struct already_AddRefed;

namespace mozilla {
namespace gmp {

class GMPParent;


class GeckoMediaPluginServiceParent final : public GeckoMediaPluginService
                                          , public mozIGeckoMediaPluginChromeService
  static already_AddRefed<GeckoMediaPluginServiceParent> GetSingleton();

  virtual nsresult Init() override;


  // mozIGeckoMediaPluginService
  NS_IMETHOD GetPluginVersionForAPI(const nsACString& aAPI,
                                    nsTArray<nsCString>* aTags,
                                    bool* aHasPlugin,
                                    nsACString& aOutVersion) override;
  NS_IMETHOD GetNodeId(const nsAString& aOrigin,
                       const nsAString& aTopLevelOrigin,
                       bool aInPrivateBrowsingMode,
                       UniquePtr<GetNodeIdCallback>&& aCallback) override;


  void AsyncShutdownNeeded(GMPParent* aParent);
  void AsyncShutdownComplete(GMPParent* aParent);

  int32_t AsyncShutdownTimeoutMs();

  friend class GMPServiceParent;

  virtual ~GeckoMediaPluginServiceParent();

  void ClearStorage();

  GMPParent* SelectPluginForAPI(const nsACString& aNodeId,
                                const nsCString& aAPI,
                                const nsTArray<nsCString>& aTags);
  GMPParent* FindPluginForAPIFrom(size_t aSearchStartIndex,
                                  const nsCString& aAPI,
                                  const nsTArray<nsCString>& aTags,
                                  size_t* aOutPluginIndex);

  nsresult GetNodeId(const nsAString& aOrigin, const nsAString& aTopLevelOrigin,
                     bool aInPrivateBrowsing, nsACString& aOutId);

  void UnloadPlugins();
  void CrashPlugins();
  void NotifySyncShutdownComplete();
  void NotifyAsyncShutdownComplete();

  void LoadFromEnvironment();
  void ProcessPossiblePlugin(nsIFile* aDir);

  void AddOnGMPThread(const nsAString& aDirectory);
  void RemoveOnGMPThread(const nsAString& aDirectory,
                         const bool aDeleteFromDisk,
                         const bool aCanDefer);

  nsresult SetAsyncShutdownTimeout();

  struct DirectoryFilter {
    virtual bool operator()(nsIFile* aPath) = 0;
    ~DirectoryFilter() {}
  void ClearNodeIdAndPlugin(DirectoryFilter& aFilter);

  void ForgetThisSiteOnGMPThread(const nsACString& aOrigin);
  void ClearRecentHistoryOnGMPThread(PRTime aSince);

  friend class GMPParent;
  void ReAddOnGMPThread(const nsRefPtr<GMPParent>& aOld);
  void PluginTerminated(const nsRefPtr<GMPParent>& aOld);
  virtual void InitializePlugins() override;
  virtual bool GetContentParentFrom(const nsACString& aNodeId,
                                    const nsCString& aAPI,
                                    const nsTArray<nsCString>& aTags,
                                    UniquePtr<GetGMPContentParentCallback>&& aCallback)
  GMPParent* ClonePlugin(const GMPParent* aOriginal);
  nsresult EnsurePluginsOnDiskScanned();
  nsresult InitStorage();

  class PathRunnable : public nsRunnable
    enum EOperation {

    PathRunnable(GeckoMediaPluginServiceParent* aService, const nsAString& aPath,
                 EOperation aOperation, bool aDefer = false)
      : mService(aService)
      , mPath(aPath)
      , mOperation(aOperation)
      , mDefer(aDefer)
    { }


    nsRefPtr<GeckoMediaPluginServiceParent> mService;
    nsString mPath;
    EOperation mOperation;
    bool mDefer;

  // Protected by mMutex from the base class.
  nsTArray<nsRefPtr<GMPParent>> mPlugins;
  bool mShuttingDown;
  nsTArray<nsRefPtr<GMPParent>> mAsyncShutdownPlugins;

  // True if we've inspected MOZ_GMP_PATH on the GMP thread and loaded any
  // plugins found there into mPlugins.
  Atomic<bool> mScannedPluginOnDisk;

  template<typename T>
  class MainThreadOnly {
    MOZ_IMPLICIT MainThreadOnly(T aValue)
      : mValue(aValue)
    operator T&() {
      return mValue;

    T mValue;

  MainThreadOnly<bool> mWaitingForPluginsSyncShutdown;

  nsTArray<nsString> mPluginsWaitingForDeletion;

  nsCOMPtr<nsIFile> mStorageBaseDir;

  // Hashes of (origin,topLevelOrigin) to the node id for
  // non-persistent sessions.
  nsClassHashtable<nsUint32HashKey, nsCString> mTempNodeIds;

  // Hashes node id to whether that node id is allowed to store data
  // persistently on disk.
  nsDataHashtable<nsCStringHashKey, bool> mPersistentStorageAllowed;

nsresult ReadSalt(nsIFile* aPath, nsACString& aOutData);
bool MatchOrigin(nsIFile* aPath, const nsACString& aSite);

class GMPServiceParent final : public PGMPServiceParent
  explicit GMPServiceParent(GeckoMediaPluginServiceParent* aService)
    : mService(aService)
  virtual ~GMPServiceParent();

  virtual bool RecvLoadGMP(const nsCString& aNodeId,
                           const nsCString& aApi,
                           nsTArray<nsCString>&& aTags,
                           nsTArray<ProcessId>&& aAlreadyBridgedTo,
                           base::ProcessId* aID,
                           nsCString* aDisplayName,
                           uint32_t* aPluginId) override;
  virtual bool RecvGetGMPNodeId(const nsString& aOrigin,
                                const nsString& aTopLevelOrigin,
                                const bool& aInPrivateBrowsing,
                                nsCString* aID) override;
  static bool RecvGetGMPPluginVersionForAPI(const nsCString& aAPI,
                                            nsTArray<nsCString>&& aTags,
                                            bool* aHasPlugin,
                                            nsCString* aVersion);

  virtual void ActorDestroy(ActorDestroyReason aWhy) override;

  static PGMPServiceParent* Create(Transport* aTransport, ProcessId aOtherPid);

  nsRefPtr<GeckoMediaPluginServiceParent> mService;

} // namespace gmp
} // namespace mozilla

#endif // GMPServiceParent_h_