Skip to content

Electron: Protocol #33

@xwcoder

Description

@xwcoder

Electron: Protocol

Protocol module is used to register a custom protocol and intercept existing protocol requests.

What is scheme

RFC 3986

IMAGE

Primary methods

protocol.registerSchemesAsPrivileged(customSchemes)

  • Code example

    // src/main.js
    const { protocol } = require('electron');
    
    protocol.registerSchemesAsPrivileged([
    	{
    		scheme: 'vscode-webview',
    		privileges: { standard: true, secure: true, supportFetchAPI: true, corsEnabled: true, allowServiceWorkers: true, }
    	},
    	{
    		scheme: 'vscode-file',
    		privileges: { secure: true, standard: true, supportFetchAPI: true, corsEnabled: true }
    	}
    ]);
  • Registers the scheme as standard, which means relative and absolute resources can be resolved correctly, allow to files through the FileSystem API , allow to use web storage apis, etc.

  • Non standard scheme will behave like the file protocol. file is not standard scheme.

  • Can be used before the ready event of the app module gets emitted and can be called only once.

protocol.registerFileProtocol(scheme, handler)

  • Registers a protocol of scheme that will send a file as the response.

  • Code example

    // vs/platform/protocol/electron-main/protocolMainService.ts
    
    // Register vscode-file:// handler
    defaultSession.protocol.registerFileProtocol('vscode-file', (request, callback) => this.handleResourceRequest(request, callback));

protocol.interceptFileProtocol(scheme, handler)

  • Intercepts scheme protocol and uses handler as the protocol's new handler which sends a file as a response.

  • Code example

    // vs/platform/protocol/electron-main/protocolMainService.ts
    
    // Block any file:// access
    defaultSession.protocol.interceptFileProtocol('file', (request, callback) => this.handleFileRequest(request, callback));
    
    ...
    
    private handleFileRequest(request: Electron.ProtocolRequest, callback: ProtocolCallback) {
    		const uri = URI.parse(request.url);
    
    		this.logService.error(`Refused to load resource ${uri.fsPath} from ${Schemas.file}: protocol (original URL: ${request.url})`);
    
    		return callback({ error: -3 /* ABORTED */ });
    	}

protocol.registerXXXProtocol(scheme, handler)

protocol.interceptXXXProtocol(scheme, handler)

Using protocol with a custom partition or session

  • A protocol is registered to a specific Electron session object.

  • If not specify a session, the protocol will be applied to the default session.

  • Code example

    const { session, app, protocol } = require('electron')
    const path = require('path')
    
    app.whenReady().then(() => {
      const partition = 'persist:example'
      const ses = session.fromPartition(partition)
    
      ses.protocol.registerFileProtocol('atom', (request, callback) => {
        const url = request.url.substr(7)
        callback({ path: path.normalize(`${__dirname}/${url}`) })
      })
    
      mainWindow = new BrowserWindow({ webPreferences: { partition } })
    })

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions