Bug 1017613 - Part 2 - Response IDL and stubs. r=baku
authorNikhil Marathe <nsm.nikhil@gmail.com>
Thu, 24 Jul 2014 17:50:32 -0700
changeset 208781 a223830b97a4a3fdf253efb40af2fd5bc8644610
parent 208780 09e3b826cf7d546939bc9e5ba9ad5d0e8ca17e6f
child 208782 9d063480262717f3909e9d0212124b799c1d66d8
push id1
push userroot
push dateMon, 20 Oct 2014 17:29:22 +0000
reviewersbaku
bugs1017613
milestone35.0a1
Bug 1017613 - Part 2 - Response IDL and stubs. r=baku
dom/bindings/Bindings.conf
dom/fetch/Response.cpp
dom/fetch/Response.h
dom/fetch/moz.build
dom/tests/mochitest/general/test_interfaces.html
dom/webidl/Response.webidl
dom/webidl/moz.build
dom/workers/test/fetch/worker_interfaces.js
--- a/dom/bindings/Bindings.conf
+++ b/dom/bindings/Bindings.conf
@@ -882,16 +882,20 @@ DOMInterfaces = {
 'Rect': {
     'nativeType': 'nsDOMCSSRect',
 },
 
 'Request': {
     'binaryNames': { 'headers': 'headers_' },
 },
 
+'Response': {
+    'binaryNames': { 'headers': 'headers_' },
+},
+
 'RGBColor': {
     'nativeType': 'nsDOMCSSRGBColor',
 },
 
 'Screen': {
     'nativeType': 'nsScreen',
 },
 
new file mode 100644
--- /dev/null
+++ b/dom/fetch/Response.cpp
@@ -0,0 +1,139 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#include "Response.h"
+#include "nsDOMString.h"
+#include "nsPIDOMWindow.h"
+#include "nsIURI.h"
+#include "nsISupportsImpl.h"
+
+#include "mozilla/ErrorResult.h"
+#include "mozilla/dom/Headers.h"
+#include "mozilla/dom/Promise.h"
+
+namespace mozilla {
+namespace dom {
+
+NS_IMPL_CYCLE_COLLECTING_ADDREF(Response)
+NS_IMPL_CYCLE_COLLECTING_RELEASE(Response)
+NS_IMPL_CYCLE_COLLECTION_WRAPPERCACHE(Response, mOwner)
+
+NS_INTERFACE_MAP_BEGIN_CYCLE_COLLECTION(Response)
+  NS_WRAPPERCACHE_INTERFACE_MAP_ENTRY
+  NS_INTERFACE_MAP_ENTRY(nsISupports)
+NS_INTERFACE_MAP_END
+
+Response::Response(nsISupports* aOwner)
+  : mOwner(aOwner)
+  , mHeaders(new Headers(aOwner))
+{
+  SetIsDOMBinding();
+}
+
+Response::~Response()
+{
+}
+
+/* static */ already_AddRefed<Response>
+Response::Error(const GlobalObject& aGlobal)
+{
+  ErrorResult result;
+  ResponseInit init;
+  init.mStatus = 0;
+  Optional<ArrayBufferOrArrayBufferViewOrScalarValueStringOrURLSearchParams> body;
+  nsRefPtr<Response> r = Response::Constructor(aGlobal, body, init, result);
+  return r.forget();
+}
+
+/* static */ already_AddRefed<Response>
+Response::Redirect(const GlobalObject& aGlobal, const nsAString& aUrl,
+                   uint16_t aStatus)
+{
+  ErrorResult result;
+  ResponseInit init;
+  Optional<ArrayBufferOrArrayBufferViewOrScalarValueStringOrURLSearchParams> body;
+  nsRefPtr<Response> r = Response::Constructor(aGlobal, body, init, result);
+  return r.forget();
+}
+
+/*static*/ already_AddRefed<Response>
+Response::Constructor(const GlobalObject& global,
+                      const Optional<ArrayBufferOrArrayBufferViewOrScalarValueStringOrURLSearchParams>& aBody,
+                      const ResponseInit& aInit, ErrorResult& rv)
+{
+  nsRefPtr<Response> response = new Response(global.GetAsSupports());
+  return response.forget();
+}
+
+already_AddRefed<Response>
+Response::Clone()
+{
+  nsRefPtr<Response> response = new Response(mOwner);
+  return response.forget();
+}
+
+already_AddRefed<Promise>
+Response::ArrayBuffer(ErrorResult& aRv)
+{
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
+  MOZ_ASSERT(global);
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
+  return promise.forget();
+}
+
+already_AddRefed<Promise>
+Response::Blob(ErrorResult& aRv)
+{
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
+  MOZ_ASSERT(global);
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
+  return promise.forget();
+}
+
+already_AddRefed<Promise>
+Response::Json(ErrorResult& aRv)
+{
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
+  MOZ_ASSERT(global);
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
+  return promise.forget();
+}
+
+already_AddRefed<Promise>
+Response::Text(ErrorResult& aRv)
+{
+  nsCOMPtr<nsIGlobalObject> global = do_QueryInterface(GetParentObject());
+  MOZ_ASSERT(global);
+  nsRefPtr<Promise> promise = Promise::Create(global, aRv);
+  if (aRv.Failed()) {
+    return nullptr;
+  }
+
+  promise->MaybeReject(NS_ERROR_NOT_AVAILABLE);
+  return promise.forget();
+}
+
+bool
+Response::BodyUsed()
+{
+  return false;
+}
+} // namespace dom
+} // namespace mozilla
new file mode 100644
--- /dev/null
+++ b/dom/fetch/Response.h
@@ -0,0 +1,108 @@
+/* -*- Mode: C++; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this
+ * file, You can obtain one at http://mozilla.org/MPL/2.0/. */
+
+#ifndef mozilla_dom_Response_h
+#define mozilla_dom_Response_h
+
+#include "nsWrapperCache.h"
+#include "nsISupportsImpl.h"
+
+#include "mozilla/dom/ResponseBinding.h"
+#include "mozilla/dom/UnionTypes.h"
+
+class nsPIDOMWindow;
+
+namespace mozilla {
+namespace dom {
+
+class Headers;
+class Promise;
+
+class Response MOZ_FINAL : public nsISupports
+                         , public nsWrapperCache
+{
+  NS_DECL_CYCLE_COLLECTING_ISUPPORTS
+  NS_DECL_CYCLE_COLLECTION_SCRIPT_HOLDER_CLASS(Response)
+
+public:
+  Response(nsISupports* aOwner);
+
+  JSObject*
+  WrapObject(JSContext* aCx)
+  {
+    return ResponseBinding::Wrap(aCx, this);
+  }
+
+  ResponseType
+  Type() const
+  {
+    return ResponseType::Error;
+  }
+
+  void
+  GetUrl(DOMString& aUrl) const
+  {
+    aUrl.AsAString() = EmptyString();
+  }
+
+  uint16_t
+  Status() const
+  {
+    return 400;
+  }
+
+  void
+  GetStatusText(nsCString& aStatusText) const
+  {
+    aStatusText = EmptyCString();
+  }
+
+  Headers*
+  Headers_() const { return mHeaders; }
+
+  static already_AddRefed<Response>
+  Error(const GlobalObject& aGlobal);
+
+  static already_AddRefed<Response>
+  Redirect(const GlobalObject& aGlobal, const nsAString& aUrl, uint16_t aStatus);
+
+  static already_AddRefed<Response>
+  Constructor(const GlobalObject& aGlobal,
+              const Optional<ArrayBufferOrArrayBufferViewOrScalarValueStringOrURLSearchParams>& aBody,
+              const ResponseInit& aInit, ErrorResult& rv);
+
+  nsISupports* GetParentObject() const
+  {
+    return mOwner;
+  }
+
+  already_AddRefed<Response>
+  Clone();
+
+  already_AddRefed<Promise>
+  ArrayBuffer(ErrorResult& aRv);
+
+  already_AddRefed<Promise>
+  Blob(ErrorResult& aRv);
+
+  already_AddRefed<Promise>
+  Json(ErrorResult& aRv);
+
+  already_AddRefed<Promise>
+  Text(ErrorResult& aRv);
+
+  bool
+  BodyUsed();
+private:
+  ~Response();
+
+  nsCOMPtr<nsISupports> mOwner;
+  nsRefPtr<Headers> mHeaders;
+};
+
+} // namespace dom
+} // namespace mozilla
+
+#endif // mozilla_dom_Response_h
--- a/dom/fetch/moz.build
+++ b/dom/fetch/moz.build
@@ -2,21 +2,23 @@
 # vim: set filetype=python:
 # This Source Code Form is subject to the terms of the Mozilla Public
 # License, v. 2.0. If a copy of the MPL was not distributed with this
 # file, You can obtain one at http://mozilla.org/MPL/2.0/.
 
 EXPORTS.mozilla.dom += [
     'Headers.h',
     'Request.h',
+    'Response.h',
 ]
 
 UNIFIED_SOURCES += [
     'Headers.cpp',
     'Request.cpp',
+    'Response.cpp',
 ]
 
 LOCAL_INCLUDES += [
     '../workers',
 ]
 
 FAIL_ON_WARNINGS = True
 MSVC_ENABLE_PGO = True
--- a/dom/tests/mochitest/general/test_interfaces.html
+++ b/dom/tests/mochitest/general/test_interfaces.html
@@ -860,16 +860,18 @@ var interfaceNamesInGlobalScope =
     "Range",
 // IMPORTANT: Do not change this list without review from a DOM peer!
     "RecordErrorEvent",
 // IMPORTANT: Do not change this list without review from a DOM peer!
     "Rect",
 // IMPORTANT: Do not change this list without review from a DOM peer!
     {name: "Request", pref: "dom.fetch.enabled"},
 // IMPORTANT: Do not change this list without review from a DOM peer!
+    {name: "Response", pref: "dom.fetch.enabled"},
+// IMPORTANT: Do not change this list without review from a DOM peer!
     "RGBColor",
 // IMPORTANT: Do not change this list without review from a DOM peer!
     {name: "RTCDataChannelEvent", pref: "media.peerconnection.enabled"},
 // IMPORTANT: Do not change this list without review from a DOM peer!
     {name: "RTCPeerConnectionIceEvent", pref: "media.peerconnection.enabled"},
 // IMPORTANT: Do not change this list without review from a DOM peer!
     {name: "RTCRtpReceiver", pref: "media.peerconnection.enabled"},
 // IMPORTANT: Do not change this list without review from a DOM peer!
new file mode 100644
--- /dev/null
+++ b/dom/webidl/Response.webidl
@@ -0,0 +1,36 @@
+/* -*- Mode: IDL; tab-width: 2; indent-tabs-mode: nil; c-basic-offset: 2 -*- */
+/* This Source Code Form is subject to the terms of the Mozilla Public
+ * License, v. 2.0. If a copy of the MPL was not distributed with this file,
+ * You can obtain one at http://mozilla.org/MPL/2.0/.
+ *
+ * The origin of this IDL file is
+ * https://fetch.spec.whatwg.org/#response-class
+ */
+
+[Constructor(optional BodyInit body, optional ResponseInit init),
+ Exposed=(Window,Worker),
+ Func="mozilla::dom::Headers::PrefEnabled"]
+interface Response {
+  static Response error();
+  static Response redirect(ScalarValueString url, optional unsigned short status = 302);
+
+  readonly attribute ResponseType type;
+
+  readonly attribute ScalarValueString url;
+  readonly attribute unsigned short status;
+  readonly attribute ByteString statusText;
+  readonly attribute Headers headers;
+
+  Response clone();
+};
+
+Response implements Body;
+
+dictionary ResponseInit {
+  unsigned short status = 200;
+  // WebIDL spec doesn't allow default values for ByteString.
+  ByteString statusText;
+  HeadersInit headers;
+};
+
+enum ResponseType { "basic", "cors", "default", "error", "opaque" };
--- a/dom/webidl/moz.build
+++ b/dom/webidl/moz.build
@@ -325,16 +325,17 @@ WEBIDL_FILES = [
     'PromiseDebugging.webidl',
     'PushManager.webidl',
     'RadioNodeList.webidl',
     'Range.webidl',
     'Rect.webidl',
     'Request.webidl',
     'ResourceStats.webidl',
     'ResourceStatsManager.webidl',
+    'Response.webidl',
     'RGBColor.webidl',
     'RTCConfiguration.webidl',
     'RTCIceCandidate.webidl',
     'RTCIdentityAssertion.webidl',
     'RTCPeerConnection.webidl',
     'RTCPeerConnectionStatic.webidl',
     'RTCRtpReceiver.webidl',
     'RTCRtpSender.webidl',
--- a/dom/workers/test/fetch/worker_interfaces.js
+++ b/dom/workers/test/fetch/worker_interfaces.js
@@ -1,10 +1,11 @@
 function ok(a, msg) {
   dump("OK: " + !!a + "  =>  " + a + " " + msg + "\n");
   postMessage({type: 'status', status: !!a, msg: a + ": " + msg });
 }
 
 onmessage = function() {
   ok(typeof Headers === "function", "Headers should be defined");
   ok(typeof Request === "function", "Request should be defined");
+  ok(typeof Response === "function", "Response should be defined");
   postMessage({ type: 'finish' });
 }