browser-manifest.ts 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199
  1. /**
  2. * Shared native messaging manifest helpers for browser bridge installers.
  3. *
  4. * Keep this module side-effect free: CLI commands, daemon install APIs, and
  5. * tests import it before touching the filesystem. The browser bridge has one
  6. * stable Chrome extension ID, while each Mavis profile gets an isolated native
  7. * messaging host name and manifest path.
  8. */
  9. import os from 'node:os';
  10. import path from 'node:path';
  11. /**
  12. * Fixed Chrome extension ID derived from the public key embedded in
  13. * `extension/manifest.json`. Chrome derives the ID by hashing the manifest
  14. * `key`; `scripts/verify-extension-id.sh` guards this constant against drift.
  15. */
  16. export const MAVIS_BROWSER_EXTENSION_ID = 'ppnnfacnjgokfmbngkgbdgiigpbfgdba';
  17. export interface NativeMessagingManifest {
  18. name: string;
  19. description: string;
  20. path: string;
  21. type: 'stdio';
  22. allowed_origins: string[];
  23. }
  24. export interface ManifestOptions {
  25. /** Mavis profile name (e.g. "default", "dev") */
  26. profile: string;
  27. /** Absolute path to the host wrapper script */
  28. hostPath: string;
  29. }
  30. /** Supported Chromium-based browsers. */
  31. export type BrowserName = 'chrome' | 'chromium' | 'brave' | 'edge' | 'arc';
  32. export interface BrowserHostDir {
  33. browser: BrowserName;
  34. dir: string;
  35. }
  36. /** Registry key path for a given browser's NativeMessagingHosts. */
  37. export interface WindowsRegistryEntry {
  38. browser: BrowserName;
  39. /** Full registry key, e.g. HKCU\SOFTWARE\Google\Chrome\NativeMessagingHosts\<host> */
  40. key: string;
  41. }
  42. /**
  43. * Compute the native messaging host name for a Mavis profile.
  44. *
  45. * Native messaging host names are global per browser profile. Encoding the
  46. * Mavis profile into the host name lets `default`, worktree, and test daemons
  47. * register independently instead of clobbering each other's manifests.
  48. */
  49. export function getHostName(profile: string): string {
  50. const sanitized = profile.toLowerCase().replace(/[^a-z0-9]/g, '_');
  51. return `com.mavis.browser_${sanitized}`;
  52. }
  53. /** Generate a native messaging manifest object for a given profile. */
  54. export function generateManifest(opts: ManifestOptions): NativeMessagingManifest {
  55. return {
  56. name: getHostName(opts.profile),
  57. description: `Mavis browser native messaging host (profile: ${opts.profile})`,
  58. path: opts.hostPath,
  59. type: 'stdio',
  60. allowed_origins: [`chrome-extension://${MAVIS_BROWSER_EXTENSION_ID}/`],
  61. };
  62. }
  63. /** Serialize a manifest to JSON string with a trailing newline. */
  64. export function serializeManifest(manifest: NativeMessagingManifest): string {
  65. return `${JSON.stringify(manifest, null, 2)}\n`;
  66. }
  67. /**
  68. * Get NativeMessagingHosts directories for all supported Chromium browsers.
  69. *
  70. * macOS/Linux discover manifests by scanning per-browser directories. Windows
  71. * discovers manifests via registry keys; the directory returned here is the
  72. * shared file location those registry values point at.
  73. */
  74. export function getNativeHostDirs(platform: string = process.platform): BrowserHostDir[] {
  75. if (platform === 'win32') {
  76. // Chrome-family browsers on Windows do not scan arbitrary filesystem
  77. // locations. We write all manifests to one stable user-local directory,
  78. // then register one HKCU key per browser in getWindowsRegistryEntries().
  79. const base = path.join(
  80. process.env.LOCALAPPDATA || path.join(os.homedir(), 'AppData', 'Local'),
  81. 'Mavis',
  82. 'NativeMessagingHosts',
  83. );
  84. return [
  85. { browser: 'chrome', dir: base },
  86. { browser: 'chromium', dir: base },
  87. { browser: 'brave', dir: base },
  88. { browser: 'edge', dir: base },
  89. ];
  90. }
  91. if (platform === 'darwin') {
  92. const base = path.join(os.homedir(), 'Library', 'Application Support');
  93. return [
  94. { browser: 'chrome', dir: path.join(base, 'Google', 'Chrome', 'NativeMessagingHosts') },
  95. { browser: 'chromium', dir: path.join(base, 'Chromium', 'NativeMessagingHosts') },
  96. {
  97. browser: 'brave',
  98. dir: path.join(base, 'BraveSoftware', 'Brave-Browser', 'NativeMessagingHosts'),
  99. },
  100. { browser: 'edge', dir: path.join(base, 'Microsoft Edge', 'NativeMessagingHosts') },
  101. { browser: 'arc', dir: path.join(base, 'Arc', 'User Data', 'NativeMessagingHosts') },
  102. ];
  103. }
  104. const base = path.join(os.homedir(), '.config');
  105. return [
  106. { browser: 'chrome', dir: path.join(base, 'google-chrome', 'NativeMessagingHosts') },
  107. { browser: 'chromium', dir: path.join(base, 'chromium', 'NativeMessagingHosts') },
  108. {
  109. browser: 'brave',
  110. dir: path.join(base, 'BraveSoftware', 'Brave-Browser', 'NativeMessagingHosts'),
  111. },
  112. { browser: 'edge', dir: path.join(base, 'microsoft-edge', 'NativeMessagingHosts') },
  113. ];
  114. }
  115. /** Get the manifest file name for a given profile. */
  116. export function getManifestFileName(profile: string): string {
  117. return `${getHostName(profile)}.json`;
  118. }
  119. /** Get the full manifest file path for a given browser host dir and profile. */
  120. export function getManifestPath(hostDir: string, profile: string): string {
  121. return path.join(hostDir, getManifestFileName(profile));
  122. }
  123. /**
  124. * Generate a wrapper script that launches native-host.cjs with profile env.
  125. *
  126. * Chrome's native messaging manifest accepts a single executable path only;
  127. * the wrapper injects MAVIS_PROFILE and MAVIS_BROWSER_BROKER_SOCKET before
  128. * handing control to Node/native-host.cjs.
  129. */
  130. export function generateHostWrapper(opts: {
  131. nodePath: string;
  132. nativeHostCjsPath: string;
  133. profile: string;
  134. brokerSocketPath: string;
  135. }): string {
  136. if ((process.platform as string) === 'win32') {
  137. return [
  138. '@echo off',
  139. `set MAVIS_PROFILE=${opts.profile}`,
  140. `set MAVIS_BROWSER_BROKER_SOCKET=${opts.brokerSocketPath}`,
  141. `"${opts.nodePath}" "${opts.nativeHostCjsPath}"`,
  142. '',
  143. ].join('\r\n');
  144. }
  145. return [
  146. '#!/bin/sh',
  147. `# Mavis browser native host wrapper (profile: ${opts.profile})`,
  148. '# Auto-generated by Mavis desktop browser integration — do not edit manually',
  149. `export MAVIS_PROFILE="${opts.profile}"`,
  150. `export MAVIS_BROWSER_BROKER_SOCKET="${opts.brokerSocketPath}"`,
  151. `exec "${opts.nodePath}" "${opts.nativeHostCjsPath}"`,
  152. '',
  153. ].join('\n');
  154. }
  155. /**
  156. * Return the per-browser HKCU registry keys used on Windows.
  157. *
  158. * The default value of each key must be the absolute manifest JSON path for
  159. * that browser. Install/uninstall code owns writing/removing those values.
  160. */
  161. export function getWindowsRegistryEntries(hostName: string): WindowsRegistryEntry[] {
  162. const base = 'HKCU\\SOFTWARE';
  163. return [
  164. {
  165. browser: 'chrome',
  166. key: `${base}\\Google\\Chrome\\NativeMessagingHosts\\${hostName}`,
  167. },
  168. {
  169. browser: 'chromium',
  170. key: `${base}\\Chromium\\NativeMessagingHosts\\${hostName}`,
  171. },
  172. {
  173. browser: 'brave',
  174. key: `${base}\\BraveSoftware\\Brave-Browser\\NativeMessagingHosts\\${hostName}`,
  175. },
  176. {
  177. browser: 'edge',
  178. key: `${base}\\Microsoft\\Edge\\NativeMessagingHosts\\${hostName}`,
  179. },
  180. ];
  181. }