forked from Blendman974/faceclaw
Add options to load an app from a URL or a QR code
(The QR code reading does not work yet)
This commit is contained in:
1 parent
6e4ece5dc7
commit
92cd05b6ca
22 files changed
+685
-93
No files matched your search
@@ -10,7 +10,7 @@
|
||||
import { type AppContext, type AppDefinition } from "../app-definition";
|
||||
import { shell } from "../../ui/shell/shell";
|
||||
import { EvenHubStoreLayer } from "./store-layer";
|
||||
import { launchPackage } from "./manager";
|
||||
import { launchPackage, launchUrl, normalizeAppUrl } from "./manager";
|
||||
import { createInProcessWindow, type InProcessWindow } from "../../ui/shell/in-process-window";
|
||||
import { renderEvenRealitiesLogo } from "../../graphics/even-realities-logo";
|
||||
import { makeImageWindowIcon, windowIcon } from "../../ui/shell/chrome-layer";
|
||||
@@ -26,6 +26,20 @@ export function openEvenHubPackage(ctx: AppContext, ehpkPath: string): Promise<v
|
||||
return launchPackage(ctx, ehpkPath);
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch an app straight from a live web server instead of an .ehpk — the
|
||||
* Developer app's "Load app from URL" / "Load app from QR code" entries, for
|
||||
* running an app off a dev server without packaging it first.
|
||||
*/
|
||||
export function openEvenHubUrl(ctx: AppContext, url: string): Promise<void> {
|
||||
return launchUrl(ctx, url);
|
||||
}
|
||||
|
||||
/** Whether a string can be loaded by openEvenHubUrl, for validating input. */
|
||||
export function isLoadableAppUrl(url: string): boolean {
|
||||
return normalizeAppUrl(url) !== null;
|
||||
}
|
||||
|
||||
const evenhubApp: AppDefinition = {
|
||||
appId: "evenhub",
|
||||
title: "EvenHub",
|
||||
|
||||
@@ -18,7 +18,8 @@ import {
|
||||
readBinaryFile,
|
||||
writeBinaryFile,
|
||||
} from "../../native/file-access";
|
||||
import { parseEhpk, parseManifest, utf8Decode } from "./ehpk";
|
||||
import { parseEhpk, parseManifest, utf8Decode, type EvenHubManifest } from "./ehpk";
|
||||
import { type EvenHubPermission } from "./permissions";
|
||||
import { EvenHubSession } from "./session";
|
||||
import { createEvenHubWindow } from "./evenhub-window";
|
||||
import { createEvenHubWebView, type EvenHubWebView } from "./webview";
|
||||
@@ -96,8 +97,23 @@ export async function launchPackage(
|
||||
}
|
||||
}
|
||||
|
||||
await startApp(ctx, manifest, `${baseDir}/dist`, "", appId);
|
||||
}
|
||||
|
||||
/**
|
||||
* Shared tail of every launch: build the session and its WebView, register the
|
||||
* pair, and open the glasses window. `distDir` serves a packaged app offline;
|
||||
* `remoteUrl`, when set, loads the app from the network instead.
|
||||
*/
|
||||
async function startApp(
|
||||
ctx: AppContext,
|
||||
manifest: EvenHubManifest,
|
||||
distDir: string,
|
||||
remoteUrl: string,
|
||||
appId: string,
|
||||
): Promise<void> {
|
||||
const windowId = `evenhub:app:${nextSerial++}`;
|
||||
const session = new EvenHubSession(manifest, `${baseDir}/dist`, ctx.appendLog);
|
||||
const session = new EvenHubSession(manifest, distDir, ctx.appendLog, remoteUrl);
|
||||
const webView = createEvenHubWebView(session);
|
||||
session.attachWebView({
|
||||
evaluateJs: webView.evaluateJs,
|
||||
@@ -119,6 +135,61 @@ export async function launchPackage(
|
||||
);
|
||||
}
|
||||
|
||||
/**
|
||||
* Launch an EvenHub app served by a live web server (the Developer app's
|
||||
* "Load app from URL" / QR flows), rather than one unpacked from an .ehpk.
|
||||
*
|
||||
* There is no manifest to read, so one is synthesized: the URL identifies the
|
||||
* app (a stable packageId keeps its localStorage across reloads) and every
|
||||
* permission is treated as declared, on the grounds that the user pointed
|
||||
* Faceclaw at this URL themselves. Loading the same URL again replaces the
|
||||
* running instance, which is what a reload after an edit should do.
|
||||
*/
|
||||
export async function launchUrl(ctx: AppContext, url: string): Promise<void> {
|
||||
const normalized = normalizeAppUrl(url);
|
||||
if (!normalized) {
|
||||
ctx.appendLog(`evenhub: not an http(s) URL: ${url}`);
|
||||
throw new Error("Not an http:// or https:// URL.");
|
||||
}
|
||||
const manifest = synthesizeUrlManifest(normalized);
|
||||
closeRunningPackage(manifest.packageId);
|
||||
await startApp(ctx, manifest, "", normalized, "evenhub");
|
||||
}
|
||||
|
||||
/** Accept a bare "example.com/app" as https; reject anything not http(s). */
|
||||
export function normalizeAppUrl(raw: string): string | null {
|
||||
const trimmed = raw.trim();
|
||||
if (!trimmed) return null;
|
||||
const withScheme = /^[a-z][a-z0-9+.-]*:\/\//i.test(trimmed) ? trimmed : `https://${trimmed}`;
|
||||
if (!/^https?:\/\/[^/?#\s]+/i.test(withScheme)) return null;
|
||||
return withScheme;
|
||||
}
|
||||
|
||||
/** Every permission name the host knows how to honor, granted to URL apps. */
|
||||
const DEV_URL_PERMISSIONS: EvenHubPermission[] = [
|
||||
{ name: "network" },
|
||||
{ name: "location" },
|
||||
{ name: "g2-microphone" },
|
||||
{ name: "phone-microphone" },
|
||||
];
|
||||
|
||||
function synthesizeUrlManifest(url: string): EvenHubManifest {
|
||||
const match = /^https?:\/\/([^/?#]+)([^?#]*)/i.exec(url);
|
||||
const authority = match?.[1] ?? url;
|
||||
const path = (match?.[2] ?? "").replace(/\/index\.html?$/i, "").replace(/\/+$/, "");
|
||||
const idSource = `${authority}${path}`.toLowerCase().replace(/[^a-z0-9.-]+/g, "-");
|
||||
return {
|
||||
packageId: `dev.url.${idSource.slice(0, 96)}`,
|
||||
name: authority,
|
||||
version: "dev",
|
||||
entrypoint: "",
|
||||
networkWhitelist: [],
|
||||
permissions: DEV_URL_PERMISSIONS,
|
||||
privacyPolicyUrl: "",
|
||||
raw: { url },
|
||||
};
|
||||
}
|
||||
|
||||
/** Launch an installed app as a launcher-addressable singleton. */
|
||||
export function launchInstalledPackage(ctx: AppContext, app: InstalledEvenHubApp): Promise<void> {
|
||||
return launchPackage(ctx, installedEvenHubPackagePath(app.packageId), {
|
||||
|
||||
@@ -314,6 +314,12 @@ export type EvenHubWindowHooks = {
|
||||
export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenHubCompassClient {
|
||||
readonly manifest: EvenHubManifest;
|
||||
readonly distDir: string;
|
||||
/**
|
||||
* For a developer-loaded app served from a live web server: the absolute
|
||||
* URL to open. Empty for a packaged app, which is served offline out of
|
||||
* distDir instead (see webview.ts).
|
||||
*/
|
||||
readonly remoteUrl: string;
|
||||
|
||||
private page: EvenHubPage | null = null;
|
||||
private pageCreated = false;
|
||||
@@ -352,9 +358,15 @@ export class EvenHubSession implements EvenHubMicClient, EvenHubImuClient, EvenH
|
||||
private readonly grantedApiKeys = new Set<string>();
|
||||
private log: (message: string) => void;
|
||||
|
||||
constructor(manifest: EvenHubManifest, distDir: string, log: (message: string) => void) {
|
||||
constructor(
|
||||
manifest: EvenHubManifest,
|
||||
distDir: string,
|
||||
log: (message: string) => void,
|
||||
remoteUrl = "",
|
||||
) {
|
||||
this.manifest = manifest;
|
||||
this.distDir = distDir;
|
||||
this.remoteUrl = remoteUrl;
|
||||
this.log = log;
|
||||
}
|
||||
|
||||
|
||||
@@ -10,6 +10,11 @@
|
||||
* from a fake per-app origin plus document-start injection of the
|
||||
* flutter_inappwebview bridge shim) and so the page keeps running JS while the
|
||||
* host app is backgrounded (see FaceclawEvenHubWebView / the host).
|
||||
*
|
||||
* A session with a remoteUrl (the Developer app's "Load app from URL") skips
|
||||
* the offline serving entirely and loads that URL from the network; the shim
|
||||
* is registered as a document-start script instead of being spliced into the
|
||||
* served HTML.
|
||||
*/
|
||||
import { Application, Utils } from "@nativescript/core";
|
||||
import { EVENHUB_BRIDGE_INJECT_SCRIPT, buildFaceclawExtensionsScript, type EvenHubSession } from "./session";
|
||||
@@ -33,6 +38,15 @@ function originHost(session: EvenHubSession): string {
|
||||
return `${safe}.evenhub.invalid`;
|
||||
}
|
||||
|
||||
/**
|
||||
* The allowed-origin rule for a remote app's document-start script: the
|
||||
* loaded URL's own origin, so the shim isn't handed to third-party frames.
|
||||
*/
|
||||
function originRule(url: string): string {
|
||||
const match = /^(https?:\/\/[^/?#]+)/i.exec(url);
|
||||
return match ? match[1]! : "*";
|
||||
}
|
||||
|
||||
/**
|
||||
* Build the app's WebView, wire it to the session, attach it to the keep-alive
|
||||
* host, and start loading. Must run on the main thread.
|
||||
@@ -56,12 +70,26 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
},
|
||||
});
|
||||
|
||||
const host = originHost(session);
|
||||
const remoteUrl = session.remoteUrl;
|
||||
// Remote apps load from their own server, so nothing is intercepted and the
|
||||
// fake per-app origin is unused.
|
||||
const host = remoteUrl ? "" : originHost(session);
|
||||
// Stock bridge shim first, then the Faceclaw extensions global (both at
|
||||
// document-start, before any app JS).
|
||||
const injectScript = EVENHUB_BRIDGE_INJECT_SCRIPT + buildFaceclawExtensionsScript(`Faceclaw/${FACECLAW_VERSION}`);
|
||||
const documentStartInstalled = remoteUrl
|
||||
? com.faceclaw.app.FaceclawEvenHubDocumentStart.install(webView, injectScript, originRule(remoteUrl)) === true
|
||||
: false;
|
||||
webView.setWebViewClient(
|
||||
new com.faceclaw.app.FaceclawEvenHubWebViewClient(session.distDir, host, injectScript, listener),
|
||||
new com.faceclaw.app.FaceclawEvenHubWebViewClient(
|
||||
session.distDir,
|
||||
host,
|
||||
injectScript,
|
||||
// onPageStarted injection is the fallback when the document-start API
|
||||
// is unavailable; packaged apps get the shim spliced into their HTML.
|
||||
!!remoteUrl && !documentStartInstalled,
|
||||
listener,
|
||||
),
|
||||
);
|
||||
webView.addJavascriptInterface(new com.faceclaw.app.FaceclawEvenHubJsBridge(listener), "__faceclawEvenHub");
|
||||
|
||||
@@ -79,7 +107,7 @@ export function createEvenHubWebView(session: EvenHubSession): EvenHubWebView {
|
||||
|
||||
const nativeHost = com.faceclaw.app.FaceclawEvenHubWebViewHost.getInstance();
|
||||
nativeHost.attach(activity, webView);
|
||||
webView.loadUrl(`https://${host}/${session.manifest.entrypoint}`);
|
||||
webView.loadUrl(remoteUrl || `https://${host}/${session.manifest.entrypoint}`);
|
||||
|
||||
return {
|
||||
native: webView,
|
||||
|
||||
Reference in new issue
Block a user