This repository has been archived by the owner on May 10, 2024. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 448
/
Copy pathMigration.swift
389 lines (326 loc) · 16.7 KB
/
Migration.swift
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
import Foundation
import Shared
import BraveShared
import SwiftKeychainWrapper
import Data
import BraveCore
import Growth
import os.log
public class Migration {
private(set) public var braveCoreSyncObjectsMigrator: BraveCoreMigrator?
private let braveCore: BraveCoreMain
public init(braveCore: BraveCoreMain) {
self.braveCore = braveCore
}
public static var isChromiumMigrationCompleted: Bool {
return Preferences.Chromium.syncV2BookmarksMigrationCompleted.value && Preferences.Chromium.syncV2HistoryMigrationCompleted.value && Preferences.Chromium.syncV2PasswordMigrationCompleted.value
}
public func launchMigrations(keyPrefix: String, profile: Profile) {
Preferences.migratePreferences(keyPrefix: keyPrefix)
Preferences.migrateWalletPreferences()
if !Preferences.Migration.documentsDirectoryCleanupCompleted.value {
documentsDirectoryCleanup()
Preferences.Migration.documentsDirectoryCleanupCompleted.value = true
}
// `.migrate` is called in `BrowserViewController.viewDidLoad()`
if !Migration.isChromiumMigrationCompleted {
braveCoreSyncObjectsMigrator = BraveCoreMigrator(braveCore: braveCore, profile: profile)
}
if !Preferences.Migration.playlistV1FileSettingsLocationCompleted.value {
movePlaylistV1Items()
}
if Preferences.General.isFirstLaunch.value {
if UIDevice.current.userInterfaceIdiom == .phone {
// Default Value for preference of tab bar visibility for new users changed to landscape only
Preferences.General.tabBarVisibility.value = TabBarVisibility.landscapeOnly.rawValue
}
// Default url bar location for new users is bottom
Preferences.General.isUsingBottomBar.value = true
}
// Adding Observer to enable sync types
NotificationCenter.default.addObserver(
self,
selector: #selector(enableUserSelectedTypesForSync),
name: BraveServiceStateObserver.coreServiceLoadedNotification,
object: nil
)
}
public func moveDatabaseToApplicationDirectory() {
if Preferences.Database.DocumentToSupportDirectoryMigration.completed.value {
// Migration has been done in some regard, so drop out.
return
}
if Preferences.Database.DocumentToSupportDirectoryMigration.previousAttemptedVersion.value == AppInfo.appVersion {
// Migration has already been attempted for this version.
return
}
// Moves Coredata sqlite file from documents dir to application support dir.
do {
try DataController.shared.migrateToNewPathIfNeeded()
} catch {
Logger.module.error("\(error.localizedDescription)")
}
// Regardless of what happened, we attemtped a migration and document it:
Preferences.Database.DocumentToSupportDirectoryMigration.previousAttemptedVersion.value = AppInfo.appVersion
}
@objc private func enableUserSelectedTypesForSync() {
guard braveCore.syncAPI.isInSyncGroup else {
Logger.module.info("Sync is not active")
return
}
braveCore.syncAPI.enableSyncTypes(syncProfileService: braveCore.syncProfileService)
}
/// Adblock files don't have to be moved, they now have a new directory and will be downloaded there.
/// Downloads folder was nefer used before, it's a leftover from FF.
private func documentsDirectoryCleanup() {
FileManager.default.removeFolder(withName: "abp-data", location: .documentDirectory)
FileManager.default.removeFolder(withName: "https-everywhere-data", location: .documentDirectory)
FileManager.default.moveFile(
sourceName: "CookiesData.json", sourceLocation: .documentDirectory,
destinationName: "CookiesData.json",
destinationLocation: .applicationSupportDirectory)
}
private func movePlaylistV1Items() {
// If moving the file fails, we'll never bother trying again.
// It doesn't hurt and the user can easily delete it themselves.
defer {
Preferences.Migration.playlistV1FileSettingsLocationCompleted.value = true
}
guard let libraryPath = FileManager.default.urls(for: .libraryDirectory, in: .userDomainMask).first,
let playlistDirectory = PlaylistDownloadManager.playlistDirectory
else {
return
}
let errorPath = "PlaylistV1FileSettingsLocationCompletion"
do {
let urls = try FileManager.default.contentsOfDirectory(
at: libraryPath,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles])
for url in urls where url.absoluteString.contains("com.apple.UserManagedAssets") {
do {
let assets = try FileManager.default.contentsOfDirectory(
at: url,
includingPropertiesForKeys: nil,
options: [.skipsHiddenFiles])
assets.forEach({
if let item = PlaylistItem.cachedItem(cacheURL: $0),
let itemId = item.uuid {
let destination = playlistDirectory.appendingPathComponent($0.lastPathComponent)
do {
try FileManager.default.moveItem(at: $0, to: destination)
try PlaylistItem.updateCache(uuid: itemId, cachedData: destination.bookmarkData())
} catch {
Logger.module.error("Moving Playlist Item for \(errorPath) failed: \(error.localizedDescription)")
}
}
})
} catch {
Logger.module.error("Moving Playlist Item for \(errorPath) failed: \(error.localizedDescription)")
}
do {
try FileManager.default.removeItem(at: url)
} catch {
Logger.module.error("Deleting Playlist Item for \(errorPath) failed: \(error.localizedDescription)")
}
}
} catch {
Logger.module.error("Moving Playlist Files for \(errorPath) failed: \(error.localizedDescription)")
}
}
private static func movePlaylistV2Items() {
// Migrate all items not belonging to a folder
func migrateItemsToSavedFolder(folderUUID: String) {
let items = PlaylistItem.getItems(parentFolder: nil)
if !items.isEmpty {
PlaylistItem.moveItems(items: items.map({ $0.objectID }), to: folderUUID)
}
Preferences.Migration.playlistV2FoldersInitialMigrationCompleted.value = true
}
if PlaylistFolder.getFolder(uuid: PlaylistFolder.savedFolderUUID) != nil {
migrateItemsToSavedFolder(folderUUID: PlaylistFolder.savedFolderUUID)
} else {
PlaylistFolder.addFolder(title: Strings.PlaylistFolders.playlistSavedFolderTitle, uuid: PlaylistFolder.savedFolderUUID) { uuid in
if PlaylistFolder.getFolder(uuid: uuid) != nil {
migrateItemsToSavedFolder(folderUUID: uuid)
} else {
Logger.module.error("Failed Moving Playlist items to Saved Folder - Unknown Error")
}
}
}
}
private static func playlistFolderSharingIdentifierV2Migration() {
let itemIDs = PlaylistItem.all().map({ $0.objectID })
DataController.performOnMainContext(save: true) { context in
let items = itemIDs.compactMap({ context.object(with: $0) as? PlaylistItem })
items.forEach({
if $0.uuid == nil {
$0.uuid = UUID().uuidString
}
})
Preferences.Migration.playlistV2SharedFoldersInitialMigrationCompleted.value = true
}
}
public static func postCoreDataInitMigrations() {
if !Preferences.Migration.removeLargeFaviconsMigrationCompleted.value {
FaviconMO.clearTooLargeFavicons()
Preferences.Migration.removeLargeFaviconsMigrationCompleted.value = true
}
if !Preferences.Migration.playlistV2FoldersInitialMigrationCompleted.value {
movePlaylistV2Items()
}
if !Preferences.Migration.playlistV2SharedFoldersInitialMigrationCompleted.value {
playlistFolderSharingIdentifierV2Migration()
}
if !Preferences.Migration.xcgloggerFilesRemovalCompleted.value {
LegacyLogsMigration.run()
Preferences.Migration.xcgloggerFilesRemovalCompleted.value = true
}
if Preferences.Migration.coreDataCompleted.value { return }
// In 1.6.6 we included private tabs in CoreData (temporarely) until the user did one of the following:
// - Cleared private data
// - Exited Private Mode
// - The app was terminated (bug)
// However due to a bug, some private tabs remain in the container. Since 1.7 removes `isPrivate` from TabMO,
// we must dismiss any records that are private tabs during migration from Model7
TabMO.deleteAllPrivateTabs()
Domain.migrateShieldOverrides()
LegacyBookmarksHelper.migrateBookmarkOrders()
Preferences.Migration.coreDataCompleted.value = true
}
}
fileprivate extension Preferences {
/// Migration preferences
final class Migration {
static let completed = Option<Bool>(key: "migration.completed", default: false)
/// Old app versions were using documents directory to store app files, database, adblock files.
/// These files are now moved to 'Application Support' folder, and documents directory is left
/// for user downloaded files.
static let documentsDirectoryCleanupCompleted =
Option<Bool>(key: "migration.documents-dir-completed", default: false)
static let playlistV1FileSettingsLocationCompleted =
Option<Bool>(key: "migration.playlistv1-file-settings-location-completed", default: false)
static let playlistV2FoldersInitialMigrationCompleted =
Option<Bool>(key: "migration.playlistv2-folders-initial-migration-2-completed", default: false)
static let playlistV2SharedFoldersInitialMigrationCompleted =
Option<Bool>(key: "migration.playlistv2-sharedfolders-initial-migration-2-completed", default: false)
static let removeLargeFaviconsMigrationCompleted =
Option<Bool>(key: "migration.remove-large-favicons", default: false)
// This is new preference introduced in iOS 1.32.3, tracks whether we should perform database migration.
// It should be called only for users who have not completed the migration beforehand.
// The reason for second migration flag is to first do file system migrations like moving database files,
// then do CRUD operations on the db if needed.
static let coreDataCompleted = Option<Bool>(
key: "migration.cd-completed",
default: Preferences.Migration.completed.value)
/// A new preference key will be introduced in 1.44.x, indicates if Wallet Preferences migration has completed
static let walletProviderAccountRequestCompleted =
Option<Bool>(key: "migration.wallet-provider-account-request-completed", default: false)
// See #5928. We used XCGLogger to handle logs, which used a custom rolling files in the system.
// After migrating to Apples OSLog those files have to be removed.
static let xcgloggerFilesRemovalCompleted =
Option<Bool>(key: "migration.xcglogger-file-removal-completed", default: false)
}
/// Migrate a given key from `Prefs` into a specific option
class func migrate<T>(keyPrefix: String, key: String, to option: Preferences.Option<T>, transform: ((T) -> T)? = nil) {
let userDefaults = UserDefaults(suiteName: AppInfo.sharedContainerIdentifier)
let profileKey = "\(keyPrefix)\(key)"
// Have to do two checks because T may be an Optional, since object(forKey:) returns Any? it will succeed
// as casting to T if T is Optional even if the key doesnt exist.
let value = userDefaults?.object(forKey: profileKey)
if value != nil, let value = value as? T {
if let transform = transform {
option.value = transform(value)
} else {
option.value = value
}
userDefaults?.removeObject(forKey: profileKey)
} else {
Logger.module.info("Could not migrate legacy pref with key: \"\(profileKey)\".")
}
}
/// Migrate the users preferences from prior versions of the app (<2.0)
class func migratePreferences(keyPrefix: String) {
if Preferences.Migration.completed.value {
return
}
/// Wrapper around BraveShared migrate, to automate prefix injection
func migrate<T>(key: String, to option: Preferences.Option<T>, transform: ((T) -> T)? = nil) {
self.migrate(keyPrefix: keyPrefix, key: key, to: option, transform: transform)
}
// General
migrate(key: "saveLogins", to: Preferences.General.saveLogins)
migrate(key: "blockPopups", to: Preferences.General.blockPopups)
migrate(key: "kPrefKeyTabsBarShowPolicy", to: Preferences.General.tabBarVisibility)
// Search
migrate(key: "search.orderedEngineNames", to: Preferences.Search.orderedEngines)
migrate(key: "search.disabledEngineNames", to: Preferences.Search.disabledEngines)
migrate(key: "search.suggestions.show", to: Preferences.Search.showSuggestions)
migrate(key: "search.suggestions.showOptIn", to: Preferences.Search.shouldShowSuggestionsOptIn)
migrate(key: "search.default.name", to: Preferences.Search.defaultEngineName)
migrate(key: "search.defaultprivate.name", to: Preferences.Search.defaultPrivateEngineName)
// Privacy
migrate(key: "privateBrowsingAlwaysOn", to: Preferences.Privacy.privateBrowsingOnly)
migrate(key: "clearprivatedata.toggles", to: Preferences.Privacy.clearPrivateDataToggles)
// Make sure to unlock all directories that may have been locked in 1.6 private mode
let baseDir = NSSearchPathForDirectoriesInDomains(.libraryDirectory, .userDomainMask, true)[0]
[baseDir + "/WebKit", baseDir + "/Caches"].forEach {
do {
try FileManager.default.setAttributes([.posixPermissions: NSNumber(value: 0o755 as Int16)], ofItemAtPath: $0)
} catch {
Logger.module.error("Failed setting the directory attributes for \($0)")
}
}
// Security
NSKeyedUnarchiver.setClass(AuthenticationKeychainInfo.self, forClassName: "AuthenticationKeychainInfo")
// Solely for 1.6.6 -> 1.7 migration
if let pinLockInfo = KeychainWrapper.standard.object(forKey: "pinLockInfo") as? AuthenticationKeychainInfo {
// Checks if browserLock was enabled in old app (1.6.6)
let browserLockKey = "\(keyPrefix)browserLock"
let isBrowserLockEnabled = Preferences.defaultContainer.bool(forKey: browserLockKey)
// Preference no longer controls passcode functionality, so delete it
Preferences.defaultContainer.removeObject(forKey: browserLockKey)
if isBrowserLockEnabled {
KeychainWrapper.sharedAppContainerKeychain.setAuthenticationInfo(pinLockInfo)
}
// No longer use this key, so remove, rely on other mechanisms
KeychainWrapper.standard.removeObject(forKey: "pinLockInfo")
}
// Shields
migrate(key: "braveBlockAdsAndTracking", to: Preferences.Shields.blockAdsAndTracking)
migrate(key: "braveHttpsEverywhere", to: Preferences.Shields.httpsEverywhere)
migrate(key: "braveSafeBrowsing", to: Preferences.Shields.blockPhishingAndMalware)
migrate(key: "noscript_on", to: Preferences.Shields.blockScripts)
migrate(key: "fingerprintprotection_on", to: Preferences.Shields.fingerprintingProtection)
migrate(key: "braveAdblockUseRegional", to: Preferences.Shields.useRegionAdBlock)
// DAU
migrate(key: "dau_stat", to: Preferences.DAU.lastLaunchInfo)
migrate(key: "week_of_installation", to: Preferences.DAU.weekOfInstallation)
// URP
migrate(key: "urpDateCheckPrefsKey", to: Preferences.URP.nextCheckDate)
migrate(key: "urpRetryCountdownPrefsKey", to: Preferences.URP.retryCountdown)
migrate(key: "downloadIdPrefsKey", to: Preferences.URP.downloadId)
migrate(key: "referralCodePrefsKey", to: Preferences.URP.referralCode)
migrate(key: "referralCodeDeleteTimePrefsKey", to: Preferences.URP.referralCodeDeleteDate)
// Block Stats
migrate(key: "adblock", to: Preferences.BlockStats.adsCount)
migrate(key: "tracking_protection", to: Preferences.BlockStats.trackersCount)
migrate(key: "httpse", to: Preferences.BlockStats.httpsUpgradeCount)
migrate(key: "fp_protection", to: Preferences.BlockStats.fingerprintingCount)
migrate(key: "safebrowsing", to: Preferences.BlockStats.phishingCount)
// On 1.6 lastLaunchInfo is used to check if it's first app launch or not.
// This needs to be translated to our new preference.
Preferences.General.isFirstLaunch.value = Preferences.DAU.lastLaunchInfo.value == nil
Preferences.Migration.completed.value = true
}
/// Migrate Wallet Preferences from version <1.43
class func migrateWalletPreferences() {
guard Preferences.Migration.walletProviderAccountRequestCompleted.value != true else { return }
// Migrate `allowDappProviderAccountRequests` to `allowEthProviderAccess`
migrate(keyPrefix: "", key: "wallet.allow-eth-provider-account-requests", to: Preferences.Wallet.allowEthProviderAccess)
Preferences.Migration.walletProviderAccountRequestCompleted.value = true
}
}