From b147cb0e49209666ec644236eab7a8b29d205a2f Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Sun, 16 Jan 2022 02:04:00 +0800 Subject: [PATCH 1/2] add storage part of relational database Signed-off-by: wuchunbo --- .../include/ikvstore_data_service.h | 12 +- .../include/rdb/irdb_service.h | 42 +++++ .../include/rdb/irdb_store.h | 37 +++++ .../include/rdb/rdb_client_death_recipient.h | 45 +++++ .../include/rdb/rdb_parcel.h | 46 ++++++ .../include/rdb/rdb_service_proxy.h | 35 ++++ .../include/rdb/rdb_service_stub.h | 41 +++++ .../include/rdb/rdb_store_proxy.h | 33 ++++ .../include/rdb/rdb_store_stub.h | 38 +++++ .../src/ikvstore_data_service.cpp | 26 +++ .../src/rdb/rdb_client_death_recipient.cpp | 48 ++++++ .../distributeddatafwk/src/rdb/rdb_parcel.cpp | 90 ++++++++++ .../src/rdb/rdb_service_proxy.cpp | 74 +++++++++ .../src/rdb/rdb_service_stub.cpp | 63 +++++++ .../src/rdb/rdb_store_proxy.cpp | 50 ++++++ .../src/rdb/rdb_store_stub.cpp | 53 ++++++ interfaces/innerkits/distributeddata/BUILD.gn | 13 ++ services/distributeddataservice/app/BUILD.gn | 15 ++ .../app/distributed_data.cfg | 2 +- .../app/distributed_data.rc | 2 +- .../app/src/kvstore_data_service.cpp | 22 ++- .../app/src/kvstore_data_service.h | 15 +- .../app/src/rdb/rdb_device_store.cpp | 112 +++++++++++++ .../app/src/rdb/rdb_device_store.h | 58 +++++++ .../app/src/rdb/rdb_service.cpp | 156 ++++++++++++++++++ .../app/src/rdb/rdb_service.h | 62 +++++++ .../app/src/rdb/rdb_store.cpp | 78 +++++++++ .../app/src/rdb/rdb_store.h | 59 +++++++ .../app/src/rdb/rdb_store_factory.cpp | 56 +++++++ .../app/src/rdb/rdb_store_factory.h | 37 +++++ .../distributeddataservice/app/test/BUILD.gn | 22 +++ .../test/unittest/kvstore_data_service.cpp | 6 + 32 files changed, 1433 insertions(+), 15 deletions(-) create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp create mode 100644 frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp create mode 100644 services/distributeddataservice/app/src/rdb/rdb_device_store.cpp create mode 100644 services/distributeddataservice/app/src/rdb/rdb_device_store.h create mode 100644 services/distributeddataservice/app/src/rdb/rdb_service.cpp create mode 100644 services/distributeddataservice/app/src/rdb/rdb_service.h create mode 100644 services/distributeddataservice/app/src/rdb/rdb_store.cpp create mode 100644 services/distributeddataservice/app/src/rdb/rdb_store.h create mode 100644 services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp create mode 100644 services/distributeddataservice/app/src/rdb/rdb_store_factory.h diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h index 3755e1f0f..fcf26133a 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/ikvstore_data_service.h @@ -27,8 +27,7 @@ #include "types.h" #include "idevice_status_change_listener.h" -namespace OHOS { -namespace DistributedKv { +namespace OHOS::DistributedKv { /* * IPC-friendly Options struct without std::string schema field. * Passing a struct with an std::string field is a potential security exploit. @@ -47,6 +46,7 @@ struct OptionsIpc { bool dataOwnership; // true indicates the ownership of distributed data is DEVICE, otherwise, ACCOUNT }; +class IRdbService; class IKvStoreDataService : public IRemoteBroker { public: enum { @@ -62,6 +62,7 @@ public: GETDEVICELIST, STARTWATCHDEVICECHANGE, STOPWATCHDEVICECHANGE, + GET_RDB_SERVICE, SERVICE_CMD_LAST, DATAUSAGESTART = 20, DATAUSAGEEND = 40, @@ -97,6 +98,7 @@ public: virtual Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy) = 0; virtual Status StopWatchDeviceChange(sptr observer) = 0; + virtual sptr GetRdbService() = 0; }; class KvStoreDataServiceStub : public IRemoteStub { @@ -116,6 +118,7 @@ private: int32_t StartWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply); int32_t StopWatchDeviceChangeOnRemote(MessageParcel &data, MessageParcel &reply); int32_t GetSingleKvStoreOnRemote(MessageParcel &data, MessageParcel &reply); + int32_t GetRdbServiceOnRemote(MessageParcel& data, MessageParcel& reply); using RequestHandler = int32_t(KvStoreDataServiceStub::*)(MessageParcel&, MessageParcel&); static constexpr RequestHandler HANDLERS[SERVICE_CMD_LAST] = { @@ -131,6 +134,7 @@ private: [GETDEVICELIST] = &KvStoreDataServiceStub::GetDeviceListOnRemote, [STARTWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StartWatchDeviceChangeOnRemote, [STOPWATCHDEVICECHANGE] = &KvStoreDataServiceStub::StopWatchDeviceChangeOnRemote, + [GET_RDB_SERVICE] = &KvStoreDataServiceStub::GetRdbServiceOnRemote, }; }; @@ -167,10 +171,10 @@ public: virtual Status GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy); virtual Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy); virtual Status StopWatchDeviceChange(sptr observer); + virtual sptr GetRdbService(); private: static inline BrokerDelegator delegator_; }; -} // namespace DistributedKv -} // namespace OHOS +} #endif // I_KV_STORE_DATA_SERVICE_H diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h new file mode 100644 index 000000000..bca6359de --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_service.h @@ -0,0 +1,42 @@ +/* + * 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 DISTRIBUTEDDATAFWK_IRDB_SERVICE_H +#define DISTRIBUTEDDATAFWK_IRDB_SERVICE_H + +#include + +#include +#include "irdb_store.h" +#include "rdb_parcel.h" +#include "rdb_client_death_recipient.h" + +namespace OHOS::DistributedKv { +class IRdbService : public IRemoteBroker { +public: + enum { + RDB_SERVICE_CMD_GET_STORE, + RDB_SERVICE_CMD_REGISTER_CLIENT_DEATH, + RDB_SERVICE_CMD_MAX + }; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbService"); + + virtual sptr GetRdbStore(const RdbStoreParam& param) = 0; + + virtual int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) = 0; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h new file mode 100644 index 000000000..86e32f07e --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/irdb_store.h @@ -0,0 +1,37 @@ +/* + * 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 DISTRUBTEDDATAFWK_IRDB_STORE_H +#define DISTRUBTEDDATAFWK_IRDB_STORE_H + +#include +#include "rdb_parcel.h" + +namespace OHOS::DistributedKv { +class IRdbStore : public IRemoteBroker { +public: + enum { + RDB_STORE_CMD_SET_DIST_TABLES, + RDB_STORE_CMD_SYNC, + RDB_STORE_CMD_SUBSCRIBE, + RDB_STORE_CMD_REMOVE_DEVICE_DATA, + RDB_STORE_CMD_MAX + }; + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbStore"); + + virtual int SetDistributedTables(const std::vector& tables) = 0; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h new file mode 100644 index 000000000..7cb33d8ee --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_client_death_recipient.h @@ -0,0 +1,45 @@ +/* + * 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 IRDB_CLIENT_DEATH_RECIPIENT_H +#define IRDB_CLIENT_DEATH_RECIPIENT_H + +#include +#include +#include + +namespace OHOS::DistributedKv { +class IRdbClientDeathRecipient : public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IRdbClientDeathRecipient"); +}; + +class RdbClientDeathRecipientProxy : public IRemoteProxy { +public: + RdbClientDeathRecipientProxy(const sptr& object); + virtual ~RdbClientDeathRecipientProxy(); + +private: + static inline BrokerDelegator delegator_; +}; + +class RdbClientDeathRecipientStub : public IRemoteStub { +public: + RdbClientDeathRecipientStub(); + virtual ~RdbClientDeathRecipientStub(); + int OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) override; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h new file mode 100644 index 000000000..b1023dd4d --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h @@ -0,0 +1,46 @@ +/* + * 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 DISTRIBUTEDDATAFWK_RDB_PARCEL_H +#define DISTRIBUTEDDATAFWK_RDB_PARCEL_H + +#include +#include + +namespace OHOS::DistributedKv { +enum RdbDistributedType { + RDB_DEVICE_COLLABORATION, + RDB_DISTRIBUTED_TYPE_MAX +}; + +struct RdbStoreParam { +public: + RdbStoreParam() = default; + RdbStoreParam(const RdbStoreParam& param) = default; + RdbStoreParam(const std::string& bundleName, const std::string& directory, + const std::string& storeName, int type = RDB_DEVICE_COLLABORATION, bool isAutoSync = false); + bool IsValid() const; + bool Marshalling(MessageParcel& data) const; + bool UnMarshalling(MessageParcel& data); + bool operator==(const RdbStoreParam& rhs) const; + + std::string bundleName_; + std::string path_; + std::string storeName_; + int type_ = 0; + bool isAutoSync_ = false; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h new file mode 100644 index 000000000..86cab5f82 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_proxy.h @@ -0,0 +1,35 @@ +/* + * 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 DISTRIBUTEDDATAFWK_IRDB_SERVICE_PROXY_H +#define DISTRIBUTEDDATAFWK_IRDB_SERVICE_PROXY_H + +#include +#include "irdb_service.h" + +namespace OHOS::DistributedKv { +class RdbServiceProxy : public IRemoteProxy { +public: + explicit RdbServiceProxy(const sptr& object); + + sptr GetRdbStore(const RdbStoreParam& param) override; + + int RegisterClientDeathRecipient(const std::string& bundleName, sptr object) override; + +private: + static inline BrokerDelegator delegator_; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h new file mode 100644 index 000000000..ca0d1b149 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_service_stub.h @@ -0,0 +1,41 @@ +/* + * 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 DISTRIBUTEDDATAFWK_IRDB_SERVICE_STUB_H +#define DISTRIBUTEDDATAFWK_IRDB_SERVICE_STUB_H + +#include +#include "irdb_service.h" + +namespace OHOS::DistributedKv { +class RdbServiceStub : public IRemoteStub { +public: + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + static bool CheckInterfaceToken(MessageParcel& data); + + int OnRemoteGetRdbStore(MessageParcel& data, MessageParcel& reply); + + int OnRemoteRegisterClientDeathRecipient(MessageParcel& data, MessageParcel& reply); + + using RequestHandle = int (RdbServiceStub::*)(MessageParcel&, MessageParcel&); + static constexpr RequestHandle HANDLES[RDB_SERVICE_CMD_MAX] = { + [RDB_SERVICE_CMD_GET_STORE] = &RdbServiceStub::OnRemoteGetRdbStore, + [RDB_SERVICE_CMD_REGISTER_CLIENT_DEATH] = &RdbServiceStub::OnRemoteRegisterClientDeathRecipient, + }; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h new file mode 100644 index 000000000..c25443272 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_proxy.h @@ -0,0 +1,33 @@ +/* + * 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 DISTRIBUTEDDATAFWK_IRDB_STORE_PROXY_H +#define DISTRIBUTEDDATAFWK_IRDB_STORE_PROXY_H + +#include +#include "irdb_store.h" + +namespace OHOS::DistributedKv { +class RdbStoreProxy : public IRemoteProxy { +public: + explicit RdbStoreProxy(const sptr& object); + + int SetDistributedTables(const std::vector& tables) override; + +private: + static inline BrokerDelegator delegator_; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h new file mode 100644 index 000000000..44af71260 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_store_stub.h @@ -0,0 +1,38 @@ +/* + * 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 DISTRIBUTEDDATAFWK_IRDB_STORE_STUB_H +#define DISTRIBUTEDDATAFWK_IRDB_STORE_STUB_H + +#include +#include "irdb_store.h" + +namespace OHOS::DistributedKv { +class RdbStoreStub : public IRemoteStub { +public: + int OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) override; + +private: + static bool CheckInterfaceToken(MessageParcel& data); + + int OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply); + + using RequestHandle = int (RdbStoreStub::*)(MessageParcel &, MessageParcel &); + static constexpr RequestHandle HANDLERS[RDB_STORE_CMD_MAX] = { + [RDB_STORE_CMD_SET_DIST_TABLES] = &RdbStoreStub::OnRemoteSetDistributedTables, + }; +}; +} +#endif diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp index 7cd68ee1c..be7827b39 100755 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/ikvstore_data_service.cpp @@ -17,6 +17,7 @@ #include "ikvstore_data_service.h" #include "constant.h" +#include "irdb_service.h" #include "message_parcel.h" #include "types.h" #include "log_print.h" @@ -389,6 +390,24 @@ Status KvStoreDataServiceProxy::StopWatchDeviceChange(sptr(reply.ReadInt32()); } +sptr KvStoreDataServiceProxy::GetRdbService() +{ + MessageParcel data; + if (!data.WriteInterfaceToken(KvStoreDataServiceProxy::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return nullptr; + } + MessageParcel reply; + MessageOption mo { MessageOption::TF_SYNC }; + int32_t error = Remote()->SendRequest(GET_RDB_SERVICE, data, reply, mo); + if (error != 0) { + ZLOGW("SendRequest returned %{public}d", error); + return nullptr; + } + auto remoteObject = reply.ReadRemoteObject(); + return iface_cast(remoteObject); +} + int32_t KvStoreDataServiceStub::GetKvStoreOnRemote(MessageParcel &data, MessageParcel &reply) { OptionsIpc optionsIpc; @@ -627,6 +646,13 @@ int32_t KvStoreDataServiceStub::GetLocalDeviceOnRemote(MessageParcel &data, Mess return 0; } +int32_t KvStoreDataServiceStub::GetRdbServiceOnRemote(MessageParcel &data, MessageParcel &reply) +{ + auto rdbService = GetRdbService(); + reply.WriteRemoteObject(rdbService->AsObject().GetRefPtr()); + return 0; +} + int32_t KvStoreDataServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) { diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp new file mode 100644 index 000000000..0cca88839 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp @@ -0,0 +1,48 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbClientDeathRecipientProxy" + +#include "rdb_client_death_recipient.h" +#include "log_print.h" + +namespace OHOS::DistributedKv { +RdbClientDeathRecipientProxy::RdbClientDeathRecipientProxy(const sptr &object) + : IRemoteProxy(object) +{ + ZLOGI("construct"); +} + +RdbClientDeathRecipientProxy::~RdbClientDeathRecipientProxy() +{ + ZLOGI("deconstruct"); +} + +RdbClientDeathRecipientStub::RdbClientDeathRecipientStub() +{ + ZLOGI("construct"); +} + +RdbClientDeathRecipientStub::~RdbClientDeathRecipientStub() +{ + ZLOGI("deconstruct"); +} + +int RdbClientDeathRecipientStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, + MessageOption &option) +{ + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} +} diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp new file mode 100644 index 000000000..3ea2da5b2 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp @@ -0,0 +1,90 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbParcel" + +#include "rdb_parcel.h" +#include "log_print.h" + +namespace OHOS::DistributedKv { +RdbStoreParam::RdbStoreParam(const std::string& bundleName, const std::string& directory, + const std::string& storeName, int type, bool isAutoSync) + : bundleName_(bundleName), path_(directory), storeName_(storeName), type_(type), + isAutoSync_(isAutoSync) +{ +} + +bool RdbStoreParam::IsValid() const +{ + return !bundleName_.empty() && !path_.empty() && !storeName_.empty(); +} + +bool RdbStoreParam::Marshalling(MessageParcel& data) const +{ + if (!data.WriteString(bundleName_)) { + ZLOGE("RdbStoreParam write bundle name failed"); + return false; + } + if (!data.WriteString(path_)) { + ZLOGE("RdbStoreParam write directory failed"); + return false; + } + if (!data.WriteString(storeName_)) { + ZLOGE("RdbStoreParam write store name failed"); + return false; + } + if (!data.WriteInt32(type_)) { + ZLOGE("RdbStoreParam write type failed"); + return false; + } + if (!data.WriteBool(isAutoSync_)) { + ZLOGE("RdbStoreParam write auto sync failed"); + return false; + } + return true; +} + +bool RdbStoreParam::UnMarshalling(MessageParcel& data) +{ + if (!data.ReadString(bundleName_)) { + ZLOGE("RdbStoreParam read bundle name failed"); + return false; + } + if (!data.ReadString(path_)) { + ZLOGE("RdbStoreParam read directory failed"); + return false; + } + if (!data.ReadString(storeName_)) { + ZLOGE("RdbStoreParam read store name failed"); + return false; + } + if (!data.ReadInt32(type_)) { + ZLOGE("RdbStoreParam read type failed"); + return false; + } + if (!data.ReadBool(isAutoSync_)) { + ZLOGE("RdbStoreParam read auto sync failed"); + return false; + } + return true; +} + +bool RdbStoreParam::operator==(const RdbStoreParam& rhs) const +{ + return bundleName_ == rhs.bundleName_ && path_ == rhs.bundleName_ && + storeName_ == rhs.storeName_ && type_ == rhs.type_; +} +} + diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp new file mode 100644 index 000000000..28f377b69 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp @@ -0,0 +1,74 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbServiceProxy" + +#include "rdb_service_proxy.h" +#include "irdb_store.h" +#include "log_print.h" + +namespace OHOS::DistributedKv { +RdbServiceProxy::RdbServiceProxy(const sptr &object) + : IRemoteProxy(object) +{ +} + +sptr RdbServiceProxy::GetRdbStore(const RdbStoreParam& param) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return nullptr; + } + if (!param.Marshalling(data)) { + return nullptr; + } + + MessageParcel reply; + MessageOption option; + if (Remote()->SendRequest(RDB_SERVICE_CMD_GET_STORE, data, reply, option) != 0) { + ZLOGE("send request failed"); + return nullptr; + } + + auto remoteObject = reply.ReadRemoteObject(); + return iface_cast(remoteObject); +} + +int RdbServiceProxy::RegisterClientDeathRecipient(const std::string& bundleName, sptr object) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(IRdbService::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return -1; + } + if (!data.WriteString(bundleName)) { + ZLOGE("write bundle name failed"); + return -1; + } + if (!data.WriteRemoteObject(object)) { + ZLOGE("write remote object failed"); + return -1; + } + MessageParcel reply; + MessageOption option; + if (Remote()->SendRequest(RDB_SERVICE_CMD_REGISTER_CLIENT_DEATH, data, reply, option) != 0) { + ZLOGE("send request failed"); + return -1; + } + int32_t res = -1; + return reply.ReadInt32(res) ? res : -1; +} +} \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp new file mode 100644 index 000000000..777668c9e --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp @@ -0,0 +1,63 @@ +/* + * 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 "rdb_service_stub.h" + +#define LOG_TAG "RdbServiceStub" +#include "log_print.h" +#include "irdb_store.h" + +namespace OHOS::DistributedKv { +int RdbServiceStub::OnRemoteGetRdbStore(MessageParcel& data, MessageParcel& reply) +{ + RdbStoreParam param; + sptr store; + if (param.UnMarshalling(data)) { + store = GetRdbStore(param); + } + reply.WriteRemoteObject(store->AsObject().GetRefPtr()); + return 0; +} + +int RdbServiceStub::OnRemoteRegisterClientDeathRecipient(MessageParcel &data, MessageParcel &reply) +{ + std::string bundleName = data.ReadString(); + auto remoteObject = data.ReadRemoteObject(); + reply.WriteInt32(RegisterClientDeathRecipient(bundleName, remoteObject)); + return 0; +} + +bool RdbServiceStub::CheckInterfaceToken(MessageParcel& data) +{ + auto localDescriptor = IRdbService::GetDescriptor(); + auto remoteDescriptor = data.ReadInterfaceToken(); + if (remoteDescriptor != localDescriptor) { + ZLOGE("interface token is not equal"); + return false; + } + return true; +} +int RdbServiceStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + ZLOGI("code=%{public}d", code); + if (!CheckInterfaceToken(data)) { + return -1; + } + if (code >= 0 && code < RDB_SERVICE_CMD_MAX) { + return (this->*HANDLES[code])(data, reply); + } + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} +} \ No newline at end of file diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp new file mode 100644 index 000000000..749644505 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp @@ -0,0 +1,50 @@ +/* + * 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 "rdb_store_proxy.h" + +#define LOG_TAG "IRdbStoreProxy" +#include "log_print.h" + +namespace OHOS::DistributedKv { +RdbStoreProxy::RdbStoreProxy(const sptr &object) + : IRemoteProxy(object) +{ +} + +int RdbStoreProxy::SetDistributedTables(const std::vector &tables) +{ + MessageParcel data; + if (!data.WriteInterfaceToken(IRdbStore::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return -1; + } + if (!data.WriteStringVector(tables)) { + ZLOGE("write tables failed"); + return -1; + } + + MessageParcel reply; + MessageOption option; + if (Remote()->SendRequest(RDB_STORE_CMD_SET_DIST_TABLES, data, reply, option) != 0) { + ZLOGE("send request failed"); + return -1; + } + + int32_t res = -1; + return reply.ReadInt32(res) ? res : -1; +} +} + diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp new file mode 100644 index 000000000..60ebaecc6 --- /dev/null +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp @@ -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. + */ + +#define LOG_TAG "IRdbStoreStub" + +#include "rdb_store_stub.h" +#include "log_print.h" + +namespace OHOS::DistributedKv { +int RdbStoreStub::OnRemoteSetDistributedTables(MessageParcel &data, MessageParcel &reply) +{ + std::vector tables; + data.ReadStringVector(&tables); + reply.WriteInt32(SetDistributedTables(tables)); + return 0; +} + +bool RdbStoreStub::CheckInterfaceToken(MessageParcel& data) +{ + auto localDescriptor = IRdbStore::GetDescriptor(); + auto remoteDescriptor = data.ReadInterfaceToken(); + if (remoteDescriptor != localDescriptor) { + ZLOGE("interface token is not equal"); + return false; + } + return true; +} + +int RdbStoreStub::OnRemoteRequest(uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option) +{ + ZLOGI("%{public}d", code); + if (!CheckInterfaceToken(data)) { + return -1; + } + if (code >= 0 && code < RDB_STORE_CMD_MAX) { + return (this->*HANDLERS[code])(data, reply); + } + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} +} + diff --git a/interfaces/innerkits/distributeddata/BUILD.gn b/interfaces/innerkits/distributeddata/BUILD.gn index d434691a8..818d75096 100755 --- a/interfaces/innerkits/distributeddata/BUILD.gn +++ b/interfaces/innerkits/distributeddata/BUILD.gn @@ -24,6 +24,7 @@ config("distributeddatafwk_config") { include_dirs = [ "include", "../../../frameworks/innerkitsimpl/distributeddatafwk/include", + "../../../frameworks/innerkitsimpl/distributeddatafwk/include/rdb", "../../../frameworks/innerkitsimpl/distributeddatafwk/src", "//utils/system/safwk/native/include", "//utils/native/base/include", @@ -36,6 +37,7 @@ config("distributeddatafwk_public_config") { include_dirs = [ "include", + "../../../frameworks/innerkitsimpl/distributeddatafwk/include/rdb", "//utils/native/base/include", ] } @@ -69,6 +71,17 @@ ohos_shared_library("distributeddata_inner") { "include/types.h", ] + rdb_sources = [ + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_client_death_recipient.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_proxy.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_service_stub.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_proxy.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_store_stub.cpp", + "../../../frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp", + ] + + sources += rdb_sources + configs = [ ":distributeddatafwk_config" ] deps = [ diff --git a/services/distributeddataservice/app/BUILD.gn b/services/distributeddataservice/app/BUILD.gn index 4a49af541..1505e634f 100755 --- a/services/distributeddataservice/app/BUILD.gn +++ b/services/distributeddataservice/app/BUILD.gn @@ -37,6 +37,7 @@ config("module_private_config") { visibility = [ ":*" ] include_dirs = [ "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include", + "//foundation/distributeddatamgr/distributeddatamgr/frameworks/innerkitsimpl/distributeddatafwk/include/rdb", "//foundation/distributeddatamgr/distributeddatamgr/interfaces/innerkits/app_distributeddata/include", "//utils/system/safwk/native/include", "../adapter/include/account", @@ -51,6 +52,7 @@ config("module_private_config") { "//utils/native/base/include", "include", "src", + "src/rdb", "//third_party/json/single_include", "//base/security/permission/interfaces/innerkits/permission_standard/permissionsdk/main/cpp/include", "//base/security/huks/interfaces/innerkits/huks_standard/main/include", @@ -83,6 +85,19 @@ ohos_shared_library("distributeddataservice") { "src/single_kvstore_impl.cpp", ] + rdb_sources = [ + "src/rdb/rdb_service.cpp", + "src/rdb/rdb_store.cpp", + "src/rdb/rdb_device_store.cpp", + "src/rdb/rdb_store_factory.cpp", + ] + + sources += rdb_sources + + defines = [ + "RELATIONAL_STORE", + ] + configs = [ ":module_private_config" ] ldflags = [ "-Wl,--exclude-libs,libcrypto_static.a" ] deps = [ diff --git a/services/distributeddataservice/app/distributed_data.cfg b/services/distributeddataservice/app/distributed_data.cfg index 232266d06..0ee10ab52 100644 --- a/services/distributeddataservice/app/distributed_data.cfg +++ b/services/distributeddataservice/app/distributed_data.cfg @@ -11,7 +11,7 @@ "path" : ["/system/bin/sa_main","/system/profile/distributeddata.xml"], "caps" : ["DAC_READ_SEARCH"], "uid" : "system", - "gid" : ["system","shell","readproc"], + "gid" : ["system","shell","readproc","ddms"], "writepid":[ "/dev/cpuset/foreground/tasks", "/dev/stune/foreground/tasks", diff --git a/services/distributeddataservice/app/distributed_data.rc b/services/distributeddataservice/app/distributed_data.rc index 7ea8455d1..5f9dc66e4 100644 --- a/services/distributeddataservice/app/distributed_data.rc +++ b/services/distributeddataservice/app/distributed_data.rc @@ -18,6 +18,6 @@ service distributeddata /system/bin/sa_main /system/profile/distributeddata.xml class z_core capabilities DAC_READ_SEARCH user system - group system shell readproc + group system shell readproc ddms seclabel u:r:distributeddata:s0 writepid /dev/cpuset/foreground/tasks /dev/stune/foreground/tasks /dev/blkio/foreground/tasks diff --git a/services/distributeddataservice/app/src/kvstore_data_service.cpp b/services/distributeddataservice/app/src/kvstore_data_service.cpp index 78e01b8d2..47b1e7dc6 100644 --- a/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -44,11 +44,11 @@ #include "permission_validator.h" #include "process_communicator_impl.h" #include "reporter.h" +#include "rdb_service.h" #include "system_ability_definition.h" #include "uninstaller/uninstaller.h" -namespace OHOS { -namespace DistributedKv { +namespace OHOS::DistributedKv { using json = nlohmann::json; using namespace std::chrono; using namespace OHOS::Security::Permission; @@ -978,6 +978,7 @@ void KvStoreDataService::OnStart() return; } } + CreateRdbService(); StartService(); } @@ -1328,6 +1329,20 @@ Status KvStoreDataService::StopWatchDeviceChange(sptr KvStoreDataService::GetRdbService() +{ + return rdbService_; +} + bool DbMetaCallbackDelegateMgr::GetKvStoreDiskSize(const std::string &storeId, uint64_t &size) { if (IsDestruct()) { @@ -1374,5 +1389,4 @@ void DbMetaCallbackDelegateMgr::GetKvStoreKeys(std::vector &dbStats) } delegate_->CloseKvStore(kvStoreNbDelegatePtr); } -} // namespace DistributedKv -} // namespace OHOS +} diff --git a/services/distributeddataservice/app/src/kvstore_data_service.h b/services/distributeddataservice/app/src/kvstore_data_service.h index 01d75536a..8969210f9 100755 --- a/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/services/distributeddataservice/app/src/kvstore_data_service.h @@ -31,9 +31,9 @@ #include "backup_handler.h" #include "device_change_listener_impl.h" -namespace OHOS { -namespace DistributedKv { +namespace OHOS::DistributedKv { +class IRdbService; class KvStoreAccountObserver; class KvStoreDataService : public SystemAbility, public KvStoreDataServiceStub { DECLARE_SYSTEM_ABILITY(KvStoreDataService); @@ -68,7 +68,8 @@ public: Status GetDeviceList(std::vector &deviceInfoList, DeviceFilterStrategy strategy) override; Status StartWatchDeviceChange(sptr observer, DeviceFilterStrategy strategy) override; Status StopWatchDeviceChange(sptr observer) override; - + sptr GetRdbService() override; + void OnDump() override; int Dump(int fd, const std::vector &args) override; @@ -94,6 +95,7 @@ public: Status RecoverMultiKvStore(const Options &options, const std::string &bundleName, const std::string &storeId, const std::vector &secretKey, std::function)> callback); + struct GetKvStorePara { std::string bundleName; std::string storeId; @@ -168,6 +170,8 @@ private: void ResolveAutoLaunchParamByIdentifier(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); bool CheckOptions(const Options &options, const std::vector &metaKey) const; + + void CreateRdbService(); static constexpr int TEN_SEC = 10; @@ -180,6 +184,8 @@ private: std::map> deviceListeners_; std::mutex deviceListenerMutex_; std::shared_ptr deviceListener_; + + sptr rdbService_; }; class DbMetaCallbackDelegateMgr : public DbMetaCallbackDelegate { @@ -216,6 +222,5 @@ private: static const inline int STORE_ID = 2; static const inline int VECTOR_SIZE = 2; }; -} // namespace DistributedKv -} // namespace OHOS +} #endif // KVSTORE_DATASERVICE_H diff --git a/services/distributeddataservice/app/src/rdb/rdb_device_store.cpp b/services/distributeddataservice/app/src/rdb/rdb_device_store.cpp new file mode 100644 index 000000000..115270d98 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_device_store.cpp @@ -0,0 +1,112 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbDeviceStore" + +#include "rdb_device_store.h" +#include "log_print.h" +#include "rdb_store_factory.h" + +using namespace DistributedDB; +namespace OHOS::DistributedKv { +void RdbDeviceStore::Initialize() +{ + RdbStoreFactory::Creator creator = RdbDeviceStore::CreateStore; + RdbStoreFactory::RegisterCreator(RDB_DEVICE_COLLABORATION, creator); +} + +RdbStore* RdbDeviceStore::CreateStore(const RdbStoreParam ¶m) +{ + ZLOGI("create device collaboration store"); + return new(std::nothrow) RdbDeviceStore(param); +} + +RdbDeviceStore::RdbDeviceStore(const RdbStoreParam ¶m) + : RdbStore(param), manager_(nullptr), delegate_(nullptr) +{ + ZLOGI("construct %{public}s", param.storeName_.c_str()); +} + +RdbDeviceStore::~RdbDeviceStore() +{ + ZLOGI("destroy"); + if (manager_ != nullptr & delegate_ != nullptr) { + manager_->CloseStore(delegate_); + } + delete manager_; +} + +int RdbDeviceStore::Init() +{ + ZLOGI("enter"); + manager_ = new(std::nothrow) RelationalStoreManager(GetAppId(), GetUserId()); + if (manager_ == nullptr) { + ZLOGE("malloc manager failed"); + return -1; + } + if (CreateMetaData() != 0) { + ZLOGE("create meta data failed"); + return -1; + } + ZLOGI("success"); + return 0; +} + +int RdbDeviceStore::CreateMetaData() +{ + ZLOGI("enter"); + return 0; +} + +int RdbDeviceStore::SetDistributedTables(const std::vector &tables) +{ + auto delegate = GetDelegate(); + if (delegate == nullptr) { + return -1; + } + + for (const auto& table : tables) { + ZLOGI("%{public}s", table.c_str()); + if (delegate->CreateDistributedTable(table) != DBStatus::OK) { + ZLOGE("create distributed table failed"); + return -1; + } + } + ZLOGE("create distributed table success"); + return 0; +} + +RelationalStoreDelegate* RdbDeviceStore::GetDelegate() +{ + if (manager_ == nullptr) { + ZLOGE("manager_ is nullptr"); + return nullptr; + } + + std::lock_guard lock(mutex_); + if (delegate_ == nullptr) { + ZLOGI("create delegate"); + RelationalStoreDelegate::Option option; + DBStatus status = manager_->OpenStore(GetPath(), GetStoreId(), option, delegate_); + if (status != DBStatus::OK) { + ZLOGE("open store failed status=%{public}d", status); + return nullptr; + } + ZLOGI("open store success"); + } + return delegate_; +} +} + diff --git a/services/distributeddataservice/app/src/rdb/rdb_device_store.h b/services/distributeddataservice/app/src/rdb/rdb_device_store.h new file mode 100644 index 000000000..f353c3d77 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_device_store.h @@ -0,0 +1,58 @@ +/* + * 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 RDB_DEVICE_STORE_H +#define RDB_DEVICE_STORE_H + +#include "rdb_store.h" +#include "rdb_store_stub.h" +#include "relational_store_manager.h" +#include "relational_store_delegate.h" + +namespace OHOS::DistributedKv { +//namespace DistributedDB { +//class RelationalStoreManager; +//class RelationalStoreDelegate; +//} + +class RdbDeviceStore : public RdbStore { +public: + explicit RdbDeviceStore(const RdbStoreParam& param); + + virtual ~RdbDeviceStore(); + + static void Initialize(); + static RdbStore* CreateStore(const RdbStoreParam& param); + + /* IPC interface */ + int SetDistributedTables(const std::vector& tables) override; + + /* RdbStore interface */ + int Init() override; + +private: + int CreateMetaData(); + + DistributedDB::RelationalStoreManager* GetManager(); + + DistributedDB::RelationalStoreDelegate* GetDelegate(); + + std::mutex mutex_; + DistributedDB::RelationalStoreManager* manager_; + DistributedDB::RelationalStoreDelegate* delegate_; + +}; +} +#endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_service.cpp b/services/distributeddataservice/app/src/rdb/rdb_service.cpp new file mode 100644 index 000000000..c45375323 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_service.cpp @@ -0,0 +1,156 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbService" + +#include "rdb_service.h" +#include "kvstore_utils.h" +#include "log_print.h" +#include "rdb_store.h" +#include "rdb_store_factory.h" + +namespace OHOS::DistributedKv { +static bool operator==(const sptr& store, const std::string& appId) +{ + auto* storePtr = (RdbStore*)store.GetRefPtr(); + return storePtr != nullptr && (storePtr->GetAppId() == appId); +} + +void RdbService::Initialzie() +{ + RdbStoreFactory::Initialize(); +} + +RdbService::ClientDeathRecipient::ClientDeathRecipient(DeathCallback& callback) + : callback_(callback) +{ + ZLOGI("construct"); +} + +RdbService::ClientDeathRecipient::~ClientDeathRecipient() +{ + ZLOGI("deconstruct"); +} + +void RdbService::ClientDeathRecipient::OnRemoteDied(const wptr& object) +{ + auto objectSptr = object.promote(); + if (objectSptr != nullptr && callback_ != nullptr) { + callback_(objectSptr); + } +} + +void RdbService::ClearClientRecipient(const std::string& bundleName, sptr& proxy) +{ + std::lock_guard lock(recipientsLock_); + ZLOGI("remove %{public}s", bundleName.c_str()); + recipients_.erase(proxy); +} + +void RdbService::ClearClientStores(const std::string& bundleName) +{ + ZLOGI("enter"); + auto appId = KvStoreUtils::GetAppIdByBundleName(bundleName); + std::lock_guard lock(storesLock_); + for (auto it = stores_.begin(); it != stores_.end();) { + if (it->second == appId) { + ZLOGI("remove %{public}s", it->first.c_str()); + stores_.erase(it++); + } else { + it++; + } + } +} + +void RdbService::OnClientDied(const std::string &bundleName, sptr& proxy) +{ + ZLOGI("%{public}s died", bundleName.c_str()); + ClearClientRecipient(bundleName, proxy); + ClearClientStores(bundleName); +} + +bool RdbService::CheckAccess(const RdbStoreParam& param) const +{ + return !KvStoreUtils::GetAppIdByBundleName(param.bundleName_).empty(); +} + +sptr RdbService::CreateStore(const RdbStoreParam& param) +{ + sptr store = RdbStoreFactory::CreateStore(param); + if (store == nullptr) { + ZLOGE("create temp store failed"); + return nullptr; + } + + std::lock_guard lock(storesLock_); + for (const auto& entry : stores_) { + if (*store == *entry.second) { + ZLOGI("find %{public}s", store->GetStoreId().c_str()); + return entry.second; + } + } + + ZLOGI("create new store %{public}s", param.storeName_.c_str()); + if (store->Init() != 0) { + ZLOGE("store init failed"); + return nullptr; + } + stores_.insert({store->GetIdentifier(), store}); + return store; +} + +sptr RdbService::GetRdbStore(const RdbStoreParam& param) +{ + ZLOGI("%{public}s %{public}s %{public}s", param.bundleName_.c_str(), + param.path_.c_str(), param.storeName_.c_str()); + if (!CheckAccess(param)) { + ZLOGI("check access failed"); + return nullptr; + } + return CreateStore(param); +} + +int RdbService::RegisterClientDeathRecipient(const std::string& bundleName, sptr proxy) +{ + if (proxy == nullptr) { + ZLOGE("recipient is nullptr"); + return -1; + } + + std::lock_guard lock(recipientsLock_); + ClientDeathRecipient::DeathCallback callback = [bundleName, this] (sptr& object) { + OnClientDied(bundleName, object); + }; + sptr recipient = new(std::nothrow) ClientDeathRecipient(callback); + if (recipient == nullptr) { + ZLOGE("malloc failed"); + return -1; + } + if (!proxy->AddDeathRecipient(recipient)) { + ZLOGE("add death recipient failed"); + return -1; + } + auto it = recipients_.insert({proxy, recipient}); + if (!it.second) { + ZLOGE("insert failed"); + return -1; + } + ZLOGI("success"); + return 0; +} +} + + + diff --git a/services/distributeddataservice/app/src/rdb/rdb_service.h b/services/distributeddataservice/app/src/rdb/rdb_service.h new file mode 100644 index 000000000..40b132644 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_service.h @@ -0,0 +1,62 @@ +/* + * 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 DISTRIBUTEDDATASERVICE_RDB_SERVICE_H +#define DISTRIBUTEDDATASERVICE_RDB_SERVICE_H + +#include "rdb_service_stub.h" + +#include +#include +#include +#include "rdb_store.h" + +namespace OHOS::DistributedKv { +class RdbService : public RdbServiceStub { +public: + sptr GetRdbStore(const RdbStoreParam& param) override; + + int RegisterClientDeathRecipient(const std::string& bundleName, sptr proxy) override; + + void OnClientDied(const std::string& bundleName, sptr& proxy); + + static void Initialzie(); + +private: + bool CheckAccess(const RdbStoreParam& param) const; + + sptr CreateStore(const RdbStoreParam& param); + + void ClearClientRecipient(const std::string& bundleName, sptr& proxy); + + void ClearClientStores(const std::string& bundleName); + + class ClientDeathRecipient : public DeathRecipient { + public: + using DeathCallback = std::function&)>; + explicit ClientDeathRecipient(DeathCallback& callback); + ~ClientDeathRecipient() override; + void OnRemoteDied(const wptr &object) override; + private: + DeathCallback callback_; + }; + + std::mutex storesLock_; + std::map> stores_; // identifier + std::mutex recipientsLock_; + std::map, sptr> recipients_; +}; +} +#endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_store.cpp b/services/distributeddataservice/app/src/rdb/rdb_store.cpp new file mode 100644 index 000000000..a93631886 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_store.cpp @@ -0,0 +1,78 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbStore" + +#include "rdb_store.h" +#include "kvstore_utils.h" +#include "log_print.h" + +namespace OHOS::DistributedKv { +static std::string GetCurrentUserId() +{ + return "0"; +} + +RdbStore::RdbStore(const RdbStoreParam ¶m) + : type_(param.type_), bundleName_(param.bundleName_), path_(param.path_), + storeId_(param.storeName_) +{ + ZLOGI("construct %{public}s %{public}s %{public}s %{public}d", + bundleName_.c_str(), userId_.c_str(), storeId_.c_str(), type_); + appId_ = KvStoreUtils::GetAppIdByBundleName(bundleName_); + userId_ = GetCurrentUserId(); + identifier_ = std::to_string(type_) + "-" + appId_ + "-" + userId_ + "-" + storeId_; +} + +RdbStore::~RdbStore() +{ + ZLOGI("destroy %{public}s", storeId_.c_str()); +} + +bool RdbStore::operator==(const RdbStore& rhs) const +{ + return identifier_ == rhs.identifier_; +} + +std::string RdbStore::GetBundleName() const +{ + return bundleName_; +} + +std::string RdbStore::GetAppId() const +{ + return appId_; +} + +std::string RdbStore::GetUserId() const +{ + return userId_; +} + +std::string RdbStore::GetStoreId() const +{ + return storeId_; +} + +std::string RdbStore::GetIdentifier() const +{ + return identifier_; +} + +std::string RdbStore::GetPath() const +{ + return path_; +} +} \ No newline at end of file diff --git a/services/distributeddataservice/app/src/rdb/rdb_store.h b/services/distributeddataservice/app/src/rdb/rdb_store.h new file mode 100644 index 000000000..1fdad04ca --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_store.h @@ -0,0 +1,59 @@ +/* + * 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 DISTRIBUTEDDATASERVICE_RDB_STORE_H +#define DISTRIBUTEDDATASERVICE_RDB_STORE_H + +#include "rdb_store_stub.h" +#include "rdb_parcel.h" + +namespace OHOS::DistributedKv { +class RdbStore : public RdbStoreStub { +public: + explicit RdbStore(const RdbStoreParam& param); + + RdbStore() = delete; + RdbStore(const RdbStore&) = delete; + RdbStore& operator=(const RdbStore&) = delete; + + virtual ~RdbStore(); + + virtual int Init() = 0; + + bool operator==(const RdbStore& rhs) const; + + std::string GetBundleName() const; + + std::string GetAppId() const; + + std::string GetUserId() const; + + std::string GetStoreId() const; + + std::string GetIdentifier() const; + + std::string GetPath() const; + +private: + int type_; + std::string bundleName_; + std::string path_; + std::string storeId_; + std::string appId_; + std::string userId_; + std::string identifier_; +}; +} +#endif diff --git a/services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp b/services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp new file mode 100644 index 000000000..0fd19ea88 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_store_factory.cpp @@ -0,0 +1,56 @@ +/* + * 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. + */ + +#define LOG_TAG "RdbStoreFactory" + +#include "rdb_store_factory.h" +#include "log_print.h" +#include "rdb_device_store.h" + +namespace OHOS::DistributedKv { +RdbStoreFactory::Creator RdbStoreFactory::creators_[RDB_DISTRIBUTED_TYPE_MAX]; + +void RdbStoreFactory::Initialize() +{ + RdbDeviceStore::Initialize(); +} + +int RdbStoreFactory::RegisterCreator(int type, Creator &creator) +{ + if (type < 0 || type >= RDB_DISTRIBUTED_TYPE_MAX) { + ZLOGE("type=%{public}d is invalid", type); + return -1; + } + if (creator == nullptr) { + ZLOGE("creator is empty"); + return -1; + } + ZLOGI("add creator for store type %{public}d", type); + creators_[type] = creator; + return 0; +} + +RdbStore* RdbStoreFactory::CreateStore(const RdbStoreParam& param) +{ + int type = param.type_; + if (type < 0 || type >= RDB_DISTRIBUTED_TYPE_MAX) { + ZLOGE("type=%{public}d is invalid", type); + return nullptr; + } + + return (creators_[type])(param); +} +} + diff --git a/services/distributeddataservice/app/src/rdb/rdb_store_factory.h b/services/distributeddataservice/app/src/rdb/rdb_store_factory.h new file mode 100644 index 000000000..5e7afbf99 --- /dev/null +++ b/services/distributeddataservice/app/src/rdb/rdb_store_factory.h @@ -0,0 +1,37 @@ +/* + * 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 RDB_STORE_FACTORY_H +#define RDB_STORE_FACTORY_H + +#include +#include "rdb_store.h" + +namespace OHOS::DistributedKv { +class RdbStoreFactory { +public: + using Creator = std::function; + + static void Initialize(); + + static int RegisterCreator(int type, Creator& creator); + + static RdbStore* CreateStore(const RdbStoreParam& param); + +private: + static Creator creators_[RDB_DISTRIBUTED_TYPE_MAX]; +}; +} +#endif diff --git a/services/distributeddataservice/app/test/BUILD.gn b/services/distributeddataservice/app/test/BUILD.gn index e85add8d5..52e8b0c7d 100755 --- a/services/distributeddataservice/app/test/BUILD.gn +++ b/services/distributeddataservice/app/test/BUILD.gn @@ -30,6 +30,7 @@ config("module_private_config") { "//utils/system/safwk/native/include", "../include", "../src", + "../src/rdb", "../src/uninstaller", "../src/flowctrl_manager", "../../../../interfaces/innerkits/distributeddata", @@ -59,6 +60,10 @@ ohos_unittest("KvStoreImplLogicalIsolationTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", + "../src/rdb/rdb_store_factory.cpp", + "../src/rdb/rdb_store.cpp", + "../src/rdb/rdb_device_store.cpp", "unittest/kvstore_impl_logical_isolation_test.cpp", ] @@ -112,6 +117,10 @@ ohos_unittest("KvStoreImplPhysicalIsolationTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", + "../src/rdb/rdb_store_factory.cpp", + "../src/rdb/rdb_store.cpp", + "../src/rdb/rdb_device_store.cpp", "unittest/kvstore_impl_physical_isolation_test.cpp", ] @@ -165,6 +174,10 @@ ohos_unittest("KvStoreDataServiceTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", + "../src/rdb/rdb_store_factory.cpp", + "../src/rdb/rdb_store.cpp", + "../src/rdb/rdb_device_store.cpp", "unittest/kvstore_data_service_test.cpp", ] @@ -216,6 +229,7 @@ ohos_unittest("KvStoreBackupTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", "unittest/kvstore_app_manager.cpp", "unittest/kvstore_backup_test.cpp", "unittest/kvstore_data_service.cpp", @@ -300,6 +314,10 @@ ohos_unittest("KvStoreSyncManagerTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", + "../src/rdb/rdb_store_factory.cpp", + "../src/rdb/rdb_store.cpp", + "../src/rdb/rdb_device_store.cpp", ] configs = [ ":module_private_config" ] @@ -352,6 +370,10 @@ ohos_unittest("KvStoreUninstallerTest") { "../src/kvstore_user_manager.cpp", "../src/query_helper.cpp", "../src/single_kvstore_impl.cpp", + "../src/rdb/rdb_service.cpp", + "../src/rdb/rdb_store_factory.cpp", + "../src/rdb/rdb_store.cpp", + "../src/rdb/rdb_device_store.cpp", "unittest/uninstaller_test.cpp", ] diff --git a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp index 2f4aacf3f..03ce10ed9 100644 --- a/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp +++ b/services/distributeddataservice/app/test/unittest/kvstore_data_service.cpp @@ -41,6 +41,7 @@ #include "permission_validator.h" #include "process_communicator_impl.h" #include "reporter.h" +#include "rdb_service.h" #include "system_ability_definition.h" #include "uninstaller/uninstaller.h" @@ -1304,6 +1305,11 @@ Status KvStoreDataService::StopWatchDeviceChange(sptr KvStoreDataService::GetRdbService() +{ + return rdbService_; +} + bool DbMetaCallbackDelegateMgr::GetKvStoreDiskSize(const std::string &storeId, uint64_t &size) { if (IsDestruct()) { -- Gitee From a48e5267450e8c18b88e6b04e22e8790f741a9eb Mon Sep 17 00:00:00 2001 From: wuchunbo Date: Sun, 16 Jan 2022 02:18:39 +0800 Subject: [PATCH 2/2] remove no use code Signed-off-by: wuchunbo --- .../distributeddatafwk/include/rdb/rdb_parcel.h | 1 - .../innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp | 6 ------ 2 files changed, 7 deletions(-) diff --git a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h index b1023dd4d..2e2c25239 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h +++ b/frameworks/innerkitsimpl/distributeddatafwk/include/rdb/rdb_parcel.h @@ -34,7 +34,6 @@ public: bool IsValid() const; bool Marshalling(MessageParcel& data) const; bool UnMarshalling(MessageParcel& data); - bool operator==(const RdbStoreParam& rhs) const; std::string bundleName_; std::string path_; diff --git a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp index 3ea2da5b2..e4481c9cb 100644 --- a/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp +++ b/frameworks/innerkitsimpl/distributeddatafwk/src/rdb/rdb_parcel.cpp @@ -80,11 +80,5 @@ bool RdbStoreParam::UnMarshalling(MessageParcel& data) } return true; } - -bool RdbStoreParam::operator==(const RdbStoreParam& rhs) const -{ - return bundleName_ == rhs.bundleName_ && path_ == rhs.bundleName_ && - storeName_ == rhs.storeName_ && type_ == rhs.type_; -} } -- Gitee