1 /* 2 * Copyright (c) 2022 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/common/utils/engine_helper.h" 17 18 #include "base/subwindow/subwindow_manager.h" 19 #include "core/common/container.h" 20 21 namespace OHOS::Ace { 22 std::shared_mutex EngineHelper::mutex_; 23 std::unordered_map<int32_t, WeakPtr<Framework::JsEngine>> EngineHelper::engineWeakMap_; 24 AddEngine(int32_t id,WeakPtr<Framework::JsEngine> engine)25void EngineHelper::AddEngine(int32_t id, WeakPtr<Framework::JsEngine> engine) 26 { 27 std::unique_lock<std::shared_mutex> lock(mutex_); 28 engineWeakMap_.emplace(id, engine); 29 } 30 GetEngine(int32_t id)31RefPtr<Framework::JsEngine> EngineHelper::GetEngine(int32_t id) 32 { 33 std::shared_lock<std::shared_mutex> lock(mutex_); 34 if (id >= MIN_SUBCONTAINER_ID) { 35 id = SubwindowManager::GetInstance()->GetParentContainerId(id); 36 } 37 auto iter = engineWeakMap_.find(id); 38 if (iter != engineWeakMap_.end()) { 39 return iter->second.Upgrade(); 40 } 41 return nullptr; 42 } 43 RemoveEngine(int32_t id)44void EngineHelper::RemoveEngine(int32_t id) 45 { 46 std::unique_lock<std::shared_mutex> lock(mutex_); 47 engineWeakMap_.erase(id); 48 } 49 GetCurrentEngine()50RefPtr<Framework::JsEngine> EngineHelper::GetCurrentEngine() 51 { 52 return GetEngine(Container::CurrentId()); 53 } 54 GetCurrentDelegate()55Framework::FrontendDelegate* EngineHelper::GetCurrentDelegate() 56 { 57 auto engine = GetCurrentEngine(); 58 if (engine) { 59 return engine->GetFrontend(); 60 } 61 return nullptr; 62 } 63 64 } // namespace OHOS::Ace