diff --git a/frameworks/native/appkit/BUILD.gn b/frameworks/native/appkit/BUILD.gn index 7b3259a5a2174d4ad0316be852392b26935b58ae..4de995640bc302d3bd8838eb7ec3715544f256f0 100644 --- a/frameworks/native/appkit/BUILD.gn +++ b/frameworks/native/appkit/BUILD.gn @@ -200,6 +200,7 @@ ohos_shared_library("app_context") { external_deps = [ "ability_base:configuration", "ability_runtime:ability_deps_wrapper", + "ability_runtime:app_manager", "ability_runtime:runtime", "bundle_framework:appexecfwk_base", "bundle_framework:appexecfwk_core", @@ -248,6 +249,7 @@ ohos_shared_library("app_context_utils") { external_deps = [ "ability_runtime:ability_runtime_error_util", + "ability_runtime:app_manager", "ability_runtime:runtime", "access_token:libtokenid_sdk", "bundle_framework:appexecfwk_base", diff --git a/frameworks/native/appkit/ability_runtime/context/application_context.cpp b/frameworks/native/appkit/ability_runtime/context/application_context.cpp index 97a95141df73bd0725e4e7d40ad36a838c2f2cab..b461aa6659866be45f09d9e80e3a49739c0abef5 100644 --- a/frameworks/native/appkit/ability_runtime/context/application_context.cpp +++ b/frameworks/native/appkit/ability_runtime/context/application_context.cpp @@ -18,6 +18,7 @@ #include #include "hilog_wrapper.h" +#include "running_process_info.h" namespace OHOS { namespace AbilityRuntime { @@ -294,6 +295,18 @@ std::string ApplicationContext::GetFilesDir() return (contextImpl_ != nullptr) ? contextImpl_->GetFilesDir() : ""; } +void ApplicationContext::KillProcessBySelf() +{ + if (contextImpl_ != nullptr) { + contextImpl_->KillProcessBySelf(); + } +} + +int32_t ApplicationContext::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info) +{ + return (contextImpl_ != nullptr) ? contextImpl_->GetProcessRunningInformation(info) : -1; +} + bool ApplicationContext::IsUpdatingConfigurations() { return (contextImpl_ != nullptr) ? contextImpl_->IsUpdatingConfigurations() : false; diff --git a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp index 2ef60d17399c266ccd38c6d4f0abd330a683f06b..089149a5fec68fea0f0b983fb3fd81304605f721 100644 --- a/frameworks/native/appkit/ability_runtime/context/context_impl.cpp +++ b/frameworks/native/appkit/ability_runtime/context/context_impl.cpp @@ -30,9 +30,11 @@ #include "os_account_manager_wrapper.h" #include "parameters.h" #include "sys_mgr_client.h" +#include "app_mgr_client.h" #include "system_ability_definition.h" #include "bundle_mgr_proxy.h" #include "configuration_convertor.h" +#include "running_process_info.h" namespace OHOS { namespace AbilityRuntime { @@ -548,6 +550,20 @@ void ContextImpl::SetConfiguration(const std::shared_ptr::GetInstance(); + appMgrClient->KillApplicationSelf(); +} + +int32_t ContextImpl::GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info) +{ + auto appMgrClient = DelayedSingleton::GetInstance(); + auto result = appMgrClient->GetProcessRunningInformation(info); + HILOG_DEBUG("result is %{public}d.", result); + return result; +} + std::shared_ptr ContextImpl::GetConfiguration() const { return config_; diff --git a/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp b/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp index d3477ec91d43709a3ddc83e78310ce3df3d936d4..ca65f94bf451299108f1c140f68bc6000bdef5c2 100644 --- a/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp +++ b/frameworks/native/appkit/ability_runtime/context/js_application_context_utils.cpp @@ -26,12 +26,14 @@ #include "js_hap_module_info_utils.h" #include "js_resource_manager_utils.h" #include "js_runtime_utils.h" +#include "running_process_info.h" #include "tokenid_kit.h" namespace OHOS { namespace AbilityRuntime { namespace { constexpr char APPLICATION_CONTEXT_NAME[] = "__application_context_ptr__"; +constexpr size_t ARGC_ZERO = 0; constexpr size_t ARGC_ONE = 1; constexpr size_t ARGC_TWO = 2; constexpr size_t ARGC_THREE = 3; @@ -79,6 +81,8 @@ public: NativeValue* OnGetDatabaseDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetPreferencesDir(NativeEngine &engine, NativeCallbackInfo &info); NativeValue* OnGetBundleCodeDir(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info); + NativeValue* OnGetProcessRunningInformation(NativeEngine &engine, NativeCallbackInfo &info); static NativeValue* GetCacheDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetTempDir(NativeEngine *engine, NativeCallbackInfo *info); @@ -88,6 +92,8 @@ public: static NativeValue* GetPreferencesDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetBundleCodeDir(NativeEngine *engine, NativeCallbackInfo *info); static NativeValue* GetApplicationContext(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info); + static NativeValue* GetProcessRunningInformation(NativeEngine *engine, NativeCallbackInfo *info); void KeepApplicationContext(std::shared_ptr applicationContext) { @@ -103,6 +109,7 @@ private: NativeValue* OnGetArea(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnCreateModuleContext(NativeEngine& engine, NativeCallbackInfo& info); NativeValue* OnGetApplicationContext(NativeEngine& engine, NativeCallbackInfo& info); + NativeValue* CreateJsProcessRunningInfo(NativeEngine &engine, const AppExecFwk::RunningProcessInfo &info); bool CheckCallerIsSystemApp(); std::shared_ptr keepApplicationContext_; @@ -451,6 +458,101 @@ NativeValue *JsApplicationContextUtils::OnGetBundleCodeDir(NativeEngine &engine, return engine.CreateString(path.c_str(), path.length()); } +NativeValue *JsApplicationContextUtils::KillProcessBySelf(NativeEngine *engine, NativeCallbackInfo *info) +{ + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnKillProcessBySelf(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnKillProcessBySelf(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext if already released"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST); + return engine.CreateUndefined(); + } + + // only support 0 or 1 params + if (info.argc != ARGC_ZERO && info.argc != ARGC_ONE) { + HILOG_ERROR("Not enough params"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + HILOG_DEBUG("kill self process"); + AsyncTask::CompleteCallback complete = + [applicationContext](NativeEngine& engine, AsyncTask& task, int32_t status) { + applicationContext->KillProcessBySelf(); + task.Resolve(engine, engine.CreateUndefined()); + }; + NativeValue* lastParam = (info.argc = ARGC_ONE) ? info.argv[INDEX_ZERO] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule("JSAppManager::OnkillProcessBySelf", + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return engine.CreateUndefined(); +} + +NativeValue *JsApplicationContextUtils::GetProcessRunningInformation(NativeEngine *engine, NativeCallbackInfo *info) +{ + JsApplicationContextUtils *me = + CheckParamsAndGetThis(engine, info, APPLICATION_CONTEXT_NAME); + return me != nullptr ? me->OnGetProcessRunningInformation(*engine, *info) : nullptr; +} + +NativeValue *JsApplicationContextUtils::OnGetProcessRunningInformation(NativeEngine &engine, NativeCallbackInfo &info) +{ + auto applicationContext = applicationContext_.lock(); + if (!applicationContext) { + HILOG_WARN("applicationContext if already released"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_CONTEXT_NOT_EXIST); + return engine.CreateUndefined(); + } + + // only support 0 or 1 params + if (info.argc != ARGC_ZERO && info.argc != ARGC_ONE) { + HILOG_ERROR("Not enough params"); + AbilityRuntimeErrorUtil::Throw(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INVALID_PARAMETER); + return engine.CreateUndefined(); + } + HILOG_DEBUG("Get Process Info"); + auto complete = + [applicationContext, this](NativeEngine& engine, AsyncTask& task, + int32_t status) { + AppExecFwk::RunningProcessInfo processInfo; + auto ret = applicationContext->GetProcessRunningInformation(processInfo); + if (ret == 0) { + task.Resolve(engine, this->CreateJsProcessRunningInfo(engine, processInfo)); + } else { + task.Reject(engine, CreateJsError(engine, ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR, + "Get process infos failed.")); + } + }; + + NativeValue* lastParam = (info.argc == ARGC_ONE) ? info.argv[INDEX_ZERO] : nullptr; + NativeValue* result = nullptr; + AsyncTask::Schedule("JSAppManager::OnGetProcessRunningInformation", + engine, CreateAsyncTaskWithLastParam(engine, lastParam, nullptr, std::move(complete), &result)); + return result; +} + +NativeValue* JsApplicationContextUtils::CreateJsProcessRunningInfo(NativeEngine &engine, + const AppExecFwk::RunningProcessInfo &info) +{ + NativeValue* objValue = engine.CreateObject(); + NativeObject* object = ConvertNativeValueTo(objValue); + + object->SetProperty("processName", CreateJsValue(engine, info.processName_)); + object->SetProperty("pid", CreateJsValue(engine, info.pid_)); + object->SetProperty("uid", CreateJsValue(engine, info.uid_)); + object->SetProperty("bundleNames", CreateNativeArray(engine, info.bundleNames)); + object->SetProperty("state", CreateJsValue(engine, info.state_)); + object->SetProperty("isContinuousTask", CreateJsValue(engine, info.isContinuousTask)); + object->SetProperty("isKeepAlive", CreateJsValue(engine, info.isKeepAlive)); + object->SetProperty("isFocused", CreateJsValue(engine, info.isFocused)); + return objValue; +} + void JsApplicationContextUtils::Finalizer(NativeEngine *engine, void *data, void *hint) { HILOG_INFO("JsApplicationContextUtils::Finalizer is called"); @@ -934,6 +1036,10 @@ NativeValue *CreateJsApplicationContext(NativeEngine &engine, std::shared_ptr &info, int32_t userId); + /** + * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. + * Obtains information about current application processes which is running on the device. + * + * @param info, app name in Application record. + * @return ERR_OK ,return back success,others fail. + */ + virtual AppMgrResultCode GetProcessRunningInformation(RunningProcessInfo &info); + /** * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. * Notify abilities background the current memory level. diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h index 08b5a161861c8253248bb7b6f588c68828634293..e75bf3e0fccef502c3d9180d71e9f3dd6eae0115 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_interface.h @@ -31,6 +31,7 @@ #include "iapplication_state_observer.h" #include "iconfiguration_observer.h" #include "iquick_fix_callback.h" +#include "running_process_info.h" namespace OHOS { namespace AppExecFwk { @@ -126,6 +127,15 @@ public: */ virtual int GetProcessRunningInfosByUserId(std::vector &info, int32_t userId) = 0; + /** + * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. + * Obtains information about current application process which is running on the device. + * + * @param info, app name in Application record. + * @return ERR_OK ,return back success,others fail. + */ + virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) = 0; + /** * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. * Notify abilities background the current memory level. @@ -335,6 +345,7 @@ public: SET_CONTINUOUSTASK_PROCESS, NOTIFY_UNLOAD_REPAIR_PATCH, PRE_START_NWEBSPAWN_PROCESS, + APP_GET_PROCESS_RUNNING_INFORMATION, }; }; } // namespace AppExecFwk diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h index d15993ccbf31c96566d94ef713ea8e5a7991a04c..4a61bd1e4eac2396e4a27cb4499cd53e77bb0aff 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_proxy.h @@ -116,6 +116,15 @@ public: */ virtual int32_t GetProcessRunningInfosByUserId(std::vector &info, int32_t userId) override; + /** + * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. + * Obtains information about current application process which is running on the device. + * + * @param info, app name in Application record. + * @return ERR_OK ,return back success,others fail. + */ + virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override; + /** * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. * Notify abilities background the current memory level. diff --git a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h index b2848afed928258465190b5d72680adb8db836ec..98081fdc07cbc95bce7cff4647ddf8480071356e 100644 --- a/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h +++ b/interfaces/inner_api/app_manager/include/appmgr/app_mgr_stub.h @@ -66,6 +66,7 @@ private: int32_t HandleClearUpApplicationData(MessageParcel &data, MessageParcel &reply); int32_t HandleGetAllRunningProcesses(MessageParcel &data, MessageParcel &reply); int32_t HandleGetProcessRunningInfosByUserId(MessageParcel &data, MessageParcel &reply); + int32_t HandleGetProcessRunningInformation(MessageParcel &data, MessageParcel &reply); int32_t HandleAddAbilityStageDone(MessageParcel &data, MessageParcel &reply); int32_t HandleNotifyMemoryLevel(MessageParcel &data, MessageParcel &reply); int32_t HandleStartupResidentProcess(MessageParcel &data, MessageParcel &reply); diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp index 3f2d088651f4ff9693c4022284ec9f8c7e823968..71695760ec3171112668a92e49099a1d830ca8f4 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_client.cpp @@ -320,6 +320,19 @@ AppMgrResultCode AppMgrClient::GetProcessRunningInfosByUserId(std::vector service = iface_cast(mgrHolder_->GetRemoteObject()); + if (service != nullptr) { + int32_t result = service->GetProcessRunningInformation(info); + if (result == ERR_OK) { + return AppMgrResultCode::RESULT_OK; + } + return AppMgrResultCode::ERROR_SERVICE_NOT_READY; + } + return AppMgrResultCode::ERROR_SERVICE_NOT_CONNECTED; +} + AppMgrResultCode AppMgrClient::NotifyMemoryLevel(MemoryLevel level) { sptr service = iface_cast(mgrHolder_->GetRemoteObject()); diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp index 8b0b8ea36fe11af3548c94e2b97044d58f8f9275..5ece7a002907d37606b6550cb8803910e4db754a 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_proxy.cpp @@ -268,6 +268,22 @@ int32_t AppMgrProxy::GetProcessRunningInfosByUserId(std::vector infoReply(reply.ReadParcelable()); + info = *infoReply; + return reply.ReadInt32(); +} + int32_t AppMgrProxy::NotifyMemoryLevel(int32_t level) { MessageParcel data; diff --git a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp index 5dbad7ae036040d1821553c4cd8b3bd03776b69f..68db27919065e5c5d23356c37bf1977ec06107d1 100644 --- a/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp +++ b/interfaces/inner_api/app_manager/src/appmgr/app_mgr_stub.cpp @@ -89,6 +89,8 @@ AppMgrStub::AppMgrStub() &AppMgrStub::HandleRegisterConfigurationObserver; memberFuncMap_[static_cast(IAppMgr::Message::UNREGISTER_CONFIGURATION_OBSERVER)] = &AppMgrStub::HandleUnregisterConfigurationObserver; + memberFuncMap_[static_cast(IAppMgr::Message::APP_GET_PROCESS_RUNNING_INFORMATION)] = + &AppMgrStub::HandleGetProcessRunningInformation; #ifdef ABILITY_COMMAND_FOR_TEST memberFuncMap_[static_cast(IAppMgr::Message::BLOCK_APP_SERVICE)] = &AppMgrStub::HandleBlockAppServiceDone; @@ -241,6 +243,20 @@ int32_t AppMgrStub::HandleGetProcessRunningInfosByUserId(MessageParcel &data, Me return NO_ERROR; } +int32_t AppMgrStub::HandleGetProcessRunningInformation(MessageParcel &data, MessageParcel &reply) +{ + HITRACE_METER(HITRACE_TAG_APP); + RunningProcessInfo info; + auto result = GetProcessRunningInformation(info); + if (!reply.WriteParcelable(&info)) { + return ERR_INVALID_VALUE; + } + if (!reply.WriteInt32(result)) { + return ERR_INVALID_VALUE; + } + return NO_ERROR; +} + int32_t AppMgrStub::HandleAddAbilityStageDone(MessageParcel &data, MessageParcel &reply) { int32_t recordId = data.ReadInt32(); diff --git a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h index 74af63729e7c602d87928982f9547627287261bf..b790f24aa5c17a3a6776a8a6d35eef448e0dc865 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/application_context.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/application_context.h @@ -74,6 +74,8 @@ public: std::shared_ptr GetConfiguration() const override; std::string GetBaseDir() const override; Global::Resource::DeviceType GetDeviceType() const override; + void KillProcessBySelf(); + int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); void InitApplicationContext(); void AttachContextImpl(const std::shared_ptr &contextImpl); diff --git a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h index d435f3cb5449bc2a02e1a42391aae381cf7f8d8f..1ace51b6ae4339a3bce6c381c9921f547a10d66b 100644 --- a/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h +++ b/interfaces/kits/native/appkit/ability_runtime/context/context_impl.h @@ -22,6 +22,9 @@ #include "bundle_mgr_interface.h" namespace OHOS { +namespace AppExecFwk { +struct RunningProcessInfo; +} namespace AbilityRuntime { class ContextImpl : public Context, public std::enable_shared_from_this { public: @@ -239,6 +242,20 @@ public: */ void SetConfiguration(const std::shared_ptr &config); + /** + * @brief Kill process itself + * + * @return error code + */ + void KillProcessBySelf(); + + /** + * @brief Get running informationfor cuirrent process + * + * @return error code + */ + int32_t GetProcessRunningInformation(AppExecFwk::RunningProcessInfo &info); + /** * @brief Get the token witch the app launched. * diff --git a/services/appmgr/include/app_mgr_service.h b/services/appmgr/include/app_mgr_service.h index d1420c47a68932f4288231750b819b1e4dc6fe72..e5041f8009b1f53b68d140f88092906d30f2a491 100644 --- a/services/appmgr/include/app_mgr_service.h +++ b/services/appmgr/include/app_mgr_service.h @@ -134,6 +134,16 @@ public: */ virtual int32_t GetProcessRunningInfosByUserId(std::vector &info, int32_t userId) override; + /** + * GetProcessRunningInformation, call GetProcessRunningInformation() through proxy project. + * Obtains information about current application process which is running on the device. + * + * @param info, app name in Application record. + * + * @return Returns ERR_OK on success, others on failure. + */ + virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info) override; + /** * NotifyMemoryLevel, call NotifyMemoryLevel() through proxy project. * Notify applications background the current memory level. diff --git a/services/appmgr/include/app_mgr_service_inner.h b/services/appmgr/include/app_mgr_service_inner.h index 155c483f55106662a1c4adc444012367e935f380..1f1f4f38c72e68beb24af8ea57153bc3eaf92245 100644 --- a/services/appmgr/include/app_mgr_service_inner.h +++ b/services/appmgr/include/app_mgr_service_inner.h @@ -256,6 +256,15 @@ public: */ virtual int32_t GetProcessRunningInfosByUserId(std::vector &info, int32_t userId); + /** + * GetProcessRunningInformation, Obtains information about current application process + * which is running on the device. + * @param info, app name in Application record. + * + * @return ERR_OK ,return back success,others fail. + */ + virtual int32_t GetProcessRunningInformation(RunningProcessInfo &info); + /** * NotifyMemoryLevel, Notify applications background the current memory level. * @@ -732,6 +741,7 @@ private: void InitGlobalConfiguration(); void GetRunningProcesses(const std::shared_ptr &appRecord, std::vector &info); + void GetRunningProcess(const std::shared_ptr &appRecord, RunningProcessInfo &info); int StartRenderProcessImpl(const std::shared_ptr &renderRecord, const std::shared_ptr appRecord, pid_t &renderPid); @@ -765,6 +775,8 @@ private: bool CheckGetRunningInfoPermission() const; + int32_t KillApplicationByBundleName(const std::string &bundleName); + private: /** * Notify application status. diff --git a/services/appmgr/src/app_mgr_service.cpp b/services/appmgr/src/app_mgr_service.cpp index b8c444e10441d07bc94ec61244f8314f45bb0991..d1d46cb3ce5e854ffc3b87972506fddcb63bcccf 100644 --- a/services/appmgr/src/app_mgr_service.cpp +++ b/services/appmgr/src/app_mgr_service.cpp @@ -302,6 +302,14 @@ int32_t AppMgrService::GetProcessRunningInfosByUserId(std::vectorGetProcessRunningInfosByUserId(info, userId); } +int32_t AppMgrService::GetProcessRunningInformation(RunningProcessInfo &info) +{ + if (!IsReady()) { + return ERR_INVALID_OPERATION; + } + return appMgrServiceInner_->GetProcessRunningInformation(info); +} + int32_t AppMgrService::NotifyMemoryLevel(int32_t level) { if (!IsReady()) { diff --git a/services/appmgr/src/app_mgr_service_inner.cpp b/services/appmgr/src/app_mgr_service_inner.cpp index 7bbf80ffb9b6cbd4f46c506fe362efe109f37cf6..0bb6d6f4affb8b7adcc966a753260b1153d79c31 100644 --- a/services/appmgr/src/app_mgr_service_inner.cpp +++ b/services/appmgr/src/app_mgr_service_inner.cpp @@ -488,28 +488,7 @@ int32_t AppMgrServiceInner::KillApplication(const std::string &bundleName) return errCode; } - int result = ERR_OK; - int64_t startTime = SystemTimeMillisecond(); - std::list pids; - - if (!appRunningManager_->ProcessExitByBundleName(bundleName, pids)) { - HILOG_INFO("The process corresponding to the package name did not start"); - return result; - } - if (WaitForRemoteProcessExit(pids, startTime)) { - HILOG_INFO("The remote process exited successfully "); - NotifyAppStatus(bundleName, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - return result; - } - for (auto iter = pids.begin(); iter != pids.end(); ++iter) { - result = KillProcessByPid(*iter); - if (result < 0) { - HILOG_ERROR("KillApplication failed for bundleName:%{public}s pid:%{public}d", bundleName.c_str(), *iter); - return result; - } - } - NotifyAppStatus(bundleName, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); - return result; + return KillApplicationByBundleName(bundleName); } int32_t AppMgrServiceInner::KillApplicationByUid(const std::string &bundleName, const int uid) @@ -564,21 +543,35 @@ int32_t AppMgrServiceInner::KillApplicationSelf() } auto callerPid = IPCSkeleton::GetCallingPid(); - if (!appRunningManager_->ProcessExitByPid(callerPid)) { - HILOG_INFO("The callerPid is invalid"); - return ERR_OK; - } - std::list pids; - pids.push_back(callerPid); + auto appRecord = GetAppRunningRecordByPid(callerPid); + auto bundleName = appRecord->GetBundleName(); + return KillApplicationByBundleName(bundleName); +} + +int32_t AppMgrServiceInner::KillApplicationByBundleName(const std::string &bundleName) +{ + int result = ERR_OK; int64_t startTime = SystemTimeMillisecond(); + std::list pids; + + if (!appRunningManager_->ProcessExitByBundleName(bundleName, pids)) { + HILOG_ERROR("The process corresponding to the package name did not start"); + return result; + } if (WaitForRemoteProcessExit(pids, startTime)) { - HILOG_INFO("The remote process exited successfully"); - return ERR_OK; + HILOG_DEBUG("The remote process exited successfully "); + NotifyAppStatus(bundleName, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); + return result; } - int result = KillProcessByPid(callerPid); - if (result < 0) { - HILOG_ERROR("KillApplication is fail, pid: %{public}d", callerPid); + for (auto iter = pids.begin(); iter != pids.end(); ++iter) { + result = KillProcessByPid(*iter); + if (result < 0) { + HILOG_ERROR("KillApplicationSelf is failed for bundleName:%{public}s, pid: %{public}d", + bundleName.c_str(), *iter); + return result; + } } + NotifyAppStatus(bundleName, EventFwk::CommonEventSupport::COMMON_EVENT_PACKAGE_RESTARTED); return result; } @@ -738,6 +731,18 @@ int32_t AppMgrServiceInner::GetProcessRunningInfosByUserId(std::vector &info) { RunningProcessInfo runningProcessInfo; - runningProcessInfo.processName_ = appRecord->GetProcessName(); - runningProcessInfo.pid_ = appRecord->GetPriorityObject()->GetPid(); - runningProcessInfo.uid_ = appRecord->GetUid(); - runningProcessInfo.state_ = static_cast(appRecord->GetState()); - runningProcessInfo.isContinuousTask = appRecord->IsContinuousTask(); - runningProcessInfo.isKeepAlive = appRecord->IsKeepAliveApp(); - runningProcessInfo.isFocused = appRecord->GetFocusFlag(); - runningProcessInfo.startTimeMillis_ = appRecord->GetAppStartTime(); - appRecord->GetBundleNames(runningProcessInfo.bundleNames); + GetRunningProcess(appRecord, runningProcessInfo); info.emplace_back(runningProcessInfo); } +void AppMgrServiceInner::GetRunningProcess(const std::shared_ptr &appRecord, + RunningProcessInfo &info) +{ + info.processName_ = appRecord->GetProcessName(); + info.pid_ = appRecord->GetPriorityObject()->GetPid(); + info.uid_ = appRecord->GetUid(); + info.state_ = static_cast(appRecord->GetState()); + info.isContinuousTask = appRecord->IsContinuousTask(); + info.isKeepAlive = appRecord->IsKeepAliveApp(); + info.isFocused = appRecord->GetFocusFlag(); + info.startTimeMillis_ = appRecord->GetAppStartTime(); + appRecord->GetBundleNames(info.bundleNames); +} + int32_t AppMgrServiceInner::KillProcessByPid(const pid_t pid) const { int32_t ret = -1; diff --git a/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h b/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h index 29c796d17d8fa8a691b9af0ee43d05d5c9765fce..357784320d35f45b1fb86f4c43cc2c40e17f3d68 100644 --- a/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h +++ b/test/mock/frameworks_kits_appkit_test/include/mock_app_mgr_service.h @@ -114,6 +114,11 @@ public: return 0; } + virtual int GetProcessRunningInformation(RunningProcessInfo &info) + { + return 0; + } + int IsBackgroundRunningRestricted(const std::string& appName) { return 0; diff --git a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h index 841db5cc142715edf8c1d836d989dcb4d75a4680..165937e635ba26fdc8a833c6d41198ebf4780213 100644 --- a/test/mock/services_appmgr_test/include/mock_app_mgr_service.h +++ b/test/mock/services_appmgr_test/include/mock_app_mgr_service.h @@ -83,6 +83,11 @@ public: return 0; } + virtual int GetProcessRunningInformation(RunningProcessInfo &info) + { + return 0; + } + virtual void RegisterAppStateCallback(const sptr& callback) { callback_ = callback;