提交 6d0c4773 编写于 作者: S shufewhx

decouple ipc_core and ipc_single

Signed-off-by: Nshufewhx <wanghaoxu1@huawei.com>
上级 c10ee8fc
...@@ -196,7 +196,6 @@ public: ...@@ -196,7 +196,6 @@ public:
* @since 9 * @since 9
*/ */
virtual int GetObjectType() const; virtual int GetObjectType() const;
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
/** /**
* @brief Invoker the calling thread. * @brief Invoker the calling thread.
......
...@@ -45,7 +45,6 @@ namespace OHOS { ...@@ -45,7 +45,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
struct SocketThreadLockInfo { struct SocketThreadLockInfo {
std::mutex mutex; std::mutex mutex;
...@@ -90,27 +89,25 @@ public: ...@@ -90,27 +89,25 @@ public:
static bool IsHandleMadeByUser(uint32_t handle); static bool IsHandleMadeByUser(uint32_t handle);
#endif #endif
bool SetMaxWorkThread(int maxThreadNum); bool SetMaxWorkThread(int maxThreadNum);
std::u16string MakeHandleDescriptor(int handle);
sptr<IRemoteObject> GetRegistryObject(); bool OnThreadTerminated(const std::string &threadName);
bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT); bool SpawnThread(int policy = IPCWorkThread::SPAWN_PASSIVE, int proto = IRemoteObject::IF_PROT_DEFAULT);
std::u16string MakeHandleDescriptor(int handle);
sptr<IRemoteObject> FindOrNewObject(int handle); sptr<IRemoteObject> FindOrNewObject(int handle);
bool IsContainsObject(IRemoteObject *object); bool IsContainsObject(IRemoteObject *object);
sptr<IRemoteObject> QueryObject(const std::u16string &descriptor); sptr<IRemoteObject> QueryObject(const std::u16string &descriptor);
bool AttachObject(IRemoteObject *object); bool AttachObject(IRemoteObject *object);
bool DetachObject(IRemoteObject *object); bool DetachObject(IRemoteObject *object);
bool OnThreadTerminated(const std::string &threadName); sptr<IRemoteObject> GetRegistryObject();
bool SetRegistryObject(sptr<IRemoteObject> &object); bool SetRegistryObject(sptr<IRemoteObject> &object);
#ifndef CONFIG_IPC_SINGLE
bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData); bool AttachRawData(uint32_t fd, std::shared_ptr<InvokerRawData> rawData);
bool DetachRawData(uint32_t fd); bool DetachRawData(uint32_t fd);
std::shared_ptr<InvokerRawData> QueryRawData(uint32_t fd); std::shared_ptr<InvokerRawData> QueryRawData(uint32_t fd);
#ifndef CONFIG_IPC_SINGLE
sptr<IRemoteObject> GetSAMgrObject(); sptr<IRemoteObject> GetSAMgrObject();
std::shared_ptr<DBinderSessionObject> ProxyDetachDBinderSession(uint32_t handle, IPCObjectProxy *proxy); std::shared_ptr<DBinderSessionObject> ProxyDetachDBinderSession(uint32_t handle, IPCObjectProxy *proxy);
bool ProxyAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object); bool ProxyAttachDBinderSession(uint32_t handle, std::shared_ptr<DBinderSessionObject> object);
...@@ -127,7 +124,6 @@ public: ...@@ -127,7 +124,6 @@ public:
std::shared_ptr<ThreadMessageInfo> QueryThreadBySeqNumber(uint64_t seqNumber); std::shared_ptr<ThreadMessageInfo> QueryThreadBySeqNumber(uint64_t seqNumber);
bool AddSendThreadInWait(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo, int userWaitTime); bool AddSendThreadInWait(uint64_t seqNumber, std::shared_ptr<ThreadMessageInfo> messageInfo, int userWaitTime);
std::thread::id GetIdleSocketThread();
int GetSocketIdleThreadNum() const; int GetSocketIdleThreadNum() const;
int GetSocketTotalThreadNum() const; int GetSocketTotalThreadNum() const;
int PopSocketIdFromThread(const std::thread::id &threadId); int PopSocketIdFromThread(const std::thread::id &threadId);
...@@ -195,10 +191,8 @@ private: ...@@ -195,10 +191,8 @@ private:
IPCProcessSkeleton(); IPCProcessSkeleton();
static IPCProcessSkeleton *instance_; static IPCProcessSkeleton *instance_;
static std::mutex procMutex_; static std::mutex procMutex_;
std::shared_mutex mutex_;
std::shared_mutex rawDataMutex_; std::shared_mutex rawDataMutex_;
std::map<std::u16string, wptr<IRemoteObject>> objects_;
std::map<IRemoteObject *, bool> isContainStub_;
std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_; std::map<uint32_t, std::shared_ptr<InvokerRawData>> rawData_;
IPCWorkThreadPool *threadPool_ = nullptr; IPCWorkThreadPool *threadPool_ = nullptr;
......
...@@ -16,13 +16,14 @@ ...@@ -16,13 +16,14 @@
#ifndef OHOS_IPC_PROCESS_SKELETON_H #ifndef OHOS_IPC_PROCESS_SKELETON_H
#define OHOS_IPC_PROCESS_SKELETON_H #define OHOS_IPC_PROCESS_SKELETON_H
#include <map>
#include <mutex> #include <mutex>
#include <shared_mutex>
#include <refbase.h> #include <refbase.h>
#include "iremote_object.h" #include "iremote_object.h"
namespace OHOS { namespace OHOS {
class ProcessSkeleton : public virtual RefBase { class ProcessSkeleton : public virtual RefBase {
public: public:
...@@ -32,15 +33,24 @@ public: ...@@ -32,15 +33,24 @@ public:
void SetSamgrFlag(bool flag); void SetSamgrFlag(bool flag);
bool GetSamgrFlag(); bool GetSamgrFlag();
bool IsContainsObject(IRemoteObject *object);
sptr<IRemoteObject> QueryObject(const std::u16string &descriptor);
bool AttachObject(IRemoteObject *object, const std::u16string &descriptor);
bool DetachObject(IRemoteObject *object, const std::u16string &descriptor);
private: private:
DISALLOW_COPY_AND_MOVE(ProcessSkeleton); DISALLOW_COPY_AND_MOVE(ProcessSkeleton);
ProcessSkeleton() = default; ProcessSkeleton() = default;
~ProcessSkeleton() = default; ~ProcessSkeleton();
static sptr<ProcessSkeleton> instance_; static sptr<ProcessSkeleton> instance_;
static std::mutex mutex_; static std::mutex mutex_;
std::shared_mutex objMutex_;
sptr<IRemoteObject> registryObject_ = nullptr; sptr<IRemoteObject> registryObject_ = nullptr;
bool isSamgr_ = false; bool isSamgr_ = false;
std::map<std::u16string, wptr<IRemoteObject>> objects_;
std::map<IRemoteObject *, bool> isContainStub_;
}; };
} // namespace OHOS } // namespace OHOS
#endif // OHOS_IPC_PROCESS_SKELETON_H #endif // OHOS_IPC_PROCESS_SKELETON_H
...@@ -49,7 +49,6 @@ namespace OHOS { ...@@ -49,7 +49,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE; using namespace IPC_SINGLE;
#endif #endif
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectProxy" }; static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectProxy" };
IPCObjectProxy::IPCObjectProxy(int handle, std::u16string descriptor, int proto) IPCObjectProxy::IPCObjectProxy(int handle, std::u16string descriptor, int proto)
......
...@@ -49,7 +49,6 @@ namespace OHOS { ...@@ -49,7 +49,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE; using namespace IPC_SINGLE;
#endif #endif
using namespace OHOS::HiviewDFX; using namespace OHOS::HiviewDFX;
static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectStub" }; static constexpr HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "IPCObjectStub" };
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
......
...@@ -15,6 +15,7 @@ ...@@ -15,6 +15,7 @@
#include "ipc_process_skeleton.h" #include "ipc_process_skeleton.h"
#include <functional>
#include <securec.h> #include <securec.h>
#include <unistd.h> #include <unistd.h>
#include <random> #include <random>
...@@ -23,6 +24,7 @@ ...@@ -23,6 +24,7 @@
#include "ipc_debug.h" #include "ipc_debug.h"
#include "ipc_types.h" #include "ipc_types.h"
#include "ipc_object_proxy.h"
#include "ipc_thread_skeleton.h" #include "ipc_thread_skeleton.h"
#include "process_skeleton.h" #include "process_skeleton.h"
#include "sys_binder.h" #include "sys_binder.h"
...@@ -37,7 +39,6 @@ namespace OHOS { ...@@ -37,7 +39,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
using namespace Communication; using namespace Communication;
#endif #endif
...@@ -93,8 +94,6 @@ IPCProcessSkeleton::~IPCProcessSkeleton() ...@@ -93,8 +94,6 @@ IPCProcessSkeleton::~IPCProcessSkeleton()
delete threadPool_; delete threadPool_;
threadPool_ = nullptr; threadPool_ = nullptr;
objects_.clear();
isContainStub_.clear();
rawData_.clear(); rawData_.clear();
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
threadLockInfo_.clear(); threadLockInfo_.clear();
...@@ -138,10 +137,11 @@ std::u16string IPCProcessSkeleton::MakeHandleDescriptor(int handle) ...@@ -138,10 +137,11 @@ std::u16string IPCProcessSkeleton::MakeHandleDescriptor(int handle)
sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle) sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
{ {
sptr<IRemoteObject> result = nullptr; sptr<IRemoteObject> result = nullptr;
std::u16string descriptor = MakeHandleDescriptor(handle); std::u16string descriptor = MakeHandleDescriptor(handle);
if (descriptor.length() == 0) { if (descriptor.length() == 0) {
ZLOGE(LOG_LABEL, "make handle descriptor failed"); ZLOGE(LOG_LABEL, "make handle descriptor failed");
return nullptr; return result;
} }
{ {
result = QueryObject(descriptor); result = QueryObject(descriptor);
...@@ -161,13 +161,14 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle) ...@@ -161,13 +161,14 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
} }
// OnFirstStrongRef will be called. // OnFirstStrongRef will be called.
result = new (std::nothrow) IPCObjectProxy(handle, descriptor); result = new (std::nothrow) IPCObjectProxy(handle, descriptor);
if (result == nullptr) {
ZLOGE(LOG_LABEL, "new IPCObjectProxy failed!");
return result;
}
AttachObject(result.GetRefPtr()); AttachObject(result.GetRefPtr());
} }
} }
sptr<IPCObjectProxy> proxy = reinterpret_cast<IPCObjectProxy *>(result.GetRefPtr()); sptr<IPCObjectProxy> proxy = reinterpret_cast<IPCObjectProxy *>(result.GetRefPtr());
// When a new proxy is initializing, other thread will find an existed proxy and need to wait,
// this makes sure proxy has been initialized when ReadRemoteObject return.
proxy->WaitForInit(); proxy->WaitForInit();
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
if (proxy->GetProto() == IRemoteObject::IF_PROT_ERROR) { if (proxy->GetProto() == IRemoteObject::IF_PROT_ERROR) {
...@@ -178,29 +179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle) ...@@ -178,29 +179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::FindOrNewObject(int handle)
return result; return result;
} }
bool IPCProcessSkeleton::SetMaxWorkThread(int maxThreadNum)
{
if (maxThreadNum <= 0) {
ZLOGE(LOG_LABEL, "Set Invalid thread Number %d", maxThreadNum);
return false;
}
if (threadPool_ == nullptr) {
threadPool_ = new (std::nothrow) IPCWorkThreadPool(maxThreadNum);
if (threadPool_ == nullptr) {
ZLOGE(LOG_LABEL, "create IPCWorkThreadPool object failed");
return false;
}
}
threadPool_->UpdateMaxThreadNum(maxThreadNum);
IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
if (invoker != nullptr) {
return invoker->SetMaxWorkThread(maxThreadNum);
}
return false;
}
bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object) bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
{ {
if (object == nullptr) { if (object == nullptr) {
...@@ -226,6 +204,29 @@ bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object) ...@@ -226,6 +204,29 @@ bool IPCProcessSkeleton::SetRegistryObject(sptr<IRemoteObject> &object)
return ret; return ret;
} }
bool IPCProcessSkeleton::SetMaxWorkThread(int maxThreadNum)
{
if (maxThreadNum <= 0) {
ZLOGE(LOG_LABEL, "Set Invalid thread Number %d", maxThreadNum);
return false;
}
if (threadPool_ == nullptr) {
threadPool_ = new (std::nothrow) IPCWorkThreadPool(maxThreadNum);
if (threadPool_ == nullptr) {
ZLOGE(LOG_LABEL, "create IPCWorkThreadPool object failed");
return false;
}
}
threadPool_->UpdateMaxThreadNum(maxThreadNum);
IRemoteInvoker *invoker = IPCThreadSkeleton::GetRemoteInvoker(IRemoteObject::IF_PROT_DEFAULT);
if (invoker != nullptr) {
return invoker->SetMaxWorkThread(maxThreadNum);
}
return false;
}
bool IPCProcessSkeleton::SpawnThread(int policy, int proto) bool IPCProcessSkeleton::SpawnThread(int policy, int proto)
{ {
if (threadPool_ != nullptr) { if (threadPool_ != nullptr) {
...@@ -245,72 +246,69 @@ bool IPCProcessSkeleton::OnThreadTerminated(const std::string &threadName) ...@@ -245,72 +246,69 @@ bool IPCProcessSkeleton::OnThreadTerminated(const std::string &threadName)
return true; return true;
} }
bool IPCProcessSkeleton::IsContainsObject(IRemoteObject *object) bool IPCProcessSkeleton::IsContainsObject(IRemoteObject *object)
{ {
// check whether it is a valid IPCObjectStub object. if (object == nullptr) {
std::shared_lock<std::shared_mutex> lockGuard(mutex_); ZLOGE(LOG_LABEL, "object is null");
auto it = isContainStub_.find(object); return false;
if (it != isContainStub_.end()) {
return it->second;
} }
auto current = ProcessSkeleton::GetInstance();
return false; if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return false;
}
return current->IsContainsObject(object);
} }
bool IPCProcessSkeleton::DetachObject(IRemoteObject *object) bool IPCProcessSkeleton::DetachObject(IRemoteObject *object)
{ {
std::unique_lock<std::shared_mutex> lockGuard(mutex_); if (object == nullptr) {
(void)isContainStub_.erase(object); ZLOGE(LOG_LABEL, "object is null");
return false;
}
std::u16string descriptor = object->GetObjectDescriptor(); std::u16string descriptor = object->GetObjectDescriptor();
if (descriptor.empty()) { if (descriptor.empty()) {
return false; return false;
} }
// This handle may have already been replaced with a new IPCObjectProxy, auto current = ProcessSkeleton::GetInstance();
// if someone failed the AttemptIncStrong. if (current == nullptr) {
auto iterator = objects_.find(descriptor); ZLOGE(LOG_LABEL, "get process skeleton failed");
if (iterator->second == object) { return false;
objects_.erase(iterator);
return true;
} }
return false; return current->DetachObject(object, descriptor);
} }
bool IPCProcessSkeleton::AttachObject(IRemoteObject *object) bool IPCProcessSkeleton::AttachObject(IRemoteObject *object)
{ {
std::unique_lock<std::shared_mutex> lockGuard(mutex_); if (object == nullptr) {
(void)isContainStub_.insert(std::pair<IRemoteObject *, bool>(object, true)); ZLOGE(LOG_LABEL, "object is null");
return false;
}
std::u16string descriptor = object->GetObjectDescriptor(); std::u16string descriptor = object->GetObjectDescriptor();
if (descriptor.empty()) { if (descriptor.empty()) {
return false; return false;
} }
// If attemptIncStrong failed, old proxy might still exist, replace it with the new proxy. auto current = ProcessSkeleton::GetInstance();
wptr<IRemoteObject> wp = object; if (current == nullptr) {
auto result = objects_.insert_or_assign(descriptor, wp); ZLOGE(LOG_LABEL, "get process skeleton failed");
return result.second; return false;
}
return current->AttachObject(object, descriptor);
} }
sptr<IRemoteObject> IPCProcessSkeleton::QueryObject(const std::u16string &descriptor) sptr<IRemoteObject> IPCProcessSkeleton::QueryObject(const std::u16string &descriptor)
{ {
sptr<IRemoteObject> result = nullptr; if (descriptor.length() == 0) {
if (descriptor.empty()) { ZLOGE(LOG_LABEL, "enter descriptor is empty");
return result; return nullptr;
}
std::shared_lock<std::shared_mutex> lockGuard(mutex_);
IRemoteObject *remoteObject = nullptr;
auto it = objects_.find(descriptor);
if (it != objects_.end()) {
// Life-time of IPCObjectProxy is extended to WEAK
// now it's weak reference counted, so it's safe to get raw pointer
remoteObject = it->second.GetRefPtr();
} }
if (remoteObject == nullptr || !remoteObject->AttemptIncStrong(this)) { auto current = ProcessSkeleton::GetInstance();
return result; if (current == nullptr) {
ZLOGE(LOG_LABEL, "get process skeleton failed");
return nullptr;
} }
result = remoteObject; return current->QueryObject(descriptor);
return result;
} }
#ifndef CONFIG_IPC_SINGLE #ifndef CONFIG_IPC_SINGLE
...@@ -595,12 +593,12 @@ int IPCProcessSkeleton::GetSocketIdleThreadNum() const ...@@ -595,12 +593,12 @@ int IPCProcessSkeleton::GetSocketIdleThreadNum() const
return 0; return 0;
} }
int IPCProcessSkeleton::GetSocketTotalThreadNum() const int IPCProcessSkeleton::GetSocketTotalThreadNum() const
{ {
if (threadPool_ != nullptr) { if (threadPool_ != nullptr) {
return threadPool_->GetSocketTotalThreadNum(); return threadPool_->GetSocketTotalThreadNum();
} }
return 0; return 0;
} }
...@@ -1181,7 +1179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::QueryDBinderCallbackProxy(sptr<IRemoteOb ...@@ -1181,7 +1179,6 @@ sptr<IRemoteObject> IPCProcessSkeleton::QueryDBinderCallbackProxy(sptr<IRemoteOb
return nullptr; return nullptr;
} }
#endif #endif
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
} // namespace IPC_SINGLE } // namespace IPC_SINGLE
......
...@@ -33,7 +33,6 @@ namespace OHOS { ...@@ -33,7 +33,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE; using namespace IPC_SINGLE;
#endif #endif
void IPCSkeleton::JoinWorkThread() void IPCSkeleton::JoinWorkThread()
{ {
IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent(); IPCThreadSkeleton *current = IPCThreadSkeleton::GetCurrent();
......
...@@ -33,7 +33,6 @@ namespace OHOS { ...@@ -33,7 +33,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
using namespace OHOS::HiviewDFX; using namespace OHOS::HiviewDFX;
pthread_key_t IPCThreadSkeleton::TLSKey_ = 0; pthread_key_t IPCThreadSkeleton::TLSKey_ = 0;
pthread_once_t IPCThreadSkeleton::TLSKeyOnce_ = PTHREAD_ONCE_INIT; pthread_once_t IPCThreadSkeleton::TLSKeyOnce_ = PTHREAD_ONCE_INIT;
...@@ -82,8 +81,8 @@ IPCThreadSkeleton::IPCThreadSkeleton() ...@@ -82,8 +81,8 @@ IPCThreadSkeleton::IPCThreadSkeleton()
IPCThreadSkeleton::~IPCThreadSkeleton() IPCThreadSkeleton::~IPCThreadSkeleton()
{ {
ZLOGE(LABEL, "IPCThreadSkeleton delete");
std::lock_guard<std::recursive_mutex> lockGuard(mutex_); std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
ZLOGE(LABEL, "IPCThreadSkeleton delete");
for (auto it = invokers_.begin(); it != invokers_.end();) { for (auto it = invokers_.begin(); it != invokers_.end();) {
delete it->second; delete it->second;
it = invokers_.erase(it); it = invokers_.erase(it);
...@@ -97,7 +96,6 @@ IRemoteInvoker *IPCThreadSkeleton::GetRemoteInvoker(int proto) ...@@ -97,7 +96,6 @@ IRemoteInvoker *IPCThreadSkeleton::GetRemoteInvoker(int proto)
if (current == nullptr) { if (current == nullptr) {
return nullptr; return nullptr;
} }
std::lock_guard<std::recursive_mutex> lockGuard(mutex_); std::lock_guard<std::recursive_mutex> lockGuard(mutex_);
auto it = current->invokers_.find(proto); auto it = current->invokers_.find(proto);
if (it != current->invokers_.end()) { if (it != current->invokers_.end()) {
......
...@@ -25,7 +25,6 @@ namespace OHOS { ...@@ -25,7 +25,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
using namespace IPC_SINGLE; using namespace IPC_SINGLE;
#endif #endif
bool IRemoteObject::CheckObjectLegality() const bool IRemoteObject::CheckObjectLegality() const
{ {
return false; return false;
......
...@@ -17,6 +17,7 @@ ...@@ -17,6 +17,7 @@
#include "log_tags.h" #include "log_tags.h"
#include "ipc_debug.h" #include "ipc_debug.h"
#include "string_ex.h"
namespace OHOS { namespace OHOS {
static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "ProcessSkeleton" }; static constexpr OHOS::HiviewDFX::HiLogLabel LOG_LABEL = { LOG_CORE, LOG_ID_IPC, "ProcessSkeleton" };
...@@ -39,6 +40,13 @@ sptr<ProcessSkeleton> ProcessSkeleton::GetInstance() ...@@ -39,6 +40,13 @@ sptr<ProcessSkeleton> ProcessSkeleton::GetInstance()
return instance_; return instance_;
} }
ProcessSkeleton::~ProcessSkeleton()
{
std::lock_guard<std::shared_mutex> lockGuard(objMutex_);
objects_.clear();
isContainStub_.clear();
}
sptr<IRemoteObject> ProcessSkeleton::GetRegistryObject() sptr<IRemoteObject> ProcessSkeleton::GetRegistryObject()
{ {
std::lock_guard<std::mutex> lockGuard(mutex_); std::lock_guard<std::mutex> lockGuard(mutex_);
...@@ -60,4 +68,64 @@ bool ProcessSkeleton::GetSamgrFlag() ...@@ -60,4 +68,64 @@ bool ProcessSkeleton::GetSamgrFlag()
{ {
return isSamgr_; return isSamgr_;
} }
bool ProcessSkeleton::IsContainsObject(IRemoteObject *object)
{
// check whether it is a valid IPCObjectStub object.
std::shared_lock<std::shared_mutex> lockGuard(objMutex_);
auto it = isContainStub_.find(object);
if (it != isContainStub_.end()) {
return it->second;
}
return false;
}
bool ProcessSkeleton::DetachObject(IRemoteObject *object, const std::u16string &descriptor)
{
std::unique_lock<std::shared_mutex> lockGuard(objMutex_);
(void)isContainStub_.erase(object);
// This handle may have already been replaced with a new IPCObjectProxy,
// if someone failed the AttemptIncStrong.
auto iterator = objects_.find(descriptor);
if (iterator != objects_.end()) {
objects_.erase(iterator);
return true;
}
return false;
}
bool ProcessSkeleton::AttachObject(IRemoteObject *object, const std::u16string &descriptor)
{
std::unique_lock<std::shared_mutex> lockGuard(objMutex_);
(void)isContainStub_.insert(std::pair<IRemoteObject *, bool>(object, true));
// If attemptIncStrong failed, old proxy might still exist, replace it with the new proxy.
wptr<IRemoteObject> wp = object;
auto result = objects_.insert_or_assign(descriptor, wp);
return result.second;
}
sptr<IRemoteObject> ProcessSkeleton::QueryObject(const std::u16string &descriptor)
{
sptr<IRemoteObject> result = nullptr;
if (descriptor.empty()) {
return result;
}
std::shared_lock<std::shared_mutex> lockGuard(objMutex_);
IRemoteObject *remoteObject = nullptr;
auto it = objects_.find(descriptor);
if (it != objects_.end()) {
// Life-time of IPCObjectProxy is extended to WEAK
// now it's weak reference counted, so it's safe to get raw pointer
remoteObject = it->second.GetRefPtr();
}
if (remoteObject == nullptr || !remoteObject->AttemptIncStrong(this)) {
return result;
}
result = remoteObject;
return result;
}
} // namespace OHOS } // namespace OHOS
\ No newline at end of file
...@@ -23,7 +23,6 @@ namespace OHOS { ...@@ -23,7 +23,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
class BinderConnector { class BinderConnector {
public: public:
static BinderConnector *GetInstance(); static BinderConnector *GetInstance();
......
...@@ -41,7 +41,6 @@ private: ...@@ -41,7 +41,6 @@ private:
std::mutex factoryMutex_; std::mutex factoryMutex_;
std::unordered_map<int, InvokerCreator> creators_; std::unordered_map<int, InvokerCreator> creators_;
}; };
template <typename T> class InvokerDelegator { template <typename T> class InvokerDelegator {
public: public:
InvokerDelegator(int prot); InvokerDelegator(int prot);
......
...@@ -27,7 +27,6 @@ namespace OHOS { ...@@ -27,7 +27,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
class IRemoteInvoker { class IRemoteInvoker {
public: public:
enum { enum {
......
...@@ -37,7 +37,6 @@ namespace OHOS { ...@@ -37,7 +37,6 @@ namespace OHOS {
#ifdef CONFIG_IPC_SINGLE #ifdef CONFIG_IPC_SINGLE
namespace IPC_SINGLE { namespace IPC_SINGLE {
#endif #endif
static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "BinderConnector" }; static constexpr HiviewDFX::HiLogLabel LABEL = { LOG_CORE, LOG_ID_IPC, "BinderConnector" };
std::mutex BinderConnector::skeletonMutex; std::mutex BinderConnector::skeletonMutex;
constexpr int SZ_1_M = 1048576; constexpr int SZ_1_M = 1048576;
......
...@@ -45,6 +45,7 @@ ohos_unittest("IPCNativeUnitTest") { ...@@ -45,6 +45,7 @@ ohos_unittest("IPCNativeUnitTest") {
"ipc_thread_skeleton_unittest.cpp", "ipc_thread_skeleton_unittest.cpp",
"iremote_object_unitest.cpp", "iremote_object_unitest.cpp",
"message_parcel_unittest.cpp", "message_parcel_unittest.cpp",
"process_skeleton_unittest.cpp",
] ]
configs = [ configs = [
...@@ -53,6 +54,7 @@ ohos_unittest("IPCNativeUnitTest") { ...@@ -53,6 +54,7 @@ ohos_unittest("IPCNativeUnitTest") {
] ]
deps = [ deps = [
"$IPC_CORE_ROOT/src/core:ipc_common",
"$IPC_TEST_ROOT/auxiliary/native:TestAssistance", "$IPC_TEST_ROOT/auxiliary/native:TestAssistance",
"//third_party/googletest:gmock_main", "//third_party/googletest:gmock_main",
"//third_party/googletest:gtest_main", "//third_party/googletest:gtest_main",
......
...@@ -18,6 +18,7 @@ ...@@ -18,6 +18,7 @@
#define private public #define private public
#define protected public #define protected public
#include "binder_invoker.h"
#include "dbinder_callback_stub.h" #include "dbinder_callback_stub.h"
#include "ipc_types.h" #include "ipc_types.h"
#include "ipc_thread_skeleton.h" #include "ipc_thread_skeleton.h"
......
...@@ -1693,22 +1693,6 @@ HWTEST_F(IPCProcessSkeletonUnitTest, QueryDBinderCallbackProxyTest002, TestSize. ...@@ -1693,22 +1693,6 @@ HWTEST_F(IPCProcessSkeletonUnitTest, QueryDBinderCallbackProxyTest002, TestSize.
EXPECT_EQ(ret, nullptr); EXPECT_EQ(ret, nullptr);
} }
/**
* @tc.name: IsContainsObjectTest002
* @tc.desc: Verify the IsContainsObject function
* @tc.type: FUNC
*/
HWTEST_F(IPCProcessSkeletonUnitTest, IsContainsObjectTest002, TestSize.Level1)
{
IPCProcessSkeleton *skeleton = IPCProcessSkeleton::GetCurrent();
ASSERT_TRUE(skeleton != nullptr);
sptr<IRemoteObject> object = new IPCObjectStub(u"testObject");
skeleton->isContainStub_.clear();
bool ret = skeleton->IsContainsObject(object.GetRefPtr());
EXPECT_EQ(ret, false);
}
/** /**
* @tc.name: ProxyQueryDBinderSessionTest002 * @tc.name: ProxyQueryDBinderSessionTest002
* @tc.desc: Verify the ProxyQueryDBinderSession function * @tc.desc: Verify the ProxyQueryDBinderSession function
......
/*
* 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 <gtest/gtest.h>
#include <gmock/gmock.h>
#define private public
#include "process_skeleton.h"
#include "ipc_types.h"
#include "iremote_object.h"
#include "ipc_object_stub.h"
#undef private
using namespace testing::ext;
using namespace OHOS;
class ProcessSkeletonUnitTest : public testing::Test {
public:
static void SetUpTestCase(void);
static void TearDownTestCase(void);
void SetUp();
void TearDown();
};
void ProcessSkeletonUnitTest::SetUpTestCase()
{
}
void ProcessSkeletonUnitTest::TearDownTestCase()
{
}
void ProcessSkeletonUnitTest::SetUp() {}
void ProcessSkeletonUnitTest::TearDown() {}
/**
* @tc.name: IsContainsObjectTest001
* @tc.desc: Verify the IsContainsObject function
* @tc.type: FUNC
*/
HWTEST_F(ProcessSkeletonUnitTest, IsContainsObjectTest001, TestSize.Level1)
{
ProcessSkeleton *skeleton = ProcessSkeleton::GetInstance();
ASSERT_TRUE(skeleton != nullptr);
sptr<IRemoteObject> object = new IPCObjectStub(u"testObject");
skeleton->isContainStub_.clear();
bool ret = skeleton->IsContainsObject(object.GetRefPtr());
EXPECT_EQ(ret, false);
}
Markdown is supported
0% .
You are about to add 0 people to the discussion. Proceed with caution.
先完成此消息的编辑!
想要评论请 注册