1 /*
2 * Copyright (c) 2025 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 "frameworks/bridge/arkts_frontend/ani_context_module.h"
17
18 #include <ani.h>
19
20 #include <mutex>
21
22 #include "base/log/log.h"
23 namespace OHOS::Ace::Framework {
24
25 std::unordered_map<int32_t, ani_ref*> AniContextModule::aniContexts_;
26 std::shared_mutex AniContextModule::aniContextsMutex_;
27
GetInstance()28 AniContextModule* AniContextModule::GetInstance()
29 {
30 static AniContextModule instance;
31 return &instance;
32 }
33
GetAniContext(int32_t key)34 ani_ref* AniContextModule::GetAniContext(int32_t key)
35 {
36 std::shared_lock<std::shared_mutex> lock(aniContextsMutex_);
37 auto it1 = aniContexts_.find(key);
38 if (it1 != aniContexts_.end()) {
39 return it1->second;
40 }
41 return nullptr;
42 }
43
AddAniContext(int32_t key,ani_ref * value)44 void AniContextModule::AddAniContext(int32_t key, ani_ref* value)
45 {
46 std::unique_lock<std::shared_mutex> lock(aniContextsMutex_);
47 if (aniContexts_.find(key) != aniContexts_.end()) {
48 LOGW("AniContext exists for key %d", key);
49 return;
50 }
51 aniContexts_.emplace(key, value);
52 }
53
RemoveAniContext(int32_t key)54 void AniContextModule::RemoveAniContext(int32_t key)
55 {
56 std::unique_lock<std::shared_mutex> lock(aniContextsMutex_);
57 auto it = aniContexts_.find(key);
58 if (it != aniContexts_.end()) {
59 aniContexts_.erase(it);
60 }
61 }
62 } // namespace OHOS::Ace::Framework
63