1 /*
2  * Copyright (c) 2022 Huawei Device Co., Ltd.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at
6  *
7  *     http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include "default_permission_profile.h"
17 
18 namespace OHOS {
19 namespace AppExecFwk {
20 namespace {
21 int32_t g_permJson = ERR_OK;
22 std::mutex g_mutex;
23 static const std::string PERMISSIONS_PROFILE_KEY_BUNDLENAME = "bundleName";
24 static const std::string PERMISSIONS_PROFILE_KEY_PERMISSIONS = "permissions";
25 static const std::string PERMISSIONS_PROFILE_KEY_NAME = "name";
26 static const std::string PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE = "userCancellable";
27 static const std::string PERMISSIONS_PROFILE_KEY_APP_SIGNATURE = "app_signature";
28 }
29 
from_json(const nlohmann::json & jsonObject,PermissionInfo & permissionInfo)30 void from_json(const nlohmann::json &jsonObject, PermissionInfo &permissionInfo)
31 {
32     const auto &jsonObjectEnd = jsonObject.end();
33     GetValueIfFindKey<std::string>(jsonObject,
34         jsonObjectEnd,
35         PERMISSIONS_PROFILE_KEY_NAME,
36         permissionInfo.name,
37         JsonType::STRING,
38         true,
39         g_permJson,
40         ArrayType::NOT_ARRAY);
41     GetValueIfFindKey<bool>(jsonObject,
42         jsonObjectEnd,
43         PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE,
44         permissionInfo.userCancellable,
45         JsonType::BOOLEAN,
46         true,
47         g_permJson,
48         ArrayType::NOT_ARRAY);
49 }
50 
TransformTo(const nlohmann::json & jsonObject,std::set<DefaultPermission> & defaultPermissions) const51 ErrCode DefaultPermissionProfile::TransformTo(const nlohmann::json &jsonObject,
52     std::set<DefaultPermission> &defaultPermissions) const
53 {
54     ErrCode result = ERR_OK;
55     if (jsonObject.is_array() && !jsonObject.is_discarded()) {
56         std::lock_guard<std::mutex> lock(g_mutex);
57         g_permJson = ERR_OK;
58         for (const auto &object : jsonObject) {
59             if (!object.is_object()) {
60                 return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
61             }
62             DefaultPermission defaultPermission;
63             const auto &objectEnd = object.end();
64             GetValueIfFindKey<std::string>(object, objectEnd,
65                 PERMISSIONS_PROFILE_KEY_BUNDLENAME,
66                 defaultPermission.bundleName,
67                 JsonType::STRING,
68                 true, g_permJson, ArrayType::NOT_ARRAY);
69 
70             GetValueIfFindKey<std::vector<std::string>>(object, objectEnd,
71                 PERMISSIONS_PROFILE_KEY_APP_SIGNATURE,
72                 defaultPermission.appSignature,
73                 JsonType::ARRAY,
74                 false, g_permJson, ArrayType::STRING);
75 
76             GetValueIfFindKey<std::vector<PermissionInfo>>(object, objectEnd,
77                 PERMISSIONS_PROFILE_KEY_PERMISSIONS,
78                 defaultPermission.grantPermission,
79                 JsonType::ARRAY,
80                 false, g_permJson, ArrayType::OBJECT);
81 
82             if (g_permJson != ERR_OK) {
83                 APP_LOGE("parse install_list_permissions.json failed, g_permJson is %{public}d, bundleName:%{public}s",
84                     g_permJson, defaultPermission.bundleName.c_str());
85                 result = g_permJson;
86                 // need recover parse result to ERR_OK
87                 g_permJson = ERR_OK;
88                 continue;
89             }
90 
91             auto iter = defaultPermissions.find(defaultPermission);
92             if (iter != defaultPermissions.end()) {
93                 APP_LOGD("Replace old defaultPermission(%{public}s)", defaultPermission.bundleName.c_str());
94                 defaultPermissions.erase(iter);
95             }
96 
97             defaultPermissions.insert(defaultPermission);
98         }
99     }
100     return result;
101 }
102 }  // namespace AppExecFwk
103 }  // namespace OHOS
104 
105