summaryrefslogtreecommitdiffstats
path: root/dom/quota/DecryptingInputStream.cpp
blob: 939b1e3ad7597d59722760b508db2a57d64904a2 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this file,
 * You can obtain one at http://mozilla.org/MPL/2.0/. */

#include "DecryptingInputStream.h"
#include "DecryptingInputStream_impl.h"

#include "nsStreamUtils.h"

namespace mozilla::dom::quota {

NS_IMPL_ADDREF(DecryptingInputStreamBase);
NS_IMPL_RELEASE(DecryptingInputStreamBase);

NS_INTERFACE_MAP_BEGIN(DecryptingInputStreamBase)
  NS_INTERFACE_MAP_ENTRY(nsIInputStream)
  NS_INTERFACE_MAP_ENTRY(nsISeekableStream)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(nsICloneableInputStream,
                                     mBaseCloneableInputStream || !mBaseStream)
  NS_INTERFACE_MAP_ENTRY_CONDITIONAL(
      nsIIPCSerializableInputStream,
      mBaseIPCSerializableInputStream || !mBaseStream)
  NS_INTERFACE_MAP_ENTRY_AMBIGUOUS(nsISupports, nsIInputStream)
NS_INTERFACE_MAP_END

DecryptingInputStreamBase::DecryptingInputStreamBase(
    MovingNotNull<nsCOMPtr<nsIInputStream>> aBaseStream, size_t aBlockSize) {
  Init(std::move(aBaseStream), aBlockSize);
}

void DecryptingInputStreamBase::Init(
    MovingNotNull<nsCOMPtr<nsIInputStream>> aBaseStream, size_t aBlockSize) {
  mBlockSize.init(aBlockSize);
  mBaseStream.init(std::move(aBaseStream));

  const nsCOMPtr<nsISeekableStream> seekableStream =
      do_QueryInterface(mBaseStream->get());
  MOZ_ASSERT(seekableStream &&
             SameCOMIdentity(mBaseStream->get(), seekableStream));
  mBaseSeekableStream.init(WrapNotNullUnchecked(seekableStream));

  const nsCOMPtr<nsICloneableInputStream> cloneableInputStream =
      do_QueryInterface(mBaseStream->get());
  if (cloneableInputStream &&
      SameCOMIdentity(mBaseStream->get(), cloneableInputStream)) {
    mBaseCloneableInputStream.init(WrapNotNullUnchecked(cloneableInputStream));
  }

  const nsCOMPtr<nsIIPCSerializableInputStream> ipcSerializeInputStream =
      do_QueryInterface(mBaseStream->get());
  if (ipcSerializeInputStream &&
      SameCOMIdentity(mBaseStream->get(), ipcSerializeInputStream)) {
    mBaseIPCSerializableInputStream.init(
        WrapNotNullUnchecked(ipcSerializeInputStream));
  }
}

NS_IMETHODIMP DecryptingInputStreamBase::Read(char* aBuf, uint32_t aCount,
                                              uint32_t* aBytesReadOut) {
  return ReadSegments(NS_CopySegmentToBuffer, aBuf, aCount, aBytesReadOut);
}

NS_IMETHODIMP DecryptingInputStreamBase::IsNonBlocking(bool* aNonBlockingOut) {
  *aNonBlockingOut = false;
  return NS_OK;
}

NS_IMETHODIMP DecryptingInputStreamBase::SetEOF() {
  // Cannot truncate a read-only stream.

  return NS_ERROR_NOT_IMPLEMENTED;
}

NS_IMETHODIMP DecryptingInputStreamBase::GetCloneable(bool* aCloneable) {
  *aCloneable = true;
  return NS_OK;
}

void DecryptingInputStreamBase::SerializedComplexity(uint32_t aMaxSize,
                                                     uint32_t* aSizeUsed,
                                                     uint32_t* aPipes,
                                                     uint32_t* aTransferables) {
  (*mBaseIPCSerializableInputStream)
      ->SerializedComplexity(aMaxSize, aSizeUsed, aPipes, aTransferables);
}

size_t DecryptingInputStreamBase::PlainLength() const {
  MOZ_ASSERT(mNextByte <= mPlainBytes);
  return mPlainBytes - mNextByte;
}

size_t DecryptingInputStreamBase::EncryptedBufferLength() const {
  return *mBlockSize;
}

}  // namespace mozilla::dom::quota