blib/lib/Firefox/Util/Profile.pm

filter: Statementstmt 37.1% 23 / 62 Branchbran 0.0% 0 / 22 Conditioncond 0.0% 0 / 3 MC/DCmcdc 0.0% 0 / 2 Subroutinesub 71.4% 5 / 7 Podpod 100% 2 / 2 totaltotal 30.6% 30 / 98 CC 16 SCAR 45.2
CC
16
Cov
26%
list_firefox_profiles
156.0
get_firefox_profile_dir
30.0
BEGIN
1.0
CRAP 92.1
↑ Summary
1 package Firefox::Util::Profile;
2  
3 1 ▶use 5.010001;
Subroutine BEGIN: called CC=1 SCAR=0.0
4 1 ▶use strict;
Subroutine BEGIN: called CC=1 SCAR=0.0
5 1 ▶use warnings;
Subroutine BEGIN: called CC=1 SCAR=0.0
6 1 ▶use Log::ger;
Subroutine BEGIN: called CC=1 SCAR=0.0
7  
8 1 ▶use Exporter 'import';
Subroutine BEGIN: called CC=1 SCAR=0.0
9  
10 1 our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY
11 1 our $DATE = '2025-04-16'; # DATE
12 1 our $DIST = 'Firefox-Util-Profile'; # DIST
13 1 our $VERSION = '0.006'; # VERSION
14  
15 1 our @EXPORT_OK = qw(list_firefox_profiles);
16  
17 1 our %SPEC;
18  
19 # TODO: allow selecting local Firefox installation
20  
21 $SPEC{list_firefox_profiles} = {
22 1 v => 1.1,
23 summary => 'List available Firefox profiles',
24 description => <<'MARKDOWN',
25  
26 This utility will read ~/.mozilla/firefox/profiles.ini (or
27 %APPDATA%\\Mozilla\\Firefox\\profiles.ini on Windows) and extracts the list of
28 profiles.
29  
30 MARKDOWN
31 args => {
32 detail => {
33 schema => 'bool',
34 cmdline_aliases => {l=>{}},
35 },
36 },
37 };
38 0 ▶sub list_firefox_profiles {
Subroutine list_firefox_profiles: not called CC=12 SCAR=50.5
Pod documented
39 0 require Config::IOD::INI::Reader;
40 0 require Sort::Sub;
41  
42 0 my %args = @_;
43  
44 0 ▶ my @ff_dirs = $^O eq 'MSWin32' ?
Branch: 1 branch
BranchTrueFalseTotal
$^O eq 'MSWin32' ? : 0 0 0
45 ("$ENV{APPDATA}/Mozilla/Firefox") :
46 ("$ENV{HOME}/.mozilla/firefox", "$ENV{HOME}/snap/firefox/common/.mozilla/firefox");
47 0 my $ff_dir;
48 0 for my $dir (@ff_dirs) {
49 0 ▶ if (-d $dir) { $ff_dir = $dir; last }
Branch: 1 branch
BranchTrueFalseTotal
if (-d $dir) 0 0 0
50 }
51 0 ▶ return [412, "Cannot find firefox directory (tried ".join(", ", @ff_dirs).")"]
Branch: 1 branch
BranchTrueFalseTotal
unless defined $ff_dir 0 0 0
52 unless defined $ff_dir;
53  
54 0 my $ini_path = "$ff_dir/profiles.ini";
55 0 ▶ unless (-f $ini_path) {
Branch: 1 branch
BranchTrueFalseTotal
unless (-f $ini_path) 0 0 0
56 0 return [412, "Cannot find $ini_path"];
57 }
58  
59 0 my @rows;
60 0 my $hoh = Config::IOD::INI::Reader->new->read_file($ini_path);
61 0 my $naturally = Sort::Sub::get_sorter('naturally');
62 SECTION:
63 0 for my $section (sort $naturally keys %$hoh) {
64 0 my $href = $hoh->{$section};
65 0 ▶ if ($section =~ /\AProfile/) {
Branch: 1 branch
BranchTrueFalseTotal
if ($section =~ /\AProfile/) 0 0 0
66 0 my $path;
67 0 ▶ if (defined($path = $href->{Path})) {
Branch: 1 branch
BranchTrueFalseTotal
if (defined($path = $href->{'Path'})) { } 0 0 0
68 0 ▶ $path = "$ff_dir/$path" if $href->{IsRelative};
Branch: 1 branch
BranchTrueFalseTotal
if $href->{"IsRelative"} 0 0 0
69 push @rows, {
70 0 ▶ name => $href->{Name} // $section,
Condition0%0/3
$href->{"Name"} // $section
l!l&&r!l&&!r
000
MC/DC0%0/2
$href->{"Name"} // $section
$href->{"Name"} $section
Truth table0%0/3
A // B
A= $href->{"Name"}B= $section
ABresult
1X1
011
000
71 path => $path,
72 ini_section => $section,
73 };
74 } else {
75 0 log_warn "$ini_path: No Path parameter for section $section, section ignored";
76 0 next SECTION;
77 }
78 }
79 # XXX add info: which sections are default in which installation
80 # ([Install...] sections)
81 }
82  
83 0 ▶ unless ($args{detail}) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($args{"detail"}) 0 0 0
84 0 @rows = map { $_->{name} } @rows;
85 }
86  
87 0 [200, "OK", \@rows];
88 }
89  
90 $SPEC{get_firefox_profile_dir} = {
91 1 v => 1.1,
92 summary => 'Given a Firefox profile name, return its directory',
93 description => <<'MARKDOWN',
94  
95 Return undef if Firefox profile is unknown.
96  
97 MARKDOWN
98 args_as => 'array',
99 args => {
100 profile => {
101 schema => 'firefox::profile_name*',
102 cmdline_aliases => {l=>{}},
103 req => 1,
104 pos => 0,
105 },
106 },
107 result_naked => 1,
108 };
109 0 ▶sub get_firefox_profile_dir {
Subroutine get_firefox_profile_dir: not called CC=5 SCAR=34.0
Pod documented
110 0 my $profile = shift;
111  
112 0 ▶ return unless defined $profile;
Branch: 1 branch
BranchTrueFalseTotal
unless defined $profile 0 0 0
113 0 my $res = list_firefox_profiles(detail=>1);
114 0 ▶ unless ($res->[0] == 200) {
Branch: 1 branch
BranchTrueFalseTotal
unless ($res->[0] == 200) 0 0 0
115 0 log_warn "Can't list Firefox profile: $res->[0] - $res->[1]";
116 0 return;
117 };
118  
119 0 for (@{ $res->[2] }) {
120 0 ▶ return $_->{path} if $_->{name} eq $profile;
Branch: 1 branch
BranchTrueFalseTotal
if $_->{"name"} eq $profile 0 0 0
121 }
122 0 return;
123 }
124  
125 1 1;
126 # ABSTRACT: Given a Firefox profile name, return its directory
127  
128 __END__
↑ Summary