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

Import error improvements #406

Merged
merged 9 commits into from
Feb 9, 2022
4 changes: 4 additions & 0 deletions DuckDuckGo.xcodeproj/project.pbxproj
Original file line number Diff line number Diff line change
Expand Up @@ -103,6 +103,7 @@
4B723E1326B0007A00E14D75 /* CSVLoginExporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B723DFD26B0002B00E14D75 /* CSVLoginExporter.swift */; };
4B723E1926B000DC00E14D75 /* TemporaryFileCreator.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B723E1726B000DC00E14D75 /* TemporaryFileCreator.swift */; };
4B78A86B26BB3ADD0071BB16 /* BrowserImportSummaryViewController.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B78A86A26BB3ADD0071BB16 /* BrowserImportSummaryViewController.swift */; };
4B7A57CF279A4EF300B1C70E /* ChromePreferences.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7A57CE279A4EF300B1C70E /* ChromePreferences.swift */; };
4B7A60A1273E0BE400BBDFEB /* WKWebsiteDataStoreExtension.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B7A60A0273E0BE400BBDFEB /* WKWebsiteDataStoreExtension.swift */; };
4B82E9B325B69E3E00656FE7 /* TrackerRadarKit in Frameworks */ = {isa = PBXBuildFile; productRef = 4B82E9B225B69E3E00656FE7 /* TrackerRadarKit */; };
4B8AC93326B3B06300879451 /* EdgeDataImporter.swift in Sources */ = {isa = PBXBuildFile; fileRef = 4B8AC93226B3B06300879451 /* EdgeDataImporter.swift */; };
Expand Down Expand Up @@ -765,6 +766,7 @@
4B723E0426B0003E00E14D75 /* CSVLoginExporterTests.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = CSVLoginExporterTests.swift; sourceTree = "<group>"; };
4B723E1726B000DC00E14D75 /* TemporaryFileCreator.swift */ = {isa = PBXFileReference; fileEncoding = 4; lastKnownFileType = sourcecode.swift; path = TemporaryFileCreator.swift; sourceTree = "<group>"; };
4B78A86A26BB3ADD0071BB16 /* BrowserImportSummaryViewController.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = BrowserImportSummaryViewController.swift; sourceTree = "<group>"; };
4B7A57CE279A4EF300B1C70E /* ChromePreferences.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = ChromePreferences.swift; sourceTree = "<group>"; };
4B7A60A0273E0BE400BBDFEB /* WKWebsiteDataStoreExtension.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = WKWebsiteDataStoreExtension.swift; sourceTree = "<group>"; };
4B8AC93226B3B06300879451 /* EdgeDataImporter.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = EdgeDataImporter.swift; sourceTree = "<group>"; };
4B8AC93426B3B2FD00879451 /* NSAlert+DataImport.swift */ = {isa = PBXFileReference; lastKnownFileType = sourcecode.swift; path = "NSAlert+DataImport.swift"; sourceTree = "<group>"; };
Expand Down Expand Up @@ -1633,6 +1635,7 @@
children = (
4B723DEB26B0002B00E14D75 /* DataImport.swift */,
4B59024726B3673600489384 /* ThirdPartyBrowser.swift */,
4B7A57CE279A4EF300B1C70E /* ChromePreferences.swift */,
4BB99CF326FE191E001E4761 /* Bookmarks */,
4B723DF126B0002B00E14D75 /* Logins */,
4B723DEC26B0002B00E14D75 /* View */,
Expand Down Expand Up @@ -3998,6 +4001,7 @@
85D885B326A5A9DE0077C374 /* NSAlert+PasswordManager.swift in Sources */,
85E11C2F25E7DC7E00974CAF /* ExternalURLHandler.swift in Sources */,
85308E27267FCB22001ABD76 /* PasswordManagerSettings.swift in Sources */,
4B7A57CF279A4EF300B1C70E /* ChromePreferences.swift in Sources */,
AA6AD95B2704B6DB00159F8A /* FirePopoverViewController.swift in Sources */,
85A0116925AF1D8900FA6A0C /* FindInPageViewController.swift in Sources */,
4BB6CE5F26B77ED000EC5860 /* Cryptography.swift in Sources */,
Expand Down
29 changes: 29 additions & 0 deletions DuckDuckGo/Data Import/ChromePreferences.swift
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
//
// ChromePreferences.swift
//
// Copyright © 2022 DuckDuckGo. All rights reserved.
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.
//

import AppKit

struct ChromePreferences: Decodable {

struct ChromeProfile: Decodable {
let name: String
}

let profile: ChromeProfile

}
52 changes: 44 additions & 8 deletions DuckDuckGo/Data Import/DataImport.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.
//

import Foundation
import AppKit

enum DataImport {

Expand Down Expand Up @@ -79,7 +79,12 @@ enum DataImport {
case .brave, .chrome, .edge:
// Chromium profiles are either named "Default", or a series of incrementing profile names, i.e. "Profile 1", "Profile 2", etc.
let potentialProfiles = profileURLs.map(BrowserProfile.init(profileURL:))
let filteredProfiles = potentialProfiles.filter { $0.name == "Default" || $0.name.hasPrefix("Profile ") }
let filteredProfiles = potentialProfiles.filter {
$0.hasNonDefaultProfileName ||
$0.profileName == "Default" ||
$0.profileName.hasPrefix("Profile ")
samsymons marked this conversation as resolved.
Show resolved Hide resolved
}

let sortedProfiles = filteredProfiles.sorted()

self.profiles = sortedProfiles
Expand All @@ -97,24 +102,37 @@ enum DataImport {
var defaultProfile: BrowserProfile? {
switch browser {
case .brave, .chrome, .edge:
return profiles.first { $0.name == "Default" } ?? profiles.first
return profiles.first { $0.profileName == "Default" } ?? profiles.first
case .firefox:
return profiles.first { $0.name == "default-release" } ?? profiles.first
return profiles.first { $0.profileName == "default-release" } ?? profiles.first
case .safari:
return nil
}
}
}

struct BrowserProfile: Comparable {
enum Constants {
static let chromePreferencesFileName = "Preferences"
}

let profileURL: URL
var profileName: String {
return detectedChromePreferencesProfileName ?? fallbackProfileName
}

var hasNonDefaultProfileName: Bool {
return detectedChromePreferencesProfileName != nil
}

private let fallbackProfileName: String
private let detectedChromePreferencesProfileName: String?

init(profileURL: URL) {
self.profileURL = profileURL
}

var name: String {
return profileURL.lastPathComponent.components(separatedBy: ".").last ?? profileURL.lastPathComponent
self.fallbackProfileName = Self.getDefaultProfileName(at: profileURL)
self.detectedChromePreferencesProfileName = Self.getChromeProfileName(at: profileURL)
}

var hasLoginData: Bool {
Expand All @@ -128,8 +146,26 @@ enum DataImport {
return hasChromiumData || hasFirefoxData
}

private static func getDefaultProfileName(at profileURL: URL) -> String {
return profileURL.lastPathComponent.components(separatedBy: ".").last ?? profileURL.lastPathComponent
}

private static func getChromeProfileName(at profileURL: URL) -> String? {
guard let profileDirectoryContents = try? FileManager.default.contentsOfDirectory(atPath: profileURL.path) else {
return nil
}

if profileDirectoryContents.contains(Constants.chromePreferencesFileName),
let chromePreferenceData = try? Data(contentsOf: profileURL.appendingPathComponent(Constants.chromePreferencesFileName)),
let chromePreferences = try? JSONDecoder().decode(ChromePreferences.self, from: chromePreferenceData) {
return chromePreferences.profile.name
}

return nil
}

static func < (lhs: DataImport.BrowserProfile, rhs: DataImport.BrowserProfile) -> Bool {
return lhs.name.localizedCompare(rhs.name) == .orderedAscending
return lhs.profileName.localizedCompare(rhs.profileName) == .orderedAscending
}
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -62,6 +62,13 @@ final class FirefoxLoginReader {
let databasePath = firefoxProfileURL.appendingPathComponent(keyDatabaseName).path

let loginsPath = firefoxProfileURL.appendingPathComponent(loginsFileName).path

// If there isn't a file where logins are expected, consider it a successful import of 0 logins
// to avoid showing an error state.
guard FileManager.default.fileExists(atPath: loginsPath) else {
return .success([])
}

guard let logins = readLoginsFile(from: loginsPath) else {
return .failure(.couldNotReadLoginsFile)
}
Expand Down
7 changes: 5 additions & 2 deletions DuckDuckGo/Data Import/ThirdPartyBrowser.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.
//

import Foundation
import AppKit

private struct BundleIdentifiers {
let production: String
Expand Down Expand Up @@ -52,7 +52,10 @@ enum ThirdPartyBrowser: CaseIterable {
}

var isInstalled: Bool {
return applicationPath != nil
let detectedApplicationPath = applicationPath != nil
let detectedBrowserProfiles = !(browserProfiles?.profiles.isEmpty ?? true)

return detectedApplicationPath && detectedBrowserProfiles
}

var isRunning: Bool {
Expand Down
8 changes: 4 additions & 4 deletions DuckDuckGo/Data Import/View/BrowserImportViewController.swift
Original file line number Diff line number Diff line change
Expand Up @@ -16,7 +16,7 @@
// limitations under the License.
//

import Foundation
import AppKit

protocol BrowserImportViewControllerDelegate: AnyObject {

Expand Down Expand Up @@ -79,7 +79,7 @@ final class BrowserImportViewController: NSViewController {
return profileList.validImportableProfiles.first
}

return profileList.validImportableProfiles.first { $0.name == selectedProfile.title }
return profileList.validImportableProfiles.first { $0.profileName == selectedProfile.title }
}

let browser: DataImport.Source
Expand Down Expand Up @@ -170,9 +170,9 @@ extension NSPopUpButton {
var selectedSourceIndex: Int?

for (index, profile) in validProfiles.enumerated() {
addItem(withTitle: profile.name)
addItem(withTitle: profile.profileName)

if profile.name == defaultProfile?.name {
if profile.profileName == defaultProfile?.profileName {
selectedSourceIndex = index
}
}
Expand Down