tor-browser

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

AppleUtils.cpp (2561B)


      1 /* This Source Code Form is subject to the terms of the Mozilla Public
      2 * License, v. 2.0. If a copy of the MPL was not distributed with this
      3 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      4 
      5 #include "apple/AppleUtils.h"
      6 
      7 #include <VideoToolbox/VideoToolbox.h>
      8 
      9 namespace mozilla {
     10 
     11 SessionPropertyManager::SessionPropertyManager(
     12    const AutoCFTypeRef<VTCompressionSessionRef>& aSession)
     13    : mSession(aSession) {
     14  MOZ_ASSERT(mSession, "Session must be valid");
     15 }
     16 
     17 SessionPropertyManager::SessionPropertyManager(
     18    const VTCompressionSessionRef& aSession)
     19    : mSession(aSession, AutoTypePolicy::Retain) {
     20  MOZ_ASSERT(mSession, "Session must be valid");
     21 }
     22 
     23 bool SessionPropertyManager::IsSupported(CFStringRef aKey) {
     24  MOZ_ASSERT(mSession);
     25  if (!mSupportedKeys) {
     26    CFDictionaryRef dict;
     27    if (VTSessionCopySupportedPropertyDictionary(mSession.Ref(), &dict) ==
     28        noErr) {
     29      mSupportedKeys.Reset(dict, AutoTypePolicy::NoRetain);
     30    }
     31  }
     32  return mSupportedKeys && CFDictionaryContainsKey(mSupportedKeys.Ref(), aKey);
     33 }
     34 
     35 OSStatus SessionPropertyManager::Set(CFStringRef aKey, int32_t aValue) {
     36  return Set(aKey, aValue, kCFNumberSInt32Type);
     37 }
     38 
     39 OSStatus SessionPropertyManager::Set(CFStringRef aKey, int64_t aValue) {
     40  return Set(aKey, aValue, kCFNumberSInt64Type);
     41 }
     42 
     43 OSStatus SessionPropertyManager::Set(CFStringRef aKey, float aValue) {
     44  return Set(aKey, aValue, kCFNumberFloatType);
     45 }
     46 
     47 OSStatus SessionPropertyManager::Set(CFStringRef aKey, bool aValue) {
     48  MOZ_ASSERT(mSession);
     49  CFBooleanRef value = aValue ? kCFBooleanTrue : kCFBooleanFalse;
     50  return VTSessionSetProperty(mSession.Ref(), aKey, value);
     51 }
     52 
     53 OSStatus SessionPropertyManager::Set(CFStringRef aKey, CFStringRef value) {
     54  MOZ_ASSERT(mSession);
     55  return VTSessionSetProperty(mSession.Ref(), aKey, value);
     56 }
     57 
     58 OSStatus SessionPropertyManager::Copy(CFStringRef aKey, bool& aValue) {
     59  MOZ_ASSERT(mSession);
     60 
     61  AutoCFTypeRef<CFBooleanRef> value;
     62  OSStatus rv = VTSessionCopyProperty(mSession.Ref(), aKey, kCFAllocatorDefault,
     63                                      value.Receive());
     64  if (rv == noErr) {
     65    aValue = value == kCFBooleanTrue;
     66  }
     67  return rv;
     68 }
     69 
     70 template <typename V>
     71 OSStatus SessionPropertyManager::Set(CFStringRef aKey, V aValue,
     72                                     CFNumberType aType) {
     73  MOZ_ASSERT(mSession);
     74  AutoCFTypeRef<CFNumberRef> value(
     75      CFNumberCreate(kCFAllocatorDefault, aType, &aValue),
     76      AutoTypePolicy::NoRetain);
     77  return VTSessionSetProperty(mSession.Ref(), aKey, value.Ref());
     78 }
     79 
     80 }  // namespace mozilla