BodyUtil.h (2999B)
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_BodyUtil_h 8 #define mozilla_dom_BodyUtil_h 9 10 #include "js/Utility.h" // JS::FreePolicy 11 #include "mozilla/dom/File.h" 12 #include "mozilla/dom/FormData.h" 13 #include "nsError.h" 14 #include "nsString.h" 15 16 namespace mozilla { 17 class ErrorResult; 18 19 namespace dom { 20 21 class BodyUtil final { 22 private: 23 BodyUtil() = delete; 24 25 public: 26 /** 27 * Creates an array buffer from an array, assigning the result to |aValue|. 28 * The array buffer takes ownership of |aInput|, which must be allocated 29 * by |malloc|. 30 */ 31 static void ConsumeArrayBuffer(JSContext* aCx, 32 JS::MutableHandle<JSObject*> aValue, 33 uint32_t aInputLength, 34 UniquePtr<uint8_t[], JS::FreePolicy> aInput, 35 ErrorResult& aRv); 36 37 /** 38 * Creates an in-memory blob from an array. The blob takes ownership of 39 * |aInput|, which must be allocated by |malloc|. 40 */ 41 static already_AddRefed<Blob> ConsumeBlob(nsIGlobalObject* aParent, 42 const nsString& aMimeType, 43 uint32_t aInputLength, 44 uint8_t* aInput, ErrorResult& aRv); 45 46 /** 47 * Creates an Uint8Array from an array, assigning the result to |aValue|. 48 * The array buffer takes ownership of |aInput|, which must be allocated 49 * by |malloc|. 50 */ 51 static void ConsumeBytes(JSContext* aCx, JS::MutableHandle<JSObject*> aValue, 52 uint32_t aInputLength, 53 UniquePtr<uint8_t[], JS::FreePolicy> aInput, 54 ErrorResult& aRv); 55 56 /** 57 * Creates a form data object from a UTF-8 encoded |aStr|. Returns |nullptr| 58 * and sets |aRv| to MSG_BAD_FORMDATA if |aStr| contains invalid data. 59 */ 60 static already_AddRefed<FormData> ConsumeFormData( 61 nsIGlobalObject* aParent, const nsCString& aMimeType, 62 const nsACString& aMixedCaseMimeType, const nsCString& aStr, 63 ErrorResult& aRv); 64 65 /** 66 * UTF-8 decodes |aInput| into |aText|. The caller may free |aInput| 67 * once this method returns. 68 */ 69 static nsresult ConsumeText(uint32_t aInputLength, uint8_t* aInput, 70 nsString& aText); 71 72 /** 73 * Parses a UTF-8 encoded |aStr| as JSON, assigning the result to |aValue|. 74 * Sets |aRv| to a syntax error if |aStr| contains invalid data. 75 */ 76 static void ConsumeJson(JSContext* aCx, JS::MutableHandle<JS::Value> aValue, 77 const nsString& aStr, ErrorResult& aRv); 78 }; 79 80 } // namespace dom 81 } // namespace mozilla 82 83 #endif // mozilla_dom_BodyUtil_h