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