Signed-off-by: caochunlei <caochunlei1@huawei.com>
This commit is contained in:
caochunlei 2022-08-04 16:58:01 +08:00
parent 463283c767
commit 8561b9d0bb
3 changed files with 78 additions and 3 deletions

View File

@ -19,6 +19,7 @@
#include <mutex>
#include <unordered_map>
#include "application_state_observer_stub.h"
#include "connection_state_item.h"
#include "connection_observer_controller.h"
#include "dlp_state_item.h"
@ -131,7 +132,35 @@ public:
*/
void RemoveDlpAbility(const std::shared_ptr<AbilityRecord> &dlpAbility);
/**
* handle app process died.
*
* @param pid app process pid.
*/
void HandleAppDied(int32_t pid);
private:
class InnerAppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
public:
using ProcessDiedHandler = std::function<void(int32_t)>;
explicit InnerAppStateObserver(const ProcessDiedHandler handler) : handler_(handler) {}
~InnerAppStateObserver() = default;
void OnForegroundApplicationChanged(const AppExecFwk::AppStateData &appStateData) {}
void OnAbilityStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
void OnExtensionStateChanged(const AppExecFwk::AbilityStateData &abilityStateData) {}
void OnProcessCreated(const AppExecFwk::ProcessData &processData) {}
void OnApplicationStateChanged(const AppExecFwk::AppStateData &appStateData) {}
void OnProcessDied(const AppExecFwk::ProcessData &processData)
{
if (handler_) {
handler_(processData.pid);
}
}
private:
ProcessDiedHandler handler_;
};
bool AddConnectionInner(const std::shared_ptr<ConnectionRecord> &connectionRecord,
AbilityRuntime::ConnectionData &data);
bool RemoveConnectionInner(const std::shared_ptr<ConnectionRecord> &connectionRecord,
@ -146,6 +175,7 @@ private:
std::vector<AbilityRuntime::ConnectionData> &allData);
bool HandleDlpAbilityInner(const std::shared_ptr<AbilityRecord> &dlpAbility,
bool isAdd, AbilityRuntime::DlpStateData &dlpData);
void InitAppStateObserver();
private:
std::shared_ptr<ConnectionObserverController> observerController_;
@ -155,6 +185,8 @@ private:
std::recursive_mutex dlpLock_;
std::unordered_map<int32_t, std::shared_ptr<DlpStateItem>> dlpItems_;
sptr<InnerAppStateObserver> appStateObserver_;
};
} // namespace AAFwk
} // namespace OHOS

View File

@ -228,8 +228,6 @@ bool AbilityManagerService::Init()
freeInstallManager_ = std::make_shared<FreeInstallManager>(weak_from_this());
CHECK_POINTER_RETURN_BOOL(freeInstallManager_);
DelayedSingleton<ConnectionStateManager>::GetInstance()->Init();
// init user controller.
userController_ = std::make_shared<UserController>();
userController_->Init();
@ -264,6 +262,8 @@ bool AbilityManagerService::Init()
SubscribeBackgroundTask();
DelayedSingleton<ConnectionStateManager>::GetInstance()->Init();
HILOG_INFO("Init success.");
return true;
}
@ -678,7 +678,7 @@ int AbilityManagerService::StartAbility(const Want &want, const StartOptions &st
HiSysEventType::FAULT, eventInfo);
return ERR_INVALID_VALUE;
}
auto result = CheckCrowdtestForeground(want, requestCode, userId);
if (result != ERR_OK) {
return result;

View File

@ -17,13 +17,27 @@
#include <fstream>
#include "app_mgr_interface.h"
#include "connection_observer_errors.h"
#include "hilog_wrapper.h"
#include "if_system_ability_manager.h"
#include "iservice_registry.h"
#include "system_ability_definition.h"
namespace OHOS {
namespace AAFwk {
namespace {
static const int MAX_PROCESS_LEN = 256;
OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppMgr()
{
OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
if (!systemAbilityManager) {
return nullptr;
}
OHOS::sptr<OHOS::IRemoteObject> object = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID);
return OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(object);
}
}
using namespace OHOS::AbilityRuntime;
@ -48,6 +62,7 @@ void ConnectionStateManager::Init()
if (!observerController_) {
observerController_ = std::make_shared<ConnectionObserverController>();
}
InitAppStateObserver();
}
int ConnectionStateManager::RegisterObserver(const sptr<AbilityRuntime::IConnectionObserver> &observer)
@ -256,6 +271,11 @@ void ConnectionStateManager::RemoveDlpAbility(const std::shared_ptr<AbilityRecor
}
}
void ConnectionStateManager::HandleAppDied(int32_t pid)
{
HandleCallerDied(pid);
}
bool ConnectionStateManager::AddConnectionInner(const std::shared_ptr<ConnectionRecord> &connectionRecord,
AbilityRuntime::ConnectionData &data)
{
@ -445,5 +465,28 @@ bool ConnectionStateManager::HandleDlpAbilityInner(const std::shared_ptr<Ability
return dlpItem->RemoveDlpConnectionState(dlpAbility, dlpData);
}
void ConnectionStateManager::InitAppStateObserver()
{
if (appStateObserver_) {
return;
}
sptr<OHOS::AppExecFwk::IAppMgr> appManager = GetAppMgr();
if (!appManager) {
HILOG_WARN("%{public}s app manager nullptr!", __func__);
return;
}
appStateObserver_ = new (std::nothrow)InnerAppStateObserver([](int32_t pid) {
DelayedSingleton<ConnectionStateManager>::GetInstance()->HandleAppDied(pid);
});
int32_t err = appManager->RegisterApplicationStateObserver(appStateObserver_);
if (err != 0) {
HILOG_ERROR("%{public}s register to appmanager failed. err:%{public}d", __func__, err);
appStateObserver_ = nullptr;
return;
}
}
} // namespace AAFwk
} // namespace OHOS