Add libipc_common

Signed-off-by: crazy_hu <huxiusong1@huawei.com>
This commit is contained in:
crazy_hu 2022-11-27 17:14:01 +08:00
parent 6342c0fe07
commit 19b30c06a9
9 changed files with 163 additions and 9 deletions

View File

@ -45,6 +45,7 @@ group("ipc_components") {
"//foundation/communication/ipc/interfaces/innerkits/ipc_core:ipc_core",
"//foundation/communication/ipc/interfaces/innerkits/ipc_single:ipc_single",
"//foundation/communication/ipc/interfaces/innerkits/libdbinder:libdbinder",
"//foundation/communication/ipc/ipc/native/src/core:ipc_common",
]
} else {
deps = [ "//foundation/communication/ipc/interfaces/innerkits/c:rpc" ]

View File

@ -70,6 +70,8 @@ ohos_shared_library("ipc_core") {
all_dependent_configs = [ ":ipc_all_deps_config" ]
deps = [ "$IPC_CORE_ROOT/src/core:ipc_common" ]
external_deps = [
"access_token:libaccesstoken_sdk",
"c_utils:utils",

View File

@ -57,6 +57,8 @@ ohos_shared_library("ipc_single") {
]
public_configs = [ "$SUBSYSTEM_DIR:ipc_util_config" ]
deps = [ "$IPC_CORE_ROOT/src/core:ipc_common" ]
external_deps = [
"c_utils:utils",
"hitrace_native:libhitracechain",

View File

@ -0,0 +1,43 @@
# 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.
import("//build/ohos.gni")
SUBSYSTEM_DIR = "//foundation/communication/ipc"
config("libipc_common_private_config") {
cflags_cc = [ "-O2" ]
}
ohos_shared_library("ipc_common") {
include_dirs = [
"$SUBSYSTEM_DIR/utils/include",
"include",
]
sources = [ "source/process_skeleton.cpp" ]
configs = [
"$SUBSYSTEM_DIR:ipc_util_config",
":libipc_common_private_config",
]
external_deps = [
"c_utils:utils",
"hiviewdfx_hilog_native:libhilog",
]
install_images = [ system_base_dir ]
relative_install_dir = "chipset-pub-sdk"
subsystem_name = "communication"
part_name = "ipc"
}

View File

@ -203,7 +203,6 @@ private:
std::map<IRemoteObject *, bool> isContainStub_;
std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
IPCWorkThreadPool *threadPool_ = nullptr;
sptr<IRemoteObject> registryObject_ = nullptr;
#ifndef CONFIG_IPC_SINGLE
std::mutex databusProcMutex_;

View File

@ -0,0 +1,43 @@
/*
* 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 OHOS_IPC_PROCESS_SKELETON_H
#define OHOS_IPC_PROCESS_SKELETON_H
#include <mutex>
#include <refbase.h>
#include "iremote_object.h"
namespace OHOS {
class ProcessSkeleton : public virtual RefBase {
public:
static sptr<ProcessSkeleton> GetInstance();
sptr<IRemoteObject> GetRegistryObject();
void SetRegistryObject(sptr<IRemoteObject> &object);
private:
DISALLOW_COPY_AND_MOVE(ProcessSkeleton);
ProcessSkeleton() = default;
~ProcessSkeleton() = default;
static sptr<ProcessSkeleton> instance_;
static std::mutex mutex_;
sptr<IRemoteObject> registryObject_ = nullptr;
};
} // namespace OHOS
#endif // OHOS_IPC_PROCESS_SKELETON_H

View File

@ -24,6 +24,7 @@
#include "ipc_types.h"
#include "ipc_thread_skeleton.h"
#include "process_skeleton.h"
#include "sys_binder.h"
#include "log_tags.h"
@ -113,11 +114,19 @@ IPCProcessSkeleton::~IPCProcessSkeleton()
sptr<IRemoteObject> IPCProcessSkeleton::GetRegistryObject()
{
if (registryObject_ == nullptr) {
registryObject_ = FindOrNewObject(REGISTRY_HANDLE);
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return nullptr;
}
return registryObject_;
sptr<IRemoteObject> object = current->GetRegistryObject();
if (object == nullptr) {
object = FindOrNewObject(REGISTRY_HANDLE);
if (object != nullptr) {
current->SetRegistryObject(object);
}
}
return object;
}
std::u16string IPCProcessSkeleton::MakeHandleDescriptor(int handle)
@ -201,16 +210,19 @@ bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
ZLOGE(LOG_LABEL, "object is null");
return false;
}
auto current = ProcessSkeleton::GetInstance();
if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return false;
}
IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
if (invoker == nullptr) {
ZLOGE(LOG_LABEL, "fail to get invoker");
return false;
}
bool ret = invoker->SetRegistryObject(object);
if (ret) {
registryObject_ = object;
current->SetRegistryObject(object);
}
ZLOGI(LOG_LABEL, "%{public}s set registry result is %{public}d", __func__, ret);
return ret;

View File

@ -0,0 +1,53 @@
/*
* 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 "process_skeleton.h"
#include "log_tags.h"
#include "ipc_debug.h"
namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "ProcessSkeleton" };
sptr<ProcessSkeleton> ProcessSkeleton::instance_ = nullptr;
std::mutex ProcessSkeleton::mutex_;
sptr<ProcessSkeleton> ProcessSkeleton::GetInstance()
{
if (instance_ == nullptr) {
std::lock_guard<std::mutex> lockGuard(mutex_);
if (instance_ == nullptr) {
instance_ = new (std::nothrow) ProcessSkeleton();
if (instance_ == nullptr) {
ZLOGE(LOG_LABEL, "create ProcessSkeleton object failed");
return nullptr;
}
}
}
return instance_;
}
sptr<IRemoteObject> ProcessSkeleton::GetRegistryObject()
{
std::lock_guard<std::mutex> lockGuard(mutex_);
return registryObject_;
}
void ProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
{
std::lock_guard<std::mutex> lockGuard(mutex_);
registryObject_ = object;
}
} // namespace OHOS

View File

@ -487,7 +487,6 @@ void BinderInvoker::OnTransaction(const uint8_t *buffer)
auto targetObject = IPCProcessSkeleton::GetCurrent()->GetRegistryObject();
if (targetObject == nullptr) {
ZLOGE(LABEL, "Invalid samgr stub object");
abort();
} else {
error = targetObject->SendRequest(tr->code, *data, reply, option);
}