source: launchers/macosx/I2PLauncher/Utils/browser/FirefoxManager.swift @ ba853a8c

Last change on this file since ba853a8c was ba853a8c, checked in by meeh <meeh@…>, 23 months ago

Mac OSX Launcher: The firefox manager code

  • Property mode set to 100644
File size: 2.5 KB
Line 
1//
2//  FirefoxManager.swift
3//  I2PLauncher
4//
5//  Created by Mikal Villa on 08/12/2018.
6//  Copyright © 2018 The I2P Project. All rights reserved.
7//
8
9import Foundation
10
11
12class FirefoxManager {
13 
14  var firefoxAppPath = ""
15  private var isFirefoxFound = false
16  private var isFirefoxProfileExtracted = false
17 
18  fileprivate func directoryExistsAtPath(_ path: String) -> Bool {
19    var isDirectory = ObjCBool(true)
20    let exists = FileManager.default.fileExists(atPath: path, isDirectory: &isDirectory)
21    return exists && isDirectory.boolValue
22  }
23 
24  func IsFirefoxFound() -> Bool {
25    return self.isFirefoxFound
26  }
27 
28  func IsProfileExtracted() -> Bool {
29    return self.isFirefoxProfileExtracted
30  }
31 
32  func bundleExecutableSuffixPath() -> String {
33    return "/Contents/MacOS/firefox"
34  }
35 
36  func executeFirefox() -> Bool {
37    let fullExecPath = "\(self.firefoxAppPath)\(self.bundleExecutableSuffixPath())"
38    let firefoxProcess = Subprocess(executablePath: fullExecPath, arguments: [ "-profile", Preferences.shared()["I2Pref_firefoxProfilePath"] as! String, "http://127.0.0.1:7657/home" ])
39    DispatchQueue.global(qos: .background).async {
40      let proc = firefoxProcess.execute()
41    }
42    return true
43  }
44
45  func tryAutoDetect() -> Bool {
46    let expectedPath = Preferences.shared()["I2Pref_firefoxBundlePath"] as! String
47   
48    self.isFirefoxProfileExtracted = directoryExistsAtPath(Preferences.shared()["I2Pref_firefoxProfilePath"] as! String)
49   
50    let result = directoryExistsAtPath(expectedPath)
51    self.isFirefoxFound = result
52    if (result) {
53      self.firefoxAppPath = expectedPath
54      return true
55    }
56    return false
57  }
58 
59 
60  private static var sharedFirefoxManager: FirefoxManager = {
61    let firefoxMgr = FirefoxManager()
62   
63    return firefoxMgr
64  }()
65 
66 
67  class func shared() -> FirefoxManager {
68    return sharedFirefoxManager
69  }
70}
71
72extension FirefoxManager {
73  func unzipProfile() -> Bool {
74    let resourceUrl = Bundle.main.url(forResource: "profile", withExtension: "tgz")
75    let profileTgz = resourceUrl!.path
76    let unzipProc = Subprocess(executablePath: "/usr/bin/tar", arguments: ["-xf",profileTgz,"-C",NSString(format: "%@/Library/Application Support/i2p", NSHomeDirectory()) as String])
77    DispatchQueue.global(qos: .background).async {
78      let proc = unzipProc.execute(captureOutput: true)
79      print("Firefox Profile Extraction Errors: \(proc?.errors)")
80      print("Firefox Profile Extraction Output: \(proc?.output)")
81    }
82    return false
83  }
84}
85
Note: See TracBrowser for help on using the repository browser.