/* * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at * * http://www.apache.org/licenses/LICENSE-2.0 * * Unless required by applicable law or agreed to in writing, software * distributed under the License is distributed on an "AS IS" BASIS, * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. * See the License for the specific language governing permissions and * limitations under the License. */ #ifndef DISTRIBUTEDDATASERVICE_RDB_SERVICE_H #define DISTRIBUTEDDATASERVICE_RDB_SERVICE_H #include "rdb_service_stub.h" #include #include #include #include "cloud/cloud_event.h" #include "commonevent/data_change_event.h" #include "commonevent/set_searchable_event.h" #include "concurrent_map.h" #include "feature/static_acts.h" #include "metadata/secret_key_meta_data.h" #include "metadata/store_meta_data.h" #include "rdb_notifier_proxy.h" #include "rdb_watcher.h" #include "rdb_query.h" #include "store/auto_cache.h" #include "store/general_store.h" #include "store_observer.h" #include "visibility.h" #include "store/general_value.h" #include "snapshot/bind_event.h" namespace OHOS::DistributedRdb { class RdbServiceImpl : public RdbServiceStub { public: using StoreMetaData = OHOS::DistributedData::StoreMetaData; using SecretKeyMetaData = DistributedData::SecretKeyMetaData; using DetailAsync = DistributedData::GeneralStore::DetailAsync; using Handler = std::function> &)>; using StoreInfo = DistributedData::StoreInfo; RdbServiceImpl(); virtual ~RdbServiceImpl(); /* IPC interface */ std::string ObtainDistributedTableName(const std::string& device, const std::string& table) override; int32_t InitNotifier(const RdbSyncerParam ¶m, sptr notifier) override; int32_t SetDistributedTables(const RdbSyncerParam ¶m, const std::vector &tables, const std::vector &references, bool isRebuild, int32_t type = DISTRIBUTED_DEVICE) override; std::pair> RemoteQuery(const RdbSyncerParam& param, const std::string& device, const std::string& sql, const std::vector& selectionArgs) override; int32_t Sync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, const AsyncDetail &async) override; int32_t Subscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; int32_t UnSubscribe(const RdbSyncerParam ¶m, const SubscribeOption &option, RdbStoreObserver *observer) override; int32_t RegisterAutoSyncCallback(const RdbSyncerParam& param, std::shared_ptr observer) override; int32_t UnregisterAutoSyncCallback(const RdbSyncerParam& param, std::shared_ptr observer) override; int32_t ResolveAutoLaunch(const std::string &identifier, DistributedDB::AutoLaunchParam ¶m) override; int32_t OnAppExit(pid_t uid, pid_t pid, uint32_t tokenId, const std::string &bundleName) override; int32_t Delete(const RdbSyncerParam ¶m) override; std::pair> QuerySharingResource(const RdbSyncerParam& param, const PredicatesMemo& predicates, const std::vector& columns) override; int32_t OnBind(const BindInfo &bindInfo) override; int32_t OnInitialize() override; int32_t NotifyDataChange(const RdbSyncerParam ¶m, const RdbChangedData &rdbChangedData, const RdbNotifyConfig &rdbNotifyConfig) override; int32_t SetSearchable(const RdbSyncerParam& param, bool isSearchable) override; int32_t Disable(const RdbSyncerParam& param) override; int32_t Enable(const RdbSyncerParam& param) override; int32_t BeforeOpen(RdbSyncerParam ¶m) override; int32_t AfterOpen(const RdbSyncerParam ¶m) override; int32_t GetPassword(const RdbSyncerParam ¶m, std::vector &password) override; std::pair LockCloudContainer(const RdbSyncerParam ¶m) override; int32_t UnlockCloudContainer(const RdbSyncerParam ¶m) override; int32_t GetDebugInfo(const RdbSyncerParam ¶m, std::map &debugInfo) override; private: using Watchers = DistributedData::AutoCache::Watchers; using StaticActs = DistributedData::StaticActs; using DBStatus = DistributedDB::DBStatus; using SyncResult = std::pair, std::map>; struct SyncAgent { SyncAgent() = default; explicit SyncAgent(const std::string &bundleName); int32_t count_ = 0; std::map callBackStores_; std::string bundleName_; sptr notifier_ = nullptr; std::shared_ptr watcher_ = nullptr; void SetNotifier(sptr notifier); void SetWatcher(std::shared_ptr watcher); }; using SyncAgents = std::map; class RdbStatic : public StaticActs { public: ~RdbStatic() override {}; int32_t OnAppUninstall(const std::string &bundleName, int32_t user, int32_t index) override; int32_t OnAppUpdate(const std::string &bundleName, int32_t user, int32_t index) override; int32_t OnClearAppStorage(const std::string &bundleName, int32_t user, int32_t index, int32_t tokenId) override; private: static constexpr inline int32_t INVALID_TOKENID = 0; int32_t CloseStore(const std::string &bundleName, int32_t user, int32_t index, int32_t tokenId = INVALID_TOKENID) const; }; class Factory { public: Factory(); ~Factory(); private: std::shared_ptr product_; std::shared_ptr staticActs_; }; static constexpr inline uint32_t WAIT_TIME = 30 * 1000; static constexpr inline uint32_t SHARE_WAIT_TIME = 60; // seconds void RegisterRdbServiceInfo(); void RegisterHandler(); void DumpRdbServiceInfo(int fd, std::map> ¶ms); void DoCloudSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, const AsyncDetail &async); void DoCompensateSync(const DistributedData::BindEvent& event); int DoSync(const RdbSyncerParam ¶m, const Option &option, const PredicatesMemo &predicates, const AsyncDetail &async); Watchers GetWatchers(uint32_t tokenId, const std::string &storeName); DetailAsync GetCallbacks(uint32_t tokenId, const std::string &storeName); bool CheckAccess(const std::string& bundleName, const std::string& storeName); std::shared_ptr GetStore(const RdbSyncerParam& param); void OnAsyncComplete(uint32_t tokenId, pid_t pid, uint32_t seqNum, Details &&result); StoreMetaData GetStoreMetaData(const RdbSyncerParam ¶m); int32_t SetSecretKey(const RdbSyncerParam ¶m, const StoreMetaData &meta); int32_t Upgrade(const RdbSyncerParam ¶m, const StoreMetaData &old); std::pair> AllocResource( StoreInfo& storeInfo, std::shared_ptr rdbQuery); static Details HandleGenDetails(const DistributedData::GenDetails &details); static std::string TransferStringToHex(const std::string& origStr); static std::string RemoveSuffix(const std::string& name); static std::pair GetInstIndexAndUser(uint32_t tokenId, const std::string &bundleName); static bool GetDBPassword(const StoreMetaData &metaData, DistributedDB::CipherPassword &password); void GetCloudSchema(const RdbSyncerParam ¶m); void SetReturnParam(StoreMetaData &metadata, RdbSyncerParam ¶m); bool IsNeedMetaSync(const StoreMetaData &meta, const std::vector &uuids); SyncResult ProcessResult(const std::map &results); StoreInfo GetStoreInfo(const RdbSyncerParam ¶m); int32_t SaveDebugInfo(const StoreMetaData &metaData, const RdbSyncerParam ¶m); int32_t PostSearchEvent(int32_t evtId, const RdbSyncerParam& param, DistributedData::SetSearchableEvent::EventInfo &eventInfo); bool IsPostImmediately(const int32_t callingPid, const RdbNotifyConfig &rdbNotifyConfig, StoreInfo &storeInfo, DistributedData::DataChangeEvent::EventInfo &eventInfo, const std::string &storeName); static Factory factory_; ConcurrentMap syncAgents_; std::shared_ptr executors_; ConcurrentMap> heartbeatTaskIds_; }; } // namespace OHOS::DistributedRdb #endif