prelim chrome support

This commit is contained in:
Robin Appelman 2025-10-12 13:58:19 +02:00
commit c87c8c5682
2 changed files with 15 additions and 14 deletions

View file

@ -3,14 +3,15 @@ use std::str::FromStr;
pub enum Browser { pub enum Browser {
Firefox, Firefox,
#[allow(dead_code)]
Chrome, Chrome,
} }
impl Browser { impl Browser {
pub fn manifest_path(&self, name: &str) -> PathBuf { pub fn manifest_path(&self) -> PathBuf {
match self { match self {
Browser::Firefox => firefox_user_manifest(name), Browser::Firefox => firefox_user_manifest(),
Browser::Chrome => chrome_user_manifest(name), Browser::Chrome => chrome_user_manifest(),
} }
} }
} }
@ -21,7 +22,7 @@ impl FromStr for Browser {
fn from_str(s: &str) -> Result<Self, Self::Err> { fn from_str(s: &str) -> Result<Self, Self::Err> {
match s { match s {
"firefox" => Ok(Browser::Firefox), "firefox" => Ok(Browser::Firefox),
"chomrium" | "chrome" => Ok(Browser::Chrome), // "chomrium" | "chrome" => Ok(Browser::Chrome),
_ => Err("unsupported browser"), _ => Err("unsupported browser"),
} }
} }
@ -34,18 +35,16 @@ fn unix_home_dir() -> PathBuf {
.expect("HOME not set") .expect("HOME not set")
} }
fn chrome_user_manifest(name: &str) -> PathBuf { fn chrome_user_manifest() -> PathBuf {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
unix_home_dir() unix_home_dir()
.join("Library/Application Support/Google/Chrome/NativeMessagingHosts") .join("Library/Application Support/Google/Chrome/NativeMessagingHosts")
.join(format!("{name}.json"))
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
unix_home_dir() unix_home_dir()
.join(".config/google-chrome/NativeMessagingHosts") .join(".config/google-chrome/NativeMessagingHosts")
.join(format!("{name}.json"))
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
@ -53,22 +52,19 @@ fn chrome_user_manifest(name: &str) -> PathBuf {
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\Users\Default\AppData\Local")) .unwrap_or_else(|| PathBuf::from(r"C:\Users\Default\AppData\Local"))
.join("NativeMessagingHosts") .join("NativeMessagingHosts")
.join(format!("{name}.json"))
} }
} }
fn firefox_user_manifest(name: &str) -> PathBuf { fn firefox_user_manifest() -> PathBuf {
#[cfg(target_os = "macos")] #[cfg(target_os = "macos")]
{ {
unix_home_dir() unix_home_dir()
.join("Library/Application Support/Mozilla/NativeMessagingHosts") .join("Library/Application Support/Mozilla/NativeMessagingHosts")
.join(format!("{name}.json"))
} }
#[cfg(target_os = "linux")] #[cfg(target_os = "linux")]
{ {
unix_home_dir() unix_home_dir()
.join(".mozilla/native-messaging-hosts") .join(".mozilla/native-messaging-hosts")
.join(format!("{name}.json"))
} }
#[cfg(target_os = "windows")] #[cfg(target_os = "windows")]
{ {
@ -76,6 +72,5 @@ fn firefox_user_manifest(name: &str) -> PathBuf {
.map(PathBuf::from) .map(PathBuf::from)
.unwrap_or_else(|| PathBuf::from(r"C:\Users\Default\AppData\Roaming")) .unwrap_or_else(|| PathBuf::from(r"C:\Users\Default\AppData\Roaming"))
.join("Mozilla\\NativeMessagingHosts") .join("Mozilla\\NativeMessagingHosts")
.join(format!("{name}.json"))
} }
} }

View file

@ -4,7 +4,7 @@ use main_error::MainResult;
use owo_colors::OwoColorize; use owo_colors::OwoColorize;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::env::{args, current_exe}; use std::env::{args, current_exe};
use std::fs::write; use std::fs::{create_dir_all, write};
use std::io::{Error as IoError, ErrorKind, IsTerminal, Read, Write, stdin, stdout}; use std::io::{Error as IoError, ErrorKind, IsTerminal, Read, Write, stdin, stdout};
use std::str::FromStr; use std::str::FromStr;
use crate::install::Browser; use crate::install::Browser;
@ -23,10 +23,16 @@ fn main() -> MainResult {
} }
"install-manifest" => { "install-manifest" => {
let browser = Browser::from_str(&args.next().unwrap_or_default())?; let browser = Browser::from_str(&args.next().unwrap_or_default())?;
let manifest_dir = browser.manifest_path();
create_dir_all(&manifest_dir)?;
write( write(
browser.manifest_path("originfox"), manifest_dir.join("originfox.json"),
generate_manifest()?, generate_manifest()?,
)?; )?;
println!("Manifest installed");
println!();
println!("Note that the installed manifest includes the absolute path to this binary.");
println!("If you move this binary you'll need to re-install the manifest.");
return Ok(()); return Ok(());
} }
_ => {} _ => {}