tor-browser

The Tor Browser
git clone https://git.dasho.dev/tor-browser.git
Log | Files | Refs | README | LICENSE

PrincipalHandle.h (1945B)


      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 * The level of privacy of a principal as considered by RTCPeerConnection.
     15 */
     16 enum class PrincipalPrivacy : uint8_t { Private, NonPrivate };
     17 
     18 /**
     19 * We pass the principal through the MediaTrackGraph by wrapping it in a thread
     20 * safe nsMainThreadPtrHandle, since it cannot be used directly off the main
     21 * thread. We can compare two PrincipalHandles to each other on any thread, but
     22 * they can only be created and converted back to nsIPrincipal* on main thread.
     23 */
     24 typedef nsMainThreadPtrHandle<nsIPrincipal> PrincipalHandle;
     25 
     26 inline PrincipalHandle MakePrincipalHandle(nsIPrincipal* aPrincipal) {
     27  RefPtr<nsMainThreadPtrHolder<nsIPrincipal>> holder =
     28      new nsMainThreadPtrHolder<nsIPrincipal>(
     29          "MakePrincipalHandle::nsIPrincipal", aPrincipal);
     30  return PrincipalHandle(holder);
     31 }
     32 
     33 #define PRINCIPAL_HANDLE_NONE nullptr
     34 
     35 inline nsIPrincipal* GetPrincipalFromHandle(
     36    const PrincipalHandle& aPrincipalHandle) {
     37  MOZ_ASSERT(NS_IsMainThread());
     38  return aPrincipalHandle.get();
     39 }
     40 
     41 inline bool PrincipalHandleMatches(const PrincipalHandle& aPrincipalHandle,
     42                                   nsIPrincipal* aOther) {
     43  if (!aOther) {
     44    return false;
     45  }
     46 
     47  nsIPrincipal* principal = GetPrincipalFromHandle(aPrincipalHandle);
     48  if (!principal) {
     49    return false;
     50  }
     51 
     52  bool result;
     53  if (NS_FAILED(principal->Equals(aOther, &result))) {
     54    NS_ERROR("Principal check failed");
     55    return false;
     56  }
     57 
     58  return result;
     59 }
     60 }  // namespace mozilla
     61 
     62 #endif  // DOM_MEDIA_PRINCIPALHANDLE_H_