tor-browser

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

ZeroCopyNSIOutputStream.h (2376B)


      1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2; -*- */
      2 /* This Source Code Form is subject to the terms of the Mozilla Public
      3 * License, v. 2.0. If a copy of the MPL was not distributed with this
      4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
      5 
      6 #ifndef mozilla_devtools_ZeroCopyNSIOutputStream__
      7 #define mozilla_devtools_ZeroCopyNSIOutputStream__
      8 
      9 #include <google/protobuf/io/zero_copy_stream.h>
     10 #include <google/protobuf/stubs/common.h>
     11 
     12 #include "nsCOMPtr.h"
     13 #include "nsIOutputStream.h"
     14 
     15 namespace mozilla {
     16 namespace devtools {
     17 
     18 // A `google::protobuf::io::ZeroCopyOutputStream` implementation that uses an
     19 // `nsIOutputStream` under the covers.
     20 //
     21 // This class will automatically write and flush its data to the
     22 // `nsIOutputStream` in its destructor, but if you care whether that call
     23 // succeeds or fails, then you should call the `flush` method yourself. Errors
     24 // will be logged, however.
     25 class MOZ_STACK_CLASS ZeroCopyNSIOutputStream
     26    : public ::google::protobuf::io::ZeroCopyOutputStream {
     27  static const int BUFFER_SIZE = 8192;
     28 
     29  // The nsIOutputStream we are streaming to.
     30  nsCOMPtr<nsIOutputStream>& out;
     31 
     32  // The buffer we write data to before passing it to the output stream.
     33  char buffer[BUFFER_SIZE];
     34 
     35  // The status of writing to the underlying output stream.
     36  nsresult result_;
     37 
     38  // The number of bytes in the buffer that have been used thus far.
     39  int amountUsed;
     40 
     41  // Excluding the amount of the buffer currently used (which hasn't been
     42  // written and flushed yet), this is the number of bytes written to the output
     43  // stream.
     44  int64_t writtenCount;
     45 
     46  // Write the internal buffer to the output stream and flush it.
     47  nsresult writeBuffer();
     48 
     49 public:
     50  explicit ZeroCopyNSIOutputStream(nsCOMPtr<nsIOutputStream>& out);
     51 
     52  nsresult flush() { return writeBuffer(); }
     53 
     54  // Return true if writing to the underlying output stream ever failed.
     55  bool failed() const { return NS_FAILED(result_); }
     56 
     57  nsresult result() const { return result_; }
     58 
     59  // ZeroCopyOutputStream Interface
     60  virtual ~ZeroCopyNSIOutputStream() override;
     61  virtual bool Next(void** data, int* size) override;
     62  virtual void BackUp(int count) override;
     63  virtual ::google::protobuf::int64 ByteCount() const override;
     64 };
     65 
     66 }  // namespace devtools
     67 }  // namespace mozilla
     68 
     69 #endif  // mozilla_devtools_ZeroCopyNSIOutputStream__