1 /* 2 * Copyright (c) 2021-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 #ifndef OS_ACCOUNT_INTERFACES_KITS_NAPI_OSACCOUNT_INCLUDE_NAPI_OS_ACCOUNT_H 17 #define OS_ACCOUNT_INTERFACES_KITS_NAPI_OSACCOUNT_INCLUDE_NAPI_OS_ACCOUNT_H 18 19 #include <uv.h> 20 #include <map> 21 #include <mutex> 22 #include <vector> 23 #include "os_account_info.h" 24 #include "os_account_manager.h" 25 #include "account_log_wrapper.h" 26 #include "napi/native_api.h" 27 #include "napi/native_node_api.h" 28 #include "napi_account_common.h" 29 30 namespace OHOS { 31 namespace AccountJsKit { 32 using namespace OHOS::AccountSA; 33 struct SubscribeCBInfo; 34 35 class SubscriberPtr : public OsAccountSubscriber { 36 public: 37 explicit SubscriberPtr(const OsAccountSubscribeInfo &subscribeInfo); 38 ~SubscriberPtr() override; 39 40 void OnAccountsChanged(const int &id) override; 41 42 void SetEnv(const napi_env &env); 43 void SetCallbackRef(const napi_ref &ref); 44 45 private: 46 napi_env env_ = nullptr; 47 napi_ref ref_ = nullptr; 48 }; 49 50 struct QueryOAByIdAsyncContext : public CommonAsyncContext { 51 int id = -1; 52 OsAccountInfo osAccountInfos; 53 }; 54 55 struct RemoveOAAsyncContext : public CommonAsyncContext { 56 int id = 0; 57 }; 58 59 struct SetOANameAsyncContext : public CommonAsyncContext { 60 int id = 0; 61 std::string name; 62 }; 63 64 struct SetOAConsAsyncContext : public CommonAsyncContext { 65 int id = 0; 66 bool enable = false; 67 std::vector<std::string> constraints; 68 }; 69 70 struct ActivateOAAsyncContext : public CommonAsyncContext { 71 int id = -1; 72 }; 73 74 struct CreateOAAsyncContext : public CommonAsyncContext { 75 OsAccountType type; 76 std::string name; 77 OsAccountInfo osAccountInfos; 78 }; 79 80 struct CreateOAForDomainAsyncContext : public CommonAsyncContext { 81 OsAccountType type; 82 DomainAccountInfo domainInfo; 83 OsAccountInfo osAccountInfos; 84 ThreadLockInfo *lockInfo; 85 }; 86 87 struct GetOACountAsyncContext : public CommonAsyncContext { 88 unsigned int osAccountsCount = 0; 89 }; 90 91 struct DbDeviceIdAsyncContext : public CommonAsyncContext { 92 std::string deviceId; 93 }; 94 95 struct GetAllConsAsyncContext : public CommonAsyncContext { 96 int id = 0; 97 std::vector<std::string> constraints; 98 }; 99 100 struct GetIdAsyncContext : public CommonAsyncContext { 101 int id = 0; 102 }; 103 104 struct QueryCreateOAAsyncContext : public CommonAsyncContext { 105 std::vector<OsAccountInfo> osAccountInfos; 106 }; 107 108 struct QueryOAConstraintSrcTypeContext : public CommonAsyncContext { 109 int32_t id = 0; 110 std::string constraint; 111 std::vector<ConstraintSourceTypeInfo> constraintSourceTypeInfos; 112 }; 113 114 struct QueryActiveIdsAsyncContext : public CommonAsyncContext { 115 std::vector<int> osAccountIds; 116 }; 117 118 struct GetOAPhotoAsyncContext : public CommonAsyncContext { 119 int id = 0; 120 std::string photo; 121 }; 122 123 struct CurrentOAAsyncContext : public CommonAsyncContext { 124 OsAccountInfo osAccountInfos; 125 }; 126 127 struct GetIdByUidAsyncContext : public CommonAsyncContext { 128 int id = 0; 129 int uid = 0; 130 }; 131 132 struct GetIdByDomainAsyncContext : public CommonAsyncContext { 133 int id = 0; 134 DomainAccountInfo domainInfo; 135 }; 136 137 struct SetOAPhotoAsyncContext : public CommonAsyncContext { 138 int id = 0; 139 std::string photo; 140 }; 141 142 struct QueryMaxNumAsyncContext : public CommonAsyncContext { 143 int maxOsAccountNumber = 0; 144 }; 145 146 struct IsActivedAsyncContext : public CommonAsyncContext { 147 int id = -1; 148 bool isOsAccountActived = false; 149 }; 150 151 struct IsConEnableAsyncContext : public CommonAsyncContext { 152 int id = 0; 153 std::string constraint; 154 bool isConsEnable = false; 155 }; 156 157 struct GetTypeAsyncContext : public CommonAsyncContext { 158 OsAccountType type; 159 }; 160 161 struct IsMultiEnAsyncContext : public CommonAsyncContext { 162 bool isMultiOAEnable = false; 163 }; 164 165 struct IsVerifiedAsyncContext : public CommonAsyncContext { 166 int id = -1; 167 bool isTestOA = false; 168 }; 169 170 struct GetSerialNumIdCBInfo : public CommonAsyncContext { 171 int id = 0; 172 int64_t serialNumber; 173 }; 174 175 struct GetSerialNumForOAInfo : public CommonAsyncContext { 176 int id = 0; 177 int64_t serialNum; 178 }; 179 180 struct IsTestOAInfo : public CommonAsyncContext { 181 bool isTestOsAccount = false; 182 }; 183 184 struct IsMainOAInfo : public CommonAsyncContext { 185 bool isMainOsAccount = false; 186 }; 187 188 struct SubscribeCBInfo : public CommonAsyncContext { SubscribeCBInfoSubscribeCBInfo189 SubscribeCBInfo(napi_env napiEnv) : CommonAsyncContext(napiEnv) {}; 190 OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::INVALID_TYPE; 191 std::string name; 192 OsAccountManager *osManager = nullptr; 193 std::shared_ptr<SubscriberPtr> subscriber = nullptr; 194 }; 195 196 struct SubscriberOAWorker : public CommonAsyncContext { 197 int id = 0; 198 napi_ref ref = nullptr; 199 SubscriberPtr *subscriber = nullptr; 200 }; 201 202 struct UnsubscribeCBInfo : public CommonAsyncContext { UnsubscribeCBInfoUnsubscribeCBInfo203 UnsubscribeCBInfo(napi_env napiEnv) : CommonAsyncContext(napiEnv){}; 204 OS_ACCOUNT_SUBSCRIBE_TYPE osSubscribeType = OS_ACCOUNT_SUBSCRIBE_TYPE::INVALID_TYPE; 205 std::string name; 206 OsAccountManager *osManager = nullptr; 207 std::vector<std::shared_ptr<SubscriberPtr>> subscribers; 208 }; 209 210 napi_value OsAccountInit(napi_env env, napi_value exports); 211 212 napi_value GetAccountManager(napi_env env, napi_callback_info cbInfo); 213 214 napi_value OsAccountJsConstructor(napi_env env, napi_callback_info cbinfo); 215 216 napi_value QueryOsAccountById(napi_env env, napi_callback_info cbInfo); 217 218 napi_value RemoveOsAccount(napi_env env, napi_callback_info cbInfo); 219 220 napi_value SetOsAccountName(napi_env env, napi_callback_info cbInfo); 221 222 napi_value SetOsAccountConstraints(napi_env env, napi_callback_info cbInfo); 223 224 napi_value ActivateOsAccount(napi_env env, napi_callback_info cbInfo); 225 226 napi_value CreateOsAccount(napi_env env, napi_callback_info cbInfo); 227 228 napi_value CreateOsAccountForDomain(napi_env env, napi_callback_info cbInfo); 229 230 napi_value GetOsAccountCount(napi_env env, napi_callback_info cbInfo); 231 232 napi_value GetCreatedOsAccountsCount(napi_env env, napi_callback_info cbInfo); 233 234 napi_value GetCreatedOsAccountsCountInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 235 236 napi_value QueryDistributedVirtualDeviceId(napi_env env, napi_callback_info cbInfo); 237 238 napi_value GetDistributedVirtualDeviceId(napi_env env, napi_callback_info cbInfo); 239 240 napi_value GetDistributedVirtualDeviceIdInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 241 242 napi_value GetOsAccountConstraints(napi_env env, napi_callback_info cbInfo); 243 244 napi_value GetOsAccountAllConstraints(napi_env env, napi_callback_info cbInfo); 245 246 napi_value GetOsAccountAllConstraintsInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 247 248 napi_value QueryOsAccountLocalIdFromProcess(napi_env env, napi_callback_info cbInfo); 249 250 napi_value GetOsAccountLocalIdFromProcess(napi_env env, napi_callback_info cbInfo); 251 252 napi_value GetOsAccountLocalIdFromProcessInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 253 254 napi_value QueryAllCreatedOsAccounts(napi_env env, napi_callback_info cbInfo); 255 256 napi_value GetActivatedOsAccountIds(napi_env env, napi_callback_info cbInfo); 257 258 napi_value QueryActivatedOsAccountIds(napi_env env, napi_callback_info cbInfo); 259 260 napi_value QueryActivatedOsAccountIdsInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 261 262 napi_value GetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo); 263 264 napi_value GetCurrentOsAccount(napi_env env, napi_callback_info cbInfo); 265 266 napi_value QueryCurrentOsAccount(napi_env env, napi_callback_info cbInfo); 267 268 napi_value QueryCurrentOsAccountInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 269 270 napi_value GetOsAccountLocalIdFromUid(napi_env env, napi_callback_info cbInfo); 271 272 napi_value GetOsAccountLocalIdForUid(napi_env env, napi_callback_info cbInfo); 273 274 napi_value GetOsAccountLocalIdForUidSync(napi_env env, napi_callback_info cbInfo); 275 276 napi_value GetOsAccountLocalIdFromUidInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 277 278 napi_value GetBundleIdFromUid(napi_env env, napi_callback_info cbInfo); 279 280 napi_value GetBundleIdForUidSync(napi_env env, napi_callback_info cbInfo); 281 282 napi_value GetOsAccountLocalIdFromDomain(napi_env env, napi_callback_info cbInfo); 283 284 napi_value QueryOsAccountLocalIdFromDomain(napi_env env, napi_callback_info cbInfo); 285 286 napi_value GetOsAccountLocalIdFromDomainInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 287 288 napi_value SetOsAccountProfilePhoto(napi_env env, napi_callback_info cbInfo); 289 290 napi_value QueryMaxOsAccountNumber(napi_env env, napi_callback_info cbInfo); 291 292 napi_value QueryOsAccountConstraintSourceTypes(napi_env env, napi_callback_info cbInfo); 293 294 napi_value IsOsAccountActived(napi_env env, napi_callback_info cbInfo); 295 296 napi_value CheckOsAccountActivated(napi_env env, napi_callback_info cbInfo); 297 298 napi_value IsOsAccountConstraintEnable(napi_env env, napi_callback_info cbInfo); 299 300 napi_value CheckConstraintEnabled(napi_env env, napi_callback_info cbInfo); 301 302 napi_value GetOsAccountType(napi_env env, napi_callback_info cbInfo); 303 304 napi_value GetOsAccountTypeFromProcess(napi_env env, napi_callback_info cbInfo); 305 306 napi_value GetOsAccountTypeFromProcessInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 307 308 napi_value IsMultiOsAccountEnable(napi_env env, napi_callback_info cbInfo); 309 310 napi_value CheckMultiOsAccountEnabled(napi_env env, napi_callback_info cbInfo); 311 312 napi_value InnerIsMultiOsAccountEnable(napi_env env, napi_callback_info cbInfo, bool throwErr); 313 314 napi_value IsOsAccountVerified(napi_env env, napi_callback_info cbInfo); 315 316 napi_value CheckOsAccountVerified(napi_env env, napi_callback_info cbInfo); 317 318 napi_value GetApplicationConstraints(napi_env env, napi_callback_info cbInfo); 319 320 napi_value QueryOsAccountLocalIdBySerialNumber(napi_env env, napi_callback_info cbInfo); 321 322 napi_value GetOsAccountLocalIdBySerialNumber(napi_env env, napi_callback_info cbInfo); 323 324 napi_value GetOsAccountLocalIdBySerialNumberInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 325 326 napi_value QuerySerialNumberByOsAccountLocalId(napi_env env, napi_callback_info cbInfo); 327 328 napi_value GetSerialNumberByOsAccountLocalId(napi_env env, napi_callback_info cbInfo); 329 330 napi_value GetSerialNumberByOsAccountLocalIdInner(napi_env env, napi_callback_info cbInfo, bool throwErr); 331 332 napi_value IsTestOsAccount(napi_env env, napi_callback_info cbInfo); 333 334 napi_value CheckOsAccountTestable(napi_env env, napi_callback_info cbInfo); 335 336 napi_value InnerIsTestOsAccount(napi_env env, napi_callback_info cbInfo, bool throwErr); 337 338 napi_value IsMainOsAccount(napi_env env, napi_callback_info cbInfo); 339 340 napi_value Subscribe(napi_env env, napi_callback_info cbInfo); 341 342 napi_value Unsubscribe(napi_env env, napi_callback_info cbInfo); 343 344 void UvQueueWorkOnAccountsChanged(uv_work_t *work, int status); 345 346 void UnsubscribeSync(napi_env env, UnsubscribeCBInfo *unsubscribeCBInfo); 347 348 void SetEnumProperty(napi_env env, napi_value dstObj, const int objValue, const char *propName); 349 } // namespace AccountJsKit 350 } // namespace OHOS 351 352 #endif // OS_ACCOUNT_INTERFACES_KITS_NAPI_OSACCOUNT_INCLUDE_NAPI_OS_ACCOUNT_H 353