1 /* 2 * Copyright (c) 2021 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 DISTRIBUTEDDB_TYPES_EXPORT_H 17 #define DISTRIBUTEDDB_TYPES_EXPORT_H 18 19 #include <climits> 20 #include <cstdint> 21 #include <functional> 22 #include <map> 23 #include <set> 24 #include <string> 25 #include <vector> 26 27 namespace DistributedDB { 28 #ifdef _WIN32 29 #ifdef DB_DLL_EXPORT 30 #define DB_API __declspec(dllexport) 31 #else 32 #define DB_API 33 #endif 34 #else 35 #define DB_API __attribute__ ((visibility ("default"))) 36 #endif 37 38 #define DB_SYMBOL DB_API 39 40 using Key = std::vector<uint8_t>; 41 using Value = std::vector<uint8_t>; 42 43 using AssetsMap = std::map<std::string, std::set<std::string>>; 44 using AssetsGroupMap = std::map<uint32_t, AssetsMap>; 45 46 struct Entry { 47 Key key; 48 Value value; 49 }; 50 51 enum class CipherType { 52 DEFAULT, 53 AES_256_GCM, // AES-256-GCM 54 }; 55 56 class CipherPassword final { 57 public: 58 enum ErrorCode { 59 OK = 0, 60 OVERSIZE, 61 INVALID_INPUT, 62 SECUREC_ERROR, 63 }; 64 65 DB_API CipherPassword(); 66 DB_API ~CipherPassword(); 67 68 DB_API bool operator==(const CipherPassword &input) const; 69 DB_API bool operator!=(const CipherPassword &input) const; 70 71 DB_API size_t GetSize() const; 72 DB_API const uint8_t *GetData() const; 73 DB_API int SetValue(const uint8_t *inputData, size_t inputSize); 74 DB_API int Clear(); 75 76 private: 77 static const size_t MAX_PASSWORD_SIZE = 128; 78 uint8_t data_[MAX_PASSWORD_SIZE] = {UCHAR_MAX}; 79 size_t size_ = 0; 80 }; 81 82 using PragmaData = void *; 83 84 struct PragmaEntryDeviceIdentifier { 85 Key key; 86 bool origDevice = true; 87 std::string deviceIdentifier; 88 }; 89 90 using KvStoreNbPublishAction = std::function<void (const Entry &local, const Entry *sync, bool isLocalLastest)>; 91 92 struct PragmaDeviceIdentifier { 93 std::string deviceID; 94 std::string deviceIdentifier; 95 }; 96 97 enum WipePolicy { 98 RETAIN_STALE_DATA = 1, // remote stale data will be retained in syncing when remote db rebuiled. 99 WIPE_STALE_DATA // remote stale data will be wiped when in syncing remote db rebuiled. 100 }; 101 102 // We don't parse, read or modify the array type, so there are not a corresponding array value 103 // The leaf object is empty, an internal object always composed by other type values. 104 struct FieldValue { 105 union { 106 bool boolValue; 107 int32_t integerValue; 108 int64_t longValue = 0; 109 double doubleValue; 110 }; 111 std::string stringValue; 112 }; 113 114 enum PermissionCheckFlag { 115 CHECK_FLAG_SEND = 1, // send 116 CHECK_FLAG_RECEIVE = 2, // receive 117 CHECK_FLAG_AUTOSYNC = 4, // autosync flag 118 CHECK_FLAG_SPONSOR = 8, // sync sponsor 119 }; 120 121 struct PermissionCheckParam { 122 std::string userId; 123 std::string appId; 124 std::string storeId; 125 std::string deviceId; 126 std::string subUserId; 127 int32_t instanceId = 0; 128 std::map<std::string, std::string> extraConditions; 129 }; 130 131 struct PermissionCheckParamV4 { 132 std::string userId; 133 std::string appId; 134 std::string storeId; 135 std::string deviceId; 136 std::string subUserId; 137 }; 138 139 struct ActivationCheckParam { 140 std::string userId; 141 std::string appId; 142 std::string storeId; 143 std::string subUserId; 144 int32_t instanceId = 0; 145 }; 146 147 struct PermissionConditionParam { 148 std::string userId; 149 std::string appId; 150 std::string storeId; 151 std::string subUserId; 152 int32_t instanceId = 0; 153 }; 154 155 struct StoreStatusNotifierParam { 156 std::string userId; 157 std::string appId; 158 std::string storeId; 159 std::string subUserId; 160 std::string deviceId; 161 }; 162 163 using PermissionCheckCallback = std::function<bool (const std::string &userId, const std::string &appId, 164 const std::string &storeId, uint8_t flag)>; 165 166 using PermissionCheckCallbackV2 = std::function<bool (const std::string &userId, const std::string &appId, 167 const std::string &storeId, const std::string &deviceId, uint8_t flag)>; 168 169 using PermissionCheckCallbackV3 = std::function<bool (const PermissionCheckParam ¶m, uint8_t flag)>; 170 171 using PermissionCheckCallbackV4 = std::function<bool (const PermissionCheckParamV4 ¶m, uint8_t flag)>; 172 173 using StoreStatusNotifier = std::function<void (std::string userId, std::string appId, std::string storeId, 174 const std::string deviceId, bool onlineStatus)>; // status, 1: online, 0: offline 175 176 using StoreStatusNotifierV2 = std::function<void (StoreStatusNotifierParam param, 177 bool onlineStatus)>; // status, 1: online, 0: offline 178 179 using SyncActivationCheckCallback = std::function<bool (const std::string &userId, const std::string &appId, 180 const std::string &storeId)>; 181 182 using SyncActivationCheckCallbackV2 = std::function<bool (const ActivationCheckParam ¶m)>; 183 184 using PermissionConditionCallback = 185 std::function<std::map<std::string, std::string> (const PermissionConditionParam ¶m)>; 186 187 enum AutoLaunchStatus { 188 WRITE_OPENED = 1, 189 WRITE_CLOSED = 2, 190 INVALID_PARAM = 3, // AutoLaunchRequestCallback, if param check failed 191 }; 192 193 using AutoLaunchNotifier = std::function<void (const std::string &userId, 194 const std::string &appId, const std::string &storeId, AutoLaunchStatus status)>; 195 196 enum SecurityLabel : int { 197 INVALID_SEC_LABEL = -1, 198 NOT_SET = 0, 199 S0, 200 S1, 201 S2, 202 S3, 203 S4 204 }; 205 206 // security flag type 207 enum SecurityFlag : int { 208 INVALID_SEC_FLAG = -1, 209 ECE = 0, 210 SECE 211 }; 212 213 struct SecurityOption { 214 int securityLabel = 0; // the securityLabel is the class of data sensitive, see enum SecurityLabel 215 int securityFlag = 0; // the securityFlag is the encryption method of the file only used for S3 like 0:ECE, 1:SECE 216 bool operator==(const SecurityOption &rhs) const 217 { 218 return securityLabel == rhs.securityLabel && securityFlag == rhs.securityFlag; 219 } 220 221 bool operator!=(const SecurityOption &rhs) const 222 { 223 return !(*this == rhs); 224 } 225 }; 226 227 enum class ResultSetCacheMode : int { 228 CACHE_FULL_ENTRY = 0, // Ordinary mode efficient when sequential access, the default mode 229 CACHE_ENTRY_ID_ONLY = 1, // Special mode efficient when random access 230 }; 231 232 struct RemotePushNotifyInfo { 233 std::string deviceId; 234 }; 235 using RemotePushFinishedNotifier = std::function<void (const RemotePushNotifyInfo &info)>; 236 using RemotePushFinisheNotifier = RemotePushFinishedNotifier; // To correct spelling errors in the previous version 237 238 enum class CompressAlgorithm : uint8_t { 239 NONE = 0, 240 ZLIB = 1 241 }; 242 243 struct StoreInfo { 244 std::string userId; 245 std::string appId; 246 std::string storeId; 247 248 bool operator<(const StoreInfo &other) const 249 { 250 return std::tie(userId, appId, storeId) < std::tie(other.userId, other.appId, other.storeId); 251 } 252 }; 253 using TranslateToDeviceIdCallback = std::function<std::string (const std::string &oriDevId, const StoreInfo &info)>; 254 } // namespace DistributedDB 255 #endif // DISTRIBUTEDDB_TYPES_EXPORT_H 256