diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_info.cpp index 17d7b1f1cd6d923facb47b893a742f296c5f43f7..aff70ea74eab98065f19343ec6e82520109788d2 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,26 @@ 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), account }); } std::map CloudInfo::GetSchemaKey() const { - for (auto &app : apps) { - + std::map keys; + for (const auto &app : apps) { + const auto key = GetKey(SCHEMA_PREFIX, { id, std::to_string(user), account, app.bundleName }); + keys.insert_or_assign(app.bundleName, key); } - return std::map(); + return keys; +} + +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); } } // 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 22f37cbb393b83d09b943e1583204709c8a4dfee..9148e8b214b05478e76074ae07bfb91a9aefbaa2 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,8 @@ public: bool Unmarshal(const json &node) override; }; int32_t user = 0; - std::string id; + std::string id = ""; + std::string account = ""; uint64_t totalSpace = 0; uint64_t remainSpace = 0; bool enableCloud = false; @@ -37,6 +38,7 @@ public: std::string GetKey() const; std::map GetSchemaKey() const; + static std::string GetPrefix(const std::initializer_list &fields); bool Marshal(json &node) const override; bool Unmarshal(const json &node) override; @@ -44,6 +46,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/framework/include/eventcenter/event.h b/datamgr_service/services/distributeddataservice/framework/include/eventcenter/event.h index d574e0f5b6fe55345666cbcc8d36a9f949e1e91e..5ee3a433e38fa5fc2e1aa82d4f6773f3f7407512 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 b78e07b7c8567bd8a1400fc55dc5d6fe69e0ad4e..81b3ebdec861bb387f946b69b15ec06a902eaee0 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,26 @@ * limitations under the License. */ +#define LOG_TAG "CloudServiceImpl" + #include "cloud_service_impl.h" +#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]() { @@ -27,21 +42,85 @@ 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()) { + ZLOGI("no cloud server"); + return; + } + UpdateCloudInfo(cloudInfo); + UpdateSchema(cloudInfo); + }); +} + 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 +132,45 @@ 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); + cloudInfo.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); + if (!MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetKey(), cloudInfo, true)) { + 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; +} + +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); +} + +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) +{ +} } \ 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 203bcc45f5839afc2ac11106e771cdad4e9a6388..d2dec093c14e2089ad2367c13936637a1dcaa7cc 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -15,10 +15,15 @@ #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: + CloudServiceImpl(); ~CloudServiceImpl() = default; int32_t EnableCloud(const std::string &id, const std::map &switches) override; int32_t DisableCloud(const std::string &id) override; @@ -34,6 +39,12 @@ private: std::shared_ptr product_; }; 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_; }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp new file mode 100644 index 0000000000000000000000000000000000000000..f920c5ec5798f8535de32db03ac6551ce898e4d0 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.cpp @@ -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. + */ + +#include "cloud_syncer.h" + +namespace OHOS::CloudData { +CloudSyncer &CloudSyncer::GetInstance() +{ + static CloudSyncer instance; + return instance; +} + +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 new file mode 100644 index 0000000000000000000000000000000000000000..c9dd79717460746661490e3cd867af603bff33e9 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_syncer.h @@ -0,0 +1,30 @@ +/* + * 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_SYNCER_H +#define OHOS_DISTRIBUTED_DATA_SERVICES_CLOUD_CLOUD_SYNCER_H + +#include "cloud/cloud_info.h" + +namespace OHOS::CloudData { +class CloudSyncer { +public: + static CloudSyncer &GetInstance(); + 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_event.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp new file mode 100644 index 0000000000000000000000000000000000000000..e968beb2b29ccd101eebea9af22b8f6df2813b30 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.cpp @@ -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. + */ + +#include "rdb_event.h" + +namespace OHOS::DistributedRdb { +RdbEvent::RdbEvent(int32_t evtId, uint32_t tokenId) + : DistributedData::Event(evtId), tokenId_(tokenId) +{ +} + +uint32_t RdbEvent::GetTokenId() const +{ + 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 new file mode 100644 index 0000000000000000000000000000000000000000..a70525a1ceb6db4c6836d22f79b6201210ceb5a5 --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_event.h @@ -0,0 +1,38 @@ +/* + * 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: + 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 6f7eb43c7717c6889443758082ada79d65d409ed..a19ad18b3680ac2f692ac472c602a28ff50c217d 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" @@ -446,4 +448,13 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) delete syncer; return RDB_OK; } + +int32_t RdbServiceImpl::OnInitialize() +{ + EventCenter::Defer defer; + 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 diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h index 35f93b7c10ff0be9af32c292b39201bd17f49b0c..3d2324bd92b5a19179f3ac629381cae03a6aa12b 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -56,6 +56,8 @@ public: int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; + int32_t OnInitialize() override; + protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, const RdbPredicates& predicates, SyncResult& result) override; diff --git a/relational_store/CMakeLists.txt b/relational_store/CMakeLists.txt index cbece15b8e87ef350dbe5e600dbbdbb9c0320556..1fa455081104e74949f108ecb619387fced5a2b1 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) @@ -20,11 +21,13 @@ aux_source_directory(${CMAKE_CURRENT_SOURCE_DIR}/frameworks/native/rdb_device_ma 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 0000000000000000000000000000000000000000..2c8501eec6fb07a32c0c0225785ba15b748893a3 --- /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 0000000000000000000000000000000000000000..8ece3de8c0c19407ea4179f8f784a2ec9046063d --- /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 0000000000000000000000000000000000000000..a21def4f50c48b9c94c8e38e23636e4a54f6231b --- /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 0000000000000000000000000000000000000000..c9a921c9b9718d721e7ea3e6e3dd0b483a6ddbbd --- /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 0000000000000000000000000000000000000000..f74edb1ddbb28f39f4c1a17855391c088e666bbf --- /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 0000000000000000000000000000000000000000..fd2955952e297bd114c74381c714c4dd3d4e1650 --- /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/frameworks/native/rdb/src/asset_value.cpp b/relational_store/frameworks/native/rdb/src/asset_value.cpp index 43ebaa059003dcf58cb68108d5a06e0d41fca313..6b8c7ae5e37f329055d52c91f8fb9fd85d6b2e5d 100644 --- a/relational_store/frameworks/native/rdb/src/asset_value.cpp +++ b/relational_store/frameworks/native/rdb/src/asset_value.cpp @@ -13,4 +13,4 @@ * limitations under the License. */ -#include "asset_value.h" +#include "asset_value.h" \ 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 0000000000000000000000000000000000000000..b17c49de7039aebee3d085ee18e9b44f93a0a51e --- /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 260c1a06acc35814f3a497eaa4ef6f77d1e44c55..c701676106eb8c8f63c1ee395671935914f63e9c 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,15 @@ public: SWITCH_OFF }; + enum Status : int32_t + { + SUCCESS = 0, + ERROR, + INVALID_ARGUMENT, + 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;