Skip to content

Commit

Permalink
[UA-CH] Add userAgentData worker support
Browse files Browse the repository at this point in the history
This implements PR #80 [1] and exposes userAgentData in workers.

[1] WICG/ua-client-hints#80

Bug: 1062192
Change-Id: I1de6067ab6c00855d7bf820e9646eb4e5a8c5fdb
Reviewed-on: https://chromium-review.googlesource.com/c/chromium/src/+/2093220
Commit-Queue: Yoav Weiss <yoavweiss@chromium.org>
Reviewed-by: Hiroki Nakagawa <nhiroki@chromium.org>
Reviewed-by: Kinuko Yasuda <kinuko@chromium.org>
Reviewed-by: Matt Falkenhagen <falken@chromium.org>
Cr-Commit-Position: refs/heads/master@{#751295}
  • Loading branch information
Yoav Weiss authored and chromium-wpt-export-bot committed Mar 18, 2020
1 parent bd040f2 commit 793b6b5
Show file tree
Hide file tree
Showing 4 changed files with 68 additions and 9 deletions.
Original file line number Diff line number Diff line change
@@ -1,10 +1,11 @@
<!DOCTYPE html>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script src="/webrtc/dictionary-helper.js"></script>
<script>
test(t => {
assert_true("userAgentData" in navigator);
assert_true("NavigatorUAData" in window);
assert_equals(typeof self.NavigatorUAData, "function")
}, "navigator.userAgentData is exposed.");

promise_test(async t => {
Expand Down
17 changes: 17 additions & 0 deletions workers/WorkerNavigator_userAgentData.http.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,17 @@
<!DOCTYPE html>
<title> WorkerNavigator.userAgentData </title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

promise_test(async () => {
const e = await new Promise((resolve, reject) => {
const worker = new Worker("./support/WorkerNavigator.js");
worker.onmessage = resolve;
});

assert_equals(e.data.uaList, undefined);
assert_equals(e.data.mobile, undefined);
assert_equals(e.data.getHighEntropyValues, undefined);
}, "Test that userAgentData is not available in workers in non-secure contexts");
</script>
29 changes: 29 additions & 0 deletions workers/WorkerNavigator_userAgentData.https.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
<!DOCTYPE html>
<title> WorkerNavigator.userAgentData </title>
<script src="/resources/testharness.js"></script>
<script src="/resources/testharnessreport.js"></script>
<script>

promise_test(async () => {
const e = await new Promise((resolve, reject) => {
const worker = new Worker("./support/WorkerNavigator.js");
worker.onmessage = resolve;
});

assert_equals(e.data.uaList.length, navigator.userAgentData.uaList.length);
for (let i = 0; i < e.data.uaList.length; ++i) {
const workerUA = e.data.uaList[i];
const windowUA = navigator.userAgentData.uaList[i];
assert_equals(workerUA.brand, windowUA.brand);
assert_equals(workerUA.version, windowUA.version);
}
assert_equals(e.data.mobile, navigator.userAgentData.mobile);
const highEntropyValues = await navigator.userAgentData.getHighEntropyValues(["platform", "platformVersion", "architecture", "model", "uaFullVersion"]);
assert_equals(e.data.model, highEntropyValues.model);
assert_equals(e.data.platform, highEntropyValues.platform);
assert_equals(e.data.platformVersion, highEntropyValues.platformVersion);
assert_equals(e.data.architecture, highEntropyValues.architecture);
assert_equals(e.data.uaFullVersion, highEntropyValues.uaFullVersion);
assert_equals(e.data.NavigatorUADataExposed, true);
}, "Test that userAgentData is available in workers in secure contexts");
</script>
28 changes: 20 additions & 8 deletions workers/support/WorkerNavigator.js
Original file line number Diff line number Diff line change
@@ -1,8 +1,20 @@
var obj = new Object();
obj.appName = navigator.appName;
obj.appVersion = navigator.appVersion;
obj.platform = navigator.platform;
obj.userAgent = navigator.userAgent;
obj.onLine = navigator.onLine;

postMessage(obj);
(async () => {
const obj = new Object();
obj.appName = navigator.appName;
obj.appVersion = navigator.appVersion;
obj.platform = navigator.platform;
obj.userAgent = navigator.userAgent;
obj.onLine = navigator.onLine;
if (navigator.userAgentData) {
obj.uaList = navigator.userAgentData.uaList;
obj.mobile = navigator.userAgentData.mobile;
const highEntropyValues = await navigator.userAgentData.getHighEntropyValues(["platform", "platformVersion", "architecture", "model", "uaFullVersion"]);
obj.platform = highEntropyValues.platform;
obj.platformVersion = highEntropyValues.platformVersion;
obj.architecture = highEntropyValues.architecture;
obj.model = highEntropyValues.model;
obj.uaFullVersion = highEntropyValues.uaFullVersion;
obj.NavigatorUADataExposed = (typeof self.NavigatorUAData != "undefined");
}
postMessage(obj);
})();

0 comments on commit 793b6b5

Please sign in to comment.