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 file,
4  * You can obtain one at http://mozilla.org/MPL/2.0/. */
5 
6 #ifndef DOM_MEDIA_PRINCIPALHANDLE_H_
7 #define DOM_MEDIA_PRINCIPALHANDLE_H_
8 
9 #include "nsIPrincipal.h"
10 #include "nsProxyRelease.h"
11 
12 namespace mozilla {
13 /**
14  * We pass the principal through the MediaTrackGraph by wrapping it in a thread
15  * safe nsMainThreadPtrHandle, since it cannot be used directly off the main
16  * thread. We can compare two PrincipalHandles to each other on any thread, but
17  * they can only be created and converted back to nsIPrincipal* on main thread.
18  */
19 typedef nsMainThreadPtrHandle<nsIPrincipal> PrincipalHandle;
20 
MakePrincipalHandle(nsIPrincipal * aPrincipal)21 inline PrincipalHandle MakePrincipalHandle(nsIPrincipal* aPrincipal) {
22   RefPtr<nsMainThreadPtrHolder<nsIPrincipal>> holder =
23       new nsMainThreadPtrHolder<nsIPrincipal>(
24           "MakePrincipalHandle::nsIPrincipal", aPrincipal);
25   return PrincipalHandle(holder);
26 }
27 
28 #define PRINCIPAL_HANDLE_NONE nullptr
29 
GetPrincipalFromHandle(const PrincipalHandle & aPrincipalHandle)30 inline nsIPrincipal* GetPrincipalFromHandle(
31     const PrincipalHandle& aPrincipalHandle) {
32   MOZ_ASSERT(NS_IsMainThread());
33   return aPrincipalHandle.get();
34 }
35 
PrincipalHandleMatches(const PrincipalHandle & aPrincipalHandle,nsIPrincipal * aOther)36 inline bool PrincipalHandleMatches(const PrincipalHandle& aPrincipalHandle,
37                                    nsIPrincipal* aOther) {
38   if (!aOther) {
39     return false;
40   }
41 
42   nsIPrincipal* principal = GetPrincipalFromHandle(aPrincipalHandle);
43   if (!principal) {
44     return false;
45   }
46 
47   bool result;
48   if (NS_FAILED(principal->Equals(aOther, &result))) {
49     NS_ERROR("Principal check failed");
50     return false;
51   }
52 
53   return result;
54 }
55 }  // namespace mozilla
56 
57 #endif  // DOM_MEDIA_PRINCIPALHANDLE_H_
58