Skip to content

Commit bd5d627

Browse files
committed
web: Add OriginAPI to the web player
The new OriginAPI has been added to the web player. It is used to access behaviour that's specific to the Ruffle origin (selfhosted, demo or extension). Unlike the SourceAPI, which manages the whole Ruffle polyfill and player, it provides minor helper methods and is given to the (internal) Ruffle player. The OriginAPI has three implementations: SelfhostedOrigin, DemoOrigin and ExtensionOrigin. The respective instance is created when installing the Ruffle instance. When using SourceAPI#createPlayer or #polyfill, the OriginAPI is given to the then-created (internal) player. This is done by calling the new RufflePlayerElement#initialize method after creating it. The player API now also provides a method to access its OriginAPI. The previous extension related variables and functions (like inExtensionPlayer) have been removed from the core code. Instead, createPanicError now uses OriginAPI calls to display the correct error message. Additionally, internalSourceApi and its usages have been replaced by several functions, reducing the likelihood of future mistakes. The documentation and code quality have also been generally improved.
1 parent 2ef9ee7 commit bd5d627

File tree

18 files changed

+309
-169
lines changed

18 files changed

+309
-169
lines changed

web/packages/core/src/install.ts

Lines changed: 10 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
import { PublicAPI } from "./public-api";
2-
import { internalSourceApi } from "./source-api";
2+
import { getSourceApiImplementation } from "./source-api";
3+
import { OriginAPI } from "./origin-api";
34

45
/**
56
* Options to use with this specific installation of Ruffle.
@@ -27,10 +28,12 @@ export interface InstallationOptions {
2728
* "extension" for browser extensions, and something else for other use cases.
2829
* Names are unique, and last-installed will replace earlier installations with the same name,
2930
* regardless of what those installations are or which version they represent.
31+
* @param originAPI The OriginAPI of this Ruffle version.
3032
* @param options Any options used to configure this specific installation of Ruffle.
3133
*/
3234
export function installRuffle(
3335
sourceName: string,
36+
originAPI: OriginAPI,
3437
options: InstallationOptions = {},
3538
): void {
3639
let publicAPI: PublicAPI;
@@ -41,8 +44,11 @@ export function installRuffle(
4144
window.RufflePlayer = publicAPI;
4245
}
4346

44-
publicAPI.sources[sourceName] = internalSourceApi;
45-
internalSourceApi.options = options;
47+
const sourceApiImplementation = getSourceApiImplementation(
48+
originAPI,
49+
options,
50+
);
51+
publicAPI.sources[sourceName] = sourceApiImplementation;
4652

4753
// Install the faux plugin detection immediately.
4854
// This is necessary because scripts such as SWFObject check for the
@@ -51,6 +57,6 @@ export function installRuffle(
5157
const polyfills =
5258
"polyfills" in publicAPI.config ? publicAPI.config.polyfills : true;
5359
if (polyfills !== false) {
54-
internalSourceApi.pluginPolyfill();
60+
sourceApiImplementation.pluginPolyfill();
5561
}
5662
}

web/packages/core/src/internal/errors.tsx

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,8 +1,7 @@
11
export class LoadSwfError extends Error {
2-
constructor(public swfUrl: URL | undefined, public inExtensionPlayer: boolean) {
2+
constructor(public swfUrl: URL | undefined) {
33
super(`Failed to fetch ${swfUrl}`);
44
this.swfUrl = swfUrl;
5-
this.inExtensionPlayer = inExtensionPlayer;
65
}
76
}
87

web/packages/core/src/internal/player/inner.tsx

Lines changed: 14 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import { createRuffleBuilder } from "../../load-ruffle";
2626
import { lookupElement } from "../register-element";
2727
import { configureBuilder } from "../builder";
2828
import { DEFAULT_CONFIG } from "../../config";
29+
import { OriginAPI } from "../../origin-api";
2930

3031
const DIMENSION_REGEX = /^\s*(\d+(\.\d+)?(%)?)/;
3132

@@ -184,9 +185,7 @@ export class InnerPlayer {
184185

185186
private volumeSettings: VolumeControls;
186187

187-
// Whether the last load command originated from the extension player
188-
// TODO: Move this variable into the extension code and replace its usages with an API call
189-
private inExtensionPlayer = false;
188+
private readonly originAPI: OriginAPI;
190189

191190
private readonly debugPlayerInfo: () => string;
192191
protected readonly onCallbackAvailable: (name: string) => void;
@@ -195,10 +194,12 @@ export class InnerPlayer {
195194
element: HTMLElement,
196195
debugPlayerInfo: () => string,
197196
onCallbackAvailable: (name: string) => void,
197+
originAPI: OriginAPI
198198
) {
199199
this.element = element;
200200
this.debugPlayerInfo = debugPlayerInfo;
201201
this.onCallbackAvailable = onCallbackAvailable;
202+
this.originAPI = originAPI;
202203

203204
this.shadow = this.element.attachShadow({ mode: "open" });
204205
this.shadow.appendChild(ruffleShadowTemplate.content.cloneNode(true));
@@ -796,9 +797,7 @@ export class InnerPlayer {
796797
async load(
797798
options: string | URLLoadOptions | DataLoadOptions,
798799
isPolyfillElement: boolean = false,
799-
inExtensionPlayer: boolean = false
800800
): Promise<void> {
801-
this.inExtensionPlayer = inExtensionPlayer;
802801
options = this.checkOptions(options);
803802

804803
if (!this.element.isConnected || this.isUnusedFallbackObject()) {
@@ -892,6 +891,14 @@ export class InnerPlayer {
892891
return false;
893892
}
894893

894+
/**
895+
* Returns the OriginAPI of this Ruffle player.
896+
* @returns The OriginAPI of this Ruffle player.
897+
*/
898+
getOriginAPI(): OriginAPI {
899+
return this.originAPI;
900+
}
901+
895902
/**
896903
* Returns the master volume of the player.
897904
*
@@ -1891,7 +1898,7 @@ export class InnerPlayer {
18911898
errorArray.push(this.getPanicData());
18921899

18931900
// Clears out any existing content (ie play button or canvas) and replaces it with the error screen
1894-
showPanicScreen(this.container, error, errorArray, this.swfUrl);
1901+
showPanicScreen(this.container, error, errorArray, this.swfUrl, this.originAPI);
18951902

18961903
// Do this last, just in case it causes any cascading issues.
18971904
this.destroy();
@@ -1934,10 +1941,6 @@ export class InnerPlayer {
19341941
this.displayRootMovieDownloadFailedMessage(false);
19351942
}
19361943

1937-
setInExtensionPlayer() {
1938-
this.inExtensionPlayer = true;
1939-
}
1940-
19411944
protected displayRootMovieDownloadFailedMessage(invalidSwf: boolean): void {
19421945
const openInNewTab = this.loadedConfig?.openInNewTab;
19431946
if (
@@ -1950,7 +1953,7 @@ export class InnerPlayer {
19501953
} else {
19511954
const error = invalidSwf
19521955
? new InvalidSwfError(this.swfUrl)
1953-
: new LoadSwfError(this.swfUrl, this.inExtensionPlayer);
1956+
: new LoadSwfError(this.swfUrl);
19541957
this.panic(error);
19551958
}
19561959
}

web/packages/core/src/internal/player/ruffle-embed-element.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,7 @@ import {
77
} from "./inner";
88
import { registerElement } from "../register-element";
99
import { isSwf } from "../../swf-utils";
10+
import { OriginAPI } from "../../origin-api";
1011

1112
/**
1213
* A polyfill html element.
@@ -134,16 +135,22 @@ export class RuffleEmbedElement extends RufflePlayerElement {
134135
* Creates a RuffleEmbed that will polyfill and replace the given element.
135136
*
136137
* @param elem Element to replace.
138+
* @param originAPI The OriginAPI that should be used for the Ruffle version
139+
* polyfilling the given Flash element.
137140
* @returns Created RuffleEmbed.
138141
*/
139-
static fromNativeEmbedElement(elem: Element): RuffleEmbedElement {
142+
static fromNativeEmbedElement(
143+
elem: Element,
144+
originAPI: OriginAPI,
145+
): RuffleEmbedElement {
140146
const externalName = registerElement(
141147
"ruffle-embed",
142148
RuffleEmbedElement,
143149
);
144150
const ruffleObj = document.createElement(
145151
externalName,
146152
) as RuffleEmbedElement;
153+
ruffleObj.initialize(originAPI);
147154
copyElement(elem, ruffleObj);
148155

149156
return ruffleObj;

web/packages/core/src/internal/player/ruffle-object-element.ts

Lines changed: 8 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ import { FLASH_ACTIVEX_CLASSID } from "../../flash-identifiers";
99
import { registerElement } from "../register-element";
1010
import { RuffleEmbedElement } from "./ruffle-embed-element";
1111
import { isSwf } from "../../swf-utils";
12+
import { OriginAPI } from "../../origin-api";
1213

1314
/**
1415
* Find and return the first value in obj with the given key.
@@ -250,16 +251,22 @@ export class RuffleObjectElement extends RufflePlayerElement {
250251
* Creates a RuffleObject that will polyfill and replace the given element.
251252
*
252253
* @param elem Element to replace.
254+
* @param originAPI The OriginAPI that should be used for the Ruffle version
255+
* polyfilling the given Flash element.
253256
* @returns Created RuffleObject.
254257
*/
255-
static fromNativeObjectElement(elem: Element): RuffleObjectElement {
258+
static fromNativeObjectElement(
259+
elem: Element,
260+
originAPI: OriginAPI,
261+
): RuffleObjectElement {
256262
const externalName = registerElement(
257263
"ruffle-object",
258264
RuffleObjectElement,
259265
);
260266
const ruffleObj: RuffleObjectElement = document.createElement(
261267
externalName,
262268
) as RuffleObjectElement;
269+
ruffleObj.initialize(originAPI);
263270

264271
// Avoid copying embeds-inside-objects to avoid double polyfilling.
265272
for (const embedElem of Array.from(

0 commit comments

Comments
 (0)