-
Notifications
You must be signed in to change notification settings - Fork 25
/
BrowserCliqz.swift
76 lines (63 loc) · 2.14 KB
/
BrowserCliqz.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
//
// Copyright (c) 2017-2019 Cliqz GmbH. All rights reserved.
//
// 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 https://mozilla.org/MPL/2.0/.
//
import Foundation
import React
import Shared
@objc(BrowserCliqz)
class BrowserCliqz: RCTEventEmitter, NativeModuleBase {
private let PrefMapping = [
"toolkit.telemetry.enabled": AppConstants.PrefSendUsageData,
]
private let OtherPrefMapping = [
"distribution.version": { AppInfo.appVersion },
]
@objc(getPref:resolve:reject:)
public func getPref(key: NSString, resolve: @escaping RCTPromiseResolveBlock, reject: @escaping RCTPromiseRejectBlock) {
if let valueGetter = OtherPrefMapping[key as String] {
resolve(valueGetter())
return
}
guard let pref = PrefMapping[key as String] else {
resolve(nil)
return
}
guard pref != AppConstants.PrefSendUsageData || Features.Telemetry.isEnabled else {
resolve(nil)
return
}
self.withAppDelegate { appDel in
guard let profile = appDel.profile else {
resolve(nil)
return
}
resolve(profile.prefs.objectForKey(pref) as Any?)
}
}
@objc(addPrefListener:)
public func addPrefListener(key: NSString) {
guard let pref = PrefMapping[key as String] else { return }
self.withAppDelegate { appDel in
appDel.profile?.prefs.addListener(pref, callback: {
self.sendEvent(withName: "prefChange", body: key)
})
}
}
@objc(removePrefListener:)
public func removePrefListener(key: NSString) {
guard let pref = PrefMapping[key as String] else { return }
self.withAppDelegate { appDel in
appDel.profile?.prefs.removeListener(pref)
}
}
override static func requiresMainQueueSetup() -> Bool {
return false
}
override open func supportedEvents() -> [String]! {
return ["prefChange"]
}
}