1 /*
2  * Copyright (c) 2024 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 "bridge/declarative_frontend/engine/jsi/nativeModule/ui_context_helper.h"
17 #include "native_engine.h"
18 
19 #include "bridge/common/utils/engine_helper.h"
20 #include "bridge/declarative_frontend/engine/jsi/nativeModule/arkts_utils.h"
21 
22 namespace OHOS::Ace::NG {
23 std::unordered_map<int32_t, panda::Global<panda::JSValueRef>> UIContextHelper::uiContextMap_;
24 std::shared_mutex UIContextHelper::uiContextMapMutex_;
25 
AddUIContext(EcmaVM * vm,int32_t instanceId,panda::Local<panda::JSValueRef> uiContext)26 void UIContextHelper::AddUIContext(EcmaVM* vm, int32_t instanceId, panda::Local<panda::JSValueRef> uiContext)
27 {
28     std::unique_lock<std::shared_mutex> lock(uiContextMapMutex_);
29     if (!uiContext->IsUndefined()) {
30         auto uiContextGlobal = panda::Global<panda::JSValueRef>(vm, uiContext);
31         uiContextMap_.emplace(instanceId, uiContextGlobal);
32     }
33 }
34 
RemoveUIContext(int32_t instanceId)35 void UIContextHelper::RemoveUIContext(int32_t instanceId)
36 {
37     std::unique_lock<std::shared_mutex> lock(uiContextMapMutex_);
38     uiContextMap_.erase(instanceId);
39 }
40 
GetUIContext(EcmaVM * vm,int32_t instanceId)41 panda::Local<panda::JSValueRef> UIContextHelper::GetUIContext(EcmaVM* vm, int32_t instanceId)
42 {
43     std::shared_lock<std::shared_mutex> lock(uiContextMapMutex_);
44     auto iter = uiContextMap_.find(instanceId);
45     if (iter == uiContextMap_.end()) {
46         ContainerScope scope(instanceId);
47         lock.unlock();
48         auto uiContext = ArkTSUtils::GetContext(vm);
49         AddUIContext(vm, instanceId, uiContext);
50         return uiContext;
51     }
52     auto uiContext = iter->second;
53     return uiContext.ToLocal();
54 }
55 
HasUIContext(int32_t instanceId)56 bool UIContextHelper::HasUIContext(int32_t instanceId)
57 {
58     std::shared_lock<std::shared_mutex> lock(uiContextMapMutex_);
59     auto iter = uiContextMap_.find(instanceId);
60     return iter != uiContextMap_.end();
61 }
62 
RegisterRemoveUIContextFunc()63 void UIContextHelper::RegisterRemoveUIContextFunc()
64 {
65     static std::once_flag onceFlag;
66     std::call_once(onceFlag, []() -> void {
67         std::function<void(int32_t)> removeUIContextFunc = UIContextHelper::RemoveUIContext;
68         EngineHelper::RegisterRemoveUIContextFunc(removeUIContextFunc);
69     });
70 }
71 } // namespace OHOS::Ace::NG