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 GMPService_h_
7 #define GMPService_h_
8 
9 #include "GMPContentParent.h"
10 #include "GMPCrashHelper.h"
11 #include "mozIGeckoMediaPluginService.h"
12 #include "mozilla/Atomics.h"
13 #include "mozilla/gmp/GMPTypes.h"
14 #include "mozilla/MozPromise.h"
15 #include "nsCOMPtr.h"
16 #include "nsClassHashtable.h"
17 #include "nsIObserver.h"
18 #include "nsString.h"
19 #include "nsTArray.h"
20 
21 class nsIAsyncShutdownClient;
22 class nsIRunnable;
23 class nsISerialEventTarget;
24 class nsIThread;
25 
26 template <class>
27 struct already_AddRefed;
28 
29 namespace mozilla {
30 
31 class GMPCrashHelper;
32 class MediaResult;
33 
34 extern LogModule* GetGMPLog();
35 
36 namespace gmp {
37 
38 typedef MozPromise<RefPtr<GMPContentParent::CloseBlocker>, MediaResult,
39                    /* IsExclusive = */ true>
40     GetGMPContentParentPromise;
41 typedef MozPromise<RefPtr<ChromiumCDMParent>, MediaResult,
42                    /* IsExclusive = */ true>
43     GetCDMParentPromise;
44 
45 class GeckoMediaPluginService : public mozIGeckoMediaPluginService,
46                                 public nsIObserver {
47  public:
48   static already_AddRefed<GeckoMediaPluginService> GetGeckoMediaPluginService();
49 
50   virtual nsresult Init();
51 
52   NS_DECL_THREADSAFE_ISUPPORTS
53 
54   RefPtr<GetCDMParentPromise> GetCDM(const NodeIdParts& aNodeIdParts,
55                                      nsTArray<nsCString> aTags,
56                                      GMPCrashHelper* aHelper);
57 
58 #if defined(MOZ_SANDBOX) && defined(MOZ_DEBUG) && defined(ENABLE_TESTS)
59   RefPtr<GetGMPContentParentPromise> GetContentParentForTest();
60 #endif
61 
62   // mozIGeckoMediaPluginService
63   NS_IMETHOD GetThread(nsIThread** aThread) override;
64   NS_IMETHOD GetDecryptingGMPVideoDecoder(
65       GMPCrashHelper* aHelper, nsTArray<nsCString>* aTags,
66       const nsACString& aNodeId,
67       UniquePtr<GetGMPVideoDecoderCallback>&& aCallback,
68       uint32_t aDecryptorId) override;
69   NS_IMETHOD GetGMPVideoEncoder(
70       GMPCrashHelper* aHelper, nsTArray<nsCString>* aTags,
71       const nsACString& aNodeId,
72       UniquePtr<GetGMPVideoEncoderCallback>&& aCallback) override;
73 
74   // Helper for backwards compatibility with WebRTC/tests.
75   NS_IMETHOD
GetGMPVideoDecoder(GMPCrashHelper * aHelper,nsTArray<nsCString> * aTags,const nsACString & aNodeId,UniquePtr<GetGMPVideoDecoderCallback> && aCallback)76   GetGMPVideoDecoder(
77       GMPCrashHelper* aHelper, nsTArray<nsCString>* aTags,
78       const nsACString& aNodeId,
79       UniquePtr<GetGMPVideoDecoderCallback>&& aCallback) override {
80     return GetDecryptingGMPVideoDecoder(aHelper, aTags, aNodeId,
81                                         std::move(aCallback), 0);
82   }
83 
84   NS_IMETHOD RunPluginCrashCallbacks(uint32_t aPluginId,
85                                      const nsACString& aPluginName) override;
86 
87   already_AddRefed<nsISerialEventTarget> GetGMPThread();
88 
89   void ConnectCrashHelper(uint32_t aPluginId, GMPCrashHelper* aHelper);
90   void DisconnectCrashHelper(GMPCrashHelper* aHelper);
91 
XPCOMWillShutdownReceived()92   bool XPCOMWillShutdownReceived() const { return mXPCOMWillShutdown; }
93 
94  protected:
95   GeckoMediaPluginService();
96   virtual ~GeckoMediaPluginService();
97 
98   virtual void InitializePlugins(nsISerialEventTarget* aGMPThread) = 0;
99 
100   virtual RefPtr<GetGMPContentParentPromise> GetContentParent(
101       GMPCrashHelper* aHelper, const NodeIdVariant& aNodeIdVariant,
102       const nsCString& aAPI, const nsTArray<nsCString>& aTags) = 0;
103 
104   nsresult GMPDispatch(nsIRunnable* event, uint32_t flags = NS_DISPATCH_NORMAL);
105   nsresult GMPDispatch(already_AddRefed<nsIRunnable> event,
106                        uint32_t flags = NS_DISPATCH_NORMAL);
107   void ShutdownGMPThread();
108 
109   static nsCOMPtr<nsIAsyncShutdownClient> GetShutdownBarrier();
110 
111   Mutex mMutex;  // Protects mGMPThread, mPluginCrashHelpers,
112                  // mGMPThreadShutdown and some members in derived classes.
113 
114   const nsCOMPtr<nsISerialEventTarget> mMainThread;
115 
116   nsCOMPtr<nsIThread> mGMPThread;
117   bool mGMPThreadShutdown;
118   bool mShuttingDownOnGMPThread;
119   Atomic<bool> mXPCOMWillShutdown;
120 
121   nsClassHashtable<nsUint32HashKey, nsTArray<RefPtr<GMPCrashHelper>>>
122       mPluginCrashHelpers;
123 };
124 
125 }  // namespace gmp
126 }  // namespace mozilla
127 
128 #endif  // GMPService_h_
129