tor-browser

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

FileSystemHashStorageFunction.cpp (2426B)


      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
      5 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "FileSystemHashStorageFunction.h"
      8 
      9 #include "FileSystemHashSource.h"
     10 #include "mozilla/dom/FileSystemTypes.h"
     11 #include "mozilla/dom/quota/QuotaCommon.h"
     12 #include "mozilla/storage/Variant.h"
     13 #include "nsString.h"
     14 #include "nsStringFwd.h"
     15 
     16 namespace mozilla::dom::fs::data {
     17 
     18 NS_IMPL_ISUPPORTS(FileSystemHashStorageFunction, mozIStorageFunction)
     19 
     20 NS_IMETHODIMP
     21 FileSystemHashStorageFunction::OnFunctionCall(
     22    mozIStorageValueArray* aFunctionArguments, nsIVariant** aResult) {
     23  MOZ_ASSERT(aFunctionArguments);
     24  MOZ_ASSERT(aResult);
     25 
     26  const int32_t parentIndex = 0;
     27  const int32_t childIndex = 1;
     28 
     29 #ifdef DEBUG
     30  {
     31    uint32_t argCount;
     32    MOZ_ALWAYS_SUCCEEDS(aFunctionArguments->GetNumEntries(&argCount));
     33    MOZ_ASSERT(argCount == 2u);
     34 
     35    int32_t parentType = mozIStorageValueArray::VALUE_TYPE_INTEGER;
     36    MOZ_ALWAYS_SUCCEEDS(
     37        aFunctionArguments->GetTypeOfIndex(parentIndex, &parentType));
     38    MOZ_ASSERT(parentType == mozIStorageValueArray::VALUE_TYPE_BLOB);
     39 
     40    int32_t childType = mozIStorageValueArray::VALUE_TYPE_INTEGER;
     41    MOZ_ALWAYS_SUCCEEDS(
     42        aFunctionArguments->GetTypeOfIndex(childIndex, &childType));
     43    MOZ_ASSERT(childType == mozIStorageValueArray::VALUE_TYPE_BLOB);
     44  }
     45 #endif
     46 
     47  QM_TRY_INSPECT(
     48      const EntryId& parentHash,
     49      MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(nsCString, aFunctionArguments,
     50                                        GetBlobAsUTF8String, parentIndex));
     51 
     52  QM_TRY_INSPECT(const Name& childName, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
     53                                            nsString, aFunctionArguments,
     54                                            GetBlobAsString, childIndex));
     55 
     56  QM_TRY_INSPECT(const EntryId& buffer,
     57                 FileSystemHashSource::GenerateHash(parentHash, childName)
     58                     .mapErr([](const auto& aRv) { return ToNSResult(aRv); }));
     59 
     60  nsCOMPtr<nsIVariant> result =
     61      new mozilla::storage::BlobVariant(std::make_pair(
     62          static_cast<const void*>(buffer.get()), int(buffer.Length())));
     63 
     64  result.forget(aResult);
     65 
     66  return NS_OK;
     67 }
     68 
     69 }  // namespace mozilla::dom::fs::data