nativehost

package
v1.4.3 Latest Latest
Warning

This package is not in the latest version of its module.

Go to latest
Published: Mar 12, 2026 License: MIT Imports: 9 Imported by: 0

Documentation

Overview

Package nativehost implements the native messaging host protocol for browser extensions. It provides stdin/stdout communication using Chrome/Firefox native messaging format: 4-byte little-endian length prefix followed by JSON payload.

Index

Constants

View Source
const HostName = "com.warpdl.host"

HostName is the native messaging host identifier. This must match the "name" field in the manifest and be used by browser extensions.

View Source
const MaxMessageSize = common.MaxMessageSize

MaxMessageSize limits native messaging payloads. Browser native messaging has a 1MB limit, but we use common.MaxMessageSize for consistency.

View Source
const OfficialChromeExtensionID = ""

OfficialChromeExtensionID is the official Chrome extension ID for WarpDL. This will be populated once the browser extension is published to the Chrome Web Store.

View Source
const OfficialFirefoxExtensionID = ""

OfficialFirefoxExtensionID is the official Firefox extension ID for WarpDL. This will be populated once the browser extension is published to Firefox Add-ons.

Variables

This section is empty.

Functions

func GenerateChromeManifest

func GenerateChromeManifest(hostPath, extensionID string) []byte

GenerateChromeManifest creates a Chrome/Chromium native messaging manifest.

func GenerateFirefoxManifest

func GenerateFirefoxManifest(hostPath, extensionID string) []byte

GenerateFirefoxManifest creates a Firefox native messaging manifest.

func HasOfficialExtensions added in v1.3.28

func HasOfficialExtensions() bool

HasOfficialExtensions returns true if at least one official extension ID is configured. Package manager hooks use this to determine if native host installation should proceed.

func MakeErrorResponse

func MakeErrorResponse(id int, err error) []byte

MakeErrorResponse creates a JSON-encoded error response.

func MakeSuccessResponse

func MakeSuccessResponse(id int, result any) []byte

MakeSuccessResponse creates a JSON-encoded success response.

func ReadMessage

func ReadMessage(r io.Reader) ([]byte, error)

ReadMessage reads a native messaging format message from the reader. Format: 4-byte little-endian length prefix followed by the message bytes.

func UninstallManifest

func UninstallManifest(path string) error

UninstallManifest removes a manifest file.

func WriteMessage

func WriteMessage(w io.Writer, msg []byte) error

WriteMessage writes a message in native messaging format to the writer. Format: 4-byte little-endian length prefix followed by the message bytes.

Types

type Browser

type Browser string

Browser represents a supported browser for native messaging.

const (
	BrowserChrome   Browser = "chrome"
	BrowserFirefox  Browser = "firefox"
	BrowserChromium Browser = "chromium"
	BrowserEdge     Browser = "edge"
	BrowserBrave    Browser = "brave"
)

func SupportedBrowsers

func SupportedBrowsers() []Browser

SupportedBrowsers returns all browsers that support native messaging.

type ChromeManifest

type ChromeManifest struct {
	Name           string   `json:"name"`
	Description    string   `json:"description"`
	Path           string   `json:"path"`
	Type           string   `json:"type"`
	AllowedOrigins []string `json:"allowed_origins"`
}

ChromeManifest represents Chrome/Chromium native messaging host manifest.

type Client

type Client interface {
	Download(url, fileName, downloadDirectory string, opts *DownloadOptions) (*common.DownloadResponse, error)
	List(opts *ListOptions) (*common.ListResponse, error)
	GetDaemonVersion() (*common.VersionResponse, error)
	StopDownload(downloadId string) (bool, error)
	Resume(downloadId string, opts *ResumeOptions) (*common.ResumeResponse, error)
	Flush(downloadId string) (bool, error)
	Close() error
}

Client interface defines the daemon client methods used by the native host. This allows mocking for tests and decouples from the concrete warpcli.Client.

type DownloadOptions

type DownloadOptions struct {
	Headers        map[string]string `json:"headers,omitempty"`
	ForceParts     bool              `json:"force_parts,omitempty"`
	MaxConnections int32             `json:"max_connections,omitempty"`
	MaxSegments    int32             `json:"max_segments,omitempty"`
	Overwrite      bool              `json:"overwrite,omitempty"`
	Proxy          string            `json:"proxy,omitempty"`
	Timeout        int               `json:"timeout,omitempty"`
	SpeedLimit     string            `json:"speed_limit,omitempty"`
}

DownloadOptions mirrors warpcli.DownloadOpts

type DownloadParams

type DownloadParams struct {
	URL               string            `json:"url"`
	FileName          string            `json:"fileName"`
	DownloadDirectory string            `json:"downloadDirectory"`
	Headers           map[string]string `json:"headers,omitempty"`
	ForceParts        bool              `json:"forceParts,omitempty"`
	MaxConnections    int32             `json:"maxConnections,omitempty"`
	MaxSegments       int32             `json:"maxSegments,omitempty"`
	Overwrite         bool              `json:"overwrite,omitempty"`
	Proxy             string            `json:"proxy,omitempty"`
	Timeout           int               `json:"timeout,omitempty"`
	SpeedLimit        string            `json:"speedLimit,omitempty"`
}

DownloadParams represents parameters for a download request.

type FirefoxManifest

type FirefoxManifest struct {
	Name              string   `json:"name"`
	Description       string   `json:"description"`
	Path              string   `json:"path"`
	Type              string   `json:"type"`
	AllowedExtensions []string `json:"allowed_extensions"`
}

FirefoxManifest represents Firefox native messaging host manifest.

type FlushParams

type FlushParams struct {
	DownloadID string `json:"downloadId"`
}

FlushParams represents parameters for a flush request.

type Host

type Host struct {
	// contains filtered or unexported fields
}

Host is the native messaging host that bridges browser extensions to the daemon.

func NewHost

func NewHost(client Client) *Host

NewHost creates a new native messaging host with the given client. Uses os.Stdin and os.Stdout for communication.

func (*Host) Run

func (h *Host) Run() error

Run starts the native messaging host main loop. It reads requests from stdin, processes them, and writes responses to stdout. Returns when stdin is closed (EOF) or an unrecoverable error occurs.

type ListOptions

type ListOptions struct {
	IncludeHidden   bool `json:"include_hidden,omitempty"`
	IncludeMetadata bool `json:"include_metadata,omitempty"`
}

ListOptions mirrors warpcli.ListOpts

type ListParams

type ListParams struct {
	IncludeHidden   bool `json:"includeHidden,omitempty"`
	IncludeMetadata bool `json:"includeMetadata,omitempty"`
}

ListParams represents parameters for a list request.

type ManifestInstaller

type ManifestInstaller struct {
	HostPath           string
	ChromeExtensionID  string
	FirefoxExtensionID string
	BaseDir            string // Override for testing; empty uses real home dir
}

ManifestInstaller handles installation and removal of native messaging manifests.

func (*ManifestInstaller) InstallChrome

func (m *ManifestInstaller) InstallChrome(browser Browser) (string, error)

InstallChrome installs a manifest for Chrome-based browsers.

func (*ManifestInstaller) InstallFirefox

func (m *ManifestInstaller) InstallFirefox() (string, error)

InstallFirefox installs a manifest for Firefox.

func (*ManifestInstaller) Validate

func (m *ManifestInstaller) Validate() error

Validate checks that all required fields are set.

type Request

type Request struct {
	ID      int             `json:"id"`
	Method  string          `json:"method"`
	Message json.RawMessage `json:"message,omitempty"`
}

Request represents an incoming native messaging request from a browser extension. It includes an ID for request-response correlation, which the daemon protocol lacks.

func ParseRequest

func ParseRequest(b []byte) (*Request, error)

ParseRequest parses a JSON byte slice into a Request struct.

type Response

type Response struct {
	ID     int    `json:"id"`
	Ok     bool   `json:"ok"`
	Error  string `json:"error,omitempty"`
	Result any    `json:"result,omitempty"`
}

Response represents a native messaging response sent back to the browser extension.

type ResumeOptions

type ResumeOptions struct {
	Headers        map[string]string `json:"headers,omitempty"`
	ForceParts     bool              `json:"force_parts,omitempty"`
	MaxConnections int32             `json:"max_connections,omitempty"`
	MaxSegments    int32             `json:"max_segments,omitempty"`
	Proxy          string            `json:"proxy,omitempty"`
	Timeout        int               `json:"timeout,omitempty"`
	SpeedLimit     string            `json:"speed_limit,omitempty"`
}

ResumeOptions mirrors warpcli.ResumeOpts

type ResumeParams

type ResumeParams struct {
	DownloadID     string            `json:"downloadId"`
	Headers        map[string]string `json:"headers,omitempty"`
	ForceParts     bool              `json:"forceParts,omitempty"`
	MaxConnections int32             `json:"maxConnections,omitempty"`
	MaxSegments    int32             `json:"maxSegments,omitempty"`
	Proxy          string            `json:"proxy,omitempty"`
	Timeout        int               `json:"timeout,omitempty"`
	SpeedLimit     string            `json:"speedLimit,omitempty"`
}

ResumeParams represents parameters for a resume request.

type StopParams

type StopParams struct {
	DownloadID string `json:"downloadId"`
}

StopParams represents parameters for a stop request.

Jump to

Keyboard shortcuts

? : This menu
/ : Search site
f or F : Jump to
y or Y : Canonical URL