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 #ifndef FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_ENGINE_HELPER_H 17 #define FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_ENGINE_HELPER_H 18 19 #include <mutex> 20 #include <shared_mutex> 21 22 #include "base/memory/referenced.h" 23 #include "base/utils/macros.h" 24 #include "base/utils/noncopyable.h" 25 #include "bridge/js_frontend/engine/common/js_engine.h" 26 #include "bridge/js_frontend/frontend_delegate.h" 27 28 namespace OHOS::Ace { 29 30 class ACE_EXPORT_WITH_PREVIEW EngineHelper final { 31 public: 32 static void AddEngine(int32_t id, WeakPtr<Framework::JsEngine> engine); 33 34 static RefPtr<Framework::JsEngine> GetEngine(int32_t id); 35 36 static void RemoveEngine(int32_t id); 37 38 static RefPtr<Framework::JsEngine> GetCurrentEngine(); 39 40 static Framework::FrontendDelegate* GetCurrentDelegate(); 41 42 private: 43 static std::unordered_map<int32_t, WeakPtr<Framework::JsEngine>> engineWeakMap_; 44 45 static std::shared_mutex mutex_; 46 47 ACE_DISALLOW_COPY_AND_MOVE(EngineHelper); 48 }; 49 50 } // namespace OHOS::Ace 51 52 #endif // FOUNDATION_ACE_FRAMEWORKS_BRIDGE_COMMON_UTILS_ENGINE_HELPER_H 53