Headers.h (4037B)
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_Headers_h 8 #define mozilla_dom_Headers_h 9 10 #include "InternalHeaders.h" 11 #include "mozilla/dom/HeadersBinding.h" 12 #include "nsClassHashtable.h" 13 #include "nsWrapperCache.h" 14 15 namespace mozilla { 16 17 class ErrorResult; 18 19 namespace dom { 20 21 template <typename K, typename V> 22 class Record; 23 class ByteStringSequenceSequenceOrByteStringByteStringRecord; 24 class OwningByteStringSequenceSequenceOrByteStringByteStringRecord; 25 26 /** 27 * This Headers class is only used to represent the content facing Headers 28 * object. It is actually backed by an InternalHeaders implementation. Gecko 29 * code should NEVER use this, except in the Request and Response 30 * implementations, where they must always be created from the backing 31 * InternalHeaders object. 32 */ 33 class Headers final : public nsISupports, public nsWrapperCache { 34 NS_DECL_CYCLE_COLLECTING_ISUPPORTS 35 NS_DECL_CYCLE_COLLECTION_WRAPPERCACHE_CLASS(Headers) 36 37 friend class Request; 38 friend class Response; 39 40 private: 41 nsCOMPtr<nsISupports> mOwner; 42 RefPtr<InternalHeaders> mInternalHeaders; 43 44 public: 45 explicit Headers(nsISupports* aOwner, InternalHeaders* aInternalHeaders) 46 : mOwner(aOwner), mInternalHeaders(aInternalHeaders) {} 47 48 explicit Headers(const Headers& aOther) = delete; 49 50 static bool PrefEnabled(JSContext* cx, JSObject* obj); 51 52 static already_AddRefed<Headers> Constructor( 53 const GlobalObject& aGlobal, 54 const Optional<ByteStringSequenceSequenceOrByteStringByteStringRecord>& 55 aInit, 56 ErrorResult& aRv); 57 58 static already_AddRefed<Headers> Constructor( 59 const GlobalObject& aGlobal, 60 const OwningByteStringSequenceSequenceOrByteStringByteStringRecord& aInit, 61 ErrorResult& aRv); 62 63 static already_AddRefed<Headers> Create( 64 nsIGlobalObject* aGlobalObject, 65 const OwningByteStringSequenceSequenceOrByteStringByteStringRecord& aInit, 66 ErrorResult& aRv); 67 68 void Append(const nsACString& aName, const nsACString& aValue, 69 ErrorResult& aRv) { 70 mInternalHeaders->Append(aName, aValue, aRv); 71 } 72 73 void Delete(const nsACString& aName, ErrorResult& aRv) { 74 mInternalHeaders->Delete(aName, aRv); 75 } 76 77 void Get(const nsACString& aName, nsACString& aValue, 78 ErrorResult& aRv) const { 79 mInternalHeaders->Get(aName, aValue, aRv); 80 } 81 82 void GetSetCookie(nsTArray<nsCString>& aValues) const { 83 mInternalHeaders->GetSetCookie(aValues); 84 } 85 86 void GetFirst(const nsACString& aName, nsACString& aValue, 87 ErrorResult& aRv) const { 88 mInternalHeaders->GetFirst(aName, aValue, aRv); 89 } 90 91 bool Has(const nsACString& aName, ErrorResult& aRv) const { 92 return mInternalHeaders->Has(aName, aRv); 93 } 94 95 void Set(const nsACString& aName, const nsACString& aValue, 96 ErrorResult& aRv) { 97 mInternalHeaders->Set(aName, aValue, aRv); 98 } 99 100 uint32_t GetIterableLength() const { 101 return mInternalHeaders->GetIterableLength(); 102 } 103 const nsString GetKeyAtIndex(unsigned aIndex) const { 104 return mInternalHeaders->GetKeyAtIndex(aIndex); 105 } 106 const nsString GetValueAtIndex(unsigned aIndex) const { 107 return mInternalHeaders->GetValueAtIndex(aIndex); 108 } 109 110 // ChromeOnly 111 HeadersGuardEnum Guard() const { return mInternalHeaders->Guard(); } 112 113 void SetGuard(HeadersGuardEnum aGuard, ErrorResult& aRv) { 114 mInternalHeaders->SetGuard(aGuard, aRv); 115 } 116 117 virtual JSObject* WrapObject(JSContext* aCx, 118 JS::Handle<JSObject*> aGivenProto) override; 119 nsISupports* GetParentObject() const { return mOwner; } 120 121 private: 122 virtual ~Headers(); 123 124 InternalHeaders* GetInternalHeaders() const { return mInternalHeaders; } 125 }; 126 127 } // namespace dom 128 } // namespace mozilla 129 130 #endif // mozilla_dom_Headers_h