Bug 1147699 - Part 1: Move Request::mContext to InternalRequest, and determine the mapping to nsContentPolicyType; r=nsm

As the documentation in InternalRequest.h in this patch shows, the
mapping between nsContentPolicyType and RequestContext is not complete
yet.  Because the InternalRequest object needs to know the actual
nsContentPolicyType in order for FetchDriver to be able to use that
information, we can't just store the RequestContext.  Therefore, this
patch adds both of these to InternalRequest.  Once we get to a stage
where we have a complete mapping of these values, we can store only one
of them and compute the other from it.  That requires addressing all of
the TODO comments in the InternalRequest.h documentation.
This commit is contained in:
Ehsan Akhgari 2015-03-25 16:38:42 -04:00
parent 8c949bf377
commit cd8bb1e29a
8 changed files with 174 additions and 11 deletions

View File

@ -11,6 +11,7 @@ using HeadersGuardEnum from "mozilla/dom/FetchIPCUtils.h";
using RequestCredentials from "mozilla/dom/FetchIPCUtils.h";
using RequestMode from "mozilla/dom/FetchIPCUtils.h";
using RequestCache from "mozilla/dom/FetchIPCUtils.h";
using RequestContext from "mozilla/dom/FetchIPCUtils.h";
using mozilla::dom::ResponseType from "mozilla/dom/FetchIPCUtils.h";
using mozilla::void_t from "ipc/IPCMessageUtils.h";
using struct nsID from "nsID.h";
@ -54,7 +55,8 @@ struct PCacheRequest
RequestMode mode;
RequestCredentials credentials;
PCacheReadStreamOrVoid body;
uint32_t context;
uint32_t contentPolicyType;
RequestContext context;
RequestCache requestCache;
};

View File

@ -224,7 +224,8 @@ TypeUtils::ToPCacheRequest(PCacheRequest& aOut, InternalRequest* aIn,
aOut.headersGuard() = headers->Guard();
aOut.mode() = aIn->Mode();
aOut.credentials() = aIn->GetCredentialsMode();
aOut.context() = aIn->ContentPolicyType();
aOut.contentPolicyType() = aIn->ContentPolicyType();
aOut.context() = aIn->Context();
aOut.requestCache() = aIn->GetCacheMode();
if (aBodyAction == IgnoreBody) {
@ -367,7 +368,11 @@ TypeUtils::ToInternalRequest(const PCacheRequest& aIn)
internalRequest->SetReferrer(aIn.referrer());
internalRequest->SetMode(aIn.mode());
internalRequest->SetCredentialsMode(aIn.credentials());
internalRequest->SetContentPolicyType(aIn.context());
internalRequest->SetContentPolicyType(aIn.contentPolicyType());
DebugOnly<RequestContext> contextAfterSetContentPolicyType = internalRequest->Context();
internalRequest->SetContext(aIn.context());
MOZ_ASSERT(contextAfterSetContentPolicyType.value == internalRequest->Context(),
"The RequestContext and nsContentPolicyType values should not get out of sync");
internalRequest->SetCacheMode(aIn.requestCache());
nsRefPtr<InternalHeaders> internalHeaders =

View File

@ -37,6 +37,11 @@ namespace IPC {
mozilla::dom::RequestCache::Default,
mozilla::dom::RequestCache::EndGuard_> {};
template<>
struct ParamTraits<mozilla::dom::RequestContext> :
public ContiguousEnumSerializer<mozilla::dom::RequestContext,
mozilla::dom::RequestContext::Audio,
mozilla::dom::RequestContext::EndGuard_> {};
template<>
struct ParamTraits<mozilla::dom::ResponseType> :
public ContiguousEnumSerializer<mozilla::dom::ResponseType,
mozilla::dom::ResponseType::Basic,

View File

@ -38,6 +38,7 @@ InternalRequest::GetRequestConstructorCopy(nsIGlobalObject* aGlobal, ErrorResult
// The default referrer is already about:client.
copy->mContentPolicyType = nsIContentPolicy::TYPE_FETCH;
copy->mContext = RequestContext::Fetch;
copy->mMode = mMode;
copy->mCredentialsMode = mCredentialsMode;
copy->mCacheMode = mCacheMode;
@ -74,6 +75,7 @@ InternalRequest::InternalRequest(const InternalRequest& aOther)
, mURL(aOther.mURL)
, mHeaders(new InternalHeaders(*aOther.mHeaders))
, mContentPolicyType(aOther.mContentPolicyType)
, mContext(aOther.mContext)
, mReferrer(aOther.mReferrer)
, mMode(aOther.mMode)
, mCredentialsMode(aOther.mCredentialsMode)
@ -97,5 +99,80 @@ InternalRequest::~InternalRequest()
{
}
void
InternalRequest::SetContentPolicyType(nsContentPolicyType aContentPolicyType)
{
mContentPolicyType = aContentPolicyType;
switch (aContentPolicyType) {
case nsIContentPolicy::TYPE_OTHER:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_SCRIPT:
mContext = RequestContext::Script;
break;
case nsIContentPolicy::TYPE_IMAGE:
mContext = RequestContext::Image;
break;
case nsIContentPolicy::TYPE_STYLESHEET:
mContext = RequestContext::Style;
break;
case nsIContentPolicy::TYPE_OBJECT:
mContext = RequestContext::Object;
break;
case nsIContentPolicy::TYPE_DOCUMENT:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_SUBDOCUMENT:
mContext = RequestContext::Iframe;
break;
case nsIContentPolicy::TYPE_REFRESH:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_XBL:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_PING:
mContext = RequestContext::Ping;
break;
case nsIContentPolicy::TYPE_XMLHTTPREQUEST:
mContext = RequestContext::Xmlhttprequest;
break;
case nsIContentPolicy::TYPE_OBJECT_SUBREQUEST:
mContext = RequestContext::Plugin;
break;
case nsIContentPolicy::TYPE_DTD:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_FONT:
mContext = RequestContext::Font;
break;
case nsIContentPolicy::TYPE_MEDIA:
mContext = RequestContext::Audio;
break;
case nsIContentPolicy::TYPE_WEBSOCKET:
mContext = RequestContext::Internal;
break;
case nsIContentPolicy::TYPE_CSP_REPORT:
mContext = RequestContext::Cspreport;
break;
case nsIContentPolicy::TYPE_XSLT:
mContext = RequestContext::Xslt;
break;
case nsIContentPolicy::TYPE_BEACON:
mContext = RequestContext::Beacon;
break;
case nsIContentPolicy::TYPE_FETCH:
mContext = RequestContext::Fetch;
break;
case nsIContentPolicy::TYPE_IMAGESET:
mContext = RequestContext::Imageset;
break;
default:
MOZ_ASSERT(false, "Unhandled nsContentPolicyType value");
mContext = RequestContext::Internal;
break;
}
}
} // namespace dom
} // namespace mozilla

View File

@ -25,6 +25,65 @@ class nsPIDOMWindow;
namespace mozilla {
namespace dom {
/*
* The mapping of RequestContext and nsContentPolicyType is currently as the
* following. Note that this mapping is not perfect yet (see the TODO comments
* below for examples), so for now we'll have to keep both an mContext and an
* mContentPolicyType, because we cannot have a two way conversion.
*
* RequestContext | nsContentPolicyType
* ------------------+--------------------
* audio | TYPE_MEDIA
* beacon | TYPE_BEACON
* cspreport | TYPE_CSP_REPORT
* download |
* embed | TYPE_OBJECT
* eventsource |
* favicon |
* fetch | TYPE_FETCH
* font | TYPE_FONT
* form |
* frame | TYPE_SUBDOCUMENT
* hyperlink |
* iframe | TYPE_SUBDOCUMENT
* image | TYPE_IMAGE
* imageset | TYPE_IMAGESET
* import | Not supported by Gecko
* internal | TYPE_DOCUMENT, TYPE_XBL, TYPE_OTHER
* location |
* manifest |
* object | TYPE_OBJECT
* ping | TYPE_PING
* plugin | TYPE_OBJECT_SUBREQUEST
* prefetch |
* script | TYPE_SCRIPT
* serviceworker |
* sharedworker |
* subresource | Not supported by Gecko
* style | TYPE_STYLESHEET
* track | TYPE_MEDIA
* video | TYPE_MEDIA
* worker |
* xmlhttprequest | TYPE_XMLHTTPREQUEST
* xslt | TYPE_XSLT
*
* TODO: Figure out if TYPE_REFRESH maps to anything useful
* TODO: Figure out if TYPE_DTD maps to anything useful
* TODO: Split TYPE_MEDIA into TYPE_AUDIO, TYPE_VIDEO and TYPE_TRACK
* TODO: Split TYPE_XMLHTTPREQUEST and TYPE_DATAREQUEST for EventSource
* TODO: Figure out if TYPE_WEBSOCKET maps to anything useful
* TODO: Differentiate between frame and iframe
* TODO: Add content types for different kinds of workers
* TODO: Add a content type for prefetch
* TODO: Use the content type for manifest when it becomes available
* TODO: Add a content type for location
* TODO: Add a content type for hyperlink
* TODO: Add a content type for form
* TODO: Add a content type for favicon
* TODO: Add a content type for download
* TODO: Split TYPE_OBJECT into TYPE_EMBED and TYPE_OBJECT
*/
class FetchBodyStream;
class Request;
@ -221,9 +280,18 @@ public:
}
void
SetContentPolicyType(nsContentPolicyType aContentPolicyType)
SetContentPolicyType(nsContentPolicyType aContentPolicyType);
RequestContext
Context() const
{
mContentPolicyType = aContentPolicyType;
return mContext;
}
void
SetContext(RequestContext aContext)
{
mContext = aContext;
}
bool
@ -312,9 +380,8 @@ private:
nsRefPtr<InternalHeaders> mHeaders;
nsCOMPtr<nsIInputStream> mBodyStream;
// nsContentPolicyType does not cover the complete set defined in the spec,
// but it is a good start.
nsContentPolicyType mContentPolicyType;
RequestContext mContext;
// Empty string: no-referrer
// "about:client": client (default)

View File

@ -33,7 +33,6 @@ Request::Request(nsIGlobalObject* aOwner, InternalRequest* aRequest)
: FetchBody<Request>()
, mOwner(aOwner)
, mRequest(aRequest)
, mContext(RequestContext::Fetch)
{
}

View File

@ -78,14 +78,20 @@ public:
RequestContext
Context() const
{
return mContext;
return mRequest->Context();
}
// [ChromeOnly]
void
SetContext(RequestContext aContext)
{
mContext = aContext;
mRequest->SetContext(aContext);
}
void
SetContentPolicyType(nsContentPolicyType aContentPolicyType)
{
mRequest->SetContentPolicyType(aContentPolicyType);
}
void
@ -126,7 +132,6 @@ private:
nsRefPtr<InternalRequest> mRequest;
// Lazily created.
nsRefPtr<Headers> mHeaders;
RequestContext mContext;
};
} // namespace dom

View File

@ -23,5 +23,8 @@
testFetch()
.then(function() {
finish();
}, function(e) {
ok(false, "A promise was rejected: " + e);
finish();
});
</script>