content/base/src/nsDOMBlobBuilder.h

Wed, 31 Dec 2014 06:09:35 +0100

author
Michael Schloh von Bennewitz <michael@schloh.com>
date
Wed, 31 Dec 2014 06:09:35 +0100
changeset 0
6474c204b198
permissions
-rw-r--r--

Cloned upstream origin tor-browser at tor-browser-31.3.0esr-4.5-1-build1
revision ID fc1c9ff7c1b2defdbc039f12214767608f46423f for hacking purpose.

michael@0 1 /* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
michael@0 2 /* This Source Code Form is subject to the terms of the Mozilla Public
michael@0 3 * License, v. 2.0. If a copy of the MPL was not distributed with this
michael@0 4 * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
michael@0 5
michael@0 6 #ifndef nsDOMBlobBuilder_h
michael@0 7 #define nsDOMBlobBuilder_h
michael@0 8
michael@0 9 #include "nsDOMFile.h"
michael@0 10
michael@0 11 #include "mozilla/CheckedInt.h"
michael@0 12 #include "mozilla/Attributes.h"
michael@0 13 #include <algorithm>
michael@0 14
michael@0 15 #define NS_DOMMULTIPARTBLOB_CID { 0x47bf0b43, 0xf37e, 0x49ef, \
michael@0 16 { 0x81, 0xa0, 0x18, 0xba, 0xc0, 0x57, 0xb5, 0xcc } }
michael@0 17 #define NS_DOMMULTIPARTBLOB_CONTRACTID "@mozilla.org/dom/multipart-blob;1"
michael@0 18
michael@0 19 #define NS_DOMMULTIPARTFILE_CID { 0xc3361f77, 0x60d1, 0x4ea9, \
michael@0 20 { 0x94, 0x96, 0xdf, 0x5d, 0x6f, 0xcd, 0xd7, 0x8f } }
michael@0 21 #define NS_DOMMULTIPARTFILE_CONTRACTID "@mozilla.org/dom/multipart-file;1"
michael@0 22
michael@0 23 class nsDOMMultipartFile : public nsDOMFile,
michael@0 24 public nsIJSNativeInitializer
michael@0 25 {
michael@0 26 public:
michael@0 27 // Create as a file
michael@0 28 nsDOMMultipartFile(nsTArray<nsCOMPtr<nsIDOMBlob> > aBlobs,
michael@0 29 const nsAString& aName,
michael@0 30 const nsAString& aContentType)
michael@0 31 : nsDOMFile(aName, aContentType, UINT64_MAX),
michael@0 32 mBlobs(aBlobs),
michael@0 33 mIsFromNsiFile(false)
michael@0 34 {
michael@0 35 }
michael@0 36
michael@0 37 // Create as a blob
michael@0 38 nsDOMMultipartFile(nsTArray<nsCOMPtr<nsIDOMBlob> >& aBlobs,
michael@0 39 const nsAString& aContentType)
michael@0 40 : nsDOMFile(aContentType, UINT64_MAX),
michael@0 41 mBlobs(aBlobs),
michael@0 42 mIsFromNsiFile(false)
michael@0 43 {
michael@0 44 }
michael@0 45
michael@0 46 // Create as a file to be later initialized
michael@0 47 nsDOMMultipartFile(const nsAString& aName)
michael@0 48 : nsDOMFile(aName, EmptyString(), UINT64_MAX),
michael@0 49 mIsFromNsiFile(false)
michael@0 50 {
michael@0 51 }
michael@0 52
michael@0 53 // Create as a blob to be later initialized
michael@0 54 nsDOMMultipartFile()
michael@0 55 : nsDOMFile(EmptyString(), UINT64_MAX),
michael@0 56 mIsFromNsiFile(false)
michael@0 57 {
michael@0 58 }
michael@0 59
michael@0 60 NS_DECL_ISUPPORTS_INHERITED
michael@0 61
michael@0 62 // nsIJSNativeInitializer
michael@0 63 NS_IMETHOD Initialize(nsISupports* aOwner,
michael@0 64 JSContext* aCx,
michael@0 65 JSObject* aObj,
michael@0 66 const JS::CallArgs& aArgs) MOZ_OVERRIDE;
michael@0 67
michael@0 68 typedef nsIDOMBlob* (*UnwrapFuncPtr)(JSContext*, JSObject*);
michael@0 69 nsresult InitBlob(JSContext* aCx,
michael@0 70 uint32_t aArgc,
michael@0 71 JS::Value* aArgv,
michael@0 72 UnwrapFuncPtr aUnwrapFunc);
michael@0 73 nsresult InitFile(JSContext* aCx,
michael@0 74 uint32_t aArgc,
michael@0 75 JS::Value* aArgv);
michael@0 76 nsresult InitChromeFile(JSContext* aCx,
michael@0 77 uint32_t aArgc,
michael@0 78 JS::Value* aArgv);
michael@0 79
michael@0 80 already_AddRefed<nsIDOMBlob>
michael@0 81 CreateSlice(uint64_t aStart, uint64_t aLength, const nsAString& aContentType) MOZ_OVERRIDE;
michael@0 82
michael@0 83 NS_IMETHOD GetSize(uint64_t*) MOZ_OVERRIDE;
michael@0 84 NS_IMETHOD GetInternalStream(nsIInputStream**) MOZ_OVERRIDE;
michael@0 85
michael@0 86 static nsresult
michael@0 87 NewFile(const nsAString& aName, nsISupports* *aNewObject);
michael@0 88
michael@0 89 // DOMClassInfo constructor (for Blob([b1, "foo"], { type: "image/png" }))
michael@0 90 static nsresult
michael@0 91 NewBlob(nsISupports* *aNewObject);
michael@0 92
michael@0 93 // DOMClassInfo constructor (for File([b1, "foo"], { type: "image/png",
michael@0 94 // name: "foo.png" }))
michael@0 95 inline static nsresult
michael@0 96 NewFile(nsISupports* *aNewObject)
michael@0 97 {
michael@0 98 // Initialization will set the filename, so we can pass in an empty string
michael@0 99 // for now.
michael@0 100 return NewFile(EmptyString(), aNewObject);
michael@0 101 }
michael@0 102
michael@0 103 virtual const nsTArray<nsCOMPtr<nsIDOMBlob> >*
michael@0 104 GetSubBlobs() const MOZ_OVERRIDE { return &mBlobs; }
michael@0 105
michael@0 106 NS_IMETHOD GetMozFullPathInternal(nsAString& aFullPath) MOZ_OVERRIDE;
michael@0 107
michael@0 108 protected:
michael@0 109 nsresult ParseBlobArrayArgument(JSContext* aCx, JS::Value& aValue,
michael@0 110 bool aNativeEOL, UnwrapFuncPtr aUnwrapFunc);
michael@0 111
michael@0 112 nsTArray<nsCOMPtr<nsIDOMBlob> > mBlobs;
michael@0 113 bool mIsFromNsiFile;
michael@0 114 };
michael@0 115
michael@0 116 class BlobSet {
michael@0 117 public:
michael@0 118 BlobSet()
michael@0 119 : mData(nullptr), mDataLen(0), mDataBufferLen(0)
michael@0 120 {}
michael@0 121
michael@0 122 nsresult AppendVoidPtr(const void* aData, uint32_t aLength);
michael@0 123 nsresult AppendString(JSString* aString, bool nativeEOL, JSContext* aCx);
michael@0 124 nsresult AppendBlob(nsIDOMBlob* aBlob);
michael@0 125 nsresult AppendArrayBuffer(JSObject* aBuffer);
michael@0 126 nsresult AppendBlobs(const nsTArray<nsCOMPtr<nsIDOMBlob> >& aBlob);
michael@0 127
michael@0 128 nsTArray<nsCOMPtr<nsIDOMBlob> >& GetBlobs() { Flush(); return mBlobs; }
michael@0 129
michael@0 130 already_AddRefed<nsIDOMBlob>
michael@0 131 GetBlobInternal(const nsACString& aContentType)
michael@0 132 {
michael@0 133 nsCOMPtr<nsIDOMBlob> blob =
michael@0 134 new nsDOMMultipartFile(GetBlobs(), NS_ConvertASCIItoUTF16(aContentType));
michael@0 135 return blob.forget();
michael@0 136 }
michael@0 137
michael@0 138 protected:
michael@0 139 bool ExpandBufferSize(uint64_t aSize)
michael@0 140 {
michael@0 141 using mozilla::CheckedUint32;
michael@0 142
michael@0 143 if (mDataBufferLen >= mDataLen + aSize) {
michael@0 144 mDataLen += aSize;
michael@0 145 return true;
michael@0 146 }
michael@0 147
michael@0 148 // Start at 1 or we'll loop forever.
michael@0 149 CheckedUint32 bufferLen =
michael@0 150 std::max<uint32_t>(static_cast<uint32_t>(mDataBufferLen), 1);
michael@0 151 while (bufferLen.isValid() && bufferLen.value() < mDataLen + aSize)
michael@0 152 bufferLen *= 2;
michael@0 153
michael@0 154 if (!bufferLen.isValid())
michael@0 155 return false;
michael@0 156
michael@0 157 void* data = moz_realloc(mData, bufferLen.value());
michael@0 158 if (!data)
michael@0 159 return false;
michael@0 160
michael@0 161 mData = data;
michael@0 162 mDataBufferLen = bufferLen.value();
michael@0 163 mDataLen += aSize;
michael@0 164 return true;
michael@0 165 }
michael@0 166
michael@0 167 void Flush() {
michael@0 168 if (mData) {
michael@0 169 // If we have some data, create a blob for it
michael@0 170 // and put it on the stack
michael@0 171
michael@0 172 nsCOMPtr<nsIDOMBlob> blob =
michael@0 173 new nsDOMMemoryFile(mData, mDataLen, EmptyString(), EmptyString());
michael@0 174 mBlobs.AppendElement(blob);
michael@0 175 mData = nullptr; // The nsDOMMemoryFile takes ownership of the buffer
michael@0 176 mDataLen = 0;
michael@0 177 mDataBufferLen = 0;
michael@0 178 }
michael@0 179 }
michael@0 180
michael@0 181 nsTArray<nsCOMPtr<nsIDOMBlob> > mBlobs;
michael@0 182 void* mData;
michael@0 183 uint64_t mDataLen;
michael@0 184 uint64_t mDataBufferLen;
michael@0 185 };
michael@0 186
michael@0 187 #endif

mercurial