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 PermissionStatusCache { 89 int32_t status; 90 std::string paramValue; 91 }; 92 93 struct PermissionParamCache { 94 long long sysCommitIdCache = PARAM_DEFAULT_VALUE; 95 int32_t commitIdCache = PARAM_DEFAULT_VALUE; 96 int32_t handle = PARAM_DEFAULT_VALUE; 97 std::string sysParamCache; 98 }; 99 100 class NapiAtManager { 101 public: 102 static napi_value Init(napi_env env, napi_value exports); 103 104 private: 105 static napi_value JsConstructor(napi_env env, napi_callback_info cbinfo); 106 static napi_value CreateAtManager(napi_env env, napi_callback_info cbInfo); 107 static napi_value VerifyAccessToken(napi_env env, napi_callback_info info); 108 static napi_value VerifyAccessTokenSync(napi_env env, napi_callback_info info); 109 static napi_value GrantUserGrantedPermission(napi_env env, napi_callback_info info); 110 static napi_value RevokeUserGrantedPermission(napi_env env, napi_callback_info info); 111 static napi_value CheckAccessToken(napi_env env, napi_callback_info info); 112 static napi_value GetPermissionFlags(napi_env env, napi_callback_info info); 113 static napi_value GetVersion(napi_env env, napi_callback_info info); 114 static napi_value SetPermissionRequestToggleStatus(napi_env env, napi_callback_info info); 115 static napi_value GetPermissionRequestToggleStatus(napi_env env, napi_callback_info info); 116 117 static bool ParseInputVerifyPermissionOrGetFlag(const napi_env env, const napi_callback_info info, 118 AtManagerAsyncContext& asyncContext); 119 static bool ParseInputSetToggleStatus(const napi_env env, const napi_callback_info info, 120 AtManagerAsyncContext& asyncContext); 121 static bool ParseInputGetToggleStatus(const napi_env env, const napi_callback_info info, 122 AtManagerAsyncContext& asyncContext); 123 static void VerifyAccessTokenExecute(napi_env env, void *data); 124 static void VerifyAccessTokenComplete(napi_env env, napi_status status, void *data); 125 static void CheckAccessTokenExecute(napi_env env, void* data); 126 static void CheckAccessTokenComplete(napi_env env, napi_status status, void* data); 127 static bool ParseInputGrantOrRevokePermission(const napi_env env, const napi_callback_info info, 128 AtManagerAsyncContext& asyncContext); 129 static void GrantUserGrantedPermissionExecute(napi_env env, void *data); 130 static void GrantUserGrantedPermissionComplete(napi_env env, napi_status status, void *data); 131 static void RevokeUserGrantedPermissionExecute(napi_env env, void *data); 132 static void RevokeUserGrantedPermissionComplete(napi_env env, napi_status status, void *data); 133 static void GetVersionExecute(napi_env env, void *data); 134 static void GetVersionComplete(napi_env env, napi_status status, void *data); 135 static void GetPermissionFlagsExecute(napi_env env, void *data); 136 static void GetPermissionFlagsComplete(napi_env env, napi_status status, void *data); 137 static void SetPermissionRequestToggleStatusExecute(napi_env env, void *data); 138 static void SetPermissionRequestToggleStatusComplete(napi_env env, napi_status status, void *data); 139 static void GetPermissionRequestToggleStatusExecute(napi_env env, void *data); 140 static void GetPermissionRequestToggleStatusComplete(napi_env env, napi_status status, void *data); 141 static void SetNamedProperty(napi_env env, napi_value dstObj, const int32_t objValue, const char *propName); 142 static void CreateObjects(napi_env env, napi_value exports); 143 static bool FillPermStateChangeInfo(const napi_env env, const napi_value* argv, const std::string& type, 144 const napi_value thisVar, RegisterPermStateChangeInfo& registerPermStateChangeInfo); 145 static bool ParseInputToRegister(const napi_env env, const napi_callback_info cbInfo, 146 RegisterPermStateChangeInfo& registerPermStateChangeInfo); 147 static napi_value RegisterPermStateChangeCallback(napi_env env, napi_callback_info cbInfo); 148 static bool IsExistRegister(const napi_env env, const RegisterPermStateChangeInfo* registerPermStateChangeInfo); 149 static bool ParseInputToUnregister(const napi_env env, napi_callback_info cbInfo, 150 UnregisterPermStateChangeInfo& unregisterPermStateChangeInfo); 151 static napi_value UnregisterPermStateChangeCallback(napi_env env, napi_callback_info cbInfo); 152 static bool FindAndGetSubscriberInVector(UnregisterPermStateChangeInfo* unregisterPermStateChangeInfo, 153 std::vector<RegisterPermStateChangeInfo*>& batchPermStateChangeRegisters, const napi_env env); 154 static void DeleteRegisterFromVector(const PermStateChangeScope& scopeInfo, const napi_env env, 155 napi_ref subscriberRef); 156 static std::string GetPermParamValue(); 157 static void UpdatePermissionCache(AtManagerAsyncContext* asyncContext); 158 }; 159 } // namespace AccessToken 160 } // namespace Security 161 } // namespace OHOS 162 /* 163 * function for module exports 164 */ 165 static napi_value Init(napi_env env, napi_value exports); 166 167 #endif /* INTERFACES_ACCESSTOKEN_KITS_NAPI_ATMANAGER_H */ 168