1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
2 /* This Source Code Form is subject to the terms of the Mozilla Public
3  * License, v. 2.0. If a copy of the MPL was not distributed with this
4  * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef GMPContentParent_h_
7 #define GMPContentParent_h_
8 
9 #include "mozilla/gmp/PGMPContentParent.h"
10 #include "GMPSharedMemManager.h"
11 #include "nsISupportsImpl.h"
12 
13 namespace mozilla {
14 namespace gmp {
15 
16 class GMPParent;
17 class GMPVideoDecoderParent;
18 class GMPVideoEncoderParent;
19 class ChromiumCDMParent;
20 
21 class GMPContentParent final : public PGMPContentParent, public GMPSharedMem {
22   friend class PGMPContentParent;
23 
24  public:
25   // Mark AddRef and Release as `final`, as they overload pure virtual
26   // implementations in PGMPContentParent.
27   NS_INLINE_DECL_THREADSAFE_REFCOUNTING(GMPContentParent, final)
28 
29   explicit GMPContentParent(GMPParent* aParent = nullptr);
30 
31   nsresult GetGMPVideoDecoder(GMPVideoDecoderParent** aGMPVD,
32                               uint32_t aDecryptorId);
33   void VideoDecoderDestroyed(GMPVideoDecoderParent* aDecoder);
34 
35   nsresult GetGMPVideoEncoder(GMPVideoEncoderParent** aGMPVE);
36   void VideoEncoderDestroyed(GMPVideoEncoderParent* aEncoder);
37 
38   already_AddRefed<ChromiumCDMParent> GetChromiumCDM();
39   void ChromiumCDMDestroyed(ChromiumCDMParent* aCDM);
40 
41   nsCOMPtr<nsISerialEventTarget> GMPEventTarget();
42 
43   // GMPSharedMem
44   void CheckThread() override;
45 
SetDisplayName(const nsCString & aDisplayName)46   void SetDisplayName(const nsCString& aDisplayName) {
47     mDisplayName = aDisplayName;
48   }
GetDisplayName()49   const nsCString& GetDisplayName() { return mDisplayName; }
SetPluginId(const uint32_t aPluginId)50   void SetPluginId(const uint32_t aPluginId) { mPluginId = aPluginId; }
GetPluginId()51   uint32_t GetPluginId() const { return mPluginId; }
52 
53   class CloseBlocker {
54    public:
NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CloseBlocker)55     NS_INLINE_DECL_THREADSAFE_REFCOUNTING(CloseBlocker)
56 
57     explicit CloseBlocker(GMPContentParent* aParent) : mParent(aParent) {
58       mParent->AddCloseBlocker();
59     }
60     RefPtr<GMPContentParent> mParent;
61 
62    private:
~CloseBlocker()63     ~CloseBlocker() { mParent->RemoveCloseBlocker(); }
64   };
65 
66  private:
67   void AddCloseBlocker();
68   void RemoveCloseBlocker();
69 
70   ~GMPContentParent();
71 
72   void ActorDestroy(ActorDestroyReason aWhy) override;
73 
74   void CloseIfUnused();
75   // Needed because NewRunnableMethod tried to use the class that the method
76   // lives on to store the receiver, but PGMPContentParent isn't refcounted.
Close()77   void Close() { PGMPContentParent::Close(); }
78 
79   nsTArray<RefPtr<GMPVideoDecoderParent>> mVideoDecoders;
80   nsTArray<RefPtr<GMPVideoEncoderParent>> mVideoEncoders;
81   nsTArray<RefPtr<ChromiumCDMParent>> mChromiumCDMs;
82   nsCOMPtr<nsISerialEventTarget> mGMPEventTarget;
83   RefPtr<GMPParent> mParent;
84   nsCString mDisplayName;
85   uint32_t mPluginId;
86   uint32_t mCloseBlockerCount = 0;
87 };
88 
89 }  // namespace gmp
90 }  // namespace mozilla
91 
92 #endif  // GMPParent_h_
93