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

Remote Settings - Playground #21813

Closed
Closed
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
3 changes: 3 additions & 0 deletions BrowserKit/Sources/Common/Logger/LoggerCategory.swift
Original file line number Diff line number Diff line change
Expand Up @@ -50,4 +50,7 @@ public enum LoggerCategory: String {

/// Multi-window management on iPad devices
case window

/// Remote settings
case remoteSettings
}
83 changes: 83 additions & 0 deletions firefox-ios/Client.xcodeproj/project.pbxproj

Large diffs are not rendered by default.

Original file line number Diff line number Diff line change
Expand Up @@ -164,4 +164,4 @@
}
],
"version" : 2
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,61 @@
// 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

enum RemoteDataTypeError: Error, LocalizedError {
case fileNotFound(fileName: String)
case decodingError(fileName: String, error: Error)

var errorDescription: String? {
switch self {
case .fileNotFound(let fileName):
return "The file '\(fileName)' was not found in the app bundle."
case .decodingError(let fileName, let error):
return "Failed to decode '\(fileName)' with \(error.localizedDescription)"
}
}
}

enum RemoteDataType: String, Codable {
case passwordRules

var type: RemoteDataTypeRecord.Type {
switch self {
case .passwordRules:
return PasswordRuleRecord.self
}
}

var fileName: String {
switch self {
case .passwordRules:
return "RemotePasswordRules"
}
}

var name: String {
switch self {
case .passwordRules:
return "Password Rules"
}
}

func loadLocalSettingsFromJSON<T: RemoteDataTypeRecord>() async throws -> T {
let fileName = self.fileName

guard let path = Bundle.main.path(forResource: fileName, ofType: "json") else {
throw RemoteDataTypeError.fileNotFound(fileName: fileName)
}

let url = URL(fileURLWithPath: path)
do {
let data = try Data(contentsOf: url)
let decodedObject = try JSONDecoder().decode(T.self, from: data)
return decodedObject
} catch {
throw RemoteDataTypeError.decodingError(fileName: fileName, error: error)
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
// 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

protocol RemoteDataTypeRecord: Codable {}

struct PasswordRuleRecord: RemoteDataTypeRecord {
let domain: String
let passwordRules: String
let id: String
let lastModified: Int

enum CodingKeys: String, CodingKey {
case domain = "Domain"
case passwordRules = "password-rules"
case id
case lastModified = "last_modified"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
// 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

struct RemoteSettingsFetchConfig: Codable {
let rules: [Rule]

struct Rule: Codable {
let name: String
let url: String
let file: String
let bucketID: String
let collectionsID: String

enum CodingKeys: String, CodingKey {
case name, url, file
case bucketID = "bucket_id"
case collectionsID = "collections_id"
}
}

/// Use this method to load all available rules from local fetch config JSON.
/// These rules can be used to know the corresponding bucket, collection and file
/// for the local remote settings object.
static func loadSettingsFetchConfig() -> RemoteSettingsFetchConfig? {
guard let path = Bundle.main.path(forResource: "RemoteSettingsFetchConfig",
ofType: "json") else { return nil }
let url = URL(fileURLWithPath: path)
do {
let data = try Data(contentsOf: url)
let config = try JSONDecoder().decode(RemoteSettingsFetchConfig.self, from: data)
return config
} catch {
return nil
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,38 @@
// 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 MozillaAppServices
import Common
import Shared

class RemoteSettingsUtilities {
private let logger: Logger

init(logger: Logger = DefaultLogger.shared) {
self.logger = logger
}

func fetchLocalRecord<T: RemoteDataTypeRecord>(for type: RemoteDataType) async -> T? {
do {
let record: T = try await type.loadLocalSettingsFromJSON()
logger.log("Successfully fetched record for \(type.name)",
level: .info,
category: .remoteSettings,
description: "Successfully fetched record for \(type.name).")
return record
} catch let error as RemoteDataTypeError {
logger.log("Failed to fetch local record for \(type.name)",
level: .warning,
category: .remoteSettings,
description: error.localizedDescription)
} catch {
logger.log("Unexpected error occurred while fetching local record for \(type.name)",
level: .warning,
category: .remoteSettings,
description: error.localizedDescription)
}
return nil
}
}
Loading