* @fileoverview Entry point for the Image Loader's service worker.
*/
import {assert} from 'chrome://resources/js/assert.js';
import type {LoadImageRequest, LoadImageResponse} from './load_image_request.js';
import type {PrivateApi} from './sw_od_messages.js';
const EXTENSION_ID = 'pmfjbimdmchhbnneeidfognadeopoehp';
const ALLOW_LISTED_FILE_MANAGER_SWA = 'chrome://file-manager';
const ALLOW_LISTED_NATIVE = 'com.google.ash_thumbnail_loader';
let creatingOffscreenDocument: Promise<void>|undefined;
async function setupOffscreenDocument() {
const url = chrome.runtime.getURL('offscreen.html');
const existingContexts = await chrome.runtime.getContexts({
contextTypes: [chrome.runtime.ContextType.OFFSCREEN_DOCUMENT],
documentUrls: [url],
});
if (existingContexts.length > 0) {
return;
}
if (creatingOffscreenDocument) {
await creatingOffscreenDocument;
return;
}
creatingOffscreenDocument = chrome.offscreen.createDocument({
url: url,
reasons: [chrome.offscreen.Reason.USER_MEDIA],
justification: 'Lets us use <img>, <canvas> and XMLHttpRequest',
});
await creatingOffscreenDocument;
creatingOffscreenDocument = undefined;
}
async function sendToOffscreenDocument(
msg: LoadImageRequest, senderOrigin: string,
sendResponse: (r: LoadImageResponse) => void) {
assert(msg);
try {
await setupOffscreenDocument();
} catch (e) {
console.error('setupOffscreenDocument:', e);
return;
}
msg.imageLoaderRequestId = senderOrigin + ':' + msg.taskId;
chrome.runtime.sendMessage(
null, msg, undefined, (response: LoadImageResponse) => {
sendResponse(response);
});
}
if (chrome.runtime.getManifest().version !== '0.5') {
chrome.runtime.reload();
} else {
chrome.runtime.onMessage.addListener(
(msg: PrivateApi, sender: chrome.runtime.MessageSender,
sendResponse: (thumbnailDataUrl: string) => void) => {
if (sender.id !== EXTENSION_ID) {
return false;
} else if (msg.apiMethod === 'getArcDocumentsProviderThumbnail') {
chrome.imageLoaderPrivate.getArcDocumentsProviderThumbnail(
msg.params.url,
msg.params.widthHint,
msg.params.heightHint,
sendResponse,
);
return true;
} else if (msg.apiMethod === 'getDriveThumbnail') {
chrome.imageLoaderPrivate.getDriveThumbnail(
msg.params.url,
msg.params.cropToSquare,
sendResponse,
);
return true;
} else if (msg.apiMethod === 'getPdfThumbnail') {
chrome.imageLoaderPrivate.getPdfThumbnail(
msg.params.url,
msg.params.width,
msg.params.height,
sendResponse,
);
return true;
}
return false;
});
chrome.runtime.onMessageExternal.addListener(
(msg: LoadImageRequest, sender: chrome.runtime.MessageSender,
sendResponse: (r: LoadImageResponse) => void) => {
if (!msg || (sender.origin !== ALLOW_LISTED_FILE_MANAGER_SWA)) {
return false;
}
sendToOffscreenDocument(
msg, ALLOW_LISTED_FILE_MANAGER_SWA, sendResponse);
return !msg.cancel;
});
chrome.runtime.onConnectNative.addListener((port: chrome.runtime.Port) => {
assert(port.sender);
if (port.sender.nativeApplication !== ALLOW_LISTED_NATIVE) {
port.disconnect();
return;
}
port.onMessage.addListener((msg: LoadImageRequest) => {
if (!msg) {
port.disconnect();
return;
} else if (msg.cancel) {
port.disconnect();
}
sendToOffscreenDocument(
msg, ALLOW_LISTED_NATIVE, (response: LoadImageResponse) => {
assert(!msg.cancel);
port.postMessage(response);
port.disconnect();
});
});
});
}