From bc784b45f145568aa83c117039e5603a236ebe5f Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 15:33:03 +0800 Subject: [PATCH 1/2] update Signed-off-by: htt1997 --- datamgr_service/datamgr_service.gni | 16 ++ .../app/src/feature_stub_impl.cpp | 8 + .../app/src/feature_stub_impl.h | 1 + .../app/src/kvstore_data_service.cpp | 10 + .../app/src/kvstore_data_service.h | 2 + .../app/src/uninstaller/uninstaller_impl.cpp | 60 ++++-- .../app/src/uninstaller/uninstaller_impl.h | 7 +- .../framework/cloud/cloud_event.cpp | 40 +--- .../framework/feature/feature_system.cpp | 6 + .../framework/include/cloud/cloud_event.h | 26 ++- .../include/feature/feature_system.h | 1 + .../framework/include/store/auto_cache.h | 4 +- .../framework/include/store/general_store.h | 3 + .../framework/store/auto_cache.cpp | 26 +-- .../distributeddataservice/service/BUILD.gn | 6 +- .../service/cloud/cloud_service_impl.cpp | 76 +++++--- .../service/cloud/cloud_service_impl.h | 8 +- .../service/kvdb/kvdb_service_impl.cpp | 2 +- .../service/kvdb/kvdb_service_impl.h | 2 +- .../service/rdb/rdb_general_store.cpp | 6 + .../service/rdb/rdb_general_store.h | 10 +- .../service/rdb/rdb_service_impl.cpp | 91 +++++++-- .../service/rdb/rdb_service_impl.h | 8 +- .../service/rdb/rdb_service_stub.cpp | 21 +- .../service/test/BUILD.gn | 30 +++ .../service/test/cloud_data_test.cpp | 182 ++++++++++++++++++ .../service/test/mock/db_store_mock.cpp | 10 +- mock/src/mock_notification.cpp | 1 + relational_store/bundle.json | 12 +- .../frameworks/js/napi/cloud_data/BUILD.gn | 63 ++++++ .../cloud_data/include/cloud_service_proxy.h | 2 +- .../native/rdb/include/rdb_manager_impl.h | 2 +- .../native/rdb/include/rdb_service_proxy.h | 7 +- .../native/rdb/include/rdb_types_util.h | 5 - .../native/rdb/src/rdb_manager_impl.cpp | 8 +- .../native/rdb/src/rdb_service_proxy.cpp | 31 +-- .../native/rdb/src/rdb_store_impl.cpp | 16 +- .../native/rdb/src/rdb_types_util.cpp | 11 -- .../interfaces/inner_api/cloud_data/BUILD.gn | 6 +- .../inner_api/rdb/include/rdb_service.h | 5 +- .../inner_api/rdb/include/rdb_types.h | 7 - relational_store/relational_store.gni | 3 +- 42 files changed, 604 insertions(+), 237 deletions(-) create mode 100644 datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp diff --git a/datamgr_service/datamgr_service.gni b/datamgr_service/datamgr_service.gni index d1abd9af..c3fa0d49 100644 --- a/datamgr_service/datamgr_service.gni +++ b/datamgr_service/datamgr_service.gni @@ -11,6 +11,22 @@ # See the License for the specific language governing permissions and # limitations under the License. +distributedfilejs_path = "//foundation/distributeddatamgr/distributedfile" + +kv_store_path = "//foundation/distributeddatamgr/kv_store" + +kv_store_common_path = "${kv_store_path}/frameworks/common" + +kv_store_distributeddb_path = "${kv_store_path}/frameworks/libs/distributeddb" + +dsoftbus_core_path = "//foundation/communication/dsoftbus/core/common/include" + +datashare_path = "//foundation/distributeddatamgr/data_share" + +ipc_core_path = "//foundation/communication/ipc/interfaces/innerkits/ipc_core" + +device_manager_path = "//foundation/distributedhardware/device_manager" + declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || diff --git a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp index 16876215..cbca54b0 100644 --- a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.cpp @@ -57,6 +57,14 @@ int32_t FeatureStubImpl::OnAppUninstall(const std::string &bundleName, int32_t u return featureImpl_->OnAppUninstall(bundleName, user, index, tokenId); } +int32_t FeatureStubImpl::OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) +{ + if (featureImpl_ == nullptr) { + return -1; + } + return featureImpl_->OnAppUpdate(bundleName, user, index, tokenId); +} + int32_t FeatureStubImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { if (featureImpl_ == nullptr) { diff --git a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.h b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.h index 6fc9c824..b00646bc 100644 --- a/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.h +++ b/datamgr_service/services/distributeddataservice/app/src/feature_stub_impl.h @@ -33,6 +33,7 @@ public: int32_t OnInitialize(std::shared_ptr executor); int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account); int32_t Online(const std::string &device); diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp index ee5ef9e7..2e190156 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.cpp @@ -716,4 +716,14 @@ int32_t KvStoreDataService::OnUninstall(const std::string &bundleName, int32_t u }); return 0; } + +int32_t KvStoreDataService::OnUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId) +{ + features_.ForEachCopies( + [bundleName, user, index, tokenId](const auto &, sptr &value) { + value->OnAppUpdate(bundleName, user, index, tokenId); + return false; + }); + return 0; +} } // namespace OHOS::DistributedKv diff --git a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h index fae1dbac..60aac352 100644 --- a/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h +++ b/datamgr_service/services/distributeddataservice/app/src/kvstore_data_service.h @@ -76,6 +76,8 @@ public: int32_t OnUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + int32_t OnUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + private: void NotifyAccountEvent(const AccountEventInfo &eventInfo); class KvStoreClientDeathObserverImpl { diff --git a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp index 81072e46..65ff559f 100644 --- a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp +++ b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.cpp @@ -27,6 +27,7 @@ #include "metadata/meta_data_manager.h" #include "metadata/store_meta_data.h" #include "permit_delegate.h" +#include "cloud/cloud_info.h" #include "utils/block_integer.h" namespace OHOS::DistributedKv { @@ -36,26 +37,28 @@ using namespace OHOS::AppExecFwk; using namespace OHOS::DistributedData; using namespace OHOS::EventFwk; -UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info, - UninstallEventCallback callback) - : CommonEventSubscriber(info), callback_(callback) -{} +UninstallEventSubscriber::UninstallEventSubscriber(const CommonEventSubscribeInfo &info) : CommonEventSubscriber(info) +{ +} void UninstallEventSubscriber::OnReceiveEvent(const CommonEventData &event) { ZLOGI("Intent Action Rec"); Want want = event.GetWant(); std::string action = want.GetAction(); - if (action != CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED && - action != OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED) { - return; - } - - std::string bundleName = want.GetElement().GetBundleName(); - int32_t userId = want.GetIntParam(USER_ID, -1); - int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); - ZLOGI("bundleName:%s, user:%d, appIndex:%d", bundleName.c_str(), userId, appIndex); - callback_(bundleName, userId, appIndex); + callbacks_.ComputeIfPresent(action, [&want](const auto& key, auto &callback) { + std::string bundleName = want.GetElement().GetBundleName(); + int32_t userId = want.GetIntParam(USER_ID, -1); + int32_t appIndex = want.GetIntParam(SANDBOX_APP_INDEX, 0); + ZLOGI("bundleName:%s, user:%d, appIndex:%d", bundleName.c_str(), userId, appIndex); + callback(bundleName, userId, appIndex); + return true; + }); +} +int32_t UninstallEventSubscriber::RegisterCallback(const std::string &action, UninstallEventCallback callback) +{ + callbacks_.InsertOrAssign(action, std::move(callback)); + return Status::SUCCESS; } UninstallerImpl::~UninstallerImpl() @@ -84,8 +87,11 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared MatchingSkills matchingSkills; matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED); matchingSkills.AddEvent(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED); + matchingSkills.AddEvent(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED); CommonEventSubscribeInfo info(matchingSkills); - auto callback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + + auto subscriber = std::make_shared(info); + auto removedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { kvStoreDataService->OnUninstall(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); std::string prefix = StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, std::to_string(userId), "default", bundleName }); @@ -102,11 +108,33 @@ Status UninstallerImpl::Init(KvStoreDataService *kvStoreDataService, std::shared MetaDataManager::GetInstance().DelMeta(meta.GetStrategyKey()); MetaDataManager::GetInstance().DelMeta(meta.appId, true); MetaDataManager::GetInstance().DelMeta(meta.GetKeyLocal(), true); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); PermitDelegate::GetInstance().DelCache(meta.GetKey()); } } }; - auto subscriber = std::make_shared(info, callback); + + auto replacedCallback = [kvStoreDataService](const std::string &bundleName, int32_t userId, int32_t appIndex) { + kvStoreDataService->OnUpdate(bundleName, userId, appIndex, IPCSkeleton::GetCallingTokenID()); + std::string prefix = StoreMetaData::GetPrefix({ DeviceManagerAdapter::GetInstance().GetLocalDevice().uuid, + std::to_string(userId), "default", bundleName }); + std::vector storeMetaData; + if (!MetaDataManager::GetInstance().LoadMeta(prefix, storeMetaData)) { + ZLOGE("load meta failed!"); + return; + } + for (auto &meta : storeMetaData) { + if (meta.instanceId == appIndex && !meta.appId.empty() && !meta.storeId.empty()) { + ZLOGI("updated bundleName:%s, stordId:%s", bundleName.c_str(), meta.storeId.c_str()); + MetaDataManager::GetInstance().DelMeta(CloudInfo::GetSchemaKey(meta), true); + } + } + }; + + subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED, removedCallback); + subscriber->RegisterCallback(OHOS::AppExecFwk::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED, removedCallback); + subscriber->RegisterCallback(CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED, replacedCallback); + subscriber_ = subscriber; executors_ = executors; executors_->Execute(GetTask()); diff --git a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h index 50ec5fe3..07a026c8 100644 --- a/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h +++ b/datamgr_service/services/distributeddataservice/app/src/uninstaller/uninstaller_impl.h @@ -25,8 +25,8 @@ using UninstallEventCallback = std::function callbacks_; + }; class UninstallerImpl : public Uninstaller { public: diff --git a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp index 0212fcc7..11f43a39 100644 --- a/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp +++ b/datamgr_service/services/distributeddataservice/framework/cloud/cloud_event.cpp @@ -16,15 +16,8 @@ #include "cloud/cloud_event.h" namespace OHOS::DistributedData { -CloudEvent::CloudEvent(int32_t evtId, uint32_t tokenId, const std::string &storeName, const std::string &bundleName, - const std::string &featureName, const std::string &schemaKey) - : Event(evtId), featureName_(featureName), tokenId_(tokenId), storeName_(storeName), bundleName_(bundleName), - schemaKey_(schemaKey) -{ -} -CloudEvent::CloudEvent(int32_t evtId, const CloudEvent &cloudEvent) - : Event(evtId), featureName_(cloudEvent.GetFeatureName()), tokenId_(cloudEvent.GetTokenId()), - storeName_(cloudEvent.GetStoreName()), bundleName_(cloudEvent.GetBundleName()) +CloudEvent::CloudEvent(int32_t evtId, CloudEvent::StoreInfo storeInfo, const std::string &featureName) + : Event(evtId), featureName_(featureName), storeInfo_(storeInfo) { } @@ -33,33 +26,8 @@ std::string CloudEvent::GetFeatureName() const return featureName_; } -void CloudEvent::SetSchemaKey(std::string schemaKey) -{ - schemaKey_ = schemaKey; -} - -std::string CloudEvent::GetSchemaKey() const -{ - return schemaKey_; -} - -std::string CloudEvent::GetBundleName() const -{ - return bundleName_; -} - -std::string CloudEvent::GetStoreName() const -{ - return storeName_; -} - -bool CloudEvent::Equals(const Event &event) const -{ - return false; -} - -uint32_t CloudEvent::GetTokenId() const +CloudEvent::StoreInfo CloudEvent::GetStoreInfo() const { - return tokenId_; + return storeInfo_; } } \ No newline at end of file diff --git a/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp b/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp index 6bfd3d23..49b69e8c 100644 --- a/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp +++ b/datamgr_service/services/distributeddataservice/framework/feature/feature_system.cpp @@ -76,6 +76,12 @@ int32_t FeatureSystem::Feature::OnAppUninstall(const std::string &bundleName, in return E_OK; } +int32_t FeatureSystem::Feature::OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, + uint32_t tokenId) +{ + return E_OK; +} + int32_t FeatureSystem::Feature::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) { return E_OK; diff --git a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h index d39a8348..e6cff13a 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h +++ b/datamgr_service/services/distributeddataservice/framework/include/cloud/cloud_event.h @@ -28,26 +28,22 @@ public: NEED_CREATE, CLOUD_BUTT }; - CloudEvent(int32_t evtId, uint32_t tokenId = 0, const std::string &storeName = "", - const std::string &bundleName = "", const std::string &featureName = "relational_store", - const std::string &schemaKey = ""); - CloudEvent(int32_t evtId, const CloudEvent &cloudEvent); + + struct StoreInfo { + uint32_t tokenId = 0; + std::string bundleName; + std::string storeName; + int32_t instanceId = 0; + }; + + CloudEvent(int32_t evtId, StoreInfo storeInfo, const std::string &featureName = "relational_store"); ~CloudEvent() = default; std::string GetFeatureName() const; - void SetSchemaKey(std::string bundleName); - std::string GetSchemaKey() const; - std::string GetBundleName() const; - std::string GetStoreName() const; - uint32_t GetTokenId() const; - bool Equals(const DistributedData::Event &event) const override; + StoreInfo GetStoreInfo() const; private: std::string featureName_; - uint32_t tokenId_; - std::string storeName_; - std::string bundleName_; - std::string schemaKey_; - + StoreInfo storeInfo_; }; } // namespace OHOS::DistributedData #endif // OHOS_DISTRIBUTED_DATA_SERVICES_FRAMEWORK_CLOUD_CLOUD_EVENT_H diff --git a/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h b/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h index 8aa732cf..6bb34530 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h +++ b/datamgr_service/services/distributeddataservice/framework/include/feature/feature_system.h @@ -42,6 +42,7 @@ public: virtual int32_t OnExecutor(std::shared_ptr executors); virtual int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName); virtual int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); + virtual int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index, uint32_t tokenId); virtual int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m); virtual int32_t OnUserChange(uint32_t code, const std::string &user, const std::string &account); virtual int32_t Online(const std::string &device); diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h index 0dd4e15d..3522dfb5 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/auto_cache.h @@ -44,9 +44,7 @@ public: API_EXPORT void Bind(std::shared_ptr executor); - API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers); - - API_EXPORT int32_t CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta); + API_EXPORT Store GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers = true); API_EXPORT void CloseStore(uint32_t tokenId, const std::string &storeId); diff --git a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h index 56cdcf87..097f8d9a 100644 --- a/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h +++ b/datamgr_service/services/distributeddataservice/framework/include/store/general_store.h @@ -23,6 +23,7 @@ #include "store/general_watcher.h" namespace OHOS::DistributedData { class CloudDB; +class SchemaMeta; class GeneralStore { public: using Watcher = GeneralWatcher; @@ -33,6 +34,8 @@ public: virtual int32_t Bind(std::shared_ptr cloudDb) = 0; + virtual int32_t SetSchema(const SchemaMeta &schemaMeta) = 0; + virtual int32_t Execute(const std::string &table, const std::string &sql) = 0; virtual int32_t BatchInsert(const std::string &table, VBuckets &&values) = 0; diff --git a/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp b/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp index 11dedf24..bb22477f 100644 --- a/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp +++ b/datamgr_service/services/distributeddataservice/framework/store/auto_cache.cpp @@ -52,7 +52,7 @@ AutoCache::~AutoCache() } } -AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers) +AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers &watchers, bool setWatchers) { Store store; if (meta.storeType >= MAX_CREATOR_NUM || !creators_[meta.storeType]) { @@ -60,10 +60,12 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & } stores_.Compute(meta.tokenId, - [this, &meta, &watchers, &store](auto &, std::map &stores) -> bool { + [this, &meta, &watchers, &store, setWatchers](auto &, std::map &stores) -> bool { auto it = stores.find(meta.storeId); if (it != stores.end()) { - it->second.SetObservers(watchers); + if (setWatchers) { + it->second.SetObservers(watchers); + } store = it->second; return !stores.empty(); } @@ -79,24 +81,6 @@ AutoCache::Store AutoCache::GetStore(const StoreMetaData &meta, const Watchers & return store; } -int32_t AutoCache::CreateTable(const StoreMetaData &storeMetaData, const SchemaMeta &schemaMeta) -{ - stores_.Compute(storeMetaData.tokenId, - [this, &storeMetaData, &schemaMeta](auto &, std::map &stores) -> bool { - auto it = stores.find(storeMetaData.storeId); - if (it != stores.end()) { - //it->second->CreateTable(schemaMeta) - return true; - } - auto *dbStore = creators_[storeMetaData.storeType](storeMetaData); - if (dbStore != nullptr) { - //dbStore->CreateTable(schemaMeta) - } - return false; - }); - return 0; -} - void AutoCache::CloseStore(uint32_t tokenId, const std::string &storeId) { stores_.ComputeIfPresent(tokenId, [&storeId](auto &key, std::map &delegates) { diff --git a/datamgr_service/services/distributeddataservice/service/BUILD.gn b/datamgr_service/services/distributeddataservice/service/BUILD.gn index 1a7ecb58..81b76328 100644 --- a/datamgr_service/services/distributeddataservice/service/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos.gni") import("//build/ohos_var.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") group("build_module") { deps = [ ":distributeddatasvc" ] @@ -21,6 +22,7 @@ config("module_public_config") { include_dirs = [ "backup/include", "bootstrap/include", + "cloud", "config/include", "crypto/include", "data_share", @@ -53,7 +55,7 @@ ohos_shared_library("distributeddatasvc") { include_dirs = [ "../../../../data_object/frameworks/innerkitsimpl/include", "../../../../relational_store/interfaces/inner_api/cloud_data/include", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb/include", + "../../../../relational_store/interfaces/inner_api/rdb/include", ] sources = [ "backup/src/backup_manager.cpp", @@ -119,6 +121,7 @@ ohos_shared_library("distributeddatasvc") { "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter:distributeddata_adapter", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/adapter/utils:distributeddata_utils_static", "//foundation/distributeddatamgr/datamgr_service/services/distributeddataservice/framework:distributeddatasvcfwk", + "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data_inner", ] external_deps = [ @@ -138,6 +141,7 @@ ohos_shared_library("distributeddatasvc") { "ipc:ipc_core", "kv_store:distributeddata_inner", "relational_store:native_rdb", + "relational_store:rdb_bms_adapter", "relational_store:rdb_data_share_adapter", "resource_management:global_resmgr", "samgr:samgr_proxy", 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 4ad57d2b..083c86b9 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.cpp @@ -37,7 +37,7 @@ using namespace DistributedData; using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; using Account = OHOS::DistributedKv::AccountDelegate; __attribute__((used)) CloudServiceImpl::Factory CloudServiceImpl::factory_; -CloudServiceImpl::Factory::Factory() +CloudServiceImpl::Factory::Factory() noexcept { FeatureSystem::GetInstance().RegisterCreator( CloudServiceImpl::SERVICE_NAME, @@ -57,7 +57,7 @@ CloudServiceImpl::CloudServiceImpl() EventCenter::GetInstance().Subscribe(CloudEvent::FEATURE_INIT, [this](const Event &event) { auto &rdbEvent = static_cast(event); CloudInfo cloudInfo; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); + cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); if (GetServerInfo(cloudInfo) != SUCCESS) { ZLOGE("failed, user:%{public}d", cloudInfo.user); return; @@ -69,39 +69,35 @@ CloudServiceImpl::CloudServiceImpl() EventCenter::GetInstance().Subscribe(CloudEvent::GET_SCHEMA, [this](const Event &event) { auto &rdbEvent = static_cast(event); - CloudInfo cloudInfo; - cloudInfo.user = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetTokenId()); - if (GetServerInfo(cloudInfo) != SUCCESS) { - ZLOGE("failed, user:%{public}d", cloudInfo.user); - return; - } + auto userId = DistributedKv::AccountDelegate::GetInstance()->GetUserByToken(rdbEvent.GetStoreInfo().tokenId); + auto schemaMeta = GetSchemaMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().instanceId); + auto storeMeta = GetStoreMata(userId, rdbEvent.GetStoreInfo().bundleName, rdbEvent.GetStoreInfo().storeName, + rdbEvent.GetStoreInfo().instanceId); + + AutoCache::Watchers watchers; + auto store = AutoCache::GetInstance().GetStore(storeMeta, watchers, false); + store->SetSchema(schemaMeta); auto instance = CloudServer::GetInstance(); if (instance == nullptr) { + ZLOGE("instance is nullptr"); return; } - SchemaMeta schemaMeta; - std::string schemaKey = cloudInfo.GetSchemaKey(rdbEvent.GetBundleName()); - if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { - schemaMeta = instance->GetAppSchema(cloudInfo.user, rdbEvent.GetBundleName()); - MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); - auto finishedEvent = std::make_unique(CloudEvent::NEED_CREATE, rdbEvent); - finishedEvent->SetSchemaKey(schemaKey); - EventCenter::GetInstance().PostEvent(move(finishedEvent)); - } - for (auto &database : schemaMeta.databases) { - if (database.name != rdbEvent.GetStoreName() /* ||TODO:不需要同步*/) { + if (database.name != rdbEvent.GetStoreInfo().storeName /* || don't need sync */) { continue; } - auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetTokenId(), database); - //TODO:同步 + auto cloudDB = instance->ConnectCloudDB(rdbEvent.GetStoreInfo().tokenId, database); + if (cloudDB != nullptr) { + store->Bind(cloudDB); + } + // do sync } + return; }); } int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map &switches) { - std::lock_guard lg(mutex_); CloudInfo cloudInfo; cloudInfo.id = id; if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { @@ -126,7 +122,6 @@ int32_t CloudServiceImpl::EnableCloud(const std::string &id, const std::map lg(mutex_); CloudInfo cloudInfo; cloudInfo.id = id; if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { @@ -141,7 +136,6 @@ int32_t CloudServiceImpl::DisableCloud(const std::string &id) int32_t CloudServiceImpl::ChangeAppSwitch(const std::string &id, const std::string &bundleName, int32_t appSwitch) { - std::lock_guard lg(mutex_); CloudInfo cloudInfo; cloudInfo.id = id; if (GetCloudInfo(cloudInfo) != SUCCESS && GetServerInfo(cloudInfo) != SUCCESS) { @@ -304,4 +298,38 @@ ExecutorPool::Task CloudServiceImpl::GetCloudTask(int32_t retry) executor_->Schedule(std::chrono::seconds(RETRY_INTERVAL), GetCloudTask(retry + 1)); }; } + +SchemaMeta CloudServiceImpl::GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId) +{ + SchemaMeta schemaMeta; + auto instance = CloudServer::GetInstance(); + if (instance == nullptr) { + ZLOGE("instance is nullptr"); + return schemaMeta; + } + auto cloudInfo = instance->GetServerInfo(userId); + if (!cloudInfo.IsValid()) { + ZLOGE("cloudInfo is invalid"); + return schemaMeta; + } + std::string schemaKey = cloudInfo.GetSchemaKey(bundleName, instanceId); + if (!MetaDataManager::GetInstance().LoadMeta(schemaKey, schemaMeta, true)) { + schemaMeta = instance->GetAppSchema(cloudInfo.user, bundleName); + MetaDataManager::GetInstance().SaveMeta(schemaKey, schemaMeta, true); + } + return schemaMeta; +} + +StoreMetaData CloudServiceImpl::GetStoreMata(int32_t userId, const std::string &bundleName, + const std::string &storeName, int32_t instanceId) +{ + StoreMetaData storeMetaData; + storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeMetaData.user = std::to_string(userId); + storeMetaData.bundleName = bundleName; + storeMetaData.storeId = storeName; + storeMetaData.instanceId = instanceId; + MetaDataManager::GetInstance().LoadMeta(storeMetaData.GetKey(), storeMetaData); + return storeMetaData; +} } // namespace OHOS::CloudData \ 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 1d1c3592..45569140 100644 --- a/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/cloud/cloud_service_impl.h @@ -24,6 +24,7 @@ namespace OHOS::CloudData { class CloudServiceImpl : public CloudServiceStub { public: + using StoreMetaData = DistributedData::StoreMetaData; CloudServiceImpl(); ~CloudServiceImpl() = default; int32_t EnableCloud(const std::string &id, const std::map &switches) override; @@ -37,7 +38,7 @@ public: private: class Factory { public: - Factory(); + Factory() noexcept; ~Factory(); private: std::shared_ptr product_; @@ -52,14 +53,15 @@ private: void UpdateCloudInfo(CloudInfo &cloudInfo); void AddSchema(CloudInfo &cloudInfo); + SchemaMeta GetSchemaMata(int32_t userId, const std::string &bundleName, int32_t instanceId); + StoreMetaData GetStoreMata(int32_t userId, const std::string &bundleName, const std::string &storeName, + int32_t instanceId); int32_t GetCloudInfo(CloudInfo &cloudInfo); int32_t GetServerInfo(CloudInfo &cloudInfo); int32_t GetAppSchema(int32_t user, const std::string &bundleName, SchemaMeta &schemaMeta); std::string GetAppId(const std::string &bundleName); ExecutorPool::Task GetCloudTask(int32_t retry); bool CheckAccess(const std::string &bundleName); - std::mutex mutex_; - ConcurrentMap cloudInfos_; std::shared_ptr executor_; }; } // namespace OHOS::DistributedData diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp index 086b2807..1d252ce8 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.cpp @@ -46,7 +46,7 @@ using namespace OHOS::Security::AccessToken; using system_clock = std::chrono::system_clock; using DMAdapter = DistributedData::DeviceManagerAdapter; __attribute__((used)) KVDBServiceImpl::Factory KVDBServiceImpl::factory_; -KVDBServiceImpl::Factory::Factory() +KVDBServiceImpl::Factory::Factory() noexcept { FeatureSystem::GetInstance().RegisterCreator("kv_store", [this]() { if (product_ == nullptr) { diff --git a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h index b1c3e5fd..5267a0bd 100644 --- a/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/kvdb/kvdb_service_impl.h @@ -84,7 +84,7 @@ private: }; class Factory { public: - Factory(); + Factory() noexcept; ~Factory(); private: std::shared_ptr product_; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp index 2d444517..f69051ca 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.cpp @@ -167,4 +167,10 @@ int32_t RdbGeneralStore::Unwatch(int32_t origin, Watcher &watcher) { return GeneralError::E_NOT_SUPPORT; } + +int32_t RdbGeneralStore::SetSchema(const SchemaMeta &schemaMeta) +{ + //SetSchema + return GeneralError::E_OK; +} } // namespace OHOS::DistributedRdb diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h index f328986a..a77e7e33 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_general_store.h @@ -32,13 +32,15 @@ public: using Value = DistributedData::Value; using Values = DistributedData::Values; using StoreMetaData = DistributedData::StoreMetaData; - using CloudDB = DistributedData::CloudDB; + using SchemaMeta = DistributedData::SchemaMeta; + using CloudDB = DistributedData::CloudDB; using RdbStore = OHOS::NativeRdb::RdbStore; - using RdbDelegate = DistributedDB::RelationalStoreDelegate; - using RdbManager = DistributedDB::RelationalStoreManager; + using RdbDelegate = DistributedDB::RelationalStoreDelegate; + using RdbManager = DistributedDB::RelationalStoreManager; - RdbGeneralStore(const StoreMetaData &metaData); + explicit RdbGeneralStore(const StoreMetaData &metaData); int32_t Bind(std::shared_ptr cloudDb) override; + int32_t SetSchema(const SchemaMeta &schemaMeta) override; int32_t Execute(const std::string &table, const std::string &sql) override; int32_t BatchInsert(const std::string &table, VBuckets &&values) override; int32_t BatchUpdate(const std::string &table, const std::string &sql, VBuckets &&values) override; 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 c4cb0968..0a05e19d 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.cpp @@ -53,7 +53,7 @@ RdbServiceImpl::Factory::Factory() FeatureSystem::GetInstance().RegisterCreator(RdbServiceImpl::SERVICE_NAME, []() { return std::make_shared(); }); - AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore * { + AutoCache::GetInstance().RegCreator(RDB_DEVICE_COLLABORATION, [](const StoreMetaData &metaData) -> GeneralStore* { return new RdbGeneralStore(metaData); }); } @@ -88,16 +88,6 @@ RdbServiceImpl::RdbServiceImpl() : autoLaunchObserver_(this) [this](const std::string& identifier, DistributedDB::AutoLaunchParam ¶m) { return ResolveAutoLaunch(identifier, param); }); - - EventCenter::GetInstance().Subscribe(CloudEvent::NEED_CREATE, [this](const Event &event) { - auto &cloudEvent = static_cast(event); - if (cloudEvent.GetFeatureName() != "relation_store") { - return; - } - DistributedData::SchemaMeta schemaMeta; - MetaDataManager::GetInstance().LoadMeta(cloudEvent.GetSchemaKey(), schemaMeta); - //CreateDatabase(schemaMeta)TODO:根据schema创建表和trigger - }); } int32_t RdbServiceImpl::ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) @@ -179,9 +169,9 @@ std::string RdbServiceImpl::ObtainDistributedTableName(const std::string &device return DistributedDB::RelationalStoreManager::GetDistributedTableName(uuid, table); } -int32_t RdbServiceImpl::InitNotifier(const std::string &bundleName, const sptr ¬ifier) +int32_t RdbServiceImpl::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) { - if (!CheckAccess(bundleName, "")) { + if (!CheckAccess(param.bundleName_, "")) { ZLOGE("permission error"); return RDB_ERROR; } @@ -375,7 +365,7 @@ std::string RdbServiceImpl::GenIdentifier(const RdbSyncerParam ¶m) pid_t uid = IPCSkeleton::GetCallingUid(); uint32_t token = IPCSkeleton::GetCallingTokenID(); auto storeId = RdbSyncer::RemoveSuffix(param.storeName_); - CheckerManager::StoreInfo storeInfo{ uid, token, param.bundleName_, storeId }; + CheckerManager::StoreInfo storeInfo { uid, token, param.bundleName_, storeId }; auto userId = AccountDelegate::GetInstance()->GetUserByToken(token); std::string appId = CheckerManager::GetInstance().GetAppId(storeInfo); std::string identifier = RelationalStoreManager::GetRelationalStoreIdentifier( @@ -473,25 +463,84 @@ int32_t RdbServiceImpl::DestroyRDBTable(const RdbSyncerParam ¶m) int32_t RdbServiceImpl::OnInitialize() { - auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, CloudEvent(IPCSkeleton::GetCallingTokenID())); + CloudEvent::StoreInfo storeInfo = { IPCSkeleton::GetCallingTokenID() }; + auto initEvt = std::make_unique(CloudEvent::FEATURE_INIT, storeInfo); EventCenter::GetInstance().PostEvent(std::move(initEvt)); return RDB_OK; } -int32_t RdbServiceImpl::GetSchema(const std::string &bundleName, const std::string &storeName) +int32_t RdbServiceImpl::GetSchema(const RdbSyncerParam ¶m) { - if (!CheckAccess(bundleName, storeName)) { + if (!CheckAccess(param.bundleName_, param.storeName_)) { ZLOGE("permission error"); return RDB_ERROR; } - auto event = std::make_unique(CloudEvent::GET_SCHEMA, IPCSkeleton::GetCallingTokenID(), - storeName, bundleName); - EventCenter::GetInstance().PostEvent(std::move(event)); + + auto storeMeta = GetStoreMetaData(param); + StoreMetaData oldMeta; + bool isCreated = MetaDataManager::GetInstance().LoadMeta(storeMeta.GetKey(), oldMeta, true); + if (isCreated && (oldMeta.storeType != storeMeta.storeType || + Constant::NotEqual(oldMeta.isEncrypt, storeMeta.isEncrypt) || + oldMeta.area != storeMeta.area)) { + ZLOGE("meta bundle:%{public}s store:%{public}s type:%{public}d->%{public}d encrypt:%{public}d->%{public}d " + "area:%{public}d->%{public}d", + storeMeta.bundleName.c_str(), storeMeta.storeId.c_str(), oldMeta.storeType, storeMeta.storeType, + oldMeta.isEncrypt, storeMeta.isEncrypt, oldMeta.area, storeMeta.area); + return RDB_ERROR; + } + auto saved = MetaDataManager::GetInstance().SaveMeta(storeMeta.GetKey(), storeMeta, true); + if (!saved) { + return RDB_ERROR; + } + CloudEvent::StoreInfo storeInfo { IPCSkeleton::GetCallingTokenID(), param.bundleName_, param.storeName_, + storeMeta.instanceId }; + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "relational_store"); + EventCenter::GetInstance().PostEvent(move(event)); return RDB_OK; } + +StoreMetaData RdbServiceImpl::GetStoreMetaData(const RdbSyncerParam ¶m) +{ + StoreMetaData metaData; + metaData.uid = IPCSkeleton::GetCallingUid(); + metaData.tokenId = IPCSkeleton::GetCallingTokenID(); + metaData.instanceId = GetInstIndex(metaData.tokenId, param.bundleName_, param.storeName_); + metaData.bundleName = param.bundleName_; + metaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + metaData.storeId = param.storeName_; + metaData.user = std::to_string(AccountDelegate::GetInstance()->GetUserByToken(metaData.tokenId)); + metaData.storeType = param.type_; + metaData.securityLevel = param.level_; + metaData.area = param.area_; + metaData.appId = CheckerManager::GetInstance().GetAppId(Converter::ConvertToStoreInfo(metaData)); + metaData.appType = "harmony"; + metaData.hapName = param.hapName_; + metaData.dataDir = DirectoryManager::GetInstance().GetStorePath(metaData) + "/" + param.storeName_; + metaData.account = AccountDelegate::GetInstance()->GetCurrentAccountId(); + metaData.isEncrypt = param.isEncrypt_; + return metaData; +} + +int32_t RdbServiceImpl::GetInstIndex(uint32_t tokenId, const std::string &bundleName, const std::string &storeName) +{ + if (AccessTokenKit::GetTokenTypeFlag(tokenId) != TOKEN_HAP) { + return 0; + } + + HapTokenInfo tokenInfo; + tokenInfo.instIndex = -1; + int errCode = AccessTokenKit::GetHapTokenInfo(tokenId, tokenInfo); + if (errCode != RET_SUCCESS) { + ZLOGE("GetHapTokenInfo error:%{public}d, tokenId:0x%{public}x bundleName:%{public}s storeName:%{public}s", + errCode, tokenId, bundleName.c_str(), storeName.c_str()); + return -1; + } + return tokenInfo.instIndex; +} + int32_t RdbServiceImpl::OnExecutor(std::shared_ptr executors) { executors_ = std::move(executors); - return E_OK; + 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 aa2d7615..89fcac3c 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_impl.h @@ -40,7 +40,7 @@ public: /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const std::string& bundleName, const sptr ¬ifier) override; + int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; @@ -59,7 +59,7 @@ public: int32_t OnInitialize() override; - int32_t GetSchema(const std::string &bundleName, const std::string &storeName) override; + int32_t GetSchema(const RdbSyncerParam ¶m) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, @@ -83,6 +83,10 @@ private: void OnAsyncComplete(pid_t pid, uint32_t seqNum, const SyncResult& result); + StoreMetaData GetStoreMetaData(const RdbSyncerParam& param); + + int32_t GetInstIndex(uint32_t tokenId, const std::string &appId, const std::string &storeName); + class DeathRecipientImpl : public IRemoteObject::DeathRecipient { public: using DeathCallback = std::function; diff --git a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp index 85e3719e..39fd62d4 100644 --- a/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp +++ b/datamgr_service/services/distributeddataservice/service/rdb/rdb_service_stub.cpp @@ -40,14 +40,15 @@ int32_t RdbServiceStub::OnRemoteObtainDistributedTableName(MessageParcel &data, return RDB_OK; } -int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) { - std::string bundleName; - std::string storeName; - if (!ITypesUtil::Unmarshal(data, bundleName, storeName)) { - ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", bundleName.c_str(), storeName.c_str()); +int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) +{ + RdbSyncerParam param; + if (!ITypesUtil::Unmarshal(data, param)) { + ZLOGE("Unmarshal bundleName_:%{public}s storeName_:%{public}s", param.bundleName_.c_str(), + param.storeName_.c_str()); return IPC_STUB_INVALID_DATA_ERR; } - auto status = GetSchema(bundleName, storeName); + auto status = GetSchema(param); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; @@ -57,14 +58,14 @@ int32_t RdbServiceStub::OnGetSchema(MessageParcel &data, MessageParcel &reply) { int32_t RdbServiceStub::OnRemoteInitNotifier(MessageParcel &data, MessageParcel &reply) { - std::string bundleName; + RdbSyncerParam param; sptr notifier; - if (!ITypesUtil::Unmarshal(data, bundleName, notifier) || bundleName.empty() || notifier == nullptr) { - ZLOGE("Unmarshal bundleName:%{public}s notifier is nullptr:%{public}d", bundleName.c_str(), + if (!ITypesUtil::Unmarshal(data, param, notifier) || param.bundleName_.empty() || notifier == nullptr) { + ZLOGE("Unmarshal bundleName:%{public}s notifier is nullptr:%{public}d", param.bundleName_.c_str(), notifier == nullptr); return IPC_STUB_INVALID_DATA_ERR; } - auto status = InitNotifier(bundleName, notifier); + auto status = InitNotifier(param, notifier); if (!ITypesUtil::Marshal(reply, status)) { ZLOGE("Marshal status:0x%{public}x", status); return IPC_STUB_WRITE_PARCEL_ERR; diff --git a/datamgr_service/services/distributeddataservice/service/test/BUILD.gn b/datamgr_service/services/distributeddataservice/service/test/BUILD.gn index 70ad6fdd..0c5173a0 100644 --- a/datamgr_service/services/distributeddataservice/service/test/BUILD.gn +++ b/datamgr_service/services/distributeddataservice/service/test/BUILD.gn @@ -12,6 +12,7 @@ # limitations under the License. import("//build/ohos_var.gni") import("//build/test.gni") +import("//foundation/distributeddatamgr/datamgr_service/datamgr_service.gni") module_output_path = "datamgr_service/distributeddatafwk" @@ -35,6 +36,34 @@ config("module_private_config") { ] } +ohos_unittest("CloudDataTest") { + module_out_path = module_output_path + sources = [ + "cloud_data_test.cpp", + "mock/db_change_data_mock.cpp", + "mock/db_store_mock.cpp", + ] + + configs = [ ":module_private_config" ] + + external_deps = [ + "ability_base:base", + "ability_base:want", + "c_utils:utils", + "hiviewdfx_hilog_native:libhilog", + "ipc:ipc_core", + "kv_store:distributeddata_inner", + ] + + deps = [ + "${kv_store_distributeddb_path}:distributeddb", + "../../adapter:distributeddata_adapter", + "../../framework:distributeddatasvcfwk", + "../../service:distributeddatasvc", + "//third_party/googletest:gtest_main", + ] +} + ohos_unittest("ConfigFactoryTest") { module_out_path = module_output_path sources = [ "config_factory_test.cpp" ] @@ -146,6 +175,7 @@ group("unittest") { deps = [] deps += [ + ":CloudDataTest", ":ConfigFactoryTest", ":CryptoManagerTest", ":DeviceMatrixTest", diff --git a/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp new file mode 100644 index 00000000..62dc043e --- /dev/null +++ b/datamgr_service/services/distributeddataservice/service/test/cloud_data_test.cpp @@ -0,0 +1,182 @@ +/* +* Copyright (c) 2022 Huawei Device Co., Ltd. +* Licensed under the Apache License, Version 2.0 (the "License"); +* you may not use this file except in compliance with the License. +* You may obtain a copy of the License at +* +* http://www.apache.org/licenses/LICENSE-2.0 +* +* Unless required by applicable law or agreed to in writing, software +* distributed under the License is distributed on an "AS IS" BASIS, +* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. +* See the License for the specific language governing permissions and +* limitations under the License. +*/ +#define LOG_TAG "CloudDataTest" +#include +#include "log_print.h" +#include "ipc_skeleton.h" +#include "account/account_delegate.h" +#include "cloud/cloud_event.h" +#include "cloud/cloud_server.h" +#include "communicator/device_manager_adapter.h" +#include "device_matrix.h" +#include "eventcenter/event_center.h" +#include "feature/feature_system.h" +#include "metadata/meta_data_manager.h" +#include "metadata/store_meta_data.h" +#include "metadata/store_meta_data_local.h" +#include "mock/db_store_mock.h" +using namespace testing::ext; +using namespace OHOS::DistributedData; +using DmAdapter = OHOS::DistributedData::DeviceManagerAdapter; + +static constexpr const char *TEST_CLOUD_BUNDLE = "test_cloud_bundleName"; +static constexpr const char *TEST_CLOUD_APPID = "test_cloud_appid"; +static constexpr const char *TEST_CLOUD_STORE = "test_cloud_database_name"; + +class CloudDataTest : public testing::Test { +public: + static void SetUpTestCase(void); + static void TearDownTestCase(void); + void SetUp(); + void TearDown(); + +protected: + static constexpr const char *TEST_DISTRIBUTEDDATA_BUNDLE = "test_distributeddata"; + static constexpr const char *TEST_DISTRIBUTEDDATA_STORE = "test_service_meta"; + static constexpr const char *TEST_DISTRIBUTEDDATA_USER = "-1"; + + void InitMetaData(); + static std::shared_ptr dbStoreMock_; + StoreMetaData metaData_; +}; + +class CloudServerMock : public CloudServer { +public: + CloudInfo GetServerInfo(int32_t userId) override; + SchemaMeta GetAppSchema(int32_t userId, const std::string &bundleName) override; + virtual ~CloudServerMock() = default; + static constexpr uint64_t REMAINSPACE = 1000; + static constexpr uint64_t TATALSPACE = 2000; +}; + +CloudInfo CloudServerMock::GetServerInfo(int32_t userId) +{ + CloudInfo cloudInfo; + cloudInfo.user = userId; + cloudInfo.id = "test_cloud_id"; + cloudInfo.remainSpace = REMAINSPACE; + cloudInfo.totalSpace = TATALSPACE; + cloudInfo.enableCloud = true; + + CloudInfo::AppInfo appInfo; + appInfo.bundleName = TEST_CLOUD_BUNDLE; + appInfo.appId = TEST_CLOUD_APPID; + appInfo.version = 1; + appInfo.cloudSwitch = true; + + cloudInfo.apps.emplace_back(std::move(appInfo)); + return cloudInfo; +} + +SchemaMeta CloudServerMock::GetAppSchema(int32_t userId, const std::string &bundleName) +{ + SchemaMeta::Field field1; + field1.colName = "test_cloud_field_name1"; + field1.alias = "test_cloud_field_alias1"; + SchemaMeta::Field field2; + field2.colName = "test_cloud_field_name2"; + field2.alias = "test_cloud_field_alias2"; + + SchemaMeta::Table table; + table.name = "test_cloud_table_name"; + table.alias = "test_cloud_table_alias"; + table.fields.emplace_back(field1); + table.fields.emplace_back(field2); + + SchemaMeta::Database database; + database.name = TEST_CLOUD_STORE; + database.alias = "test_cloud_database_alias"; + database.tables.emplace_back(table); + + SchemaMeta schemaMeta; + schemaMeta.version = 1; + schemaMeta.databases.emplace_back(database); + + return schemaMeta; +} + +std::shared_ptr CloudDataTest::dbStoreMock_ = std::make_shared(); + +void CloudDataTest::InitMetaData() +{ + metaData_.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + metaData_.appId = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.bundleName = TEST_DISTRIBUTEDDATA_BUNDLE; + metaData_.user = TEST_DISTRIBUTEDDATA_USER; + metaData_.area = OHOS::DistributedKv::EL1; + metaData_.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + metaData_.instanceId = 0; + metaData_.isAutoSync = true; + metaData_.storeType = 1; + metaData_.storeId = TEST_DISTRIBUTEDDATA_STORE; + PolicyValue value; + value.type = OHOS::DistributedKv::PolicyType::IMMEDIATE_SYNC_ON_ONLINE; +} + +void CloudDataTest::SetUpTestCase(void) +{ + MetaDataManager::GetInstance().Initialize(dbStoreMock_, nullptr, [](const auto &, auto) { + DeviceMatrix::GetInstance().OnChanged(DeviceMatrix::META_STORE_MASK); + }); + + auto cloudServerMock = new CloudServerMock(); + ASSERT_TRUE(CloudServer::RegisterCloudInstance(cloudServerMock)); + FeatureSystem::GetInstance().GetCreator("cloud")(); +} + +void CloudDataTest::TearDownTestCase() {} + +void CloudDataTest::SetUp() +{ + InitMetaData(); + MetaDataManager::GetInstance().SaveMeta(metaData_.GetKey(), metaData_); + + StoreMetaData storeMetaData; + storeMetaData.deviceId = DmAdapter::GetInstance().GetLocalDevice().uuid; + storeMetaData.user = -1; + storeMetaData.bundleName = TEST_CLOUD_BUNDLE; + storeMetaData.storeId = TEST_CLOUD_STORE; + storeMetaData.instanceId = 0; + storeMetaData.isAutoSync = true; + storeMetaData.storeType = 1; + storeMetaData.area = OHOS::DistributedKv::EL1; + storeMetaData.tokenId = OHOS::IPCSkeleton::GetCallingTokenID(); + MetaDataManager::GetInstance().SaveMeta(storeMetaData.GetKey(), storeMetaData); +} + +void CloudDataTest::TearDown() {} + +/** +* @tc.name: GetSchema +* @tc.desc: GetSchema from cloud. +* @tc.type: FUNC +* @tc.require: +* @tc.author: ht +*/ +HWTEST_F(CloudDataTest, GetSchema, TestSize.Level0) +{ + ZLOGI("CloudDataTest start"); + std::shared_ptr cloudServerMock = std::make_shared(); + auto cloudInfo = cloudServerMock->GetServerInfo(-1); + ASSERT_TRUE(MetaDataManager::GetInstance().DelMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), true)); + StoreMetaData storeMetaData; + ASSERT_FALSE( + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); + CloudEvent::StoreInfo storeInfo { 0, TEST_CLOUD_BUNDLE, TEST_CLOUD_STORE, 0 }; + auto event = std::make_unique(CloudEvent::GET_SCHEMA, std::move(storeInfo), "test_service"); + EventCenter::GetInstance().PostEvent(move(event)); + ASSERT_TRUE( + MetaDataManager::GetInstance().LoadMeta(cloudInfo.GetSchemaKey(TEST_CLOUD_BUNDLE), storeMetaData, true)); +} diff --git a/datamgr_service/services/distributeddataservice/service/test/mock/db_store_mock.cpp b/datamgr_service/services/distributeddataservice/service/test/mock/db_store_mock.cpp index 87639965..e4f0f3b2 100644 --- a/datamgr_service/services/distributeddataservice/service/test/mock/db_store_mock.cpp +++ b/datamgr_service/services/distributeddataservice/service/test/mock/db_store_mock.cpp @@ -263,7 +263,7 @@ DBStatus DBStoreMock::GetEntries(ConcurrentMap &store, const Key &ke store.ForEach([&entries, &keyPrefix](const Key &key, Value &value) { auto it = std::search(key.begin(), key.end(), keyPrefix.begin(), keyPrefix.end()); if (it == key.begin()) { - entries.push_back({ key, value }); + entries.push_back( { key, value } ); } return false; }); @@ -273,9 +273,9 @@ DBStatus DBStoreMock::GetEntries(ConcurrentMap &store, const Key &ke DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vector &entries) { for (auto &entry : entries) { - entries_.InsertOrAssign(entry.key, entry.value); + store.InsertOrAssign(entry.key, entry.value); } - DBChangeDataMock changeData({}, entries, {}); + DBChangeDataMock changeData( {}, entries, {} ); observers_.ForEachCopies([&changeData](const auto &observer, auto &keys) mutable { if (observer) { observer->OnChange(changeData); @@ -287,13 +287,13 @@ DBStatus DBStoreMock::PutBatch(ConcurrentMap &store, const std::vect DBStatus DBStoreMock::DeleteBatch(ConcurrentMap &store, const std::vector &keys) { - DBChangeDataMock changeData({}, {}, {}); + DBChangeDataMock changeData( {}, {}, {} ); for (auto &key : keys) { auto it = store.Find(key); if (!it.first) { continue; } - changeData.AddEntry({key, std::move(it.second)}); + changeData.AddEntry( {key, std::move(it.second)} ); store.Erase(key); } diff --git a/mock/src/mock_notification.cpp b/mock/src/mock_notification.cpp index 0de94585..d6f1beb8 100644 --- a/mock/src/mock_notification.cpp +++ b/mock/src/mock_notification.cpp @@ -192,6 +192,7 @@ const std::string CommonEventSupport::COMMON_EVENT_USER_REMOVED = "COMMON_EVENT_ const std::string CommonEventSupport::COMMON_EVENT_HWID_TOKEN_INVALID = "COMMON_EVENT_HWID_TOKEN_INVALID"; const std::string CommonEventSupport::COMMON_EVENT_HWID_LOGOUT = "COMMON_EVENT_HWID_LOGOUT"; const std::string CommonEventSupport::COMMON_EVENT_HWID_LOGIN = "COMMON_EVENT_HWID_LOGIN"; +const std::string CommonEventSupport::COMMON_EVENT_PACKAGE_REPLACED = "COMMON_EVENT_PACKAGE_REPLACED"; const std::string CommonEventSupport::COMMON_EVENT_PACKAGE_REMOVED = "COMMON_EVENT_PACKAGE_REMOVED"; const std::string CommonEventSupport::COMMON_EVENT_USER_SWITCHED = "COMMON_EVENT_USER_SWITCHED"; const std::string CommonEventSupport::COMMON_EVENT_SANDBOX_PACKAGE_REMOVED = "COMMON_EVENT_SANDBOX_PACKAGE_REMOVED"; diff --git a/relational_store/bundle.json b/relational_store/bundle.json index 6b9df4d3..58e70977 100644 --- a/relational_store/bundle.json +++ b/relational_store/bundle.json @@ -71,7 +71,7 @@ "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/dataability:native_dataability", "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb_data_share_adapter:rdb_data_share_adapter", "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb:native_rdb", - "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data", + "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/cloud_data:cloud_data", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/dataability:dataability", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/rdb:rdb", "//foundation/distributeddatamgr/relational_store/frameworks/js/napi/rdb:napi_rdb", @@ -137,16 +137,6 @@ ], "header_base": "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/rdb_data_ability_adapter/include" } - }, - { - "name": "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data:cloud_data", - "header": { - "header_files": [ - "cloud_manager.h", - "cloud_service.h" - ], - "header_base": "//foundation/distributeddatamgr/relational_store/interfaces/inner_api/cloud_data/include" - } } ], "test": [ diff --git a/relational_store/frameworks/js/napi/cloud_data/BUILD.gn b/relational_store/frameworks/js/napi/cloud_data/BUILD.gn index e69de29b..9aaddce9 100644 --- a/relational_store/frameworks/js/napi/cloud_data/BUILD.gn +++ b/relational_store/frameworks/js/napi/cloud_data/BUILD.gn @@ -0,0 +1,63 @@ +# 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. +import("//build/ohos.gni") +import("//foundation/distributeddatamgr/relational_store/relational_store.gni") + +config("cloud_data_config") { + visibility = [ ":*" ] + + include_dirs = [ + "../../../../../kv_store/frameworks/common", + "${cloud_data_native_path}/include", + "${relational_store_innerapi_path}/rdb/include", + "${relational_store_innerapi_path}/cloud_data/include", + "include", + ] + + defines = [ + "RELATIONAL_STORE", + "SQLITE_HAS_CODEC", + ] +} + +base_sources = [ + "${cloud_data_native_path}/src/cloud_manager.cpp", + "${cloud_data_native_path}/src/cloud_service_proxy.cpp", +] + +ohos_shared_library("cloud_data") { + part_name = "relational_store" + sources = base_sources + + configs = [ ":cloud_data_config" ] + + deps = [] + ldflags = [ "-Wl,--exclude-libs,ALL" ] + cflags_cc = [ "-fvisibility=hidden" ] + + sources += [] + + public_deps = + [ "${relational_store_innerapi_path}/appdatafwk:native_appdatafwk" ] + + external_deps = [ + "c_utils:utils", + "hilog_native:libhilog", + "hitrace_native:hitrace_meter", + "ipc:ipc_core", + "samgr:samgr_proxy", + ] + + innerapi_tags = [ "platformsdk" ] + subsystem_name = "distributeddatamgr" +} \ No newline at end of file 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 index 7dec3bf8..b94c8032 100644 --- a/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h +++ b/relational_store/frameworks/native/cloud_data/include/cloud_service_proxy.h @@ -21,7 +21,7 @@ #include "iremote_proxy.h" namespace OHOS::CloudData { -class CloudServiceProxy: public IRemoteProxy { +class CloudServiceProxy : public IRemoteProxy { public: explicit CloudServiceProxy(const sptr& object); virtual ~CloudServiceProxy() = default; diff --git a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h index 22150516..61b84c29 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h +++ b/relational_store/frameworks/native/rdb/include/rdb_manager_impl.h @@ -38,7 +38,7 @@ public: static RdbManagerImpl &GetInstance(); - int GetRdbService(const std::string& bundleName, std::shared_ptr &service); + int GetRdbService(const RdbSyncerParam ¶m, std::shared_ptr &service); void OnRemoteDied(); diff --git a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h index 7dcab25e..6c7bc59d 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h +++ b/relational_store/frameworks/native/rdb/include/rdb_service_proxy.h @@ -33,8 +33,8 @@ public: std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; - int32_t InitNotifier(const std::string& bundleName); - int32_t InitNotifier(const std::string& bundleName, const sptr ¬ifier) override; + int32_t InitNotifier(const RdbSyncerParam ¶m); + int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam& param, const std::vector& tables) override; @@ -57,8 +57,7 @@ public: void ImportObservers(ObserverMap& observers); /*CLoudData*/ - int32_t GetSchema(const std::string &bundleName, const std::string &storeName) override; - + int32_t GetSchema(const RdbSyncerParam ¶m) override; protected: int32_t DoSync(const RdbSyncerParam& param, const SyncOption& option, const RdbPredicates& predicates, SyncResult& result) override; diff --git a/relational_store/frameworks/native/rdb/include/rdb_types_util.h b/relational_store/frameworks/native/rdb/include/rdb_types_util.h index cb1be91c..019b9ce4 100644 --- a/relational_store/frameworks/native/rdb/include/rdb_types_util.h +++ b/relational_store/frameworks/native/rdb/include/rdb_types_util.h @@ -23,7 +23,6 @@ namespace OHOS::ITypesUtil { using SyncerParam = DistributedRdb::RdbSyncerParam; using SyncOption = DistributedRdb::SyncOption; -using CloudParam = DistributedRdb::CloudParam; using RdbPredicates = DistributedRdb::RdbPredicates; using RdbOperation = DistributedRdb::RdbPredicateOperation; using ValueObject = NativeRdb::ValueObject; @@ -36,10 +35,6 @@ API_EXPORT bool Unmarshalling(SyncerParam &output, MessageParcel &data); template<> API_EXPORT bool Marshalling(const SyncOption &input, MessageParcel &data); template<> -API_EXPORT bool Marshalling(const CloudParam &input, MessageParcel &data); -template<> -API_EXPORT bool Unmarshalling(CloudParam &output, MessageParcel &data); -template<> API_EXPORT bool Unmarshalling(SyncOption &output, MessageParcel &data); template<> API_EXPORT bool Marshalling(const RdbPredicates &input, MessageParcel &data); diff --git a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp index 0e38609f..d7321f92 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_manager_impl.cpp @@ -80,7 +80,7 @@ RdbManagerImpl& RdbManagerImpl::GetInstance() return manager; } -int RdbManagerImpl::GetRdbService(const std::string& bundleName, std::shared_ptr &service) +int RdbManagerImpl::GetRdbService(const RdbSyncerParam ¶m, std::shared_ptr &service) { std::lock_guard lock(mutex_); if (rdbService_ != nullptr) { @@ -101,7 +101,7 @@ int RdbManagerImpl::GetRdbService(const std::string& bundleName, std::shared_ptr return E_NOT_SUPPORTED; } sptr serviceProxy = iface_cast(remote); - if (serviceProxy->InitNotifier(bundleName) != RDB_OK) { + if (serviceProxy->InitNotifier(param) != RDB_OK) { ZLOGE("init notifier failed"); return E_ERROR; } @@ -111,7 +111,7 @@ int RdbManagerImpl::GetRdbService(const std::string& bundleName, std::shared_ptr if (rdbService_ == nullptr) { return E_ERROR; } - bundleName_ = bundleName; + bundleName_ = param.bundleName_; service = rdbService_; return E_OK; } @@ -131,7 +131,7 @@ void RdbManagerImpl::OnRemoteDied() RdbSyncerParam param; param.bundleName_ = bundleName_; std::shared_ptr service = nullptr; - int errCode = GetRdbService(bundleName_, service); + int errCode = GetRdbService(param, service); if (errCode != E_OK) { return; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp index 42bfe9a9..fa87c474 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_service_proxy.cpp @@ -83,19 +83,22 @@ std::string RdbServiceProxy::ObtainDistributedTableName(const std::string &devic return reply.ReadString(); } -int32_t RdbServiceProxy::InitNotifier(const std::string &bundleName) +int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m) { - notifier_ = new (std::nothrow) - RdbNotifierStub([this](uint32_t seqNum, const SyncResult &result) { OnSyncComplete(seqNum, result); }, - [this](const std::string &storeName, const std::vector &devices) { - OnDataChange(storeName, devices); - }); + notifier_ = new (std::nothrow) RdbNotifierStub( + [this] (uint32_t seqNum, const SyncResult& result) { + OnSyncComplete(seqNum, result); + }, + [this] (const std::string& storeName, const std::vector& devices) { + OnDataChange(storeName, devices); + } + ); if (notifier_ == nullptr) { ZLOGE("create notifier failed"); return RDB_ERROR; } - if (InitNotifier(bundleName, notifier_->AsObject().GetRefPtr()) != RDB_OK) { + if (InitNotifier(param, notifier_->AsObject().GetRefPtr()) != RDB_OK) { notifier_ = nullptr; return RDB_ERROR; } @@ -104,12 +107,12 @@ int32_t RdbServiceProxy::InitNotifier(const std::string &bundleName) return RDB_OK; } -int32_t RdbServiceProxy::InitNotifier(const std::string& bundleName, const sptr ¬ifier) +int32_t RdbServiceProxy::InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) { MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, bundleName, notifier); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_INIT_NOTIFIER, reply, param, notifier); if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s", status, bundleName.c_str()); + ZLOGE("status:%{public}d, bundleName:%{public}s", status, param.bundleName_.c_str()); } return status; } @@ -339,13 +342,13 @@ int32_t RdbServiceProxy::DestroyRDBTable(const RdbSyncerParam ¶m) return status; } -int32_t RdbServiceProxy::GetSchema(const std::string &bundleName, const std::string &storeName) +int32_t RdbServiceProxy::GetSchema(const RdbSyncerParam ¶m) { MessageParcel reply; - int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, bundleName, storeName); + int32_t status = IPC_SEND(RDB_SERVICE_CMD_GET_SCHEMA, reply, param); if (status != RDB_OK) { - ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, bundleName.c_str(), - storeName.c_str()); + ZLOGE("status:%{public}d, bundleName:%{public}s, storeName:%{public}s", status, param.bundleName_.c_str(), + param.storeName_.c_str()); } return status; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp index 2c63d00c..8a5b3aa5 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_store_impl.cpp @@ -90,13 +90,13 @@ int RdbStoreImpl::InnerOpen(const RdbStoreConfig &config) syncerParam_.password_ = {}; std::shared_ptr service = nullptr; - errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(config.GetBundleName(), service); + errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { LOG_ERROR("RdbStoreImpl::InnerOpen get service failed, err is %{public}d.", errCode); return E_OK; } - errCode = service->GetSchema(config.GetBundleName(), config.GetName()); + errCode = service->GetSchema(syncerParam_); if (errCode != E_OK) { LOG_ERROR("RdbStoreImpl::InnerOpen GetSchema failed, err is %{public}d.", errCode); return E_OK; @@ -127,7 +127,7 @@ RdbStoreImpl::~RdbStoreImpl() #if !defined(WINDOWS_PLATFORM) && !defined(MAC_PLATFORM) && !defined(ANDROID_PLATFORM) && !defined(IOS_PLATFORM) if (isShared_) { std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { LOG_ERROR("RdbStoreImpl::~RdbStoreImpl get service failed"); return; @@ -432,7 +432,7 @@ std::shared_ptr RdbStoreImpl::RemoteQuery(const std::string &device, std::vector selectionArgs = predicates.GetWhereArgs(); std::string sql = SqliteSqlBuilder::BuildQueryString(predicates, columns); std::shared_ptr service = nullptr; - errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { LOG_ERROR("RdbStoreImpl::RemoteQuery get service failed"); return nullptr; @@ -1139,7 +1139,7 @@ int RdbStoreImpl::SetDistributedTables(const std::vector &tables) } std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { return errCode; } @@ -1185,7 +1185,7 @@ int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predica { DISTRIBUTED_DATA_HITRACE(std::string(__FUNCTION__)); std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { LOG_ERROR("GetRdbService is failed, err is %{public}d.", errCode); return errCode; @@ -1201,7 +1201,7 @@ int RdbStoreImpl::Sync(const SyncOption &option, const AbsRdbPredicates &predica int RdbStoreImpl::Subscribe(const SubscribeOption &option, RdbStoreObserver *observer) { std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { return errCode; } @@ -1212,7 +1212,7 @@ int RdbStoreImpl::UnSubscribe(const SubscribeOption &option, RdbStoreObserver *o { LOG_INFO("enter"); std::shared_ptr service = nullptr; - int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_.bundleName_, service); + int errCode = DistributedRdb::RdbManagerImpl::GetInstance().GetRdbService(syncerParam_, service); if (errCode != E_OK) { return errCode; } diff --git a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp index 08d63960..444ba037 100644 --- a/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp +++ b/relational_store/frameworks/native/rdb/src/rdb_types_util.cpp @@ -28,17 +28,6 @@ bool Unmarshalling(SyncerParam &output, MessageParcel &data) output.level_, output.type_, output.isAutoSync_, output.isEncrypt_, output.password_); } -template<> -bool Marshalling(const CloudParam &input, MessageParcel &data) -{ - return ITypesUtil::Marshal(data, input.bundleName, input.storeName); -} -template<> -bool Unmarshalling(CloudParam &output, MessageParcel &data) -{ - return ITypesUtil::Unmarshal(data, output.bundleName, output.storeName); -} - template<> bool Marshalling(const SyncOption &input, MessageParcel &data) { diff --git a/relational_store/interfaces/inner_api/cloud_data/BUILD.gn b/relational_store/interfaces/inner_api/cloud_data/BUILD.gn index 8c07306e..2a9adf45 100644 --- a/relational_store/interfaces/inner_api/cloud_data/BUILD.gn +++ b/relational_store/interfaces/inner_api/cloud_data/BUILD.gn @@ -13,8 +13,10 @@ import("//build/ohos.gni") import("//foundation/distributeddatamgr/relational_store/relational_store.gni") config("cloud_public_config") { - visibility = [ "//foundation/distributeddatamgr/datamgr_service:*" ] - visibility = [ "//foundation/distributeddatamgr/relational_store:*" ] + visibility = [ + "../../../../datamgr_service/*", + "../../../../relational_store/*" + ] include_dirs = [ "include", diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h index 9cef9274..fc8f6ed3 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_service.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_service.h @@ -55,14 +55,15 @@ public: virtual int32_t RemoteQuery(const RdbSyncerParam ¶m, const std::string &device, const std::string &sql, const std::vector &selectionArgs, sptr &resultSet) = 0; + virtual int32_t CreateRDBTable(const RdbSyncerParam ¶m, const std::string &writePermission, const std::string &readPermission) = 0; virtual int32_t DestroyRDBTable(const RdbSyncerParam ¶m) = 0; - virtual int32_t InitNotifier(const std::string& bundleName, const sptr ¬ifier) = 0; + virtual int32_t InitNotifier(const RdbSyncerParam ¶m, const sptr notifier) = 0; - virtual int32_t GetSchema(const std::string &bundleName, const std::string &storeName) = 0; + virtual int32_t GetSchema(const RdbSyncerParam ¶m) = 0; static constexpr const char *SERVICE_NAME = "relational_store"; protected: diff --git a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h index 291c6484..ed0d372f 100644 --- a/relational_store/interfaces/inner_api/rdb/include/rdb_types.h +++ b/relational_store/interfaces/inner_api/rdb/include/rdb_types.h @@ -48,13 +48,6 @@ struct RdbSyncerParam { }; }; -struct CloudParam { - std::string bundleName; - std::string storeName; - std::string cloudId; -// int32_t schemaVerion; -}; - enum SyncMode { PUSH, PULL, diff --git a/relational_store/relational_store.gni b/relational_store/relational_store.gni index 442ce6d8..0b286407 100644 --- a/relational_store/relational_store.gni +++ b/relational_store/relational_store.gni @@ -27,4 +27,5 @@ relational_store_native_path = "${relational_store_base_path}/frameworks/native" relational_store_innerapi_path = "${relational_store_base_path}/interfaces/inner_api" -cloud_data_native_path = "${relational_store_base_path}/frameworks/native/cloud_data" \ No newline at end of file +cloud_data_native_path = + "${relational_store_base_path}/frameworks/native/cloud_data" -- Gitee From 2b8c45e81f51b6dd0a501d9fe5f838e0e29db159 Mon Sep 17 00:00:00 2001 From: htt1997 Date: Wed, 10 May 2023 15:44:56 +0800 Subject: [PATCH 2/2] merge Signed-off-by: htt1997 --- datamgr_service/datamgr_service.gni | 1 - 1 file changed, 1 deletion(-) diff --git a/datamgr_service/datamgr_service.gni b/datamgr_service/datamgr_service.gni index c3fa0d49..6ec402a1 100644 --- a/datamgr_service/datamgr_service.gni +++ b/datamgr_service/datamgr_service.gni @@ -26,7 +26,6 @@ datashare_path = "//foundation/distributeddatamgr/data_share" ipc_core_path = "//foundation/communication/ipc/interfaces/innerkits/ipc_core" device_manager_path = "//foundation/distributedhardware/device_manager" - declare_args() { datamgr_service_power = true if (!defined(global_parts_info.power_manager_native_powermgr_client) || -- Gitee