diff --git a/capsules/home-gui/browser/home-gui.js b/capsules/home-gui/browser/home-gui.js index 49fc750be..3c00a8c0c 100644 --- a/capsules/home-gui/browser/home-gui.js +++ b/capsules/home-gui/browser/home-gui.js @@ -18,6 +18,7 @@ import { toolbarInboxButton, toolbarSignOutButton, ensureHomeGuiDom, + fetchBlob, initializeRecentTargets, initializeShellLayout, rememberSharedUiPreferences, @@ -900,11 +901,52 @@ export function syncHomeGuiAppearance(summary) { } desktopBackdrop.dataset.overlay = overlayEnabled ? "true" : "false"; desktopBackdrop.style.setProperty("--desktop-overlay-opacity", String(overlayOpacity)); + void applyDesktopWallpaper(imageUrl); +} + +// The summary URL carries `v=` so it changes whenever the user uploads or +// resets the image; that string is the cache key for the loaded object URL. +const wallpaperLoad = { sourceUrl: "", objectUrl: "" }; + +function clearDesktopWallpaper() { + desktopBackdrop.style.removeProperty("--desktop-wallpaper"); + if (wallpaperLoad.objectUrl) { + URL.revokeObjectURL(wallpaperLoad.objectUrl); + } + wallpaperLoad.objectUrl = ""; +} + +async function applyDesktopWallpaper(imageUrl) { + if (wallpaperLoad.sourceUrl === imageUrl) { + return; + } + wallpaperLoad.sourceUrl = imageUrl; if (!imageUrl) { - desktopBackdrop.style.removeProperty("--desktop-wallpaper"); + clearDesktopWallpaper(); return; } - desktopBackdrop.style.setProperty("--desktop-wallpaper", `url("${imageUrl}")`); + let objectUrl = ""; + try { + objectUrl = URL.createObjectURL(await fetchBlob(imageUrl)); + } catch (error) { + // Fail closed to the default wallpaper instead of a broken image. The + // source URL stays recorded so summary refreshes do not retry until the + // Runtime advertises a new image version. + if (wallpaperLoad.sourceUrl === imageUrl) { + clearDesktopWallpaper(); + } + console.warn("home-gui wallpaper unavailable", error); + return; + } + if (wallpaperLoad.sourceUrl !== imageUrl) { + URL.revokeObjectURL(objectUrl); + return; + } + if (wallpaperLoad.objectUrl) { + URL.revokeObjectURL(wallpaperLoad.objectUrl); + } + wallpaperLoad.objectUrl = objectUrl; + desktopBackdrop.style.setProperty("--desktop-wallpaper", `url("${objectUrl}")`); } function targetsChanged(previous, next) { diff --git a/capsules/home-gui/browser/shell-core.js b/capsules/home-gui/browser/shell-core.js index 099441231..5281d75e4 100644 --- a/capsules/home-gui/browser/shell-core.js +++ b/capsules/home-gui/browser/shell-core.js @@ -287,6 +287,23 @@ export async function fetchJson(url, init) { return response.json(); } +/** + * Fetch a binary Home resource with the launch token. Home GUI runs in an + * opaque sandboxed frame, so cookie-authenticated CSS `url()` loads cannot be + * relied on; this is the same authenticated path every other Home request uses. + */ +export async function fetchBlob(url) { + const response = await fetch(url, { + headers: homeGuiLaunchToken ? { "x-elastos-home-token": homeGuiLaunchToken } : {}, + }); + if (!response.ok) { + const error = new Error(`request failed: ${response.status} ${response.statusText}`); + error.status = response.status; + throw error; + } + return response.blob(); +} + export function allVisibleTargets(summary) { if (!summary || !Array.isArray(summary.targets)) { return []; diff --git a/capsules/home/browser/home-shell-host.js b/capsules/home/browser/home-shell-host.js index 67b931a86..93a12404e 100644 --- a/capsules/home/browser/home-shell-host.js +++ b/capsules/home/browser/home-shell-host.js @@ -107,6 +107,9 @@ const SHELL_MESSAGE_DELIVER_TARGET_SOURCES = Object.freeze({ documents: new Set(["chat-room"]), library: new Set(["archive-manager", "browser", "chat-room"]), }); +// Apps that may ask Home to make one of the person's own objects the wallpaper. +const SHELL_MESSAGE_DESKTOP_BACKGROUND_SOURCES = new Set(["library"]); +const DESKTOP_BACKGROUND_SOURCE_URI_MAX_LENGTH = 2048; const PASSKEY_STEP_UP_TARGETS = new Set(["inbox", SYSTEM_APP_ID, "wallet"]); const launchedAppContexts = new Map(); const pendingBrowserAuthorityRenewals = new Map(); @@ -1974,6 +1977,10 @@ window.addEventListener("message", (event) => { }); return; } + if (data.type === "home:set-desktop-background") { + handleSetDesktopBackgroundMessage(event, context, data); + return; + } if (data.type === "home:deliver-to-target") { const target = typeof data.target === "string" ? data.target.trim() : ""; if (!target || !canDeliverTargetFromHomeMessage(context, target)) { @@ -2334,6 +2341,62 @@ function canOpenTargetFromHomeMessage(context, target) { return policy.has(target); } +// The app expresses intent; Home performs the change with its own appearance +// authority, so the app never gains one. Home carries only the single-use +// ticket the Library frame asked Runtime for (one object, one minute), never +// Library's launch token, and Runtime reads the object under Library's scope, +// Trash, and protection rules. Home never reads or decrypts the object itself. +const DESKTOP_BACKGROUND_TICKET_PATTERN = /^[A-Za-z0-9_-]{43}$/; +const DESKTOP_BACKGROUND_MESSAGE_MAX_LENGTH = 600; + +function handleSetDesktopBackgroundMessage(event, context, data) { + const requestId = typeof data.requestId === "string" ? data.requestId.trim() : ""; + const uri = typeof data.uri === "string" ? data.uri.trim() : ""; + const ticket = typeof data.ticket === "string" ? data.ticket : ""; + if ( + context.kind !== "app-frame" || + !SHELL_MESSAGE_DESKTOP_BACKGROUND_SOURCES.has(context.targetId) || + !hasExactMessageKeys(data, ["type", "requestId", "homeToken", "uri", "ticket"]) || + !requestId || + requestId.length > 128 || + !uri.startsWith("localhost://Users/") || + uri.length > DESKTOP_BACKGROUND_SOURCE_URI_MAX_LENGTH || + !DESKTOP_BACKGROUND_TICKET_PATTERN.test(ticket) + ) { + console.warn("home ignored unauthorized set-desktop-background message", context.targetId); + replyToShellRequest(event, requestId, null, new Error("Home denied the desktop background change")); + return; + } + fetch("/api/apps/home/appearance/background-image", { + method: "POST", + headers: { "content-type": "application/json" }, + body: JSON.stringify({ source_uri: uri, library_ticket: ticket }), + }) + .then(async (response) => { + if (response.ok) { + requestShellSummaryRefresh({ reason: "desktop-background" }); + replyToShellRequest(event, requestId, { accepted: true }); + return; + } + // Runtime answers 400 with a plain sentence that tells the person what + // to do next (for example, which protected file to move back). Only + // that sentence goes back to Library; Library checks it again. + const detail = response.status === 400 ? (await response.text().catch(() => "")).trim() : ""; + console.warn("home could not set the desktop background", response.status); + replyToShellRequest( + event, + requestId, + detail && detail.length <= DESKTOP_BACKGROUND_MESSAGE_MAX_LENGTH + ? { accepted: false, message: detail } + : { accepted: false }, + ); + }) + .catch((error) => { + console.warn("home could not set the desktop background", error); + replyToShellRequest(event, requestId, { accepted: false }); + }); +} + function canDeliverTargetFromHomeMessage(context, target) { if (context.kind !== "app-frame" && context.kind !== "shell-frame") { return false; diff --git a/capsules/library/browser/index.html b/capsules/library/browser/index.html index 2f0ffa428..f0a72dc10 100644 --- a/capsules/library/browser/index.html +++ b/capsules/library/browser/index.html @@ -45,7 +45,6 @@

Favorites

-