1 /* 2 * Copyright (c) 2022 Huawei Device Co., Ltd. 3 * Licensed under the Apache License, Version 2.0 (the "License"); 4 * you may not use this file except in compliance with the License. 5 * You may obtain a copy of the License at 6 * 7 * http://www.apache.org/licenses/LICENSE-2.0 8 * 9 * Unless required by applicable law or agreed to in writing, software 10 * distributed under the License is distributed on an "AS IS" BASIS, 11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 12 * See the License for the specific language governing permissions and 13 * limitations under the License. 14 */ 15 16 #ifndef DISTRIBUTEDDATAMGR_OBJECT_MANAGER_H 17 #define DISTRIBUTEDDATAMGR_OBJECT_MANAGER_H 18 19 #include <atomic> 20 21 #include "communication_provider.h" 22 #include "iobject_callback.h" 23 #include "kvstore_sync_callback.h" 24 25 #include "task_scheduler.h" 26 #include "timer.h" 27 #include "types.h" 28 #include "kv_store_delegate_manager.h" 29 #include "object_data_listener.h" 30 #include "concurrent_map.h" 31 #include "object_common.h" 32 33 namespace OHOS { 34 namespace DistributedObject { 35 using SyncCallBack = std::function<void(const std::map<std::string, int32_t> &results)>; 36 37 enum Status { 38 OBJECT_SUCCESS, 39 OBJECT_DBSTATUS_ERROR, 40 OBJECT_INNER_ERROR, 41 OBJECT_PERMISSION_DENIED, 42 OBJECT_STORE_NOT_FOUND 43 }; 44 45 class SequenceSyncManager { 46 public: 47 enum Result { 48 SUCCESS_USER_IN_USE, 49 SUCCESS_USER_HAS_FINISHED, 50 ERR_SID_NOT_EXIST 51 }; GetInstance()52 static SequenceSyncManager *GetInstance() 53 { 54 static SequenceSyncManager sequenceSyncManager; 55 return &sequenceSyncManager; 56 } 57 58 uint64_t AddNotifier(const std::string &userId, SyncCallBack &callback); 59 Result DeleteNotifier(uint64_t sequenceId, std::string &userId); 60 Result Process( 61 uint64_t sequenceId, const std::map<std::string, DistributedDB::DBStatus> &results, std::string &userId); 62 63 private: 64 Result DeleteNotifierNoLock(uint64_t sequenceId, std::string &userId); 65 std::mutex notifierLock_; 66 std::map<std::string, std::vector<uint64_t>> userIdSeqIdRelations_; 67 std::map<uint64_t, SyncCallBack> seqIdCallbackRelations_; 68 }; 69 70 class ObjectStoreManager { 71 public: 72 ObjectStoreManager(); GetInstance()73 static ObjectStoreManager *GetInstance() 74 { 75 static ObjectStoreManager *manager = new ObjectStoreManager(); 76 return manager; 77 } 78 int32_t Save(const std::string &appId, const std::string &sessionId, 79 const std::map<std::string, std::vector<uint8_t>> &data, const std::string &deviceId, 80 sptr<IObjectSaveCallback> &callback); 81 int32_t RevokeSave( 82 const std::string &appId, const std::string &sessionId, sptr<IObjectRevokeSaveCallback> &callback); 83 int32_t Retrieve(const std::string &appId, const std::string &sessionId, sptr<IObjectRetrieveCallback> callback); 84 void SetData(const std::string &dataDir, const std::string &userId); 85 int32_t Clear(); 86 int32_t DeleteByAppId(const std::string &appId); 87 void RegisterRemoteCallback(const std::string &bundleName, const std::string &sessionId, 88 pid_t pid, uint32_t tokenId, 89 sptr <IObjectChangeCallback> &callback); 90 void UnregisterRemoteCallback(const std::string &bundleName, pid_t pid, uint32_t tokenId, 91 const std::string &sessionId = ""); 92 void NotifyChange(std::map<std::string, std::vector<uint8_t>> &changedData); 93 void CloseAfterMinute(); 94 int32_t Open(); 95 private: 96 constexpr static const char *SEPERATOR = "_"; 97 constexpr static const char *LOCAL_DEVICE = "local"; 98 constexpr static int8_t MAX_OBJECT_SIZE_PER_APP = 16; 99 constexpr static int8_t DECIMAL_BASE = 10; 100 struct CallbackInfo { 101 pid_t pid; 102 std::map<std::string, sptr<IObjectChangeCallback>> observers_; 103 bool operator<(const CallbackInfo &it_) const 104 { 105 if (pid < it_.pid) { 106 return true; 107 } 108 return false; 109 } 110 }; 111 DistributedDB::KvStoreNbDelegate *OpenObjectKvStore(); 112 void FlushClosedStore(); 113 void Close(); 114 int32_t SetSyncStatus(bool status); 115 int32_t SaveToStore(const std::string &appId, const std::string &sessionId, const std::string &toDeviceId, 116 const std::map<std::string, std::vector<uint8_t>> &data); 117 int32_t SyncOnStore(const std::string &prefix, const std::vector<std::string> &deviceList, SyncCallBack &callback); 118 int32_t RevokeSaveToStore(const std::string &prefix); 119 int32_t RetrieveFromStore( 120 const std::string &appId, const std::string &sessionId, std::map<std::string, std::vector<uint8_t>> &results); 121 void SyncCompleted(const std::map<std::string, DistributedDB::DBStatus> &results, uint64_t sequenceId); 122 void ProcessKeyByIndex(std::string &key, uint8_t index); 123 std::string GetPropertyName(const std::string &key); 124 std::string GetSessionId(const std::string &key); 125 std::string GetBundleName(const std::string &key); 126 int64_t GetTime(const std::string &key); 127 void ProcessOldEntry(const std::string &appId); 128 void ProcessSyncCallback(const std::map<std::string, int32_t> &results, const std::string &appId, 129 const std::string &sessionId, const std::string &deviceId); GetPropertyPrefix(const std::string & appId,const std::string & sessionId)130 inline std::string GetPropertyPrefix(const std::string &appId, const std::string &sessionId) 131 { 132 return appId + SEPERATOR + sessionId + SEPERATOR 133 + AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().udid + SEPERATOR; 134 }; GetPropertyPrefix(const std::string & appId,const std::string & sessionId,const std::string & toDeviceId)135 inline std::string GetPropertyPrefix( 136 const std::string &appId, const std::string &sessionId, const std::string &toDeviceId) 137 { 138 return appId + SEPERATOR + sessionId + SEPERATOR 139 + AppDistributedKv::CommunicationProvider::GetInstance().GetLocalDevice().udid + SEPERATOR + toDeviceId 140 + SEPERATOR; 141 }; GetPrefixWithoutDeviceId(const std::string & appId,const std::string & sessionId)142 inline std::string GetPrefixWithoutDeviceId(const std::string &appId, const std::string &sessionId) 143 { 144 return appId + SEPERATOR + sessionId + SEPERATOR; 145 }; 146 std::recursive_mutex kvStoreMutex_; 147 std::mutex mutex_; 148 DistributedDB::KvStoreDelegateManager *kvStoreDelegateManager_ = nullptr; 149 DistributedDB::KvStoreNbDelegate *delegate_ = nullptr; 150 ObjectDataListener *objectDataListener_ = nullptr; 151 uint32_t syncCount_ = 0; 152 std::string userId_; 153 std::atomic<bool> isSyncing_ = false; 154 Utils::Timer timer_; 155 ConcurrentMap<uint32_t /* tokenId */, CallbackInfo > callbacks_; 156 static constexpr size_t TIME_TASK_NUM = 1; 157 static constexpr int64_t INTERVAL = 1; 158 TaskScheduler scheduler_ { TIME_TASK_NUM, "object_mgr" }; 159 }; 160 } // namespace DistributedObject 161 } // namespace OHOS 162 #endif // DISTRIBUTEDDATAMGR_OBJECT_MANAGER_H 163