NS_IMETHODIMP
WebBrowserPersistSerializeChild::Write(const char* aBuf, uint32_t aCount,
                                       uint32_t* aWritten)
{
    // Normally an nsIOutputStream would have to be thread-safe, but
    // nsDocumentEncoder currently doesn't call this off the main
    // thread (which also means it's difficult to test the
    // thread-safety code this class doesn't yet have).
    //
    // This is *not* an NS_ERROR_NOT_IMPLEMENTED, because at this
    // point we've probably already misused the non-thread-safe
    // refcounting.
    MOZ_RELEASE_ASSERT(NS_IsMainThread(), "Fix this class to be thread-safe.");

    // Limit the size of an individual IPC message.
    static const uint32_t kMaxWrite = 65536;

    // Work around bug 1181433 by sending multiple messages if
    // necessary to write the entire aCount bytes, even though
    // nsIOutputStream.idl says we're allowed to do a short write.
    const char* buf = aBuf;
    uint32_t count = aCount;
    *aWritten = 0;
    while (count > 0) {
        uint32_t toWrite = std::min(kMaxWrite, count);
        nsTArray<uint8_t> arrayBuf;
        // It would be nice if this extra copy could be avoided.
        arrayBuf.AppendElements(buf, toWrite);
        SendWriteData(Move(arrayBuf));
        *aWritten += toWrite;
        buf += toWrite;
        count -= toWrite;
    }
    return NS_OK;
}
bool
AltDataOutputStreamChild::WriteDataInChunks(const nsCString& data)
{
  const uint32_t kChunkSize = 128*1024;
  uint32_t next = std::min(data.Length(), kChunkSize);
  for (uint32_t i = 0; i < data.Length();
       i = next, next = std::min(data.Length(), next + kChunkSize)) {
    nsCString chunk(Substring(data, i, kChunkSize));
    if (mIPCOpen && !SendWriteData(chunk)) {
      mIPCOpen = false;
      return false;
    }
  }
  return true;
}