tor-browser

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

WebBrowserPersistSerializeParent.cpp (2608B)


      1 /* -*- Mode: C++; tab-width: 4; indent-tabs-mode: nil; c-basic-offset: 2 -*-
      2 *
      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 "WebBrowserPersistSerializeParent.h"
      8 
      9 #include "nsReadableUtils.h"
     10 #include "nsThreadUtils.h"
     11 
     12 namespace mozilla {
     13 
     14 WebBrowserPersistSerializeParent::WebBrowserPersistSerializeParent(
     15    nsIWebBrowserPersistDocument* aDocument, nsIOutputStream* aStream,
     16    nsIWebBrowserPersistWriteCompletion* aFinish)
     17    : mDocument(aDocument),
     18      mStream(aStream),
     19      mFinish(aFinish),
     20      mOutputError(NS_OK) {
     21  MOZ_ASSERT(aDocument);
     22  MOZ_ASSERT(aStream);
     23  MOZ_ASSERT(aFinish);
     24 }
     25 
     26 WebBrowserPersistSerializeParent::~WebBrowserPersistSerializeParent() = default;
     27 
     28 mozilla::ipc::IPCResult WebBrowserPersistSerializeParent::RecvWriteData(
     29    nsTArray<uint8_t>&& aData) {
     30  if (NS_FAILED(mOutputError)) {
     31    return IPC_OK();
     32  }
     33 
     34  uint32_t written = 0;
     35  static_assert(sizeof(char) == sizeof(uint8_t),
     36                "char must be (at least?) 8 bits");
     37  const char* data = reinterpret_cast<const char*>(aData.Elements());
     38  // nsIOutputStream::Write is allowed to return short writes.
     39  while (written < aData.Length()) {
     40    uint32_t writeReturn;
     41    nsresult rv =
     42        mStream->Write(data + written, aData.Length() - written, &writeReturn);
     43    if (NS_FAILED(rv)) {
     44      mOutputError = rv;
     45      return IPC_OK();
     46    }
     47    written += writeReturn;
     48  }
     49  return IPC_OK();
     50 }
     51 
     52 mozilla::ipc::IPCResult WebBrowserPersistSerializeParent::Recv__delete__(
     53    const nsACString& aContentType, const nsresult& aStatus) {
     54  if (NS_SUCCEEDED(mOutputError)) {
     55    mOutputError = aStatus;
     56  }
     57  mFinish->OnFinish(mDocument, mStream, aContentType, mOutputError);
     58  mFinish = nullptr;
     59  return IPC_OK();
     60 }
     61 
     62 void WebBrowserPersistSerializeParent::ActorDestroy(ActorDestroyReason aWhy) {
     63  if (mFinish) {
     64    MOZ_ASSERT(aWhy != Deletion);
     65    // See comment in WebBrowserPersistDocumentParent::ActorDestroy
     66    // (or bug 1202887) for why this is deferred.
     67    nsCOMPtr<nsIRunnable> errorLater =
     68        NewRunnableMethod<nsCOMPtr<nsIWebBrowserPersistDocument>,
     69                          nsCOMPtr<nsIOutputStream>, nsCString, nsresult>(
     70            "nsIWebBrowserPersistWriteCompletion::OnFinish", mFinish,
     71            &nsIWebBrowserPersistWriteCompletion::OnFinish, mDocument, mStream,
     72            ""_ns, NS_ERROR_FAILURE);
     73    NS_DispatchToCurrentThread(errorLater);
     74    mFinish = nullptr;
     75  }
     76 }
     77 
     78 }  // namespace mozilla