适配 hiviwex, 修复进程退出收不到监听

Signed-off-by: ZhangJianxin <zhangjianxin23@huawei.com>
Change-Id: I51e794c3029716333f78d98674c17f2ef30649d1
This commit is contained in:
ZhangJianxin 2024-11-14 21:28:24 +08:00
parent 4fb806ffbe
commit da0d462772
4 changed files with 34 additions and 27 deletions

View File

@ -22,15 +22,17 @@
#include <string>
#include "application_state_observer_stub.h"
#include "c_string_wrapper.h"
namespace OHOS::Request {
class ApplicationStateObserver {
public:
~ApplicationStateObserver();
using RegCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid)>;
using ProcessCallBack = std::function<void(int32_t uid, int32_t state, int32_t pid, CStringWrapper bundleName)>;
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
}

View File

@ -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<int32_t>(processData.state), processData.pid);
RunProcessStateCallback(processData.uid, static_cast<int32_t>(processData.state), processData.pid);
RunProcessDiedCallback(
processData.uid, static_cast<int32_t>(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");
}
ApplicationStateObserver::GetInstance().RegisterProcessDied(fun);
REQUEST_HILOGD("running RegisterProcessDiedCallback");
}

View File

@ -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));
}

View File

@ -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<TaskInfo> {