tor-browser

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

WebBrowserPersistSerializeChild.cpp (4500B)


      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 "WebBrowserPersistSerializeChild.h"
      8 
      9 #include <algorithm>
     10 
     11 #include "nsThreadUtils.h"
     12 
     13 namespace mozilla {
     14 
     15 NS_IMPL_ISUPPORTS(WebBrowserPersistSerializeChild,
     16                  nsIWebBrowserPersistWriteCompletion,
     17                  nsIWebBrowserPersistURIMap, nsIOutputStream)
     18 
     19 WebBrowserPersistSerializeChild::WebBrowserPersistSerializeChild(
     20    const WebBrowserPersistURIMap& aMap)
     21    : mMap(aMap) {}
     22 
     23 WebBrowserPersistSerializeChild::~WebBrowserPersistSerializeChild() = default;
     24 
     25 NS_IMETHODIMP
     26 WebBrowserPersistSerializeChild::OnFinish(
     27    nsIWebBrowserPersistDocument* aDocument, nsIOutputStream* aStream,
     28    const nsACString& aContentType, nsresult aStatus) {
     29  MOZ_ASSERT(aStream == this);
     30  nsCString contentType(aContentType);
     31  Send__delete__(this, contentType, aStatus);
     32  return NS_OK;
     33 }
     34 
     35 NS_IMETHODIMP
     36 WebBrowserPersistSerializeChild::GetNumMappedURIs(uint32_t* aNum) {
     37  *aNum = static_cast<uint32_t>(mMap.mapURIs().Length());
     38  return NS_OK;
     39 }
     40 
     41 NS_IMETHODIMP
     42 WebBrowserPersistSerializeChild::GetURIMapping(uint32_t aIndex,
     43                                               nsACString& aMapFrom,
     44                                               nsACString& aMapTo) {
     45  if (aIndex >= mMap.mapURIs().Length()) {
     46    return NS_ERROR_INVALID_ARG;
     47  }
     48  aMapFrom = mMap.mapURIs()[aIndex].mapFrom();
     49  aMapTo = mMap.mapURIs()[aIndex].mapTo();
     50  return NS_OK;
     51 }
     52 
     53 NS_IMETHODIMP
     54 WebBrowserPersistSerializeChild::GetTargetBaseURI(nsACString& aURI) {
     55  aURI = mMap.targetBaseURI();
     56  return NS_OK;
     57 }
     58 
     59 NS_IMETHODIMP
     60 WebBrowserPersistSerializeChild::Close() {
     61  NS_WARNING("WebBrowserPersistSerializeChild::Close()");
     62  return NS_ERROR_NOT_IMPLEMENTED;
     63 }
     64 
     65 NS_IMETHODIMP
     66 WebBrowserPersistSerializeChild::Flush() {
     67  NS_WARNING("WebBrowserPersistSerializeChild::Flush()");
     68  return NS_ERROR_NOT_IMPLEMENTED;
     69 }
     70 
     71 NS_IMETHODIMP
     72 WebBrowserPersistSerializeChild::StreamStatus() {
     73  // XXX: This stream doesn't appear to have a closed state.
     74  return NS_OK;
     75 }
     76 
     77 NS_IMETHODIMP
     78 WebBrowserPersistSerializeChild::Write(const char* aBuf, uint32_t aCount,
     79                                       uint32_t* aWritten) {
     80  // Normally an nsIOutputStream would have to be thread-safe, but
     81  // nsDocumentEncoder currently doesn't call this off the main
     82  // thread (which also means it's difficult to test the
     83  // thread-safety code this class doesn't yet have).
     84  //
     85  // This is *not* an NS_ERROR_NOT_IMPLEMENTED, because at this
     86  // point we've probably already misused the non-thread-safe
     87  // refcounting.
     88  MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Fix this class to be thread-safe.");
     89 
     90  // Limit the message size to 64k because large messages are
     91  // potentially bad for the latency of other messages on the same channel.
     92  static const uint32_t kMaxWrite = 65536;
     93 
     94  // Work around bug 1181433 by sending multiple messages if
     95  // necessary to write the entire aCount bytes, even though
     96  // nsIOutputStream.idl says we're allowed to do a short write.
     97  const char* buf = aBuf;
     98  uint32_t count = aCount;
     99  *aWritten = 0;
    100  while (count > 0) {
    101    uint32_t toWrite = std::min(kMaxWrite, count);
    102    nsTArray<uint8_t> arrayBuf;
    103    // It would be nice if this extra copy could be avoided.
    104    arrayBuf.AppendElements(buf, toWrite);
    105    SendWriteData(std::move(arrayBuf));
    106    *aWritten += toWrite;
    107    buf += toWrite;
    108    count -= toWrite;
    109  }
    110  return NS_OK;
    111 }
    112 
    113 NS_IMETHODIMP
    114 WebBrowserPersistSerializeChild::WriteFrom(nsIInputStream* aFrom,
    115                                           uint32_t aCount,
    116                                           uint32_t* aWritten) {
    117  NS_WARNING("WebBrowserPersistSerializeChild::WriteFrom()");
    118  return NS_ERROR_NOT_IMPLEMENTED;
    119 }
    120 
    121 NS_IMETHODIMP
    122 WebBrowserPersistSerializeChild::WriteSegments(nsReadSegmentFun aFun,
    123                                               void* aCtx, uint32_t aCount,
    124                                               uint32_t* aWritten) {
    125  NS_WARNING("WebBrowserPersistSerializeChild::WriteSegments()");
    126  return NS_ERROR_NOT_IMPLEMENTED;
    127 }
    128 
    129 NS_IMETHODIMP
    130 WebBrowserPersistSerializeChild::IsNonBlocking(bool* aNonBlocking) {
    131  // Writes will never fail with NS_BASE_STREAM_WOULD_BLOCK, so:
    132  *aNonBlocking = false;
    133  return NS_OK;
    134 }
    135 
    136 }  // namespace mozilla