Skip to content

Commit

Permalink
fix: Electron protocol.registerFile deprecation (Fixes #2672 )
Browse files Browse the repository at this point in the history
  • Loading branch information
danielweck committed Dec 31, 2024
1 parent 80061ab commit efbd949
Show file tree
Hide file tree
Showing 3 changed files with 67 additions and 26 deletions.
2 changes: 1 addition & 1 deletion scripts/afterPack.js
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ module.exports = async function afterPack(context) {
[FuseV1Options.LoadBrowserProcessSpecificV8Snapshot]: false,

// GrantFileProtocolExtraPrivileges = 7,
[FuseV1Options.GrantFileProtocolExtraPrivileges]: true, // TODO: make this FALSE! (file://)
[FuseV1Options.GrantFileProtocolExtraPrivileges]: false,
});

// if (context.electronPlatformName === 'linux') {
Expand Down
56 changes: 34 additions & 22 deletions src/main/redux/sagas/app.ts
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,7 @@
import * as debug_ from "debug";
import { app, protocol, ipcMain } from "electron";
import * as path from "path";
import * as fs from "fs";
import { takeSpawnEveryChannel } from "readium-desktop/common/redux/sagas/takeSpawnEvery";
import { tryDecodeURIComponent } from "readium-desktop/common/utils/uri";
import { closeProcessLock, diMainGet, getLibraryWindowFromDi, getAllReaderWindowFromDi } from "readium-desktop/main/di";
Expand Down Expand Up @@ -139,17 +140,25 @@ export function* init() {
});
}

// register file protocol to link locale file to renderer
protocol.registerFileProtocol("store",
(request, callback) => {

// Extract publication item relative url
const relativeUrl = request.url.substr(6);
const pubStorage = diMainGet("publication-storage");
const filePath: string = path.join(pubStorage.getRootPath(), relativeUrl);
callback(filePath);
},
);
const streamProtocolHandler_Store = (
request: Electron.ProtocolRequest,
callback: (response: (NodeJS.ReadableStream) | (Electron.ProtocolResponse)) => void,
) => {
debug("---streamProtocolHandler_Store");
debug(request);
const urlPath = request.url.substring("store://".length);
debug(urlPath);
// const urlPathDecoded = tryDecodeURIComponent(urlPath);
// debug(urlPathDecoded);
const pubStorage = diMainGet("publication-storage");
const rootPath = pubStorage.getRootPath();
debug(rootPath);
const filePath = path.join(rootPath, urlPath);
debug(filePath);
callback(fs.createReadStream(filePath));
};
protocol.registerStreamProtocol("store", streamProtocolHandler_Store);
// protocol.unregisterProtocol("store");

app.on("will-quit", () => {

Expand All @@ -158,18 +167,21 @@ export function* init() {
debug("#####");
});

const streamProtocolHandler_PDF = (
request: Electron.ProtocolRequest,
callback: (response: (NodeJS.ReadableStream) | (Electron.ProtocolResponse)) => void,
) => {
debug("---streamProtocolHandler_PDF");
debug(request);
const urlPath = request.url.substring("pdfjs-extract://host/".length);
debug(urlPath);
const urlPathDecoded = tryDecodeURIComponent(urlPath);
debug(urlPathDecoded);
callback(fs.createReadStream(urlPathDecoded));
};
protocol.registerStreamProtocol("pdfjs-extract", streamProtocolHandler_PDF);
// protocol.unregisterProtocol("pdfjs-extract");

protocol.registerFileProtocol("pdfjs-extract", async (request, callback) => {

debug("register file protocol pdfjs-extract");
debug("request", request);
const arg = request.url.split("pdfjs-extract://host/")[1];
debug(arg);
const p = tryDecodeURIComponent(arg);
debug(p);

callback(p);
});

yield call(() => {
const deviceIdManager = diMainGet("device-id-manager");
Expand Down
35 changes: 32 additions & 3 deletions src/main/streamer/streamerNoHttp.ts
Original file line number Diff line number Diff line change
Expand Up @@ -1142,12 +1142,39 @@ export function initSessions() {
{
privileges: {
allowServiceWorkers: false, // Default false
bypassCSP: true, // Default false
bypassCSP: false, // Default false
corsEnabled: false, // Default false
secure: true, // Default false
stream: true, // Default false
supportFetchAPI: false, // Default false
standard: false, // Default false
codeCache: false, // Default false (only works with standard=true)
},
scheme: "store",
},
{
privileges: {
allowServiceWorkers: false, // Default false
bypassCSP: false, // Default false
corsEnabled: false, // Default false
secure: true, // Default false
stream: true, // Default false
supportFetchAPI: false, // Default false
standard: false, // Default false
codeCache: false, // Default false (only works with standard=true)
},
scheme: "pdfjs-extract",
},
{
privileges: {
allowServiceWorkers: false, // Default false
bypassCSP: true, // Default false
corsEnabled: false, // Default false
secure: true, // Default false
stream: true, // Default false
supportFetchAPI: false, // Default false
standard: false, // Default false
codeCache: false, // Default false (only works with standard=true)
},
scheme: OPDS_MEDIA_SCHEME,
},
Expand All @@ -1157,9 +1184,10 @@ export function initSessions() {
bypassCSP: false,
corsEnabled: true,
secure: true,
standard: true,
stream: true,
supportFetchAPI: true,
standard: true, // Default false
codeCache: false, // Default false (only works with standard=true)
},
scheme: THORIUM_READIUM2_ELECTRON_HTTP_PROTOCOL,
}, {
Expand All @@ -1168,9 +1196,10 @@ export function initSessions() {
bypassCSP: false,
corsEnabled: true,
secure: true,
standard: true,
stream: true,
supportFetchAPI: true,
standard: true, // Default false
codeCache: false, // Default false (only works with standard=true)
},
scheme: READIUM2_ELECTRON_HTTP_PROTOCOL,
}]);
Expand Down

0 comments on commit efbd949

Please sign in to comment.