ruffle/web/packages/core/src/plugin-polyfill.ts

224 lines
6.1 KiB
TypeScript
Raw Normal View History

2022-08-29 07:21:24 +00:00
import {
FLASH_MIMETYPE,
FUTURESPLASH_MIMETYPE,
FLASH7_AND_8_MIMETYPE,
FLASH_MOVIE_MIMETYPE,
} from "./flash-identifiers";
2022-08-29 07:21:24 +00:00
/**
* Replacement object for `MimeTypeArray` that lets us install new fake mime
* types.
*
* Unlike plugins we can at least enumerate mime types in Firefox, so we don't
* lose data.
*
* We also expose a method called `install` which adds a new plugin. This is
* used to falsify Flash detection. If the existing `navigator.mimeTypes` has an
* `install` method, you should not use `RuffleMimeTypeArray` as some other
* plugin emulator is already present.
*/
class RuffleMimeTypeArray implements MimeTypeArray {
2023-02-20 14:19:18 +00:00
private readonly __mimeTypes: MimeType[];
private readonly __namedMimeTypes: Record<string, MimeType>;
2023-02-20 14:19:18 +00:00
constructor(mimeTypes?: MimeTypeArray) {
this.__mimeTypes = [];
this.__namedMimeTypes = {};
2023-02-20 14:19:18 +00:00
if (mimeTypes) {
for (let i = 0; i < mimeTypes.length; i++) {
this.install(mimeTypes[i]!);
}
}
}
/**
* Install a MIME Type into the array.
*
2023-02-20 14:19:18 +00:00
* @param mimeType The mime type to install
*/
2023-02-20 14:19:18 +00:00
install(mimeType: MimeType): void {
const index = this.__mimeTypes.length;
this.__mimeTypes.push(mimeType);
this.__namedMimeTypes[mimeType.type] = mimeType;
this[mimeType.type] = mimeType;
this[index] = mimeType;
}
item(index: number): MimeType {
2023-02-20 14:19:18 +00:00
return this.__mimeTypes[index]!;
}
namedItem(name: string): MimeType {
2023-02-20 14:19:18 +00:00
return this.__namedMimeTypes[name]!;
}
get length(): number {
2023-02-20 14:19:18 +00:00
return this.__mimeTypes.length;
}
[index: number]: MimeType;
2021-06-03 16:32:40 +00:00
[name: string]: unknown;
[Symbol.iterator](): IterableIterator<MimeType> {
2023-02-20 14:19:18 +00:00
return this.__mimeTypes[Symbol.iterator]();
}
}
/**
* Equivalent object to `Plugin` that allows us to falsify plugins.
*/
class RufflePlugin extends RuffleMimeTypeArray implements Plugin {
constructor(
2023-02-20 14:19:18 +00:00
readonly name: string,
readonly description: string,
readonly filename: string
) {
2023-02-20 14:19:18 +00:00
super();
}
}
/**
* Replacement object for `PluginArray` that lets us install new fake plugins.
*
* This object needs to wrap the native plugin array, since the user might have
* actual plugins installed. Firefox doesn't let us enumerate the array, though,
* which has some consequences. Namely, we can't actually perfectly wrap the
* native plugin array, at least unless there's some secret "unresolved object
* property name handler" that I've never known before in JS...
*
* We can still wrap `namedItem` perfectly at least.
*
* We also expose a method called `install` which adds a new plugin. This is
* used to falsify Flash detection. If the existing `navigator.plugins` has an
* `install` method, you should not use `RufflePluginArray` as some other plugin
* emulator is already present.
*/
2023-02-20 14:19:18 +00:00
class RufflePluginArray implements PluginArray {
private readonly __plugins: Plugin[];
2023-02-20 14:19:18 +00:00
private readonly __namedPlugins: Record<string, Plugin>;
2023-02-20 14:19:18 +00:00
constructor(plugins: PluginArray) {
this.__plugins = [];
2023-02-20 14:19:18 +00:00
this.__namedPlugins = {};
2023-02-20 14:19:18 +00:00
for (let i = 0; i < plugins.length; i++) {
this.install(plugins[i]!);
}
}
2021-06-03 16:32:40 +00:00
install(plugin: Plugin): void {
2023-02-20 14:19:18 +00:00
const index = this.__plugins.length;
this.__plugins.push(plugin);
2023-02-20 14:19:18 +00:00
this.__namedPlugins[plugin.name] = plugin;
2021-06-03 16:32:40 +00:00
this[plugin.name] = plugin;
2023-02-20 14:19:18 +00:00
this[index] = plugin;
}
item(index: number): Plugin {
2023-02-20 14:19:18 +00:00
return this.__plugins[index]!;
}
namedItem(name: string): Plugin {
2023-02-20 14:19:18 +00:00
return this.__namedPlugins[name]!;
}
refresh(): void {
// Nothing to do, we just need to define the method.
}
2021-06-03 16:32:40 +00:00
[index: number]: Plugin;
[name: string]: unknown;
2022-12-19 09:11:02 +00:00
[Symbol.iterator](): IterableIterator<Plugin> {
return this.__plugins[Symbol.iterator]();
}
get length(): number {
return this.__plugins.length;
}
}
/**
* A fake plugin designed to trigger Flash detection scripts.
*/
export const FLASH_PLUGIN = new RufflePlugin(
"Shockwave Flash",
"Shockwave Flash 32.0 r0",
2023-02-20 14:19:18 +00:00
"ruffle.js"
);
FLASH_PLUGIN.install({
2022-08-29 07:21:24 +00:00
type: FUTURESPLASH_MIMETYPE,
description: "Shockwave Flash",
suffixes: "spl",
enabledPlugin: FLASH_PLUGIN,
});
FLASH_PLUGIN.install({
2022-08-29 07:21:24 +00:00
type: FLASH_MIMETYPE,
description: "Shockwave Flash",
suffixes: "swf",
enabledPlugin: FLASH_PLUGIN,
});
FLASH_PLUGIN.install({
2022-08-29 07:21:24 +00:00
type: FLASH7_AND_8_MIMETYPE,
description: "Shockwave Flash",
suffixes: "swf",
enabledPlugin: FLASH_PLUGIN,
});
FLASH_PLUGIN.install({
2022-08-29 07:21:24 +00:00
type: FLASH_MOVIE_MIMETYPE,
description: "Shockwave Flash",
suffixes: "swf",
enabledPlugin: FLASH_PLUGIN,
});
2021-06-03 16:32:40 +00:00
declare global {
interface PluginArray {
install?: (plugin: Plugin) => void;
}
interface MimeTypeArray {
2023-02-20 14:19:18 +00:00
install?: (mimeType: MimeType) => void;
2021-06-03 16:32:40 +00:00
}
}
/**
* Install a fake plugin such that detectors will see it in `navigator.plugins`.
*
* This function takes care to check if the existing implementation of
* `navigator.plugins` already accepts fake plugin entries. If so, it will use
* that version of the plugin array. This allows the plugin polyfill to compose
* across multiple plugin emulators with the first emulator's polyfill winning.
2020-11-17 22:53:17 +00:00
*
* @param plugin The plugin to install
*/
export function installPlugin(plugin: RufflePlugin): void {
if (!("install" in navigator.plugins) || !navigator.plugins["install"]) {
Object.defineProperty(navigator, "plugins", {
value: new RufflePluginArray(navigator.plugins),
writable: false,
});
}
2021-06-03 16:32:40 +00:00
const plugins = navigator.plugins;
plugins.install!(plugin);
if (
plugin.length > 0 &&
(!("install" in navigator.mimeTypes) || !navigator.mimeTypes["install"])
) {
Object.defineProperty(navigator, "mimeTypes", {
value: new RuffleMimeTypeArray(navigator.mimeTypes),
writable: false,
});
}
2021-06-03 16:32:40 +00:00
const mimeTypes = navigator.mimeTypes;
for (let i = 0; i < plugin.length; i += 1) {
2023-02-20 14:19:18 +00:00
mimeTypes.install!(plugin[i]!);
}
}