mirror of
https://github.com/bitwarden/browser
synced 2025-12-24 04:04:24 +00:00
[PM-14270] Use rust to access windows registry (#11413)
This commit is contained in:
32
apps/desktop/desktop_native/Cargo.lock
generated
32
apps/desktop/desktop_native/Cargo.lock
generated
@@ -546,6 +546,7 @@ dependencies = [
|
||||
"napi-derive",
|
||||
"tokio",
|
||||
"tokio-util",
|
||||
"windows-registry",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
@@ -2226,7 +2227,7 @@ checksum = "d2ed2439a290666cd67ecce2b0ffaad89c2a56b976b736e6ece670297897832d"
|
||||
dependencies = [
|
||||
"windows-implement",
|
||||
"windows-interface",
|
||||
"windows-result",
|
||||
"windows-result 0.1.2",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
@@ -2252,6 +2253,17 @@ dependencies = [
|
||||
"syn",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-registry"
|
||||
version = "0.3.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "bafa604f2104cf5ae2cc2db1dee84b7e6a5d11b05f737b60def0ffdc398cbc0a"
|
||||
dependencies = [
|
||||
"windows-result 0.2.0",
|
||||
"windows-strings",
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.1.2"
|
||||
@@ -2261,6 +2273,24 @@ dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-result"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "1d1043d8214f791817bab27572aaa8af63732e11bf84aa21a45a78d6c317ae0e"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-strings"
|
||||
version = "0.2.0"
|
||||
source = "registry+https://github.com/rust-lang/crates.io-index"
|
||||
checksum = "978d65aedf914c664c510d9de43c8fd85ca745eaff1ed53edf409b479e441663"
|
||||
dependencies = [
|
||||
"windows-targets 0.52.6",
|
||||
]
|
||||
|
||||
[[package]]
|
||||
name = "windows-sys"
|
||||
version = "0.48.0"
|
||||
|
||||
@@ -21,5 +21,8 @@ napi-derive = "=2.16.12"
|
||||
tokio = { version = "1.38.0" }
|
||||
tokio-util = "0.7.11"
|
||||
|
||||
[target.'cfg(windows)'.dependencies]
|
||||
windows-registry = "=0.3.0"
|
||||
|
||||
[build-dependencies]
|
||||
napi-build = "=2.1.3"
|
||||
|
||||
4
apps/desktop/desktop_native/napi/index.d.ts
vendored
4
apps/desktop/desktop_native/napi/index.d.ts
vendored
@@ -51,6 +51,10 @@ export declare namespace powermonitors {
|
||||
export function onLock(callback: (err: Error | null, ) => any): Promise<void>
|
||||
export function isLockMonitorAvailable(): Promise<boolean>
|
||||
}
|
||||
export declare namespace windows_registry {
|
||||
export function createKey(key: string, subkey: string, value: string): Promise<void>
|
||||
export function deleteKey(key: string, subkey: string): Promise<void>
|
||||
}
|
||||
export declare namespace ipc {
|
||||
export interface IpcMessage {
|
||||
clientId: number
|
||||
|
||||
@@ -1,5 +1,8 @@
|
||||
#[macro_use]
|
||||
extern crate napi_derive;
|
||||
|
||||
mod registry;
|
||||
|
||||
#[napi]
|
||||
pub mod passwords {
|
||||
/// Fetch the stored password from the keychain.
|
||||
@@ -190,6 +193,21 @@ pub mod powermonitors {
|
||||
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub mod windows_registry {
|
||||
#[napi]
|
||||
pub async fn create_key(key: String, subkey: String, value: String) -> napi::Result<()> {
|
||||
crate::registry::create_key(&key, &subkey, &value)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub async fn delete_key(key: String, subkey: String) -> napi::Result<()> {
|
||||
crate::registry::delete_key(&key, &subkey)
|
||||
.map_err(|e| napi::Error::from_reason(e.to_string()))
|
||||
}
|
||||
}
|
||||
|
||||
#[napi]
|
||||
pub mod ipc {
|
||||
use desktop_core::ipc::server::{Message, MessageType};
|
||||
|
||||
9
apps/desktop/desktop_native/napi/src/registry/dummy.rs
Normal file
9
apps/desktop/desktop_native/napi/src/registry/dummy.rs
Normal file
@@ -0,0 +1,9 @@
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
pub fn create_key(_key: &str, _subkey: &str, _value: &str) -> Result<()> {
|
||||
bail!("Not implemented")
|
||||
}
|
||||
|
||||
pub fn delete_key(_key: &str, _subkey: &str) -> Result<()> {
|
||||
bail!("Not implemented")
|
||||
}
|
||||
4
apps/desktop/desktop_native/napi/src/registry/mod.rs
Normal file
4
apps/desktop/desktop_native/napi/src/registry/mod.rs
Normal file
@@ -0,0 +1,4 @@
|
||||
#[cfg_attr(target_os = "windows", path = "windows.rs")]
|
||||
#[cfg_attr(not(target_os = "windows"), path = "dummy.rs")]
|
||||
mod internal;
|
||||
pub use internal::*;
|
||||
29
apps/desktop/desktop_native/napi/src/registry/windows.rs
Normal file
29
apps/desktop/desktop_native/napi/src/registry/windows.rs
Normal file
@@ -0,0 +1,29 @@
|
||||
use anyhow::{bail, Result};
|
||||
|
||||
fn convert_key(key: &str) -> Result<&'static windows_registry::Key> {
|
||||
Ok(match key.to_uppercase().as_str() {
|
||||
"HKEY_CURRENT_USER" | "HKCU" => windows_registry::CURRENT_USER,
|
||||
"HKEY_LOCAL_MACHINE" | "HKLM" => windows_registry::LOCAL_MACHINE,
|
||||
"HKEY_CLASSES_ROOT" | "HKCR" => windows_registry::CLASSES_ROOT,
|
||||
_ => bail!("Invalid key"),
|
||||
})
|
||||
}
|
||||
|
||||
pub fn create_key(key: &str, subkey: &str, value: &str) -> Result<()> {
|
||||
let key = convert_key(key)?;
|
||||
|
||||
let subkey = key.create(subkey)?;
|
||||
|
||||
const DEFAULT: &str = "";
|
||||
subkey.set_string(DEFAULT, value)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
|
||||
pub fn delete_key(key: &str, subkey: &str) -> Result<()> {
|
||||
let key = convert_key(key)?;
|
||||
|
||||
key.remove_tree(subkey)?;
|
||||
|
||||
Ok(())
|
||||
}
|
||||
Reference in New Issue
Block a user