diff --git a/services/abilitymgr_lite/include/ability_thread.h b/services/abilitymgr_lite/include/ability_thread.h new file mode 100644 index 0000000000000000000000000000000000000000..4e8b472abdbe70c0f3ac12551e02c0291c211850 --- /dev/null +++ b/services/abilitymgr_lite/include/ability_thread.h @@ -0,0 +1,25 @@ +// +// Created by dian on 2023/3/9. +// + +#ifndef ABILITYLITE_ABILITY_THREAD_H +#define ABILITYLITE_ABILITY_THREAD_H + +#include + +namespace OHOS { +class AbilityThread { +public: + AbilityThread() = default; + + virtual ~AbilityThread() = default; + + virtual int GetAbilityThreadType() = 0; + + virtual void SetAbilityThreadType(int32_t type) = 0; + +protected: + int32_t threadType_ = 0; +}; +} // namespace OHOS +#endif // ABILITYLITE_ABILITY_THREAD_H \ No newline at end of file diff --git a/services/abilitymgr_lite/include/ability_thread_loader.h b/services/abilitymgr_lite/include/ability_thread_loader.h new file mode 100644 index 0000000000000000000000000000000000000000..2fbb1ee5f41bf06f9332eefa7cf058ccd2bedca6 --- /dev/null +++ b/services/abilitymgr_lite/include/ability_thread_loader.h @@ -0,0 +1,59 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef ABILITYLITE_ABILITY_THREAD_LOADER_H +#define ABILITYLITE_ABILITY_THREAD_LOADER_H + +#include "ability_thread.h" + +namespace OHOS { +/** + * @brief Method to create AbilityThread. + * + * This method can create an AbilityThread and return a pointer to the AbilityThread. + * + */ +using AbilityThreadCreator = AbilityThread*(*)(); + +enum AbilityThreadCreatorType { + JS_CREATOR, + NATIVE_CREATOR, +}; + +class AbilityThreadLoader { +public: + static AbilityThreadLoader &GetInstance() + { + static AbilityThreadLoader instance; + return instance; + } + + AbilityThreadLoader() = default; + + ~AbilityThreadLoader() = default; + + void SetCreatorFunc(AbilityThreadCreatorType type, AbilityThreadCreator creator); + + AbilityThread *CreateAbilityThread(AbilityThreadCreatorType type) const; + + void unRegister(AbilityThreadCreatorType type); + +private: + AbilityThreadCreator jsAbilityThreadCreator_ = { nullptr }; + + AbilityThreadCreator nativeAbilityThreadCreator_ = { nullptr }; +}; +} // namespace OHOS +#endif //ABILITYLITE_ABILITY_THREAD_LOAD_H \ No newline at end of file diff --git a/services/abilitymgr_lite/src/ability_thread_loader.cpp b/services/abilitymgr_lite/src/ability_thread_loader.cpp new file mode 100644 index 0000000000000000000000000000000000000000..2967427c1a4a196315b19e42934cbfd963424953 --- /dev/null +++ b/services/abilitymgr_lite/src/ability_thread_loader.cpp @@ -0,0 +1,60 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "ability_thread_loader.h" + +namespace OHOS { +void AbilityThreadLoader::SetCreatorFunc(AbilityThreadCreatorType type, AbilityThreadCreator creator) +{ + if (creator == nullptr) { + return; + } + switch (type) { + case AbilityThreadCreatorType::JS_CREATOR: + jsAbilityThreadCreator_ = creator; + break; + case AbilityThreadCreatorType::NATIVE_CREATOR: + nativeAbilityThreadCreator_ = creator; + break; + default: + break; + } +} + +AbilityThread *AbilityThreadLoader::CreateAbilityThread(AbilityThreadCreatorType type) const +{ + if (AbilityThreadCreatorType::JS_CREATOR == type && jsAbilityThreadCreator_ != nullptr) { + return jsAbilityThreadCreator_(); + } + if (AbilityThreadCreatorType::NATIVE_CREATOR == type && nativeAbilityThreadCreator_ != nullptr){ + return nativeAbilityThreadCreator_(); + } + return nullptr; +} + +void AbilityThreadLoader::unRegister(AbilityThreadCreatorType type) +{ + switch (type) { + case AbilityThreadCreatorType::JS_CREATOR: + jsAbilityThreadCreator_ = { nullptr }; + break; + case AbilityThreadCreatorType::NATIVE_CREATOR: + nativeAbilityThreadCreator_ = { nullptr }; + break; + default: + break; + } +} +} \ No newline at end of file