tor-browser

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

XMLHttpRequestString.h (4028B)


      1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
      2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */
      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 #ifndef mozilla_dom_XMLHttpRequestString_h
      8 #define mozilla_dom_XMLHttpRequestString_h
      9 
     10 #include "mozilla/Mutex.h"
     11 #include "nsString.h"
     12 
     13 struct JSContext;
     14 class JSString;
     15 
     16 namespace mozilla::dom {
     17 
     18 class ArrayBufferBuilder;
     19 class BlobImpl;
     20 class DOMString;
     21 class XMLHttpRequestStringBuffer;
     22 class XMLHttpRequestStringSnapshot;
     23 class XMLHttpRequestStringWriterHelper;
     24 
     25 // We want to avoid the dup of strings when XHR in workers has access to
     26 // responseText for events dispatched during the loading state. For this reason
     27 // we use this class, able to create snapshots of the current size of itself
     28 // without making extra copies.
     29 class XMLHttpRequestString final {
     30  friend class XMLHttpRequestStringWriterHelper;
     31 
     32 public:
     33  XMLHttpRequestString();
     34  ~XMLHttpRequestString();
     35 
     36  void Truncate();
     37 
     38  uint32_t Length() const;
     39 
     40  void Append(const nsAString& aString);
     41 
     42  // This method should be called only when the string is really needed because
     43  // it can cause the duplication of the strings in case the loading of the XHR
     44  // is not completed yet.
     45  [[nodiscard]] bool GetAsString(nsAString& aString) const;
     46 
     47  size_t SizeOfThis(MallocSizeOf aMallocSizeOf) const;
     48 
     49  const char16_t* Data() const;
     50 
     51  bool IsEmpty() const;
     52 
     53  void CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot);
     54 
     55 private:
     56  XMLHttpRequestString(const XMLHttpRequestString&) = delete;
     57  XMLHttpRequestString& operator=(const XMLHttpRequestString&) = delete;
     58  XMLHttpRequestString& operator=(const XMLHttpRequestString&&) = delete;
     59 
     60  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
     61 };
     62 
     63 // This class locks the buffer and allows the callee to write data into it.
     64 class MOZ_STACK_CLASS XMLHttpRequestStringWriterHelper final {
     65 public:
     66  explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString);
     67  ~XMLHttpRequestStringWriterHelper();
     68 
     69  /**
     70   * The existing length of the string. Do not call during BulkWrite().
     71   */
     72  uint32_t Length() const;
     73 
     74  mozilla::Result<mozilla::BulkWriteHandle<char16_t>, nsresult> BulkWrite(
     75      uint32_t aCapacity);
     76 
     77 private:
     78  XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper&) =
     79      delete;
     80  XMLHttpRequestStringWriterHelper& operator=(
     81      const XMLHttpRequestStringWriterHelper&) = delete;
     82  XMLHttpRequestStringWriterHelper& operator=(
     83      const XMLHttpRequestStringWriterHelper&&) = delete;
     84 
     85  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
     86  MutexAutoLock mLock;
     87 };
     88 
     89 // This class is the internal XMLHttpRequestStringBuffer of the
     90 // XMLHttpRequestString plus the current length. GetAsString will return the
     91 // string with that particular length also if the XMLHttpRequestStringBuffer is
     92 // grown in the meantime.
     93 class XMLHttpRequestStringSnapshot final {
     94  friend class XMLHttpRequestStringBuffer;
     95 
     96 public:
     97  XMLHttpRequestStringSnapshot();
     98  ~XMLHttpRequestStringSnapshot();
     99 
    100  XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&) =
    101      delete;
    102 
    103  void Reset() { ResetInternal(false /* isVoid */); }
    104 
    105  void SetVoid() { ResetInternal(true /* isVoid */); }
    106 
    107  bool IsVoid() const { return mVoid; }
    108 
    109  bool IsEmpty() const { return !mLength; }
    110 
    111  [[nodiscard]] bool GetAsString(DOMString& aString) const;
    112 
    113  JSString* GetAsJSStringCopy(JSContext* aCx) const;
    114 
    115 private:
    116  XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot&) = delete;
    117  XMLHttpRequestStringSnapshot& operator=(
    118      const XMLHttpRequestStringSnapshot&&) = delete;
    119 
    120  void Set(XMLHttpRequestStringBuffer* aBuffer, uint32_t aLength);
    121 
    122  void ResetInternal(bool aIsVoid);
    123 
    124  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
    125  uint32_t mLength;
    126  bool mVoid;
    127 };
    128 
    129 }  // namespace mozilla::dom
    130 
    131 #endif  // mozilla_dom_XMLHttpRequestString_h