Headers.h 3.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144
  1. /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
  2. /* This Source Code Form is subject to the terms of the Mozilla Public
  3. * License, v. 2.0. If a copy of the MPL was not distributed with this
  4. * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
  5. #ifndef mozilla_dom_Headers_h
  6. #define mozilla_dom_Headers_h
  7. #include "mozilla/dom/HeadersBinding.h"
  8. #include "nsClassHashtable.h"
  9. #include "nsWrapperCache.h"
  10. #include "InternalHeaders.h"
  11. namespace mozilla {
  12. class ErrorResult;
  13. namespace dom {
  14. template<typename K, typename V> class Record;
  15. class HeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord;
  16. class OwningHeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord;
  17. /**
  18. * This Headers class is only used to represent the content facing Headers
  19. * object. It is actually backed by an InternalHeaders implementation. Gecko
  20. * code should NEVER use this, except in the Request and Response
  21. * implementations, where they must always be created from the backing
  22. * InternalHeaders object.
  23. */
  24. class Headers final : public nsISupports
  25. , public nsWrapperCache
  26. {
  27. NS_DECL_CYCLE_COLLECTING_ISUPPORTS
  28. NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Headers)
  29. friend class Request;
  30. friend class Response;
  31. private:
  32. nsCOMPtr<nsISupports> mOwner;
  33. RefPtr<InternalHeaders> mInternalHeaders;
  34. public:
  35. explicit Headers(nsISupports* aOwner, InternalHeaders* aInternalHeaders)
  36. : mOwner(aOwner)
  37. , mInternalHeaders(aInternalHeaders)
  38. {
  39. }
  40. explicit Headers(const Headers& aOther) = delete;
  41. static bool PrefEnabled(JSContext* cx, JSObject* obj);
  42. static already_AddRefed<Headers>
  43. Constructor(const GlobalObject& aGlobal,
  44. const Optional<HeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord>& aInit,
  45. ErrorResult& aRv);
  46. static already_AddRefed<Headers>
  47. Constructor(const GlobalObject& aGlobal,
  48. const OwningHeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord& aInit,
  49. ErrorResult& aRv);
  50. static already_AddRefed<Headers>
  51. Create(nsIGlobalObject* aGlobalObject,
  52. const OwningHeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord& aInit,
  53. ErrorResult& aRv);
  54. void Append(const nsACString& aName, const nsACString& aValue,
  55. ErrorResult& aRv)
  56. {
  57. mInternalHeaders->Append(aName, aValue, aRv);
  58. }
  59. void Delete(const nsACString& aName, ErrorResult& aRv)
  60. {
  61. mInternalHeaders->Delete(aName, aRv);
  62. }
  63. void Get(const nsACString& aName, nsACString& aValue, ErrorResult& aRv) const
  64. {
  65. mInternalHeaders->Get(aName, aValue, aRv);
  66. }
  67. void GetFirst(const nsACString& aName, nsACString& aValue, ErrorResult& aRv) const
  68. {
  69. mInternalHeaders->GetFirst(aName, aValue, aRv);
  70. }
  71. bool Has(const nsACString& aName, ErrorResult& aRv) const
  72. {
  73. return mInternalHeaders->Has(aName, aRv);
  74. }
  75. void Set(const nsACString& aName, const nsACString& aValue, ErrorResult& aRv)
  76. {
  77. mInternalHeaders->Set(aName, aValue, aRv);
  78. }
  79. uint32_t GetIterableLength() const
  80. {
  81. return mInternalHeaders->GetIterableLength();
  82. }
  83. const nsString GetKeyAtIndex(unsigned aIndex) const
  84. {
  85. return mInternalHeaders->GetKeyAtIndex(aIndex);
  86. }
  87. const nsString GetValueAtIndex(unsigned aIndex) const
  88. {
  89. return mInternalHeaders->GetValueAtIndex(aIndex);
  90. }
  91. // ChromeOnly
  92. HeadersGuardEnum Guard() const
  93. {
  94. return mInternalHeaders->Guard();
  95. }
  96. void SetGuard(HeadersGuardEnum aGuard, ErrorResult& aRv)
  97. {
  98. mInternalHeaders->SetGuard(aGuard, aRv);
  99. }
  100. virtual JSObject* WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto) override;
  101. nsISupports* GetParentObject() const { return mOwner; }
  102. private:
  103. virtual ~Headers();
  104. InternalHeaders*
  105. GetInternalHeaders() const
  106. {
  107. return mInternalHeaders;
  108. }
  109. };
  110. } // namespace dom
  111. } // namespace mozilla
  112. #endif // mozilla_dom_Headers_h