Commit b29d10e4 authored by Karan Bhatia's avatar Karan Bhatia Committed by Commit Bot

WebRequest API: Use the main_frame/sub_frame resource type for navigation preload requests.

This CL changes the web request API to use the main_frame and sub_frame resource
types for navigation preload requests. Currently these requests use the 'other'
resource type. This also requires separating the
content::ResourceType::kNavigationPreload type into distinct types for
main-frame and sub-frame.

BUG=965960

Change-Id: I82340a87d18d9ccb9dc16f66a4c4531aa0f0c94b
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/1666715Reviewed-by: default avatarMustafa Emre Acer <meacer@chromium.org>
Reviewed-by: default avatarMatt Falkenhagen <falken@chromium.org>
Reviewed-by: default avatarAlex Ilin <alexilin@chromium.org>
Reviewed-by: default avatarIstiaque Ahmed <lazyboy@chromium.org>
Reviewed-by: default avatarVarun Khaneja <vakh@chromium.org>
Reviewed-by: default avatarIlya Sherman <isherman@chromium.org>
Commit-Queue: Karan Bhatia <karandeepb@chromium.org>
Cr-Commit-Position: refs/heads/master@{#676640}
parent 9b7327b0
......@@ -257,6 +257,10 @@ class ExtensionWebRequestApiTest : public ExtensionApiTest {
name: 'frameId',
value: details.frameId.toString()
});
details.requestHeaders.push({
name: 'resourceType',
value: details.type
});
return {requestHeaders: details.requestHeaders};
}, {urls: ['*://*/echoheader*']}, ['blocking', 'requestHeaders']);
......@@ -2525,7 +2529,7 @@ IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
// response for the navigation preload request, and respond with it to create
// the page.
GURL url = embedded_test_server()->GetURL(
"/echoheader?foo&frameId&service-worker-navigation-preload");
"/echoheader?frameId&resourceType&service-worker-navigation-preload");
ui_test_utils::NavigateToURL(browser(), url);
content::WebContents* web_contents =
......@@ -2534,19 +2538,20 @@ IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
// Since the request was to "/echoheader", the response describes the request
// headers.
//
// The expectation is "bar\n0\ntrue" because...
// The expectation is "0\nmain_frame\ntrue" because...
//
// 1) The extension is expected to add a "foo: bar" header to the request
// before it goes to network.
// 2) The extension is similarly expected to add a "frameId: {id}" header,
// where {id} is details.frameId. This id is 0 for the main frame.
// 1) The extension is expected to add a "frameId: {id}" header, where {id} is
// details.frameId. This id is 0 for the main frame.
// 2) The extension is similarly expected to add a "resourceType: {type}"
// header, where {type} is details.type.
// 3) The browser adds a "service-worker-navigation-preload: true" header for
// navigation preload requests, so also sanity check that header to prove
// that this test is really testing the navigation preload request.
EXPECT_EQ("bar\n0\ntrue", EvalJs(web_contents, "document.body.textContent;"));
EXPECT_EQ("0\nmain_frame\ntrue",
EvalJs(web_contents, "document.body.textContent;"));
// Repeat the test from an iframe, to test that details.frameId is populated
// correctly.
// Repeat the test from an iframe, to test that details.frameId and resource
// type is populated correctly.
const char kAddIframe[] = R"(
(async () => {
const iframe = document.createElement('iframe');
......@@ -2557,10 +2562,13 @@ IN_PROC_BROWSER_TEST_F(ExtensionWebRequestApiTest,
});
const result = iframe.contentWindow.document.body.textContent;
// Expect "bar\n{frameId}\ntrue" where {frameId} is a positive integer.
// Expect "{frameId}\nsub_frame\ntrue" where {frameId} is a positive
// integer.
const split = result.split('\n');
if (split[0] == 'bar' && parseInt(split[1]) > 0 && split[2] == 'true')
return 'ok';
if (parseInt(split[0]) > 0 && split[1] == 'sub_frame' &&
split[2] == 'true') {
return 'ok';
}
return 'bad result: ' + result;
})();
)";
......
......@@ -42,7 +42,8 @@ net::RequestPriority GetRequestPriority(content::ResourceType resource_type) {
case content::ResourceType::kServiceWorker:
case content::ResourceType::kCspReport:
case content::ResourceType::kPluginResource:
case content::ResourceType::kNavigationPreload:
case content::ResourceType::kNavigationPreloadMainFrame:
case content::ResourceType::kNavigationPreloadSubFrame:
return net::LOWEST;
}
}
......
......@@ -120,7 +120,7 @@ RemoteSafeBrowsingDatabaseManager::RemoteSafeBrowsingDatabaseManager() {
if (ints_str.empty()) {
// By default, we check all types except a few.
static_assert(content::ResourceType::kMaxValue ==
content::ResourceType::kNavigationPreload,
content::ResourceType::kNavigationPreloadSubFrame,
"Decide if new resource type should be skipped on mobile.");
for (int t_int = 0;
t_int <= static_cast<int>(content::ResourceType::kMaxValue); t_int++) {
......
......@@ -658,8 +658,15 @@ bool ServiceWorkerFetchDispatcher::MaybeStartNavigationPreload(
return false;
network::ResourceRequest resource_request(original_request);
resource_request.resource_type =
static_cast<int>(ResourceType::kNavigationPreload);
if (resource_type_ == ResourceType::kMainFrame) {
resource_request.resource_type =
static_cast<int>(ResourceType::kNavigationPreloadMainFrame);
} else {
DCHECK_EQ(ResourceType::kSubFrame, resource_type_);
resource_request.resource_type =
static_cast<int>(ResourceType::kNavigationPreloadSubFrame);
}
resource_request.skip_service_worker = true;
resource_request.do_not_prompt_for_login = true;
......
......@@ -11,26 +11,30 @@ namespace content {
// Used in histograms; explicitly assign each type and do not re-use old values.
enum class ResourceType {
kMainFrame = 0, // top level page
kSubFrame = 1, // frame or iframe
kStylesheet = 2, // a CSS stylesheet
kScript = 3, // an external script
kImage = 4, // an image (jpg/gif/png/etc)
kFontResource = 5, // a font
kSubResource = 6, // an "other" subresource.
kObject = 7, // an object (or embed) tag for a plugin.
kMedia = 8, // a media resource.
kWorker = 9, // the main resource of a dedicated worker.
kSharedWorker = 10, // the main resource of a shared worker.
kPrefetch = 11, // an explicitly requested prefetch
kFavicon = 12, // a favicon
kXhr = 13, // a XMLHttpRequest
kPing = 14, // a ping request for <a ping>/sendBeacon.
kServiceWorker = 15, // the main resource of a service worker.
kCspReport = 16, // a report of Content Security Policy violations.
kPluginResource = 17, // a resource that a plugin requested.
kNavigationPreload = 18, // a service worker navigation preload request.
kMaxValue = kNavigationPreload,
kMainFrame = 0, // top level page
kSubFrame = 1, // frame or iframe
kStylesheet = 2, // a CSS stylesheet
kScript = 3, // an external script
kImage = 4, // an image (jpg/gif/png/etc)
kFontResource = 5, // a font
kSubResource = 6, // an "other" subresource.
kObject = 7, // an object (or embed) tag for a plugin.
kMedia = 8, // a media resource.
kWorker = 9, // the main resource of a dedicated worker.
kSharedWorker = 10, // the main resource of a shared worker.
kPrefetch = 11, // an explicitly requested prefetch
kFavicon = 12, // a favicon
kXhr = 13, // a XMLHttpRequest
kPing = 14, // a ping request for <a ping>/sendBeacon.
kServiceWorker = 15, // the main resource of a service worker.
kCspReport = 16, // a report of Content Security Policy violations.
kPluginResource = 17, // a resource that a plugin requested.
// kNavigationPreload = 18, // Deprecated.
// a main-frame service worker navigation preload request.
kNavigationPreloadMainFrame = 19,
// a sub-frame service worker navigation preload request.
kNavigationPreloadSubFrame = 20,
kMaxValue = kNavigationPreloadSubFrame,
};
CONTENT_EXPORT bool IsResourceTypeFrame(ResourceType type);
......
......@@ -24,5 +24,9 @@ enum ResourceType {
kServiceWorker = 15, // the main resource of a service worker.
kCspReport = 16, // a report of Content Security Policy violations.
kPluginResource = 17, // a resource that a plugin requested.
kNavigationPreload = 18, // a service worker navigation preload request.
// kNavigationPreload = 18, // Deprecated.
// a main-frame service worker navigation preload request.
kNavigationPreloadMainFrame = 19,
// a sub-frame service worker navigation preload request.
kNavigationPreloadSubFrame = 20,
};
......@@ -47,8 +47,10 @@ EnumTraits<content::mojom::ResourceType, content::ResourceType>::ToMojom(
return content::mojom::ResourceType::kCspReport;
case content::ResourceType::kPluginResource:
return content::mojom::ResourceType::kPluginResource;
case content::ResourceType::kNavigationPreload:
return content::mojom::ResourceType::kNavigationPreload;
case content::ResourceType::kNavigationPreloadMainFrame:
return content::mojom::ResourceType::kNavigationPreloadMainFrame;
case content::ResourceType::kNavigationPreloadSubFrame:
return content::mojom::ResourceType::kNavigationPreloadSubFrame;
}
NOTREACHED();
......@@ -114,8 +116,11 @@ bool EnumTraits<content::mojom::ResourceType, content::ResourceType>::FromMojom(
case content::mojom::ResourceType::kPluginResource:
*output = content::ResourceType::kPluginResource;
return true;
case content::mojom::ResourceType::kNavigationPreload:
*output = content::ResourceType::kNavigationPreload;
case content::mojom::ResourceType::kNavigationPreloadMainFrame:
*output = content::ResourceType::kNavigationPreloadMainFrame;
return true;
case content::mojom::ResourceType::kNavigationPreloadSubFrame:
*output = content::ResourceType::kNavigationPreloadSubFrame;
return true;
}
return false;
......
......@@ -37,9 +37,9 @@ flat_rule::ElementType GetElementType(content::ResourceType type) {
switch (type) {
case content::ResourceType::kPrefetch:
case content::ResourceType::kSubResource:
case content::ResourceType::kNavigationPreload:
return flat_rule::ElementType_OTHER;
case content::ResourceType::kMainFrame:
case content::ResourceType::kNavigationPreloadMainFrame:
return flat_rule::ElementType_MAIN_FRAME;
case content::ResourceType::kCspReport:
return flat_rule::ElementType_CSP_REPORT;
......@@ -59,6 +59,7 @@ flat_rule::ElementType GetElementType(content::ResourceType type) {
case content::ResourceType::kXhr:
return flat_rule::ElementType_XMLHTTPREQUEST;
case content::ResourceType::kSubFrame:
case content::ResourceType::kNavigationPreloadSubFrame:
return flat_rule::ElementType_SUBDOCUMENT;
case content::ResourceType::kPing:
return flat_rule::ElementType_PING;
......
......@@ -249,14 +249,13 @@ bool WebRequestPermissions::HideRequest(
if (is_request_from_browser) {
// Hide all non-navigation requests made by the browser. crbug.com/884932.
if (!request.is_navigation_request &&
request.type != content::ResourceType::kNavigationPreload) {
if (!request.is_navigation_request)
return true;
}
DCHECK(request.type == content::ResourceType::kMainFrame ||
request.type == content::ResourceType::kSubFrame ||
request.type == content::ResourceType::kNavigationPreload);
request.type == content::ResourceType::kNavigationPreloadMainFrame ||
request.type == content::ResourceType::kNavigationPreloadSubFrame);
// Hide sub-frame requests to clientsX.google.com.
// TODO(crbug.com/890006): Determine if the code here can be cleaned up
......
......@@ -44,8 +44,10 @@ static_assert(kResourceTypesLength ==
WebRequestResourceType ToWebRequestResourceType(content::ResourceType type) {
switch (type) {
case content::ResourceType::kMainFrame:
case content::ResourceType::kNavigationPreloadMainFrame:
return WebRequestResourceType::MAIN_FRAME;
case content::ResourceType::kSubFrame:
case content::ResourceType::kNavigationPreloadSubFrame:
return WebRequestResourceType::SUB_FRAME;
case content::ResourceType::kStylesheet:
return WebRequestResourceType::STYLESHEET;
......@@ -78,8 +80,6 @@ WebRequestResourceType ToWebRequestResourceType(content::ResourceType type) {
return WebRequestResourceType::CSP_REPORT;
case content::ResourceType::kPluginResource:
return WebRequestResourceType::OBJECT;
case content::ResourceType::kNavigationPreload:
return WebRequestResourceType::OTHER;
}
NOTREACHED();
return WebRequestResourceType::OTHER;
......
......@@ -9344,7 +9344,9 @@ Called by update_net_error_codes.py.-->
<int value="15" label="RESOURCE_TYPE_SERVICE_WORKER"/>
<int value="16" label="RESOURCE_TYPE_CSP_REPORT"/>
<int value="17" label="RESOURCE_TYPE_PLUGIN_RESOURCE"/>
<int value="18" label="RESOURCE_TYPE_NAVIGATION_PRELOAD"/>
<int value="18" label="RESOURCE_TYPE_NAVIGATION_PRELOAD (obsolete)"/>
<int value="19" label="RESOURCE_TYPE_NAVIGATION_PRELOAD_MAIN_FRAME"/>
<int value="20" label="RESOURCE_TYPE_NAVIGATION_PRELOAD_SUB_FRAME"/>
</enum>
<enum name="ContentSetting">
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment