!8693 快速拉起退出,再次拉起失败

Merge pull request !8693 from savior-xzh/onprocess
This commit is contained in:
openharmony_ci
2024-05-25 04:20:39 +00:00
committed by Gitee
15 changed files with 117 additions and 109 deletions
@@ -62,12 +62,19 @@ public:
* @param bundleInfos resident process bundle infos.
*/
virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
/**
* @brief Notify abilityms app process OnRemoteDied
* @param abilityTokens abilities in died process.
*/
virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) override;
private:
int32_t HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply);
int32_t HandleOnAbilityRequestDone(MessageParcel &data, MessageParcel &reply);
int32_t HandleNotifyConfigurationChange(MessageParcel &data, MessageParcel &reply);
int32_t HandleNotifyStartResidentProcess(MessageParcel &data, MessageParcel &reply);
int32_t HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply);
using AppStateCallbackFunc = int32_t (AppStateCallbackHost::*)(MessageParcel &data, MessageParcel &reply);
std::map<uint32_t, AppStateCallbackFunc> memberFuncMap_;
@@ -57,6 +57,12 @@ public:
*/
virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
/**
* @brief Notify abilityms app process OnRemoteDied
* @param abilityTokens abilities in died process.
*/
virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) override;
private:
bool WriteInterfaceToken(MessageParcel &data);
static inline BrokerDelegator<AppStateCallbackProxy> delegator_;
@@ -63,11 +63,18 @@ public:
*/
virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) {}
/**
* @brief Notify abilityms app process OnRemoteDied
* @param abilityTokens abilities in died process.
*/
virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) {}
enum class Message {
TRANSACT_ON_APP_STATE_CHANGED = 0,
TRANSACT_ON_ABILITY_REQUEST_DONE,
TRANSACT_ON_NOTIFY_CONFIG_CHANGE,
TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS
TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS,
TRANSACT_ON_APP_REMOTE_DIED
};
};
} // namespace AppExecFwk
@@ -38,6 +38,8 @@ AppStateCallbackHost::AppStateCallbackHost()
&AppStateCallbackHost::HandleNotifyConfigurationChange;
memberFuncMap_[static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_NOTIFY_START_RESIDENT_PROCESS)] =
&AppStateCallbackHost::HandleNotifyStartResidentProcess;
memberFuncMap_[static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED)] =
&AppStateCallbackHost::HandleOnAppRemoteDied;
}
AppStateCallbackHost::~AppStateCallbackHost()
@@ -87,6 +89,11 @@ void AppStateCallbackHost::NotifyStartResidentProcess(std::vector<AppExecFwk::Bu
TAG_LOGD(AAFwkTag::APPMGR, "called");
}
void AppStateCallbackHost::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
{
TAG_LOGD(AAFwkTag::APPMGR, "called");
}
int32_t AppStateCallbackHost::HandleOnAppStateChanged(MessageParcel &data, MessageParcel &reply)
{
HITRACE_METER(HITRACE_TAG_APP);
@@ -143,5 +150,25 @@ int32_t AppStateCallbackHost::HandleNotifyStartResidentProcess(MessageParcel &da
NotifyStartResidentProcess(bundleInfos);
return NO_ERROR;
}
int32_t AppStateCallbackHost::HandleOnAppRemoteDied(MessageParcel &data, MessageParcel &reply)
{
std::vector<sptr<IRemoteObject>> abilityTokens;
int32_t infoSize = data.ReadInt32();
if (infoSize > CYCLE_LIMIT) {
TAG_LOGE(AAFwkTag::APPMGR, "infoSize is too large");
return ERR_INVALID_VALUE;
}
for (int32_t i = 0; i < infoSize; i++) {
sptr<IRemoteObject> obj = data.ReadRemoteObject();
if (!obj) {
TAG_LOGE(AAFwkTag::APPMGR, "Read token failed.");
return ERR_INVALID_VALUE;
}
abilityTokens.emplace_back(obj);
}
OnAppRemoteDied(abilityTokens);
return NO_ERROR;
}
} // namespace AppExecFwk
} // namespace OHOS
@@ -138,6 +138,35 @@ void AppStateCallbackProxy::NotifyStartResidentProcess(std::vector<AppExecFwk::B
}
}
void AppStateCallbackProxy::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
{
MessageParcel data;
MessageParcel reply;
MessageOption option;
if (!WriteInterfaceToken(data)) {
TAG_LOGE(AAFwkTag::APPMGR, "WriteInterfaceToken failed");
return;
}
if (!data.WriteInt32(abilityTokens.size())) {
TAG_LOGE(AAFwkTag::APPMGR, "write token size failed.");
return;
}
for (auto &token : abilityTokens) {
if (!data.WriteRemoteObject(token.GetRefPtr())) {
TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
return;
}
}
auto ret = SendTransactCmd(
static_cast<uint32_t>(IAppStateCallback::Message::TRANSACT_ON_APP_REMOTE_DIED),
data, reply, option);
if (ret != NO_ERROR) {
TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
}
}
int32_t AppStateCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
MessageParcel &reply, MessageOption &option)
{
-1
View File
@@ -14,7 +14,6 @@
import("//foundation/ability/ability_runtime/ability_runtime.gni")
abilityms_files = [
"src/ability_app_state_observer.cpp",
"src/preload_uiext_state_observer.cpp",
"src/ability_background_connection.cpp",
"src/ability_connect_manager.cpp",
@@ -1,36 +0,0 @@
/*
* Copyright (c) 2023 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#ifndef OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H
#define OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H
#include <memory>
#include "application_state_observer_stub.h"
namespace OHOS {
namespace AAFwk {
class AbilityRecord;
class AbilityAppStateObserver : public AppExecFwk::ApplicationStateObserverStub {
public:
AbilityAppStateObserver(std::shared_ptr<AbilityRecord> abilityRecord);
virtual void OnProcessDied(const AppExecFwk::ProcessData &processData) override;
private:
std::weak_ptr<AbilityRecord> abilityRecord_;
};
} // namespace AAFwk
} // namespace OHOS
#endif // OHOS_ABILITY_RUNTIME_ABILITY_APP_STATE_OBSERVER_H
@@ -1751,6 +1751,8 @@ protected:
void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) override;
private:
int TerminateAbilityWithFlag(const sptr<IRemoteObject> &token, int resultCode = DEFAULT_INVAL_VALUE,
const Want *resultWant = nullptr, bool flag = true);
@@ -57,7 +57,6 @@ class ConnectionRecord;
class Mission;
class MissionList;
class CallContainer;
class AbilityAppStateObserver;
constexpr const char* ABILITY_TOKEN_NAME = "AbilityToken";
constexpr const char* LAUNCHER_BUNDLE_NAME = "com.ohos.launcher";
@@ -1001,7 +1000,6 @@ private:
*/
void GetAbilityTypeString(std::string &typeStr);
void OnSchedulerDied(const wptr<IRemoteObject> &remote);
void RemoveAppStateObserver(bool force = false);
void GrantUriPermission(Want &want, std::string targetBundleName, bool isSandboxApp, uint32_t tokenId);
void GrantDmsUriPermission(Want &want, std::string targetBundleName);
bool IsDmsCall(Want &want);
@@ -1177,7 +1175,6 @@ private:
// scene session
sptr<SessionInfo> sessionInfo_ = nullptr;
mutable ffrt::mutex sessionLock_;
sptr<AbilityAppStateObserver> abilityAppStateObserver_;
std::map<uint64_t, AbilityWindowState> abilityWindowStateMap_;
sptr<SessionInfo> uiExtRequestSessionInfo_ = nullptr;
@@ -93,6 +93,8 @@ public:
virtual void NotifyConfigurationChange(const AppExecFwk::Configuration &config, int32_t userId) {}
virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) {}
virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) {}
};
class StartSpecifiedAbilityResponse : public AppExecFwk::StartSpecifiedAbilityResponseStub {
@@ -461,6 +463,8 @@ protected:
*/
virtual void NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo> &bundleInfos) override;
virtual void OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens) override;
private:
std::mutex lock_;
bool isInit_ {false};
@@ -1,43 +0,0 @@
/*
* Copyright (c) 2023-2024 Huawei Device Co., Ltd.
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
#include "ability_app_state_observer.h"
#include "ability_record.h"
#include "hilog_tag_wrapper.h"
#include "hilog_wrapper.h"
namespace OHOS {
namespace AAFwk {
AbilityAppStateObserver::AbilityAppStateObserver(std::shared_ptr<AbilityRecord> abilityRecord)
: abilityRecord_(abilityRecord) {}
void AbilityAppStateObserver::OnProcessDied(const AppExecFwk::ProcessData &processData)
{
auto abilityRecord = abilityRecord_.lock();
if (abilityRecord) {
const auto &abilityInfo = abilityRecord->GetAbilityInfo();
if (abilityInfo.bundleName == processData.bundleName &&
processData.processType == AppExecFwk::ProcessType::NORMAL &&
abilityInfo.type == AppExecFwk::AbilityType::PAGE) {
abilityRecord->OnProcessDied();
}
if (abilityRecord->IsSceneBoard() && abilityRecord->GetPid() == processData.pid) {
abilityRecord->OnProcessDied();
}
} else {
TAG_LOGW(AAFwkTag::ABILITYMGR, "AbilityRecord null");
}
}
} // namespace AAFwk
} // namespace OHOS
@@ -9869,6 +9869,20 @@ void AbilityManagerService::NotifyStartResidentProcess(std::vector<AppExecFwk::B
}
}
void AbilityManagerService::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
{
std::shared_ptr<AbilityRecord> abilityRecord;
for (auto &token : abilityTokens) {
abilityRecord = Token::GetAbilityRecordByToken(token);
if (abilityRecord == nullptr) {
continue;
}
TAG_LOGI(AAFwkTag::ABILITYMGR, "App OnRemoteDied, ability is %{public}s, app is %{public}s",
abilityRecord->GetAbilityInfo().name.c_str(), abilityRecord->GetAbilityInfo().bundleName.c_str());
abilityRecord->OnProcessDied();
}
}
int32_t AbilityManagerService::OpenFile(const Uri& uri, uint32_t flag)
{
auto accessTokenId = IPCSkeleton::GetCallingTokenID();
+4 -25
View File
@@ -21,7 +21,6 @@
#include <unordered_map>
#include "constants.h"
#include "ability_app_state_observer.h"
#include "ability_event_handler.h"
#include "ability_manager_service.h"
#include "ability_resident_process_rdb.h"
@@ -246,7 +245,6 @@ AbilityRecord::~AbilityRecord()
}
}
want_.CloseAllFd();
RemoveAppStateObserver(true);
}
std::shared_ptr<AbilityRecord> AbilityRecord::CreateAbilityRecord(const AbilityRequest &abilityRequest)
@@ -273,30 +271,9 @@ std::shared_ptr<AbilityRecord> AbilityRecord::CreateAbilityRecord(const AbilityR
abilityRecord->collaboratorType_ = abilityRequest.collaboratorType;
abilityRecord->missionAffinity_ = abilityRequest.want.GetStringParam(PARAM_MISSION_AFFINITY_KEY);
// Before the ability attaches
abilityRecord->abilityAppStateObserver_ = sptr<AbilityAppStateObserver>(
new AbilityAppStateObserver(abilityRecord));
DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->RegisterApplicationStateObserver(
abilityRecord->abilityAppStateObserver_, {abilityRequest.abilityInfo.bundleName});
return abilityRecord;
}
void AbilityRecord::RemoveAppStateObserver(bool force)
{
if (!force && IsSceneBoard()) {
TAG_LOGI(AAFwkTag::ABILITYMGR, "Special ability no need to RemoveAppStateObserver.");
return;
}
auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
if (handler && abilityAppStateObserver_) {
handler->SubmitTask([appStateObserver = abilityAppStateObserver_]() {
DelayedSingleton<AppExecFwk::AppMgrClient>::GetInstance()->UnregisterApplicationStateObserver(
appStateObserver);
});
abilityAppStateObserver_ = nullptr;
}
}
bool AbilityRecord::Init()
{
lifecycleDeal_ = std::make_unique<LifecycleDeal>();
@@ -1424,7 +1401,6 @@ void AbilityRecord::SetScheduler(const sptr<IAbilityScheduler> &scheduler)
}
});
}
RemoveAppStateObserver();
isReady_ = true;
scheduler_ = scheduler;
lifecycleDeal_->SetScheduler(scheduler);
@@ -2390,7 +2366,10 @@ void AbilityRecord::OnSchedulerDied(const wptr<IRemoteObject> &remote)
void AbilityRecord::OnProcessDied()
{
std::lock_guard<ffrt::mutex> guard(lock_);
RemoveAppStateObserver(true);
if (!IsSceneBoard() && scheduler_ != nullptr) {
TAG_LOGD(AAFwkTag::ABILITYMGR, "OnProcessDied: '%{public}s', attached.", abilityInfo_.name.c_str());
return;
}
isWindowAttached_ = false;
auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
@@ -239,6 +239,13 @@ void AppScheduler::NotifyStartResidentProcess(std::vector<AppExecFwk::BundleInfo
callback->NotifyStartResidentProcess(bundleInfos);
}
void AppScheduler::OnAppRemoteDied(const std::vector<sptr<IRemoteObject>> &abilityTokens)
{
auto callback = callback_.lock();
CHECK_POINTER(callback);
callback->OnAppRemoteDied(abilityTokens);
}
int AppScheduler::KillApplication(const std::string &bundleName)
{
TAG_LOGI(AAFwkTag::ABILITYMGR, "[%{public}s(%{public}s)] enter", __FILE__, __FUNCTION__);
@@ -3024,6 +3024,15 @@ void AppMgrServiceInner::OnRemoteDied(const wptr<IRemoteObject> &remote, bool is
return;
}
std::vector<sptr<IRemoteObject>> abilityTokens;
for (const auto &token : appRecord->GetAbilities()) {
abilityTokens.emplace_back(token.first);
}
for (const auto &callback : appStateCallbacks_) {
if (callback != nullptr) {
callback->OnAppRemoteDied(abilityTokens);
}
}
ClearData(appRecord);
}