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

[Web Workers] Navigator #1235

Merged
merged 2 commits into from
Jun 26, 2019
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
91 changes: 91 additions & 0 deletions src/Navigator.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
const os = require('os');

const {
getGamepads,
getHMDType,
} = require('./VR.js');
const {
nativeAudio: {
MicrophoneMediaStream,
},
nativeVideo: {
VideoDevice,
},
nativeWindow,
} = require('./native-bindings');
const GlobalContext = require('./GlobalContext');

function getUserMedia(constraints) {
if (constraints.audio) {
return Promise.resolve(new MicrophoneMediaStream());
} else if (constraints.video) {
const dev = new VideoDevice();
dev.constraints = constraints.video;
return Promise.resolve(dev);
} else {
return Promise.reject(new Error('constraints not met'));
}
}

class MediaDevices {
constructor() {
this.getUserMedia = getUserMedia;
}
enumerateDevices() {
let deviceIds = 0;
let groupIds = 0;
return Promise.resolve([
{
deviceId: (++deviceIds) + '',
groupId: (++groupIds) + '',
kind: 'audioinput',
label: 'Microphone',
},
]);
}
}
module.exports.MediaDevices = MediaDevices;

class Clipboard {
read() {
return Promise.resolve(); // TODO
}
readText() {
return new Promise(resolve => {
resolve(nativeWindow.getClipboard().slice(0, 256));// why do we slice this?
});
}
write() {
return Promise.resolve(); // TODO
}
writeText(clipboardContents) {
return new Promise(resolve => {
nativeWindow.setClipboard(clipboardContents);
resolve();
});
}
}
module.exports.Clipboard = Clipboard;

class Navigator {
constructor() {
this.userAgent = `Mozilla/5.0 (OS) AppleWebKit/999.0 (KHTML, like Gecko) Chrome/999.0.0.0 Safari/999.0 Exokit/${GlobalContext.version}`;
this.vendor = 'Exokit';
this.platform = os.platform();
this.hardwareConcurrency = os.cpus().length;
this.appCodeName = 'Mozilla';
this.appName = 'Netscape';
this.appVersion = '5.0';
this.language = 'en-US';
this.mediaDevices = new MediaDevices();
this.clipboard = new Clipboard();
this.webkitGetUserMedia = getUserMedia; // for feature detection
}
getVRDisplaysSync() {
return getHMDType() ? [window[symbols.mrDisplaysSymbol].vrDisplay] : [];
}
getGamepads() {
return getGamepads();
}
}
module.exports.Navigator = Navigator;
56 changes: 0 additions & 56 deletions src/Window.js
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,6 @@ const {
FakeXRDisplay,
Gamepad,
GamepadButton,
getGamepads,
getHMDType,
lookupHMDTypeString,
} = require('./VR.js');

Expand Down Expand Up @@ -89,11 +87,9 @@ const {
AnalyserNode,
PannerNode,
StereoPannerNode,
MicrophoneMediaStream,
},
nativeVideo: {
Video,
VideoDevice,
},
nativeOpenVR,
nativeOculusVR,
Expand Down Expand Up @@ -481,58 +477,6 @@ const _makeRequestAnimationFrame = window => (fn, priority = 0) => {
},
});
window.history = new History(location.href);
function getUserMedia(constraints) {
if (constraints.audio) {
return Promise.resolve(new MicrophoneMediaStream());
} else if (constraints.video) {
const dev = new VideoDevice();
dev.constraints = constraints.video;
return Promise.resolve(dev);
} else {
return Promise.reject(new Error('constraints not met'));
}
}
window.navigator = {
userAgent: `Mozilla/5.0 (OS) AppleWebKit/999.0 (KHTML, like Gecko) Chrome/999.0.0.0 Safari/999.0 Exokit/${GlobalContext.version}`,
vendor: 'Exokit',
platform: os.platform(),
hardwareConcurrency: os.cpus().length,
appCodeName: 'Mozilla',
appName: 'Netscape',
appVersion: '5.0',
language: 'en-US',
mediaDevices: {
getUserMedia,
enumerateDevices() {
let deviceIds = 0;
let groupIds = 0;
return Promise.resolve([
{
deviceId: (++deviceIds) + '',
groupId: (++groupIds) + '',
kind: 'audioinput',
label: 'Microphone',
},
]);
},
},
webkitGetUserMedia: getUserMedia, // for feature detection
getVRDisplaysSync() {
return getHMDType() ? [window[symbols.mrDisplaysSymbol].vrDisplay] : [];
},
getGamepads,
clipboard: {
read: () => Promise.resolve(), // Not implemented yet
readText: () => new Promise(resolve => {
resolve(nativeWindow.getClipboard().slice(0, 256));// why do we slice this?
}),
write: () => Promise.resolve(), // Not implemented yet
writeText: clipboardContents => new Promise(resolve => {
nativeWindow.setClipboard(clipboardContents);
resolve();
})
}
};
window.matchMedia = media => ({
media,
matches: false,
Expand Down
8 changes: 7 additions & 1 deletion src/WindowBase.js
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,8 @@ const {
} = require('worker_threads');

const {CustomEvent, DragEvent, ErrorEvent, Event, EventTarget, KeyboardEvent, MessageEvent, MouseEvent, WheelEvent, PromiseRejectionEvent} = require('./Event');
const {FileReader} = require('./File.js');
const {MediaDevices, Clipboard, Navigator} = require('./Navigator');
const {FileReader} = require('./File');
const {XMLHttpRequest, FormData} = require('window-xhr');
const fetch = require('window-fetch');
const {Request, Response, Headers, Blob} = fetch;
Expand Down Expand Up @@ -148,6 +149,11 @@ class Worker extends EventTarget {

self.URL = URL;

self.MediaDevices = MediaDevices;
self.Clipboard = Clipboard;
self.Navigator = Navigator;
self.navigator = new Navigator();

self.fetch = (u, options) => {
if (typeof u === 'string') {
const blob = URL.lookupObjectURL(u);
Expand Down