tor-browser

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

SDBResults.cpp (1330B)


      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 "SDBResults.h"
      8 
      9 #include <cstdint>
     10 #include <cstring>
     11 #include <new>
     12 
     13 #include "ErrorList.h"
     14 #include "js/RootingAPI.h"
     15 #include "js/TypeDecls.h"
     16 #include "mozilla/dom/TypedArray.h"
     17 #include "nsDebug.h"
     18 #include "nsError.h"
     19 #include "nsTArray.h"
     20 #include "nscore.h"
     21 
     22 namespace mozilla::dom {
     23 
     24 SDBResult::SDBResult(const nsACString& aData) : mData(aData) {}
     25 
     26 NS_IMPL_ISUPPORTS(SDBResult, nsISDBResult)
     27 
     28 NS_IMETHODIMP
     29 SDBResult::GetAsArray(nsTArray<uint8_t>& aData) {
     30  uint32_t length = mData.Length();
     31  aData.SetLength(length);
     32 
     33  if (length != 0) {
     34    memcpy(aData.Elements(), mData.BeginReading(), length * sizeof(uint8_t));
     35  }
     36 
     37  return NS_OK;
     38 }
     39 
     40 NS_IMETHODIMP
     41 SDBResult::GetAsArrayBuffer(JSContext* aCx,
     42                            JS::MutableHandle<JS::Value> _retval) {
     43  ErrorResult rv;
     44  JS::Rooted<JSObject*> arrayBuffer(aCx, ArrayBuffer::Create(aCx, mData, rv));
     45  RETURN_NSRESULT_ON_FAILURE(rv);
     46 
     47  _retval.setObject(*arrayBuffer);
     48  return NS_OK;
     49 }
     50 
     51 }  // namespace mozilla::dom