improve ipc code

Signed-off-by: huyiqi <huyiqi3@huawei.com>
This commit is contained in:
huyiqi 2024-07-02 09:53:16 +08:00
parent 6e26548104
commit 3bd0ff56c4
4 changed files with 6 additions and 104 deletions

View File

@ -79,7 +79,6 @@ ohos_static_library("libcert_manager_ipc_client_static") {
sources = [
"./cm_ipc/src/cm_ipc_client.c",
"./cm_ipc/src/cm_ipc_client_serialization.c",
"./cm_ipc/src/cm_load_sa.cpp",
"./cm_ipc/src/cm_request.cpp",
]
deps = [ ":libcert_manager_log_mem_static" ]

View File

@ -1,36 +0,0 @@
/*
* Copyright (c) 2022 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 CM_LOAD_SA_H
#define CM_LOAD_SA_H
#include <future>
#include <string>
#include "iremote_object.h"
#include "system_ability_load_callback_stub.h"
class OnDemandLoadCertManagerCallback : public OHOS::SystemAbilityLoadCallbackStub {
public:
OnDemandLoadCertManagerCallback(std::string servers);
void OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const OHOS::sptr<IRemoteObject>& remoteObject) override;
void OnLoadSystemAbilityFail(int32_t systemAbilityId) override;
OHOS::sptr<IRemoteObject> Promise(void);
private:
std::string servers;
std::promise<OHOS::sptr<IRemoteObject>> promise_;
};
#endif /* CM_LOAD_SA_H */

View File

@ -1,23 +0,0 @@
/*
* Copyright (c) 2022 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 "cm_load_sa.h"
#include "cm_log.h"
using namespace std;
OnDemandLoadCertManagerCallback::OnDemandLoadCertManagerCallback(string servers) : servers(servers)
{
CM_LOG_I("Servers %s on demand Callback constructor success", servers.c_str());
}

View File

@ -21,7 +21,6 @@
#include "securec.h"
#include "cm_load_sa.h"
#include "cm_log.h"
#include "iservice_registry.h"
@ -31,7 +30,7 @@ using namespace OHOS;
namespace {
constexpr int SA_ID_KEYSTORE_SERVICE = 3512;
constexpr uint32_t MAX_SA_BOOT_DELAY_TIME = 30;
constexpr int32_t LOAD_ABILITY_TIME_OUT_SECONDS = 3;
const std::u16string SA_KEYSTORE_SERVICE_DESCRIPTOR = u"ohos.security.cm.service";
}
@ -48,20 +47,7 @@ static sptr<IRemoteObject> CmLoadSystemAbility(void)
return object;
}
string servers = "CertManager";
sptr<OnDemandLoadCertManagerCallback> loadCallBack = new (std::nothrow)OnDemandLoadCertManagerCallback(servers);
if (loadCallBack == nullptr) {
CM_LOG_E("new OnDemandLoadCertManagerCallback failed");
return {};
}
int32_t ret = saManager->LoadSystemAbility(SA_ID_KEYSTORE_SERVICE, loadCallBack);
if (ret != ERR_OK) {
CM_LOG_E("systemAbilityId:%d load failed,result code:%d", SA_ID_KEYSTORE_SERVICE, ret);
return {};
}
return loadCallBack->Promise();
return saManager->LoadSystemAbility(SA_ID_KEYSTORE_SERVICE, LOAD_ABILITY_TIME_OUT_SECONDS);
}
static int32_t CmReadRequestReply(MessageParcel &reply, struct CmBlob *outBlob)
@ -101,17 +87,12 @@ static int32_t CmReadRequestReply(MessageParcel &reply, struct CmBlob *outBlob)
int32_t SendRequest(enum CertManagerInterfaceCode type, const struct CmBlob *inBlob,
struct CmBlob *outBlob)
{
uint32_t i = 0;
sptr<IRemoteObject> cmProxy = CmLoadSystemAbility();
while ((cmProxy == nullptr) && i < MAX_SA_BOOT_DELAY_TIME) {
CM_LOG_E("cmProxy is nullptr, i = %u", i);
std::this_thread::sleep_for(std::chrono::milliseconds(100)); /* 100 is time */
i++;
}
cmProxy = CmLoadSystemAbility();
if (cmProxy == nullptr) {
CM_LOG_E("Certtificate manager Proxy is null.");
cmProxy = CmLoadSystemAbility();
}
if (cmProxy == nullptr) {
CM_LOG_E("Certificate manager Proxy is null.");
return CMR_ERROR_NULL_POINTER;
}
@ -135,22 +116,3 @@ int32_t SendRequest(enum CertManagerInterfaceCode type, const struct CmBlob *inB
}
return CmReadRequestReply(reply, outBlob);
}
void OnDemandLoadCertManagerCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId,
const sptr<IRemoteObject> &remoteObject)
{
CM_LOG_D("OnLoadSystemAbility Success systemAbilityId: %d, IRemoteObject result:%s",
systemAbilityId, ((remoteObject != nullptr) ? "succeed" : "failed"));
promise_.set_value(remoteObject);
}
void OnDemandLoadCertManagerCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
{
CM_LOG_E("OnLoadSystemAbility Fail systemAbilityId: %d", systemAbilityId);
promise_.set_value(nullptr);
}
sptr<IRemoteObject> OnDemandLoadCertManagerCallback::Promise(void)
{
return promise_.get_future().get();
}