1 /* 2 * Copyright (c) 2023 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 DRIVE_KIT_H 17 #define DRIVE_KIT_H 18 #include <map> 19 #include <mutex> 20 #include <string> 21 #include <time.h> 22 #include <vector> 23 24 #include "dk_cloud_callback.h" 25 #include "dk_container.h" 26 #include "dk_error.h" 27 #include "dk_record.h" 28 29 namespace DriveKit { 30 using DKAppId = std::string; 31 using DKDatabaseName = std::string; 32 struct DKContainerInfo { 33 DKAppBundleName appBundleName; 34 DKContainerName containerName; 35 DKDatabaseName databaseName; 36 DKSchema schema; 37 }; 38 struct DKAppInfo { 39 DKAppBundleName appBundleName; 40 DKAppId appId; 41 std::string appFinger; 42 int schemaGroupVersion; 43 int enableCloud; 44 DKAppSwitchStatus switchStatus; 45 DKContainerInfo defaultContainer; 46 std::vector<DKContainerInfo> shareContainer; 47 }; 48 enum class DKSpaceStatus { 49 DK_SPACE_STATUS_NORMAL = 0, 50 DK_SPACE_STATUS_ALREADY_FULL, 51 }; 52 enum class DKCloudSyncDemon { 53 DK_CLOUD_DATA = 0, //数据子系统加载时传入 54 DK_CLOUD_FILE, //文件子系统加载时传入 55 DK_CLOUD_UNKNOWN, 56 }; 57 struct DKRecordChangeEvent { 58 std::string accountId; 59 DKAppBundleName appBundleName; 60 DKContainerName containerName; 61 std::vector<DKDatabaseScope> databaseScopes; 62 std::vector<DKRecordType> recordTypes; 63 std::map<std::string, std::string> properties; 64 }; 65 struct DKUserInfo { 66 std::string accountId; //云空间用户id 67 DKCloudStatus cloudStatus; //云空间登录状态 68 DKSpaceStatus spaceStatus; //云空间存储状态 69 uint64_t totalSpace; //用户总的云空间 70 uint64_t remainSpace; //用户剩余云空间 71 std::string pushKeyId; 72 std::string pushKeyValue; 73 }; 74 class DriveKitNative : public std::enable_shared_from_this<DriveKitNative> { 75 friend class DKContainer; 76 friend class DKDatabase; 77 78 public: 79 DriveKitNative(const DriveKitNative &) = delete; 80 DriveKitNative(DriveKitNative &&) = delete; 81 DriveKitNative &operator=(const DriveKitNative &) = delete; 82 DriveKitNative &operator=(DriveKitNative &&) = delete; 83 static std::shared_ptr<DriveKitNative> GetInstance(int userId); 84 static std::shared_ptr<DriveKitNative> GetInstance(int userId, DKCloudSyncDemon syncDemon); 85 virtual ~DriveKitNative(); 86 87 std::shared_ptr<DKContainer> GetDefaultContainer(DKAppBundleName bundleName); 88 std::shared_ptr<DKContainer> GetContainer(DKAppBundleName bundleName, DKContainerName containerName); 89 DKError GetCloudUserInfo(DKUserInfo &userInfo); 90 DKError GetCloudAppInfo(const std::vector<DKAppBundleName> &bundleNames, 91 std::map<DKAppBundleName, DKAppInfo> &appInfos); 92 DKError GetCloudAppSwitches(const std::vector<DKAppBundleName> &bundleNames, 93 std::map<DKAppBundleName, DKAppSwitchStatus> &appSwitchs); 94 DKError GetAppConfigParams(const DKAppBundleName &bundleNames, 95 std::map<std::string, std::string> ¶m); 96 DKError GetServerTime(time_t &time); 97 //校验和解析通知消息,调用该函数前,要先调用GetCloudUserInfo,否则会报错 98 DKError ResolveNotificationEvent(const std::string &extraData, DKRecordChangeEvent &event); 99 int32_t OnUploadAsset(const std::string &request, const std::string &result); 100 void ReleaseDefaultContainer(DKAppBundleName bundleName); 101 void ReleaseContainer(DKAppBundleName bundleName, DKContainerName containerName); 102 void ReleaseCloudUserInfo(); 103 void CleanCloudUserInfo(); 104 private: 105 DriveKitNative(int userId); 106 107 std::mutex containMutex_; 108 std::map<std::string, std::shared_ptr<DKContainer>> containers_; 109 std::mutex appInfoMutex_; 110 std::map<DKAppBundleName, DKAppInfo> appInfos_; 111 static std::mutex drivekitMutex_; 112 static std::map<int, std::shared_ptr<DriveKitNative>> driveKits_; 113 DKUserInfo userInfo_; 114 int userId_; 115 DKCloudSyncDemon syncDemon_; 116 }; 117 } // namespace DriveKit 118 #endif 119