rightkit-browser 0.3.15

Shared Chrome DevTools Protocol browser runtime for Right Suite: multi-page sessions, named profiles, real CDP input, observations with stale-ref checks.
Documentation
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
649
650
651
652
653
654
655
656
657
658
659
660
661
662
663
664
665
666
667
668
669
670
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
766
767
768
769
770
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
870
871
872
873
874
875
876
877
878
879
880
881
882
883
884
885
886
887
888
889
890
891
892
893
894
895
896
897
898
899
900
901
902
903
904
905
906
907
908
909
910
911
912
913
914
915
916
917
918
919
920
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
939
940
941
942
943
944
945
946
947
948
949
950
951
952
953
954
955
956
957
958
959
960
961
962
//! Consented real-session mode: detect the user's default browser, record what the user agreed
//! to in a persistable, revocable receipt, copy a Chromium-family profile's sign-ins into a
//! named RightKit profile, and scope every action of the resulting session to the consented
//! domains.
//!
//! What this module does NOT do: it never reads, decrypts or transmits cookie values. Sign-ins
//! travel as the browser's own files (cookie database, local storage, the `Local State` key
//! material) copied into a private RightKit profile on the same machine. The browser's password
//! store (`Login Data`), form data (`Web Data`) and history are never copied, and typing into a
//! password field is refused, so passwords never pass through the agent: they stay in the
//! browser's own password manager.

use crate::error::{BrowserError, Result};
use crate::policy::{ActionKind, AdmissionHook, AdmissionRequest, Decision};
use futures::FutureExt;
use serde::{Deserialize, Serialize};
use std::collections::BTreeMap;
use std::path::{Path, PathBuf};
use std::sync::atomic::{AtomicBool, Ordering};
use std::sync::Arc;
use std::time::{SystemTime, UNIX_EPOCH};

/// Schema version of [`UserConsentReceipt`].
pub const CONSENT_SCHEMA: u32 = 1;

/// Marker file written into an imported profile.
pub(crate) const IMPORT_MARKER: &str = ".rightkit-import.json";

pub(crate) fn now_ms() -> u64 {
    SystemTime::now()
        .duration_since(UNIX_EPOCH)
        .map(|d| d.as_millis() as u64)
        .unwrap_or(0)
}

// ---------------------------------------------------------------------------
// Browser kinds and default-browser detection
// ---------------------------------------------------------------------------

/// A concrete browser product.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BrowserKind {
    Chrome,
    Edge,
    Chromium,
    Brave,
    Safari,
    Firefox,
    Unknown,
}

impl BrowserKind {
    /// Stable lower-case name (also used in profile names).
    pub fn name(self) -> &'static str {
        match self {
            BrowserKind::Chrome => "chrome",
            BrowserKind::Edge => "edge",
            BrowserKind::Chromium => "chromium",
            BrowserKind::Brave => "brave",
            BrowserKind::Safari => "safari",
            BrowserKind::Firefox => "firefox",
            BrowserKind::Unknown => "unknown",
        }
    }

    /// True for browsers that run on Chromium and keep a Chromium user-data directory.
    pub fn is_chromium_family(self) -> bool {
        matches!(
            self,
            BrowserKind::Chrome | BrowserKind::Edge | BrowserKind::Chromium | BrowserKind::Brave
        )
    }
}

/// Which browser an import reads from.
#[derive(Clone, Copy, Debug, Eq, PartialEq, Hash, Serialize, Deserialize)]
#[serde(rename_all = "snake_case")]
pub enum BrowserSource {
    DefaultBrowser,
    Chrome,
    Edge,
    Safari,
    Firefox,
}

impl BrowserSource {
    /// The concrete browser (`DefaultBrowser` is detected now).
    pub fn resolve(self) -> BrowserKind {
        match self {
            BrowserSource::DefaultBrowser => default_browser(),
            BrowserSource::Chrome => BrowserKind::Chrome,
            BrowserSource::Edge => BrowserKind::Edge,
            BrowserSource::Safari => BrowserKind::Safari,
            BrowserSource::Firefox => BrowserKind::Firefox,
        }
    }
}

/// The user's default web browser on Windows, macOS and Linux, or [`BrowserKind::Unknown`]
/// when it cannot be determined.
pub fn default_browser() -> BrowserKind {
    detect_default_browser().unwrap_or(BrowserKind::Unknown)
}

#[cfg(windows)]
fn detect_default_browser() -> Option<BrowserKind> {
    use std::os::windows::process::CommandExt;
    const CREATE_NO_WINDOW: u32 = 0x0800_0000;
    let output = std::process::Command::new("reg")
        .args([
            "query",
            "HKCU\\Software\\Microsoft\\Windows\\Shell\\Associations\\UrlAssociations\\https\\UserChoice",
            "/v",
            "ProgId",
        ])
        .creation_flags(CREATE_NO_WINDOW)
        .output()
        .ok()?;
    parse_windows_prog_id(&String::from_utf8_lossy(&output.stdout))
}

#[cfg(target_os = "macos")]
fn detect_default_browser() -> Option<BrowserKind> {
    let output = std::process::Command::new("defaults")
        .args([
            "read",
            "com.apple.LaunchServices/com.apple.launchservices.secure",
            "LSHandlers",
        ])
        .output()
        .ok()?;
    // No https handler recorded means the system default, Safari.
    Some(
        parse_macos_handlers(&String::from_utf8_lossy(&output.stdout))
            .unwrap_or(BrowserKind::Safari),
    )
}

#[cfg(not(any(windows, target_os = "macos")))]
fn detect_default_browser() -> Option<BrowserKind> {
    let output = std::process::Command::new("xdg-settings")
        .args(["get", "default-web-browser"])
        .output()
        .ok()?;
    parse_desktop_entry(&String::from_utf8_lossy(&output.stdout))
}

/// `reg query ... /v ProgId` output, for example `ProgId    REG_SZ    ChromeHTML`.
#[allow(dead_code)]
pub(crate) fn parse_windows_prog_id(output: &str) -> Option<BrowserKind> {
    let line = output.lines().find(|l| l.contains("ProgId"))?;
    let prog_id = line.split_whitespace().last()?.to_ascii_lowercase();
    let kind = if prog_id.contains("chromehtml") {
        BrowserKind::Chrome
    } else if prog_id.contains("msedge") {
        BrowserKind::Edge
    } else if prog_id.contains("firefox") {
        BrowserKind::Firefox
    } else if prog_id.contains("bravehtml") {
        BrowserKind::Brave
    } else if prog_id.contains("safari") {
        BrowserKind::Safari
    } else {
        BrowserKind::Unknown
    };
    Some(kind)
}

/// `defaults read ... LSHandlers` output: find the block that handles the `https` scheme.
#[allow(dead_code)]
pub(crate) fn parse_macos_handlers(output: &str) -> Option<BrowserKind> {
    for block in output.split('}') {
        let lower = block.to_ascii_lowercase();
        let handles_https = lower
            .lines()
            .any(|l| l.contains("lshandlerurlscheme") && l.contains("https"));
        if !handles_https {
            continue;
        }
        for line in lower.lines() {
            if line.contains("lshandlerroleall") {
                let bundle = line
                    .split('=')
                    .nth(1)?
                    .trim()
                    .trim_end_matches(';')
                    .trim_matches('"');
                return Some(match bundle {
                    "com.google.chrome" => BrowserKind::Chrome,
                    "com.microsoft.edgemac" => BrowserKind::Edge,
                    "org.chromium.chromium" => BrowserKind::Chromium,
                    "com.brave.browser" => BrowserKind::Brave,
                    "org.mozilla.firefox" => BrowserKind::Firefox,
                    "com.apple.safari" => BrowserKind::Safari,
                    _ => BrowserKind::Unknown,
                });
            }
        }
    }
    None
}

/// `xdg-settings get default-web-browser` output, for example `google-chrome.desktop`.
#[allow(dead_code)]
pub(crate) fn parse_desktop_entry(output: &str) -> Option<BrowserKind> {
    let entry = output.trim().to_ascii_lowercase();
    if entry.is_empty() {
        return None;
    }
    let kind = if entry.contains("google-chrome") {
        BrowserKind::Chrome
    } else if entry.contains("microsoft-edge") {
        BrowserKind::Edge
    } else if entry.contains("brave") {
        BrowserKind::Brave
    } else if entry.contains("chromium") {
        BrowserKind::Chromium
    } else if entry.contains("firefox") {
        BrowserKind::Firefox
    } else {
        BrowserKind::Unknown
    };
    Some(kind)
}

// ---------------------------------------------------------------------------
// Consent
// ---------------------------------------------------------------------------

/// Lower-case a domain and reject anything that is not a bare host name. A leading `*.` or `.`
/// is accepted and dropped: a domain always covers its subdomains.
pub(crate) fn normalize_domain(raw: &str) -> Result<String> {
    let d = raw
        .trim()
        .trim_start_matches("*.")
        .trim_start_matches('.')
        .trim_end_matches('.')
        .to_ascii_lowercase();
    let ok = !d.is_empty()
        && d.len() <= 253
        && d.split('.').all(|label| {
            !label.is_empty()
                && label.len() <= 63
                && !label.starts_with('-')
                && !label.ends_with('-')
                && label.chars().all(|c| c.is_ascii_alphanumeric() || c == '-')
        });
    if ok {
        Ok(d)
    } else {
        Err(BrowserError::Invalid(format!(
            "'{raw}' is not a bare domain name"
        )))
    }
}

fn normalize_domains(domains: Option<Vec<String>>) -> Result<Option<Vec<String>>> {
    match domains {
        None => Ok(None),
        Some(list) => {
            if list.is_empty() {
                return Err(BrowserError::Invalid(
                    "an empty domain list grants nothing; use None for all domains".into(),
                ));
            }
            let mut out = Vec::with_capacity(list.len());
            for d in &list {
                let d = normalize_domain(d)?;
                if !out.contains(&d) {
                    out.push(d);
                }
            }
            Ok(Some(out))
        }
    }
}

/// A plain profile directory name such as `Default` or `Profile 1`.
pub(crate) fn validate_source_profile(name: &str) -> Result<()> {
    let ok = !name.is_empty()
        && name.len() <= 64
        && !name.starts_with('.')
        && name
            .chars()
            .all(|c| c.is_ascii_alphanumeric() || matches!(c, ' ' | '-' | '_' | '.'));
    if ok {
        Ok(())
    } else {
        Err(BrowserError::Invalid(format!(
            "invalid source profile name '{name}'"
        )))
    }
}

fn host_matches(domain: &str, host: &str) -> bool {
    let host = host.trim_end_matches('.').to_ascii_lowercase();
    host == domain || host.ends_with(&format!(".{domain}"))
}

fn domain_allowed(domains: &Option<Vec<String>>, host: &str) -> bool {
    match domains {
        None => true,
        Some(list) => list.iter().any(|d| host_matches(d, host)),
    }
}

/// What the user agreed to: which browser, which profile, which domains. The app persists it
/// (it is `Serialize`/`Deserialize`), shows it, and revokes it. A revoked or expired receipt
/// refuses every later import, attach and action.
#[derive(Clone, Debug, Eq, PartialEq, Serialize, Deserialize)]
pub struct UserConsentReceipt {
    pub schema: u32,
    /// Caller-chosen unique id (for example a UUID).
    pub id: String,
    pub browser: BrowserKind,
    /// Source profile directory name, for example `Default` or `Profile 1`.
    pub profile: String,
    /// Domains (each covers its subdomains) the agent may act on; `None` means every domain.
    pub domains: Option<Vec<String>>,
    pub granted_at_ms: u64,
    pub expires_at_ms: Option<u64>,
    pub revoked_at_ms: Option<u64>,
}

impl UserConsentReceipt {
    pub fn new(
        id: impl Into<String>,
        browser: BrowserKind,
        profile: impl Into<String>,
        domains: Option<Vec<String>>,
        granted_at_ms: u64,
    ) -> Result<Self> {
        let receipt = Self {
            schema: CONSENT_SCHEMA,
            id: id.into(),
            browser,
            profile: profile.into(),
            domains,
            granted_at_ms,
            expires_at_ms: None,
            revoked_at_ms: None,
        };
        receipt.validated()
    }

    /// Receipt that lapses at `expires_at_ms` (Unix milliseconds).
    pub fn with_expiry(mut self, expires_at_ms: u64) -> Self {
        self.expires_at_ms = Some(expires_at_ms);
        self
    }

    fn validated(mut self) -> Result<Self> {
        if self.schema != CONSENT_SCHEMA {
            return Err(BrowserError::Consent(format!(
                "unsupported receipt schema {}",
                self.schema
            )));
        }
        if self.id.is_empty() || self.id.len() > 128 {
            return Err(BrowserError::Invalid(
                "consent id must be 1-128 characters".into(),
            ));
        }
        if self.browser == BrowserKind::Unknown {
            return Err(BrowserError::Invalid(
                "consent names an unknown browser".into(),
            ));
        }
        validate_source_profile(&self.profile)?;
        self.domains = normalize_domains(self.domains.take())?;
        Ok(self)
    }

    /// Mark the receipt revoked at `now_ms`. The app persists the result.
    pub fn revoke(&mut self, now_ms: u64) {
        if self.revoked_at_ms.is_none() {
            self.revoked_at_ms = Some(now_ms);
        }
    }

    pub fn is_active_at(&self, now_ms: u64) -> bool {
        self.revoked_at_ms.is_none() && self.expires_at_ms.is_none_or(|e| now_ms < e)
    }

    pub fn check_active(&self) -> Result<()> {
        if self.is_active_at(now_ms()) {
            Ok(())
        } else {
            Err(BrowserError::Consent(format!(
                "consent {} is revoked or has expired",
                self.id
            )))
        }
    }

    /// Whether `host` is inside the consented domains (a domain covers its subdomains).
    pub fn permits_host(&self, host: &str) -> bool {
        domain_allowed(&self.domains, host)
    }

    /// Whether an http(s) `url` is inside the consented domains.
    pub fn permits_url(&self, url: &str) -> bool {
        match url::Url::parse(url) {
            Ok(u) if matches!(u.scheme(), "http" | "https") => {
                u.host_str().is_some_and(|h| self.permits_host(h))
            }
            _ => false,
        }
    }

    pub fn to_json(&self) -> Result<String> {
        serde_json::to_string_pretty(self).map_err(|e| BrowserError::Consent(e.to_string()))
    }

    pub fn from_json(json: &str) -> Result<Self> {
        let receipt: Self =
            serde_json::from_str(json).map_err(|e| BrowserError::Consent(e.to_string()))?;
        receipt.validated()
    }
}

/// Live revocation switch shared with a running session. Calling [`Self::revoke`] makes the
/// session's next action fail, without waiting for the app to persist the receipt.
#[derive(Clone, Debug, Default)]
pub struct ConsentRevocation(Arc<AtomicBool>);

impl ConsentRevocation {
    pub fn new() -> Self {
        Self::default()
    }
    pub fn revoke(&self) {
        self.0.store(true, Ordering::SeqCst);
    }
    pub fn is_revoked(&self) -> bool {
        self.0.load(Ordering::SeqCst)
    }
}

impl PartialEq for ConsentRevocation {
    fn eq(&self, other: &Self) -> bool {
        Arc::ptr_eq(&self.0, &other.0)
    }
}
impl Eq for ConsentRevocation {}

// ---------------------------------------------------------------------------
// Import specification
// ---------------------------------------------------------------------------

/// Consent-gated copy of a browser profile's sign-ins into a named RightKit profile (see
/// [`crate::ProfileSpec::ImportedFrom`]).
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct ImportSpec {
    pub browser: BrowserSource,
    /// Source profile directory name; `None` means the consent's profile.
    pub source_profile: Option<String>,
    /// Narrow the consent further; `None` means the consent's domains. Every entry must be
    /// inside the consent. Scopes the session's actions (the cookie database is copied whole).
    pub domains: Option<Vec<String>>,
    pub consent: UserConsentReceipt,
    pub revocation: ConsentRevocation,
    /// Copy again even when this consent already imported into the profile.
    pub reimport: bool,
}

impl ImportSpec {
    pub fn new(browser: BrowserSource, consent: UserConsentReceipt) -> Self {
        Self {
            browser,
            source_profile: None,
            domains: None,
            consent,
            revocation: ConsentRevocation::new(),
            reimport: false,
        }
    }

    pub fn source_profile(mut self, name: impl Into<String>) -> Self {
        self.source_profile = Some(name.into());
        self
    }

    pub fn domains(mut self, domains: Vec<String>) -> Self {
        self.domains = Some(domains);
        self
    }

    pub fn revocation(mut self, revocation: ConsentRevocation) -> Self {
        self.revocation = revocation;
        self
    }

    pub fn reimport(mut self) -> Self {
        self.reimport = true;
        self
    }

    pub(crate) fn effective_profile(&self) -> &str {
        self.source_profile
            .as_deref()
            .unwrap_or(self.consent.profile.as_str())
    }

    /// Domains the session may act on: the narrowed list, else the consent's.
    pub(crate) fn effective_domains(&self) -> Result<Option<Vec<String>>> {
        match normalize_domains(self.domains.clone())? {
            Some(list) => {
                for d in &list {
                    if !self.consent.permits_host(d) {
                        return Err(BrowserError::Consent(format!(
                            "domain {d} is outside the consent"
                        )));
                    }
                }
                Ok(Some(list))
            }
            None => Ok(self.consent.domains.clone()),
        }
    }

    /// Everything that must hold before any browser data is touched.
    pub(crate) fn validate(&self) -> Result<BrowserKind> {
        self.consent.check_active()?;
        if self.revocation.is_revoked() {
            return Err(BrowserError::Consent("consent was revoked".into()));
        }
        let kind = self.browser.resolve();
        if kind != self.consent.browser {
            return Err(BrowserError::Consent(format!(
                "consent was granted for {}, not {}",
                self.consent.browser.name(),
                kind.name()
            )));
        }
        validate_source_profile(self.effective_profile())?;
        if self.effective_profile() != self.consent.profile {
            return Err(BrowserError::Consent(format!(
                "consent was granted for profile '{}', not '{}'",
                self.consent.profile,
                self.effective_profile()
            )));
        }
        self.effective_domains()?;
        if !kind.is_chromium_family() {
            return Err(BrowserError::Unsupported(format!(
                "{} sessions cannot be imported into a CDP profile; drive {} through rightkit-automation",
                kind.name(),
                kind.name()
            )));
        }
        Ok(kind)
    }
}

// ---------------------------------------------------------------------------
// Scoped admission
// ---------------------------------------------------------------------------

fn scope_verdict(
    consent: &UserConsentReceipt,
    domains: &Option<Vec<String>>,
    revocation: &ConsentRevocation,
    req: &AdmissionRequest,
) -> Option<String> {
    if revocation.is_revoked() || !consent.is_active_at(now_ms()) {
        return Some("user consent was revoked or has expired".into());
    }
    if matches!(req.action, ActionKind::Fill | ActionKind::TypeText) {
        let target = req.target.as_deref().unwrap_or("").to_ascii_lowercase();
        if target.contains("password") || target.contains("passwd") {
            return Some(
                "passwords come from the browser's own password manager, never from the agent"
                    .into(),
            );
        }
    }
    match req.url.as_deref() {
        None => {
            if matches!(req.action, ActionKind::Navigate) {
                Some("navigation without a URL".into())
            } else {
                None
            }
        }
        Some("about:blank") => None,
        Some(raw) => match url::Url::parse(raw) {
            Ok(u) if matches!(u.scheme(), "http" | "https") => match u.host_str() {
                Some(host) if domain_allowed(domains, host) => None,
                _ => Some(format!(
                    "{raw} is outside the domains the user consented to"
                )),
            },
            _ => Some(format!("{raw} is not an http(s) page inside the consent")),
        },
    }
}

/// Admission hook that refuses every action outside the consent, then defers to `inner`.
pub(crate) fn scoped_admission(
    spec: &ImportSpec,
    inner: Option<AdmissionHook>,
) -> Result<AdmissionHook> {
    let consent = spec.consent.clone();
    let domains = spec.effective_domains()?;
    let revocation = spec.revocation.clone();
    Ok(Arc::new(move |req: AdmissionRequest| {
        let verdict = scope_verdict(&consent, &domains, &revocation, &req);
        let inner = inner.clone();
        async move {
            if let Some(reason) = verdict {
                return Decision::Deny(reason);
            }
            match inner {
                Some(hook) => hook(req).await,
                None => Decision::Allow,
            }
        }
        .boxed()
    }))
}

// ---------------------------------------------------------------------------
// Chromium-family profile discovery and import
// ---------------------------------------------------------------------------

/// The browser's own user-data directory (read only by this module), when it exists.
pub fn user_data_dir(kind: BrowserKind) -> Option<PathBuf> {
    let rel: &[&str] = match kind {
        BrowserKind::Chrome => &["Google", "Chrome"],
        BrowserKind::Edge => &["Microsoft", "Edge"],
        BrowserKind::Chromium => &["Chromium"],
        BrowserKind::Brave => &["BraveSoftware", "Brave-Browser"],
        _ => return None,
    };
    let base: PathBuf = if cfg!(windows) {
        let mut p = PathBuf::from(std::env::var_os("LOCALAPPDATA")?);
        for part in rel {
            p.push(part);
        }
        p.push("User Data");
        return p.is_dir().then_some(p);
    } else if cfg!(target_os = "macos") {
        let mut p = PathBuf::from(std::env::var_os("HOME")?);
        p.push("Library");
        p.push("Application Support");
        p
    } else {
        let mut p = PathBuf::from(std::env::var_os("HOME")?);
        p.push(".config");
        p
    };
    let mut path = base;
    if cfg!(target_os = "macos") {
        for part in rel {
            path.push(part);
        }
    } else {
        // Linux directory names are lower-case and hyphenated.
        let name = match kind {
            BrowserKind::Chrome => "google-chrome",
            BrowserKind::Edge => "microsoft-edge",
            BrowserKind::Chromium => "chromium",
            _ => "BraveSoftware/Brave-Browser",
        };
        path.push(name);
    }
    path.is_dir().then_some(path)
}

/// The installed browser executable for a Chromium-family kind.
pub fn browser_executable(kind: BrowserKind) -> Option<PathBuf> {
    let candidates: &[&str] = if cfg!(windows) {
        match kind {
            BrowserKind::Chrome => &[
                "C:\\Program Files\\Google\\Chrome\\Application\\chrome.exe",
                "C:\\Program Files (x86)\\Google\\Chrome\\Application\\chrome.exe",
            ],
            BrowserKind::Edge => &[
                "C:\\Program Files (x86)\\Microsoft\\Edge\\Application\\msedge.exe",
                "C:\\Program Files\\Microsoft\\Edge\\Application\\msedge.exe",
            ],
            BrowserKind::Brave => {
                &["C:\\Program Files\\BraveSoftware\\Brave-Browser\\Application\\brave.exe"]
            }
            _ => &[],
        }
    } else if cfg!(target_os = "macos") {
        match kind {
            BrowserKind::Chrome => {
                &["/Applications/Google Chrome.app/Contents/MacOS/Google Chrome"]
            }
            BrowserKind::Edge => {
                &["/Applications/Microsoft Edge.app/Contents/MacOS/Microsoft Edge"]
            }
            BrowserKind::Chromium => &["/Applications/Chromium.app/Contents/MacOS/Chromium"],
            BrowserKind::Brave => &["/Applications/Brave Browser.app/Contents/MacOS/Brave Browser"],
            _ => &[],
        }
    } else {
        match kind {
            BrowserKind::Chrome => &["/usr/bin/google-chrome", "/usr/bin/google-chrome-stable"],
            BrowserKind::Edge => &["/usr/bin/microsoft-edge", "/usr/bin/microsoft-edge-stable"],
            BrowserKind::Chromium => &["/usr/bin/chromium", "/usr/bin/chromium-browser"],
            BrowserKind::Brave => &["/usr/bin/brave-browser"],
            _ => &[],
        }
    };
    let per_user: &[&str] = if cfg!(windows) {
        match kind {
            BrowserKind::Chrome => &[r"Google\Chrome\Application\chrome.exe"],
            BrowserKind::Edge => &[r"Microsoft\Edge\Application\msedge.exe"],
            BrowserKind::Brave => &[r"BraveSoftware\Brave-Browser\Application\brave.exe"],
            _ => &[],
        }
    } else {
        &[]
    };
    candidates
        .iter()
        .map(PathBuf::from)
        .chain(crate::config::windows_local_app_candidates(per_user))
        .find(|p| p.exists())
}

/// One profile of a Chromium-family browser.
#[derive(Clone, Debug, Eq, PartialEq)]
pub struct SourceProfile {
    /// Directory name inside the user-data directory (`Default`, `Profile 1`).
    pub dir_name: String,
    /// Name shown in the browser's profile picker.
    pub display_name: String,
}

pub(crate) fn parse_profiles(local_state: &str) -> Result<Vec<SourceProfile>> {
    let value: serde_json::Value = serde_json::from_str(local_state)
        .map_err(|e| BrowserError::Profile(format!("unreadable Local State: {e}")))?;
    let mut out = Vec::new();
    if let Some(cache) = value
        .get("profile")
        .and_then(|p| p.get("info_cache"))
        .and_then(|c| c.as_object())
    {
        for (dir, info) in cache {
            let display = info
                .get("name")
                .and_then(|n| n.as_str())
                .unwrap_or(dir.as_str())
                .to_string();
            out.push(SourceProfile {
                dir_name: dir.clone(),
                display_name: display,
            });
        }
    }
    out.sort_by(|a, b| a.dir_name.cmp(&b.dir_name));
    Ok(out)
}

/// Profiles of an installed Chromium-family browser, for the consent screen.
pub fn list_profiles(kind: BrowserKind) -> Result<Vec<SourceProfile>> {
    if !kind.is_chromium_family() {
        return Err(BrowserError::Unsupported(format!(
            "profile listing covers Chromium-family browsers, not {}",
            kind.name()
        )));
    }
    let dir = user_data_dir(kind).ok_or_else(|| {
        BrowserError::Profile(format!("{} has no user-data directory", kind.name()))
    })?;
    let text = std::fs::read_to_string(dir.join("Local State"))?;
    parse_profiles(&text)
}

/// Result of one import. Per-domain cookie counts need the cookie database opened, which this
/// module does not do, so `domain_counts` is `None`.
#[derive(Clone, Debug, Eq, PartialEq, Serialize)]
pub struct ImportReport {
    pub consent_id: String,
    pub browser: BrowserKind,
    pub source_profile: String,
    /// Paths copied, relative to the source user-data directory.
    pub files_copied: Vec<String>,
    pub bytes_copied: u64,
    pub domain_counts: Option<BTreeMap<String, u64>>,
    pub imported_at_ms: u64,
}

fn copy_tree(src: &Path, dst: &Path, bytes: &mut u64) -> Result<()> {
    std::fs::create_dir_all(dst)?;
    for entry in std::fs::read_dir(src)? {
        let entry = entry?;
        let meta = std::fs::symlink_metadata(entry.path())?;
        if meta.file_type().is_symlink() {
            continue;
        }
        let target = dst.join(entry.file_name());
        if meta.is_dir() {
            copy_tree(&entry.path(), &target, bytes)?;
        } else if entry.file_name() != "LOCK" {
            *bytes += copy_file(&entry.path(), &target)?;
        }
    }
    Ok(())
}

fn copy_file(src: &Path, dst: &Path) -> Result<u64> {
    if let Some(parent) = dst.parent() {
        std::fs::create_dir_all(parent)?;
    }
    std::fs::copy(src, dst).map_err(|e| {
        BrowserError::Profile(format!(
            "could not copy {} ({e}); close the browser or wait for it to release the file, then import again",
            src.display()
        ))
    })
}

/// Copy the sign-in files of `source_profile` from `user_data` into `dest_root` (the RightKit
/// profile directory, which Chrome later opens as its user-data directory). Copies the cookie
/// database, local storage and `Local State`; never `Login Data`, `Web Data` or history.
pub(crate) fn import_chromium_profile(
    user_data: &Path,
    source_profile: &str,
    dest_root: &Path,
    consent_id: &str,
    browser: BrowserKind,
) -> Result<ImportReport> {
    validate_source_profile(source_profile)?;
    let src = user_data.join(source_profile);
    if !src.is_dir() {
        return Err(BrowserError::Profile(format!(
            "source profile not found: {}",
            src.display()
        )));
    }
    let dst = dest_root.join("Default");
    let mut files: Vec<String> = Vec::new();
    let mut bytes: u64 = 0;

    for rel in [
        "Network/Cookies",
        "Network/Cookies-journal",
        "Cookies",
        "Cookies-journal",
    ] {
        let from = src.join(rel);
        if from.is_file() {
            bytes += copy_file(&from, &dst.join(rel))?;
            files.push(format!("{source_profile}/{rel}"));
        }
    }
    if !files.iter().any(|f| f.ends_with("Cookies")) {
        return Err(BrowserError::Profile(format!(
            "no cookie database in {}",
            src.display()
        )));
    }
    let local_storage = src.join("Local Storage");
    if local_storage.is_dir() {
        let target = dst.join("Local Storage");
        if target.exists() {
            std::fs::remove_dir_all(&target)?;
        }
        copy_tree(&local_storage, &target, &mut bytes)?;
        files.push(format!("{source_profile}/Local Storage"));
    }
    let local_state = user_data.join("Local State");
    if local_state.is_file() {
        bytes += copy_file(&local_state, &dest_root.join("Local State"))?;
        files.push("Local State".into());
    }
    Ok(ImportReport {
        consent_id: consent_id.to_string(),
        browser,
        source_profile: source_profile.to_string(),
        files_copied: files,
        bytes_copied: bytes,
        domain_counts: None,
        imported_at_ms: now_ms(),
    })
}

fn marker_consent_id(dest_root: &Path) -> Option<String> {
    let text = std::fs::read_to_string(dest_root.join(IMPORT_MARKER)).ok()?;
    let value: serde_json::Value = serde_json::from_str(&text).ok()?;
    value.get("consent_id")?.as_str().map(|s| s.to_string())
}

/// Validate the spec and import into `dest_root` when this consent has not imported there yet
/// (or `reimport` is set). Returns the report when a copy happened.
pub(crate) fn import_if_needed(
    spec: &ImportSpec,
    dest_root: &Path,
) -> Result<Option<ImportReport>> {
    let kind = spec.validate()?;
    if !spec.reimport && marker_consent_id(dest_root).as_deref() == Some(spec.consent.id.as_str()) {
        return Ok(None);
    }
    let user_data = user_data_dir(kind).ok_or_else(|| {
        BrowserError::Profile(format!(
            "{} has no user-data directory on this machine",
            kind.name()
        ))
    })?;
    let report = import_chromium_profile(
        &user_data,
        spec.effective_profile(),
        dest_root,
        &spec.consent.id,
        kind,
    )?;
    let json =
        serde_json::to_string_pretty(&report).map_err(|e| BrowserError::Profile(e.to_string()))?;
    std::fs::write(dest_root.join(IMPORT_MARKER), json)?;
    Ok(Some(report))
}

/// Delete an imported RightKit profile (revoking consent should call this). Refuses a directory
/// RightKit did not import into, and a profile a live session holds.
pub fn purge_imported_profile(root: &Path, name: &str) -> Result<()> {
    crate::config::validate_profile_name(name)?;
    crate::config::reject_real_browser_profile(root)?;
    let dir = root.join(name);
    if !dir.join(IMPORT_MARKER).is_file() {
        return Err(BrowserError::Profile(format!(
            "'{name}' is not an imported profile; nothing deleted"
        )));
    }
    {
        let lock = std::fs::OpenOptions::new()
            .create(true)
            .write(true)
            .truncate(false)
            .open(dir.join(".rightkit-browser.lock"))?;
        match lock.try_lock() {
            Ok(()) => {}
            Err(std::fs::TryLockError::WouldBlock) => {
                return Err(BrowserError::ProfileInUse(name.to_string()));
            }
            Err(std::fs::TryLockError::Error(e)) => return Err(e.into()),
        }
    }
    std::fs::remove_dir_all(&dir)?;
    Ok(())
}

/// Profile name for [`crate::BrowserSession::attach_user_browser`].
pub(crate) fn attach_profile_name(consent: &UserConsentReceipt) -> String {
    let slug: String = consent
        .profile
        .chars()
        .map(|c| {
            if c.is_ascii_alphanumeric() {
                c.to_ascii_lowercase()
            } else {
                '-'
            }
        })
        .collect();
    format!("user-{}-{}", consent.browser.name(), slug)
}