From ca0842764ece4052d0ac09fd02a04ff0da6666df Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Tue, 4 Apr 2023 17:25:07 +0800 Subject: [PATCH 1/9] fix code Signed-off-by: zuojiangjiang --- .../framework/cloud/auto_cache.cpp | 2 +- .../framework/cloud/cloud_info.cpp | 20 ++- .../framework/include/cloud/cloud_info.h | 3 +- relational_store/CMakeLists.txt | 3 + .../cloud_data/include/cloud_manager_impl.h | 51 ++++++++ .../cloud_data/include/cloud_service_proxy.h | 39 ++++++ .../cloud_data/include/icloud_service.h | 37 ++++++ .../native/cloud_data/src/cloud_manager.cpp | 24 ++++ .../cloud_data/src/cloud_manager_impl.cpp | 122 ++++++++++++++++++ .../cloud_data/src/cloud_service_proxy.cpp | 105 +++++++++++++++ .../cloud_data/include/cloud_manager.h | 33 +++++ .../cloud_data/include/cloud_service.h | 8 ++ 12 files changed, 440 insertions(+), 7 deletions(-) create mode 100644 relational_store/frameworks/native/cloud_data/include/cloud_manager_impl.h create mode 100644 relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h create mode 100644 relational_store/frameworks/native/cloud_data/include/icloud_service.h create mode 100644 relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp create mode 100644 relational_store/frameworks/native/cloud_data/src/cloud_manager_impl.cpp create mode 100644 relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp create mode 100644 relational_store/interfaces/inner_api/cloud_data/include/cloud_manager.h diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/auto_cache.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/auto_cache.cpp index c080d640..1b202c42 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/auto_cache.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/auto_cache.cpp @@ -13,5 +13,5 @@ * limitations under the License. */ -#include "store_auto_cache.h" +#include "store/auto_cache.h" diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index 17d7b1f1..d8080306 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -14,6 +14,8 @@ */ #include "cloud/cloud_info.h" +#include "utils/constant.h" + namespace OHOS::DistributedData { bool CloudInfo::Marshal(Serializable::json &node) const { @@ -57,14 +59,22 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) std::string CloudInfo::GetKey() const { - return std::string(); + return GetKey(INFO_PREFIX, { id, std::to_string(user) }); } -std::map CloudInfo::GetSchemaKey() const +std::vector CloudInfo::GetSchemaKey() const { - for (auto &app : apps) { - + std::vector keys; + keys.reserve(apps.size()); + for (const auto &app : apps) { + const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), app.bundleName }); + keys.push_back(key); } - return std::map(); + return keys; +} + +std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list &fields) const +{ + return Constant::Join(prefix, Constant::KEY_SEPARATOR, fields); } } // namespace OHOS::DistributedData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index 22f37cbb..b9bee281 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -36,7 +36,8 @@ public: std::vector apps; std::string GetKey() const; - std::map GetSchemaKey() const; + std::vector GetSchemaKey() const; + std::string GetKey(const std::string &prefix, const std::initializer_list &fields) const; bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; diff --git a/relational_store/CMakeLists.txt b/relational_store/CMakeLists.txt index 559ae0fd..ab7ad9b0 100644 --- a/relational_store/CMakeLists.txt +++ b/relational_store/CMakeLists.txt @@ -12,6 +12,7 @@ set(MOCK_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../mock) set(KV_STORE_DIR ${CMAKE_CURRENT_SOURCE_DIR}/../kv_store) add_definitions(-DNAPI_EXPERIMENTAL) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/appdatafwk/src relational_store_src) +aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/cloud_data/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/dataability/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_data_ability_adapter/src relational_store_src) @@ -19,11 +20,13 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_data_shar aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/src relational_store_src) aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/rdb/src relational_store_src) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/cloud_data/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/common/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/dataability/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/js/napi/rdb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/appdatafwk/include) +include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/cloud_data/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/dataability/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/rdb/include) include_directories(${CMAKE_CURRENT_SOURCE_DIR}/interfaces/inner_api/rdb_data_ability_adapter/include) diff --git a/relational_store/frameworks/native/cloud_data/include/cloud_manager_impl.h b/relational_store/frameworks/native/cloud_data/include/cloud_manager_impl.h new file mode 100644 index 00000000..2c8501ee --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/include/cloud_manager_impl.h @@ -0,0 +1,51 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_IMPL_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_IMPL_H + +#include +#include "cloud_service.h" +#include "cloud_service_proxy.h" +#include "icloud_service.h" +#include "iremote_object.h" +#include "iremote_proxy.h" + +namespace OHOS::CloudData { +class CloudDataServiceProxy; +class CloudManagerImpl { +public: + static CloudManagerImpl &GetInstance(); + std::shared_ptr GetCloudService(); + +private: + CloudManagerImpl() = default; + ~CloudManagerImpl() = default; + static std::shared_ptr GetDistributedDataManager(); + sptr GetCloudServiceProxy(); + + std::shared_ptr distributedDataMgr_; + std::mutex mutex_; + std::shared_ptr cloudService_; +}; + +class CloudDataServiceProxy : public IRemoteProxy { +public: + explicit CloudDataServiceProxy(const sptr &impl); + ~CloudDataServiceProxy() = default; + sptr GetFeatureInterface(const std::string &name) override; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_IMPL_H diff --git a/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h b/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h new file mode 100644 index 00000000..8ece3de8 --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h @@ -0,0 +1,39 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_SERVICE_PROXY_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_SERVICE_PROXY_H + +#include "icloud_service.h" +#include "iremote_object.h"" +#include "iremote_proxy.h" + +namespace OHOS::CloudData { +class CloudServiceProxy: public IRemoteProxy { +public: + explicit CloudServiceProxy(const sptr& object); + virtual ~CloudServiceProxy() = default; + int32_t EnableCloud(const std::string &id, const std::map &switches) override; + int32_t DisableCloud(const std::string &id) override; + int32_t ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) override; + int32_t Clean(const std::string &id, const std::map &actions) override; + int32_t NotifyDataChange(const std::string &id, const std::string &bundleName) override; + +private: + sptr remote_; + static inline BrokerDelegator delegator_; +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_SERVICE_PROXY_H diff --git a/relational_store/frameworks/native/cloud_data/include/icloud_service.h b/relational_store/frameworks/native/cloud_data/include/icloud_service.h new file mode 100644 index 00000000..a21def4f --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/include/icloud_service.h @@ -0,0 +1,37 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_ICLOUD_SERVICE_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_ICLOUD_SERVICE_H + +#include "cloud_service.h" +#include "iremote_broker.h" + +namespace OHOS::CloudData { +class ICloudService : public CloudService, public IRemoteBroker { +public: + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.CloudData.CloudServer"); +}; + +class IKvStoreDataService : public IRemoteBroker { +public: + enum { GET_FEATURE_INTERFACE = 0 }; + + virtual sptr GetFeatureInterface(const std::string &name) = 0; + + DECLARE_INTERFACE_DESCRIPTOR(u"OHOS.DistributedKv.IKvStoreDataService"); +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_ICLOUD_SERVICE_H diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp new file mode 100644 index 00000000..c9a921c9 --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/src/cloud_manager.cpp @@ -0,0 +1,24 @@ +/* + * Copyright (c) 2023 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 "cloud_manager.h" +#include "cloud_manager_impl.h" + +namespace OHOS::CloudData { +std::shared_ptr CloudManager::GetCloudService() +{ + return CloudManagerImpl::GetInstance().GetCloudService(); +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_manager_impl.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_manager_impl.cpp new file mode 100644 index 00000000..f74edb1d --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/src/cloud_manager_impl.cpp @@ -0,0 +1,122 @@ +/* + * Copyright (c) 2023 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 "CloudManagerImpl" + +#include "cloud_manager_impl.h" +#include "itypes_util.h" +#include "iservice_registry.h" +#include "log_print.h" +#include "system_ability_definition.h" + +namespace OHOS::CloudData { +CloudManagerImpl &CloudManagerImpl::GetInstance() +{ + static CloudManagerImpl instance; + return instance; +} + +std::shared_ptr CloudManagerImpl::GetCloudService() +{ + std::lock_guard lg(mutex_); + if (cloudService_ != nullptr) { + return cloudService_; + } + auto proxy = GetCloudServiceProxy(); + if (proxy == nullptr) { + return nullptr; + } + cloudService_ = std::shared_ptr(proxy.GetRefPtr(), [holder = proxy] (const auto*) {}); + if (cloudService_ == nullptr) { + return nullptr; + } + return cloudService_; +} + +sptr CloudManagerImpl::GetCloudServiceProxy() +{ + if (distributedDataMgr_ == nullptr) { + distributedDataMgr_ = GetDistributedDataManager(); + } + if (distributedDataMgr_ == nullptr) { + ZLOGE("get distributed data manager failed"); + return nullptr; + } + + auto remote = distributedDataMgr_->GetFeatureInterface(CloudService::SERVICE_NAME); + if (remote == nullptr) { + ZLOGE("get cloud service failed"); + return nullptr; + } + return iface_cast(remote); +} + +CloudDataServiceProxy::CloudDataServiceProxy(const sptr &impl) + : IRemoteProxy(impl) +{ + ZLOGI("init proxy"); +} + +std::shared_ptr CloudManagerImpl::GetDistributedDataManager() +{ + auto abilityMgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (abilityMgr == nullptr) { + ZLOGE("get system ability manager failed"); + return nullptr; + } + auto remoteObject = abilityMgr->CheckSystemAbility(DISTRIBUTED_KV_DATA_SERVICE_ABILITY_ID); + if (remoteObject == nullptr) { + ZLOGE("get distributed data manager failed"); + return nullptr; + } + sptr proxy = new(std::nothrow) CloudDataServiceProxy(remoteObject); + if (proxy == nullptr) { + ZLOGE("new CloudDataServiceProxy failed"); + return nullptr; + } + return std::shared_ptr(proxy.GetRefPtr(), + [holder = proxy](const auto *) {}); +} + +sptr CloudDataServiceProxy::GetFeatureInterface(const std::string &name) +{ + ZLOGI("%s", name.c_str()); + MessageParcel data; + if (!data.WriteInterfaceToken(CloudDataServiceProxy::GetDescriptor())) { + ZLOGE("write descriptor failed"); + return nullptr; + } + + if (!ITypesUtil::Marshal(data, name)) { + ZLOGE("write descriptor failed"); + return nullptr; + } + + MessageParcel reply; + MessageOption mo { MessageOption::TF_SYNC }; + int32_t error = Remote()->SendRequest(GET_FEATURE_INTERFACE, data, reply, mo); + if (error != 0) { + ZLOGE("SendRequest returned %{public}d", error); + return nullptr; + } + + sptr remoteObject; + if (!ITypesUtil::Unmarshal(reply, remoteObject)) { + ZLOGE("remote object is nullptr"); + return nullptr; + } + return remoteObject; +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp new file mode 100644 index 00000000..fd295595 --- /dev/null +++ b/relational_store/frameworks/native/cloud_data/src/cloud_service_proxy.cpp @@ -0,0 +1,105 @@ +/* + * Copyright (c) 2023 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 "CloudServiceProxy" + +#include "cloud_service_proxy.h" +#include "itypes_util.h" +#include "log_print.h" + +namespace OHOS::CloudData { +#define IPC_SEND(code, reply, ...) \ +({ \ + int32_t __status = SUCCESS; \ + do { \ + MessageParcel request; \ + if (!request.WriteInterfaceToken(GetDescriptor())) { \ + __status = IPC_PARCEL_ERROR; \ + break; \ + } \ + if (!ITypesUtil::Marshal(request, ##__VA_ARGS__)) { \ + __status = IPC_PARCEL_ERROR; \ + break; \ + } \ + MessageOption option; \ + auto result = remote_->SendRequest((code), request, reply, option); \ + if (result != 0) { \ + __status = IPC_ERROR; \ + break; \ + } \ + \ + ITypesUtil::Unmarshal(reply, __status); \ + } while (0); \ + __status; \ +}) + +CloudServiceProxy::CloudServiceProxy(const sptr &object) + : IRemoteProxy(object) +{ + remote_ = Remote(); +} + +int32_t CloudServiceProxy::EnableCloud(const std::string &id, const std::map &switches) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_ENABLE_CLOUD, reply, id, switches); + if (status != SUCCESS) { + ZLOGE("status:0x%{public}x id:%{public}.6s size:%{public}zu", status, id.c_str(), switches.size()); + } + return static_cast(status); +} + +int32_t CloudServiceProxy::DisableCloud(const std::string &id) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_DISABLE_CLOUD, reply, id); + if (status != SUCCESS) { + ZLOGE("status:0x%{public}x id:%{public}.6s", status, id.c_str()); + } + return static_cast(status); +} + +int32_t CloudServiceProxy::ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CHANGE_APP_SWITCH, reply, id, bundleName, appSwitch); + if (status != SUCCESS) { + ZLOGE("status:0x%{public}x id:%{public}.6s bundleName:%{public}s switch:%{public}d", + status, id.c_str(), bundleName.c_str(), appSwitch); + } + return static_cast(status); +} + +int32_t CloudServiceProxy::Clean(const std::string &id, const std::map &actions) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_CLEAN, reply, id, actions); + if (status != SUCCESS) { + ZLOGE("status:0x%{public}x id:%{public}.6s size:%{public}zu", status, id.c_str(), actions.size()); + } + return static_cast(status); + +} + +int32_t CloudServiceProxy::NotifyDataChange(const std::string &id, const std::string &bundleName) +{ + MessageParcel reply; + int32_t status = IPC_SEND(TRANS_NOTIFY_DATA_CHANGE, reply, id, bundleName); + if (status != SUCCESS) { + ZLOGE("status:0x%{public}x id:%{public}.6s bundleName:%{public}s", status, id.c_str(), bundleName.c_str()); + } + return static_cast(status); +} +} // namespace OHOS::CloudData \ No newline at end of file diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_manager.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_manager.h new file mode 100644 index 00000000..b17c49de --- /dev/null +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_manager.h @@ -0,0 +1,33 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_H +#define OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_H + +#include +#include +#include "cloud_service.h" +#include "rdb_visibility.h" + +namespace OHOS::CloudData { +class RDB_API_EXPORT CloudManager final { +public: + /** + * @brief Obtain cloud service. + */ + static std::shared_ptr GetCloudService(); +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_CLOUD_CLOUD_MANAGER_H diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h index 260c1a06..c3040ff7 100644 --- a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h @@ -44,6 +44,14 @@ public: SWITCH_OFF }; + enum Status : int32_t + { + SUCCESS = 0, + ERROR, + IPC_ERROR, + IPC_PARCEL_ERROR + }; + static constexpr const char *SERVICE_NAME = "cloud"; virtual ~CloudService() = default; virtual int32_t EnableCloud(const std::string &id, const std::map &switches) = 0; -- Gitee From 47e04598261b20547937052dd7babb7cf8624793 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Thu, 6 Apr 2023 17:02:22 +0800 Subject: [PATCH 2/9] add service Signed-off-by: zuojiangjiang --- .../framework/cloud/cloud_info.cpp | 7 +- .../framework/include/cloud/cloud_info.h | 6 +- .../service/cloud/cloud_service_impl.cpp | 86 ++++++++++++++++++- .../service/cloud/cloud_service_impl.h | 8 ++ .../cloud_data/include/cloud_service.h | 1 + 5 files changed, 102 insertions(+), 6 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index d8080306..6c64bfac 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -73,7 +73,12 @@ std::vector CloudInfo::GetSchemaKey() const return keys; } -std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list &fields) const +std::string CloudInfo::GetPrefix(const std::initializer_list &fields) +{ + return GetKey(INFO_PREFIX, fields).append(Constant::KEY_SEPARATOR); +} + +std::string CloudInfo::GetKey(const std::string &prefix, const std::initializer_list &fields) { return Constant::Join(prefix, Constant::KEY_SEPARATOR, fields); } diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index b9bee281..3b4e55fb 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -29,7 +29,7 @@ public: bool Unmarshal(const json &node) override; }; int32_t user = 0; - std::string id; + std::string id = ""; uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; @@ -37,7 +37,7 @@ public: std::string GetKey() const; std::vector GetSchemaKey() const; - std::string GetKey(const std::string &prefix, const std::initializer_list &fields) const; + static std::string GetPrefix(const std::initializer_list &fields); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; @@ -45,6 +45,8 @@ public: private: static constexpr const char *INFO_PREFIX = "CLOUD_INFO"; static constexpr const char *SCHEMA_PREFIX = "CLOUD_SCHEMA"; + + static std::string GetKey(const std::string &prefix, const std::initializer_list &fields); }; } #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_INFO_H diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index b78e07b7..85c1f4cd 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -13,11 +13,21 @@ * limitations under the License. */ +#define LOG_TAG "CloudServiceImpl" + #include "cloud_service_impl.h" +#include "account/account_delegate.h" +#include "checker/checker_manager.h" #include "feature/feature_system.h" +#include "ipc_skeleton.h" +#include "kvstore_utils.h" +#include "log_print.h" +#include "metadata/meta_data_manager.h" + namespace OHOS::CloudData { using namespace DistributedData; +using namespace DistributedKv; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; CloudServiceImpl::Factory::Factory() { FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { @@ -31,17 +41,65 @@ CloudServiceImpl::Factory::~Factory() {} int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { - return 0; + std::lock_guard lg(mutex_); + CloudInfo cloudInfo; + if (GetCloudInfo(id, cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + cloudInfo.enableCloud = true; + for (const auto &item : switches) { + std::string appId = GetAppId(item.first); + for (auto &app : cloudInfo.apps) { + if (app.appId == appId) { + app.cloudSwitch = item.second; + break; + } + } + } + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; } int32_t CloudServiceImpl::DisableCloud(const std::string &id) { - return 0; + std::lock_guard lg(mutex_); + CloudInfo cloudInfo; + if (GetCloudInfo(id, cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + cloudInfo.enableCloud = false; + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; } int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) { - return 0; + std::lock_guard lg(mutex_); + CloudInfo cloudInfo; + if (GetCloudInfo(id, cloudInfo) != SUCCESS) { + return INVALID_ARGUMENT; + } + bool exist = false; + std::string appId = GetAppId(bundleName); + for (auto &app : cloudInfo.apps) { + if (app.appId == appId) { + app.cloudSwitch = appSwitch; + exist = true; + break; + } + } + if (!exist) { + ZLOGE("bundleName:%{public}s", bundleName.c_str()); + return INVALID_ARGUMENT; + } + if (!MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true)) { + return ERROR; + } + return SUCCESS; } int32_t CloudServiceImpl::Clean(const std::string &id, const std::map &actions) @@ -53,4 +111,26 @@ int32_t CloudServiceImpl::NotifyDataChange(const std::string &id, const std::str { return 0; } + +int32_t CloudServiceImpl::GetCloudInfo(const std::string &id, CloudInfo &cloudInfo) +{ + cloudInfo.id = id; + uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); + cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + ZLOGE("invalid argument id:%{public}s, user:%{public}d", + KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user); + return ERROR; + } + return SUCCESS; +} + +std::string CloudServiceImpl::GetAppId(const std::string &bundleName) +{ + CheckerManager::StoreInfo storeInfo; + storeInfo.uid = IPCSkeleton::GetCallingUid(); + storeInfo.tokenId = IPCSkeleton::GetCallingTokenID(); + storeInfo.bundleName = bundleName; + return CheckerManager::GetInstance().GetAppId(storeInfo); +} } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 203bcc45..46e52efd 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -15,7 +15,11 @@ #ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H #define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SERVICE_IMPL_H + +#include #include "cloud_service_stub.h" +#include "cloud/cloud_info.h" + namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: @@ -34,6 +38,10 @@ private: std::shared_ptr product_; }; static Factory factory_; + + int32_t GetCloudInfo(const std::string &id, DistributedData::CloudInfo &cloudInfo); + std::string GetAppId(const std::string &bundleName); + std::mutex mutex_; }; } // namespace OHOS::DistributedData diff --git a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h index c3040ff7..c7016761 100644 --- a/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h +++ b/relational_store/interfaces/inner_api/cloud_data/include/cloud_service.h @@ -48,6 +48,7 @@ public: { SUCCESS = 0, ERROR, + INVALID_ARGUMENT, IPC_ERROR, IPC_PARCEL_ERROR }; -- Gitee From 7fee9b9913a76bbfea99ae1cadf19e78eaaef9dc Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 7 Apr 2023 10:41:09 +0800 Subject: [PATCH 3/9] fix code Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_event.cpp | 16 +++++++++++ .../service/cloud/cloud_event.h | 27 ++++++++++++++++++ .../service/cloud/cloud_service_impl.cpp | 4 +++ .../service/cloud/cloud_syncer.cpp | 27 ++++++++++++++++++ .../service/cloud/cloud_syncer.h | 28 +++++++++++++++++++ .../service/rdb/rdb_service_impl.cpp | 5 ++++ .../service/rdb/rdb_service_impl.h | 2 ++ 7 files changed, 109 insertions(+) create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp new file mode 100644 index 00000000..7900bb94 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp @@ -0,0 +1,16 @@ +/* + * Copyright (c) 2023 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 "cloud_event.h" diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h new file mode 100644 index 00000000..e708b6db --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h @@ -0,0 +1,27 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H + +#include "eventcenter/event.h" + +namespace OHOS::CloudData { +class CloudEvent : public DistributedData::Event { +public: + +}; +} // namespace OHOS::CloudData +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 85c1f4cd..754ff714 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -19,6 +19,7 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" +#include "cloud_syncer.h" #include "feature/feature_system.h" #include "ipc_skeleton.h" #include "kvstore_utils.h" @@ -59,6 +60,7 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map Date: Fri, 7 Apr 2023 15:35:54 +0800 Subject: [PATCH 4/9] fix code Signed-off-by: zuojiangjiang --- .../distributeddataservice/service/cloud/cloud_event.cpp | 6 ++++++ .../distributeddataservice/service/cloud/cloud_event.h | 7 +++++++ .../distributeddataservice/service/cloud/cloud_syncer.cpp | 2 +- .../distributeddataservice/service/cloud/cloud_syncer.h | 4 +++- .../service/rdb/rdb_service_impl.cpp | 1 + 5 files changed, 18 insertions(+), 2 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp index 7900bb94..732d6925 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp @@ -14,3 +14,9 @@ */ #include "cloud_event.h" + +namespace OHOS::CloudData { +CloudEvent::CloudEvent(int32_t evtId) : DistributedData::Event(evtId) +{ +} +} diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h index e708b6db..1e5f9cfc 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h @@ -21,7 +21,14 @@ namespace OHOS::CloudData { class CloudEvent : public DistributedData::Event { public: + enum : int32_t { + CLOUD_FEATURE_INIT, + CLOUD_RDB_CREATE, + CLOUD_BUTT + }; + CloudEvent(int32_t evtId); + ~CloudEvent() = default; }; } // namespace OHOS::CloudData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp index ebd01243..fded747d 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp @@ -21,7 +21,7 @@ CloudSyncer &CloudSyncer::GetInstance() static CloudSyncer instance; return instance; } -void CloudSyncer::Sync(const DistributedData::CloudInfo cloudInfo) +void CloudSyncer::Sync(const DistributedData::CloudInfo &cloudInfo) { } } // namespace OHOS::CloudData \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h index 519a7158..c9dd7971 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h @@ -22,7 +22,9 @@ namespace OHOS::CloudData { class CloudSyncer { public: static CloudSyncer &GetInstance(); - void Sync(const DistributedData::CloudInfo cloudInfo); + void Sync(const DistributedData::CloudInfo &cloudInfo); + +private: }; } // namespace OHOS::CloudData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 109463e9..6b7d1228 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -450,5 +450,6 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { // feature start + return 0; } } // namespace OHOS::DistributedRdb -- Gitee From 4e5e105bdb844d2531f44238d7a21d7f46108392 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Fri, 7 Apr 2023 18:14:13 +0800 Subject: [PATCH 5/9] fix code Signed-off-by: zuojiangjiang --- .../distributeddataservice/service/cloud/cloud_event.h | 6 ------ .../service/cloud/cloud_service_impl.cpp | 2 +- 2 files changed, 1 insertion(+), 7 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h index 1e5f9cfc..fa747dea 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h @@ -21,12 +21,6 @@ namespace OHOS::CloudData { class CloudEvent : public DistributedData::Event { public: - enum : int32_t { - CLOUD_FEATURE_INIT, - CLOUD_RDB_CREATE, - CLOUD_BUTT - }; - CloudEvent(int32_t evtId); ~CloudEvent() = default; }; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 754ff714..bfcd3204 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -120,7 +120,7 @@ int32_t CloudServiceImpl::GetCloudInfo(const std::string &id, CloudInfo &cloudIn { cloudInfo.id = id; uint32_t tokenId = IPCSkeleton::GetCallingTokenID(); - cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); // account? if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { ZLOGE("invalid argument id:%{public}s, user:%{public}d", KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user); -- Gitee From 724edb6f2bbb9b644535cf399882168a598de002 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 10 Apr 2023 09:51:47 +0800 Subject: [PATCH 6/9] fix code Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_event.cpp | 22 --------------- .../service/cloud/cloud_event.h | 28 ------------------- 2 files changed, 50 deletions(-) delete mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp delete mode 100644 datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp deleted file mode 100644 index 732d6925..00000000 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.cpp +++ /dev/null @@ -1,22 +0,0 @@ -/* - * Copyright (c) 2023 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 "cloud_event.h" - -namespace OHOS::CloudData { -CloudEvent::CloudEvent(int32_t evtId) : DistributedData::Event(evtId) -{ -} -} diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h deleted file mode 100644 index fa747dea..00000000 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_event.h +++ /dev/null @@ -1,28 +0,0 @@ -/* - * Copyright (c) 2023 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H -#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H - -#include "eventcenter/event.h" - -namespace OHOS::CloudData { -class CloudEvent : public DistributedData::Event { -public: - CloudEvent(int32_t evtId); - ~CloudEvent() = default; -}; -} // namespace OHOS::CloudData -#endif // OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_EVENT_H -- Gitee From 4d50d2d3ec75a6f8d8bf51b86575601adf94b6aa Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 10 Apr 2023 14:37:21 +0800 Subject: [PATCH 7/9] fix code Signed-off-by: zuojiangjiang --- .../framework/cloud/cloud_info.cpp | 4 +-- .../framework/include/cloud/cloud_info.h | 1 + .../service/cloud/cloud_service_impl.cpp | 6 ++-- .../service/cloud/cloud_syncer.cpp | 1 + .../service/rdb/rdb_event.cpp | 22 +++++++++++++++ .../service/rdb/rdb_event.h | 28 +++++++++++++++++++ .../service/rdb/rdb_service_impl.cpp | 1 - 7 files changed, 56 insertions(+), 7 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp create mode 100644 datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index 6c64bfac..294d0d38 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -59,7 +59,7 @@ bool CloudInfo::AppInfo::Unmarshal(const Serializable::json &node) std::string CloudInfo::GetKey() const { - return GetKey(INFO_PREFIX, { id, std::to_string(user) }); + return GetKey(INFO_PREFIX, { id, std::to_string(user), account }); } std::vector CloudInfo::GetSchemaKey() const @@ -67,7 +67,7 @@ std::vector CloudInfo::GetSchemaKey() const std::vector keys; keys.reserve(apps.size()); for (const auto &app : apps) { - const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), app.bundleName }); + const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), account, app.bundleName }); keys.push_back(key); } return keys; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index 3b4e55fb..0e2fc18c 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -30,6 +30,7 @@ public: }; int32_t user = 0; std::string id = ""; + std::string account = ""; uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index bfcd3204..5cbb2af9 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -60,7 +60,6 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::mapGetUserByToken(tokenId); // account? + cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); + cloudInfo.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { ZLOGE("invalid argument id:%{public}s, user:%{public}d", KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user); diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp index fded747d..f920c5ec 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp @@ -21,6 +21,7 @@ CloudSyncer &CloudSyncer::GetInstance() static CloudSyncer instance; return instance; } + void CloudSyncer::Sync(const DistributedData::CloudInfo &cloudInfo) { } diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp new file mode 100644 index 00000000..043c6a4c --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp @@ -0,0 +1,22 @@ +/* + * Copyright (c) 2023 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_event.h" + +namespace OHOS::DistributedRdb { +RdbEvent::RdbEvent(int32_t evtId) : Event(evtId) +{ +} +} \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h new file mode 100644 index 00000000..39c438ba --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h @@ -0,0 +1,28 @@ +/* + * Copyright (c) 2023 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H + +#include "eventcenter/event.h" + +namespace OHOS::DistributedRdb { +class RdbEvent : public DistributedData::Event { +public: + RdbEvent(int32_t evtId); + ~RdbEvent() = default; +}; +} // namespace OHOS::DistributedRdb +#endif // OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index 6b7d1228..ce1e6e78 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -449,7 +449,6 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - // feature start return 0; } } // namespace OHOS::DistributedRdb -- Gitee From 14cdd217d76e139d2343f3cd4f545273d6c26903 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 10 Apr 2023 17:04:24 +0800 Subject: [PATCH 8/9] update Signed-off-by: zuojiangjiang --- .../framework/cloud/cloud_info.cpp | 7 ++- .../framework/include/cloud/cloud_info.h | 2 +- .../framework/include/eventcenter/event.h | 3 +- .../service/cloud/cloud_service_impl.cpp | 43 ++++++++++++++++++- .../service/cloud/cloud_service_impl.h | 3 ++ .../service/rdb/rdb_event.cpp | 8 +++- .../service/rdb/rdb_event.h | 12 +++++- .../service/rdb/rdb_service_impl.cpp | 6 ++- 8 files changed, 73 insertions(+), 11 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index 294d0d38..aff70ea7 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp @@ -62,13 +62,12 @@ std::string CloudInfo::GetKey() const return GetKey(INFO_PREFIX, { id, std::to_string(user), account }); } -std::vector CloudInfo::GetSchemaKey() const +std::map CloudInfo::GetSchemaKey() const { - std::vector keys; - keys.reserve(apps.size()); + std::map keys; for (const auto &app : apps) { const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), account, app.bundleName }); - keys.push_back(key); + keys.insert_or_assign(app.bundleName, key); } return keys; } diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h index 0e2fc18c..9148e8b2 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_info.h @@ -37,7 +37,7 @@ public: std::vector apps; std::string GetKey() const; - std::vector GetSchemaKey() const; + std::map GetSchemaKey() const; static std::string GetPrefix(const std::initializer_list &fields); bool Marshal(json &node) const override; diff --git a/datamgr_service/services/distributeddataservice/framework/include/eventcenter/event.h b/datamgr_service/services/distributeddataservice/framework/include/eventcenter/event.h index d574e0f5..5ee3a433 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/eventcenter/event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/eventcenter/event.h @@ -27,7 +27,8 @@ public: EVT_INVALID, EVT_INITED, EVT_UPDATE, - EVT_CUSTOM = 0x1000 + EVT_CUSTOM = 0x1000, + EVT_RDB = 0x2000 }; API_EXPORT Event(int32_t evtId); API_EXPORT Event(Event &&) noexcept = delete; diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 5cbb2af9..09898a67 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -20,15 +20,19 @@ #include "account/account_delegate.h" #include "checker/checker_manager.h" #include "cloud_syncer.h" +#include "cloud/cloud_server.h" +#include "eventcenter/event_center.h" #include "feature/feature_system.h" #include "ipc_skeleton.h" #include "kvstore_utils.h" #include "log_print.h" #include "metadata/meta_data_manager.h" +#include "rdb_event.h" namespace OHOS::CloudData { using namespace DistributedData; using namespace DistributedKv; +using namespace DistributedRdb; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; CloudServiceImpl::Factory::Factory() { FeatureSystem::GetInstance().RegisterCreator(CloudServiceImpl::SERVICE_NAME, [this]() { @@ -38,8 +42,23 @@ CloudServiceImpl::Factory::Factory() { return product_; }); } + CloudServiceImpl::Factory::~Factory() {} +CloudServiceImpl::CloudServiceImpl() +{ + EventCenter::GetInstance().Subscribe(RdbEvent::RDB_FEATURE_INIT, [this](const Event &event) { + auto &rdbEvent = static_cast(event); + auto user = AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); + auto cloudInfo = CloudServer::GetInstance()->GetServerInfo(user); + if (cloudInfo.id.empty()) { + return; + } + UpdateCloudInfo(cloudInfo); + UpdateSchema(cloudInfo); + }); +} + int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { std::lock_guard lg(mutex_); @@ -120,8 +139,9 @@ int32_t CloudServiceImpl::GetCloudInfo(const std::string &id, CloudInfo &cloudIn cloudInfo.user = AccountDelegate::GetInstance()->GetUserByToken(tokenId); cloudInfo.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { - ZLOGE("invalid argument id:%{public}s, user:%{public}d", - KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user); + ZLOGE("invalid argument id:%{public}s, user:%{public}d, account:%{public}s", + KvStoreUtils::ToBeAnonymous(cloudInfo.id).c_str(), cloudInfo.user, + KvStoreUtils::ToBeAnonymous(cloudInfo.account).c_str()); return ERROR; } return SUCCESS; @@ -135,4 +155,23 @@ std::string CloudServiceImpl::GetAppId(const std::string &bundleName) storeInfo.bundleName = bundleName; return CheckerManager::GetInstance().GetAppId(storeInfo); } + +void CloudServiceImpl::UpdateCloudInfo(DistributedData::CloudInfo &cloudInfo) +{ + CloudInfo oldInfo; + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), oldInfo, true)) { + MetaDataManager::GetInstance().SaveMeta(cloudInfo.GetKey(), cloudInfo, true); + return; + } + oldInfo.totalSpace = cloudInfo.totalSpace; + oldInfo.remainSpace = cloudInfo.remainSpace; + cloudInfo = oldInfo; + MetaDataManager::GetInstance().SaveMeta(oldInfo.GetKey(), oldInfo, true); +} + +void CloudServiceImpl::UpdateSchema(DistributedData::CloudInfo &cloudInfo) +{ + auto keys = cloudInfo.GetSchemaKey(); + for (const auto) +} } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h index 46e52efd..d2dec093 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -23,6 +23,7 @@ namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: + CloudServiceImpl(); ~CloudServiceImpl() = default; int32_t EnableCloud(const std::string &id, const std::map &switches) override; int32_t DisableCloud(const std::string &id) override; @@ -39,6 +40,8 @@ private: }; static Factory factory_; + void UpdateCloudInfo(DistributedData::CloudInfo &cloudInfo); + void UpdateSchema(DistributedData::CloudInfo &cloudInfo); int32_t GetCloudInfo(const std::string &id, DistributedData::CloudInfo &cloudInfo); std::string GetAppId(const std::string &bundleName); std::mutex mutex_; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp index 043c6a4c..7e8020d8 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp @@ -16,7 +16,13 @@ #include "rdb_event.h" namespace OHOS::DistributedRdb { -RdbEvent::RdbEvent(int32_t evtId) : Event(evtId) +RdbEvent::RdbEvent(int32_t evtId, uint32_t tokenId) + : Event(evtId), tokenId_(tokenId) { } + +uint32_t GetTokenId() +{ + return tokenId_; +} } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h index 39c438ba..a70525a1 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h @@ -21,8 +21,18 @@ namespace OHOS::DistributedRdb { class RdbEvent : public DistributedData::Event { public: - RdbEvent(int32_t evtId); + enum : int32_t { + RDB_FEATURE_INIT = EVT_RDB, + RDB_CREATE, + RDB_BUTT + }; + + RdbEvent(int32_t evtId, uint32_t tokenId); ~RdbEvent() = default; + uint32_t GetTokenId() const; + +private: + uint32_t tokenId_; }; } // namespace OHOS::DistributedRdb #endif // OHOS_DISTRIBUTED_DATA_SERVICES_RDB_RDB_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index ce1e6e78..c9c47d51 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -19,11 +19,13 @@ #include "checker/checker_manager.h" #include "communicator/device_manager_adapter.h" #include "crypto_manager.h" +#include "eventcenter/event_center.h" #include "ipc_skeleton.h" #include "log_print.h" #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permission/permission_validator.h" +#include "rdb_event.h" #include "rdb_notifier_proxy.h" #include "types_export.h" #include "utils/anonymous.h" @@ -449,6 +451,8 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - return 0; + EventCenter::Defer defer; + auto initEvt = std::make_unique(RdbEvent::RDB_FEATURE_INIT); + EventCenter::GetInstance().PostEvent(std::move(initEvt)); } } // namespace OHOS::DistributedRdb -- Gitee From b6961b6d3b2136ca12752b0333d67d3232ac04d3 Mon Sep 17 00:00:00 2001 From: zuojiangjiang Date: Mon, 10 Apr 2023 20:13:12 +0800 Subject: [PATCH 9/9] update Signed-off-by: zuojiangjiang --- .../service/cloud/cloud_service_impl.cpp | 3 +-- .../services/distributeddataservice/service/rdb/rdb_event.cpp | 4 ++-- .../distributeddataservice/service/rdb/rdb_service_impl.cpp | 4 +++- 3 files changed, 6 insertions(+), 5 deletions(-) diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp index 09898a67..81b3ebde 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -52,6 +52,7 @@ CloudServiceImpl::CloudServiceImpl() auto user = AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); auto cloudInfo = CloudServer::GetInstance()->GetServerInfo(user); if (cloudInfo.id.empty()) { + ZLOGI("no cloud server"); return; } UpdateCloudInfo(cloudInfo); @@ -171,7 +172,5 @@ void CloudServiceImpl::UpdateCloudInfo(DistributedData::CloudInfo &cloudInfo) void CloudServiceImpl::UpdateSchema(DistributedData::CloudInfo &cloudInfo) { - auto keys = cloudInfo.GetSchemaKey(); - for (const auto) } } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp index 7e8020d8..e968beb2 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp @@ -17,11 +17,11 @@ namespace OHOS::DistributedRdb { RdbEvent::RdbEvent(int32_t evtId, uint32_t tokenId) - : Event(evtId), tokenId_(tokenId) + : DistributedData::Event(evtId), tokenId_(tokenId) { } -uint32_t GetTokenId() +uint32_t RdbEvent::GetTokenId() const { return tokenId_; } diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp index c9c47d51..a19ad18b 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -452,7 +452,9 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { EventCenter::Defer defer; - auto initEvt = std::make_unique(RdbEvent::RDB_FEATURE_INIT); + auto tokenId = IPCSkeleton::GetCallingTokenID(); + auto initEvt = std::make_unique(RdbEvent::RDB_FEATURE_INIT, tokenId); EventCenter::GetInstance().PostEvent(std::move(initEvt)); + return RDB_OK; } } // namespace OHOS::DistributedRdb -- Gitee