• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 #include <mutex>
19 
20 #include "app_log_wrapper.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 namespace {
25 int32_t g_permJson = ERR_OK;
26 std::mutex g_mutex;
27 static const std::string PERMISSIONS_PROFILE_KEY_BUNDLENAME = "bundleName";
28 static const std::string PERMISSIONS_PROFILE_KEY_PERMISSIONS = "permissions";
29 static const std::string PERMISSIONS_PROFILE_KEY_NAME = "name";
30 static const std::string PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE = "userCancellable";
31 static const std::string PERMISSIONS_PROFILE_KEY_APP_SIGNATURE = "app_signature";
32 }
33 
from_json(const nlohmann::json & jsonObject,PermissionInfo & permissionInfo)34 void from_json(const nlohmann::json &jsonObject, PermissionInfo &permissionInfo)
35 {
36     const auto &jsonObjectEnd = jsonObject.end();
37     GetValueIfFindKey<std::string>(jsonObject,
38         jsonObjectEnd,
39         PERMISSIONS_PROFILE_KEY_NAME,
40         permissionInfo.name,
41         JsonType::STRING,
42         true,
43         g_permJson,
44         ArrayType::NOT_ARRAY);
45     GetValueIfFindKey<uint32_t>(jsonObject,
46         jsonObjectEnd,
47         PERMISSIONS_PROFILE_KEY_USER_CANCELLABLE,
48         permissionInfo.userCancellable,
49         JsonType::BOOLEAN,
50         true,
51         g_permJson,
52         ArrayType::NOT_ARRAY);
53 }
54 
TransformTo(const nlohmann::json & jsonObject,std::set<DefaultPermission> & defaultPermissions) const55 ErrCode DefaultPermissionProfile::TransformTo(const nlohmann::json &jsonObject,
56     std::set<DefaultPermission> &defaultPermissions) const
57 {
58     if (jsonObject.is_array() && !jsonObject.is_discarded()) {
59         std::lock_guard<std::mutex> lock(g_mutex);
60         g_permJson = ERR_OK;
61         for (const auto &object : jsonObject) {
62             if (!object.is_object()) {
63                 return ERR_APPEXECFWK_PARSE_PROFILE_PROP_TYPE_ERROR;
64             }
65             DefaultPermission defaultPermission;
66             const auto &objectEnd = object.end();
67             GetValueIfFindKey<std::string>(object,
68                 objectEnd,
69                 PERMISSIONS_PROFILE_KEY_BUNDLENAME,
70                 defaultPermission.bundleName,
71                 JsonType::STRING,
72                 true,
73                 g_permJson,
74                 ArrayType::NOT_ARRAY);
75             GetValueIfFindKey<std::vector<std::string>>(object,
76                 objectEnd,
77                 PERMISSIONS_PROFILE_KEY_APP_SIGNATURE,
78                 defaultPermission.appSignature,
79                 JsonType::ARRAY,
80                 false,
81                 g_permJson,
82                 ArrayType::STRING);
83             GetValueIfFindKey<std::vector<PermissionInfo>>(object,
84                 objectEnd,
85                 PERMISSIONS_PROFILE_KEY_PERMISSIONS,
86                 defaultPermission.grantPermission,
87                 JsonType::ARRAY,
88                 false,
89                 g_permJson,
90                 ArrayType::OBJECT);
91             if (g_permJson != ERR_OK) {
92                 APP_LOGE("g_permJson is %{public}d", g_permJson);
93                 int32_t ret = g_permJson;
94                 // need recover parse result to ERR_OK
95                 g_permJson = ERR_OK;
96                 return ret;
97             }
98 
99             auto iter = defaultPermissions.find(defaultPermission);
100             if (iter != defaultPermissions.end()) {
101                 APP_LOGD("Replace old defaultPermission(%{public}s)", defaultPermission.bundleName.c_str());
102                 defaultPermissions.erase(iter);
103             }
104 
105             defaultPermissions.insert(defaultPermission);
106         }
107     }
108     return ERR_OK;
109 }
110 }  // namespace AppExecFwk
111 }  // namespace OHOS
112 
113