Skip to content

Commit 107277e

Browse files
author
brizental
committed
Implement a PlatformInfo class for web ext
1 parent c8473c2 commit 107277e

File tree

3 files changed

+117
-0
lines changed

3 files changed

+117
-0
lines changed

src/platform_info/index.ts

Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
// Must be up to date with https://github.com/mozilla/glean/blob/main/glean-core/src/system.rs
6+
export const enum KnownOperatingSystems {
7+
Android = "Android",
8+
iOS = "iOS",
9+
Linux = "Linux",
10+
MacOS = "Darwin",
11+
Windows = "Windows",
12+
FreeBSD = "FreeBSD",
13+
NetBSD = "NetBSD",
14+
OpenBSD = "OpenBSD",
15+
Solaris = "Solaris",
16+
// ChromeOS is not listed in the Glean SDK because it is not a possibility there.
17+
ChromeOS = "ChromeOS",
18+
Unknown = "Unknown",
19+
}
20+
21+
export interface PlatformInfo {
22+
/**
23+
* Gets and returns the current OS system.
24+
*
25+
* @returns The current OS.
26+
*/
27+
os(): Promise<KnownOperatingSystems>;
28+
29+
/**
30+
* Gets and returns the current OS system version.
31+
*
32+
* @returns The current OS version.
33+
*/
34+
osVersion(): Promise<string>;
35+
36+
/**
37+
* Gets and returnst the current system architecture.
38+
*
39+
* @returns The current system architecture.
40+
*/
41+
arch(): Promise<string>;
42+
43+
/**
44+
* Gets and returnst the current system / browser locale.
45+
*
46+
* @returns The current system / browser locale.
47+
*/
48+
locale(): Promise<string>;
49+
}
50+
51+
// Default export for tests sake.
52+
const MockPlatformInfo: PlatformInfo = {
53+
os(): Promise<KnownOperatingSystems> {
54+
return Promise.resolve(KnownOperatingSystems.Unknown);
55+
},
56+
57+
osVersion(): Promise<string> {
58+
return Promise.resolve("Unknown");
59+
},
60+
61+
arch(): Promise<string> {
62+
return Promise.resolve("Unknown");
63+
},
64+
65+
locale(): Promise<string> {
66+
return Promise.resolve("Unknown");
67+
},
68+
};
69+
export default MockPlatformInfo;

src/platform_info/webext.ts

Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
/* This Source Code Form is subject to the terms of the Mozilla Public
2+
* License, v. 2.0. If a copy of the MPL was not distributed with this
3+
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
4+
5+
import { PlatformInfo, KnownOperatingSystems } from "./index";
6+
7+
const WebExtPlatformInfo: PlatformInfo = {
8+
async os(): Promise<KnownOperatingSystems> {
9+
// Possible values are listed in https://developer.mozilla.org/en-US/docs/Mozilla/Add-ons/WebExtensions/API/runtime/PlatformOs
10+
const platformInfo = await browser.runtime.getPlatformInfo();
11+
switch(platformInfo.os) {
12+
case "mac":
13+
return KnownOperatingSystems.MacOS;
14+
case "win":
15+
return KnownOperatingSystems.Windows;
16+
case "android":
17+
return KnownOperatingSystems.Android;
18+
case "cros":
19+
return KnownOperatingSystems.ChromeOS;
20+
case "linux":
21+
return KnownOperatingSystems.Linux;
22+
case "openbsd":
23+
return KnownOperatingSystems.OpenBSD;
24+
default:
25+
return KnownOperatingSystems.Unknown;
26+
}
27+
},
28+
29+
async osVersion(): Promise<string> {
30+
// The only way I found to extract osVersion from the browser was through the userAgent string
31+
// and that is unreliable enought that I would rather not do it.
32+
//
33+
// For web extensions, browser type and version are more important anyways.
34+
return Promise.resolve("Unknown");
35+
},
36+
37+
async arch(): Promise<string> {
38+
const platformInfo = await browser.runtime.getPlatformInfo();
39+
// Possible values are: "arm", "x86-32", "x86-64"
40+
return platformInfo.arch;
41+
},
42+
43+
async locale(): Promise<string> {
44+
return Promise.resolve(navigator.language || "Unknown");
45+
}
46+
};
47+
export default WebExtPlatformInfo;

webpack.config.webext.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ module.exports = {
1818
...baseConfig.resolve,
1919
alias: {
2020
"storage/persistent": path.resolve(__dirname, "src/storage/persistent/webext"),
21+
"platform_info": path.resolve(__dirname, "src/platform_info/webext"),
2122
}
2223
}
2324
};

0 commit comments

Comments
 (0)