mfbt/FStream.h
author Petru Lingurar <petru.lingurar@softvision.ro>
Fri, 21 Dec 2018 08:56:47 +0000
changeset 501492 65621d0fe1262af0643cec37c23b2d9ec42588ad
parent 481501 23fb443b22143c059267eb254ddd4b2e9fcc4e41
child 508163 6f3709b3878117466168c40affa7bca0b60cf75b
permissions -rw-r--r--
Bug 1513938 - Enforce a Bundle size limit and drop `privateSession` if exceeds it. r=JanH, a=jcristau The `privateSession` key would normally allow persisting the Private Browsing session across OOMs in Activity's Bundle. We need to do that to avoid storing private, sensible data on disk like we do with the normal browsing session. In some cases `privateSession` would contain a lot of data which, along with other possible concurrent transactions could overflow Binder's buffer which has a limited fixed size, currently 1Mb. To avoid this, we will drop `privateSession` from the Bundle if the resulting size is greater than a _speculative_ size of 300KBs which would mean that in the case of an OOM all Private Browsing state would be lost. Bug 1515592 is filed to investigate for a better solution. Differential Revision: https://phabricator.services.mozilla.com/D15067

/* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
/* vim: set ts=8 sts=2 et sw=2 tw=80: */
/* This Source Code Form is subject to the terms of the Mozilla Public
 * License, v. 2.0. If a copy of the MPL was not distributed with this
 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */

// Similar to std::ifstream/ofstream, but takes char16ptr_t on Windows.
// Until C++17, std functions can only take char* filenames. So Unicode
// filenames were lost on Windows. To address this limitations, this wrapper
// uses proprietary wchar_t* overloads on MSVC, and __gnu_cxx::stdio_filebuf
// extension on MinGW. Once we can use C++17 filesystem API everywhere,
// we will be able to avoid this wrapper.

#ifndef mozilla_FStream_h
#define mozilla_FStream_h

#include "mozilla/Char16.h"
#include <istream>
#include <ostream>
#include <fstream>
#if defined(__MINGW32__) && defined(__GLIBCXX__)
#include "mozilla/UniquePtr.h"
#include <fcntl.h>
#include <ext/stdio_filebuf.h>
#endif

namespace mozilla {

#if defined(__MINGW32__) && defined(__GLIBCXX__)
// MinGW does not support wchar_t* overloads that are MSVC extension until
// C++17, so we have to implement widechar wrappers using a GNU extension.
class IFStream : public std::istream
{
public:
  explicit IFStream(char16ptr_t filename, openmode mode = in);

  std::filebuf* rdbuf() const { return mFileBuf.get(); }

  bool is_open() const { return mFileBuf && mFileBuf->is_open(); }
  void open(char16ptr_t filename, openmode mode = in);
  void close() { mFileBuf && mFileBuf->close(); }

private:
  UniquePtr<std::filebuf> mFileBuf;
};

inline
IFStream::IFStream(char16ptr_t filename, openmode mode)
  : std::istream(nullptr)
{
  open(filename, mode);
}

inline void
IFStream::open(char16ptr_t filename, openmode mode)
{
  int fmode = _O_RDONLY;
  if (mode & binary) {
    fmode |= _O_BINARY;
  } else {
    fmode |= _O_TEXT;
  }
  int fd = _wopen(filename, fmode);
  mFileBuf = MakeUnique<__gnu_cxx::stdio_filebuf<char>>(fd, mode);
  std::istream::rdbuf(mFileBuf.get());
}

class OFStream : public std::ostream
{
public:
  explicit OFStream(char16ptr_t filename, openmode mode = out);

  std::filebuf* rdbuf() const { return mFileBuf.get(); }

  bool is_open() const { return mFileBuf && mFileBuf->is_open(); }
  void open(char16ptr_t filename, openmode mode = out);
  void close() { mFileBuf && mFileBuf->close(); }

private:
  UniquePtr<std::filebuf> mFileBuf;
};

inline
OFStream::OFStream(char16ptr_t filename, openmode mode)
  : std::ostream(nullptr)
{
  open(filename, mode);
}

inline void
OFStream::open(char16ptr_t filename, openmode mode)
{
  int fmode = _O_WRONLY;
  if (mode & binary) {
    fmode |= _O_BINARY;
  } else {
    fmode |= _O_TEXT;
  }
  if (mode & trunc) {
    fmode |= _O_CREAT | _O_TRUNC;
  }
  int fd = _wopen(filename, fmode);
  mFileBuf = MakeUnique<__gnu_cxx::stdio_filebuf<char>>(fd, mode);
  std::ostream::rdbuf(mFileBuf.get());
}

#elif defined(XP_WIN)
class IFStream : public std::ifstream
{
public:
  explicit IFStream(char16ptr_t filename, openmode mode = in)
    : std::ifstream(filename, mode) {}

  void open(char16ptr_t filename, openmode mode = in)
  {
    std::ifstream::open(filename, mode);
  }
};

class OFStream : public std::ofstream
{
public:
  explicit OFStream(char16ptr_t filename, openmode mode = out)
    : std::ofstream(filename, mode) {}

  void open(char16ptr_t filename, openmode mode = out)
  {
    std::ofstream::open(filename, mode);
  }
};
#else
using IFStream = std::ifstream;
using OFStream = std::ofstream;
#endif

} // namespace mozilla

#endif /* mozilla_FStream_h */