1 /* 2 * Copyright (c) 2024 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 OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 17 #define OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H 18 19 #include "abs_shared_result_set.h" 20 #include "background_cloud_file_download_callback.h" 21 #include "medialibrary_async_worker.h" 22 #include "metadata.h" 23 #include "rdb_predicates.h" 24 #include "timer.h" 25 #include "userfile_manager_types.h" 26 #include "values_bucket.h" 27 #include "media_file_uri.h" 28 29 namespace OHOS { 30 namespace Media { 31 #define EXPORT __attribute__ ((visibility ("default"))) 32 constexpr int32_t PROCESS_INTERVAL = 5 * 60 * 1000; // 5 minute 33 constexpr int32_t DOWNLOAD_INTERVAL = 1 * 60 * 1000; // 1 minute 34 constexpr int32_t DOWNLOAD_DURATION = 10 * 1000; // 10 seconds 35 constexpr int32_t DOWNLOAD_FAIL_MAX_TIMES = 5; // 5 times 36 37 typedef struct { 38 bool isCloud; 39 bool isVideo; 40 } QueryOption; 41 42 class BackgroundCloudFileProcessor { 43 public: 44 EXPORT static void StartTimer(); 45 EXPORT static void StopTimer(); 46 EXPORT static void SetDownloadLatestFinished(bool downloadLatestFinished); 47 EXPORT static bool GetDownloadLatestFinished(); 48 EXPORT static void HandleSuccessCallback(const DownloadProgressObj &progress); 49 EXPORT static void HandleFailedCallback(const DownloadProgressObj &progress); 50 EXPORT static void HandleStoppedCallback(const DownloadProgressObj &progress); 51 EXPORT static void RepairMimeType(); 52 EXPORT static void HandleRepairMimeType(const int32_t &repairRecord); 53 54 private: 55 typedef struct { 56 std::vector<std::string> uris; 57 MediaType mediaType; 58 } DownloadFiles; 59 60 typedef struct { 61 int32_t fileId; 62 std::string path; 63 std::string displayName; 64 int64_t size; 65 int32_t width; 66 int32_t height; 67 std::string mimeType; 68 std::string mediaSuffix; 69 int32_t duration; 70 MediaType mediaType; 71 bool isCloud; 72 bool isVideo; 73 } AbnormalData; 74 75 typedef struct { 76 std::vector<AbnormalData> abnormalData; 77 } UpdateData; 78 79 enum DownloadStatus : int32_t { 80 INIT = 0, 81 SUCCESS, 82 NETWORK_UNAVAILABLE, 83 STORAGE_FULL, 84 STOPPED, 85 UNKNOWN, 86 }; 87 88 class DownloadCloudFilesData : public AsyncTaskData { 89 public: DownloadCloudFilesData(DownloadFiles downloadFiles)90 DownloadCloudFilesData(DownloadFiles downloadFiles) : downloadFiles_(downloadFiles){}; 91 ~DownloadCloudFilesData() override = default; 92 93 DownloadFiles downloadFiles_; 94 }; 95 96 class UpdateAbnormalData : public AsyncTaskData { 97 public: UpdateAbnormalData(UpdateData updateData)98 UpdateAbnormalData(UpdateData updateData) : updateData_(updateData){}; 99 ~UpdateAbnormalData() override = default; 100 101 UpdateData updateData_; 102 }; 103 104 static void DownloadCloudFiles(); 105 static bool GetStorageFreeRatio(double &freeRatio); 106 static void SetLastDownloadMilliSecond(int64_t lastDownloadMilliSecond); 107 static int64_t GetLastDownloadMilliSecond(); 108 static void ClearDownloadCnt(); 109 static void UpdateDownloadCnt(std::string path, int64_t cnt); 110 static int64_t GetDownloadCnt(std::string path); 111 static std::shared_ptr<NativeRdb::ResultSet> QueryCloudFiles(double freeRatio); 112 static void CheckAndUpdateDownloadCnt(std::string path, int64_t cnt); 113 static void GetDownloadNum(int64_t &downloadNum); 114 static void DownloadLatestFinished(); 115 static void ParseDownloadFiles(std::shared_ptr<NativeRdb::ResultSet> &resultSet, DownloadFiles &downloadFiles); 116 static void removeFinishedResult(const std::vector<std::string>& downloadingPaths); 117 static int32_t AddDownloadTask(const DownloadFiles &downloadFiles); 118 static void DownloadCloudFilesExecutor(AsyncTaskData *data); 119 static void StopDownloadFiles(); 120 static void ProcessCloudData(); 121 static void UpdateCloudData(); 122 static std::shared_ptr<NativeRdb::ResultSet> QueryUpdateData(bool isCloud, bool isVideo); 123 static void SetPredicates(NativeRdb::RdbPredicates &predicates, bool isCloud, bool isVideo); 124 static void ParseUpdateData(std::shared_ptr<NativeRdb::ResultSet> &resultSet, UpdateData &updateData, 125 bool isCloud, bool isVideo); 126 static int32_t AddUpdateDataTask(const UpdateData &updateData); 127 static void UpdateCloudDataExecutor(AsyncTaskData *data); 128 static void UpdateAbnormaldata(std::unique_ptr<Metadata> &metadata, const std::string &tableName); 129 static void GetSizeAndMimeType(std::unique_ptr<Metadata> &metadata); 130 static int32_t GetExtractMetadata(std::unique_ptr<Metadata> &metadata); 131 static void StopUpdateData(); 132 static void UpdateCurrentOffset(bool isCloud, bool isVideo); 133 134 static int32_t processInterval_; 135 static int32_t downloadInterval_; 136 static int32_t downloadDuration_; 137 static std::recursive_mutex mutex_; 138 static Utils::Timer timer_; 139 static uint32_t cloudDataTimerId_; 140 static uint32_t startTimerId_; 141 static uint32_t stopTimerId_; 142 static bool isUpdating_; 143 static int32_t cloudUpdateOffset_; 144 static int32_t localImageUpdateOffset_; 145 static int32_t localVideoUpdateOffset_; 146 static int32_t cloudRetryCount_; 147 static std::mutex downloadResultMutex_; 148 static std::mutex repairMimeTypeMutex_; 149 static std::unordered_map<std::string, DownloadStatus> downloadResult_; 150 static int64_t downloadId_; 151 }; 152 } // namespace Media 153 } // namespace OHOS 154 #endif // OHOS_MEDIALIBRARY_BACKGROUND_CLOUD_FILE_PROCESSOR_H