UIExtension生命周期调整

Signed-off-by: jsjzju <jinsenjun@huawei.com>
Change-Id: I88956a6db3798dea60e77a1a24633af7dd96c5be
This commit is contained in:
jsjzju 2023-12-04 20:56:57 +08:00
parent 19c273bdc7
commit 74f678ee33
19 changed files with 141 additions and 50 deletions

View File

@ -110,17 +110,17 @@ void JsActionExtension::OnCommand(const AAFwk::Want &want, bool restart, int32_t
jsUIExtensionBase_->OnCommand(want, restart, startId);
}
void JsActionExtension::OnForeground(const Want &want)
void JsActionExtension::OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HILOG_DEBUG("called.");
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
Extension::OnForeground(want);
Extension::OnForeground(want, sessionInfo);
if (jsUIExtensionBase_ == nullptr) {
HILOG_ERROR("jsUIExtensionBase_ is nullptr");
return;
}
jsUIExtensionBase_->OnForeground(want);
jsUIExtensionBase_->OnForeground(want, sessionInfo);
}
void JsActionExtension::OnBackground()

View File

@ -403,10 +403,11 @@ void JsAutoFillExtension::OnCommand(const AAFwk::Want &want, bool restart, int s
HILOG_DEBUG("End.");
}
void JsAutoFillExtension::OnForeground(const Want &want)
void JsAutoFillExtension::OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HILOG_DEBUG("Called.");
Extension::OnForeground(want);
Extension::OnForeground(want, sessionInfo);
ForegroundWindow(want, sessionInfo);
HandleScope handleScope(jsRuntime_);
CallObjectMethod("onForeground");
}

View File

@ -110,7 +110,7 @@ void Extension::OnCommandWindow(const AAFwk::Want &want, const sptr<AAFwk::Sessi
HILOG_DEBUG("call");
}
void Extension::OnForeground(const AAFwk::Want &want)
void Extension::OnForeground(const AAFwk::Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
HILOG_DEBUG("extension:%{public}s.", abilityInfo_->name.c_str());

View File

@ -91,7 +91,10 @@ void ExtensionImpl::HandleExtensionTransaction(const Want &want, const AAFwk::Li
break;
}
case AAFwk::ABILITY_STATE_FOREGROUND_NEW: {
Foreground(want);
if (lifecycleState_ == AAFwk::ABILITY_STATE_INITIAL) {
Start(want, sessionInfo);
}
Foreground(want, sessionInfo);
break;
}
case AAFwk::ABILITY_STATE_BACKGROUND_NEW: {
@ -423,7 +426,7 @@ void ExtensionImpl::SendResult(int requestCode, int resultCode, const Want &resu
HILOG_DEBUG("end.");
}
void ExtensionImpl::Foreground(const Want &want)
void ExtensionImpl::Foreground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HILOG_DEBUG("ExtensionImpl::Foreground begin");
if (extension_ == nullptr) {
@ -431,7 +434,7 @@ void ExtensionImpl::Foreground(const Want &want)
return;
}
extension_->OnForeground(want);
extension_->OnForeground(want, sessionInfo);
lifecycleState_ = AAFwk::ABILITY_STATE_FOREGROUND_NEW;
}

View File

@ -110,17 +110,17 @@ void JsShareExtension::OnCommand(const AAFwk::Want &want, bool restart, int32_t
jsUIExtensionBase_->OnCommand(want, restart, startId);
}
void JsShareExtension::OnForeground(const Want &want)
void JsShareExtension::OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HILOG_DEBUG("called.");
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
Extension::OnForeground(want);
Extension::OnForeground(want, sessionInfo);
if (jsUIExtensionBase_ == nullptr) {
HILOG_ERROR("jsUIExtensionBase_ is nullptr");
return;
}
jsUIExtensionBase_->OnForeground(want);
jsUIExtensionBase_->OnForeground(want, sessionInfo);
}
void JsShareExtension::OnBackground()

View File

@ -528,11 +528,13 @@ void JsUIExtension::OnCommand(const AAFwk::Want &want, bool restart, int startId
HILOG_DEBUG("JsUIExtension OnCommand end.");
}
void JsUIExtension::OnForeground(const Want &want)
void JsUIExtension::OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
HILOG_DEBUG("JsUIExtension OnForeground begin.");
Extension::OnForeground(want);
Extension::OnForeground(want, sessionInfo);
ForegroundWindow(want, sessionInfo);
HandleScope handleScope(jsRuntime_);
CallObjectMethod("onForeground");

View File

@ -365,9 +365,10 @@ void JsUIExtensionBase::OnCommand(const AAFwk::Want &want, bool restart, int32_t
CallObjectMethod("onRequest", argv, ARGC_TWO);
}
void JsUIExtensionBase::OnForeground(const Want &want)
void JsUIExtensionBase::OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo)
{
HILOG_DEBUG("called");
ForegroundWindow(want, sessionInfo);
HandleScope handleScope(jsRuntime_);
CallObjectMethod("onForeground");
}

View File

@ -103,7 +103,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
void OnForeground(const Want &want) override;
void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -97,7 +97,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
void OnForeground(const Want &want) override;
void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -204,7 +204,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
virtual void OnForeground(const Want &want);
virtual void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -182,7 +182,7 @@ protected:
*
* @param want The Want object to switch the life cycle.
*/
void Foreground(const Want &want);
void Foreground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
/**
* @brief Toggles the lifecycle status of Extension to AAFwk::ABILITY_STATE_BACKGROUND. And notifies the

View File

@ -105,7 +105,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
void OnForeground(const Want &want) override;
void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -150,7 +150,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
virtual void OnForeground(const Want &want) override;
virtual void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo) override;
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -106,7 +106,7 @@ public:
* The extension in the <b>STATE_FOREGROUND</b> state is visible.
* You can override this function to implement your own processing logic.
*/
void OnForeground(const Want &want);
void OnForeground(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo);
/**
* @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state.

View File

@ -353,6 +353,7 @@ private:
void HandleCommandTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
void HandleCommandWindowTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord,
const sptr<SessionInfo> &sessionInfo, WindowCommand winCmd);
void HandleForegroundTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord);
void HandleRestartResidentTask(const AbilityRequest &abilityRequest);
void HandleActiveAbility(std::shared_ptr<AbilityRecord> &targetService,
std::shared_ptr<ConnectionRecord> &connectRecord);
@ -464,6 +465,9 @@ private:
void HandleInactiveTimeout(const std::shared_ptr<AbilityRecord> &ability);
void MoveToTerminatingMap(const std::shared_ptr<AbilityRecord>& abilityRecord);
void DoForegroundUIExtension(std::shared_ptr<AbilityRecord> abilityRecord, const AbilityRequest &abilityRequest);
void SaveUIExtRequestSessionInfo(std::shared_ptr<AbilityRecord> abilityRecord, sptr<SessionInfo> sessionInfo);
/**
* When a service is under starting, enque the request and handle it after the service starting completes
*/

View File

@ -341,7 +341,7 @@ public:
*
*/
void ForegroundAbility(uint32_t sceneFlag = 0);
void ForegroundAbility(const Closure &task, uint32_t sceneFlag = 0);
void ForegroundAbility(const Closure &task, sptr<SessionInfo> sessionInfo = nullptr, uint32_t sceneFlag = 0);
/**
* process request of foregrounding the ability.
@ -421,6 +421,16 @@ public:
return sessionInfo_;
}
sptr<SessionInfo> GetUIExtRequestSessionInfo() const
{
return uiExtRequestSessionInfo_;
}
void SetUIExtRequestSessionInfo(sptr<SessionInfo> sessionInfo)
{
uiExtRequestSessionInfo_ = sessionInfo;
}
/**
* get ability's token.
*
@ -1109,6 +1119,7 @@ private:
sptr<SessionInfo> sessionInfo_ = nullptr;
sptr<AbilityAppStateObserver> abilityAppStateObserver_;
std::map<uint64_t, AbilityWindowState> abilityWindowStateMap_;
sptr<SessionInfo> uiExtRequestSessionInfo_ = nullptr;
#ifdef SUPPORT_GRAPHICS
bool isStartingWindow_ = false;

View File

@ -50,11 +50,13 @@ const int LOAD_TIMEOUT_MULTIPLE = 150;
const int CONNECT_TIMEOUT_MULTIPLE = 45;
const int COMMAND_TIMEOUT_MULTIPLE = 75;
const int COMMAND_WINDOW_TIMEOUT_MULTIPLE = 75;
const int UI_EXTENSION_CONSUME_SESSION_TIMEOUT_MULTIPLE = 45;
#else
const int LOAD_TIMEOUT_MULTIPLE = 10;
const int CONNECT_TIMEOUT_MULTIPLE = 3;
const int COMMAND_TIMEOUT_MULTIPLE = 5;
const int COMMAND_WINDOW_TIMEOUT_MULTIPLE = 5;
const int UI_EXTENSION_CONSUME_SESSION_TIMEOUT_MULTIPLE = 3;
#endif
const int32_t AUTO_DISCONNECT_INFINITY = -1;
const std::unordered_map<std::string, std::string> trustMap = {
@ -142,6 +144,9 @@ int AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityReque
auto &remoteObj = abilityRequest.sessionInfo->sessionToken;
uiExtensionMap_[remoteObj] = UIExtWindowMapValType(targetService, abilityRequest.sessionInfo);
AddUIExtWindowDeathRecipient(remoteObj);
if (!isLoadedAbility) {
SaveUIExtRequestSessionInfo(targetService, abilityRequest.sessionInfo);
}
}
if (!isLoadedAbility) {
@ -151,12 +156,8 @@ int AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityReque
// It may have been started through connect
targetService->SetWant(abilityRequest.want);
CommandAbility(targetService);
} else if (IsUIExtensionAbility(targetService) &&
targetService->IsReady() && !targetService->IsAbilityState(AbilityState::INACTIVATING) &&
!targetService->IsAbilityState(AbilityState::BACKGROUNDING) &&
targetService->IsAbilityWindowReady()) {
targetService->SetWant(abilityRequest.want);
CommandAbilityWindow(targetService, abilityRequest.sessionInfo, WIN_CMD_FOREGROUND);
} else if (IsUIExtensionAbility(targetService)) {
DoForegroundUIExtension(targetService, abilityRequest);
} else {
HILOG_INFO("Target service is already activating.");
EnqueueStartServiceReq(abilityRequest);
@ -172,6 +173,48 @@ int AbilityConnectManager::StartAbilityLocked(const AbilityRequest &abilityReque
return ERR_OK;
}
void AbilityConnectManager::DoForegroundUIExtension(std::shared_ptr<AbilityRecord> abilityRecord,
const AbilityRequest &abilityRequest)
{
CHECK_POINTER(abilityRecord);
if (abilityRecord->IsReady() && !abilityRecord->IsAbilityState(AbilityState::INACTIVATING) &&
!abilityRecord->IsAbilityState(AbilityState::FOREGROUNDING) &&
!abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING) &&
abilityRecord->IsAbilityWindowReady()) {
if (abilityRecord->IsAbilityState(AbilityState::FOREGROUND)) {
abilityRecord->SetWant(abilityRequest.want);
CommandAbilityWindow(abilityRecord, abilityRequest.sessionInfo, WIN_CMD_FOREGROUND);
return;
} else {
if (abilityRecord->GetUIExtRequestSessionInfo() == nullptr) {
abilityRecord->SetWant(abilityRequest.want);
SaveUIExtRequestSessionInfo(abilityRecord, abilityRequest.sessionInfo);
DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(abilityRecord->GetToken());
return;
}
}
}
EnqueueStartServiceReq(abilityRequest);
}
void AbilityConnectManager::SaveUIExtRequestSessionInfo(std::shared_ptr<AbilityRecord> abilityRecord,
sptr<SessionInfo> sessionInfo)
{
CHECK_POINTER(abilityRecord);
CHECK_POINTER(taskHandler_);
abilityRecord->SetUIExtRequestSessionInfo(sessionInfo);
auto callback = [abilityRecord, connectManager = shared_from_this()]() {
std::lock_guard guard{connectManager->Lock_};
HILOG_ERROR("consume session timeout, abilityUri: %{public}s", abilityRecord->GetURI().c_str());
abilityRecord->SetUIExtRequestSessionInfo(nullptr);
};
int consumeSessionTimeout = AmsConfigurationParameter::GetInstance().GetAppStartTimeoutTime() *
UI_EXTENSION_CONSUME_SESSION_TIMEOUT_MULTIPLE;
std::string taskName = std::string("ConsumeSessionTimeout_") + std::to_string(abilityRecord->GetRecordId());
taskHandler_->SubmitTask(callback, taskName, consumeSessionTimeout);
}
void AbilityConnectManager::EnqueueStartServiceReq(const AbilityRequest &abilityRequest)
{
std::lock_guard guard(startServiceReqListLock_);
@ -561,7 +604,11 @@ int AbilityConnectManager::AttachAbilityThreadLocked(
std::string element = abilityRecord->GetURI();
HILOG_DEBUG("Ability: %{public}s", element.c_str());
abilityRecord->SetScheduler(scheduler);
abilityRecord->Inactivate();
if (IsUIExtensionAbility(abilityRecord) && !abilityRecord->IsCreateByConnect()) {
DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token);
} else {
abilityRecord->Inactivate();
}
return ERR_OK;
}
@ -822,16 +869,6 @@ int AbilityConnectManager::ScheduleCommandAbilityWindowDone(
}
switch (abilityCmd) {
case ABILITY_CMD_FOREGROUND: {
if (abilityRecord->IsAbilityState(AbilityState::INACTIVE) ||
abilityRecord->IsAbilityState(AbilityState::ACTIVE) ||
abilityRecord->IsAbilityState(AbilityState::BACKGROUND) ||
abilityRecord->IsAbilityState(AbilityState::BACKGROUNDING)) {
HILOG_DEBUG("Foreground %{public}s", element.c_str());
DelayedSingleton<AppScheduler>::GetInstance()->MoveToForeground(token);
}
break;
}
case ABILITY_CMD_BACKGROUND: {
if (abilityRecord->IsAbilityState(AbilityState::INACTIVE) ||
abilityRecord->IsAbilityState(AbilityState::ACTIVE) ||
@ -913,6 +950,9 @@ void AbilityConnectManager::CompleteStartServiceReq(const std::string &serviceUr
if (it != startServiceReqList_.end()) {
reqList = it->second;
startServiceReqList_.erase(it);
if (taskHandler_) {
taskHandler_->CancelTask(std::string("start_service_timeout:") + serviceUri);
}
}
}
@ -1294,8 +1334,6 @@ int AbilityConnectManager::DispatchInactive(const std::shared_ptr<AbilityRecord>
abilityRecord->SetAbilityState(AbilityState::INACTIVE);
if (abilityRecord->IsCreateByConnect()) {
ConnectAbility(abilityRecord);
} else if (IsUIExtensionAbility(abilityRecord)) {
CommandAbilityWindow(abilityRecord, abilityRecord->GetSessionInfo(), WIN_CMD_FOREGROUND);
} else {
CommandAbility(abilityRecord);
if (abilityRecord->GetConnectRecordList().size() > 0) {
@ -2043,8 +2081,20 @@ void AbilityConnectManager::MoveToForeground(const std::shared_ptr<AbilityRecord
}
HILOG_ERROR("move to foreground timeout.");
selfObj->PrintTimeOutLog(abilityRecord, AbilityManagerService::FOREGROUND_TIMEOUT_MSG);
selfObj->HandleForegroundTimeoutTask(abilityRecord);
};
abilityRecord->ForegroundAbility(task);
auto sessionInfo = abilityRecord->GetUIExtRequestSessionInfo();
if (sessionInfo != nullptr) {
abilityRecord->ForegroundAbility(task, sessionInfo);
abilityRecord->SetUIExtRequestSessionInfo(nullptr);
} else {
HILOG_WARN("SessionInfo is nullptr. Move to background");
abilityRecord->SetAbilityState(AbilityState::BACKGROUND);
DelayedSingleton<AppScheduler>::GetInstance()->MoveToBackground(abilityRecord->GetToken());
}
if (taskHandler_) {
taskHandler_->CancelTask(std::string("ConsumeSessionTimeout_") + std::to_string(abilityRecord->GetRecordId()));
}
}
void AbilityConnectManager::MoveToBackground(const std::shared_ptr<AbilityRecord> &abilityRecord)
@ -2075,17 +2125,37 @@ void AbilityConnectManager::MoveToBackground(const std::shared_ptr<AbilityRecord
void AbilityConnectManager::CompleteForeground(const std::shared_ptr<AbilityRecord> &abilityRecord)
{
std::lock_guard guard(Lock_);
if (abilityRecord == nullptr) {
HILOG_ERROR("abilityRecord is nullptr");
return;
}
if (abilityRecord->GetAbilityState() != AbilityState::FOREGROUNDING) {
HILOG_ERROR("Ability state is %{public}d, it can't complete foreground.", abilityRecord->GetAbilityState());
return;
}
abilityRecord->SetAbilityState(AbilityState::FOREGROUND);
CompleteStartServiceReq(abilityRecord->GetURI());
}
void AbilityConnectManager::HandleForegroundTimeoutTask(const std::shared_ptr<AbilityRecord> &abilityRecord)
{
std::lock_guard guard(Lock_);
if (abilityRecord == nullptr) {
HILOG_ERROR("abilityRecord is nullptr");
return;
}
abilityRecord->SetAbilityState(AbilityState::BACKGROUND);
CompleteStartServiceReq(abilityRecord->GetURI());
}
void AbilityConnectManager::CompleteBackground(const std::shared_ptr<AbilityRecord> &abilityRecord)
{
std::lock_guard guard(Lock_);
if (abilityRecord == nullptr) {
HILOG_ERROR("abilityRecord is nullptr");
return;
}
if (abilityRecord->GetAbilityState() != AbilityState::BACKGROUNDING) {
HILOG_ERROR("Ability state is %{public}d, it can't complete background.", abilityRecord->GetAbilityState());
return;

View File

@ -437,7 +437,7 @@ void AbilityRecord::ForegroundAbility(uint32_t sceneFlag)
}
}
void AbilityRecord::ForegroundAbility(const Closure &task, uint32_t sceneFlag)
void AbilityRecord::ForegroundAbility(const Closure &task, sptr<SessionInfo> sessionInfo, uint32_t sceneFlag)
{
HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
HILOG_INFO("name:%{public}s.", abilityInfo_.name.c_str());
@ -464,7 +464,7 @@ void AbilityRecord::ForegroundAbility(const Closure &task, uint32_t sceneFlag)
// earlier than above actions.
SetAbilityStateInner(AbilityState::FOREGROUNDING);
lifeCycleStateInfo_.sceneFlag = sceneFlag;
lifecycleDeal_->ForegroundNew(GetWant(), lifeCycleStateInfo_, sessionInfo_);
lifecycleDeal_->ForegroundNew(GetWant(), lifeCycleStateInfo_, sessionInfo);
lifeCycleStateInfo_.sceneFlag = 0;
lifeCycleStateInfo_.sceneFlagBak = 0;
{
@ -3116,10 +3116,7 @@ void AbilityRecord::SetAttachDebug(const bool isAttachDebug)
void AbilityRecord::AddAbilityWindowStateMap(uint64_t uiExtensionComponentId,
AbilityWindowState abilityWindowState)
{
if (abilityWindowState == AbilityWindowState::FOREGROUNDING ||
abilityWindowStateMap_.find(uiExtensionComponentId) != abilityWindowStateMap_.end()) {
abilityWindowStateMap_[uiExtensionComponentId] = abilityWindowState;
}
abilityWindowStateMap_[uiExtensionComponentId] = abilityWindowState;
}
void AbilityRecord::RemoveAbilityWindowStateMap(uint64_t uiExtensionComponentId)

View File

@ -384,7 +384,9 @@ HWTEST_F(ExtensionTest, AaFwk_Extension_1800, Function | MediumTest | Level1)
Want want;
want.SetElementName("DemoDeviceId", "DemoBundleName", "DemoAbilityName");
EXPECT_TRUE(extension_ != nullptr);
extension_->OnForeground(want);
sptr<AAFwk::SessionInfo> session = new (std::nothrow) AAFwk::SessionInfo();
EXPECT_NE(session, nullptr);
extension_->OnForeground(want, session);
GTEST_LOG_(INFO) << "AaFwk_Extension_1800 end";
}