diff --git a/frameworks/native/source/system_ability_load_callback_stub.cpp b/frameworks/native/source/system_ability_load_callback_stub.cpp new file mode 100644 index 0000000000000000000000000000000000000000..407e6404b91c89c113456abc5a9d7932dcfd51f4 --- /dev/null +++ b/frameworks/native/source/system_ability_load_callback_stub.cpp @@ -0,0 +1,62 @@ +/* + * Copyright (c) 2022 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 "system_ability_load_callback_stub.h" + +#include "ipc_skeleton.h" +#include "sam_log.h" +#include "system_ability_definition.h" + +namespace OHOS { +int32_t SystemAbilityLoadCallbackStub::OnRemoteRequest(uint32_t code, + MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + HILOGI("SystemAbilityLoadCallbackStub::OnRemoteRequest, code = %{public}d", code); + if (!EnforceInterceToken(data)) { + HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest check interface token failed!"); + return ERR_PERMISSION_DENIED; + } + switch (code) { + case ON_LOAD_SYSTEM_ABILITY_SUCCESS: + return OnLoadSystemAbilitySuccessInner(data, reply); + default: + HILOGW("SystemAbilityLoadCallbackStub::OnRemoteRequest unknown request code!"); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t SystemAbilityLoadCallbackStub::OnLoadSystemAbilitySuccessInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t systemAbilityId = data.ReadInt32(); + if (!CheckInputSystemAbilityId(systemAbilityId)) { + HILOGW("OnLoadSystemAbilitySuccessInner invalid systemAbilityId:%{public}d !", systemAbilityId); + return ERR_INVALID_VALUE; + } + sptr remoteObject = data.ReadRemoteObject(); + OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject); + return ERR_NONE; +} + +bool SystemAbilityLoadCallbackStub::CheckInputSystemAbilityId(int32_t systemAbilityId) +{ + return (systemAbilityId >= FIRST_SYS_ABILITY_ID) && (systemAbilityId <= LAST_SYS_ABILITY_ID); +} + +bool SystemAbilityLoadCallbackStub::EnforceInterceToken(MessageParcel& data) +{ + std::u16string interfaceToken = data.ReadInterfaceToken(); + return interfaceToken == GetDescriptor(); +} +} diff --git a/frameworks/native/source/system_ability_manager_proxy.cpp b/frameworks/native/source/system_ability_manager_proxy.cpp index 8e4b0578b008ef06311cd732f19e4f4862ed5368..b1549871aeda914ece6601388c47de0bb6fe09d9 100755 --- a/frameworks/native/source/system_ability_manager_proxy.cpp +++ b/frameworks/native/source/system_ability_manager_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -430,6 +430,54 @@ int32_t SystemAbilityManagerProxy::UnSubscribeSystemAbility(int32_t systemAbilit return result; } +int32_t SystemAbilityManagerProxy::LoadSystemAbility(int32_t systemAbilityId, + const sptr& callback) +{ + if (!CheckInputSysAbilityId(systemAbilityId) || callback == nullptr) { + HILOGE("LoadSystemAbility systemAbilityId:%{public}d or callback invalid!", systemAbilityId); + return ERR_INVALID_VALUE; + } + + HILOGI("LoadSystemAbility systemAbilityId:%{public}d", systemAbilityId); + sptr remote = Remote(); + if (remote == nullptr) { + HILOGE("LoadSystemAbility remote is null!"); + return ERR_INVALID_OPERATION; + } + + MessageParcel data; + if (!data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN)) { + HILOGW("LoadSystemAbility Write interface token failed!"); + return ERR_FLATTEN_OBJECT; + } + bool ret = data.WriteInt32(systemAbilityId); + if (!ret) { + HILOGW("LoadSystemAbility Write systemAbilityId failed!"); + return ERR_FLATTEN_OBJECT; + } + ret = data.WriteRemoteObject(callback->AsObject()); + if (!ret) { + HILOGW("LoadSystemAbility Write callback failed!"); + return ERR_FLATTEN_OBJECT; + } + + MessageParcel reply; + MessageOption option; + int32_t err = remote->SendRequest(LOAD_SYSTEM_ABILITY_TRANSACTION, data, reply, option); + if (err != ERR_NONE) { + HILOGE("LoadSystemAbility SendRequest error:%{public}d!", err); + return err; + } + HILOGI("LoadSystemAbility SendRequest succeed!"); + int32_t result = 0; + ret = reply.ReadInt32(result); + if (!ret) { + HILOGW("LoadSystemAbility Read reply failed!"); + return ERR_FLATTEN_OBJECT; + } + return result; +} + int32_t SystemAbilityManagerProxy::MarshalSAExtraProp(const SAExtraProp& extraProp, MessageParcel& data) const { if (!data.WriteBool(extraProp.isDistributed)) { diff --git a/interfaces/innerkits/samgr_proxy/BUILD.gn b/interfaces/innerkits/samgr_proxy/BUILD.gn old mode 100755 new mode 100644 index 47f0b41b9256b0b50c71196fb41464a6f1e3b1a6..63687db463f03bbdc632a5f5c272c1060b29b999 --- a/interfaces/innerkits/samgr_proxy/BUILD.gn +++ b/interfaces/innerkits/samgr_proxy/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -38,6 +38,7 @@ config("samgr_proxy_private_config") { ohos_shared_library("samgr_proxy") { sources = [ + "//foundation/distributedschedule/samgr/frameworks/native/source/system_ability_load_callback_stub.cpp", "//foundation/distributedschedule/samgr/frameworks/native/source/system_ability_manager_proxy.cpp", "//foundation/distributedschedule/samgr/frameworks/native/source/system_ability_status_change_stub.cpp", "//foundation/distributedschedule/samgr/services/lsamgr/src/local_ability_manager_proxy.cpp", diff --git a/interfaces/innerkits/samgr_proxy/include/if_system_ability_manager.h b/interfaces/innerkits/samgr_proxy/include/if_system_ability_manager.h old mode 100755 new mode 100644 index 6c12b2efb6cb968250e388fda9d742f90ead2dac..52f0a4a1434921a78258bf312464148dcb6f4538 --- a/interfaces/innerkits/samgr_proxy/include/if_system_ability_manager.h +++ b/interfaces/innerkits/samgr_proxy/include/if_system_ability_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -22,6 +22,7 @@ #include "iremote_broker.h" #include "iremote_object.h" #include "iremote_proxy.h" +#include "isystem_ability_load_callback.h" #include "isystem_ability_status_change.h" namespace OHOS { @@ -54,6 +55,7 @@ public: REMOVE_SYSTEM_ABILITY_TRANSACTION = 4, LIST_SYSTEM_ABILITY_TRANSACTION = 5, SUBSCRIBE_SYSTEM_ABILITY_TRANSACTION = 6, + LOAD_SYSTEM_ABILITY_TRANSACTION = 7, CHECK_REMOTE_SYSTEM_ABILITY_TRANSACTION = 9, ADD_ONDEMAND_SYSTEM_ABILITY_TRANSACTION = 10, CHECK_SYSTEM_ABILITY_IMMEDIATELY_TRANSACTION = 12, @@ -111,6 +113,7 @@ public: const SAExtraProp& extraProp = SAExtraProp(false, DUMP_FLAG_PRIORITY_DEFAULT, u"", u"")) = 0; virtual int32_t AddSystemProcess(const std::u16string& procName, const sptr& procObject) = 0; + virtual int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr& callback) = 0; public: DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISystemAbilityManager"); protected: diff --git a/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h b/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h new file mode 100644 index 0000000000000000000000000000000000000000..ff2e189122c4aafd131cb34f423d507b1d1a714d --- /dev/null +++ b/interfaces/innerkits/samgr_proxy/include/isystem_ability_load_callback.h @@ -0,0 +1,35 @@ +/* + * Copyright (c) 2022 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 SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_ISYSTEM_ABILITY_LOAD_CALLBACK_H +#define SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_ISYSTEM_ABILITY_LOAD_CALLBACK_H + +#include "iremote_broker.h" +#include "iremote_object.h" +#include "iremote_proxy.h" + +namespace OHOS { +class ISystemAbilityLoadCallback : public IRemoteBroker { +public: + virtual ~ISystemAbilityLoadCallback() = default; + virtual void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) = 0; + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.ISystemAbilityLoadCallback"); +protected: + enum { + ON_LOAD_SYSTEM_ABILITY_SUCCESS = 1, + }; +}; +} +#endif /* SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_ISYSTEM_ABILITY_LOAD_CALLBACK_H */ diff --git a/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h b/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h index 2928c80bbee859612b6b19df5ac12e646a918c3f..c16158a4f2baf842d2b21cc255c33b64699e450f 100755 --- a/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h +++ b/interfaces/innerkits/samgr_proxy/include/system_ability_definition.h @@ -24,6 +24,7 @@ namespace OHOS { enum { FIRST_SYS_ABILITY_ID = 0x00000001, SUBSYS_COMMON_SYS_ABILITY_ID_BEGIN = 1, + RENDER_SERVICE = 10, SUBSYS_AAFWK_SYS_ABILITY_ID_BEGIN = 100, ABILITY_TOOLS_SERVICE_ID = 116, ABILITY_TEST_SERVICE_ID = 179, @@ -99,6 +100,7 @@ enum { DISTRIBUTED_SCHED_TEST_MEDIA_ID = 1499, SUBSYS_DRIVERS_SYS_ABILITY_ID_BEGIN = 1500, SUBSYS_GLOBAL_SYS_ABILITY_ID_BEGIN = 1600, + ENTERPRISE_DEVICE_MANAGER_SA_ID = 1601, SUBSYS_GRAPHIC_SYS_ABILITY_ID_BEGIN = 1700, SUBSYS_HBS_SYS_ABILITY_ID_BEGIN = 1800, SUBSYS_IAWARE_SYS_ABILITY_ID_BEGIN = 1900, @@ -192,18 +194,24 @@ enum { GRAPHIC_DUMPER_SERVICE_SA_ID = 4603, GRAPHIC_DUMPER_COMMAND_SA_ID = 4604, ANIMATION_SERVER_SA_ID = 4605, + WINDOW_MANAGER_SERVICE_ID = 4606, + DISPLAY_MANAGER_SERVICE_SA_ID = 4607, SOFTBUS_SERVER_SA_ID = 4700, DEVICE_AUTH_SERVICE_ID = 4701, SUBSYS_DISTRIBUTED_HARDWARE_SYS_ABILITY_ID_BEGIN = 4800, DISTRIBUTED_HARDWARE_SA_ID = 4801, DISTRIBUTED_HARDWARE_DEVICEMANAGER_SA_ID = 4802, + DISTRIBUTED_HARDWARE_CAMERA_SOURCE_SA_ID = 4803, + DISTRIBUTED_HARDWARE_CAMERA_SINK_SA_ID = 4804, SUBSYS_STORAGE_SYS_ABILITY_ID_BEGIN = 4900, STORAGE_DISTRIBUTED_FILE_DAEMON_SA_ID = 4901, STORAGE_DISTRIBUTED_FILE_SERVICE_SA_ID = 4902, DEVICE_STORAGE_MANAGER_SERVICE_ID = 5000, STORAGE_SERVICE_ID = 5001, + STORAGE_MANAGER_DAEMON_ID = 5002, DEVICE_SERVICE_MANAGER_SA_ID = 5100, DISTRIBUTED_DEVICE_PROFILE_SA_ID = 6001, + SUBSYS_ACE_SYS_ABILITY_ID_BEGIN = 7001, LAST_SYS_ABILITY_ID = 0x00ffffff, // 16777215 }; @@ -248,6 +256,8 @@ static const std::map saNameMap_ = { { USB_SYSTEM_ABILITY_ID, "UsbService" }, { DEVICE_SERVICE_MANAGER_SA_ID, "HdfDeviceServiceManager" }, { DISTRIBUTED_DEVICE_PROFILE_SA_ID, "DistributedDeviceProfile" }, + { WINDOW_MANAGER_SERVICE_ID, "WindowManagerService" }, + { DISPLAY_MANAGER_SERVICE_SA_ID, "DisplayManagerService" }, { WINDOW_MANAGER_ID, "WindowManager" }, { VSYNC_MANAGER_ID, "VsyncManager" }, { VSYNC_MANAGER_TEST_ID, "VsyncManagerTest" }, diff --git a/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h b/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h new file mode 100644 index 0000000000000000000000000000000000000000..3f66df0ccb8b377f2ae9f8ce794a243ff3ee4ae5 --- /dev/null +++ b/interfaces/innerkits/samgr_proxy/include/system_ability_load_callback_stub.h @@ -0,0 +1,34 @@ +/* + * Copyright (c) 2022 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 SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_STUB_H +#define SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_STUB_H + +#include "iremote_stub.h" +#include "isystem_ability_load_callback.h" + +namespace OHOS { +class SystemAbilityLoadCallbackStub : public IRemoteStub { +public: + int32_t OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; + +private: + static bool CheckInputSystemAbilityId(int32_t systemAbilityId); + static bool EnforceInterceToken(MessageParcel& data); + + int32_t OnLoadSystemAbilitySuccessInner(MessageParcel& data, MessageParcel& reply); +}; +} +#endif /* SAMGR_INTERFACES_INNERKITS_SAMGR_PROXY_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_STUB_H */ diff --git a/interfaces/innerkits/samgr_proxy/include/system_ability_manager_proxy.h b/interfaces/innerkits/samgr_proxy/include/system_ability_manager_proxy.h index adfa7cb889a5ba8d2df4faf74010ea44efa0230a..93304fa70dbe0469f3e135859cb276e6d10c2b0c 100755 --- a/interfaces/innerkits/samgr_proxy/include/system_ability_manager_proxy.h +++ b/interfaces/innerkits/samgr_proxy/include/system_ability_manager_proxy.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -44,6 +44,7 @@ public: const SAExtraProp& extraProp) override; int32_t AddSystemProcess(const std::u16string& procName, const sptr& procObject) override; + int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr& callback) override; private: sptr GetSystemAbilityWrapper(int32_t systemAbilityId, const std::string& deviceId = ""); sptr CheckSystemAbilityWrapper(int32_t code, MessageParcel& data); diff --git a/ohos.build b/ohos.build old mode 100755 new mode 100644 index 7465c6664fff3f98b8a0c75ce230e5999d052bb6..afeea530355f1bbdffcdaf378cc7dab8d5b513f2 --- a/ohos.build +++ b/ohos.build @@ -9,8 +9,10 @@ "header_files": [ "if_system_ability_manager.h", "iservice_registry.h", + "isystem_ability_load_callback.h", "isystem_ability_status_change.h", "system_ability_manager_proxy.h", + "system_ability_load_callback_stub.h", "system_ability_status_change_stub.h", "sam_log.h" ] diff --git a/services/samgr/native/BUILD.gn b/services/samgr/native/BUILD.gn old mode 100755 new mode 100644 index db125811cbe395dc63e65a2c842b3d61b290b260..bc7fa7c1d0098a18e2a6cdf12297dafd6f9ba86d --- a/services/samgr/native/BUILD.gn +++ b/services/samgr/native/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -57,6 +57,7 @@ ohos_executable("samgr") { sources = [ "//foundation/distributedschedule/samgr/services/samgr/native/source/ability_death_recipient.cpp", "//foundation/distributedschedule/samgr/services/samgr/native/source/main.cpp", + "//foundation/distributedschedule/samgr/services/samgr/native/source/system_ability_load_callback_proxy.cpp", "//foundation/distributedschedule/samgr/services/samgr/native/source/system_ability_manager.cpp", "//foundation/distributedschedule/samgr/services/samgr/native/source/system_ability_manager_stub.cpp", "//foundation/distributedschedule/samgr/services/samgr/native/source/system_ability_status_change_proxy.cpp", @@ -81,6 +82,7 @@ ohos_executable("samgr") { external_deps = [ "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", "ipc:ipc_core", "ipc:libdbinder", ] diff --git a/services/samgr/native/include/ability_death_recipient.h b/services/samgr/native/include/ability_death_recipient.h old mode 100755 new mode 100644 index f11ea992fd5c75793b6a77ea8cdb8a8fb39551eb..27fe3c9b90f5bc633dc8e6f046165fdea1e36a7c --- a/services/samgr/native/include/ability_death_recipient.h +++ b/services/samgr/native/include/ability_death_recipient.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -39,6 +39,13 @@ public: AbilityStatusDeathRecipient() = default; ~AbilityStatusDeathRecipient() override = default; }; + +class AbilityCallbackDeathRecipient : public IRemoteObject::DeathRecipient { +public: + void OnRemoteDied(const wptr& remote) override; + AbilityCallbackDeathRecipient() = default; + ~AbilityCallbackDeathRecipient() override = default; +}; } // namespace OHOS #endif // !defined(SERVICES_SAMGR_NATIVE_INCLUDE_ABILITY_DEATH_RECIPIENT_H_) diff --git a/services/samgr/native/include/system_ability_load_callback_proxy.h b/services/samgr/native/include/system_ability_load_callback_proxy.h new file mode 100644 index 0000000000000000000000000000000000000000..4cf905f58b8a334d3b9f3aff1cfdefa6c466732c --- /dev/null +++ b/services/samgr/native/include/system_ability_load_callback_proxy.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2022 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 SAMGR_SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_PROXY_H +#define SAMGR_SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_PROXY_H + +#include "isystem_ability_load_callback.h" + +namespace OHOS { +class SystemAbilityLoadCallbackProxy : public IRemoteProxy { +public: + explicit SystemAbilityLoadCallbackProxy(const sptr& impl) + : IRemoteProxy(impl) {} + ~SystemAbilityLoadCallbackProxy() = default; + + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; +private: + static inline BrokerDelegator delegator_; +}; +} +#endif /* SAMGR_SERVICES_SAMGR_NATIVE_INCLUDE_SYSTEM_ABILITY_LOAD_CALLBACK_PROXY_H */ diff --git a/services/samgr/native/include/system_ability_manager.h b/services/samgr/native/include/system_ability_manager.h index ba0b5d152e2774686a9a359578b8f908cf822753..093160184692c6bf0b6e6686df2b8248cbd617a8 100755 --- a/services/samgr/native/include/system_ability_manager.h +++ b/services/samgr/native/include/system_ability_manager.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -73,8 +73,7 @@ public: sptr CheckSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; - int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, - const std::u16string& localAbilityManagerName) override; + int32_t AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, const std::u16string& procName) override; sptr CheckSystemAbility(int32_t systemAbilityId, bool& isExist) override; @@ -88,13 +87,25 @@ public: int32_t AddSystemProcess(const std::u16string& procName, const sptr& procObject) override; int32_t RemoveSystemProcess(const sptr& procObject); + + int32_t LoadSystemAbility(int32_t systemAbilityId, const sptr& callback) override; + void OnAbilityCallbackDied(const sptr& remoteObject); private: + enum class AbilityState { + INIT, + STARTING, + STARTED, + }; + struct AbilityItem { + AbilityState state = AbilityState::INIT; + std::list, int32_t>> callbackList; + }; + SystemAbilityManager(); std::u16string GetSystemAbilityName(int32_t index) override; void DoInsertSaData(const std::u16string& name, const sptr& ability, const SAExtraProp& extraProp); bool IsNameInValid(const std::u16string& name); int32_t StartOnDemandAbility(int32_t systemAbilityId); - void DeleteStartingAbilityMember(int32_t systemAbilityId); void ParseRemoteSaName(const std::u16string& name, std::string& deviceId, std::u16string& saName); bool IsLocalDeviceId(const std::string& deviceId); bool CheckDistributedPermission(); @@ -104,7 +115,7 @@ private: int32_t FindSystemAbilityNotify(int32_t systemAbilityId, const std::string& deviceId, int32_t code); sptr GetSystemProcess(const std::u16string& procName); - sptr CheckLocalAbilityManager(const std::u16string& name); + void InitSaProfile(); bool GetSaProfile(int32_t saId, SaProfile& saProfile); void NotifySystemAbilityChanged(int32_t systemAbilityId, const std::string& deviceId, int32_t code, @@ -112,12 +123,26 @@ private: void UnSubscribeSystemAbilityLocked(std::list, int32_t>>& listenerList, const sptr& listener); + void SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr& remoteObject); + void SendSystemAbilityRemovedMsg(int32_t systemAbilityId); + + void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject); + void NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject, + const sptr& callback); + int32_t StartingSystemProcess(const std::u16string& name, int32_t systemAbilityId); + void StartOnDemandAbility(const std::u16string& name, int32_t systemAbilityId); + void StartOnDemandAbilityInner(const std::u16string& name, int32_t systemAbilityId, AbilityItem& abilityItem); + int32_t StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId); + void RemoveStartingAbilityCallback(AbilityItem& abilityItem, const sptr& remoteObject); + void RemoveStartingAbilityCallbackLocked(std::pair, int32_t>& itemPair); + std::u16string deviceName_; static sptr instance; static std::mutex instanceLock; sptr abilityDeath_; sptr systemProcessDeath_; sptr abilityStatusDeath_; + sptr abilityCallbackDeath_; sptr dBinderService_; bool isDbinderStart_ = false; @@ -125,21 +150,19 @@ private: std::shared_mutex abilityMapLock_; std::map abilityMap_; - // must hold systemProcessMapLock_ never access other locks - std::recursive_mutex systemProcessMapLock_; - std::map> systemProcessMap_; - - // maybe hold listenerMapLock_ and then access systemProcessMapLock_ + // maybe hold listenerMapLock_ and then access onDemandLock_ std::recursive_mutex listenerMapLock_; std::map, int32_t>>> listenerMap_; std::map subscribeCountMap_; - // maybe hold onDemandAbilityMapLock_ and then access systemProcessMapLock_ - std::recursive_mutex onDemandAbilityMapLock_; + std::recursive_mutex onDemandLock_; std::map onDemandAbilityMap_; - std::list startingAbilityList_; + std::map startingAbilityMap_; + std::map> systemProcessMap_; + std::map startingProcessMap_; + std::map callbackCountMap_; - std::shared_ptr parseHandler_; + std::shared_ptr workHandler_; std::map saProfileMap_; std::mutex saProfileMapLock_; diff --git a/services/samgr/native/include/system_ability_manager_stub.h b/services/samgr/native/include/system_ability_manager_stub.h old mode 100755 new mode 100644 index 079bd27ec73e2696d14bfa76e1fa60ab18d10c11..01bd44b275550192e7cbeb3a892702cc31212d6e --- a/services/samgr/native/include/system_ability_manager_stub.h +++ b/services/samgr/native/include/system_ability_manager_stub.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -47,6 +47,7 @@ private: int32_t CheckSystemAbilityInner(MessageParcel& data, MessageParcel& reply); int32_t AddSystemProcessInner(MessageParcel& data, MessageParcel& reply); int32_t RemoveSystemAbilityInner(MessageParcel& data, MessageParcel& reply); + int32_t LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply); int32_t UnmarshalingSaExtraProp(MessageParcel& data, SAExtraProp& extraProp); static int32_t GetHapIdMultiuser(int32_t uid); diff --git a/services/samgr/native/source/ability_death_recipient.cpp b/services/samgr/native/source/ability_death_recipient.cpp old mode 100755 new mode 100644 index a355b803b544f5c76d9f2676e0735ee47bbe0d6a..a8b46fa12d88772158c26eb23e325926c8196b09 --- a/services/samgr/native/source/ability_death_recipient.cpp +++ b/services/samgr/native/source/ability_death_recipient.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -40,4 +40,11 @@ void AbilityStatusDeathRecipient::OnRemoteDied(const wptr& remote SystemAbilityManager::GetInstance()->UnSubscribeSystemAbility(remote.promote()); HILOGI("%{public}s death notice success", __func__); } + +void AbilityCallbackDeathRecipient::OnRemoteDied(const wptr& remote) +{ + HILOGI("%{public}s called!", __func__); + SystemAbilityManager::GetInstance()->OnAbilityCallbackDied(remote.promote()); + HILOGI("%{public}s death notice success", __func__); +} } // namespace OHOS diff --git a/services/samgr/native/source/system_ability_load_callback_proxy.cpp b/services/samgr/native/source/system_ability_load_callback_proxy.cpp new file mode 100644 index 0000000000000000000000000000000000000000..d3e9a672f50953c4c6a9a35bdf42d355a3206b5d --- /dev/null +++ b/services/samgr/native/source/system_ability_load_callback_proxy.cpp @@ -0,0 +1,64 @@ +/* + * Copyright (c) 2022 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 "system_ability_load_callback_proxy.h" + +#include "ipc_types.h" +#include "parcel.h" +#include "sam_log.h" + +namespace OHOS { +void SystemAbilityLoadCallbackProxy::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + if (systemAbilityId <= 0) { + HILOGE("OnLoadSystemAbilitySuccess systemAbilityId:%{public}d invalid!", systemAbilityId); + return; + } + + sptr remote = Remote(); + if (remote == nullptr) { + HILOGE("OnLoadSystemAbilitySuccess Remote() return null!"); + return; + } + MessageParcel data; + if (!data.WriteInterfaceToken(GetDescriptor())) { + HILOGE("OnLoadSystemAbilitySuccess write interface token failed!"); + return; + } + bool ret = data.WriteInt32(systemAbilityId); + if (!ret) { + HILOGE("OnLoadSystemAbilitySuccess write systemAbilityId failed!"); + return; + } + if (remoteObject == nullptr) { + HILOGE("OnLoadSystemAbilitySuccess IRemoteObject null!"); + return; + } + ret = data.WriteRemoteObject(remoteObject); + if (!ret) { + HILOGE("OnLoadSystemAbilitySuccess write IRemoteObject failed!"); + return; + } + + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + int32_t status = remote->SendRequest(ON_LOAD_SYSTEM_ABILITY_SUCCESS, data, reply, option); + if (status != NO_ERROR) { + HILOGE("OnLoadSystemAbilitySuccess SendRequest failed, return value:%{public}d !", status); + return; + } +} +} diff --git a/services/samgr/native/source/system_ability_manager.cpp b/services/samgr/native/source/system_ability_manager.cpp index e4e7beeb8aed4b3e32df66ae59a1934b470cbc38..540e9e3f0d1fa96d38634e70460ca057f5189964 100755 --- a/services/samgr/native/source/system_ability_manager.cpp +++ b/services/samgr/native/source/system_ability_manager.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -23,14 +23,13 @@ #include "directory_ex.h" #include "errors.h" #include "if_local_ability_manager.h" +#include "ipc_skeleton.h" #include "local_ability_manager_proxy.h" #include "parse_util.h" - #include "sam_log.h" +#include "service_control.h" #include "string_ex.h" -#include "ipc_skeleton.h" #include "system_ability_definition.h" - #include "tools.h" using namespace std; @@ -63,9 +62,10 @@ void SystemAbilityManager::Init() abilityDeath_ = sptr(new AbilityDeathRecipient()); systemProcessDeath_ = sptr(new SystemProcessDeathRecipient()); abilityStatusDeath_ = sptr(new AbilityStatusDeathRecipient()); - if (parseHandler_ == nullptr) { - auto parseRunner = AppExecFwk::EventRunner::Create("ParseHandler"); - parseHandler_ = make_shared(parseRunner); + abilityCallbackDeath_ = sptr(new AbilityCallbackDeathRecipient()); + if (workHandler_ == nullptr) { + auto runner = AppExecFwk::EventRunner::Create("workHandler"); + workHandler_ = make_shared(runner); } InitSaProfile(); } @@ -86,7 +86,7 @@ sptr SystemAbilityManager::GetInstance() void SystemAbilityManager::InitSaProfile() { - if (parseHandler_ == nullptr) { + if (workHandler_ == nullptr) { HILOGE("InitSaProfile parseHandler_ not init!"); return; } @@ -110,7 +110,7 @@ void SystemAbilityManager::InitSaProfile() } HILOGI("[PerformanceTest] InitSaProfile spend %{public}" PRId64 " ms", GetTickCount() - begin); }; - bool ret = parseHandler_->PostTask(callback); + bool ret = workHandler_->PostTask(callback); if (!ret) { HILOGW("SystemAbilityManager::InitSaProfile PostTask fail"); } @@ -235,16 +235,43 @@ bool SystemAbilityManager::IsNameInValid(const std::u16string& name) return ret; } +void SystemAbilityManager::StartOnDemandAbility(const std::u16string& procName, int32_t systemAbilityId) +{ + lock_guard autoLock(onDemandLock_); + auto iter = startingAbilityMap_.find(systemAbilityId); + if (iter == startingAbilityMap_.end()) { + return; + } + auto& abilityItem = iter->second; + StartOnDemandAbilityInner(procName, systemAbilityId, abilityItem); +} + +void SystemAbilityManager::StartOnDemandAbilityInner(const std::u16string& procName, int32_t systemAbilityId, + AbilityItem& abilityItem) +{ + if (abilityItem.state != AbilityState::INIT) { + return; + } + sptr procObject = + iface_cast(GetSystemProcess(procName)); + if (procObject == nullptr) { + HILOGI("get process:%{public}s fail", Str16ToStr8(procName).c_str()); + return; + } + procObject->StartAbility(systemAbilityId); + abilityItem.state = AbilityState::STARTING; +} + int32_t SystemAbilityManager::AddOnDemandSystemAbilityInfo(int32_t systemAbilityId, - const std::u16string& localAbilityManagerName) + const std::u16string& procName) { HILOGI("%{public}s called", __func__); - if (!CheckInputSysAbilityId(systemAbilityId) || IsNameInValid(localAbilityManagerName)) { - HILOGI("var is invalid."); + if (!CheckInputSysAbilityId(systemAbilityId) || IsNameInValid(procName)) { + HILOGW("AddOnDemandSystemAbilityInfo systemAbilityId or procName invalid."); return ERR_INVALID_VALUE; } - lock_guard autoLock(onDemandAbilityMapLock_); + lock_guard autoLock(onDemandLock_); auto onDemandSaSize = onDemandAbilityMap_.size(); if (onDemandSaSize >= MAX_SERVICES) { HILOGE("map size error, (Has been greater than %{public}zu)", @@ -252,30 +279,39 @@ int32_t SystemAbilityManager::AddOnDemandSystemAbilityInfo(int32_t systemAbility return ERR_INVALID_VALUE; } - onDemandAbilityMap_[systemAbilityId] = localAbilityManagerName; - HILOGI("insert %{public}d. size : %{public}zu", systemAbilityId, onDemandAbilityMap_.size()); + if (systemProcessMap_.count(procName) == 0) { + HILOGW("AddOnDemandSystemAbilityInfo procName:%{public}s not exist.", Str16ToStr8(procName).c_str()); + return ERR_INVALID_VALUE; + } + onDemandAbilityMap_[systemAbilityId] = procName; + HILOGI("insert onDemand systemAbilityId:%{public}d. size : %{public}zu", systemAbilityId, + onDemandAbilityMap_.size()); + if (startingAbilityMap_.count(systemAbilityId) != 0) { + if (workHandler_ != nullptr) { + auto pendingTask = [procName, systemAbilityId, this] () { + StartOnDemandAbility(procName, systemAbilityId); + }; + bool ret = workHandler_->PostTask(pendingTask); + if (!ret) { + HILOGW("AddOnDemandSystemAbilityInfo PostTask failed!"); + } + } + } return ERR_OK; } int32_t SystemAbilityManager::StartOnDemandAbility(int32_t systemAbilityId) { HILOGI("%{public}s called, systemAbilityId is %{public}d", __func__, systemAbilityId); - lock_guard onDemandAbilityLock(onDemandAbilityMapLock_); + lock_guard onDemandAbilityLock(onDemandLock_); auto iter = onDemandAbilityMap_.find(systemAbilityId); - if (iter != onDemandAbilityMap_.end()) { - HILOGI("found onDemandAbility: %{public}d.", systemAbilityId); - sptr procObject = - iface_cast(GetSystemProcess(iter->second)); - if (procObject == nullptr) { - HILOGI("get process:%{public}s fail", Str16ToStr8(iter->second).c_str()); - return ERR_OK; - } - procObject->StartAbility(systemAbilityId); - startingAbilityList_.emplace_back(systemAbilityId); - return ERR_OK; + if (iter == onDemandAbilityMap_.end()) { + return ERR_INVALID_VALUE; } - - return ERR_INVALID_VALUE; + HILOGI("found onDemandAbility: %{public}d.", systemAbilityId); + AbilityItem& abilityItem = startingAbilityMap_[systemAbilityId]; + StartOnDemandAbilityInner(iter->second, systemAbilityId, abilityItem); + return ERR_OK; } sptr SystemAbilityManager::CheckSystemAbility(int32_t systemAbilityId, bool& isExist) @@ -285,14 +321,11 @@ sptr SystemAbilityManager::CheckSystemAbility(int32_t systemAbili } sptr abilityProxy = CheckSystemAbility(systemAbilityId); if (abilityProxy == nullptr) { - { - lock_guard autoLock(onDemandAbilityMapLock_); - for (int32_t startingAbilityId : startingAbilityList_) { - if (startingAbilityId == systemAbilityId) { - isExist = true; - return nullptr; - } - } + lock_guard autoLock(onDemandLock_); + auto iter = startingAbilityMap_.find(systemAbilityId); + if (iter != startingAbilityMap_.end() && iter->second.state == AbilityState::STARTING) { + isExist = true; + return nullptr; } int32_t ret = StartOnDemandAbility(systemAbilityId); @@ -310,21 +343,6 @@ sptr SystemAbilityManager::CheckSystemAbility(int32_t systemAbili return abilityProxy; } -void SystemAbilityManager::DeleteStartingAbilityMember(int32_t systemAbilityId) -{ - if (!CheckInputSysAbilityId(systemAbilityId)) { - return; - } - lock_guard autoLock(onDemandAbilityMapLock_); - for (auto iter = startingAbilityList_.begin(); iter != startingAbilityList_.end();) { - if (*iter == systemAbilityId) { - iter = startingAbilityList_.erase(iter); - } else { - ++iter; - } - } -} - int32_t SystemAbilityManager::RemoveSystemAbility(int32_t systemAbilityId) { HILOGI("%s called (name)", __func__); @@ -347,9 +365,7 @@ int32_t SystemAbilityManager::RemoveSystemAbility(int32_t systemAbilityId) HILOGI("%s called, systemAbilityId : %{public}d, size : %{public}zu", __func__, systemAbilityId, abilityMap_.size()); } - - FindSystemAbilityNotify(systemAbilityId, REMOVE_SYSTEM_ABILITY_TRANSACTION); - DeleteStartingAbilityMember(systemAbilityId); + SendSystemAbilityRemovedMsg(systemAbilityId); return ERR_OK; } @@ -379,8 +395,7 @@ int32_t SystemAbilityManager::RemoveSystemAbility(const sptr& abi } if (saId != 0) { - FindSystemAbilityNotify(saId, REMOVE_SYSTEM_ABILITY_TRANSACTION); - DeleteStartingAbilityMember(saId); + SendSystemAbilityRemovedMsg(saId); } return ERR_OK; } @@ -572,7 +587,7 @@ int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sp if (abilityDeath_ != nullptr) { ability->AddDeathRecipient(abilityDeath_); } - FindSystemAbilityNotify(systemAbilityId, ADD_SYSTEM_ABILITY_TRANSACTION); + u16string strName = Str8ToStr16(to_string(systemAbilityId)); if (extraProp.isDistributed && dBinderService_ != nullptr) { dBinderService_->RegisterRemoteProxy(strName, systemAbilityId); @@ -585,6 +600,7 @@ int32_t SystemAbilityManager::AddSystemAbility(int32_t systemAbilityId, const sp isDbinderStart_ = true; } } + SendSystemAbilityAddedMsg(systemAbilityId, ability); return ERR_OK; } @@ -596,7 +612,7 @@ int32_t SystemAbilityManager::AddSystemProcess(const u16string& procName, return ERR_INVALID_VALUE; } - lock_guard autoLock(systemProcessMapLock_); + lock_guard autoLock(onDemandLock_); size_t procNum = systemProcessMap_.size(); if (procNum >= MAX_SERVICES) { HILOGE("AddSystemProcess map size reach MAX_SERVICES already"); @@ -609,6 +625,13 @@ int32_t SystemAbilityManager::AddSystemProcess(const u16string& procName, } HILOGI("AddSystemProcess insert %{public}s. size : %{public}zu", Str16ToStr8(procName).c_str(), systemProcessMap_.size()); + auto iterStarting = startingProcessMap_.find(procName); + if (iterStarting != startingProcessMap_.end()) { + int64_t end = GetTickCount(); + HILOGI("[PerformanceTest] AddSystemProcess start process:%{public}s spend %{public}" PRId64 " ms", + Str16ToStr8(procName).c_str(), (end - iterStarting->second)); + startingProcessMap_.erase(iterStarting); + } return ERR_OK; } @@ -620,7 +643,7 @@ int32_t SystemAbilityManager::RemoveSystemProcess(const sptr& pro return ERR_INVALID_VALUE; } - lock_guard autoLock(systemProcessMapLock_); + lock_guard autoLock(onDemandLock_); for (const auto& [procName, object] : systemProcessMap_) { if (object == procObject) { if (systemProcessDeath_ != nullptr) { @@ -643,7 +666,7 @@ sptr SystemAbilityManager::GetSystemProcess(const u16string& proc return nullptr; } - lock_guard autoLock(systemProcessMapLock_); + lock_guard autoLock(onDemandLock_); auto iter = systemProcessMap_.find(procName); if (iter != systemProcessMap_.end()) { HILOGI("process:%{public}s found", Str16ToStr8(procName).c_str()); @@ -653,6 +676,190 @@ sptr SystemAbilityManager::GetSystemProcess(const u16string& proc return nullptr; } +void SystemAbilityManager::SendSystemAbilityAddedMsg(int32_t systemAbilityId, const sptr& remoteObject) +{ + if (workHandler_ == nullptr) { + HILOGE("SendSystemAbilityAddedMsg work handler not initialized!"); + return; + } + auto notifyAddedTask = [systemAbilityId, remoteObject, this]() { + FindSystemAbilityNotify(systemAbilityId, ADD_SYSTEM_ABILITY_TRANSACTION); + NotifySystemAbilityLoaded(systemAbilityId, remoteObject); + }; + bool ret = workHandler_->PostTask(notifyAddedTask); + if (!ret) { + HILOGW("SendSystemAbilityAddedMsg PostTask failed!"); + } +} + +void SystemAbilityManager::SendSystemAbilityRemovedMsg(int32_t systemAbilityId) +{ + if (workHandler_ == nullptr) { + HILOGE("SendSystemAbilityRemovedMsg work handler not initialized!"); + return; + } + auto notifyRemovedTask = [systemAbilityId, this]() { + FindSystemAbilityNotify(systemAbilityId, REMOVE_SYSTEM_ABILITY_TRANSACTION); + }; + bool ret = workHandler_->PostTask(notifyRemovedTask); + if (!ret) { + HILOGW("SendSystemAbilityRemovedMsg PostTask failed!"); + } +} + +void SystemAbilityManager::NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject, + const sptr& callback) +{ + if (callback == nullptr) { + HILOGE("NotifySystemAbilityLoaded callback null!"); + return; + } + callback->OnLoadSystemAbilitySuccess(systemAbilityId, remoteObject); +} + +void SystemAbilityManager::NotifySystemAbilityLoaded(int32_t systemAbilityId, const sptr& remoteObject) +{ + lock_guard autoLock(onDemandLock_); + auto iter = startingAbilityMap_.find(systemAbilityId); + if (iter == startingAbilityMap_.end()) { + return; + } + auto& abilityItem = iter->second; + for (auto& callbackItem : abilityItem.callbackList) { + NotifySystemAbilityLoaded(systemAbilityId, remoteObject, callbackItem.first); + RemoveStartingAbilityCallbackLocked(callbackItem); + } + startingAbilityMap_.erase(iter); +} + +int32_t SystemAbilityManager::StartDynamicSystemProcess(const std::u16string& name, int32_t systemAbilityId) +{ + std::string strExtra = std::to_string(systemAbilityId); + auto extraArgv = strExtra.c_str(); + auto result = ServiceControlWithExtra(Str16ToStr8(name).c_str(), START, &extraArgv, 1); + HILOGI("StartDynamicSystemProcess call ServiceControlWithExtra result:%{public}d!", result); + if (result == 0) { + return ERR_OK; + } + return ERR_INVALID_VALUE; +} + +int32_t SystemAbilityManager::StartingSystemProcess(const std::u16string& procName, int32_t systemAbilityId) +{ + lock_guard autoLock(onDemandLock_); + if (startingProcessMap_.count(procName) != 0) { + HILOGI("StartingSystemProcess process:%{public}s already starting!", Str16ToStr8(procName).c_str()); + return ERR_OK; + } + auto iter = systemProcessMap_.find(procName); + if (iter != systemProcessMap_.end()) { + StartOnDemandAbility(systemAbilityId); + return ERR_OK; + } + // call init start process + int64_t begin = GetTickCount(); + int32_t result = StartDynamicSystemProcess(procName, systemAbilityId); + if (result == ERR_OK) { + startingProcessMap_.emplace(procName, begin); + } + return result; +} + +int32_t SystemAbilityManager::LoadSystemAbility(int32_t systemAbilityId, + const sptr& callback) +{ + if (!CheckInputSysAbilityId(systemAbilityId) || callback == nullptr) { + HILOGW("LoadSystemAbility systemAbilityId or callback invalid!"); + return ERR_INVALID_VALUE; + } + SaProfile saProfile; + bool ret = GetSaProfile(systemAbilityId, saProfile); + if (!ret) { + HILOGE("LoadSystemAbility systemAbilityId:%{public}d not supported!", systemAbilityId); + return ERR_INVALID_VALUE; + } + + sptr targetObject = CheckSystemAbility(systemAbilityId); + if (targetObject != nullptr) { + NotifySystemAbilityLoaded(systemAbilityId, targetObject, callback); + return ERR_OK; + } + + auto callingPid = IPCSkeleton::GetCallingPid(); + lock_guard autoLock(onDemandLock_); + auto& abilityItem = startingAbilityMap_[systemAbilityId]; + for (const auto& itemCallback : abilityItem.callbackList) { + if (callback->AsObject() == itemCallback.first->AsObject()) { + HILOGI("LoadSystemAbility already existed callback object systemAbilityId:%{public}d", systemAbilityId); + return ERR_OK; + } + } + auto& count = callbackCountMap_[callingPid]; + if (count >= MAX_SUBSCRIBE_COUNT) { + HILOGE("LoadSystemAbility pid:%{public}d overflow max callback count!", callingPid); + return ERR_PERMISSION_DENIED; + } + ++count; + abilityItem.callbackList.emplace_back(callback, callingPid); + if (abilityCallbackDeath_ != nullptr) { + bool ret = callback->AsObject()->AddDeathRecipient(abilityCallbackDeath_); + HILOGI("LoadSystemAbility systemAbilityId:%{public}d AddDeathRecipient %{public}s", + systemAbilityId, ret ? "succeed" : "failed"); + } + int32_t result = StartingSystemProcess(saProfile.process, systemAbilityId); + HILOGI("LoadSystemAbility systemAbilityId:%{public}d size : %{public}zu", + systemAbilityId, abilityItem.callbackList.size()); + return result; +} + +void SystemAbilityManager::RemoveStartingAbilityCallbackLocked( + std::pair, int32_t>& itemPair) +{ + if (abilityCallbackDeath_ != nullptr) { + itemPair.first->AsObject()->RemoveDeathRecipient(abilityCallbackDeath_); + } + auto iterCount = callbackCountMap_.find(itemPair.second); + if (iterCount != callbackCountMap_.end()) { + --iterCount->second; + if (iterCount->second == 0) { + callbackCountMap_.erase(iterCount); + } + } +} + +void SystemAbilityManager::RemoveStartingAbilityCallback(AbilityItem& abilityItem, + const sptr& remoteObject) +{ + auto& callbacks = abilityItem.callbackList; + auto iterCallback = callbacks.begin(); + while (iterCallback != callbacks.end()) { + auto& callbackPair = *iterCallback; + if (callbackPair.first->AsObject() == remoteObject) { + RemoveStartingAbilityCallbackLocked(callbackPair); + iterCallback = callbacks.erase(iterCallback); + break; + } else { + ++iterCallback; + } + } +} + +void SystemAbilityManager::OnAbilityCallbackDied(const sptr& remoteObject) +{ + HILOGI("OnAbilityCallbackDied received remoteObject died message!"); + lock_guard autoLock(onDemandLock_); + auto iter = startingAbilityMap_.begin(); + while (iter != startingAbilityMap_.end()) { + AbilityItem& abilityItem = iter->second; + RemoveStartingAbilityCallback(abilityItem, remoteObject); + if (abilityItem.callbackList.empty()) { + iter = startingAbilityMap_.erase(iter); + } else { + ++iter; + } + } +} + std::string SystemAbilityManager::TransformDeviceId(const std::string& deviceId, int32_t type, bool isPrivate) { return isPrivate ? std::string() : deviceId; diff --git a/services/samgr/native/source/system_ability_manager_stub.cpp b/services/samgr/native/source/system_ability_manager_stub.cpp index 6a16adab59bb81e019041ec2bbeb964fc05645fc..db9a845ae805798ae57009b4932ccbeb2c5bf50d 100755 --- a/services/samgr/native/source/system_ability_manager_stub.cpp +++ b/services/samgr/native/source/system_ability_manager_stub.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -55,6 +55,8 @@ SystemAbilityManagerStub::SystemAbilityManagerStub() &SystemAbilityManagerStub::UnSubsSystemAbilityInner; memberFuncMap_[ADD_SYSTEM_PROCESS_TRANSACTION] = &SystemAbilityManagerStub::AddSystemProcessInner; + memberFuncMap_[LOAD_SYSTEM_ABILITY_TRANSACTION] = + &SystemAbilityManagerStub::LoadSystemAbilityInner; } int32_t SystemAbilityManagerStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption &option) @@ -422,6 +424,33 @@ int32_t SystemAbilityManagerStub::AddSystemProcessInner(MessageParcel& data, Mes return result; } +int32_t SystemAbilityManagerStub::LoadSystemAbilityInner(MessageParcel& data, MessageParcel& reply) +{ + int32_t systemAbilityId = data.ReadInt32(); + if (!CheckInputSysAbilityId(systemAbilityId)) { + HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read systemAbilityId failed!"); + return ERR_INVALID_VALUE; + } + sptr remoteObject = data.ReadRemoteObject(); + if (remoteObject == nullptr) { + HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner read callback failed!"); + return ERR_INVALID_VALUE; + } + sptr callback = iface_cast(remoteObject); + if (callback == nullptr) { + HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner iface_cast failed!"); + return ERR_INVALID_VALUE; + } + int32_t result = LoadSystemAbility(systemAbilityId, callback); + HILOGI("SystemAbilityManagerStub::LoadSystemAbilityInner result is %{public}d", result); + bool ret = reply.WriteInt32(result); + if (!ret) { + HILOGW("SystemAbilityManagerStub::LoadSystemAbilityInner write reply failed."); + return ERR_FLATTEN_OBJECT; + } + return result; +} + int32_t SystemAbilityManagerStub::GetHapIdMultiuser(int32_t uid) { return uid % MULTIUSER_HAP_PER_USER_RANGE; diff --git a/services/samgr/native/test/unittest/BUILD.gn b/services/samgr/native/test/unittest/BUILD.gn old mode 100755 new mode 100644 index 7f948fb6d9d7330c77fb0b1910bfe827c2998019..c326589c51516e285c426a358d414049746bdd94 --- a/services/samgr/native/test/unittest/BUILD.gn +++ b/services/samgr/native/test/unittest/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2021-2022 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 @@ -28,6 +28,7 @@ ohos_unittest("SystemAbilityMgrTest") { sources = [ "${samgr_dir}/utils/native/source/tools.cpp", "${samgr_services_dir}/source/ability_death_recipient.cpp", + "${samgr_services_dir}/source/system_ability_load_callback_proxy.cpp", "${samgr_services_dir}/source/system_ability_manager.cpp", "${samgr_services_dir}/source/system_ability_manager_stub.cpp", "${samgr_services_dir}/source/system_ability_status_change_proxy.cpp", @@ -58,12 +59,41 @@ ohos_unittest("SystemAbilityMgrTest") { external_deps = [ "eventhandler:libeventhandler", "hiviewdfx_hilog_native:libhilog", + "init:libbegetutil", "ipc:ipc_core", "ipc:libdbinder", ] } +ohos_executable("ondemand") { + sources = [ "./src/ondemand_helper.cpp" ] + configs = [ + ":sam_test_config", + "${samgr_dir}/interfaces/innerkits/lsamgr:config_samgr", + ] + deps = [ "//utils/native/base:utils" ] + if (is_standard_system) { + external_deps = [ "hiviewdfx_hilog_native:libhilog_base" ] + } else { + external_deps = [ "hilog:libhilog" ] + } + + cflags = [] + if (target_cpu == "arm") { + cflags += [ "-DBINDER_IPC_32BIT" ] + } + external_deps += [ + "ipc:ipc_core", + "samgr_standard:samgr_proxy", + ] + part_name = "samgr_standard" + subsystem_name = "distributedschedule" +} + group("unittest") { testonly = true - deps = [ ":SystemAbilityMgrTest" ] + deps = [ + ":SystemAbilityMgrTest", + ":ondemand", + ] } diff --git a/services/samgr/native/test/unittest/include/ondemand_helper.h b/services/samgr/native/test/unittest/include/ondemand_helper.h new file mode 100644 index 0000000000000000000000000000000000000000..8de9f8cb693d2cef0a4ec649cd107c2e157f8d4a --- /dev/null +++ b/services/samgr/native/test/unittest/include/ondemand_helper.h @@ -0,0 +1,40 @@ +/* + * Copyright (c) 2022 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 SAMGR_TEST_UNITTEST_INCLUDE_ONDEMAND_HELPER_H +#define SAMGR_TEST_UNITTEST_INCLUDE_ONDEMAND_HELPER_H + +#include +#include +#include "system_ability_load_callback_stub.h" +namespace OHOS { +class OnDemandHelper { +public: + ~OnDemandHelper() = default; + static OnDemandHelper& GetInstance(); + + int32_t OnDemandAbility(int32_t systemAbilityId); + sptr GetSystemAbility(int32_t systemAbilityId); + void OnLoadSystemAbility(int32_t systemAbilityId); +protected: + class OnDemandLoadCallback : public SystemAbilityLoadCallbackStub { + public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + }; +private: + OnDemandHelper(); + sptr loadCallback_; +}; +} +#endif /* SAMGR_TEST_UNITTEST_INCLUDE_ONDEMAND_HELPER_H */ diff --git a/services/samgr/native/test/unittest/include/sa_status_change_mock.h b/services/samgr/native/test/unittest/include/sa_status_change_mock.h index 9383a7b4c9711546be974bdff433b80ca112d8f5..0e818f4850d4bf9aef3dd4e866e3386d54d8314d 100755 --- a/services/samgr/native/test/unittest/include/sa_status_change_mock.h +++ b/services/samgr/native/test/unittest/include/sa_status_change_mock.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -17,11 +17,31 @@ #define SAMGR_SERVICES_SAMGR_NATIVE_TEST_UNITTEST_INCLUDE_SA_STATUS_CHANGE_MOCK_H #include "system_ability_status_change_stub.h" +#include "system_ability_load_callback_stub.h" + namespace OHOS { class SaStatusChangeMock : public SystemAbilityStatusChangeStub { public: void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; }; + +class SystemAbilityLoadCallbackMock : public SystemAbilityLoadCallbackStub { +public: + void OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr& remoteObject) override; + + int32_t GetSystemAbilityId() const + { + return currSystemAbilityId; + } + + sptr GetRemoteObject() const + { + return currRemoteObject; + } +private: + int32_t currSystemAbilityId = 0; + sptr currRemoteObject; +}; } #endif /* SAMGR_SERVICES_SAMGR_NATIVE_TEST_UNITTEST_INCLUDE_SA_STATUS_CHANGE_MOCK_H */ diff --git a/services/samgr/native/test/unittest/src/ondemand_helper.cpp b/services/samgr/native/test/unittest/src/ondemand_helper.cpp new file mode 100644 index 0000000000000000000000000000000000000000..1f61c8e5101929553fbb025a3d6db7f928bf054f --- /dev/null +++ b/services/samgr/native/test/unittest/src/ondemand_helper.cpp @@ -0,0 +1,116 @@ +/* + * Copyright (c) 2022 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 "ondemand_helper.h" + +#include +#include "datetime_ex.h" +#include "if_system_ability_manager.h" +#include "iservice_registry.h" +#include "system_ability_definition.h" + +using namespace OHOS; +using namespace std; + +namespace OHOS { +OnDemandHelper::OnDemandHelper() +{ + loadCallback_ = new OnDemandLoadCallback(); +} + +OnDemandHelper& OnDemandHelper::GetInstance() +{ + static OnDemandHelper instance; + return instance; +} + +int32_t OnDemandHelper::OnDemandAbility(int32_t systemAbilityId) +{ + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "GetSystemAbilityManager samgr object null!" << endl; + return ERR_NULL_OBJECT; + } + int32_t result = sm->LoadSystemAbility(systemAbilityId, loadCallback_); + if (result != ERR_OK) { + cout << "systemAbilityId:" << systemAbilityId << " load failed, result code:" << result << endl; + return result; + } + return ERR_OK; +} + +sptr OnDemandHelper::GetSystemAbility(int32_t systemAbilityId) +{ + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + cout << "GetSystemAbilityManager samgr object null!" << endl; + return nullptr; + } + sptr remoteObject = sm->GetSystemAbility(systemAbilityId); + if (remoteObject == nullptr) { + cout << "GetSystemAbility systemAbilityId:" << systemAbilityId << " failed !" << endl; + return nullptr; + } + return remoteObject; +} + +void OnDemandHelper::OnLoadSystemAbility(int32_t systemAbilityId) +{ +} + +void OnDemandHelper::OnDemandLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + cout << "OnLoadSystemAbilitySuccess systemAbilityId:" << systemAbilityId << " IRemoteObject result:" << + ((remoteObject != nullptr) ? "succeed" : "failed") << endl; + OnDemandHelper::GetInstance().OnLoadSystemAbility(systemAbilityId); +} +} + +int main(int argc, char* argv[]) +{ + OHOS::OnDemandHelper& ondemandHelper = OnDemandHelper::GetInstance(); + string cmd = "load"; + do { + if (cmd != "load" && cmd != "get") { + cmd = "load"; + } + int32_t systemAbilityId = 0; + cout << "please input systemAbilityId for " << cmd << " operation" << endl; + cin >> systemAbilityId; + if (systemAbilityId < FIRST_SYS_ABILITY_ID || systemAbilityId > LAST_SYS_ABILITY_ID) { + cout << "input systemAbilityId:" << systemAbilityId << " invalid!" << endl; + break; + } + int64_t begin = GetTickCount(); + if (cmd == "get") { + auto remoteObject = ondemandHelper.GetSystemAbility(systemAbilityId); + cout << "GetSystemAbility result:" << ((remoteObject != nullptr) ? "succeed" : "failed") << " spend:" + << (GetTickCount() - begin) << " ms" << " systemAbilityId:" << systemAbilityId << endl; + } else { + int32_t ret = ondemandHelper.OnDemandAbility(systemAbilityId); + cout << "LoadSystemAbility result:" << ret << " spend:" << (GetTickCount() - begin) << " ms" + << " systemAbilityId:" << systemAbilityId << endl; + } + + cout << "-----Input q or Q to quit, [load] for LoadSystemAbility, [get] for GetSystemAbility-----" << endl; + cmd.clear(); + cin >> cmd; + cin.clear(); + cin.ignore(numeric_limits::max(), '\n'); + } while (cmd[0] != 'q' && cmd[0] != 'Q'); + cout << "see you, bye!" << endl; + return 0; +} diff --git a/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp b/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp index bd2b678b2ffcc0c5c5c69fe8e763246d5d7c117c..2298938fd5ddf1492d0c0a5c0758c3be883439c7 100755 --- a/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp +++ b/services/samgr/native/test/unittest/src/sa_status_change_mock.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -27,4 +27,12 @@ void SaStatusChangeMock::OnRemoveSystemAbility(int32_t systemAbilityId, const st { HILOGI("OnRemoveSystemAbility systemAbilityId:%{public}d removed!", systemAbilityId); } + +void SystemAbilityLoadCallbackMock::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, + const sptr& remoteObject) +{ + HILOGI("OnLoadSystemAbilitySuccess systemAbilityId:%{public}d loaded!", systemAbilityId); + currSystemAbilityId = systemAbilityId; + currRemoteObject = remoteObject; +} } diff --git a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp index ea5b88a7070798f5b1dc8b223054c8a88fee3954..fb7adb199aa7c1386c0ec2462627c5eb545a0838 100755 --- a/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp +++ b/services/samgr/native/test/unittest/src/system_ability_mgr_test.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2021-2022 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 @@ -300,7 +300,7 @@ HWTEST_F(SystemAbilityMgrTest, CheckOnDemandSystemAbility001, TestSize.Level1) EXPECT_TRUE(sm != nullptr); sm->AddSystemAbility(systemAbilityId, new TestTransactionService()); int32_t ret = sm->AddOnDemandSystemAbilityInfo(systemAbilityId, u"test_localmanagername"); - EXPECT_EQ(ret, ERR_OK); + EXPECT_TRUE(ret != ERR_OK); sm->RemoveSystemAbility(systemAbilityId); } @@ -320,7 +320,7 @@ HWTEST_F(SystemAbilityMgrTest, CheckOnDemandSystemAbility002, TestSize.Level1) EXPECT_EQ(result, ERR_OK); sptr saObject = sm->CheckSystemAbility(systemAbilityId); result = sm->AddOnDemandSystemAbilityInfo(systemAbilityId, u"just_test"); - EXPECT_EQ(result, ERR_OK); + EXPECT_TRUE(result != ERR_OK); sm->RemoveSystemAbility(systemAbilityId); } @@ -342,4 +342,339 @@ HWTEST_F(SystemAbilityMgrTest, ListSystemAbility001, TestSize.Level1) EXPECT_TRUE(iter != saList.end()); sm->RemoveSystemAbility(systemAbilityId); } + +/** + * @tc.name: LoadSystemAbility001 + * @tc.desc: load system ability with invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility001, TestSize.Level0) +{ + int32_t systemAbilityId = TEST_EXCEPTION_LOW_SA_ID; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sm != nullptr); + int32_t result = sm->LoadSystemAbility(systemAbilityId, nullptr); + EXPECT_TRUE(result != ERR_OK); +} + +/** + * @tc.name: LoadSystemAbility002 + * @tc.desc: load system ability with invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility002, TestSize.Level0) +{ + int32_t systemAbilityId = TEST_EXCEPTION_HIGH_SA_ID; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sm != nullptr); + int32_t result = sm->LoadSystemAbility(systemAbilityId, nullptr); + EXPECT_TRUE(result != ERR_OK); +} + +/** + * @tc.name: LoadSystemAbility003 + * @tc.desc: load system ability with invalid callback. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility003, TestSize.Level0) +{ + int32_t systemAbilityId = DISTRIBUTED_SCHED_TEST_SO_ID; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sm != nullptr); + int32_t result = sm->LoadSystemAbility(systemAbilityId, nullptr); + EXPECT_TRUE(result != ERR_OK); +} + +/** + * @tc.name: LoadSystemAbility004 + * @tc.desc: load system ability with not exist systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility004, TestSize.Level0) +{ + int32_t systemAbilityId = DISTRIBUTED_SCHED_TEST_SO_ID; + sptr sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + EXPECT_TRUE(sm != nullptr); + sptr callback = new SystemAbilityLoadCallbackMock(); + int32_t result = sm->LoadSystemAbility(systemAbilityId, callback); + EXPECT_TRUE(result != ERR_OK); +} + +/** + * @tc.name: LoadSystemAbility005 + * @tc.desc: test OnRemoteRequest, invalid interface token. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility005, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + MessageParcel data; + MessageParcel reply; + MessageOption option; + int32_t result = saMgr->OnRemoteRequest(ISystemAbilityManager::LOAD_SYSTEM_ABILITY_TRANSACTION, + data, reply, option); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: LoadSystemAbility006 + * @tc.desc: test OnRemoteRequest, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility006, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + MessageParcel data; + data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN); + MessageParcel reply; + MessageOption option; + int32_t result = saMgr->OnRemoteRequest(ISystemAbilityManager::LOAD_SYSTEM_ABILITY_TRANSACTION, + data, reply, option); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: LoadSystemAbility007 + * @tc.desc: test OnRemoteRequest, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility007, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + MessageParcel data; + data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN); + data.WriteInt32(TEST_EXCEPTION_HIGH_SA_ID); + MessageParcel reply; + MessageOption option; + int32_t result = saMgr->OnRemoteRequest(ISystemAbilityManager::LOAD_SYSTEM_ABILITY_TRANSACTION, + data, reply, option); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: LoadSystemAbility008 + * @tc.desc: test OnRemoteRequest, null callback. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility008, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + MessageParcel data; + data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN); + data.WriteInt32(DISTRIBUTED_SCHED_TEST_SO_ID); + MessageParcel reply; + MessageOption option; + int32_t result = saMgr->OnRemoteRequest(ISystemAbilityManager::LOAD_SYSTEM_ABILITY_TRANSACTION, + data, reply, option); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: LoadSystemAbility009 + * @tc.desc: test OnRemoteRequest, not exist systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, LoadSystemAbility009, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + MessageParcel data; + data.WriteInterfaceToken(SAMANAGER_INTERFACE_TOKEN); + data.WriteInt32(DISTRIBUTED_SCHED_TEST_SO_ID); + sptr callback = new SystemAbilityLoadCallbackMock(); + data.WriteRemoteObject(callback->AsObject()); + MessageParcel reply; + MessageOption option; + int32_t result = saMgr->OnRemoteRequest(ISystemAbilityManager::LOAD_SYSTEM_ABILITY_TRANSACTION, + data, reply, option); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: OnRemoteDied001 + * @tc.desc: test OnRemoteDied, remove registered callback. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, OnRemoteDied001, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + sptr callback = new SystemAbilityLoadCallbackMock(); + saMgr->OnAbilityCallbackDied(callback->AsObject()); + EXPECT_TRUE(saMgr->startingAbilityMap_.empty()); +} + +/** + * @tc.name: StartOnDemandAbility001 + * @tc.desc: test StartOnDemandAbility, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, StartOnDemandAbility001, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->StartOnDemandAbility(TEST_EXCEPTION_LOW_SA_ID); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: StartOnDemandAbility002 + * @tc.desc: test StartOnDemandAbility, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, StartOnDemandAbility002, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->StartOnDemandAbility(TEST_EXCEPTION_HIGH_SA_ID); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: StartOnDemandAbility003 + * @tc.desc: test StartOnDemandAbility, not exist systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, StartOnDemandAbility003, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->StartOnDemandAbility(DISTRIBUTED_SCHED_TEST_SO_ID); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: StartOnDemandAbility004 + * @tc.desc: test StartOnDemandAbility, not on-demand systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, StartOnDemandAbility004, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->StartOnDemandAbility(DISTRIBUTED_SCHED_SA_ID); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: AddOnDemandSystemAbilityInfo001 + * @tc.desc: test AddOnDemandSystemAbilityInfo, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, AddOnDemandSystemAbilityInfo001, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->AddOnDemandSystemAbilityInfo(TEST_EXCEPTION_LOW_SA_ID, u""); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: AddOnDemandSystemAbilityInfo002 + * @tc.desc: test AddOnDemandSystemAbilityInfo, invalid systemAbilityId. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, AddOnDemandSystemAbilityInfo002, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->AddOnDemandSystemAbilityInfo(TEST_EXCEPTION_HIGH_SA_ID, u""); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: AddOnDemandSystemAbilityInfo003 + * @tc.desc: test AddOnDemandSystemAbilityInfo, invalid procName. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, AddOnDemandSystemAbilityInfo003, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->AddOnDemandSystemAbilityInfo(DISTRIBUTED_SCHED_TEST_SO_ID, u""); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: AddOnDemandSystemAbilityInfo004 + * @tc.desc: test AddOnDemandSystemAbilityInfo, procName not registered. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, AddOnDemandSystemAbilityInfo004, TestSize.Level0) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + int32_t result = saMgr->AddOnDemandSystemAbilityInfo(DISTRIBUTED_SCHED_TEST_SO_ID, u"fake_process_name"); + EXPECT_TRUE(result != ERR_NONE); +} + +/** + * @tc.name: OnLoadSystemAbilitySuccess001 + * @tc.desc: test OnLoadSystemAbilitySuccess, null callback. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, OnLoadSystemAbilitySuccess001, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + sptr callback = new SystemAbilityLoadCallbackMock(); + saMgr->NotifySystemAbilityLoaded(DISTRIBUTED_SCHED_TEST_SO_ID, nullptr, nullptr); + EXPECT_TRUE(callback->GetSystemAbilityId() == 0); +} + +/** + * @tc.name: OnLoadSystemAbilitySuccess002 + * @tc.desc: test OnLoadSystemAbilitySuccess, null IRemoteObject. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, OnLoadSystemAbilitySuccess002, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + sptr callback = new SystemAbilityLoadCallbackMock(); + saMgr->NotifySystemAbilityLoaded(DISTRIBUTED_SCHED_TEST_SO_ID, nullptr, callback); + EXPECT_TRUE(callback->GetSystemAbilityId() == DISTRIBUTED_SCHED_TEST_SO_ID); + EXPECT_TRUE(callback->GetRemoteObject() == nullptr); +} + +/** + * @tc.name: OnLoadSystemAbilitySuccess003 + * @tc.desc: test OnLoadSystemAbilitySuccess. + * @tc.type: FUNC + * @tc.require: #I4OH9B + */ +HWTEST_F(SystemAbilityMgrTest, OnLoadSystemAbilitySuccess003, TestSize.Level1) +{ + sptr saMgr = SystemAbilityManager::GetInstance(); + EXPECT_TRUE(saMgr != nullptr); + sptr callback = new SystemAbilityLoadCallbackMock(); + sptr remoteObject = new TestTransactionService(); + saMgr->NotifySystemAbilityLoaded(DISTRIBUTED_SCHED_TEST_SO_ID, remoteObject, callback); + EXPECT_TRUE(callback->GetSystemAbilityId() == DISTRIBUTED_SCHED_TEST_SO_ID); + EXPECT_TRUE(callback->GetRemoteObject() == remoteObject); +} } // namespace OHOS