From 962f93cc7a6d11b653e7d46840acca3dc66e3b6b Mon Sep 17 00:00:00 2001 From: songjindian Date: Wed, 8 Mar 2023 10:27:21 +0800 Subject: [PATCH] =?UTF-8?q?AMS=E6=8F=90=E4=BE=9BAbilityThread=E5=88=9B?= =?UTF-8?q?=E5=BB=BA=E5=B7=A5=E5=8E=82=E7=B1=BB=E7=9A=84=E8=83=BD=E5=8A=9B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: songjindian --- services/abilitymgr_lite/BUILD.gn | 1 + .../include/ability_thread_loader.h | 54 +++++++++++++++++ .../include/slite_ability_thread.h | 41 +++++++++++++ .../src/ability_thread_loader.cpp | 60 +++++++++++++++++++ 4 files changed, 156 insertions(+) create mode 100644 services/abilitymgr_lite/include/ability_thread_loader.h create mode 100644 services/abilitymgr_lite/include/slite_ability_thread.h create mode 100644 services/abilitymgr_lite/src/ability_thread_loader.cpp diff --git a/services/abilitymgr_lite/BUILD.gn b/services/abilitymgr_lite/BUILD.gn index a8cf167..5a7db5c 100644 --- a/services/abilitymgr_lite/BUILD.gn +++ b/services/abilitymgr_lite/BUILD.gn @@ -25,6 +25,7 @@ lite_library("abilityms") { "src/ability_service.cpp", "src/ability_stack.cpp", "src/js_app_host.cpp", + "src/ability_thread_loader.cpp", ] if (defined(config_ohos_aafwk_ams_task_size) && 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 0000000..80a0d9b --- /dev/null +++ b/services/abilitymgr_lite/include/ability_thread_loader.h @@ -0,0 +1,54 @@ +/* + * 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 "slite_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 = SliteAbilityThread*(*)(); + +class AbilityThreadLoader { +public: + static AbilityThreadLoader &GetInstance() + { + static AbilityThreadLoader instance; + return instance; + } + + AbilityThreadLoader() = default; + + ~AbilityThreadLoader() = default; + + void SetCreatorFunc(AbilityThreadCreatorType type, AbilityThreadCreator creator); + + SliteAbilityThread *CreateAbilityThread(AbilityThreadCreatorType type) const; + + void unRegister(AbilityThreadCreatorType type); + +private: + AbilityThreadCreator jsAbilityThreadCreator_ = { nullptr }; + + AbilityThreadCreator nativeAbilityThreadCreator_ = { nullptr }; +}; +} // namespace OHOS +#endif //ABILITYLITE_ABILITY_THREAD_LOADER_H \ No newline at end of file diff --git a/services/abilitymgr_lite/include/slite_ability_thread.h b/services/abilitymgr_lite/include/slite_ability_thread.h new file mode 100644 index 0000000..e26f580 --- /dev/null +++ b/services/abilitymgr_lite/include/slite_ability_thread.h @@ -0,0 +1,41 @@ +/* + * 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_SLITE_ABILITY_THREAD_H +#define ABILITYLITE_SLITE_ABILITY_THREAD_H + +namespace OHOS { + +enum AbilityThreadCreatorType { + DEFAULT, + JS_CREATOR, + NATIVE_CREATOR, +}; + +class SliteAbilityThread { +public: + SliteAbilityThread() = default; + + virtual ~SliteAbilityThread() = default; + + virtual AbilityThreadCreatorType GetAbilityThreadType() = 0; + + virtual void SetAbilityThreadType(AbilityThreadCreatorType type) = 0; + +protected: + AbilityThreadCreatorType threadType_ = DEFAULT; +}; +} // namespace OHOS +#endif // ABILITYLITE_SLITE_ABILITY_THREAD_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 0000000..47b5572 --- /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; + } +} + +SliteAbilityThread *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 -- Gitee