Coverage Report

Created: 2018-09-25 14:53

/src/mozilla-central/dom/xhr/XMLHttpRequestString.h
Line
Count
Source (jump to first uncovered line)
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
namespace mozilla {
14
namespace dom {
15
16
class DOMString;
17
class XMLHttpRequestStringBuffer;
18
class XMLHttpRequestStringSnapshot;
19
class XMLHttpRequestStringWriterHelper;
20
class XMLHttpRequestStringSnapshotReaderHelper;
21
22
// We want to avoid the dup of strings when XHR in workers has access to
23
// responseText for events dispatched during the loading state. For this reason
24
// we use this class, able to create snapshots of the current size of itself
25
// without making extra copies.
26
class XMLHttpRequestString final
27
{
28
  friend class XMLHttpRequestStringWriterHelper;
29
30
public:
31
  XMLHttpRequestString();
32
  ~XMLHttpRequestString();
33
34
  void Truncate();
35
36
  uint32_t Length() const;
37
38
  void Append(const nsAString& aString);
39
40
  // This method should be called only when the string is really needed because
41
  // it can cause the duplication of the strings in case the loading of the XHR
42
  // is not completed yet.
43
  MOZ_MUST_USE bool GetAsString(nsAString& aString) const;
44
45
  size_t SizeOfThis(MallocSizeOf aMallocSizeOf) const;
46
47
  const char16_t* Data() const;
48
49
  bool IsEmpty() const;
50
51
  void CreateSnapshot(XMLHttpRequestStringSnapshot& aSnapshot);
52
53
private:
54
  XMLHttpRequestString(const XMLHttpRequestString&) = delete;
55
  XMLHttpRequestString& operator=(const XMLHttpRequestString&) = delete;
56
  XMLHttpRequestString& operator=(const XMLHttpRequestString&&) = delete;
57
58
  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
59
};
60
61
// This class locks the buffer and allows the callee to write data into it.
62
class MOZ_STACK_CLASS XMLHttpRequestStringWriterHelper final
63
{
64
public:
65
  explicit XMLHttpRequestStringWriterHelper(XMLHttpRequestString& aString);
66
67
  /**
68
   * The existing length of the string. Do not call during BulkWrite().
69
   */
70
  uint32_t
71
  Length() const;
72
73
  mozilla::BulkWriteHandle<char16_t>
74
  BulkWrite(uint32_t aCapacity, nsresult& aRv);
75
76
private:
77
  XMLHttpRequestStringWriterHelper(const XMLHttpRequestStringWriterHelper&) = delete;
78
  XMLHttpRequestStringWriterHelper& operator=(const XMLHttpRequestStringWriterHelper&) = delete;
79
  XMLHttpRequestStringWriterHelper& operator=(const XMLHttpRequestStringWriterHelper&&) = delete;
80
81
  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
82
  MutexAutoLock mLock;
83
};
84
85
// This class is the internal XMLHttpRequestStringBuffer of the
86
// XMLHttpRequestString plus the current length. GetAsString will return the
87
// string with that particular length also if the XMLHttpRequestStringBuffer is
88
// grown in the meantime.
89
class XMLHttpRequestStringSnapshot final
90
{
91
  friend class XMLHttpRequestStringBuffer;
92
  friend class XMLHttpRequestStringSnapshotReaderHelper;
93
94
public:
95
  XMLHttpRequestStringSnapshot();
96
  ~XMLHttpRequestStringSnapshot();
97
98
  XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&);
99
100
  void Reset()
101
0
  {
102
0
    ResetInternal(false /* isVoid */);
103
0
  }
104
105
  void SetVoid()
106
0
  {
107
0
    ResetInternal(true /* isVoid */);
108
0
  }
109
110
  bool IsVoid() const
111
0
  {
112
0
    return mVoid;
113
0
  }
114
115
  bool IsEmpty() const
116
0
  {
117
0
    return !mLength;
118
0
  }
119
120
  MOZ_MUST_USE bool GetAsString(DOMString& aString) const;
121
122
private:
123
  XMLHttpRequestStringSnapshot(const XMLHttpRequestStringSnapshot&) = delete;
124
  XMLHttpRequestStringSnapshot& operator=(const XMLHttpRequestStringSnapshot&&) = delete;
125
126
  void Set(XMLHttpRequestStringBuffer* aBuffer, uint32_t aLength);
127
128
  void ResetInternal(bool aIsVoid);
129
130
  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
131
  uint32_t mLength;
132
  bool mVoid;
133
};
134
135
// This class locks the buffer and allows the callee to read data from it.
136
class MOZ_STACK_CLASS XMLHttpRequestStringSnapshotReaderHelper final
137
{
138
public:
139
  explicit XMLHttpRequestStringSnapshotReaderHelper(XMLHttpRequestStringSnapshot& aSnapshot);
140
141
  const char16_t*
142
  Buffer() const;
143
144
  uint32_t
145
  Length() const;
146
147
private:
148
  XMLHttpRequestStringSnapshotReaderHelper(const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
149
  XMLHttpRequestStringSnapshotReaderHelper& operator=(const XMLHttpRequestStringSnapshotReaderHelper&) = delete;
150
  XMLHttpRequestStringSnapshotReaderHelper& operator=(const XMLHttpRequestStringSnapshotReaderHelper&&) = delete;
151
152
  RefPtr<XMLHttpRequestStringBuffer> mBuffer;
153
  MutexAutoLock mLock;
154
};
155
156
} // dom namespace
157
} // mozilla namespace
158
159
#endif // mozilla_dom_XMLHttpRequestString_h