11using System ;
22using System . Collections . Generic ;
3+ using System . IO ;
34using System . Linq ;
45using System . Runtime . InteropServices ;
56using System . Threading . Tasks ;
@@ -20,6 +21,7 @@ public class FirefoxLauncher : LauncherBase
2021 ] ;
2122
2223 private static readonly string [ ] _profileCommandLineArguments = [ "-profile" , "--profile" ] ;
24+ private readonly string _userDataDir ;
2325
2426 /// <summary>
2527 /// Initializes a new instance of the <see cref="FirefoxLauncher"/> class.
@@ -29,7 +31,7 @@ public class FirefoxLauncher : LauncherBase
2931 public FirefoxLauncher ( string executable , LaunchOptions options )
3032 : base ( executable , options )
3133 {
32- ( var firefoxArgs , TempUserDataDir ) = PrepareFirefoxArgs ( options ) ;
34+ ( var firefoxArgs , TempUserDataDir , _userDataDir ) = PrepareFirefoxArgs ( options ) ;
3335
3436 Process . StartInfo . Arguments = string . Join ( " " , firefoxArgs ) ;
3537 }
@@ -79,7 +81,35 @@ internal static string[] GetDefaultArgs(LaunchOptions options)
7981 return firefoxArguments . ToArray ( ) ;
8082 }
8183
82- private static ( List < string > FirefoxArgs , TempDirectory TempUserDataDirectory ) PrepareFirefoxArgs ( LaunchOptions options )
84+ internal override void OnExit ( )
85+ {
86+ // If TempUserDataDir is null it means that the user provided their own userDataDir
87+ if ( TempUserDataDir is null )
88+ {
89+ var backupSuffix = ".puppeteer" ;
90+ string [ ] backupFiles = [ "prefs.js" , "user.js" ] ;
91+ var basePath = _userDataDir . Unquote ( ) ;
92+ foreach ( var backupFile in backupFiles )
93+ {
94+ var backupPath = Path . Combine ( basePath , backupFile + backupSuffix ) ;
95+ var originalPath = Path . Combine ( basePath , backupFile ) ;
96+ if ( File . Exists ( backupPath ) )
97+ {
98+ // We don't have the overwrite parameter in netstandard
99+ if ( File . Exists ( originalPath ) )
100+ {
101+ File . Delete ( originalPath ) ;
102+ }
103+
104+ File . Move ( backupPath , Path . Combine ( basePath , backupFile ) ) ;
105+ }
106+ }
107+ }
108+
109+ base . OnExit ( ) ;
110+ }
111+
112+ private static ( List < string > FirefoxArgs , TempDirectory TempUserDataDirectory , string UserDataDir ) PrepareFirefoxArgs ( LaunchOptions options )
83113 {
84114 var firefoxArguments = new List < string > ( ) ;
85115
@@ -126,7 +156,7 @@ private static (List<string> FirefoxArgs, TempDirectory TempUserDataDirectory) P
126156
127157 Firefox . CreateProfile ( userDataDir , GetPreferences ( options . ExtraPrefsFirefox ) ) ;
128158
129- return ( firefoxArguments , tempUserDataDirectory ) ;
159+ return ( firefoxArguments , tempUserDataDirectory , userDataDir ) ;
130160 }
131161
132162 private static Dictionary < string , object > GetPreferences ( Dictionary < string , object > optionsExtraPreferencesFirefox )
0 commit comments