Headers.cpp 2.9 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697
  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. #include "mozilla/dom/Headers.h"
  6. #include "mozilla/ErrorResult.h"
  7. #include "mozilla/dom/WorkerPrivate.h"
  8. #include "mozilla/Preferences.h"
  9. namespace mozilla {
  10. namespace dom {
  11. NS_IMPL_CYCLE_COLLECTING_ADDREF(Headers)
  12. NS_IMPL_CYCLE_COLLECTING_RELEASE(Headers)
  13. NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Headers, mOwner)
  14. NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Headers)
  15. NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
  16. NS_INTERFACE_MAP_ENTRY(nsISupports)
  17. NS_INTERFACE_MAP_END
  18. // static
  19. already_AddRefed<Headers>
  20. Headers::Constructor(const GlobalObject& aGlobal,
  21. const Optional<HeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord>& aInit,
  22. ErrorResult& aRv)
  23. {
  24. RefPtr<InternalHeaders> ih = new InternalHeaders();
  25. RefPtr<Headers> headers = new Headers(aGlobal.GetAsSupports(), ih);
  26. if (!aInit.WasPassed()) {
  27. return headers.forget();
  28. }
  29. if (aInit.Value().IsHeaders()) {
  30. ih->Fill(*aInit.Value().GetAsHeaders().mInternalHeaders, aRv);
  31. } else if (aInit.Value().IsByteStringSequenceSequence()) {
  32. ih->Fill(aInit.Value().GetAsByteStringSequenceSequence(), aRv);
  33. } else if (aInit.Value().IsByteStringByteStringRecord()) {
  34. ih->Fill(aInit.Value().GetAsByteStringByteStringRecord(), aRv);
  35. }
  36. if (aRv.Failed()) {
  37. return nullptr;
  38. }
  39. return headers.forget();
  40. }
  41. // static
  42. already_AddRefed<Headers>
  43. Headers::Constructor(const GlobalObject& aGlobal,
  44. const OwningHeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord& aInit,
  45. ErrorResult& aRv)
  46. {
  47. nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(aGlobal.GetAsSupports());
  48. return Create(global, aInit, aRv);
  49. }
  50. /* static */ already_AddRefed<Headers>
  51. Headers::Create(nsIGlobalObject* aGlobal,
  52. const OwningHeadersOrByteStringSequenceSequenceOrByteStringByteStringRecord& aInit,
  53. ErrorResult& aRv)
  54. {
  55. RefPtr<InternalHeaders> ih = new InternalHeaders();
  56. RefPtr<Headers> headers = new Headers(aGlobal, ih);
  57. if (aInit.IsHeaders()) {
  58. ih->Fill(*(aInit.GetAsHeaders().get()->mInternalHeaders), aRv);
  59. } else if (aInit.IsByteStringSequenceSequence()) {
  60. ih->Fill(aInit.GetAsByteStringSequenceSequence(), aRv);
  61. } else if (aInit.IsByteStringByteStringRecord()) {
  62. ih->Fill(aInit.GetAsByteStringByteStringRecord(), aRv);
  63. }
  64. if (NS_WARN_IF(aRv.Failed())) {
  65. return nullptr;
  66. }
  67. return headers.forget();
  68. }
  69. JSObject*
  70. Headers::WrapObject(JSContext* aCx, JS::Handle<JSObject*> aGivenProto)
  71. {
  72. return mozilla::dom::HeadersBinding::Wrap(aCx, this, aGivenProto);
  73. }
  74. Headers::~Headers()
  75. {
  76. }
  77. } // namespace dom
  78. } // namespace mozilla