1 /*
<lambda>null2  * Copyright (C) 2022 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.server.permission.access.appop
18 
19 import android.util.Slog
20 import com.android.modules.utils.BinaryXmlPullParser
21 import com.android.modules.utils.BinaryXmlSerializer
22 import com.android.server.permission.access.AccessState
23 import com.android.server.permission.access.MutableAccessState
24 import com.android.server.permission.access.collection.* // ktlint-disable no-wildcard-imports
25 import com.android.server.permission.access.immutable.* // ktlint-disable no-wildcard-imports
26 import com.android.server.permission.access.util.attributeInt
27 import com.android.server.permission.access.util.attributeInterned
28 import com.android.server.permission.access.util.forEachTag
29 import com.android.server.permission.access.util.getAttributeIntOrThrow
30 import com.android.server.permission.access.util.getAttributeValueOrThrow
31 import com.android.server.permission.access.util.tag
32 import com.android.server.permission.access.util.tagName
33 
34 abstract class BaseAppOpPersistence {
35     abstract fun BinaryXmlPullParser.parseUserState(state: MutableAccessState, userId: Int)
36 
37     abstract fun BinaryXmlSerializer.serializeUserState(state: AccessState, userId: Int)
38 
39     protected fun BinaryXmlPullParser.parseAppOps(appOpModes: MutableIndexedMap<String, Int>) {
40         forEachTag {
41             when (tagName) {
42                 TAG_APP_OP -> parseAppOp(appOpModes)
43                 else -> Slog.w(LOG_TAG, "Ignoring unknown tag $name when parsing app-op state")
44             }
45         }
46     }
47 
48     private fun BinaryXmlPullParser.parseAppOp(appOpModes: MutableIndexedMap<String, Int>) {
49         val name = getAttributeValueOrThrow(ATTR_NAME).intern()
50         val mode = getAttributeIntOrThrow(ATTR_MODE)
51         appOpModes[name] = mode
52     }
53 
54     protected fun BinaryXmlSerializer.serializeAppOps(appOpModes: IndexedMap<String, Int>) {
55         appOpModes.forEachIndexed { _, name, mode -> serializeAppOp(name, mode) }
56     }
57 
58     private fun BinaryXmlSerializer.serializeAppOp(name: String, mode: Int) {
59         tag(TAG_APP_OP) {
60             attributeInterned(ATTR_NAME, name)
61             attributeInt(ATTR_MODE, mode)
62         }
63     }
64 
65     companion object {
66         private val LOG_TAG = BaseAppOpPersistence::class.java.simpleName
67 
68         private const val TAG_APP_OP = "app-op"
69 
70         private const val ATTR_MODE = "mode"
71         private const val ATTR_NAME = "name"
72     }
73 }
74