tor-browser

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

StreamUtils.cpp (2840B)


      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 file,
      5 * You can obtain one at http://mozilla.org/MPL/2.0/. */
      6 
      7 #include "StreamUtils.h"
      8 
      9 #include "mozilla/GeckoTrace.h"
     10 #include "mozilla/Result.h"
     11 #include "mozilla/dom/quota/Assertions.h"
     12 #include "mozilla/dom/quota/QuotaCommon.h"
     13 #include "mozilla/dom/quota/ResultExtensions.h"
     14 #include "nsCOMPtr.h"
     15 #include "nsIFile.h"
     16 #include "nsIObjectInputStream.h"
     17 #include "nsIObjectOutputStream.h"
     18 #include "nsIOutputStream.h"
     19 #include "nsIRandomAccessStream.h"
     20 #include "nsNetUtil.h"
     21 #include "prio.h"
     22 
     23 namespace mozilla::dom::quota {
     24 
     25 Result<nsCOMPtr<nsIOutputStream>, nsresult> GetOutputStream(
     26    nsIFile& aFile, FileFlag aFileFlag) {
     27  AssertIsOnIOThread();
     28 
     29  switch (aFileFlag) {
     30    case FileFlag::Truncate:
     31      QM_TRY_RETURN(NS_NewLocalFileOutputStream(&aFile));
     32 
     33    case FileFlag::Update: {
     34      QM_TRY_INSPECT(const bool& exists,
     35                     MOZ_TO_RESULT_INVOKE_MEMBER(&aFile, Exists));
     36 
     37      if (!exists) {
     38        return nsCOMPtr<nsIOutputStream>();
     39      }
     40 
     41      QM_TRY_INSPECT(const auto& stream,
     42                     NS_NewLocalFileRandomAccessStream(&aFile));
     43 
     44      nsCOMPtr<nsIOutputStream> outputStream = do_QueryInterface(stream);
     45      QM_TRY(OkIf(outputStream), Err(NS_ERROR_FAILURE));
     46 
     47      return outputStream;
     48    }
     49 
     50    case FileFlag::Append:
     51      QM_TRY_RETURN(NS_NewLocalFileOutputStream(
     52          &aFile, PR_WRONLY | PR_CREATE_FILE | PR_APPEND));
     53 
     54    default:
     55      MOZ_CRASH("Should never get here!");
     56  }
     57 }
     58 
     59 Result<nsCOMPtr<nsIBinaryOutputStream>, nsresult> GetBinaryOutputStream(
     60    nsIFile& aFile, FileFlag aFileFlag) {
     61  QM_TRY_UNWRAP(auto outputStream, GetOutputStream(aFile, aFileFlag));
     62 
     63  QM_TRY(OkIf(outputStream), Err(NS_ERROR_UNEXPECTED));
     64 
     65  return nsCOMPtr<nsIBinaryOutputStream>(
     66      NS_NewObjectOutputStream(outputStream));
     67 }
     68 
     69 Result<nsCOMPtr<nsIBinaryInputStream>, nsresult> GetBinaryInputStream(
     70    nsIFile& aDirectory, const nsAString& aFilename) {
     71  GECKO_TRACE_SCOPE("dom::quota", "GetBinaryInputStream")
     72 
     73  MOZ_ASSERT(!NS_IsMainThread());
     74 
     75  QM_TRY_INSPECT(const auto& file, MOZ_TO_RESULT_INVOKE_MEMBER_TYPED(
     76                                       nsCOMPtr<nsIFile>, aDirectory, Clone));
     77 
     78  QM_TRY(MOZ_TO_RESULT(file->Append(aFilename)));
     79 
     80  QM_TRY_UNWRAP(auto stream, NS_NewLocalFileInputStream(file));
     81 
     82  QM_TRY_INSPECT(const auto& bufferedStream,
     83                 NS_NewBufferedInputStream(stream.forget(), 512));
     84 
     85  QM_TRY(OkIf(bufferedStream), Err(NS_ERROR_FAILURE));
     86 
     87  return nsCOMPtr<nsIBinaryInputStream>(
     88      NS_NewObjectInputStream(bufferedStream));
     89 }
     90 
     91 }  // namespace mozilla::dom::quota