1 /*
2 * Copyright (c) 2023 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 "napi_request_data_manager.h"
17
18 #include "security_guard_log.h"
19
20 namespace OHOS::Security::SecurityGuard {
21 // LCOV_EXCL_START
GetInstance()22 NapiRequestDataManager& NapiRequestDataManager::GetInstance()
23 {
24 static NapiRequestDataManager instance;
25 return instance;
26 }
27
GetContext(napi_env env)28 std::shared_ptr<RequestSecurityEventInfoContext> NapiRequestDataManager::GetContext(napi_env env)
29 {
30 bool isExist = false;
31 return GetContext(env, isExist);
32 }
33
GetContext(napi_env env,bool & isExist)34 std::shared_ptr<RequestSecurityEventInfoContext> NapiRequestDataManager::GetContext(napi_env env, bool &isExist)
35 {
36 std::lock_guard<std::mutex> lock(mutex_);
37 auto iter = envContextMap_.find(env);
38 if (iter != envContextMap_.end()) {
39 SGLOGI("find the env entry");
40 isExist = true;
41 return iter->second;
42 }
43
44 auto context = std::make_shared<RequestSecurityEventInfoContext>();
45 envContextMap_[env] = context;
46 isExist = false;
47 return context;
48 }
49
DeleteContext(napi_env env)50 void NapiRequestDataManager::DeleteContext(napi_env env)
51 {
52 SGLOGI("begin delete the env entry");
53 std::lock_guard<std::mutex> lock(mutex_);
54 auto iter = envContextMap_.find(env);
55 if (iter != envContextMap_.end()) {
56 if (iter->second != nullptr) {
57 napi_delete_reference(env, iter->second->endCallback);
58 iter->second->endCallback = nullptr;
59 napi_delete_reference(env, iter->second->dataCallback);
60 iter->second->dataCallback = nullptr;
61 napi_delete_reference(env, iter->second->errorCallback);
62 iter->second->errorCallback = nullptr;
63 napi_delete_reference(env, iter->second->ref);
64 iter->second->ref = nullptr;
65 }
66 envContextMap_.erase(iter);
67 }
68 }
69
AddDataCallback(napi_env env)70 uint32_t NapiRequestDataManager::AddDataCallback(napi_env env)
71 {
72 std::lock_guard<std::mutex> lock(envQuerierMutex_);
73 auto iter = envQuerierMap_.find(env);
74 if (iter != envQuerierMap_.end()) {
75 envQuerierMap_[env]++;
76 return iter->second;
77 }
78 uint32_t dataCallbackSize = 1;
79 envQuerierMap_[env] = dataCallbackSize;
80 return dataCallbackSize;
81 }
82
DelDataCallback(napi_env env)83 uint32_t NapiRequestDataManager::DelDataCallback(napi_env env)
84 {
85 std::lock_guard<std::mutex> lock(envQuerierMutex_);
86 auto iter = envQuerierMap_.find(env);
87 if (iter != envQuerierMap_.end()) {
88 envQuerierMap_.erase(iter);
89 }
90 return 0;
91 }
92
GetDataCallback(napi_env env)93 bool NapiRequestDataManager::GetDataCallback(napi_env env)
94 {
95 std::lock_guard<std::mutex> lock(envQuerierMutex_);
96 auto iter = envQuerierMap_.find(env);
97 if (iter != envQuerierMap_.end()) {
98 return true;
99 }
100 return false;
101 }
102 // LCOV_EXCL_STOP
103 } // OHOS::Security::SecurityGuard