From da0d462772fb311dc5070b58b4756e1e28227355 Mon Sep 17 00:00:00 2001 From: ZhangJianxin Date: Thu, 14 Nov 2024 21:28:24 +0800 Subject: [PATCH] =?UTF-8?q?=E9=80=82=E9=85=8D=20hiviwex,=20=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E8=BF=9B=E7=A8=8B=E9=80=80=E5=87=BA=E6=94=B6=E4=B8=8D?= =?UTF-8?q?=E5=88=B0=E7=9B=91=E5=90=AC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit Signed-off-by: ZhangJianxin Change-Id: I51e794c3029716333f78d98674c17f2ef30649d1 --- services/include/application_state_observer.h | 11 ++++++---- .../src/cxx/application_state_observer.cpp | 19 ++++++++++------- services/src/manage/app_state.rs | 21 ++++++++++++------- services/src/manage/task_manager.rs | 10 ++------- 4 files changed, 34 insertions(+), 27 deletions(-) diff --git a/services/include/application_state_observer.h b/services/include/application_state_observer.h index 81e28797..7d205d63 100644 --- a/services/include/application_state_observer.h +++ b/services/include/application_state_observer.h @@ -22,15 +22,17 @@ #include #include "application_state_observer_stub.h" +#include "c_string_wrapper.h" namespace OHOS::Request { class ApplicationStateObserver { public: ~ApplicationStateObserver(); using RegCallBack = std::function; + using ProcessCallBack = std::function; static ApplicationStateObserver &GetInstance(); bool RegisterAppStateChanged(RegCallBack &&callback); - void RegisterProcessStateChanged(RegCallBack &&callback); + void RegisterProcessDied(ProcessCallBack &&callback); public: class AppProcessState : public AppExecFwk::ApplicationStateObserverStub { @@ -47,12 +49,12 @@ public: public: void RunAppStateCallback(int32_t uid, int32_t state, int32_t pid); - void RunProcessStateCallback(int32_t uid, int32_t state, int32_t pid); + void RunProcessDiedCallback(int32_t uid, int32_t state, int32_t pid, const std::string &bundleName); ApplicationStateObserver &appStateObserver_; }; ApplicationStateObserver(); RegCallBack appStateCallback_ = nullptr; - RegCallBack processCallback_ = nullptr; + ProcessCallBack processCallback_ = nullptr; }; } // namespace OHOS::Request @@ -61,8 +63,9 @@ extern "C" { #endif typedef void (*APPStateCallback)(int32_t, int32_t, int32_t); +typedef void (*ProcessStateCallback)(int32_t, int32_t, int32_t, CStringWrapper); void RegisterAPPStateCallback(APPStateCallback fun); -void RegisterProcessStateCallback(APPStateCallback fun); +void RegisterProcessDiedCallback(ProcessStateCallback fun); #ifdef __cplusplus } diff --git a/services/src/cxx/application_state_observer.cpp b/services/src/cxx/application_state_observer.cpp index 3c34531f..c5cfd876 100644 --- a/services/src/cxx/application_state_observer.cpp +++ b/services/src/cxx/application_state_observer.cpp @@ -74,7 +74,7 @@ bool ApplicationStateObserver::RegisterAppStateChanged(RegCallBack &&callback) return false; } -void ApplicationStateObserver::RegisterProcessStateChanged(RegCallBack &&callback) +void ApplicationStateObserver::RegisterProcessDied(ProcessCallBack &&callback) { processCallback_ = callback; } @@ -105,7 +105,8 @@ void ApplicationStateObserver::AppProcessState::OnProcessDied(const AppExecFwk:: { REQUEST_HILOGD("OnProcessDied uid=%{public}d, bundleName=%{public}s, state=%{public}d, pid=%{public}d", processData.uid, processData.bundleName.c_str(), static_cast(processData.state), processData.pid); - RunProcessStateCallback(processData.uid, static_cast(processData.state), processData.pid); + RunProcessDiedCallback( + processData.uid, static_cast(processData.state), processData.pid, processData.bundleName); } void ApplicationStateObserver::AppProcessState::RunAppStateCallback(int32_t uid, int32_t state, int32_t pid) @@ -117,13 +118,15 @@ void ApplicationStateObserver::AppProcessState::RunAppStateCallback(int32_t uid, appStateObserver_.appStateCallback_(uid, state, pid); } -void ApplicationStateObserver::AppProcessState::RunProcessStateCallback(int32_t uid, int32_t state, int32_t pid) +void ApplicationStateObserver::AppProcessState::RunProcessDiedCallback( + int32_t uid, int32_t state, int32_t pid, const std::string &bundleName) { if (appStateObserver_.processCallback_ == nullptr) { REQUEST_HILOGE("processStateObserver callback is nullptr"); return; } - appStateObserver_.processCallback_(uid, state, pid); + CStringWrapper name = WrapperCString(bundleName); + appStateObserver_.processCallback_(uid, state, pid, name); } } // namespace OHOS::Request @@ -134,8 +137,8 @@ void RegisterAPPStateCallback(APPStateCallback fun) REQUEST_HILOGD("running RegisterAPPStateCallback"); } -void RegisterProcessStateCallback(APPStateCallback fun) +void RegisterProcessDiedCallback(ProcessStateCallback fun) { - ApplicationStateObserver::GetInstance().RegisterProcessStateChanged(fun); - REQUEST_HILOGD("running RegisterProcessStateCallback"); -} \ No newline at end of file + ApplicationStateObserver::GetInstance().RegisterProcessDied(fun); + REQUEST_HILOGD("running RegisterProcessDiedCallback"); +} diff --git a/services/src/manage/app_state.rs b/services/src/manage/app_state.rs index 3af5e79f..01c32570 100644 --- a/services/src/manage/app_state.rs +++ b/services/src/manage/app_state.rs @@ -17,6 +17,7 @@ use std::sync::Once; use super::task_manager::TaskManagerTx; use crate::manage::events::{StateEvent, TaskManagerEvent}; use crate::service::client::ClientManagerEntry; +use crate::utils::c_wrapper::CStringWrapper; use crate::utils::{CommonEventSubscriber, CommonEventWant}; pub(crate) struct AppStateListener { @@ -37,7 +38,7 @@ impl AppStateListener { }); }); RegisterAPPStateCallback(app_state_change_callback); - RegisterProcessStateCallback(process_state_change_callback); + RegisterProcessDiedCallback(process_died_callback); } } @@ -45,7 +46,7 @@ impl AppStateListener { if ONCE.is_completed() { unsafe { RegisterAPPStateCallback(app_state_change_callback); - RegisterProcessStateCallback(process_state_change_callback); + RegisterProcessDiedCallback(process_died_callback); } } } @@ -69,19 +70,25 @@ extern "C" fn app_state_change_callback(uid: i32, state: i32, _pid: i32) { } } -extern "C" fn process_state_change_callback(uid: i32, state: i32, pid: i32) { +extern "C" fn process_died_callback(uid: i32, state: i32, pid: i32, bundle_name: CStringWrapper) { debug!( "Receives process change, uid {} pid {} state {}", uid, pid, state ); - if state == 5 { - info!("Receives process died, uid {} pid {}", uid, pid); + let name = bundle_name.to_string(); + if name.starts_with("com.") && name.ends_with(".hmos.hiviewx") { unsafe { APP_STATE_LISTENER .assume_init_ref() .task_manager - .notify_process_terminate(uid as u64, pid as u64); + .notify_special_process_terminate(uid as u64); + } + info!("hiviewx terminate. {:?}, {:?}", uid, pid); + } + if state == 5 { + info!("Receives process died, uid {} pid {}", uid, pid); + unsafe { APP_STATE_LISTENER .assume_init_ref() .client_manager @@ -115,5 +122,5 @@ impl CommonEventSubscriber for AppUninstallSubscriber { #[cfg(feature = "oh")] extern "C" { fn RegisterAPPStateCallback(f: extern "C" fn(i32, i32, i32)); - fn RegisterProcessStateCallback(f: extern "C" fn(i32, i32, i32)); + fn RegisterProcessDiedCallback(f: extern "C" fn(i32, i32, i32, CStringWrapper)); } diff --git a/services/src/manage/task_manager.rs b/services/src/manage/task_manager.rs index 9fb85653..e57a6fec 100644 --- a/services/src/manage/task_manager.rs +++ b/services/src/manage/task_manager.rs @@ -41,7 +41,6 @@ use crate::manage::scheduler::Scheduler; use crate::service::client::ClientManagerEntry; use crate::service::notification_bar::subscribe_notification_bar; use crate::service::run_count::RunCountManagerEntry; -use crate::task::bundle::get_name_and_index; use crate::utils::{runtime_spawn, subscribe_common_event}; const CLEAR_INTERVAL: u64 = 30 * 60; @@ -391,13 +390,8 @@ impl TaskManagerTx { let _ = self.send_event(TaskManagerEvent::State(StateEvent::BackgroundTimeout(uid))); } - pub(crate) fn notify_process_terminate(&self, uid: u64, pid: u64) { - if let Some((_index, name)) = get_name_and_index(uid as i32) { - if name.starts_with("com.") && name.ends_with(".hmos.hiviewx") { - info!("hiviewx terminate. {:?}, {:?}", uid, pid); - let _ = self.send_event(TaskManagerEvent::State(StateEvent::SpecialTerminate(uid))); - } - } + pub(crate) fn notify_special_process_terminate(&self, uid: u64) { + let _ = self.send_event(TaskManagerEvent::State(StateEvent::SpecialTerminate(uid))); } pub(crate) fn show(&self, uid: u64, task_id: u32) -> Option {