tor-browser

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

MediaKeyStatusMap.h (2907B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      3 /* This Source Code Form is subject to the terms of the Mozilla Public
      4 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #ifndef mozilla_dom_MediaKeyStatuses_h
      8 #define mozilla_dom_MediaKeyStatuses_h
      9 
     10 #include "mozilla/CDMCaps.h"
     11 #include "mozilla/dom/BufferSourceBindingFwd.h"
     12 #include "mozilla/dom/MediaKeyStatusMapBinding.h"
     13 #include "mozilla/dom/TypedArray.h"
     14 #include "nsCycleCollectionParticipant.h"
     15 #include "nsWrapperCache.h"
     16 
     17 class nsPIDOMWindowInner;
     18 
     19 namespace mozilla {
     20 class ErrorResult;
     21 
     22 namespace dom {
     23 
     24 // The MediaKeyStatusMap WebIDL interface; maps a keyId to its status.
     25 // Note that the underlying "map" is stored in an array, since we assume
     26 // that a MediaKeySession won't have many key statuses to report.
     27 class MediaKeyStatusMap final : public nsISupports, public nsWrapperCache {
     28 public:
     29  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
     30  NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(MediaKeyStatusMap)
     31 
     32 public:
     33  explicit MediaKeyStatusMap(nsPIDOMWindowInner* aParent);
     34 
     35 protected:
     36  ~MediaKeyStatusMap();
     37 
     38 public:
     39  nsPIDOMWindowInner* GetParentObject() const;
     40 
     41  JSObject* WrapObject(JSContext* aCx,
     42                       JS::Handle<JSObject*> aGivenProto) override;
     43 
     44  void Get(const BufferSource& aKey, OwningMediaKeyStatusOrUndefined& aOutValue,
     45           ErrorResult& aOutRv) const;
     46  bool Has(const BufferSource& aKey) const;
     47  uint32_t Size() const;
     48 
     49  uint32_t GetIterableLength() const;
     50  TypedArrayCreator<ArrayBuffer> GetKeyAtIndex(uint32_t aIndex) const;
     51  nsString GetKeyIDAsHexString(uint32_t aIndex) const;
     52  MediaKeyStatus GetValueAtIndex(uint32_t aIndex) const;
     53 
     54  void Update(const nsTArray<CDMCaps::KeyStatus>& keys);
     55 
     56 private:
     57  nsCOMPtr<nsPIDOMWindowInner> mParent;
     58 
     59  struct KeyStatus {
     60    KeyStatus(const nsTArray<uint8_t>& aKeyId, MediaKeyStatus aStatus)
     61        : mKeyId(aKeyId.Clone()), mStatus(aStatus) {}
     62    bool operator==(const KeyStatus& aOther) const {
     63      return aOther.mKeyId == mKeyId;
     64    }
     65    bool operator<(const KeyStatus& aOther) const {
     66      // Copy chromium and compare keys' bytes.
     67      // Update once https://github.com/w3c/encrypted-media/issues/69
     68      // is resolved.
     69      const nsTArray<uint8_t>& other = aOther.mKeyId;
     70      const nsTArray<uint8_t>& self = mKeyId;
     71      size_t length = std::min<size_t>(other.Length(), self.Length());
     72      int cmp = memcmp(self.Elements(), other.Elements(), length);
     73      if (cmp != 0) {
     74        return cmp < 0;
     75      }
     76      return self.Length() <= other.Length();
     77    }
     78    nsTArray<uint8_t> mKeyId;
     79    MediaKeyStatus mStatus;
     80  };
     81 
     82  const KeyStatus* FindKey(const BufferSource& aKey) const;
     83 
     84  nsTArray<KeyStatus> mStatuses;
     85 };
     86 
     87 }  // namespace dom
     88 }  // namespace mozilla
     89 
     90 #endif