Skip to content

Commit

Permalink
Merge pull request #1235 from exokitxr/worker-navigator
Browse files Browse the repository at this point in the history
[Web Workers] Navigator
  • Loading branch information
avaer committed Jun 26, 2019
2 parents 130dfae + d82bab3 commit 751ccd7
Show file tree
Hide file tree
Showing 3 changed files with 98 additions and 57 deletions.
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 @@ -20,7 +20,8 @@ const {
const {Buffer} = global;

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 @@ -150,6 +151,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

0 comments on commit 751ccd7

Please sign in to comment.