Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix(Xbox): Support screen resolution detection on Xbox when using WebView2 #7144

Merged
merged 4 commits into from
Aug 8, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions CONTRIBUTORS
Original file line number Diff line number Diff line change
Expand Up @@ -92,6 +92,7 @@ Martin Stark <[email protected]>
Mariano Facundo Scigliano <[email protected]>
Matias Russitto <[email protected]>
Mathieu Côté <[email protected]>
Mathieu Massicotte <[email protected]>
Matthias Van Parijs <[email protected]>
Mattias Wadman <[email protected]>
Mehmet Guney <[email protected]>
Expand Down
18 changes: 18 additions & 0 deletions docs/tutorials/screen-resolution-detection.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,6 +32,24 @@ Note: in order to use this feature in a UWP app, you must add the URI of your
web app in the ContentURIs section of the Package.appxmanifest file and set
the `WinRT access` field to `All`.

### WebView2

When using a WebView2 control in a UWP app, additional steps are required in
order to enable the screen resolution detection. First, the WebView2's user-agent
is the same as Edge Chromium and does not contain the term "Xbox One", so it has
to be manually added like this when initializing your WebView2:

```CSharp
webView.CoreWebView2.Settings.UserAgent += " Xbox One";
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Is there some improvement we could make to UA detection to understand WebView2 as Xbox One without this?

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Based only on the user agent, I don't think it will be possible:

Edge Chromium on Windows 11:
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/127.0.0.0 Safari/537.36 Edg/127.0.0.0

WebView2 on Xbox (not modified):
Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/125.0.0.0 Safari/537.36 Edg/125.0.0.0

They are basically the same, except for the Chrome/Edge version, which is a bit newer on Windows.

Maybe that detecting the presence of the WinRT APIs could be enough. Something like

static isXboxOne() {
    return shaka.util.Platform.userAgentContains_('Xbox One') || typeof Windows !== 'undefined' || typeof chrome?.webview?.hostObjects?.sync?.Windows !== 'undefined';
}

But it could still be a UWP running on Windows... Maybe there's a way to get the device type from WinRT once we can access to it.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Okay, thanks for considering it. We are very open to improvements in detection later.

```

Also, you will need to add a special project called WinRTAdapter in your project's
solution. This project allows WinRT APIs to be exposed in the WebView2 control.
You will find more information on this [here](https://learn.microsoft.com/en-us/microsoft-edge/webview2/how-to/winrt-from-js).
Make sure you put `Windows.Media.Protection.ProtectionCapabilities`
and `Windows.Media.Protection.ProtectionCapabilityResult` in the WinRTAdapter
_Include filters_ configuration.

## Hisense

We can detect if the device supports 3840x2160 (4K).
Expand Down
20 changes: 20 additions & 0 deletions externs/xbox.js
Original file line number Diff line number Diff line change
Expand Up @@ -41,3 +41,23 @@ Windows.Media.Protection.ProtectionCapabilityResult = {
maybe: 'Maybe',
probably: 'Probably',
};


/** @const */
var chrome = {};


/** @const */
chrome.webview = {};


/** @const */
chrome.webview.hostObjects = {};


/** @const */
chrome.webview.hostObjects.sync = {};


/** @const */
chrome.webview.hostObjects.sync.Windows = Windows;
15 changes: 13 additions & 2 deletions lib/util/platform.js
Original file line number Diff line number Diff line change
Expand Up @@ -749,10 +749,21 @@ shaka.util.Platform = class {
maxResolution.width = 1920;
maxResolution.height = 1080;
try {
let winRT = undefined;

// Try to access to WinRT for WebView, if it's not defined,
// try to access to WinRT for WebView2, if it's not defined either,
// let it throw.
if (typeof Windows !== 'undefined') {
winRT = Windows;
} else {
winRT = chrome.webview.hostObjects.sync.Windows;
}

const protectionCapabilities =
new Windows.Media.Protection.ProtectionCapabilities();
new winRT.Media.Protection.ProtectionCapabilities();
const protectionResult =
Windows.Media.Protection.ProtectionCapabilityResult;
winRT.Media.Protection.ProtectionCapabilityResult;
// isTypeSupported may return "maybe", which means the operation is not
// completed. This means we need to retry
// https://learn.microsoft.com/en-us/uwp/api/windows.media.protection.protectioncapabilityresult?view=winrt-22621
Expand Down
Loading