Closed Bug 1402892 Opened 7 years ago Closed 6 years ago

Implement Fetch API RequestDestination and Request.destination

Categories

(Core :: DOM: Core & HTML, enhancement, P2)

57 Branch
enhancement

Tracking

()

RESOLVED FIXED
mozilla61
Tracking Status
firefox61 --- fixed

People

(Reporter: bkelly, Assigned: catalinb)

References

(Blocks 1 open bug)

Details

(Keywords: dev-doc-complete)

Attachments

(3 files, 6 obsolete files)

Currently we have a disabled implementation of the old Request.context:

https://searchfox.org/mozilla-central/source/dom/webidl/Request.webidl#21

We should eventually implement Request.destination.

https://fetch.spec.whatwg.org/#dom-request-destination
Priority: -- → P2
Can we add this to our planning for first half next year?  Developers have begun to ask for it, like in:

https://github.com/whatwg/fetch/issues/634

(Or should I just clear the priority to get it re-triaged?  I don't know how this works...)
Flags: needinfo?(htsai)
(In reply to Ben Kelly [:bkelly] from comment #1)
> Can we add this to our planning for first half next year?  Developers have
> begun to ask for it, like in:
> 
> https://github.com/whatwg/fetch/issues/634

Noted. Let me take that into 2018 planning consideration, thanks Ben.

> 
> (Or should I just clear the priority to get it re-triaged?  I don't know how
> this works...)

Needinfo works very well, though either way should do. :)
Flags: needinfo?(htsai)
Eden is planning to put this into his working queue. Thanks!
Assignee: nobody → echuang
Flags: needinfo?(catalin.badea392)
Attached patch Implement Request.destination. (obsolete) — Splinter Review
Attachment #8959120 - Flags: review?(bugmail)
Attachment #8958118 - Attachment is obsolete: true
Attachment #8958119 - Attachment is obsolete: true
Attached patch Implement Request.destination. (obsolete) — Splinter Review
Attachment #8959121 - Flags: review?(bugmail)
Flags: needinfo?(catalin.badea392)
Assignee: echuang → catalin.badea392
Status: NEW → ASSIGNED
:ckerschb, I could use some guidance on this bug.  The situation as I understand it is:

- CSP defines a certain set of policy types, one of which is "media".
- We define a set of additional types of INTERNAL types that originate from within Gecko, such as when <audio> or <video> tags are encountered.  These get down-mapped to CSP types when dealt with "externally".  For example, we map TYPE_INTERNAL_{AUDIO,VIDEO,TRACK} to TYPE_MEDIA.
- We use the same type "nsContentPolicyType" for both internal and external types.  nsILoadInfo distinguishes between the two types.  It only stores mInternalContentPolicyType, but its `externalContentPolicyType` getter uses nsContentUtils::InternalContentPolicyTypeToExternal to return an external value which is guaranteed to not be one of the INTERNAL constants.  Unfortunately, most variables of type nsContentPolicy do not indicate whether they are expected to be internal/external.

- fetch defines a set of "destination" types at https://fetch.spec.whatwg.org/#concept-request-destination that are more specific than those CSP understands.  Specifically, there are distinct "audio", "video", and "track" destinations.
- It's possible to map these destinations to/from our nsContentPolicyType values, but things get awkward because INTERNAL no longer makes a lot of sense.
- Preload added a <link as="fetch-destination"> thing.
  - Link::ParseAsValue maps from a subset of these strings to Link::ASDestination enums.
  - Link::AsValueToContentPolicy maps from these DESTIONATION_* enum values to nsContentPolicyType values, flattening all of DESTINATION_{AUDIO,TRACK,VIDEO} to nsIContentPolicy::TYPE_MEDIA.

In order to implement Request.destination and make us conformant with existing WPT tests, these patches make Link::AsValueToContentPolicy map to internal types as the path of least resistance.  Unfortunately, it appears that there are areas in the code where TYPE_MEDIA is used, TYPE_INTERNAL_{AUDIO,VIDEO,TRACK} are not, but the INTERNAL script types are.  My first instinct would be to expand the cases to include the internal media types as well, but I'm worried that might be making the situation worse.

My question is:
- What should we be doing about the destination types versus the CSP types?  Should the internal nsContentPolicyTypes become a completely distinct type?  Practically speaking, I think this means nsInternalContentType would be introduced and all its values would all have an INTERNAL prefix.
- If not that, should we just ensure that every switch-statement-like thing that's not dealing with nsILoadInfo and guaranteed to have a notion of internal/external therefore have complete coverage of all of the nsContentPolicyType cases?
Flags: needinfo?(ckerschb)
Comment on attachment 8959121 [details] [diff] [review]
Implement Request.destination.

Review of attachment 8959121 [details] [diff] [review]:
-----------------------------------------------------------------

:baku, can you sign off on the WebIDL changes (and I guess notionally delegate review to me for these patches)?  I confirm they're consistent with what's currently in https://fetch.spec.whatwg.org/#idl-index

:catalinb, please also remove the pref from the default prefs file at https://searchfox.org/mozilla-central/rev/de5c4376b89c3603341d226a65acea12f8851ec5/modules/libpref/init/all.js#5877.

And the tests where it gets set:
- https://searchfox.org/mozilla-central/rev/de5c4376b89c3603341d226a65acea12f8851ec5/dom/serviceworkers/test/test_not_intercept_plugin.html#68
- https://searchfox.org/mozilla-central/rev/de5c4376b89c3603341d226a65acea12f8851ec5/dom/serviceworkers/test/test_request_context.js#67
- https://searchfox.org/mozilla-central/rev/de5c4376b89c3603341d226a65acea12f8851ec5/dom/tests/mochitest/fetch/fetch_test_framework.js#17

::: dom/base/Link.cpp
@@ -944,2 @@
>    case DESTINATION_VIDEO:
> -    return nsIContentPolicy::TYPE_MEDIA;

I've needinfo'ed :ckerschb about the meta-issue here, although I think I'm now okay with this change.  It's all just very complicated and confusing because the nsContentPolicyType seems to conflate three sets of semantics into a single type.

::: dom/fetch/InternalRequest.cpp
@@ +230,5 @@
>    SetContentPolicyType(aContentPolicyType);
>    mContentPolicyTypeOverridden = true;
>  }
>  
> +/* static */ RequestDestination

It would be great to add an explanation of the relationship between this method, its return type, and the private Link::ASDestination enum type at https://searchfox.org/mozilla-central/rev/57bbc1ac58816dc054df242948f3ecf75e12df5f/dom/base/Link.h#200

Based on my understanding, perhaps something like this:

"""
Map the content policy type to the associated fetch destination, as defined by the spec at https://fetch.spec.whatwg.org/#concept-request-destination.  Note that while the HTML spec for the "Link" element and its "as" attribute (https://html.spec.whatwg.org/#attr-link-as) reuse fetch's definition of destination, and the Link class has an internal Link::ASDestination enum type, the latter is only a support type to map the string values via Link::ParseAsValue and Link::AsValueToContentPolicy to our canonical nsContentPolicyType.
"""

::: dom/fetch/InternalRequest.h
@@ +44,2 @@
>   * font              | TYPE_FONT
> + * image             | TYPE_INTERNAL_IMAGE, TYPE_INTERNAL_IMAGE_PRELOAD,

image is missing TYPE_IMAGE

@@ +47,5 @@
>   * manifest          | TYPE_WEB_MANIFEST
> + * object            | TYPE_INTERNAL_OBJECT, TYPE_OBJECT
> + * "paintworklet"    | TODO
> + * report"           | TODO
> + * script            | TYPE_INTERNAL_SCRIPT, TYPE_INTERNAL_SCRIPT_PRELOAD,

script is missing TYPE_SCRIPT

@@ +53,1 @@
>   * sharedworker      | TYPE_INTERNAL_SHARED_WORKER

There should probably be a "serviceworker" entry here with a TODO or "impossible" or something like that, given the presence of the entry in the fetch spec.

::: testing/web-platform/tests/fetch/api/request/destination/resources/fetch-destination-worker-no-load-event.js
@@ +6,4 @@
>              if (event.request.destination == destination) {
> +              result = "PASS";
> +            }
> +            let cl = await clients.matchAll({includeUncontrolled: true});

It's not clear why the changes to this file and fetch-destination-no-load-event.https.html were made.  Was this a temporary hack fix that snuck in the patch but is no longer required?

Restating my understanding of the test without the changes:
- The test pages (fetch-destination-no-load-event.https.html and also fetch-destination-worker.https.html) register this SW with the scope of an iframe it will create.
- The test pages wait for the SW to activate.
- The iframe is created and therefore should be controlled.  (And since the test works at all, this seems to be sound.)
- All test cases manipulate the iframe, so the SW should intercept the requests.
- The (unmodified) SW sends the results back to the client that triggered the fetch.
- Matching that, the tests add their message listener inside the iframe, so they will receive the message in the client.

Aside: Since the SW is dealing with a fetch event and its clientId field, bug 1446225 does come into play, but as noted there, although our id's are inconsistent in this case, clients.get() should make things work out.
Attachment #8959121 - Flags: review?(bugmail)
Attachment #8959121 - Flags: review?(amarchesini)
Attachment #8959121 - Flags: review+
Attachment #8959120 - Flags: review?(bugmail) → review+
Attachment #8959122 - Flags: review?(bugmail) → review+
Attachment #8959121 - Flags: review?(amarchesini) → review+
(In reply to Andrew Sutherland [:asuth] from comment #9)

Andrew, let me first provide some background information. Legacy extensions were able to hook into nsIContentPolicy API which allowed them to do all kinds of content blocking. Among others, that API exposed the content policy type to addons. Since legacy extensions were able to hook into that API we basically haven't touched it for years but needed some workarounds for dealing with the various types that we needed to distinguish within Firefox. E.g. CSP (which implements nsIContentPolicy) needed to handle script preloads differently, hence we started to add INTERNAL_SCRIPT_PRELOAD. To implementations within addons however, we only passed the external type. Now that legacy extensions are gone we updated the API and pass a loadinfo instead of the various arguments (see Bug 965637) which improves things drastically. Having that freedom allows use some other bit within the loadInfo to indicate a preload and not having to rely on the INTERNAL_SCRIPT_PRELOAD type if needed.

> My question is:
> - What should we be doing about the destination types versus the CSP types? 
CSP uses it's own mapping internally [1] which should be fairly easy to update depending on what we decide.
[1] https://dxr.mozilla.org/mozilla-central/source/dom/security/nsCSPUtils.cpp#207

> Should the internal nsContentPolicyTypes become a completely distinct type? 
> Practically speaking, I think this means nsInternalContentType would be
> introduced and all its values would all have an INTERNAL prefix.

I don't think that anything 'extern' can still access any of our content policy types. So there is no need to prefix 'INTERNAL'. In other words we have all the freedom we need as long as we update all of our own consumers of nsIContentPolicy to deal with the new types correctly.

> - If not that, should we just ensure that every switch-statement-like thing
> that's not dealing with nsILoadInfo and guaranteed to have a notion of
> internal/external therefore have complete coverage of all of the
> nsContentPolicyType cases?

Given what you are trying to accomplish within this bug, this second solution of fixing all the switch statements sounds more error prone to me. But one thing I don't understand. All channels should have a loadinfo by now and all desitinations are somehow based on a requests/channel right? What are the contests where we would need such switch statements? Would it make sense to expose a method on the Loadinfo like
  readonly attribute DestinationType destinationPolicyType;
or am I missing something?
Flags: needinfo?(ckerschb) → needinfo?(bugmail)
> It's not clear why the changes to this file and
> fetch-destination-no-load-event.https.html were made.  Was this a temporary
> hack fix that snuck in the patch but is no longer required?

The client creates a worker that loads importer.js
https://searchfox.org/mozilla-central/rev/a0665934fa05158a5a943d4c8b277465910c029c/testing/web-platform/tests/fetch/api/request/destination/resources/importer.js

This worker is the client which shows up in the intercepted request and gets the PASS/FAIL message from the service worker. I think this is related to bug 1451124.
Attachment #8959120 - Attachment is obsolete: true
Attachment #8959121 - Attachment is obsolete: true
Attachment #8959122 - Attachment is obsolete: true
This patch removes mochitests that were testing only request.context and
updates a couple others to use request.destination instead.

Carrying r+, :asuth was ok (on vidyo) with removing request.context specific
mochitests.
Pushed by catalin.badea392@gmail.com:
https://hg.mozilla.org/integration/mozilla-inbound/rev/34e219813b64
Enable fetch Request.destination webplatform tests. r=asuth
https://hg.mozilla.org/integration/mozilla-inbound/rev/2fce477733f0
Implement Request.destination. r=asuth,baku
https://hg.mozilla.org/integration/mozilla-inbound/rev/7218641c9b12
Update mochitests related to request.context. r=asuth
Attachment #8966247 - Attachment is obsolete: true
Pushed by catalin.badea392@gmail.com:
https://hg.mozilla.org/integration/mozilla-inbound/rev/2d761483af02
Enable fetch Request.destination webplatform tests. r=asuth
https://hg.mozilla.org/integration/mozilla-inbound/rev/284b35d145ff
Implement Request.destination. r=asuth,baku
https://hg.mozilla.org/integration/mozilla-inbound/rev/30dd7a04d53a
Update mochitests related to request.context. r=asuth
Created web-platform-tests PR https://github.com/w3c/web-platform-tests/pull/10425 for changes under testing/web-platform/tests
Pushed by catalin.badea392@gmail.com:
https://hg.mozilla.org/integration/mozilla-inbound/rev/aaafa0277f81
Update wpt expectations for request.destination. a=test-only CLOSED TREE
New pages:

https://developer.mozilla.org/en-US/docs/Web/API/RequestDestination

https://developer.mozilla.org/en-US/docs/Web/API/Request.destination

Updated page:

https://developer.mozilla.org/en-US/Firefox/Releases/61

I'm certain there's more that could be done here in terms of integrating the concept into the main body of the docs, but there are clarifications that need to be gathered, so this will do for now.
Oh - also, BCD and other database submissions have PRs filed awaiting review.
removing old need-info
Flags: needinfo?(bugmail)
Component: DOM → DOM: Core & HTML
You need to log in before you can comment on or make changes to this bug.

Attachment

General

Created:
Updated:
Size: