1 /* 2 * Copyright (c) 2021-2024 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 #ifndef INTERFACES_ACCESSTOKEN_KITS_NAPI_ATMANAGER_H 16 #define INTERFACES_ACCESSTOKEN_KITS_NAPI_ATMANAGER_H 17 18 #include "accesstoken_kit.h" 19 #include "napi_error.h" 20 #include "napi_context_common.h" 21 22 namespace OHOS { 23 namespace Security { 24 namespace AccessToken { 25 const int32_t PARAM_DEFAULT_VALUE = -1; 26 27 enum PermissionStateChangeType { 28 PERMISSION_REVOKED_OPER = 0, 29 PERMISSION_GRANTED_OPER = 1, 30 }; 31 32 static thread_local napi_ref g_atManagerRef_; 33 const std::string ATMANAGER_CLASS_NAME = "atManager"; 34 class RegisterPermStateChangeScopePtr : public std::enable_shared_from_this<RegisterPermStateChangeScopePtr>, 35 public PermStateChangeCallbackCustomize { 36 public: 37 explicit RegisterPermStateChangeScopePtr(const PermStateChangeScope& subscribeInfo); 38 ~RegisterPermStateChangeScopePtr() override; 39 void PermStateChangeCallback(PermStateChangeInfo& result) override; 40 void SetEnv(const napi_env& env); 41 void SetCallbackRef(const napi_ref& ref); 42 void SetValid(bool valid); 43 void DeleteNapiRef(); 44 private: 45 napi_env env_ = nullptr; 46 napi_ref ref_ = nullptr; 47 bool valid_ = true; 48 std::mutex validMutex_; 49 }; 50 51 struct RegisterPermStateChangeWorker { 52 napi_env env = nullptr; 53 napi_ref ref = nullptr; 54 PermStateChangeInfo result; 55 std::shared_ptr<RegisterPermStateChangeScopePtr> subscriber = nullptr; 56 }; 57 58 struct PermStateChangeContext { 59 virtual ~PermStateChangeContext(); 60 napi_env env = nullptr; 61 napi_ref callbackRef = nullptr; 62 int32_t errCode = RET_SUCCESS; 63 std::string permStateChangeType; 64 AccessTokenKit* accessTokenKit = nullptr; 65 std::thread::id threadId_; 66 std::shared_ptr<RegisterPermStateChangeScopePtr> subscriber = nullptr; 67 }; 68 69 typedef PermStateChangeContext RegisterPermStateChangeInfo; 70 71 struct UnregisterPermStateChangeInfo : public PermStateChangeContext { 72 PermStateChangeScope scopeInfo; 73 }; 74 75 struct AtManagerAsyncContext : public AtManagerAsyncWorkData { AtManagerAsyncContextAtManagerAsyncContext76 explicit AtManagerAsyncContext(napi_env env) : AtManagerAsyncWorkData(env) {} 77 78 AccessTokenID tokenId = 0; 79 std::string permissionName; 80 union { 81 uint32_t flag = 0; 82 uint32_t status; 83 }; 84 int32_t result = RET_FAILED; 85 int32_t errorCode = 0; 86 }; 87 88 struct AtManagerSyncContext { AtManagerSyncContextAtManagerSyncContext89 explicit AtManagerSyncContext() {} 90 91 napi_env env = nullptr; 92 AccessTokenID tokenId = 0; 93 std::string permissionName; 94 int32_t result = RET_FAILED; 95 int32_t errorCode = 0; 96 }; 97 98 struct PermissionStatusCache { 99 int32_t status; 100 std::string paramValue; 101 }; 102 103 struct PermissionParamCache { 104 long long sysCommitIdCache = PARAM_DEFAULT_VALUE; 105 int32_t commitIdCache = PARAM_DEFAULT_VALUE; 106 int32_t handle = PARAM_DEFAULT_VALUE; 107 std::string sysParamCache; 108 }; 109 110 class NapiAtManager { 111 public: 112 static napi_value Init(napi_env env, napi_value exports); 113 114 private: 115 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 116 static napi_value CreateAtManager(napi_env env, napi_callback_info cbInfo); 117 static napi_value VerifyAccessToken(napi_env env, napi_callback_info info); 118 static napi_value VerifyAccessTokenSync(napi_env env, napi_callback_info info); 119 static napi_value GrantUserGrantedPermission(napi_env env, napi_callback_info info); 120 static napi_value RevokeUserGrantedPermission(napi_env env, napi_callback_info info); 121 static napi_value CheckAccessToken(napi_env env, napi_callback_info info); 122 static napi_value GetPermissionFlags(napi_env env, napi_callback_info info); 123 static napi_value GetVersion(napi_env env, napi_callback_info info); 124 static napi_value SetPermissionRequestToggleStatus(napi_env env, napi_callback_info info); 125 static napi_value GetPermissionRequestToggleStatus(napi_env env, napi_callback_info info); 126 static napi_value RequestAppPermOnSetting(napi_env env, napi_callback_info info); 127 128 static bool ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, 129 AtManagerAsyncContext& asyncContext); 130 static bool ParseInputVerifyPermissionSync(const napi_env env, const napi_callback_info info, 131 AtManagerSyncContext& syncContext); 132 static bool ParseInputSetToggleStatus(const napi_env env, const napi_callback_info info, 133 AtManagerAsyncContext& asyncContext); 134 static bool ParseInputGetToggleStatus(const napi_env env, const napi_callback_info info, 135 AtManagerAsyncContext& asyncContext); 136 static void VerifyAccessTokenExecute(napi_env env, void *data); 137 static void VerifyAccessTokenComplete(napi_env env, napi_status status, void *data); 138 static void CheckAccessTokenExecute(napi_env env, void* data); 139 static void CheckAccessTokenComplete(napi_env env, napi_status status, void* data); 140 static bool ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, 141 AtManagerAsyncContext& asyncContext); 142 static void GrantUserGrantedPermissionExecute(napi_env env, void *data); 143 static void GrantUserGrantedPermissionComplete(napi_env env, napi_status status, void *data); 144 static void RevokeUserGrantedPermissionExecute(napi_env env, void *data); 145 static void RevokeUserGrantedPermissionComplete(napi_env env, napi_status status, void *data); 146 static void GetVersionExecute(napi_env env, void *data); 147 static void GetVersionComplete(napi_env env, napi_status status, void *data); 148 static void GetPermissionFlagsExecute(napi_env env, void *data); 149 static void GetPermissionFlagsComplete(napi_env env, napi_status status, void *data); 150 static void SetPermissionRequestToggleStatusExecute(napi_env env, void *data); 151 static void SetPermissionRequestToggleStatusComplete(napi_env env, napi_status status, void *data); 152 static void GetPermissionRequestToggleStatusExecute(napi_env env, void *data); 153 static void GetPermissionRequestToggleStatusComplete(napi_env env, napi_status status, void *data); 154 static void RequestAppPermOnSettingExecute(napi_env env, void *data); 155 static void RequestAppPermOnSettingComplete(napi_env env, napi_status status, void *data); 156 static void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName); 157 static void CreateObjects(napi_env env, napi_value exports); 158 static bool GetPermStateChangeType(const napi_env env, const size_t argc, const napi_value* argv, 159 std::string& type); 160 static bool FillPermStateChangeInfo(const napi_env env, const napi_value* argv, const std::string& type, 161 const napi_value thisVar, RegisterPermStateChangeInfo& registerPermStateChangeInfo); 162 static bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, 163 RegisterPermStateChangeInfo& registerPermStateChangeInfo); 164 static napi_value RegisterPermStateChangeCallback(napi_env env, napi_callback_info cbInfo); 165 static bool IsExistRegister(const napi_env env, const RegisterPermStateChangeInfo* registerPermStateChangeInfo); 166 static bool FillPermStateChangeScope(const napi_env env, const napi_value* argv, 167 const std::string& type, PermStateChangeScope& scopeInfo); 168 static bool ParseInputToUnregister(const napi_env env, napi_callback_info cbInfo, 169 UnregisterPermStateChangeInfo& unregisterPermStateChangeInfo); 170 static napi_value UnregisterPermStateChangeCallback(napi_env env, napi_callback_info cbInfo); 171 static bool FindAndGetSubscriberInVector(UnregisterPermStateChangeInfo* unregisterPermStateChangeInfo, 172 std::vector<RegisterPermStateChangeInfo*>& batchPermStateChangeRegisters, const napi_env env); 173 static void DeleteRegisterFromVector(const PermStateChangeScope& scopeInfo, const napi_env env, 174 napi_ref subscriberRef); 175 static std::string GetPermParamValue(); 176 static void UpdatePermissionCache(AtManagerSyncContext* syncContext); 177 }; 178 } // namespace AccessToken 179 } // namespace Security 180 } // namespace OHOS 181 /* 182 * function for module exports 183 */ 184 static napi_value Init(napi_env env, napi_value exports); 185 186 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_ATMANAGER_H */ 187