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 "application_context_manager.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AbilityRuntime {
22 namespace {
HandleClean(void * data)23 void HandleClean(void *data)
24 {
25     EnvData* envData = static_cast<EnvData*>(data);
26     if (envData != nullptr) {
27         ApplicationContextManager::GetApplicationContextManager().RemoveGlobalObject(envData->env);
28         delete envData;
29         envData = nullptr;
30     }
31 }
32 }
ApplicationContextManager()33 ApplicationContextManager::ApplicationContextManager()
34 {}
35 
~ApplicationContextManager()36 ApplicationContextManager::~ApplicationContextManager()
37 {
38     std::lock_guard<std::mutex> lock(applicationContextMutex_);
39     for (auto &iter : applicationContextMap_) {
40         iter.second.reset();
41     }
42 }
43 
GetApplicationContextManager()44 ApplicationContextManager& ApplicationContextManager::GetApplicationContextManager()
45 {
46     static ApplicationContextManager applicationContextManager;
47     return applicationContextManager;
48 }
49 
AddGlobalObject(napi_env env,std::shared_ptr<NativeReference> applicationContextObj)50 void ApplicationContextManager::AddGlobalObject(napi_env env,
51     std::shared_ptr<NativeReference> applicationContextObj)
52 {
53     EnvData* envData = new (std::nothrow) EnvData(env);
54     if (envData == nullptr) {
55         return;
56     }
57     auto ret = napi_add_env_cleanup_hook(env, HandleClean, static_cast<void*>(envData));
58     if (ret != napi_status::napi_ok) {
59         TAG_LOGE(AAFwkTag::APPKIT, "add hook err");
60         return;
61     }
62     std::lock_guard<std::mutex> lock(applicationContextMutex_);
63     auto iter = applicationContextMap_.find(env);
64     if (iter == applicationContextMap_.end()) {
65         applicationContextMap_[env] = applicationContextObj;
66         return;
67     }
68     if (iter->second != nullptr) {
69         iter->second.reset();
70         iter->second = nullptr;
71     }
72     iter->second = applicationContextObj;
73 }
74 
GetGlobalObject(napi_env env)75 std::shared_ptr<NativeReference> ApplicationContextManager::GetGlobalObject(napi_env env)
76 {
77     std::lock_guard<std::mutex> lock(applicationContextMutex_);
78     return applicationContextMap_[env];
79 }
80 
RemoveGlobalObject(napi_env env)81 void ApplicationContextManager::RemoveGlobalObject(napi_env env)
82 {
83     std::lock_guard<std::mutex> lock(applicationContextMutex_);
84     auto iter = applicationContextMap_.find(env);
85     if (iter != applicationContextMap_.end() && iter->second != nullptr) {
86         iter->second.reset();
87         iter->second = nullptr;
88         applicationContextMap_.erase(env);
89     }
90 }
91 }  // namespace AbilityRuntime
92 }  // namespace OHOS
93