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_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H
17 #define OHOS_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H
18 
19 #include <iostream>
20 #include <thread>
21 #include <mutex>
22 #include <condition_variable>
23 #include <queue>
24 #include <atomic>
25 #include <memory>
26 #include <chrono>
27 
28 #include "cloud_media_asset_callback.h"
29 #include "cloud_media_asset_types.h"
30 #include "cloud_media_asset_observer.h"
31 #include "cloud_sync_common.h"
32 #include "cloud_sync_manager.h"
33 #include "datashare_helper.h"
34 #include "medialibrary_command.h"
35 #include "medialibrary_rdbstore.h"
36 
37 namespace OHOS {
38 namespace Media {
39 #define EXPORT __attribute__ ((visibility ("default")))
40 using namespace FileManagement::CloudSync;
41 
42 class CloudDeathRecipient : public IRemoteObject::DeathRecipient {
43 public:
CloudDeathRecipient(std::shared_ptr<CloudMediaAssetDownloadOperation> operation)44     CloudDeathRecipient(std::shared_ptr<CloudMediaAssetDownloadOperation> operation) : operation_(operation) {}
~CloudDeathRecipient()45     ~CloudDeathRecipient() {}
46     void OnRemoteDied(const wptr<IRemoteObject> &object);
47 
48 private:
49     std::shared_ptr<CloudMediaAssetDownloadOperation> operation_ = nullptr;
50 };
51 
52 class CloudMediaAssetDownloadOperation {
53 public:
54     struct DownloadFileData {
55         std::vector<std::string> pathVec;
56         std::map<std::string, int64_t> fileDownloadMap;
57         std::vector<std::string> batchFileIdNeedDownload;
58         int64_t batchSizeNeedDownload = 0;
59         int64_t batchCountNeedDownload = 0;
60     };
61 
62     enum class Status : int32_t {
63         FORCE_DOWNLOADING,
64         GENTLE_DOWNLOADING,
65         PAUSE_FOR_TEMPERATURE_LIMIT,
66         PAUSE_FOR_ROM_LIMIT,
67         PAUSE_FOR_NETWORK_FLOW_LIMIT,
68         PAUSE_FOR_WIFI_UNAVAILABLE,
69         PAUSE_FOR_POWER_LIMIT,
70         PAUSE_FOR_BACKGROUND_TASK_UNAVAILABLE,
71         PAUSE_FOR_FREQUENT_USER_REQUESTS,
72         PAUSE_FOR_CLOUD_ERROR,
73         PAUSE_FOR_USER_PAUSE,
74         RECOVER_FOR_MANAUL_ACTIVE,
75         RECOVER_FOR_PASSIVE_STATUS,
76         IDLE,
77     };
78 
CloudMediaAssetDownloadOperation()79     CloudMediaAssetDownloadOperation() {}
~CloudMediaAssetDownloadOperation()80     ~CloudMediaAssetDownloadOperation() {}
81     CloudMediaAssetDownloadOperation(const CloudMediaAssetDownloadOperation &) = delete;
82     CloudMediaAssetDownloadOperation& operator=(const CloudMediaAssetDownloadOperation &) = delete;
83 
84     EXPORT static std::shared_ptr<CloudMediaAssetDownloadOperation> GetInstance();
85     EXPORT int32_t StartDownloadTask(int32_t cloudMediaDownloadType);
86     EXPORT int32_t PauseDownloadTask(const CloudMediaTaskPauseCause &pauseCause);
87     EXPORT int32_t CancelDownloadTask();
88     EXPORT int32_t ManualActiveRecoverTask(int32_t cloudMediaDownloadType);
89     EXPORT int32_t PassiveStatusRecoverTask(const CloudMediaTaskRecoverCause &recoverCause);
90 
91     EXPORT void HandleSuccessCallback(const DownloadProgressObj &progress);
92     EXPORT void HandleFailedCallback(const DownloadProgressObj &progress);
93     EXPORT void HandleStoppedCallback(const DownloadProgressObj &progress);
94 
95     EXPORT CloudMediaDownloadType GetDownloadType();
96     EXPORT CloudMediaAssetTaskStatus GetTaskStatus();
97     EXPORT CloudMediaTaskPauseCause GetTaskPauseCause();
98     EXPORT std::string GetTaskInfo();
99     EXPORT int32_t InitDownloadTaskInfo();
100 
101 private:
102     void ClearData(DownloadFileData &data);
103     bool IsDataEmpty(const DownloadFileData &data);
104     int32_t DoRelativedRegister();
105     int32_t SetDeathRecipient();
106     bool IsProperFgTemperature();
107     void InitStartDownloadTaskStatus(const bool &isForeground);
108     void ResetParameter();
109     bool IsNetworkUnavailable();
110 
111     void SetTaskStatus(Status status);
112     std::shared_ptr<NativeRdb::ResultSet> QueryDownloadFilesNeeded(const bool &isQueryInfo);
113     DownloadFileData ReadyDataForBatchDownload();
114     EXPORT int32_t DoForceTaskExecute();
115     EXPORT int32_t SubmitBatchDownload(DownloadFileData &data, const bool &isCache);
116     void StartFileCacheFailed(const int64_t batchNum, const int64_t batchSize);
117     void StartBatchDownload(const int64_t batchNum, const int64_t batchSize);
118     EXPORT int32_t DoRecoverExecute();
119     EXPORT int32_t PassiveStatusRecover();
120     int32_t SubmitBatchDownloadAgain();
121     void MoveDownloadFileToCache(const DownloadProgressObj &progress);
122     void MoveDownloadFileToNotFound(const DownloadProgressObj &progress);
123 
124 public:
125     static std::shared_ptr<CloudMediaAssetDownloadOperation> instance_;
126 
127     // Confirmation of the notification
128     bool isThumbnailUpdate_ = true;
129     bool isBgDownloadPermission_ = false;
130     bool isUnlimitedTrafficStatusOn_ = false;
131 
132 private:
133     std::reference_wrapper<CloudSyncManager> cloudSyncManager_ = CloudSyncManager::GetInstance();
134     CloudMediaAssetTaskStatus taskStatus_ = CloudMediaAssetTaskStatus::IDLE;
135     CloudMediaDownloadType downloadType_;
136     CloudMediaTaskPauseCause pauseCause_ = CloudMediaTaskPauseCause::NO_PAUSE;
137     std::shared_ptr<DataShare::DataShareHelper> cloudHelper_;
138     std::shared_ptr<CloudMediaAssetObserver> cloudMediaAssetObserver_;
139     std::shared_ptr<MediaCloudDownloadCallback> downloadCallback_;
140     OHOS::sptr<OHOS::IRemoteObject> cloudRemoteObject_;
141     static std::mutex mutex_;
142     static std::mutex callbackMutex_;
143 
144     DownloadFileData readyForDownload_;
145     DownloadFileData notFoundForDownload_;
146 
147     // data cache
148     DownloadFileData cacheForDownload_;
149 
150     // data downloading
151     bool isCache_;
152     int64_t downloadId_ = -1;
153     DownloadFileData dataForDownload_;
154 
155     // common info
156     int64_t totalCount_ = 0;
157     int64_t totalSize_ = 0;
158     int64_t remainCount_ = 0;
159     int64_t remainSize_ = 0;
160 };
161 } // namespace Media
162 } // namespace OHOS
163 #endif // OHOS_CLOUD_MEDIA_ASSET_DOWNLOAD_OPERATION_H