disconnectAbility

Signed-off-by: zhaogan <zhaogan2@huawei.com>
This commit is contained in:
zhaogan 2024-10-30 10:34:15 +08:00
parent 6b8112cbb5
commit c2ba9996b2
8 changed files with 95 additions and 0 deletions

View File

@ -99,6 +99,12 @@ public:
void DisconnectAbility();
bool SendRequest(int32_t code, MessageParcel &data, MessageParcel &reply);
/**
* @brief Executed when a service callback
* @param installResultStr The json string of InstallResult
*/
void OnServiceCenterReceived(std::string installResultStr);
private:
/**
* @brief Notify the service center center to start the installation free process.

View File

@ -47,6 +47,11 @@ enum ServiceCenterFunction {
CONNECT_DELAYED_HEARTBEAT = 8,
};
const std::set<ServiceCenterFunction> DISCONNECT_ABILITY_FUNC = {
ServiceCenterFunction::CONNECT_UPGRADE_CHECK,
ServiceCenterFunction::CONNECT_PRELOAD_INSTALL
};
enum ServiceCenterResultCode {
FREE_INSTALL_OK = 0,
FREE_INSTALL_DOWNLOADING = 1,

View File

@ -39,6 +39,12 @@ public:
* @param installResult the json of InstallResult
*/
virtual int32_t OnDelayedHeartbeat(std::string installResult) = 0;
/**
* @brief Executed when a service callback
* @param installResultStr The json string of InstallResult
*/
virtual int32_t OnServiceCenterReceived(std::string installResultStr) = 0;
};
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -44,6 +44,11 @@ public:
*/
int32_t OnDelayedHeartbeat(std::string installResult);
/**
* @brief Executed when a service callback
* @param installResultStr The json string of InstallResult
*/
int32_t OnServiceCenterReceived(std::string installResultStr);
private:
std::weak_ptr<BundleConnectAbilityMgr> server_;
};

View File

@ -159,11 +159,35 @@ void BundleConnectAbilityMgr::PreloadRequest(int32_t flag, const TargetAbilityIn
LOG_E(BMS_TAG_DEFAULT, "failed to WriteParcelable targetAbilityInfo");
return;
}
sptr<ServiceCenterStatusCallback> callback = new(std::nothrow) ServiceCenterStatusCallback(weak_from_this());
if (callback == nullptr) {
LOG_E(BMS_TAG_DEFAULT, "callback is nullptr");
return;
}
if (!data.WriteRemoteObject(callback)) {
LOG_E(BMS_TAG_DEFAULT, "failed to WriteRemoteObject callbcak");
return;
}
serviceCenterRemoteObject_ = serviceCenterConnection_->GetRemoteObject();
if (serviceCenterRemoteObject_ == nullptr) {
LOG_E(BMS_TAG_DEFAULT, "failed to get remote object");
return;
}
sptr<FreeInstallParams> freeInstallParams = new(std::nothrow) FreeInstallParams();
if (freeInstallParams == nullptr) {
LOG_E(BMS_TAG_DEFAULT, "freeInstallParams is nullptr");
return;
}
freeInstallParams->serviceCenterFunction = ServiceCenterFunction::CONNECT_PRELOAD_INSTALL;
std::unique_lock<std::mutex> lock(mapMutex_);
auto emplaceResult = freeInstallParamsMap_.emplace(targetAbilityInfo.targetInfo.transactId, *freeInstallParams);
LOG_I(BMS_TAG_DEFAULT, "emplace map size = %{public}zu, transactId = %{public}s",
freeInstallParamsMap_.size(), targetAbilityInfo.targetInfo.transactId.c_str());
if (!emplaceResult.second) {
LOG_E(BMS_TAG_DEFAULT, "freeInstallParamsMap emplace error");
return;
}
lock.unlock();
int32_t result = serviceCenterRemoteObject_->SendRequest(flag, data, reply, option);
if (result != ERR_OK) {
LOG_E(BMS_TAG_DEFAULT, "Failed to sendRequest, result = %{public}d", result);
@ -655,6 +679,36 @@ void BundleConnectAbilityMgr::OnDelayedHeartbeat(std::string installResultStr)
LOG_I(BMS_TAG_DEFAULT, "OnDelayedHeartbeat end");
}
void BundleConnectAbilityMgr::OnServiceCenterReceived(std::string installResultStr)
{
LOG_I(BMS_TAG_DEFAULT, "installResultStr = %{public}s", installResultStr.c_str());
InstallResult installResult;
if (!ParseInfoFromJsonStr(installResultStr.c_str(), installResult)) {
LOG_E(BMS_TAG_DEFAULT, "Parse info from json fail");
return;
}
FreeInstallParams freeInstallParams;
std::unique_lock<std::mutex> lock(mapMutex_);
auto node = freeInstallParamsMap_.find(installResult.result.transactId);
if (node == freeInstallParamsMap_.end()) {
LOG_E(BMS_TAG_DEFAULT, "Can not find node");
return;
}
if (DISCONNECT_ABILITY_FUNC.find(node->second.serviceCenterFunction) == DISCONNECT_ABILITY_FUNC.end()) {
LOG_E(BMS_TAG_DEFAULT, "%{public}d not in DISCONNECT_ABILITY_FUNC", node->second.serviceCenterFunction);
return;
}
freeInstallParamsMap_.erase(installResult.result.transactId);
LOG_I(BMS_TAG_DEFAULT, "erase map size = %{public}zu, transactId = %{public}s",
freeInstallParamsMap_.size(), installResult.result.transactId.c_str());
if (freeInstallParamsMap_.size() == 0) {
if (connectState_ == ServiceCenterConnectState::CONNECTED) {
LOG_I(BMS_TAG_DEFAULT, "DisconnectDelay");
DisconnectDelay();
}
}
}
void BundleConnectAbilityMgr::OutTimeMonitor(std::string transactId)
{
LOG_I(BMS_TAG_DEFAULT, "BundleConnectAbilityMgr::OutTimeMonitor");

View File

@ -47,5 +47,17 @@ int32_t ServiceCenterStatusCallback::OnDelayedHeartbeat(std::string installResul
server->OnDelayedHeartbeat(installResult);
return ERR_OK;
}
int32_t ServiceCenterStatusCallback::OnServiceCenterReceived(std::string installResultStr)
{
LOG_I(BMS_TAG_DEFAULT, "OnDelayedHeartbeat");
auto server = server_.lock();
if (server == nullptr) {
LOG_E(BMS_TAG_DEFAULT, "pointer is nullptr");
return ERR_INVALID_VALUE;
}
server->OnServiceCenterReceived(installResultStr);
return ERR_OK;
}
} // namespace AppExecFwk
} // namespace OHOS

View File

@ -45,6 +45,9 @@ int32_t ServiceCenterStatusCallbackStub::OnRemoteRequest(
if (code == ServiceCenterFunction::CONNECT_DELAYED_HEARTBEAT) {
return OnDelayedHeartbeat(Str16ToStr8(result));
}
if (DISCONNECT_ABILITY_FUNC.find(static_cast<ServiceCenterFunction>(code)) != DISCONNECT_ABILITY_FUNC.end()) {
return OnServiceCenterReceived(Str16ToStr8(result));
}
return OnInstallFinished(Str16ToStr8(result));
}
} // namespace AppExecFwk

View File

@ -41,6 +41,10 @@ public:
{
return 0;
}
virtual int32_t OnServiceCenterReceived(std::string installResultStr) override
{
return 0;
}
};
uint32_t GetU32Data(const char* ptr)
{