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 constexpr const char* PERMISSIONS_PROFILE_KEY_BUNDLENAME = "bundleName";
24 static constexpr const char* PERMISSIONS_PROFILE_KEY_PERMISSIONS = "permissions";
25 static constexpr const char* PERMISSIONS_PROFILE_KEY_NAME = "name";
26 static constexpr const char* PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE = "userCancellable";
27 static constexpr const char* 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     BMSJsonUtil::GetStrValueIfFindKey(jsonObject,
34         jsonObjectEnd,
35         PERMISSIONS_PROFILE_KEY_NAME,
36         permissionInfo.name,
37         true,
38         g_permJson);
39     BMSJsonUtil::GetBoolValueIfFindKey(jsonObject,
40         jsonObjectEnd,
41         PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE,
42         permissionInfo.userCancellable,
43         true,
44         g_permJson);
45 }
46 
TransformTo(const nlohmann::json &jsonObject, std::set<DefaultPermission> &defaultPermissions) const47 ErrCode DefaultPermissionProfile::TransformTo(const nlohmann::json &jsonObject,
48     std::set<DefaultPermission> &defaultPermissions) const
49 {
50     ErrCode result = ERR_OK;
51     if (jsonObject.is_array() && !jsonObject.is_discarded()) {
52         std::lock_guard<std::mutex> lock(g_mutex);
53         g_permJson = ERR_OK;
54         for (const auto &object : jsonObject) {
55             if (!object.is_object()) {
56                 return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
57             }
58             DefaultPermission defaultPermission;
59             const auto &objectEnd = object.end();
60             BMSJsonUtil::GetStrValueIfFindKey(object, objectEnd,
61                 PERMISSIONS_PROFILE_KEY_BUNDLENAME,
62                 defaultPermission.bundleName,
63                 true, g_permJson);
64 
65             GetValueIfFindKey<std::vector<std::string>>(object, objectEnd,
66                 PERMISSIONS_PROFILE_KEY_APP_SIGNATURE,
67                 defaultPermission.appSignature,
68                 JsonType::ARRAY,
69                 false, g_permJson, ArrayType::STRING);
70 
71             GetValueIfFindKey<std::vector<PermissionInfo>>(object, objectEnd,
72                 PERMISSIONS_PROFILE_KEY_PERMISSIONS,
73                 defaultPermission.grantPermission,
74                 JsonType::ARRAY,
75                 false, g_permJson, ArrayType::OBJECT);
76 
77             if (g_permJson != ERR_OK) {
78                 APP_LOGE("parse install_list_permissions.json failed, g_permJson is %{public}d, bundleName:%{public}s",
79                     g_permJson, defaultPermission.bundleName.c_str());
80                 result = g_permJson;
81                 // need recover parse result to ERR_OK
82                 g_permJson = ERR_OK;
83                 continue;
84             }
85 
86             auto iter = defaultPermissions.find(defaultPermission);
87             if (iter != defaultPermissions.end()) {
88                 APP_LOGD("Replace old defaultPermission(%{public}s)", defaultPermission.bundleName.c_str());
89                 defaultPermissions.erase(iter);
90             }
91 
92             defaultPermissions.insert(defaultPermission);
93         }
94     }
95     return result;
96 }
97 }  // namespace AppExecFwk
98 }  // namespace OHOS
99 
100