Bug 1607461 - [remote] Implement Network.setCacheDisabled. r=remote-protocol-reviewers,ato

Differential Revision: https://phabricator.services.mozilla.com/D58996

--HG--
extra : moz-landing-system : lando
This commit is contained in:
Henrik Skupin 2020-01-08 12:40:49 +00:00
parent 654ba476fb
commit 15e30b89a5
7 changed files with 175 additions and 0 deletions

View File

@ -17,6 +17,7 @@ XPCOMUtils.defineLazyModuleGetters(ContentProcessDomains, {
Emulation: "chrome://remote/content/domains/content/Emulation.jsm",
Input: "chrome://remote/content/domains/content/Input.jsm",
Log: "chrome://remote/content/domains/content/Log.jsm",
Network: "chrome://remote/content/domains/content/Network.jsm",
Page: "chrome://remote/content/domains/content/Page.jsm",
Performance: "chrome://remote/content/domains/content/Performance.jsm",
Runtime: "chrome://remote/content/domains/content/Runtime.jsm",

View File

@ -0,0 +1,24 @@
/* 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/. */
"use strict";
var EXPORTED_SYMBOLS = ["Network"];
const { ContentProcessDomain } = ChromeUtils.import(
"chrome://remote/content/domains/ContentProcessDomain.jsm"
);
class Network extends ContentProcessDomain {
// commands
/**
* Internal methods: the following methods are not part of CDP;
* note the _ prefix.
*/
_updateLoadFlags(flags) {
this.docShell.defaultLoadFlags = flags;
}
}

View File

@ -150,6 +150,26 @@ class Network extends Domain {
return { cookies };
}
/**
* Toggles ignoring cache for each request. If true, cache will not be used.
*
* @param {Object} options
* @param {boolean} options.cacheDisabled
* Cache disabled state.
*/
async setCacheDisabled(options = {}) {
const { cacheDisabled = false } = options;
const { INHIBIT_CACHING, LOAD_BYPASS_CACHE, LOAD_NORMAL } = Ci.nsIRequest;
let loadFlags = LOAD_NORMAL;
if (cacheDisabled) {
loadFlags = LOAD_BYPASS_CACHE | INHIBIT_CACHING;
}
await this.executeInChild("_updateLoadFlags", loadFlags);
}
/**
* Allows overriding user agent with the given string.
*

View File

@ -42,6 +42,7 @@ remote.jar:
content/domains/content/Emulation.jsm (domains/content/Emulation.jsm)
content/domains/content/Input.jsm (domains/content/Input.jsm)
content/domains/content/Log.jsm (domains/content/Log.jsm)
content/domains/content/Network.jsm (domains/content/Network.jsm)
content/domains/content/Page.jsm (domains/content/Page.jsm)
content/domains/content/Performance.jsm (domains/content/Performance.jsm)
content/domains/content/Runtime.jsm (domains/content/Runtime.jsm)

View File

@ -6,6 +6,7 @@ support-files =
!/remote/test/browser/chrome-remote-interface.js
!/remote/test/browser/head.js
head.js
doc_empty.html
doc_requestWillBeSent.html
file_requestWillBeSent.js
sjs-cookies.sjs
@ -13,3 +14,4 @@ support-files =
[browser_getCookies.js]
skip-if = (os == 'win' && os_version == '10.0' && ccov) # Bug 1605650
[browser_requestWillBeSent.js]
[browser_setCacheDisabled.js]

View File

@ -0,0 +1,118 @@
/* Any copyright is dedicated to the Public Domain.
* http://creativecommons.org/publicdomain/zero/1.0/ */
"use strict";
const { INHIBIT_CACHING, LOAD_BYPASS_CACHE, LOAD_NORMAL } = Ci.nsIRequest;
const TEST_PAGE =
"http://example.com/browser/remote/test/browser/network/doc_empty.html";
add_task(async function cacheEnabledAfterDisabled({ Network }) {
await Network.setCacheDisabled({ cacheDisabled: true });
await Network.setCacheDisabled({ cacheDisabled: false });
const checkPromise = checkLoadFlags(LOAD_NORMAL, TEST_PAGE);
await loadURL(TEST_PAGE);
await checkPromise;
});
add_task(async function cacheEnabledByDefault({ Network }) {
const checkPromise = checkLoadFlags(LOAD_NORMAL, TEST_PAGE);
await loadURL(TEST_PAGE);
await checkPromise;
});
add_task(async function cacheDisabled({ Network }) {
await Network.setCacheDisabled({ cacheDisabled: true });
const checkPromise = checkLoadFlags(
LOAD_BYPASS_CACHE | INHIBIT_CACHING,
TEST_PAGE
);
await loadURL(TEST_PAGE);
await checkPromise;
});
function checkLoadFlags(flags, url) {
return SpecialPowers.spawn(
gBrowser.selectedBrowser,
[flags, url],
async (flags, url) => {
// an nsIWebProgressListener that checks all requests made by the docShell
// have the flags we expect.
var RequestWatcher = {
init(docShell, expectedLoadFlags, url, callback) {
this.callback = callback;
this.docShell = docShell;
this.expectedLoadFlags = expectedLoadFlags;
this.url = url;
this.requestCount = 0;
const {
NOTIFY_STATE_DOCUMENT,
NOTIFY_STATE_REQUEST,
} = Ci.nsIWebProgress;
this.docShell
.QueryInterface(Ci.nsIInterfaceRequestor)
.getInterface(Ci.nsIWebProgress)
.addProgressListener(
this,
NOTIFY_STATE_DOCUMENT | NOTIFY_STATE_REQUEST
);
},
onStateChange(webProgress, request, flags, status) {
// We are checking requests - if there isn't one, ignore it.
if (!request) {
return;
}
// We will usually see requests for 'about:document-onload-blocker' not
// have the flag, so we just ignore them.
// We also see, eg, resource://gre-resources/loading-image.png, so
// skip resource:// URLs too.
// We may also see, eg, chrome://global/skin/icons/resizer.svg, so
// skip chrome:// URLs too.
if (
request.name.startsWith("about:") ||
request.name.startsWith("resource:") ||
request.name.startsWith("chrome:")
) {
return;
}
is(
request.loadFlags & this.expectedLoadFlags,
this.expectedLoadFlags,
"request " + request.name + " has the expected flags"
);
this.requestCount += 1;
var stopFlags =
Ci.nsIWebProgressListener.STATE_STOP |
Ci.nsIWebProgressListener.STATE_IS_DOCUMENT;
if (request.name == this.url && (flags & stopFlags) == stopFlags) {
this.docShell.removeProgressListener(this);
ok(
this.requestCount > 1,
this.url + " saw " + this.requestCount + " requests"
);
this.callback();
}
},
QueryInterface: ChromeUtils.generateQI([
Ci.nsIWebProgressListener,
Ci.nsISupportsWeakReference,
]),
};
await new Promise(resolve => {
RequestWatcher.init(docShell, flags, url, resolve);
});
}
);
}

View File

@ -0,0 +1,9 @@
<!DOCTYPE html>
<html>
<head>
<title>Test page</title>
</head>
<body>
<div id="content">Example page</div>
</body>
</html>