Bug 1864123 - Optimize flushing to base stream; r=dom-storage-reviewers,asuth,hsingh

Differential Revision: https://phabricator.services.mozilla.com/D193286
This commit is contained in:
Jan Varga 2023-11-24 10:02:29 +00:00
parent d886bade84
commit d26ddce442
2 changed files with 25 additions and 0 deletions

View File

@ -24,6 +24,7 @@
#include "nscore.h"
class nsIInputStream;
class nsIRandomGenerator;
namespace mozilla::dom::quota {
class EncryptingOutputStreamBase : public nsIOutputStream {
@ -83,6 +84,8 @@ class EncryptingOutputStream final : public EncryptingOutputStreamBase {
// effective block size at a block boundary.
nsTArray<uint8_t> mBuffer;
nsCOMPtr<nsIRandomGenerator> mRandomGenerator;
// The next byte in the plain data to copy incoming data to.
size_t mNextByte = 0;

View File

@ -18,6 +18,8 @@
#include "nsDebug.h"
#include "nsError.h"
#include "nsIAsyncOutputStream.h"
#include "nsIRandomGenerator.h"
#include "nsServiceManagerUtils.h"
namespace mozilla::dom::quota {
template <typename CipherStrategy>
@ -207,6 +209,26 @@ nsresult EncryptingOutputStream<CipherStrategy>::FlushToBaseStream() {
return NS_OK;
}
if (mNextByte < mEncryptedBlock->MaxPayloadLength()) {
if (!mRandomGenerator) {
mRandomGenerator =
do_GetService("@mozilla.org/security/random-generator;1");
if (NS_WARN_IF(!mRandomGenerator)) {
return NS_ERROR_FAILURE;
}
}
const auto payload = mEncryptedBlock->MutablePayload();
const auto unusedPayload = payload.From(mNextByte);
nsresult rv = mRandomGenerator->GenerateRandomBytesInto(
unusedPayload.Elements(), unusedPayload.Length());
if (NS_WARN_IF(NS_FAILED(rv))) {
return rv;
}
}
// XXX The compressing stream implementation this was based on wrote a stream
// identifier, containing e.g. the block size. Should we do something like
// that as well? At the moment, we don't need it, but maybe this were