tor-browser

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

DecryptJob.cpp (1691B)


      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 #include "DecryptJob.h"
      7 
      8 #include "mozilla/Atomics.h"
      9 
     10 namespace mozilla {
     11 
     12 static Atomic<uint32_t> sDecryptJobInstanceCount(0u);
     13 
     14 DecryptJob::DecryptJob(MediaRawData* aSample)
     15    : mId(++sDecryptJobInstanceCount), mSample(aSample) {}
     16 
     17 RefPtr<DecryptPromise> DecryptJob::Ensure() {
     18  return mPromise.Ensure(__func__);
     19 }
     20 
     21 void DecryptJob::PostResult(DecryptStatus aResult) {
     22  nsTArray<uint8_t> empty;
     23  PostResult(aResult, empty);
     24 }
     25 
     26 void DecryptJob::PostResult(DecryptStatus aResult,
     27                            Span<const uint8_t> aDecryptedData) {
     28  if (aDecryptedData.Length() != mSample->Size()) {
     29    NS_WARNING("CDM returned incorrect number of decrypted bytes");
     30  }
     31  if (aResult == DecryptStatus::Ok) {
     32    UniquePtr<MediaRawDataWriter> writer(mSample->CreateWriter());
     33    if (NS_WARN_IF(!writer->Replace(aDecryptedData.Elements(),
     34                                    aDecryptedData.Length()))) {
     35      aResult = DecryptStatus::GenericErr;
     36    }
     37  } else if (aResult == DecryptStatus::NoKeyErr) {
     38    NS_WARNING("CDM returned NoKeyErr");
     39    // We still have the encrypted sample, so we can re-enqueue it to be
     40    // decrypted again once the key is usable again.
     41  } else {
     42    nsAutoCString str("CDM returned decode failure DecryptStatus=");
     43    str.AppendInt(aResult);
     44    NS_WARNING(str.get());
     45  }
     46  mPromise.Resolve(DecryptResult(aResult, mSample), __func__);
     47 }
     48 
     49 }  // namespace mozilla