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