Bug 1665426 - Implement fetch flag for private browsing downloads in WebExecutor r=geckoview-reviewers,agi,snorp

Differential Revision: https://phabricator.services.mozilla.com/D93044
This commit is contained in:
owlishDeveloper 2020-10-13 17:59:49 +00:00
parent 19bd39d8da
commit 532e1b8155
5 changed files with 56 additions and 6 deletions

View File

@ -1324,6 +1324,7 @@ package org.mozilla.geckoview {
field public static final int FETCH_FLAGS_ANONYMOUS = 1;
field public static final int FETCH_FLAGS_NONE = 0;
field public static final int FETCH_FLAGS_NO_REDIRECTS = 2;
field public static final int FETCH_FLAGS_PRIVATE = 8;
field public static final int FETCH_FLAGS_STREAM_FAILURE_TEST = 1024;
}

View File

@ -252,9 +252,24 @@ class WebExecutorTest {
}
@Test
fun testAnonymous() {
fun testAnonymousSendCookies() {
val uptimeMillis = SystemClock.uptimeMillis()
val response = fetch(WebRequest("$TEST_ENDPOINT/cookies/set/uptimeMillis/$uptimeMillis"), GeckoWebExecutor.FETCH_FLAGS_ANONYMOUS)
// We get redirected to /cookies which returns the cookies that were sent in the request
assertThat("URI should match", response.uri, equalTo("$TEST_ENDPOINT/cookies"))
assertThat("Status code should match", response.statusCode, equalTo(200))
val body = response.getJSONBody()
assertThat("Cookies should not be set for the test server",
body.getJSONObject("cookies").length(),
equalTo(0))
}
@Test
fun testAnonymousGetCookies() {
// Ensure a cookie is set for the test server
testCookies();
testCookies()
val response = fetch(WebRequest("$TEST_ENDPOINT/cookies"),
GeckoWebExecutor.FETCH_FLAGS_ANONYMOUS)
@ -264,6 +279,31 @@ class WebExecutorTest {
assertThat("Cookies should be empty", cookies.length(), equalTo(0))
}
@Test
fun testPrivateCookies() {
val uptimeMillis = SystemClock.uptimeMillis()
val response = fetch(WebRequest("$TEST_ENDPOINT/cookies/set/uptimeMillis/$uptimeMillis"), GeckoWebExecutor.FETCH_FLAGS_PRIVATE)
// We get redirected to /cookies which returns the cookies that were sent in the request
assertThat("URI should match", response.uri, equalTo("$TEST_ENDPOINT/cookies"))
assertThat("Status code should match", response.statusCode, equalTo(200))
val body = response.getJSONBody()
assertThat("Cookies should be set for the test server",
body.getJSONObject("cookies").getString("uptimeMillis"),
equalTo(uptimeMillis.toString()))
val anotherBody = fetch(WebRequest("$TEST_ENDPOINT/cookies"), GeckoWebExecutor.FETCH_FLAGS_PRIVATE).getJSONBody()
assertThat("Body should match",
anotherBody.getJSONObject("cookies").getString("uptimeMillis"),
equalTo(uptimeMillis.toString()))
val yetAnotherBody = fetch(WebRequest("$TEST_ENDPOINT/cookies")).getJSONBody()
assertThat("Cookies set in private session are not supposed to be seen in normal download",
yetAnotherBody.getJSONObject("cookies").length(),
equalTo(0))
}
@Test
fun testSpeculativeConnect() {
// We don't have a way to know if it succeeds or not, but at least we can ensure

View File

@ -89,12 +89,11 @@ public class GeckoWebExecutor {
@WrapForJNI
/* package */ static final int FETCH_FLAGS_ALLOW_SOME_ERRORS = 1 << 2;
// TODO: implement in WebExecutorSupport and make public bug 1538348
/**
* Associates this download with a private browsing session
* Associates this download with the current private browsing session
*/
@WrapForJNI
/* package */ static final int FETCH_FLAGS_PRIVATE = 1 << 3;
public static final int FETCH_FLAGS_PRIVATE = 1 << 3;
/**
* This flag causes a read error in the {@link WebResponse} body. Useful for testing.

View File

@ -29,11 +29,14 @@ exclude: true
- Added [`GeckoRuntime.ActivityDelegate`][83.4] which allows applications to handle
starting external Activities on behalf of GeckoView. Currently this is used to integrate
FIDO support for WebAuthn.
- Added ['GeckoWebExecutor#FETCH_FLAG_PRIVATE'][83.5]. This new flag allows for private browsing downloads using WebExecutor.
([bug 1665426]({{bugzilla}}1665426))
[83.1]: {{javadoc_uri}}/WebExtension.MetaData.html#temporary
[83.2]: {{javadoc_uri}}/MediaSession.Delegate.html#onMetadata-org.mozilla.geckoview.GeckoSession-org.mozilla.geckoview.MediaSession-org.mozilla.geckoview.MediaSession.Metadata-
[83.3]: {{javadoc_uri}}/ContentBlocking.SafeBrowsingProvider.html
[83.4]: {{javadoc_uri}}/GeckoRuntime.ActivityDelegate.html
[83.5]: {{javadoc_uri}}/GeckoWebExecutor.html#FETCH_FLAG_PRIVATE
## v82
- ⚠️ [`WebNotification.source`][79.2] is now `@Nullable` to account for
@ -823,4 +826,4 @@ to allow adding gecko profiler markers.
[65.24]: {{javadoc_uri}}/CrashReporter.html#sendCrashReport-android.content.Context-android.os.Bundle-java.lang.String-
[65.25]: {{javadoc_uri}}/GeckoResult.html
[api-version]: d3186b70503f71a6476c765f76c6d49f9a1fb282
[api-version]: b55b29412d4d11b9a62dfd71f60568d1286b1e7b

View File

@ -25,6 +25,7 @@
#include "mozilla/net/CookieJarSettings.h"
#include "mozilla/Preferences.h"
#include "GeckoViewStreamListener.h"
#include "nsIPrivateBrowsingChannel.h"
#include "nsNetUtil.h" // for NS_NewURI, NS_NewChannel, NS_NewStreamLoader
@ -378,6 +379,12 @@ nsresult WebExecutorSupport::CreateStreamLoader(
channel->SetLoadFlags(nsIRequest::LOAD_ANONYMOUS);
}
if (aFlags & java::GeckoWebExecutor::FETCH_FLAGS_PRIVATE) {
nsCOMPtr<nsIPrivateBrowsingChannel> pbChannel = do_QueryInterface(channel);
NS_ENSURE_TRUE(pbChannel, NS_ERROR_FAILURE);
pbChannel->SetPrivate(true);
}
nsCOMPtr<nsICookieJarSettings> cookieJarSettings =
CookieJarSettings::Create();
MOZ_ASSERT(cookieJarSettings);