diff --git a/frameworks/native/ability/native/ability_thread.cpp b/frameworks/native/ability/native/ability_thread.cpp index e03e2d87b3..0d9c27a579 100644 --- a/frameworks/native/ability/native/ability_thread.cpp +++ b/frameworks/native/ability/native/ability_thread.cpp @@ -88,10 +88,11 @@ void AbilityThread::AbilityThreadMain(const std::shared_ptr &ap TAG_LOGD(AAFwkTag::ABILITY, "end"); } -void AbilityThread::ScheduleAbilityTransaction( +bool AbilityThread::ScheduleAbilityTransaction( const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo) { TAG_LOGD(AAFwkTag::ABILITY, "called"); + return true; } void AbilityThread::ScheduleShareData(const int32_t &requestCode) diff --git a/frameworks/native/ability/native/extension_ability_thread.cpp b/frameworks/native/ability/native/extension_ability_thread.cpp index a7b6eb37f6..e59686c3f2 100644 --- a/frameworks/native/ability/native/extension_ability_thread.cpp +++ b/frameworks/native/ability/native/extension_ability_thread.cpp @@ -373,7 +373,7 @@ void ExtensionAbilityThread::HandleExtensionUpdateConfiguration(const AppExecFwk TAG_LOGD(AAFwkTag::EXT, "End"); } -void ExtensionAbilityThread::ScheduleAbilityTransaction( +bool ExtensionAbilityThread::ScheduleAbilityTransaction( const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo, sptr sessionInfo) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -381,11 +381,11 @@ void ExtensionAbilityThread::ScheduleAbilityTransaction( want.GetElement().GetAbilityName().c_str(), lifeCycleStateInfo.state, lifeCycleStateInfo.isNewWant); if (token_ == nullptr) { TAG_LOGE(AAFwkTag::EXT, "null token_"); - return; + return false; } if (abilityHandler_ == nullptr) { TAG_LOGE(AAFwkTag::EXT, "null abilityHandler_"); - return; + return false; } wptr weak = this; auto task = [weak, want, lifeCycleStateInfo, sessionInfo]() { @@ -399,7 +399,9 @@ void ExtensionAbilityThread::ScheduleAbilityTransaction( bool ret = abilityHandler_->PostTask(task, AppExecFwk::EventQueue::Priority::HIGH); if (!ret) { TAG_LOGE(AAFwkTag::EXT, "PostTask error"); + return false; } + return true; } void ExtensionAbilityThread::ScheduleConnectAbility(const Want &want) diff --git a/frameworks/native/ability/native/fa_ability_thread.cpp b/frameworks/native/ability/native/fa_ability_thread.cpp index 75166a0c06..1231a68e26 100644 --- a/frameworks/native/ability/native/fa_ability_thread.cpp +++ b/frameworks/native/ability/native/fa_ability_thread.cpp @@ -710,7 +710,7 @@ void FAAbilityThread::HandleExtensionUpdateConfiguration(const AppExecFwk::Confi extensionImpl_->ScheduleUpdateConfiguration(config); } -void FAAbilityThread::ScheduleAbilityTransaction( +bool FAAbilityThread::ScheduleAbilityTransaction( const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo, sptr sessionInfo) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -723,11 +723,11 @@ void FAAbilityThread::ScheduleAbilityTransaction( if (token_ == nullptr) { TAG_LOGE(AAFwkTag::FA, "null token_"); - return; + return false; } if (abilityHandler_ == nullptr) { TAG_LOGE(AAFwkTag::FA, "null abilityHandler_"); - return; + return false; } wptr weak = this; auto task = [weak, want, lifeCycleStateInfo, sessionInfo]() { @@ -747,7 +747,9 @@ void FAAbilityThread::ScheduleAbilityTransaction( bool ret = abilityHandler_->PostTask(task, "FAAbilityThread:AbilityTransaction"); if (!ret) { TAG_LOGE(AAFwkTag::FA, "PostTask error"); + return false; } + return true; } void FAAbilityThread::ScheduleShareData(const int32_t &uniqueId) diff --git a/frameworks/native/ability/native/ui_ability_thread.cpp b/frameworks/native/ability/native/ui_ability_thread.cpp index 23a64db970..ded519166e 100644 --- a/frameworks/native/ability/native/ui_ability_thread.cpp +++ b/frameworks/native/ability/native/ui_ability_thread.cpp @@ -306,7 +306,7 @@ void UIAbilityThread::HandleUpdateConfiguration(const AppExecFwk::Configuration abilityImpl_->ScheduleUpdateConfiguration(config); } -void UIAbilityThread::ScheduleAbilityTransaction( +bool UIAbilityThread::ScheduleAbilityTransaction( const Want &want, const LifeCycleStateInfo &lifeCycleStateInfo, sptr sessionInfo) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -319,11 +319,11 @@ void UIAbilityThread::ScheduleAbilityTransaction( if (token_ == nullptr) { TAG_LOGE(AAFwkTag::UIABILITY, "null token_"); - return; + return false; } if (abilityHandler_ == nullptr) { TAG_LOGE(AAFwkTag::UIABILITY, "null abilityHandler_"); - return; + return false; } wptr weak = this; auto task = [weak, want, lifeCycleStateInfo, sessionInfo]() { @@ -338,7 +338,9 @@ void UIAbilityThread::ScheduleAbilityTransaction( bool ret = abilityHandler_->PostTask(task, "UIAbilityThread:AbilityTransaction"); if (!ret) { TAG_LOGE(AAFwkTag::UIABILITY, "postTask error"); + return false; } + return true; } void UIAbilityThread::ScheduleShareData(const int32_t &uniqueId) diff --git a/frameworks/native/appkit/app/main_thread.cpp b/frameworks/native/appkit/app/main_thread.cpp index 716a5e6b7e..24916203db 100644 --- a/frameworks/native/appkit/app/main_thread.cpp +++ b/frameworks/native/appkit/app/main_thread.cpp @@ -410,7 +410,7 @@ std::shared_ptr MainThread::GetMainHandler() const * @brief Schedule the foreground lifecycle of application. * */ -void MainThread::ScheduleForegroundApplication() +bool MainThread::ScheduleForegroundApplication() { HITRACE_METER_NAME(HITRACE_TAG_APP, __PRETTY_FUNCTION__); TAG_LOGD(AAFwkTag::APPKIT, "called"); @@ -429,10 +429,10 @@ void MainThread::ScheduleForegroundApplication() auto tmpWatchdog = watchdog_; if (tmpWatchdog == nullptr) { TAG_LOGE(AAFwkTag::APPKIT, "Watch dog is nullptr."); - return; + } else { + tmpWatchdog->SetBackgroundStatus(false); } - tmpWatchdog->SetBackgroundStatus(false); - tmpWatchdog = nullptr; + return true; } /** diff --git a/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h b/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h index f3e0df40b5..a970ec86a0 100644 --- a/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h +++ b/interfaces/inner_api/ability_manager/include/ability_scheduler_interface.h @@ -52,7 +52,7 @@ public: * @param targetState, The lifecycle state to be transformed * @param sessionInfo, The session info */ - virtual void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, + virtual bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo = nullptr) = 0; /* @@ -389,7 +389,7 @@ public: DUMP_ABILITY_RUNNER_INNER, SCHEDULE_CALL, - + SCHEDULE_SHARE_DATA, // ipc id for scheduling service ability to prepare terminate (30) diff --git a/interfaces/inner_api/ability_manager/include/ability_state.h b/interfaces/inner_api/ability_manager/include/ability_state.h index 73f535bf13..d62ac7ebb3 100644 --- a/interfaces/inner_api/ability_manager/include/ability_state.h +++ b/interfaces/inner_api/ability_manager/include/ability_state.h @@ -74,6 +74,11 @@ enum class AbilityLoadState: uint8_t { LOADED, FAILED }; + +enum class FreezeStrategy : uint8_t { + PRINT_FREEZE_LOG, + NOTIFY_FREEZE_MGR, +}; } // namespace AAFwk } // namespace OHOS #endif // OHOS_ABILITY_RUNTIME_ABILITY_STATE_H diff --git a/interfaces/inner_api/app_manager/BUILD.gn b/interfaces/inner_api/app_manager/BUILD.gn index 3881341c36..028d740075 100644 --- a/interfaces/inner_api/app_manager/BUILD.gn +++ b/interfaces/inner_api/app_manager/BUILD.gn @@ -61,6 +61,9 @@ ohos_shared_library("app_manager") { "src/appmgr/app_debug_info.cpp", "src/appmgr/app_debug_listener_proxy.cpp", "src/appmgr/app_debug_listener_stub.cpp", + "src/appmgr/app_exception_callback_proxy.cpp", + "src/appmgr/app_exception_callback_stub.cpp", + "src/appmgr/app_exception_manager.cpp", "src/appmgr/app_foreground_state_observer_proxy.cpp", "src/appmgr/app_foreground_state_observer_stub.cpp", "src/appmgr/app_jsheap_mem_info.cpp", diff --git a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h index 2027c42e7d..9dfdb6d763 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_interface.h @@ -443,6 +443,8 @@ public: return false; } + virtual void SetAppExceptionCallback(sptr callback) {} + enum class Message { LOAD_ABILITY = 0, TERMINATE_ABILITY, @@ -496,6 +498,7 @@ public: FORCE_KILL_APPLICATION_BY_ACCESS_TOKEN_ID = 49, IS_PROCESS_ATTACHED, IS_APP_KILLING, + SET_APP_EXCEPTION_CALLBACK, // Add enumeration values above END }; diff --git a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h index f1b3db5105..134259b322 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_proxy.h @@ -390,6 +390,8 @@ public: virtual bool IsAppKilling(sptr token) override; + virtual void SetAppExceptionCallback(sptr callback) override; + private: bool WriteInterfaceToken(MessageParcel &data); int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); diff --git a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_stub.h b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_stub.h index ffacfd0dbf..6f6db7594b 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_stub.h +++ b/interfaces/inner_api/app_manager/include/appmgr/ams_mgr_stub.h @@ -102,6 +102,7 @@ private: int32_t HandleIsProcessContainsOnlyUIAbility(MessageParcel &data, MessageParcel &reply); int32_t HandleIsProcessAttached(MessageParcel &data, MessageParcel &reply); int32_t HandleIsAppKilling(MessageParcel &data, MessageParcel &reply); + int32_t HandleSetAppExceptionCallback(MessageParcel &data, MessageParcel &reply); DISALLOW_COPY_AND_MOVE(AmsMgrStub); }; } // namespace AppExecFwk diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_proxy.h new file mode 100644 index 0000000000..16b571960b --- /dev/null +++ b/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_proxy.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_PROXY_H +#define OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_PROXY_H + +#include "iapp_exception_callback.h" +#include "iremote_proxy.h" + +namespace OHOS { +namespace AppExecFwk { +class AppExceptionCallbackProxy : public IRemoteProxy { +public: + explicit AppExceptionCallbackProxy(const sptr &impl); + virtual ~AppExceptionCallbackProxy() = default; + + /** + * Notify abilityManager lifecycle exception. + * + * @param type lifecycle failed type + * @param token associated ability + */ + virtual void OnLifecycleException(LifecycleException type, sptr token); +private: + bool WriteInterfaceToken(MessageParcel &data); + static inline BrokerDelegator delegator_; + int32_t SendTransactCmd(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_PROXY_H diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_stub.h b/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_stub.h new file mode 100644 index 0000000000..686fe9a38e --- /dev/null +++ b/interfaces/inner_api/app_manager/include/appmgr/app_exception_callback_stub.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_STUB_H +#define OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_STUB_H + +#include "iapp_exception_callback.h" +#include "iremote_stub.h" +#include "nocopyable.h" + +namespace OHOS { +namespace AppExecFwk { +class AppExceptionCallbackStub : public IRemoteStub { +public: + AppExceptionCallbackStub() = default; + virtual ~AppExceptionCallbackStub() = default; + + virtual int32_t OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; +private: + int32_t HandleLifecycleException(MessageParcel &data, MessageParcel &reply); + + DISALLOW_COPY_AND_MOVE(AppExceptionCallbackStub); +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_APP_EXCEPTION_CALLBACK_STUB_H diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_exception_manager.h b/interfaces/inner_api/app_manager/include/appmgr/app_exception_manager.h new file mode 100644 index 0000000000..126eecdb60 --- /dev/null +++ b/interfaces/inner_api/app_manager/include/appmgr/app_exception_manager.h @@ -0,0 +1,43 @@ +/* + * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_APP_EXCEPTION_MANAGER_H +#define OHOS_ABILITY_RUNTIME_APP_EXCEPTION_MANAGER_H + +#include "iapp_exception_callback.h" + +namespace OHOS { +namespace AppExecFwk { +class AppExceptionManager { +public: + static AppExceptionManager &GetInstance(); + AppExceptionManager(AppExceptionManager &) = delete; + void operator=(AppExceptionManager &) = delete; + + void LaunchAbilityFailed(sptr token, const std::string &msg); + void ForegroundAppFailed(sptr token, const std::string &msg); + void ForegroundAppWait(sptr token, const std::string &msg); + + void NotifyLifecycleException(LifecycleException type, sptr token); + void SetExceptionCallback(sptr exceptionCallback); + sptr GetExceptionCallback() const; +private: + AppExceptionManager() = default; + mutable std::mutex exceptionCallbackMutex_; + sptr exceptionCallback_; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_APP_EXCEPTION_MANAGER_H diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_interface.h index 71176612d9..6bef043e4a 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_interface.h @@ -39,7 +39,7 @@ public: * * @return */ - virtual void ScheduleForegroundApplication() = 0; + virtual bool ScheduleForegroundApplication() = 0; /** * ScheduleBackgroundApplication, call ScheduleBackgroundApplication() through proxy project, diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_proxy.h index ed481f8222..33e47f5346 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_scheduler_proxy.h @@ -34,7 +34,7 @@ public: * * @return */ - virtual void ScheduleForegroundApplication() override; + virtual bool ScheduleForegroundApplication() override; /** * ScheduleBackgroundApplication, call ScheduleBackgroundApplication() through proxy project, diff --git a/interfaces/inner_api/app_manager/include/appmgr/iapp_exception_callback.h b/interfaces/inner_api/app_manager/include/appmgr/iapp_exception_callback.h new file mode 100644 index 0000000000..40e2355f6e --- /dev/null +++ b/interfaces/inner_api/app_manager/include/appmgr/iapp_exception_callback.h @@ -0,0 +1,49 @@ +/* + * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_I_APP_EXCEPTION_CALLBACK_H +#define OHOS_ABILITY_RUNTIME_I_APP_EXCEPTION_CALLBACK_H + +#include "iremote_broker.h" +#include "iremote_object.h" + +namespace OHOS { +namespace AppExecFwk { +enum class LifecycleException { + LAUNCH_ABILITY_FAIL, + FOREGROUND_APP_FAIL, + FOREGROUND_APP_WAIT, + END +}; + +class IAppExceptionCallback : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"ohos.appexecfwk.AppExceptionCallback"); + + /** + * Notify abilityManager lifecycle exception. + * + * @param type lifecycle failed type + * @param token associated ability + */ + virtual void OnLifecycleException(LifecycleException type, sptr token) {} + + enum class Message { + LIFECYCLE_EXCEPTION_MSG_ID = 0, + }; +}; +} // namespace AppExecFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_I_APP_EXCEPTION_CALLBACK_H diff --git a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp index de947b7dbd..a5d188377e 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_proxy.cpp @@ -1320,5 +1320,27 @@ bool AmsMgrProxy::IsAppKilling(sptr token) } return reply.ReadBool(); } + +void AmsMgrProxy::SetAppExceptionCallback(sptr callback) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option; + if (!WriteInterfaceToken(data)) { + TAG_LOGE(AAFwkTag::APPMGR, "Write interface token failed."); + return; + } + if (!data.WriteRemoteObject(callback.GetRefPtr())) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to write callback"); + return; + } + + auto ret = SendTransactCmd(static_cast(IAmsMgr::Message::SET_APP_EXCEPTION_CALLBACK), + data, reply, option); + if (ret != NO_ERROR) { + TAG_LOGE(AAFwkTag::APPMGR, "Send request failed, error code is %{public}d.", ret); + return; + } +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp index a8967ecbb2..9fb792e73d 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/ams_mgr_stub.cpp @@ -855,5 +855,12 @@ int32_t AmsMgrStub::HandleIsAppKilling(MessageParcel &data, MessageParcel &reply } return NO_ERROR; } + +int32_t AmsMgrStub::HandleSetAppExceptionCallback(MessageParcel &data, MessageParcel &reply) +{ + sptr callback = data.ReadRemoteObject(); + SetAppExceptionCallback(callback); + return NO_ERROR; +} } // namespace AppExecFwk } // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_proxy.cpp new file mode 100644 index 0000000000..e23d723ae1 --- /dev/null +++ b/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_proxy.cpp @@ -0,0 +1,85 @@ +/* + * Copyright (c) 2024 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 "app_exception_callback_proxy.h" + +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { +AppExceptionCallbackProxy::AppExceptionCallbackProxy(const sptr &impl) + : IRemoteProxy(impl) {} + +bool AppExceptionCallbackProxy::WriteInterfaceToken(MessageParcel &data) +{ + if (!data.WriteInterfaceToken(AppExceptionCallbackProxy::GetDescriptor())) { + TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed"); + return false; + } + return true; +} + +int32_t AppExceptionCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data, + MessageParcel &reply, MessageOption &option) +{ + sptr remote = Remote(); + if (remote == nullptr) { + TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr."); + return ERR_NULL_OBJECT; + } + + auto ret = remote->SendRequest(code, data, reply, option); + if (ret != NO_ERROR) { + TAG_LOGE(AAFwkTag::APPMGR, "Send request failed with error code: %{public}d", ret); + return ret; + } + return ret; +} + +void AppExceptionCallbackProxy::OnLifecycleException(LifecycleException type, sptr token) +{ + MessageParcel data; + MessageParcel reply; + MessageOption option(MessageOption::TF_ASYNC); + if (!WriteInterfaceToken(data)) { + return; + } + + int32_t exceptionType = static_cast(type); + if (!data.WriteInt32(exceptionType)) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to write exceptionType"); + return; + } + + if (token) { + if (!data.WriteBool(true) || !data.WriteRemoteObject(token.GetRefPtr())) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag and token"); + return; + } + } else { + if (!data.WriteBool(false)) { + TAG_LOGE(AAFwkTag::APPMGR, "Failed to write flag"); + return; + } + } + + int32_t ret = SendTransactCmd( + static_cast(IAppExceptionCallback::Message::LIFECYCLE_EXCEPTION_MSG_ID), data, reply, option); + if (ret != NO_ERROR) { + TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret); + } +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_stub.cpp new file mode 100644 index 0000000000..10e7b8dc77 --- /dev/null +++ b/interfaces/inner_api/app_manager/src/appmgr/app_exception_callback_stub.cpp @@ -0,0 +1,55 @@ +/* + * Copyright (c) 2024 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 "app_exception_callback_stub.h" + +#include "hilog_tag_wrapper.h" + +namespace OHOS { +namespace AppExecFwk { +int32_t AppExceptionCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + std::u16string descriptor = AppExceptionCallbackStub::GetDescriptor(); + std::u16string remoteDescriptor = data.ReadInterfaceToken(); + if (descriptor != remoteDescriptor) { + TAG_LOGE(AAFwkTag::APPMGR, "local descriptor is not equal to remote"); + return ERR_INVALID_STATE; + } + + switch (code) { + case static_cast(IAppExceptionCallback::Message::LIFECYCLE_EXCEPTION_MSG_ID): + return HandleLifecycleException(data, reply); + default: + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); + } +} + +int32_t AppExceptionCallbackStub::HandleLifecycleException(MessageParcel &data, MessageParcel &reply) +{ + auto type = data.ReadInt32(); + if (type < 0 || type > static_cast(LifecycleException::END)) { + return ERR_INVALID_STATE; + } + auto lifecycleExceptType = static_cast(type); + sptr token; + if (data.ReadBool()) { + token = data.ReadRemoteObject(); + } + OnLifecycleException(lifecycleExceptType, token); + return ERR_OK; +} +} // namespace AppExecFwk +} // namespace OHOS diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_exception_manager.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_exception_manager.cpp new file mode 100644 index 0000000000..164b960207 --- /dev/null +++ b/interfaces/inner_api/app_manager/src/appmgr/app_exception_manager.cpp @@ -0,0 +1,78 @@ +/* + * Copyright (c) 2024 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 "app_exception_manager.h" + +#include "freeze_util.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +using AbilityRuntime::FreezeUtil; +namespace AppExecFwk { +AppExceptionManager &AppExceptionManager::GetInstance() +{ + static AppExceptionManager appExceptionMgr; + return appExceptionMgr; +} + +void AppExceptionManager::LaunchAbilityFailed(sptr token, const std::string &msg) +{ + FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::LOAD}; + FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("LaunchAbilityFailed: " + msg)); + NotifyLifecycleException(LifecycleException::LAUNCH_ABILITY_FAIL, token); +} + +void AppExceptionManager::ForegroundAppFailed(sptr token, const std::string &msg) +{ + FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::FOREGROUND}; + FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("ForegroundAppFailed: " + msg)); + NotifyLifecycleException(LifecycleException::FOREGROUND_APP_FAIL, token); +} + +void AppExceptionManager::ForegroundAppWait(sptr token, const std::string &msg) +{ + FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::FOREGROUND}; + FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("ForegroundAppWait: " + msg)); + NotifyLifecycleException(LifecycleException::FOREGROUND_APP_WAIT, token); +} + +void AppExceptionManager::NotifyLifecycleException(LifecycleException type, sptr token) +{ + auto callback = GetExceptionCallback(); + if (callback != nullptr) { + TAG_LOGI(AAFwkTag::APPMGR, "notify app exception"); + callback->OnLifecycleException(type, token); + } +} + +void AppExceptionManager::SetExceptionCallback(sptr exceptionCallback) +{ + if (exceptionCallback == nullptr) { + TAG_LOGW(AAFwkTag::APPMGR, "callback null"); + } + std::lock_guard lock(exceptionCallbackMutex_); + if (exceptionCallback_ != nullptr) { + TAG_LOGI(AAFwkTag::APPMGR, "inner callback not null"); + } + exceptionCallback_ = exceptionCallback; +} + +sptr AppExceptionManager::GetExceptionCallback() const +{ + std::lock_guard lock(exceptionCallbackMutex_); + return exceptionCallback_; +} +} // namespace AppExecFwk +} // namespace OHOS \ No newline at end of file diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_proxy.cpp index 1bcd18b141..d1e152b725 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_scheduler_proxy.cpp @@ -15,6 +15,7 @@ #include "app_scheduler_proxy.h" +#include "app_exception_manager.h" #include "hilog_tag_wrapper.h" #include "hitrace_meter.h" #include "ipc_types.h" @@ -35,14 +36,14 @@ bool AppSchedulerProxy::WriteInterfaceToken(MessageParcel &data) return true; } -void AppSchedulerProxy::ScheduleForegroundApplication() +bool AppSchedulerProxy::ScheduleForegroundApplication() { TAG_LOGD(AAFwkTag::APPMGR, "AppSchedulerProxy::ScheduleForegroundApplication start"); MessageParcel data; MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { - return; + return false; } int32_t ret = SendTransactCmd(static_cast(IAppScheduler::Message::SCHEDULE_FOREGROUND_APPLICATION_TRANSACTION), @@ -51,7 +52,9 @@ void AppSchedulerProxy::ScheduleForegroundApplication() option); if (ret != NO_ERROR) { TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret); + return false; } + return true; } void AppSchedulerProxy::ScheduleBackgroundApplication() @@ -214,6 +217,8 @@ void AppSchedulerProxy::ScheduleLaunchAbility(const AbilityInfo &info, const spt static_cast(IAppScheduler::Message::SCHEDULE_LAUNCH_ABILITY_TRANSACTION), data, reply, option); if (ret != NO_ERROR) { TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret); + AppExceptionManager::GetInstance().LaunchAbilityFailed(token, std::string("SendRequest is failed") + + std::to_string(ret)); } } diff --git a/interfaces/kits/native/ability/native/ability_thread.h b/interfaces/kits/native/ability/native/ability_thread.h index 3ad1873200..707799a2a2 100644 --- a/interfaces/kits/native/ability/native/ability_thread.h +++ b/interfaces/kits/native/ability/native/ability_thread.h @@ -94,7 +94,7 @@ public: * @param targetState Indicates the lifecycle state. * @param sessionInfo Indicates the session info. */ - void ScheduleAbilityTransaction( + bool ScheduleAbilityTransaction( const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo = nullptr) override; /** diff --git a/interfaces/kits/native/ability/native/extension_ability_thread.h b/interfaces/kits/native/ability/native/extension_ability_thread.h index ba1d30db57..b5eca2c860 100644 --- a/interfaces/kits/native/ability/native/extension_ability_thread.h +++ b/interfaces/kits/native/ability/native/extension_ability_thread.h @@ -63,7 +63,7 @@ public: * @param targetState Indicates the lifecycle state. * @param sessionInfo Indicates the session info. */ - void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, + bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo = nullptr) override; /** diff --git a/interfaces/kits/native/ability/native/fa_ability_thread.h b/interfaces/kits/native/ability/native/fa_ability_thread.h index 0648cb7c11..91854f19be 100644 --- a/interfaces/kits/native/ability/native/fa_ability_thread.h +++ b/interfaces/kits/native/ability/native/fa_ability_thread.h @@ -71,7 +71,7 @@ public: * @param targetState Indicates the lifecycle state. * @param sessionInfo Indicates the session info. */ - void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, + bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo = nullptr) override; /** @@ -563,7 +563,7 @@ private: std::shared_ptr BuildAbilityContext(const std::shared_ptr &abilityInfo, const std::shared_ptr &application, const sptr &token, const std::shared_ptr &stageContext); - + void AddLifecycleEvent(uint32_t state, std::string &methodName) const; std::shared_ptr abilityImpl_; diff --git a/interfaces/kits/native/ability/native/ui_ability_thread.h b/interfaces/kits/native/ability/native/ui_ability_thread.h index f55f5b1298..3b81b5bef8 100644 --- a/interfaces/kits/native/ability/native/ui_ability_thread.h +++ b/interfaces/kits/native/ability/native/ui_ability_thread.h @@ -76,7 +76,7 @@ public: * @param targetState Indicates the lifecycle state. * @param sessionInfo Indicates the session info. */ - void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, + bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &targetState, sptr sessionInfo = nullptr) override; /** diff --git a/interfaces/kits/native/appkit/app/main_thread.h b/interfaces/kits/native/appkit/app/main_thread.h index 67c2c3bf51..76f2150c4d 100644 --- a/interfaces/kits/native/appkit/app/main_thread.h +++ b/interfaces/kits/native/appkit/app/main_thread.h @@ -134,7 +134,7 @@ public: * @brief Schedule the foreground lifecycle of application. * */ - void ScheduleForegroundApplication() override; + bool ScheduleForegroundApplication() override; /** * diff --git a/services/abilitymgr/abilitymgr.gni b/services/abilitymgr/abilitymgr.gni index 5a25dc5581..10689bd7a5 100644 --- a/services/abilitymgr/abilitymgr.gni +++ b/services/abilitymgr/abilitymgr.gni @@ -28,6 +28,7 @@ abilityms_files = [ "src/ability_manager_xcollie.cpp", "src/ability_scheduler_proxy.cpp", "src/ability_token_stub.cpp", + "src/app_exception_handler.cpp", "src/app_scheduler.cpp", "src/app_exit_reason_helper.cpp", "src/assert_fault_callback_death_mgr.cpp", diff --git a/services/abilitymgr/include/ability_record.h b/services/abilitymgr/include/ability_record.h index 4a6550f0df..19322b11a6 100644 --- a/services/abilitymgr/include/ability_record.h +++ b/services/abilitymgr/include/ability_record.h @@ -1118,6 +1118,16 @@ public: return securityFlag_; } + FreezeStrategy GetFreezeStrategy() const + { + return freezeStrategy_; + } + + void SetFreezeStrategy(FreezeStrategy value) + { + freezeStrategy_ = value; + } + protected: void SendEvent(uint32_t msg, uint32_t timeOut, int32_t param = -1, bool isExtension = false); @@ -1343,6 +1353,7 @@ private: LaunchDebugInfo launchDebugInfo_; std::string instanceKey_ = ""; bool securityFlag_ = false; + std::atomic freezeStrategy_{FreezeStrategy::NOTIFY_FREEZE_MGR}; }; } // namespace AAFwk } // namespace OHOS diff --git a/services/abilitymgr/include/ability_scheduler_proxy.h b/services/abilitymgr/include/ability_scheduler_proxy.h index 7ab622d31b..c06ff4deec 100644 --- a/services/abilitymgr/include/ability_scheduler_proxy.h +++ b/services/abilitymgr/include/ability_scheduler_proxy.h @@ -47,7 +47,7 @@ public: * @param Want, Special Want for service type's ability. * @param stateInfo, The lifecycle state to be transformed. */ - void ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo, + bool ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo, sptr sessionInfo = nullptr) override; /* diff --git a/services/abilitymgr/include/app_exception_handler.h b/services/abilitymgr/include/app_exception_handler.h new file mode 100644 index 0000000000..a94f98e376 --- /dev/null +++ b/services/abilitymgr/include/app_exception_handler.h @@ -0,0 +1,38 @@ +/* + * Copyright (c) 2024 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 OHOS_ABILITY_RUNTIME_APP_EXCEPTION_HANDLER_H +#define OHOS_ABILITY_RUNTIME_APP_EXCEPTION_HANDLER_H + +#include + +#include "iremote_object.h" + +namespace OHOS { +namespace AAFwk { +class AppExceptionHandler { +public: + static AppExceptionHandler &GetInstance(); + AppExceptionHandler(AppExceptionHandler &) = delete; + void operator=(AppExceptionHandler &) = delete; + + void RegisterAppExceptionCallback(); + void AbilityForegroundFailed(sptr token, const std::string &msg); +private: + AppExceptionHandler() = default; +}; +} // namespace AAFwk +} // namespace OHOS +#endif // OHOS_ABILITY_RUNTIME_APP_EXCEPTION_HANDLER_H diff --git a/services/abilitymgr/include/lifecycle_deal.h b/services/abilitymgr/include/lifecycle_deal.h index ceb2e4c13a..3945edd8bb 100644 --- a/services/abilitymgr/include/lifecycle_deal.h +++ b/services/abilitymgr/include/lifecycle_deal.h @@ -73,7 +73,7 @@ public: /** * schedule ability life cycle to foreground */ - void ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo, + bool ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo, sptr sessionInfo = nullptr); /** * schedule ability life cycle to background diff --git a/services/abilitymgr/src/ability_manager_service.cpp b/services/abilitymgr/src/ability_manager_service.cpp index 7ae371f7ad..2024cf6f11 100644 --- a/services/abilitymgr/src/ability_manager_service.cpp +++ b/services/abilitymgr/src/ability_manager_service.cpp @@ -21,6 +21,7 @@ #include "ability_resident_process_rdb.h" #include "accesstoken_kit.h" #include "ability_manager_xcollie.h" +#include "app_exception_handler.h" #include "app_utils.h" #include "app_exit_reason_data_manager.h" #include "application_util.h" @@ -3660,8 +3661,8 @@ int AbilityManagerService::CloseUIAbilityBySCB(const sptr &sessionI abilityRecord->GetPid(), true)); } else { IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton::GetInstance()->SetProcessCacheStatus( - abilityRecord->GetPid(), false)); - } + abilityRecord->GetPid(), false)); + } EventInfo eventInfo; eventInfo.bundleName = abilityRecord->GetAbilityInfo().bundleName; eventInfo.abilityName = abilityRecord->GetAbilityInfo().name; @@ -7119,6 +7120,7 @@ void AbilityManagerService::ConnectServices() TAG_LOGE(AAFwkTag::ABILITYMGR, "failed init appScheduler"); usleep(REPOLL_TIME_MICRO_SECONDS); } + AppExceptionHandler::GetInstance().RegisterAppExceptionCallback(); TAG_LOGI(AAFwkTag::ABILITYMGR, "waiting bundleMgr service run completed"); while (AbilityUtil::GetBundleManagerHelper() == nullptr) { @@ -12031,7 +12033,7 @@ int32_t AbilityManagerService::CleanUIAbilityBySCB(const sptr &sess abilityRecord->GetPid(), true)); } else { IN_PROCESS_CALL_WITHOUT_RET(DelayedSingleton::GetInstance()->SetProcessCacheStatus( - abilityRecord->GetPid(), false)); + abilityRecord->GetPid(), false)); } int32_t errCode = uiAbilityManager->CleanUIAbility(abilityRecord, forceKillProcess); ReportCleanSession(sessionInfo, abilityRecord, errCode); @@ -12134,7 +12136,7 @@ void AbilityManagerService::SetAbilityRequestSessionInfo(AbilityRequest &ability auto sceneSessionManager = Rosen::SessionManagerLite::GetInstance(). GetSceneSessionManagerLiteProxy(); CHECK_POINTER_LOG(sceneSessionManager, "sceneSessionManager is nullptr"); - auto err = sceneSessionManager->GetRootMainWindowId(static_cast(callerSessionInfo->hostWindowId),mainWindowId); + auto err = sceneSessionManager->GetRootMainWindowId(static_cast(callerSessionInfo->hostWindowId),mainWindowId); TAG_LOGI(AAFwkTag::ABILITYMGR, "callerSessionInfo->hostWindowId = %{public}d, mainWindowId = %{public}d, err = %{public}d", callerSessionInfo->hostWindowId, mainWindowId, err); abilityRequest.want.SetParam(WANT_PARAMS_HOST_WINDOW_ID_KEY, mainWindowId); diff --git a/services/abilitymgr/src/ability_record.cpp b/services/abilitymgr/src/ability_record.cpp index 306cf5cb1a..9a9d20ea94 100644 --- a/services/abilitymgr/src/ability_record.cpp +++ b/services/abilitymgr/src/ability_record.cpp @@ -20,6 +20,7 @@ #include "ability_manager_service.h" #include "ability_resident_process_rdb.h" #include "ability_scheduler_stub.h" +#include "app_exception_handler.h" #include "app_exit_reason_data_manager.h" #include "app_utils.h" #include "array_wrapper.h" @@ -398,7 +399,9 @@ void AbilityRecord::ForegroundAbility(uint32_t sceneFlag) lifeCycleStateInfo_.sceneFlag = sceneFlag; Want want = GetWant(); UpdateDmsCallerInfo(want); - lifecycleDeal_->ForegroundNew(want, lifeCycleStateInfo_, GetSessionInfo()); + if (!lifecycleDeal_->ForegroundNew(want, lifeCycleStateInfo_, GetSessionInfo()) && token_) { + AppExceptionHandler::GetInstance().AbilityForegroundFailed(token_->AsObject(), "ForegroundNew"); + } lifeCycleStateInfo_.sceneFlag = 0; lifeCycleStateInfo_.sceneFlagBak = 0; { @@ -494,6 +497,7 @@ void AbilityRecord::RemoveForegroundTimeoutTask() CHECK_POINTER(handler); handler->RemoveEvent(AbilityManagerService::FOREGROUND_HALF_TIMEOUT_MSG, GetAbilityRecordId()); handler->RemoveEvent(AbilityManagerService::FOREGROUND_TIMEOUT_MSG, GetAbilityRecordId()); + SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR); } void AbilityRecord::RemoveLoadTimeoutTask() @@ -502,6 +506,7 @@ void AbilityRecord::RemoveLoadTimeoutTask() CHECK_POINTER(handler); handler->RemoveEvent(AbilityManagerService::LOAD_HALF_TIMEOUT_MSG, GetAbilityRecordId()); handler->RemoveEvent(AbilityManagerService::LOAD_TIMEOUT_MSG, GetAbilityRecordId()); + SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR); } void AbilityRecord::PostUIExtensionAbilityTimeoutTask(uint32_t messageId) diff --git a/services/abilitymgr/src/ability_scheduler_proxy.cpp b/services/abilitymgr/src/ability_scheduler_proxy.cpp index 362b8e233f..d10f2da969 100644 --- a/services/abilitymgr/src/ability_scheduler_proxy.cpp +++ b/services/abilitymgr/src/ability_scheduler_proxy.cpp @@ -39,7 +39,7 @@ bool AbilitySchedulerProxy::WriteInterfaceToken(MessageParcel &data) return true; } -void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo, +bool AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const LifeCycleStateInfo &stateInfo, sptr sessionInfo) { HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); @@ -50,26 +50,27 @@ void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const L MessageParcel reply; MessageOption option(MessageOption::TF_ASYNC); if (!WriteInterfaceToken(data)) { - return; + return false; } if (!data.WriteParcelable(&want)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write want failed"); - return; + return false; } data.WriteParcelable(&stateInfo); if (sessionInfo) { if (!data.WriteBool(true) || !data.WriteParcelable(sessionInfo)) { TAG_LOGE(AAFwkTag::ABILITYMGR, "write sessionInfo failed"); - return; + return false; } } else { if (!data.WriteBool(false)) { - return; + return false; } } int32_t err = SendTransactCmd(IAbilityScheduler::SCHEDULE_ABILITY_TRANSACTION, data, reply, option); if (err != NO_ERROR) { TAG_LOGE(AAFwkTag::ABILITYMGR, "fail, err: %{public}d", err); + return false; } int64_t cost = std::chrono::duration_cast( std::chrono::system_clock::now().time_since_epoch()).count() - start; @@ -82,6 +83,7 @@ void AbilitySchedulerProxy::ScheduleAbilityTransaction(const Want &want, const L "ScheduleAbilityTransaction proxy cost %{public}" PRId64 "mirco seconds, data size: %{public}zu", cost, data.GetWritePosition()); } + return true; } void AbilitySchedulerProxy::ScheduleShareData(const int32_t &uniqueId) diff --git a/services/abilitymgr/src/app_exception_handler.cpp b/services/abilitymgr/src/app_exception_handler.cpp new file mode 100644 index 0000000000..af0976f87b --- /dev/null +++ b/services/abilitymgr/src/app_exception_handler.cpp @@ -0,0 +1,88 @@ +/* + * Copyright (c) 2024 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 "app_exception_handler.h" + +#include "ability_record.h" +#include "app_exception_callback_stub.h" +#include "app_mgr_util.h" +#include "freeze_util.h" +#include "hilog_tag_wrapper.h" + +namespace OHOS { +using AppExecFwk::LifecycleException; +using AbilityRuntime::FreezeUtil; +namespace AAFwk { +namespace { +class AppExceptionCallback : public AppExecFwk::AppExceptionCallbackStub { + /** + * Notify abilityManager lifecycle exception. + * + * @param type lifecycle failed type + * @param token associated ability + */ + void OnLifecycleException(LifecycleException type, sptr token) override + { + auto abilityRecord = Token::GetAbilityRecordByToken(token); + if (abilityRecord == nullptr) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null"); + return; + } + + TAG_LOGI(AAFwkTag::ABILITYMGR, "lifecycle exception: %{public}s, %{public}d", + abilityRecord->GetURI().c_str(), type); + abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR); + } +}; +} + +AppExceptionHandler &AppExceptionHandler::GetInstance() +{ + static AppExceptionHandler appExceptionHandler; + return appExceptionHandler; +} + +void AppExceptionHandler::RegisterAppExceptionCallback() +{ + auto appMgr = AppMgrUtil::GetAppMgr(); + if (appMgr == nullptr) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "AppMgrUtil::GetAppMgr failed"); + return; + } + + auto service = appMgr->GetAmsMgr(); + if (service == nullptr) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "GetAmsMgr failed"); + return; + } + auto callback = sptr(new AppExceptionCallback()); + service->SetAppExceptionCallback(callback->AsObject()); +} + +void AppExceptionHandler::AbilityForegroundFailed(sptr token, const std::string &msg) +{ + auto abilityRecord = Token::GetAbilityRecordByToken(token); + if (abilityRecord == nullptr) { + TAG_LOGW(AAFwkTag::ABILITYMGR, "abilityRecord null"); + return; + } + FreezeUtil::LifecycleFlow flow{token, FreezeUtil::TimeoutState::FOREGROUND}; + FreezeUtil::GetInstance().AppendLifecycleEvent(flow, std::string("AbilityForegroundFailed: " + msg)); + + TAG_LOGI(AAFwkTag::ABILITYMGR, "AbilityForegroundFailed: %{public}s", abilityRecord->GetURI().c_str()); + abilityRecord->SetFreezeStrategy(FreezeStrategy::NOTIFY_FREEZE_MGR); +} +} // namespace AAFwk +} // namespace OHOS diff --git a/services/abilitymgr/src/lifecycle_deal.cpp b/services/abilitymgr/src/lifecycle_deal.cpp index 6c3d3fb491..bebca5b6f1 100644 --- a/services/abilitymgr/src/lifecycle_deal.cpp +++ b/services/abilitymgr/src/lifecycle_deal.cpp @@ -126,17 +126,17 @@ void LifecycleDeal::RestoreAbilityState(const PacMap &inState) abilityScheduler->ScheduleRestoreAbilityState(inState); } -void LifecycleDeal::ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo, +bool LifecycleDeal::ForegroundNew(const Want &want, LifeCycleStateInfo &stateInfo, sptr sessionInfo) { TAG_LOGD(AAFwkTag::ABILITYMGR, "call"); auto abilityScheduler = GetScheduler(); - CHECK_POINTER(abilityScheduler); + CHECK_POINTER_AND_RETURN(abilityScheduler, false); TAG_LOGD(AAFwkTag::ABILITYMGR, "caller %{public}s, %{public}s", stateInfo.caller.bundleName.c_str(), stateInfo.caller.abilityName.c_str()); stateInfo.state = AbilityLifeCycleState::ABILITY_STATE_FOREGROUND_NEW; - abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo); + return abilityScheduler->ScheduleAbilityTransaction(want, stateInfo, sessionInfo); } void LifecycleDeal::BackgroundNew(const Want &want, LifeCycleStateInfo &stateInfo, diff --git a/services/abilitymgr/src/mission/mission_list_manager.cpp b/services/abilitymgr/src/mission/mission_list_manager.cpp index d06476031c..688f3a160b 100644 --- a/services/abilitymgr/src/mission/mission_list_manager.cpp +++ b/services/abilitymgr/src/mission/mission_list_manager.cpp @@ -2212,11 +2212,7 @@ void MissionListManager::PostMissionLabelUpdateTask(int missionId) const #endif // SUPPORT_SCREEN void MissionListManager::PrintTimeOutLog(const std::shared_ptr &ability, uint32_t msgId, bool isHalf) { - if (ability == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "ability null"); - return; - } - + CHECK_POINTER_LOG(ability, "ability null"); AppExecFwk::RunningProcessInfo processInfo = {}; DelayedSingleton::GetInstance()->GetRunningProcessInfoByToken(ability->GetToken(), processInfo); if (processInfo.pid_ == 0) { @@ -2234,9 +2230,8 @@ void MissionListManager::PrintTimeOutLog(const std::shared_ptr &a std::string eventName = isHalf ? AppExecFwk::AppFreezeType::LIFECYCLE_HALF_TIMEOUT : AppExecFwk::AppFreezeType::LIFECYCLE_TIMEOUT; - TAG_LOGW(AAFwkTag::ABILITYMGR, - "%{public}s: uid: %{public}d, pid: %{public}d, bundleName: %{public}s, abilityName: %{public}s," - "msg: %{public}s!", + TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s: uid: %{public}d, pid: %{public}d, bundleName: %{public}s, " + "abilityName: %{public}s, msg: %{public}s!", eventName.c_str(), processInfo.uid_, processInfo.pid_, ability->GetAbilityInfo().bundleName.c_str(), ability->GetAbilityInfo().name.c_str(), msgContent.c_str()); @@ -2246,8 +2241,8 @@ void MissionListManager::PrintTimeOutLog(const std::shared_ptr &a .eventName = eventName, .bundleName = ability->GetAbilityInfo().bundleName, }; + FreezeUtil::LifecycleFlow flow; if (state != FreezeUtil::TimeoutState::UNKNOWN) { - FreezeUtil::LifecycleFlow flow; if (ability->GetToken() != nullptr) { flow.token = ability->GetToken()->AsObject(); flow.state = state; @@ -2256,10 +2251,13 @@ void MissionListManager::PrintTimeOutLog(const std::shared_ptr &a if (!isHalf) { FreezeUtil::GetInstance().DeleteLifecycleEvent(flow); } - AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info, flow); } else { info.msg = msgContent; - AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info); + } + if (ability->GetFreezeStrategy() == FreezeStrategy::NOTIFY_FREEZE_MGR) { + AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info, flow); + } else { + TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s", info.msg.c_str()); } } diff --git a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp index 4208ac9ba4..b04bc61ceb 100644 --- a/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp +++ b/services/abilitymgr/src/scene_board/ui_ability_lifecycle_manager.cpp @@ -1100,10 +1100,7 @@ void UIAbilityLifecycleManager::NotifyAbilityToken(const sptr &to void UIAbilityLifecycleManager::PrintTimeOutLog(std::shared_ptr ability, uint32_t msgId, bool isHalf) { - if (ability == nullptr) { - TAG_LOGE(AAFwkTag::ABILITYMGR, "null ability"); - return; - } + CHECK_POINTER_LOG(ability, "null ability"); AppExecFwk::RunningProcessInfo processInfo = {}; DelayedSingleton::GetInstance()->GetRunningProcessInfoByToken(ability->GetToken(), processInfo); if (processInfo.pid_ == 0) { @@ -1120,9 +1117,8 @@ void UIAbilityLifecycleManager::PrintTimeOutLog(std::shared_ptr a std::string eventName = isHalf ? AppExecFwk::AppFreezeType::LIFECYCLE_HALF_TIMEOUT : AppExecFwk::AppFreezeType::LIFECYCLE_TIMEOUT; - TAG_LOGW(AAFwkTag::ABILITYMGR, - "%{public}s: uid: %{public}d, pid: %{public}d, bundleName: %{public}s, abilityName: %{public}s," - "msg: %{public}s", + TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s: uid: %{public}d, pid: %{public}d, bundleName: %{public}s, " + "abilityName: %{public}s, msg: %{public}s", eventName.c_str(), processInfo.uid_, processInfo.pid_, ability->GetAbilityInfo().bundleName.c_str(), ability->GetAbilityInfo().name.c_str(), msgContent.c_str()); @@ -1133,8 +1129,8 @@ void UIAbilityLifecycleManager::PrintTimeOutLog(std::shared_ptr a .bundleName = ability->GetAbilityInfo().bundleName, }; FreezeUtil::TimeoutState state = MsgId2State(msgId); + FreezeUtil::LifecycleFlow flow; if (state != FreezeUtil::TimeoutState::UNKNOWN) { - FreezeUtil::LifecycleFlow flow; if (ability->GetToken() != nullptr) { flow.token = ability->GetToken()->AsObject(); flow.state = state; @@ -1143,10 +1139,13 @@ void UIAbilityLifecycleManager::PrintTimeOutLog(std::shared_ptr a if (!isHalf) { FreezeUtil::GetInstance().DeleteLifecycleEvent(flow); } - AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info, flow); } else { info.msg = msgContent; - AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info); + } + if (ability->GetFreezeStrategy() == FreezeStrategy::NOTIFY_FREEZE_MGR) { + AppExecFwk::AppfreezeManager::GetInstance()->LifecycleTimeoutHandle(info, flow); + } else { + TAG_LOGW(AAFwkTag::ABILITYMGR, "%{public}s", info.msg.c_str()); } } diff --git a/services/appmgr/include/ams_mgr_scheduler.h b/services/appmgr/include/ams_mgr_scheduler.h index 657f287540..89f6663d41 100644 --- a/services/appmgr/include/ams_mgr_scheduler.h +++ b/services/appmgr/include/ams_mgr_scheduler.h @@ -417,6 +417,8 @@ public: virtual bool IsAppKilling(sptr token) override; + virtual void SetAppExceptionCallback(sptr callback) override; + private: /** * @brief Judge whether the application service is ready. diff --git a/services/appmgr/include/app_lifecycle_deal.h b/services/appmgr/include/app_lifecycle_deal.h index 568b26b85f..0800927fc1 100644 --- a/services/appmgr/include/app_lifecycle_deal.h +++ b/services/appmgr/include/app_lifecycle_deal.h @@ -84,9 +84,9 @@ public: * ScheduleForegroundRunning, call ScheduleForegroundApplication() through proxy project, * Notify application to switch to foreground. * - * @return + * @return bool operation status */ - void ScheduleForegroundRunning(); + bool ScheduleForegroundRunning(); /** * ScheduleBackgroundRunning, call ScheduleBackgroundApplication() through proxy project, diff --git a/services/appmgr/include/app_running_record.h b/services/appmgr/include/app_running_record.h index fd3f559b20..69518fc60d 100644 --- a/services/appmgr/include/app_running_record.h +++ b/services/appmgr/include/app_running_record.h @@ -944,9 +944,9 @@ public: /** * ScheduleForegroundRunning, Notify application to switch to foreground. * - * @return + * @return bool operation status */ - void ScheduleForegroundRunning(); + bool ScheduleForegroundRunning(); /** * ScheduleBackgroundRunning, Notify application to switch to background. diff --git a/services/appmgr/src/ams_mgr_scheduler.cpp b/services/appmgr/src/ams_mgr_scheduler.cpp index e0cbe60cbc..fdea701f2e 100644 --- a/services/appmgr/src/ams_mgr_scheduler.cpp +++ b/services/appmgr/src/ams_mgr_scheduler.cpp @@ -22,6 +22,7 @@ #include "accesstoken_kit.h" #include "app_death_recipient.h" +#include "app_exception_manager.h" #include "app_mgr_constants.h" #include "app_utils.h" #include "hilog_tag_wrapper.h" @@ -734,5 +735,25 @@ bool AmsMgrScheduler::IsAppKilling(sptr token) } return amsMgrServiceInner_->IsAppKilling(token); } + +void AmsMgrScheduler::SetAppExceptionCallback(sptr callback) +{ + if (!IsReady()) { + TAG_LOGE(AAFwkTag::APPMGR, "AmsMgrService is not ready."); + return; + } + pid_t callingPid = IPCSkeleton::GetCallingPid(); + pid_t procPid = getprocpid(); + if (callingPid != procPid) { + TAG_LOGE(AAFwkTag::APPMGR, "not allow other process to call"); + return; + } + + if (callback == nullptr) { + TAG_LOGW(AAFwkTag::APPMGR, "callback null"); + } + auto exceptionCallback = iface_cast(callback); + return AppExceptionManager::GetInstance().SetExceptionCallback(exceptionCallback); +} } // namespace AppExecFwk } // namespace OHOS diff --git a/services/appmgr/src/app_lifecycle_deal.cpp b/services/appmgr/src/app_lifecycle_deal.cpp index 37e5deaf51..ce3195eec1 100644 --- a/services/appmgr/src/app_lifecycle_deal.cpp +++ b/services/appmgr/src/app_lifecycle_deal.cpp @@ -98,16 +98,16 @@ void AppLifeCycleDeal::ScheduleTerminate(bool isLastProcess) appThread->ScheduleTerminateApplication(isLastProcess); } -void AppLifeCycleDeal::ScheduleForegroundRunning() +bool AppLifeCycleDeal::ScheduleForegroundRunning() { auto appThread = GetApplicationClient(); if (!appThread) { TAG_LOGE(AAFwkTag::APPMGR, "null appThread"); - return; + return false; } HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__); - appThread->ScheduleForegroundApplication(); + return appThread->ScheduleForegroundApplication(); } void AppLifeCycleDeal::ScheduleBackgroundRunning() diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index eedc88f7b0..35604f55d6 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -1227,13 +1227,14 @@ void AppMgrServiceInner::ApplicationBackgrounded(const int32_t recordId) TAG_LOGW(AAFwkTag::APPMGR, "app name(%{public}s), app state(%{public}d)", appRecord->GetName().c_str(), static_cast(appRecord->GetState())); } - if (appRecord->GetApplicationPendingState() == ApplicationPendingState::FOREGROUNDING) { + auto pendingState = appRecord->GetApplicationPendingState(); + TAG_LOGI(AAFwkTag::APPMGR, "app backgrounded: %{public}s, pState: %{public}d", appRecord->GetBundleName().c_str(), + pendingState); + if (pendingState == ApplicationPendingState::FOREGROUNDING) { appRecord->ScheduleForegroundRunning(); - } else if (appRecord->GetApplicationPendingState() == ApplicationPendingState::BACKGROUNDING) { + } else if (pendingState == ApplicationPendingState::BACKGROUNDING) { appRecord->SetApplicationPendingState(ApplicationPendingState::READY); } - - TAG_LOGI(AAFwkTag::APPMGR, "ApplicationBackgrounded, bundle: %{public}s", appRecord->GetBundleName().c_str()); auto eventInfo = BuildEventInfo(appRecord); AAFwk::EventReport::SendAppBackgroundEvent(AAFwk::EventName::APP_BACKGROUND, eventInfo); } diff --git a/services/appmgr/src/app_running_record.cpp b/services/appmgr/src/app_running_record.cpp index dc5f4e7a96..231603550a 100644 --- a/services/appmgr/src/app_running_record.cpp +++ b/services/appmgr/src/app_running_record.cpp @@ -14,6 +14,7 @@ */ #include "ability_window_configuration.h" +#include "app_exception_manager.h" #include "app_running_record.h" #include "app_mgr_service_inner.h" #include "event_report.h" @@ -661,12 +662,13 @@ void AppRunningRecord::LaunchPendingAbilities() moduleRecord->LaunchPendingAbilities(); } } -void AppRunningRecord::ScheduleForegroundRunning() +bool AppRunningRecord::ScheduleForegroundRunning() { SetApplicationScheduleState(ApplicationScheduleState::SCHEDULE_FOREGROUNDING); if (appLifeCycleDeal_) { - appLifeCycleDeal_->ScheduleForegroundRunning(); + return appLifeCycleDeal_->ScheduleForegroundRunning(); } + return false; } void AppRunningRecord::ScheduleBackgroundRunning() @@ -989,8 +991,8 @@ void AppRunningRecord::AbilityForeground(const std::shared_ptrGetName().c_str()); + TAG_LOGI(AAFwkTag::APPMGR, "appState: %{public}d, pState: %{public}d, bundle: %{public}s, ability: %{public}s", + curState_, pendingState_, mainBundleName_.c_str(), ability->GetName().c_str()); // We need schedule application to foregrounded when current application state is ready or background running. if (curState_ == ApplicationState::APP_STATE_FOREGROUND && pendingState_ != ApplicationPendingState::BACKGROUNDING) { @@ -1010,11 +1012,14 @@ void AppRunningRecord::AbilityForeground(const std::shared_ptrGetToken(), "schedule failed"); + } + } else { + AppExceptionManager::GetInstance().ForegroundAppWait(ability->GetToken(), "pendingState not ready"); } foregroundingAbilityTokens_.insert(ability->GetToken()); TAG_LOGD(AAFwkTag::APPMGR, "foregroundingAbility size: %{public}d", @@ -2061,8 +2066,9 @@ void AppRunningRecord::OnWindowVisibilityChanged( } } + TAG_LOGI(AAFwkTag::APPMGR, "window id empty: %{public}d, pState: %{public}d, cState: %{public}d", + windowIds_.empty(), pendingState_, curState_); if (pendingState_ == ApplicationPendingState::READY) { - TAG_LOGD(AAFwkTag::APPMGR, "pending state is READY."); if (!windowIds_.empty() && curState_ != ApplicationState::APP_STATE_FOREGROUND) { SetApplicationPendingState(ApplicationPendingState::FOREGROUNDING); ScheduleForegroundRunning(); @@ -2072,7 +2078,6 @@ void AppRunningRecord::OnWindowVisibilityChanged( ScheduleBackgroundRunning(); } } else { - TAG_LOGI(AAFwkTag::APPMGR, "not READY"); if (!windowIds_.empty()) { SetApplicationPendingState(ApplicationPendingState::FOREGROUNDING); } diff --git a/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp b/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp index c6cd9e3112..a86db63462 100755 --- a/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp +++ b/test/fuzztest/abilityschedulerstub_fuzzer/abilityschedulerstub_fuzzer.cpp @@ -42,9 +42,11 @@ public: AbilitySchedulerStubFuzzTest() = default; virtual ~AbilitySchedulerStubFuzzTest() {}; - void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, + bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo = nullptr) override - {} + { + return true; + } void ScheduleShareData(const int32_t &uniqueId) override {} void SendResult(int requestCode, int resultCode, const Want& resultWant) override diff --git a/test/fuzztest/attachabilitythread_fuzzer/attachabilitythread_fuzzer.cpp b/test/fuzztest/attachabilitythread_fuzzer/attachabilitythread_fuzzer.cpp index deb9d5af34..21b9710d3b 100755 --- a/test/fuzztest/attachabilitythread_fuzzer/attachabilitythread_fuzzer.cpp +++ b/test/fuzztest/attachabilitythread_fuzzer/attachabilitythread_fuzzer.cpp @@ -39,9 +39,11 @@ public: AbilitySchedulerFuzzTest() = default; virtual ~AbilitySchedulerFuzzTest() {}; - void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, + bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo = nullptr) override - {} + { + return true; + } void ScheduleShareData(const int32_t &uniqueId) override {} void SendResult(int requestCode, int resultCode, const Want& resultWant) override diff --git a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h index d8eedb1507..bb5825c3cf 100644 --- a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h +++ b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_manager_client.h @@ -48,8 +48,11 @@ public: class MockAbilityThread : public IRemoteStub { public: - virtual void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, - sptr sessionInfo = nullptr) {}; + virtual bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, + sptr sessionInfo = nullptr) + { + return true; + } virtual void SendResult(int requestCode, int resultCode, const Want& resultWant) {}; virtual void ScheduleConnectAbility(const Want& want) {}; virtual void ScheduleDisconnectAbility(const Want& want) {}; diff --git a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h index 3e4befc22b..a14300b575 100644 --- a/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h +++ b/test/mock/frameworks_kits_ability_native_test/include/mock_ability_scheduler_for_observer.h @@ -29,7 +29,7 @@ class MockAbilitySchedulerStub : public AAFwk::AbilitySchedulerStub { public: MockAbilitySchedulerStub() = default; virtual ~MockAbilitySchedulerStub() = default; - MOCK_METHOD3(ScheduleAbilityTransaction, void(const AAFwk::Want&, const AAFwk::LifeCycleStateInfo&, + MOCK_METHOD3(ScheduleAbilityTransaction, bool(const AAFwk::Want&, const AAFwk::LifeCycleStateInfo&, sptr)); MOCK_METHOD1(ScheduleShareData, void(const int32_t &uniqueId)); MOCK_METHOD3(SendResult, void(int, int, const AAFwk::Want&)); diff --git a/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h b/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h index d9beef8a9a..4483dee569 100644 --- a/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h +++ b/test/mock/services_abilitymgr_test/libs/aakit/include/ability_scheduler.h @@ -30,7 +30,7 @@ public: AbilityScheduler(); virtual ~AbilityScheduler(); - void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, + bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo = nullptr) override; void ScheduleShareData(const int32_t &uniqueId) override; diff --git a/test/mock/services_abilitymgr_test/libs/aakit/src/ability_scheduler.cpp b/test/mock/services_abilitymgr_test/libs/aakit/src/ability_scheduler.cpp index 7ba8cb175a..5248299037 100644 --- a/test/mock/services_abilitymgr_test/libs/aakit/src/ability_scheduler.cpp +++ b/test/mock/services_abilitymgr_test/libs/aakit/src/ability_scheduler.cpp @@ -25,11 +25,12 @@ AbilityScheduler::AbilityScheduler() AbilityScheduler::~AbilityScheduler() {} -void AbilityScheduler::ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, +bool AbilityScheduler::ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo) { TAG_LOGI(AAFwkTag::TEST, "AbilityScheduler ScheduleAbilityTransaction %d", targetState.state); (void)want; + return true; } void AbilityScheduler::ScheduleShareData(const int32_t &uniqueId) diff --git a/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h b/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h index 477ef3fb33..0e25d1deb7 100644 --- a/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h +++ b/test/mock/services_abilitymgr_test/libs/ability_scheduler_mock/ability_scheduler_mock.h @@ -31,7 +31,7 @@ public: virtual ~AbilitySchedulerMock() {} - MOCK_METHOD3(ScheduleAbilityTransaction, void(const Want&, const LifeCycleStateInfo&, sptr)); + MOCK_METHOD3(ScheduleAbilityTransaction, bool(const Want&, const LifeCycleStateInfo&, sptr)); MOCK_METHOD3(SendResult, void(int, int, const Want&)); MOCK_METHOD1(ScheduleConnectAbility, void(const Want&)); MOCK_METHOD1(ScheduleDisconnectAbility, void(const Want&)); diff --git a/test/mock/services_appmgr_test/include/mock_app_scheduler.h b/test/mock/services_appmgr_test/include/mock_app_scheduler.h index ab2957c17f..fa1b34779e 100644 --- a/test/mock/services_appmgr_test/include/mock_app_scheduler.h +++ b/test/mock/services_appmgr_test/include/mock_app_scheduler.h @@ -29,7 +29,7 @@ class MockAppScheduler : public AppSchedulerHost { public: MockAppScheduler() = default; virtual ~MockAppScheduler() = default; - MOCK_METHOD0(ScheduleForegroundApplication, void()); + MOCK_METHOD0(ScheduleForegroundApplication, bool()); MOCK_METHOD0(ScheduleBackgroundApplication, void()); MOCK_METHOD1(ScheduleTerminateApplication, void(bool)); MOCK_METHOD2(ScheduleLaunchApplication, void(const AppLaunchData&, const Configuration& config)); diff --git a/test/mock/services_appmgr_test/include/mock_app_scheduler_client.h b/test/mock/services_appmgr_test/include/mock_app_scheduler_client.h index 25b14c1724..5139f09bde 100644 --- a/test/mock/services_appmgr_test/include/mock_app_scheduler_client.h +++ b/test/mock/services_appmgr_test/include/mock_app_scheduler_client.h @@ -29,7 +29,7 @@ class MockAppSchedulerClient : public AppSchedulerProxy { public: MockAppSchedulerClient(const sptr &impl) : AppSchedulerProxy(impl) {} virtual ~MockAppSchedulerClient() = default; - MOCK_METHOD0(ScheduleForegroundApplication, void()); + MOCK_METHOD0(ScheduleForegroundApplication, bool()); MOCK_METHOD0(ScheduleBackgroundApplication, void()); MOCK_METHOD1(ScheduleTerminateApplication, void(bool)); MOCK_METHOD2(ScheduleLaunchApplication, void(const AppLaunchData&, const Configuration& config)); diff --git a/test/mock/services_appmgr_test/include/mock_application.h b/test/mock/services_appmgr_test/include/mock_application.h index 1d19080d02..0726231801 100644 --- a/test/mock/services_appmgr_test/include/mock_application.h +++ b/test/mock/services_appmgr_test/include/mock_application.h @@ -24,7 +24,7 @@ namespace OHOS { namespace AppExecFwk { class MockApplication : public AppSchedulerHost { public: - MOCK_METHOD0(ScheduleForegroundApplication, void()); + MOCK_METHOD0(ScheduleForegroundApplication, bool()); MOCK_METHOD0(ScheduleBackgroundApplication, void()); MOCK_METHOD1(ScheduleTerminateApplication, void(bool)); MOCK_METHOD1(ScheduleShrinkMemory, void(const int)); diff --git a/test/mock/services_appmgr_test/include/mock_application_proxy.h b/test/mock/services_appmgr_test/include/mock_application_proxy.h index 6d7e3b3279..8a015c1610 100644 --- a/test/mock/services_appmgr_test/include/mock_application_proxy.h +++ b/test/mock/services_appmgr_test/include/mock_application_proxy.h @@ -25,7 +25,7 @@ namespace AppExecFwk { class MockApplicationProxy : public AppSchedulerProxy { public: MockApplicationProxy(const sptr &impl) : AppSchedulerProxy(impl) {} - MOCK_METHOD0(ScheduleForegroundApplication, void()); + MOCK_METHOD0(ScheduleForegroundApplication, bool()); MOCK_METHOD0(ScheduleBackgroundApplication, void()); MOCK_METHOD1(ScheduleTerminateApplication, void(bool)); MOCK_METHOD1(ScheduleShrinkMemory, void(const int)); diff --git a/test/moduletest/ability_record_test/ability_record_module_test.cpp b/test/moduletest/ability_record_test/ability_record_module_test.cpp index 9efb887352..b23682d0d9 100644 --- a/test/moduletest/ability_record_test/ability_record_module_test.cpp +++ b/test/moduletest/ability_record_test/ability_record_module_test.cpp @@ -273,7 +273,7 @@ HWTEST_F(AbilityRecordModuleTest, AbilityScheduler_001, TestSize.Level3) testResult = false; EXPECT_CALL(*mockAbilityScheduerStub, ScheduleAbilityTransaction(_, _, _)) .Times(1) - .WillOnce(Invoke(mockActivateHandler)); + .WillOnce(testing::DoAll(Invoke(mockActivateHandler), testing::Return(true))); abilityRecord->Activate(); EXPECT_TRUE(testResult); @@ -287,7 +287,7 @@ HWTEST_F(AbilityRecordModuleTest, AbilityScheduler_001, TestSize.Level3) }; EXPECT_CALL(*mockAbilityScheduerStub, ScheduleAbilityTransaction(_, _, _)) .Times(1) - .WillOnce(Invoke(mockInactivateHandler)); + .WillOnce(testing::DoAll(Invoke(mockInactivateHandler), testing::Return(true))); abilityRecord->Inactivate(); EXPECT_TRUE(testResult); diff --git a/test/moduletest/common/ams/ability_running_record_test/ams_ability_running_record_module_test.cpp b/test/moduletest/common/ams/ability_running_record_test/ams_ability_running_record_module_test.cpp index cd31741a1f..1a12a4e3bb 100644 --- a/test/moduletest/common/ams/ability_running_record_test/ams_ability_running_record_module_test.cpp +++ b/test/moduletest/common/ams/ability_running_record_test/ams_ability_running_record_module_test.cpp @@ -71,9 +71,10 @@ protected: class MockedApplication : public AppSchedulerHost, public MockedSchedulerBase { public: - void ScheduleForegroundApplication() override + bool ScheduleForegroundApplication() override { scheduled_ |= FOREGROUND_SCHEDULED; + return true; } void ScheduleBackgroundApplication() override { diff --git a/test/moduletest/common/ams/app_mgr_service_test/ams_app_mgr_service_module_test.cpp b/test/moduletest/common/ams/app_mgr_service_test/ams_app_mgr_service_module_test.cpp index be4222d4ca..737cb87910 100644 --- a/test/moduletest/common/ams/app_mgr_service_test/ams_app_mgr_service_module_test.cpp +++ b/test/moduletest/common/ams/app_mgr_service_test/ams_app_mgr_service_module_test.cpp @@ -41,8 +41,10 @@ namespace OHOS { namespace AppExecFwk { class TestAppSchedulerImpl : public AppSchedulerHost { public: - void ScheduleForegroundApplication() override - {} + bool ScheduleForegroundApplication() override + { + return true; + } void ScheduleBackgroundApplication() override {} void ScheduleTerminateApplication(bool isLastProcess = false) override diff --git a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp index 74e0e075a4..98c7f7c09f 100644 --- a/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp +++ b/test/moduletest/common/ams/app_running_processes_info_module_test/app_running_processes_info_module_test.cpp @@ -218,7 +218,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_001, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -280,7 +283,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_002, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -344,7 +350,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_003, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -424,7 +433,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_004, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -488,7 +500,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_005, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -550,7 +565,10 @@ HWTEST_F(AppRunningProcessesInfoModuleTest, ApplicationStart_006, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); diff --git a/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp b/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp index aaabec41be..7ef5fd102b 100644 --- a/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp +++ b/test/moduletest/common/ams/app_running_record_test/ams_app_running_record_module_test.cpp @@ -212,7 +212,10 @@ HWTEST_F(AmsAppRunningRecordModuleTest, ApplicationStart_001, TestSize.Level1) EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -419,7 +422,10 @@ HWTEST_F(AmsAppRunningRecordModuleTest, ApplicationStartAndQuit_005, TestSize.Le EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // set foreground and update foreground state record->ScheduleForegroundRunning(); mockApplication->Wait(); @@ -490,7 +496,10 @@ HWTEST_F(AmsAppRunningRecordModuleTest, ApplicationStatusChange_006, TestSize.Le for (int i = 0; i < startCount; i++) { EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // set foreground and update foreground state record->ScheduleForegroundRunning(); mockApplication->Wait(); diff --git a/test/moduletest/common/ams/ipc_app_scheduler_test/ams_ipc_app_scheduler_module_test.cpp b/test/moduletest/common/ams/ipc_app_scheduler_test/ams_ipc_app_scheduler_module_test.cpp index 2b04000abd..b5f2c1b262 100644 --- a/test/moduletest/common/ams/ipc_app_scheduler_test/ams_ipc_app_scheduler_module_test.cpp +++ b/test/moduletest/common/ams/ipc_app_scheduler_test/ams_ipc_app_scheduler_module_test.cpp @@ -76,7 +76,10 @@ HWTEST_F(AmsIpcAppSchedulerModuleTest, ExcuteApplicationIPCInterface_001, TestSi EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); client->ScheduleForegroundApplication(); mockApplication->Wait(); } diff --git a/test/moduletest/ipc_ability_scheduler_test/ipc_ability_scheduler_module_test.cpp b/test/moduletest/ipc_ability_scheduler_test/ipc_ability_scheduler_module_test.cpp index eea5a856ab..e53ae2cf2e 100644 --- a/test/moduletest/ipc_ability_scheduler_test/ipc_ability_scheduler_module_test.cpp +++ b/test/moduletest/ipc_ability_scheduler_test/ipc_ability_scheduler_module_test.cpp @@ -93,7 +93,8 @@ HWTEST_F(IpcAbilitySchedulerModuleTest, ScheduleAbilityTransaction_001, TestSize sem.Post(); }; - EXPECT_CALL(*stub, ScheduleAbilityTransaction(_, _, _)).Times(1).WillOnce(Invoke(stubHandler)); + EXPECT_CALL(*stub, ScheduleAbilityTransaction(_, _, _)).Times(1) + .WillOnce(testing::DoAll(Invoke(stubHandler), testing::Return(true))); proxy->ScheduleAbilityTransaction(proxyState.want, proxyState.lifeCycleStateInfo); diff --git a/test/moduletest/mock/include/mock_ability_scheduler.h b/test/moduletest/mock/include/mock_ability_scheduler.h index 453c2e2474..b4d4aa8e79 100644 --- a/test/moduletest/mock/include/mock_ability_scheduler.h +++ b/test/moduletest/mock/include/mock_ability_scheduler.h @@ -25,7 +25,7 @@ namespace OHOS { namespace AAFwk { class MockAbilityScheduler : public IAbilityScheduler { public: - MOCK_METHOD3(ScheduleAbilityTransaction, void(const Want& want, const LifeCycleStateInfo& targetState, + MOCK_METHOD3(ScheduleAbilityTransaction, bool(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo)); MOCK_METHOD1(ScheduleShareData, void(const int32_t &uniqueId)); MOCK_METHOD3(SendResult, void(int requestCode, int resultCode, const Want& resultWant)); diff --git a/test/moduletest/mock/include/mock_ability_scheduler_stub.h b/test/moduletest/mock/include/mock_ability_scheduler_stub.h index b18602cfd4..cefc9d5e14 100644 --- a/test/moduletest/mock/include/mock_ability_scheduler_stub.h +++ b/test/moduletest/mock/include/mock_ability_scheduler_stub.h @@ -22,7 +22,7 @@ namespace OHOS { namespace AAFwk { class MockAbilitySchedulerStub : public AbilitySchedulerStub { public: - MOCK_METHOD3(ScheduleAbilityTransaction, void(const Want&, const LifeCycleStateInfo&, sptr)); + MOCK_METHOD3(ScheduleAbilityTransaction, bool(const Want&, const LifeCycleStateInfo&, sptr)); MOCK_METHOD1(ScheduleShareData, void(const int32_t &uniqueId)); MOCK_METHOD3(SendResult, void(int, int, const Want&)); MOCK_METHOD1(ScheduleConnectAbility, void(const Want&)); diff --git a/test/moduletest/mock/include/mock_app_scheduler.h b/test/moduletest/mock/include/mock_app_scheduler.h index 917ed62d4f..dcae09a2bc 100644 --- a/test/moduletest/mock/include/mock_app_scheduler.h +++ b/test/moduletest/mock/include/mock_app_scheduler.h @@ -31,7 +31,7 @@ public: MockAppScheduler() = default; virtual ~MockAppScheduler() = default; - MOCK_METHOD0(ScheduleForegroundApplication, void()); + MOCK_METHOD0(ScheduleForegroundApplication, bool()); MOCK_METHOD0(ScheduleBackgroundApplication, void()); MOCK_METHOD1(ScheduleTerminateApplication, void(bool)); MOCK_METHOD2(ScheduleLaunchApplication, void(const AppExecFwk::AppLaunchData&, const Configuration& config)); diff --git a/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h b/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h index d8ce11f5ec..f470ccb212 100644 --- a/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h +++ b/test/unittest/ability_scheduler_stub_test/ability_schedule_stub_mock.h @@ -21,9 +21,11 @@ namespace OHOS { namespace AAFwk { class AbilitySchedulerStubMock : public AbilitySchedulerStub { public: - void ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, + bool ScheduleAbilityTransaction(const Want& want, const LifeCycleStateInfo& targetState, sptr sessionInfo = nullptr) override - {} + { + return true; + } void ScheduleShareData(const int32_t &uniqueId) override {} diff --git a/test/unittest/ams_ipc_interface_test/ams_ipc_appscheduler_interface_test.cpp b/test/unittest/ams_ipc_interface_test/ams_ipc_appscheduler_interface_test.cpp index dce26bd268..e495fd9d87 100644 --- a/test/unittest/ams_ipc_interface_test/ams_ipc_appscheduler_interface_test.cpp +++ b/test/unittest/ams_ipc_interface_test/ams_ipc_appscheduler_interface_test.cpp @@ -73,7 +73,10 @@ HWTEST_F(AmsIpcAppSchedulerInterfaceTest, Interface_001, TestSize.Level1) EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplication::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); client->ScheduleForegroundApplication(); mockApplication->Wait(); TAG_LOGD(AAFwkTag::TEST, "AppSchedulerInterfaceTest_001 end"); diff --git a/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp b/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp index 057c77835b..ab312622ad 100644 --- a/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp +++ b/test/unittest/app_running_processes_info_test/app_running_processes_info_test.cpp @@ -272,7 +272,10 @@ HWTEST_F(AppRunningProcessesInfoTest, UpdateAppRunningRecord_002, TestSize.Level EXPECT_CALL(*mockApplication, ScheduleForegroundApplication()) .Times(1) - .WillOnce(InvokeWithoutArgs(mockApplication.GetRefPtr(), &MockApplicationProxy::Post)); + .WillOnce([mockApplication]() { + mockApplication->Post(); + return true; + }); // application enter in foreground and check the result record->ScheduleForegroundRunning(); mockApplication->Wait(); diff --git a/test/unittest/lifecycle_deal_test/lifecycle_deal_test.cpp b/test/unittest/lifecycle_deal_test/lifecycle_deal_test.cpp index 1eb77d7b27..6c737d5d70 100644 --- a/test/unittest/lifecycle_deal_test/lifecycle_deal_test.cpp +++ b/test/unittest/lifecycle_deal_test/lifecycle_deal_test.cpp @@ -60,7 +60,7 @@ HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_001, TestSize.Level1) LifeCycleStateInfo val; EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_)) .Times(1) - .WillOnce(testing::SaveArg<1>(&val)); + .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true))); const Want want; CallerInfo caller; @@ -92,7 +92,7 @@ HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_002, TestSize.Level1) LifeCycleStateInfo val; EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_)) .Times(1) - .WillOnce(testing::SaveArg<1>(&val)); + .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true))); const Want want; CallerInfo caller; @@ -124,7 +124,7 @@ HWTEST_F(LifecycleDealTest, LifecycleDeal_oprator_003, TestSize.Level1) LifeCycleStateInfo val; EXPECT_CALL(*abilityScheduler_, ScheduleAbilityTransaction(::testing::_, ::testing::_, ::testing::_)) .Times(1) - .WillOnce(testing::SaveArg<1>(&val)); + .WillOnce(testing::DoAll(testing::SaveArg<1>(&val), testing::Return(true))); const Want want; CallerInfo caller; diff --git a/utils/global/freeze/include/freeze_util.h b/utils/global/freeze/include/freeze_util.h index cc91e3ae62..ff5827ba21 100644 --- a/utils/global/freeze/include/freeze_util.h +++ b/utils/global/freeze/include/freeze_util.h @@ -47,6 +47,7 @@ public: static FreezeUtil& GetInstance(); void AddLifecycleEvent(const LifecycleFlow &flow, const std::string &entry); + bool AppendLifecycleEvent(const LifecycleFlow &flow, const std::string &entry); std::string GetLifecycleEvent(const LifecycleFlow &flow); void DeleteLifecycleEvent(const LifecycleFlow &flow); void DeleteLifecycleEvent(sptr token); diff --git a/utils/global/freeze/src/freeze_util.cpp b/utils/global/freeze/src/freeze_util.cpp index 417bc3a07b..b0ada2bf4d 100644 --- a/utils/global/freeze/src/freeze_util.cpp +++ b/utils/global/freeze/src/freeze_util.cpp @@ -29,13 +29,26 @@ void FreezeUtil::AddLifecycleEvent(const LifecycleFlow &flow, const std::string { auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry; std::lock_guard lock(mutex_); - if (lifecycleFlow_.count(flow)) { - lifecycleFlow_[flow] = lifecycleFlow_[flow] + "\n" + newEntry; + auto iter = lifecycleFlow_.find(flow); + if (iter != lifecycleFlow_.end()) { + iter->second += "\n" + newEntry; } else { - lifecycleFlow_[flow] = newEntry; + lifecycleFlow_.emplace(flow, newEntry); } } +bool FreezeUtil::AppendLifecycleEvent(const LifecycleFlow &flow, const std::string &entry) +{ + std::lock_guard lock(mutex_); + auto iter = lifecycleFlow_.find(flow); + if (iter == lifecycleFlow_.end()) { + return false; + } + auto newEntry = TimeUtil::DefaultCurrentTimeStr() + "; " + entry; + iter->second += "\n" + newEntry; + return true; +} + std::string FreezeUtil::GetLifecycleEvent(const LifecycleFlow &flow) { std::lock_guard lock(mutex_); diff --git a/utils/global/time/include/time_util.h b/utils/global/time/include/time_util.h index 6ff92bcb0f..7b1f0c93e6 100644 --- a/utils/global/time/include/time_util.h +++ b/utils/global/time/include/time_util.h @@ -28,6 +28,7 @@ constexpr int64_t NANOSECONDS = 1000000000; constexpr int64_t MICROSECONDS = 1000000; constexpr int64_t SEC_TO_MILLISEC = 1000; constexpr int64_t MAX_TIME_BUFF = 64; // 64 : for example 2021-05-27-01-01-01 +constexpr int32_t DECIMAL_BASE = 10; [[maybe_unused]] static int64_t SystemTimeMillisecond() { @@ -60,7 +61,13 @@ constexpr int64_t MAX_TIME_BUFF = 64; // 64 : for example 2021-05-27-01-01-01 localtime_r(&tt, &t); char buffer[MAX_TIME_BUFF] = {0}; std::strftime(buffer, sizeof(buffer), "%Y-%m-%d %H:%M:%S", &t); - return std::string(buffer) + "." + std::to_string(timestamp % SEC_TO_MILLISEC); + auto remainder = timestamp % SEC_TO_MILLISEC; + std::string milliStr("000"); + for (int i = 2; i >= 0 && remainder > 0; i--) { + milliStr[i] = '0' + remainder % DECIMAL_BASE; + remainder /= DECIMAL_BASE; + } + return std::string(buffer) + "." + milliStr; } } // namespace TimeUtil } // namespace OHOS::AbilityRuntime