1 /*
2  * Copyright (C) 2023 The Android Open Source Project
3  *
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *      http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 package com.android.ondevicepersonalization.services.policyengine
18 
19 import android.adservices.ondevicepersonalization.UserData
20 import com.android.libraries.pcc.chronicle.api.ConnectionRequest
21 import com.android.libraries.pcc.chronicle.api.ProcessorNode
22 import com.android.libraries.pcc.chronicle.api.error.ChronicleError
23 import com.android.libraries.pcc.chronicle.util.MutableTypedMap
24 import com.android.libraries.pcc.chronicle.util.TypedMap
25 import com.android.ondevicepersonalization.internal.util.LoggerFactory
26 import com.android.ondevicepersonalization.services.policyengine.api.ChronicleManager
27 import com.android.ondevicepersonalization.services.policyengine.data.UserDataReader
28 import com.android.ondevicepersonalization.services.policyengine.policy.DataIngressPolicy
29 import com.android.ondevicepersonalization.services.policyengine.policy.rules.KidStatusEnabled
30 import com.android.ondevicepersonalization.services.policyengine.policy.rules.LimitedAdsTrackingEnabled
31 
32 class UserDataAccessor : ProcessorNode {
33     private val sLogger: LoggerFactory.Logger = LoggerFactory.getLogger()
34     private val TAG: String = "UserDataAccessor"
35     private var policyContext: MutableTypedMap = MutableTypedMap()
36 
37     override val requiredConnectionTypes = setOf(UserDataReader::class.java)
38 
39     private val chronicleManager: ChronicleManager = ChronicleManager.getInstance()
40 
41     init {
42         policyContext[KidStatusEnabled] = false
43         policyContext[LimitedAdsTrackingEnabled] = false
44 
45         chronicleManager.chronicle.updateConnectionContext(TypedMap(policyContext))
46         chronicleManager.failNewConnections(false)
47     }
48 
getUserDatanull49     fun getUserData(): UserData? {
50         try {
51             val userDataReader: UserDataReader? =
52                     chronicleManager.chronicle.getConnectionOrThrow(
53                             ConnectionRequest(UserDataReader::class.java, this,
54                                     DataIngressPolicy.NPA_DATA_POLICY)
55                     )
56             return userDataReader?.readUserData()
57         } catch (e: ChronicleError) {
58             sLogger.e(e, TAG + ": Expect success but connection failed with: ")
59             return null
60         }
61     }
62 
getUserDataWithAppInstallnull63     fun getUserDataWithAppInstall(): UserData? {
64         try {
65             val userDataReader: UserDataReader? =
66                 chronicleManager.chronicle.getConnectionOrThrow(
67                     ConnectionRequest(UserDataReader::class.java, this,
68                         DataIngressPolicy.NPA_DATA_POLICY)
69                 )
70             return userDataReader?.readUserDataWithAppInstall()
71         } catch (e: ChronicleError) {
72             sLogger.e(e, TAG + ": Expect success but connection failed with: ")
73             return null
74         }
75     }
76 }
77