Firefox-Marionette

 view release on metacpan or  search on metacpan

lib/Firefox/Marionette/Profile.pm  view on Meta::CPAN

package Firefox::Marionette::Profile;

use strict;
use warnings;
use English qw( -no_match_vars );
use File::Spec();
use FileHandle();
use Fcntl();
use Config::INI::Reader();

BEGIN {
    if ( $OSNAME eq 'MSWin32' ) {
        require Win32;
    }
}
our $VERSION = '1.70';

sub ANY_PORT            { return 0 }
sub _GETPWUID_DIR_INDEX { return 7 }

sub profile_ini_directory {
    my $profile_ini_directory;
    if ( $OSNAME eq 'darwin' ) {
        my $home_directory =
          ( getpwuid $EFFECTIVE_USER_ID )[ _GETPWUID_DIR_INDEX() ];
        defined $home_directory
          or Firefox::Marionette::Exception->throw(
            "Failed to execute getpwuid for $OSNAME:$EXTENDED_OS_ERROR");
        $profile_ini_directory = File::Spec->catdir( $home_directory, 'Library',
            'Application Support', 'Firefox' );
    }
    elsif ( $OSNAME eq 'MSWin32' ) {
        $profile_ini_directory =
          File::Spec->catdir( Win32::GetFolderPath( Win32::CSIDL_APPDATA() ),
            'Mozilla', 'Firefox' );
    }
    elsif ( $OSNAME eq 'cygwin' ) {
        $profile_ini_directory =
          File::Spec->catdir( $ENV{APPDATA}, 'Mozilla', 'Firefox' );
    }
    else {
        my $home_directory =
          ( getpwuid $EFFECTIVE_USER_ID )[ _GETPWUID_DIR_INDEX() ];
        defined $home_directory
          or Firefox::Marionette::Exception->throw(
            "Failed to execute getpwuid for $OSNAME:$EXTENDED_OS_ERROR");
        $profile_ini_directory =
          File::Spec->catdir( $home_directory, '.mozilla', 'firefox' );
    }
    return $profile_ini_directory;
}

sub _read_ini_file {
    my ( $class, $profile_ini_directory, $handle ) = @_;
    if ( defined $handle ) {
        my $config = Config::INI::Reader->read_handle($handle);
        return $config;
    }
    else {
        if ( -d $profile_ini_directory ) {
            my $profile_ini_path =
              File::Spec->catfile( $profile_ini_directory, 'profiles.ini' );
            if ( -f $profile_ini_path ) {
                my $config = Config::INI::Reader->read_file($profile_ini_path);
                return $config;
            }
        }
    }
    return {};
}

sub default_name {
    my ($class)               = @_;
    my $profile_ini_directory = $class->profile_ini_directory();
    my $config                = $class->_read_ini_file($profile_ini_directory);
    foreach my $key (
        sort { $config->{$a}->{Name} cmp $config->{$b}->{Name} }
        grep { exists $config->{$_}->{Name} } keys %{$config}
      )
    {
        if ( ( $config->{$key}->{Default} ) && ( $config->{$key}->{Name} ) ) {
            return $config->{$key}->{Name};
        }
    }
    return;
}

sub names {
    my ($class)               = @_;
    my $profile_ini_directory = $class->profile_ini_directory();
    my $config                = $class->_read_ini_file($profile_ini_directory);
    my @names;
    foreach my $key (
        sort { $config->{$a}->{Name} cmp $config->{$b}->{Name} }
        grep { exists $config->{$_}->{Name} } keys %{$config}
      )
    {
        if ( defined $config->{$key}->{Name} ) {
            push @names, $config->{$key}->{Name};
        }
    }
    return @names;
}

sub path {
    my ( $class, $name ) = @_;

lib/Firefox/Marionette/Profile.pm  view on Meta::CPAN

=head2 names

returns a list of existing profile names that this module can discover on the filesystem.

=head2 default_name

returns the default profile name.

=head2 directory

accepts a profile name and returns the directory path that contains the C<prefs.js> file.

=head2 download_directory

accepts a directory path that will contain downloaded files.  Returns the previous value for download directory.

=head2 existing

accepts a profile name and returns a L<profile|Firefox::Marionette::Profile> object for that specified profile name.

=head2 parse

accepts a path as the parameter.  This path should be to a C<prefs.js> file.  Parses the file and returns it as a L<profile|Firefox::Marionette::Profile>.

=head2 parse_by_handle

accepts a filehandle as the parameter to a C<prefs.js> file.  Parses the file and returns it as a L<profile|Firefox::Marionette::Profile>.

=head2 path

accepts a profile name and returns the corresponding path to the C<prefs.js> file.

=head2 profile_ini_directory

returns the base directory for profiles.

=head2 save

accepts a path as the parameter.  Saves the current profile to this location.

=head2 as_string

returns the contents of current profile as a string.

=head2 get_value

accepts a key name (such as C<browser.startup.homepage>) and returns the value of the key from the profile.

=head2 set_value

accepts a key name (such as C<browser.startup.homepage>) and a value (such as C<https://duckduckgo.com>) and sets this value in the profile.  It returns itself to aid in chaining methods

=head2 clear_value

accepts a key name (such as C<browser.startup.homepage>) and removes the key from the profile.  It returns the old value of the key (if any).

=head1 DIAGNOSTICS

=over
 
=item C<< Failed to execute getpwuid for %s:%s >>
 
The module was unable to to execute L<perlfunc/getpwuid>.  This is probably a bug in this module's logic.  Please report as described in the BUGS AND LIMITATIONS section below.

=item C<< Failed to open '%s' for writing:%s >>
 
The module was unable to open the named file.  Maybe your disk is full or the file permissions need to be changed?

=item C<< Failed to write to '%s':%s >>
 
The module was unable to write to the named file.  Maybe your disk is full?

=item C<< Failed to close '%s':%s >>
 
The module was unable to close a handle to the named file.  Something is seriously wrong with your environment.

=item C<< Failed to rename '%s' to '%s':%s >>
 
The module was unable to rename the named file to the second file.  Something is seriously wrong with your environment.

=item C<< Failed to open '%s' for reading:%s >>
 
The module was unable to open the named file.  Maybe your disk is full or the file permissions need to be changed?

=item C<< Failed to parse line '%s' >>
 
The module was unable to parse the line for a Firefox prefs.js configuration.  This is probably a bug in this module's logic.  Please report as described in the BUGS AND LIMITATIONS section below.

=back

=head1 CONFIGURATION AND ENVIRONMENT

Firefox::Marionette::Profile requires no configuration files or environment variables.

=head1 DEPENDENCIES

Firefox::Marionette::Profile requires the following non-core Perl modules
 
=over
 
=item *
L<Config::INI::Reader|Config::INI::Reader>
 
=back

=head1 INCOMPATIBILITIES

None reported.

=head1 BUGS AND LIMITATIONS

To report a bug, or view the current list of bugs, please visit L<https://github.com/david-dick/firefox-marionette/issues>

=head1 AUTHOR

David Dick  C<< <ddick@cpan.org> >>

=head1 LICENSE AND COPYRIGHT

Copyright (c) 2024, David Dick C<< <ddick@cpan.org> >>. All rights reserved.

This module is free software; you can redistribute it and/or
modify it under the same terms as Perl itself. See L<perlartistic/perlartistic>.



( run in 2.583 seconds using v1.01-cache-2.11-cpan-140bd7fdf52 )