Skip to content

Commit 7015663

Browse files
adudek-bwdjsmith85coroiu
authored
[PM-25521] Move importer metadata to native code (#16695)
* Add importer metadata to native code * Impl napi code in ts * Impl napi code in ts * Fix clippy * Fix clippy * remove ts util tests * Check for installed browsers * PR fixes * test fix * fix clippy * fix tests * Bug fix * clippy fix * Correct tests * fix clippy * fix clippy * Correct tests * Correct tests * [PM-25521] Wire up loading metadata on desktop (#16813) * Initial commit * Fix issues regarding now unused feature flag * Fixed ts-strict issues --------- Co-authored-by: Daniel James Smith <[email protected]> Co-authored-by: adudek-bw <[email protected]> * Remove logic to skip Brave as that now happens via the native code * Define default capabilities which can be overwritten by specifc client/platform * Fix DI issues * Do not overwrite existing importers, just add new ones or update existing ones * feat: [PM-25521] return metadata directly (not as JSON) (#16882) * feat: return metadata directly (not as JSON) * Fix broken builds Move getMetaData into chromium_importer Remove chromium_importer_metadata and any related service Parse object from native instead of json * Run cargo fmt * Fix cargo dependency sort order * Use exposed type from NAPI instead of redefining it. * Run cargo fmt --------- Co-authored-by: Daniel James Smith <[email protected]> * Only enable chromium loader for installed and supported browsers --------- Co-authored-by: Daniel James Smith <[email protected]> Co-authored-by: Daniel James Smith <[email protected]> Co-authored-by: Andreas Coroiu <[email protected]>
1 parent 9ba1de7 commit 7015663

32 files changed

+641
-337
lines changed

apps/browser/src/background/main.background.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -221,8 +221,10 @@ import {
221221
UsernameGenerationServiceAbstraction,
222222
} from "@bitwarden/generator-legacy";
223223
import {
224+
DefaultImportMetadataService,
224225
ImportApiService,
225226
ImportApiServiceAbstraction,
227+
ImportMetadataServiceAbstraction,
226228
ImportService,
227229
ImportServiceAbstraction,
228230
} from "@bitwarden/importer-core";
@@ -368,6 +370,7 @@ export default class MainBackground {
368370
authService: AuthServiceAbstraction;
369371
loginEmailService: LoginEmailServiceAbstraction;
370372
importApiService: ImportApiServiceAbstraction;
373+
importMetadataService: ImportMetadataServiceAbstraction;
371374
importService: ImportServiceAbstraction;
372375
exportApiService: VaultExportApiService;
373376
exportService: VaultExportServiceAbstraction;
@@ -1088,6 +1091,18 @@ export default class MainBackground {
10881091

10891092
this.importApiService = new ImportApiService(this.apiService);
10901093

1094+
this.importMetadataService = new DefaultImportMetadataService(
1095+
createSystemServiceProvider(
1096+
new KeyServiceLegacyEncryptorProvider(this.encryptService, this.keyService),
1097+
this.stateProvider,
1098+
this.policyService,
1099+
buildExtensionRegistry(),
1100+
this.logService,
1101+
this.platformUtilsService,
1102+
this.configService,
1103+
),
1104+
);
1105+
10911106
this.importService = new ImportService(
10921107
this.cipherService,
10931108
this.folderService,
@@ -1099,15 +1114,6 @@ export default class MainBackground {
10991114
this.pinService,
11001115
this.accountService,
11011116
this.restrictedItemTypesService,
1102-
createSystemServiceProvider(
1103-
new KeyServiceLegacyEncryptorProvider(this.encryptService, this.keyService),
1104-
this.stateProvider,
1105-
this.policyService,
1106-
buildExtensionRegistry(),
1107-
this.logService,
1108-
this.platformUtilsService,
1109-
this.configService,
1110-
),
11111117
);
11121118

11131119
this.individualVaultExportService = new IndividualVaultExportService(

apps/browser/src/tools/popup/settings/import/import-browser-v2.component.ts

Lines changed: 18 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,16 @@ import { Router } from "@angular/router";
44

55
import { JslibModule } from "@bitwarden/angular/jslib.module";
66
import { AsyncActionsModule, ButtonModule, DialogModule } from "@bitwarden/components";
7-
import { ImportComponent } from "@bitwarden/importer-ui";
7+
import {
8+
DefaultImportMetadataService,
9+
ImportMetadataServiceAbstraction,
10+
} from "@bitwarden/importer-core";
11+
import {
12+
ImportComponent,
13+
ImporterProviders,
14+
SYSTEM_SERVICE_PROVIDER,
15+
} from "@bitwarden/importer-ui";
16+
import { safeProvider } from "@bitwarden/ui-common";
817

918
import { PopOutComponent } from "../../../../platform/popup/components/pop-out.component";
1019
import { PopupFooterComponent } from "../../../../platform/popup/layout/popup-footer.component";
@@ -25,6 +34,14 @@ import { PopupPageComponent } from "../../../../platform/popup/layout/popup-page
2534
PopupHeaderComponent,
2635
PopOutComponent,
2736
],
37+
providers: [
38+
...ImporterProviders,
39+
safeProvider({
40+
provide: ImportMetadataServiceAbstraction,
41+
useClass: DefaultImportMetadataService,
42+
deps: [SYSTEM_SERVICE_PROVIDER],
43+
}),
44+
],
2845
})
2946
export class ImportBrowserV2Component {
3047
protected disabled = false;

apps/cli/src/service-container/service-container.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -151,8 +151,10 @@ import {
151151
PasswordGenerationServiceAbstraction,
152152
} from "@bitwarden/generator-legacy";
153153
import {
154+
DefaultImportMetadataService,
154155
ImportApiService,
155156
ImportApiServiceAbstraction,
157+
ImportMetadataServiceAbstraction,
156158
ImportService,
157159
ImportServiceAbstraction,
158160
} from "@bitwarden/importer-core";
@@ -252,6 +254,7 @@ export class ServiceContainer {
252254
auditService: AuditService;
253255
importService: ImportServiceAbstraction;
254256
importApiService: ImportApiServiceAbstraction;
257+
importMetadataService: ImportMetadataServiceAbstraction;
255258
exportService: VaultExportServiceAbstraction;
256259
vaultExportApiService: VaultExportApiService;
257260
individualExportService: IndividualVaultExportServiceAbstraction;
@@ -845,6 +848,18 @@ export class ServiceContainer {
845848

846849
this.importApiService = new ImportApiService(this.apiService);
847850

851+
this.importMetadataService = new DefaultImportMetadataService(
852+
createSystemServiceProvider(
853+
new KeyServiceLegacyEncryptorProvider(this.encryptService, this.keyService),
854+
this.stateProvider,
855+
this.policyService,
856+
buildExtensionRegistry(),
857+
this.logService,
858+
this.platformUtilsService,
859+
this.configService,
860+
),
861+
);
862+
848863
this.importService = new ImportService(
849864
this.cipherService,
850865
this.folderService,
@@ -856,15 +871,6 @@ export class ServiceContainer {
856871
this.pinService,
857872
this.accountService,
858873
this.restrictedItemTypesService,
859-
createSystemServiceProvider(
860-
new KeyServiceLegacyEncryptorProvider(this.encryptService, this.keyService),
861-
this.stateProvider,
862-
this.policyService,
863-
buildExtensionRegistry(),
864-
this.logService,
865-
this.platformUtilsService,
866-
this.configService,
867-
),
868874
);
869875

870876
this.individualExportService = new IndividualVaultExportService(

apps/desktop/desktop_native/Cargo.lock

Lines changed: 2 additions & 0 deletions
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

apps/desktop/desktop_native/bitwarden_chromium_importer/Cargo.toml

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,8 @@ base64 = { workspace = true }
1414
cbc = { workspace = true, features = ["alloc"] }
1515
hex = { workspace = true }
1616
homedir = { workspace = true }
17+
napi = { workspace = true }
18+
napi-derive = { workspace = true }
1719
pbkdf2 = "=0.12.2"
1820
rand = { workspace = true }
1921
rusqlite = { version = "=0.37.0", features = ["bundled"] }

apps/desktop/desktop_native/bitwarden_chromium_importer/src/chromium.rs

Lines changed: 25 additions & 17 deletions
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ use rusqlite::{params, Connection};
1111
#[cfg_attr(target_os = "linux", path = "linux.rs")]
1212
#[cfg_attr(target_os = "windows", path = "windows.rs")]
1313
#[cfg_attr(target_os = "macos", path = "macos.rs")]
14-
mod platform;
14+
pub mod platform;
1515

1616
//
1717
// Public API
@@ -50,18 +50,26 @@ pub enum LoginImportResult {
5050
Failure(LoginImportFailure),
5151
}
5252

53-
// TODO: Make thus async
54-
pub fn get_installed_browsers() -> Result<Vec<String>> {
55-
let mut browsers = Vec::with_capacity(SUPPORTED_BROWSER_MAP.len());
53+
pub trait InstalledBrowserRetriever {
54+
fn get_installed_browsers() -> Result<Vec<String>>;
55+
}
56+
57+
pub struct DefaultInstalledBrowserRetriever {}
58+
59+
impl InstalledBrowserRetriever for DefaultInstalledBrowserRetriever {
60+
// TODO: Make thus async
61+
fn get_installed_browsers() -> Result<Vec<String>> {
62+
let mut browsers = Vec::with_capacity(SUPPORTED_BROWSER_MAP.len());
5663

57-
for (browser, config) in SUPPORTED_BROWSER_MAP.iter() {
58-
let data_dir = get_browser_data_dir(config)?;
59-
if data_dir.exists() {
60-
browsers.push((*browser).to_string());
64+
for (browser, config) in SUPPORTED_BROWSER_MAP.iter() {
65+
let data_dir = get_browser_data_dir(config)?;
66+
if data_dir.exists() {
67+
browsers.push((*browser).to_string());
68+
}
6169
}
62-
}
6370

64-
Ok(browsers)
71+
Ok(browsers)
72+
}
6573
}
6674

6775
// TODO: Make thus async
@@ -104,13 +112,13 @@ pub async fn import_logins(
104112
// Private
105113
//
106114

107-
#[derive(Debug)]
108-
struct BrowserConfig {
109-
name: &'static str,
110-
data_dir: &'static str,
115+
#[derive(Debug, Clone, Copy)]
116+
pub struct BrowserConfig {
117+
pub name: &'static str,
118+
pub data_dir: &'static str,
111119
}
112120

113-
static SUPPORTED_BROWSER_MAP: LazyLock<
121+
pub static SUPPORTED_BROWSER_MAP: LazyLock<
114122
std::collections::HashMap<&'static str, &'static BrowserConfig>,
115123
> = LazyLock::new(|| {
116124
platform::SUPPORTED_BROWSERS
@@ -132,12 +140,12 @@ fn get_browser_data_dir(config: &BrowserConfig) -> Result<PathBuf> {
132140
//
133141

134142
#[async_trait]
135-
trait CryptoService: Send {
143+
pub trait CryptoService: Send {
136144
async fn decrypt_to_string(&mut self, encrypted: &[u8]) -> Result<String>;
137145
}
138146

139147
#[derive(serde::Deserialize, Clone)]
140-
struct LocalState {
148+
pub struct LocalState {
141149
profile: AllProfiles,
142150
#[allow(dead_code)]
143151
os_crypt: Option<OsCrypt>,
Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1 +1,8 @@
1+
#[macro_use]
2+
extern crate napi_derive;
3+
14
pub mod chromium;
5+
pub mod metadata;
6+
pub mod util;
7+
8+
pub use crate::chromium::platform::SUPPORTED_BROWSERS as PLATFORM_SUPPORTED_BROWSERS;

apps/desktop/desktop_native/bitwarden_chromium_importer/src/linux.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ use oo7::XDG_SCHEMA_ATTRIBUTE;
66

77
use crate::chromium::{BrowserConfig, CryptoService, LocalState};
88

9-
mod util;
9+
use crate::util;
1010

1111
//
1212
// Public API

apps/desktop/desktop_native/bitwarden_chromium_importer/src/macos.rs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -4,7 +4,7 @@ use security_framework::passwords::get_generic_password;
44

55
use crate::chromium::{BrowserConfig, CryptoService, LocalState};
66

7-
mod util;
7+
use crate::util;
88

99
//
1010
// Public API

0 commit comments

Comments
 (0)