Coverage Report

Created: 2018-09-25 14:53

/work/obj-fuzz/dist/include/mozilla/dom/Blob.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_Blob_h
8
#define mozilla_dom_Blob_h
9
10
#include "mozilla/Attributes.h"
11
#include "mozilla/ErrorResult.h"
12
#include "mozilla/dom/BindingDeclarations.h"
13
#include "mozilla/dom/BlobImpl.h"
14
#include "nsCycleCollectionParticipant.h"
15
#include "nsCOMPtr.h"
16
#include "nsIMutable.h"
17
#include "nsWrapperCache.h"
18
#include "nsWeakReference.h"
19
20
class nsIInputStream;
21
22
namespace mozilla {
23
namespace dom {
24
25
struct BlobPropertyBag;
26
class File;
27
class OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString;
28
29
#define NS_DOM_BLOB_IID \
30
{ 0x648c2a83, 0xbdb1, 0x4a7d, \
31
  { 0xb5, 0x0a, 0xca, 0xcd, 0x92, 0x87, 0x45, 0xc2 } }
32
33
34
class Blob : public nsIMutable
35
           , public nsSupportsWeakReference
36
           , public nsWrapperCache
37
{
38
public:
39
  NS_DECL_NSIMUTABLE
40
41
  NS_DECL_CYCLE_COLLECTING_ISUPPORTS_FINAL
42
  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS_AMBIGUOUS(Blob, nsIMutable)
43
  NS_DECLARE_STATIC_IID_ACCESSOR(NS_DOM_BLOB_IID)
44
45
  typedef OwningArrayBufferViewOrArrayBufferOrBlobOrUSVString BlobPart;
46
47
  // This creates a Blob or a File based on the type of BlobImpl.
48
  static Blob*
49
  Create(nsISupports* aParent, BlobImpl* aImpl);
50
51
  static already_AddRefed<Blob>
52
  CreateStringBlob(nsISupports* aParent, const nsACString& aData,
53
                   const nsAString& aContentType);
54
55
  // The returned Blob takes ownership of aMemoryBuffer. aMemoryBuffer will be
56
  // freed by free so it must be allocated by malloc or something
57
  // compatible with it.
58
  static already_AddRefed<Blob>
59
  CreateMemoryBlob(nsISupports* aParent, void* aMemoryBuffer, uint64_t aLength,
60
                   const nsAString& aContentType);
61
62
  BlobImpl* Impl() const
63
0
  {
64
0
    return mImpl;
65
0
  }
66
67
  bool IsFile() const;
68
69
  const nsTArray<RefPtr<BlobImpl>>* GetSubBlobImpls() const;
70
71
  // This method returns null if this Blob is not a File; it returns
72
  // the same object in case this Blob already implements the File interface;
73
  // otherwise it returns a new File object with the same BlobImpl.
74
  already_AddRefed<File> ToFile();
75
76
  // This method creates a new File object with the given name and the same
77
  // BlobImpl.
78
  already_AddRefed<File> ToFile(const nsAString& aName,
79
                                ErrorResult& aRv) const;
80
81
  already_AddRefed<Blob>
82
  CreateSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType,
83
              ErrorResult& aRv);
84
85
  void
86
  CreateInputStream(nsIInputStream** aStream, ErrorResult& aRv);
87
88
  int64_t
89
  GetFileId();
90
91
  // A utility function that enforces the spec constraints on the type of a
92
  // blob: no codepoints outside the ASCII range (otherwise type becomes empty)
93
  // and lowercase ASCII only.  We can't just use our existing nsContentUtils
94
  // ASCII-related helpers because we need the "outside ASCII range" check, and
95
  // we can't use NS_IsAscii because its definition of "ASCII" (chars all <=
96
  // 0x7E) differs from the file API definition (which excludes control chars).
97
  static void
98
  MakeValidBlobType(nsAString& aType);
99
100
  // WebIDL methods
101
  nsISupports* GetParentObject() const
102
0
  {
103
0
    return mParent;
104
0
  }
105
106
  bool
107
  IsMemoryFile() const;
108
109
  // Blob constructor
110
  static already_AddRefed<Blob>
111
  Constructor(const GlobalObject& aGlobal,
112
              const Optional<Sequence<BlobPart>>& aData,
113
              const BlobPropertyBag& aBag,
114
              ErrorResult& aRv);
115
116
  virtual JSObject* WrapObject(JSContext* aCx,
117
                               JS::Handle<JSObject*> aGivenProto) override;
118
119
  uint64_t GetSize(ErrorResult& aRv);
120
121
  void GetType(nsAString& aType);
122
123
  already_AddRefed<Blob> Slice(const Optional<int64_t>& aStart,
124
                               const Optional<int64_t>& aEnd,
125
                               const Optional<nsAString>& aContentType,
126
                               ErrorResult& aRv);
127
128
  size_t GetAllocationSize() const;
129
130
  nsresult
131
  GetSendInfo(nsIInputStream** aBody,
132
              uint64_t* aContentLength,
133
              nsACString& aContentType,
134
              nsACString& aCharset) const;
135
136
protected:
137
  // File constructor should never be used directly. Use Blob::Create instead.
138
  Blob(nsISupports* aParent, BlobImpl* aImpl);
139
  virtual ~Blob();
140
141
0
  virtual bool HasFileInterface() const { return false; }
142
143
  // The member is the real backend implementation of this File/Blob.
144
  // It's thread-safe and not CC-able and it's the only element that is moved
145
  // between threads.
146
  // Note: we should not store any other state in this class!
147
  RefPtr<BlobImpl> mImpl;
148
149
private:
150
  nsCOMPtr<nsISupports> mParent;
151
};
152
153
NS_DEFINE_STATIC_IID_ACCESSOR(Blob, NS_DOM_BLOB_IID)
154
155
// Override BindingJSObjectMallocBytes for blobs to tell the JS GC how much
156
// memory is held live by the binding object.
157
size_t BindingJSObjectMallocBytes(Blob* aBlob);
158
159
} // namespace dom
160
} // namespace mozilla
161
162
inline nsISupports*
163
ToSupports(mozilla::dom::Blob* aBlob)
164
0
{
165
0
  return static_cast<nsIMutable*>(aBlob);
166
0
}
167
168
#endif // mozilla_dom_Blob_h