Bug 1017613 - Part 2 - Response IDL and stubs. r=baku

--HG--
extra : transplant_source : %AEc%0E%00%0D%92%AB.f%B5%EB%87%F7%C9%5DgX%93%AC%0A
This commit is contained in:
Nikhil Marathe 2014-07-24 17:50:32 -07:00
parent 98a91cf36f
commit 8f0bb32f46
8 changed files with 293 additions and 0 deletions

View File

@ -887,6 +887,10 @@ DOMInterfaces = {
'binaryNames': { 'headers': 'headers_' },
},
'Response': {
'binaryNames': { 'headers': 'headers_' },
},
'RGBColor': {
'nativeType': 'nsDOMCSSRGBColor',
},

139
dom/fetch/Response.cpp Normal file
View File

@ -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

108
dom/fetch/Response.h Normal file
View File

@ -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

View File

@ -7,11 +7,13 @@
EXPORTS.mozilla.dom += [
'Headers.h',
'Request.h',
'Response.h',
]
UNIFIED_SOURCES += [
'Headers.cpp',
'Request.cpp',
'Response.cpp',
]
LOCAL_INCLUDES += [

View File

@ -864,6 +864,8 @@ var interfaceNamesInGlobalScope =
"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!

View File

@ -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" };

View File

@ -330,6 +330,7 @@ WEBIDL_FILES = [
'Request.webidl',
'ResourceStats.webidl',
'ResourceStatsManager.webidl',
'Response.webidl',
'RGBColor.webidl',
'RTCConfiguration.webidl',
'RTCIceCandidate.webidl',

View File

@ -6,5 +6,6 @@ function ok(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' });
}