Skip to content
This repository has been archived by the owner on Mar 25, 2024. It is now read-only.

Fix #471: Migrate per-domain shield overrides #583

Merged
merged 2 commits into from Dec 5, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
34 changes: 34 additions & 0 deletions Client/Application/Migration.swift
Expand Up @@ -6,6 +6,7 @@ import Foundation
import Shared
import BraveShared
import SwiftKeychainWrapper
import Data

private let log = Logger.browserLogger

Expand Down Expand Up @@ -82,6 +83,39 @@ extension Preferences {
// This needs to be translated to our new preference.
Preferences.General.isFirstLaunch.value = Preferences.DAU.lastLaunchInfo.value == nil

// Migrate the shield overrides
migrateShieldOverrides()

Preferences.Migration.completed.value = true
}

private class func migrateShieldOverrides() {
// 1.6 had an unfortunate bug that caused shield overrides to create new Domain objects using `http` regardless
// which would lead to a duplicated Domain object using http that held custom shield overides settings for that
// domain
//
// Therefore we need to migrate any `http` Domains shield overrides to its sibiling `https` domain if it exists
let allHttpPredicate = NSPredicate(format: "url BEGINSWITH[cd] 'http://'")
let backgroundContext = DataController.newBackgroundContext()

guard let httpDomains = Domain.all(where: allHttpPredicate, context: backgroundContext) else {
return
}

for domain in httpDomains {
guard let urlString = domain.url, var urlComponents = URLComponents(string: urlString) else { continue }
urlComponents.scheme = "https"
guard let httpsUrl = urlComponents.url?.absoluteString else { continue }
if let httpsDomain = Domain.first(where: NSPredicate(format: "url == %@", httpsUrl), context: backgroundContext) {
httpsDomain.shield_allOff = domain.shield_allOff
httpsDomain.shield_adblockAndTp = domain.shield_adblockAndTp
httpsDomain.shield_noScript = domain.shield_noScript
httpsDomain.shield_fpProtection = domain.shield_fpProtection
httpsDomain.shield_safeBrowsing = domain.shield_safeBrowsing
httpsDomain.shield_httpse = domain.shield_httpse
// Could call `domain.delete()` here (or add to batch to delete)
}
}
DataController.save(context: backgroundContext)
}
}
1 change: 0 additions & 1 deletion Shared/Extensions/URLExtensions.swift
Expand Up @@ -244,7 +244,6 @@ extension URL {
components.scheme = self.scheme
components.port = self.port
components.host = normalized
components.path = "/"
return components.url ?? self
}
return self
Expand Down