Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Avoid an assertion failure when importing browser data #341

Merged
merged 1 commit into from
Nov 19, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
4 changes: 2 additions & 2 deletions DuckDuckGo/Data Import/DataImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -65,14 +65,14 @@ enum DataImport {
}

struct BrowserProfileList {
let browser: ThirdPartyBrowser.BrowserType
let browser: ThirdPartyBrowser
let profiles: [BrowserProfile]

var validImportableProfiles: [BrowserProfile] {
return profiles.filter(\.hasLoginData)
}

init(browser: ThirdPartyBrowser.BrowserType, profileURLs: [URL]) {
init(browser: ThirdPartyBrowser, profileURLs: [URL]) {
self.browser = browser

switch browser {
Expand Down
66 changes: 32 additions & 34 deletions DuckDuckGo/Data Import/ThirdPartyBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -28,46 +28,46 @@ private struct BundleIdentifiers {
}
}

struct ThirdPartyBrowser {

static var brave: ThirdPartyBrowser { ThirdPartyBrowser(type: .brave) }
static var chrome: ThirdPartyBrowser { ThirdPartyBrowser(type: .chrome) }
static var edge: ThirdPartyBrowser { ThirdPartyBrowser(type: .edge) }
static var firefox: ThirdPartyBrowser { ThirdPartyBrowser(type: .firefox) }
static var safari: ThirdPartyBrowser { ThirdPartyBrowser(type: .safari) }
enum ThirdPartyBrowser: CaseIterable {

case brave
case chrome
case edge
case firefox
case safari

static var installedBrowsers: [ThirdPartyBrowser] {
return allCases.filter(\.isInstalled)
}

static func browser(for source: DataImport.Source) -> ThirdPartyBrowser? {
switch source {
case .brave:
return Self.brave
case .chrome:
return Self.chrome
case .edge:
return Self.edge
case .firefox:
return Self.firefox
case .safari:
return Self.safari
case .csv:
return nil
case .brave: return .brave
case .chrome: return .chrome
case .edge: return .edge
case .firefox: return .firefox
case .safari: return .safari
case .csv: return nil
}
}

enum BrowserType {
case brave
case chrome
case edge
case firefox
case safari
}

var isInstalled: Bool {
return applicationPath != nil
}

var isRunning: Bool {
return !findRunningApplications().isEmpty
}

var importSource: DataImport.Source {
switch self {
case .brave: return .brave
case .chrome: return .chrome
case .edge: return .edge
case .firefox: return .firefox
case .safari: return .safari
}
}

var applicationIcon: NSImage? {
guard let applicationPath = applicationPath else {
Expand All @@ -85,14 +85,14 @@ struct ThirdPartyBrowser {
options: [.skipsHiddenFiles]).filter(\.hasDirectoryPath) else {
// Safari is an exception, as it may need permissions granted before being able to read the contents of the profile path. To be safe,
// return the profile anyway and check the file system permissions when preparing to import.
if type == .safari {
return DataImport.BrowserProfileList(browser: self.type, profileURLs: [profilePath])
if self == .safari {
return DataImport.BrowserProfileList(browser: self, profileURLs: [profilePath])
} else {
return nil
}
}

return DataImport.BrowserProfileList(browser: self.type, profileURLs: potentialProfileURLs)
return DataImport.BrowserProfileList(browser: self, profileURLs: potentialProfileURLs)
}

// Returns the first available path to the application. This will test the production bundle ID, and any known pre-release versions, such as the
Expand All @@ -108,7 +108,7 @@ struct ThirdPartyBrowser {
}

private var bundleIdentifiers: BundleIdentifiers {
switch type {
switch self {
case .brave: return BundleIdentifiers(productionBundleID: "com.brave.Browser", relatedBundleIDs: ["com.brave.Browser.nightly"])
case .chrome: return BundleIdentifiers(productionBundleID: "com.google.Chrome", relatedBundleIDs: ["com.google.Chrome.canary"])
case .edge: return BundleIdentifiers(productionBundleID: "com.microsoft.edgemac", relatedBundleIDs: [])
Expand All @@ -120,8 +120,6 @@ struct ThirdPartyBrowser {
}
}

private let type: BrowserType

func forceTerminate() {
let applications = findRunningApplications()

Expand All @@ -145,7 +143,7 @@ struct ThirdPartyBrowser {
private func profilesDirectory() -> URL {
let applicationSupportURL = FileManager.default.urls(for: .applicationSupportDirectory, in: .userDomainMask)[0]

switch type {
switch self {
case .brave: return applicationSupportURL.appendingPathComponent("BraveSoftware/Brave-Browser/")
case .chrome: return applicationSupportURL.appendingPathComponent("Google/Chrome/")
case .edge: return applicationSupportURL.appendingPathComponent("Microsoft Edge/")
Expand Down
10 changes: 9 additions & 1 deletion DuckDuckGo/Data Import/View/DataImportViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -39,14 +39,22 @@ final class DataImportViewController: NSViewController {
private struct ViewState {
var selectedImportSource: DataImport.Source
var interactionState: InteractionState

static func defaultState() -> ViewState {
if let firstInstalledBrowser = ThirdPartyBrowser.installedBrowsers.first {
return ViewState(selectedImportSource: firstInstalledBrowser.importSource, interactionState: .ableToImport)
} else {
return ViewState(selectedImportSource: .csv, interactionState: .ableToImport)
}
}
}

static func create() -> DataImportViewController {
let storyboard = NSStoryboard(name: Constants.storyboardName, bundle: nil)
return storyboard.instantiateController(identifier: Constants.identifier)
}

private var viewState: ViewState = ViewState(selectedImportSource: .brave, interactionState: .ableToImport) {
private var viewState: ViewState = .defaultState() {
didSet {
renderCurrentViewState()

Expand Down