Skip to content

Commit

Permalink
added migrateBookmarks
Browse files Browse the repository at this point in the history
  • Loading branch information
jamesstout committed Dec 29, 2020
1 parent d0e6eb0 commit 8731ca5
Show file tree
Hide file tree
Showing 3 changed files with 106 additions and 4 deletions.
104 changes: 101 additions & 3 deletions Source/Controllers/Other/SecureBookmarkManager.swift
Original file line number Diff line number Diff line change
Expand Up @@ -27,18 +27,21 @@ import os.log

private var resolvedBookmarks: [URL] = []
private let URLBookmarkResolutionWithSecurityScope = URL.BookmarkResolutionOptions(rawValue: 1 << 10)
private let URLBookmarkCreationWithSecurityScope = URL.BookmarkCreationOptions(rawValue: 1 << 11)

private let log: OSLog
private let prefs: UserDefaults = UserDefaults.standard
private var observer: NSKeyValueObservation?

private var iChangedTheBookmarks: Bool = false
private var bookmarksHaveBeenMigrated: Bool = false

override init() {
if let bundleID = Bundle.main.bundleIdentifier {
log = OSLog(subsystem: bundleID, category: "secureBookmarks")
}
else{
log = OSLog.`default`
log = OSLog.default
}

os_log("SecureBookmarkManager init.", log: log, type: .info)
Expand Down Expand Up @@ -73,8 +76,19 @@ import os.log

os_log("bookmarks = %@", log: log, type: .info, bookmarks)

// I think part of init of this manager should be to re-request access
reRequestSecureAccessToBookmarks()
let bookmarksHaveBeenMigrated = prefs.bool(forKey: SPSecureBookmarksHaveBeenMigrated)
os_log("bookmarksHaveBeenMigrated: %d", log: log, type: .debug, bookmarksHaveBeenMigrated)

if bookmarksHaveBeenMigrated == false {
if migrateBookmarks() == true {
// after migration, re-request.
reRequestSecureAccessToBookmarks()
}
}
else {
// I think part of init of this manager should be to re-request access
reRequestSecureAccessToBookmarks()
}

os_log("resolvedBookmarks count = %i", log: log, type: .info, resolvedBookmarks.count)
os_log("staleBookmarks count = %i", log: log, type: .info, staleBookmarks.count)
Expand All @@ -83,12 +97,96 @@ import os.log
Crashlytics.crashlytics().log("staleBookmarks count = \(staleBookmarks.count)")
}

/// migrateBookmarks
// load up old bookmarks into array
// loop though resolving them to URLS
// Attempt to create secure bookmark
// Attempt to create SecureBookmark object
// Attempt to getEncodedData for object
// Add to bookmarks and save to prefs
private func migrateBookmarks() -> Bool {
os_log("migrating old format Bookmarks", log: log, type: .debug)

var migratedBookmarks: [Dictionary<String, Data>] = []

for bookmarkDict in bookmarks {
for (key, urlData) in bookmarkDict {
os_log("Bookmark URL = %@", log: log, type: .info, key)

var bookmarkDataIsStale = false

do {
os_log("Attempting to resolve bookmark data for %@", log: log, type: .debug, key)
Crashlytics.crashlytics().log("Attempting to resolve bookmark data for: \(key)")
// always resolve with just URLBookmarkResolutionWithSecurityScope
let urlForBookmark = try URL(resolvingBookmarkData: urlData, options: [URLBookmarkResolutionWithSecurityScope], relativeTo: nil, bookmarkDataIsStale: &bookmarkDataIsStale)

if bookmarkDataIsStale {
os_log("The bookmark is outdated and needs to be regenerated: key = %@", log: log, type: .error, key)
Crashlytics.crashlytics().log("The bookmark is outdated and needs to be regenerated: key = \(key)")
staleBookmarks.append(key)
continue
} else {
os_log("Resolved bookmark: %@", log: log, type: .info, key)
Crashlytics.crashlytics().log("Resolved bookmark: \(key)")
}

os_log("Attempting to create SecureBookmark object for %@", log: log, type: .debug, urlForBookmark.absoluteString)
Crashlytics.crashlytics().log("Attempting to create SecureBookmark object for: \(urlForBookmark.absoluteString)")
// NSURLBookmarkCreationWithSecurityScope is a guess .. could be read only, BUT read only fails to resolve! so it has to be securityScope
let sp = SecureBookmark(data: urlData, options: Double(URLBookmarkCreationWithSecurityScope.rawValue), url: urlForBookmark)

os_log("Attempting to getEncodedData for %@", log: log, type: .debug, sp.debugDescription)
Crashlytics.crashlytics().log("Attempting getEncodedData for: \(sp.debugDescription)")

guard let spData = sp.getEncodedData() else {
os_log("Failed to getEncodedData for %@", log: log, type: .debug, sp.debugDescription)
Crashlytics.crashlytics().log("Failed to getEncodedData for: \(sp.debugDescription)")
staleBookmarks.append(key)
continue
}

os_log("SUCCESS: Migrated %@", log: log, type: .debug, urlForBookmark.absoluteString)
Crashlytics.crashlytics().log("SUCCESS: Migrated: \(urlForBookmark.absoluteString)")
migratedBookmarks.append([urlForBookmark.absoluteString: spData])

}
catch {
staleBookmarks.append(key)
os_log("Error resolving bookmark: key = %@. Error: %@", log: log, type: .error, key, error.localizedDescription)
Crashlytics.crashlytics().log("Error resolving bookmark: key = \(key). Error: \(error.localizedDescription)")
continue
}
}
}

bookmarksHaveBeenMigrated = true
iChangedTheBookmarks = true

// update prefs - keep a copy of the old bookmark data for the moment
prefs.set(true, forKey: SPSecureBookmarksHaveBeenMigrated)
prefs.set(migratedBookmarks, forKey: SASecureBookmarks)
prefs.set(bookmarks, forKey: SPSecureBookmarksOldFormat) // backup

return true
}

/// reRequestSecureAccessToBookmarks
// loops through current bookmarks from prefs and re-requests secure access
// NOTE: when re-requesting access (resolvingBookmarkData) you only need to use
// URLBookmarkResolutionWithSecurityScope, not the options it was originally created with
// otherwise it will be markes as stale.
private func reRequestSecureAccessToBookmarks() {

// re-read - must do this after migration, could put it in an if, but it only happens once per app start.
guard let secureBookmarks = prefs.array(forKey: SASecureBookmarks) as? [[String: Data]], secureBookmarks.isNotEmpty else {
os_log("Could not get secureBookmarks from prefs.", log: log, type: .error)
Crashlytics.crashlytics().log("Could not get secureBookmarks from prefs.")
return
}

bookmarks = secureBookmarks

let bmCopy = bookmarks

// start afresh
Expand Down
2 changes: 2 additions & 0 deletions Source/Other/Data/SPConstants.h
Original file line number Diff line number Diff line change
Expand Up @@ -425,6 +425,8 @@ extern NSString *SPFileName24HourTimeTokenName;
extern NSString *SPFileNameFavoriteTokenName;
extern NSString *SPFileNameTableTokenName;
extern NSString *SASecureBookmarks;
extern NSString *SPSecureBookmarksOldFormat;
extern NSString *SPSecureBookmarksHaveBeenMigrated;

// Misc
extern NSString *SPContentFilters;
Expand Down
4 changes: 3 additions & 1 deletion Source/Other/Data/SPConstants.m
Original file line number Diff line number Diff line change
Expand Up @@ -218,7 +218,9 @@ - (SPTableViewType)tableViewTypeEnumFromString{
NSString *SPCSVFieldImportMappingAlignment = @"CSVFieldImportMappingAlignment";
NSString *SPImportClipboardTempFileNamePrefix = @"~/tmp/_SP_ClipBoard_Import_File_";
NSString *SPLastExportSettings = @"LastExportSettings";
NSString *SASecureBookmarks = @"SPSecureBookmarks";
NSString *SASecureBookmarks = @"SPSecureBookmarks";
NSString *SPSecureBookmarksOldFormat = @"SPSecureBookmarksOldFormat";
NSString *SPSecureBookmarksHaveBeenMigrated = @"SPSecureBookmarksHaveBeenMigrated";
// Export filename tokens
NSString *SPFileNameDatabaseTokenName = @"database";
NSString *SPFileNameHostTokenName = @"host";
Expand Down

0 comments on commit 8731ca5

Please sign in to comment.