1 /*
2  * Copyright (c) 2023-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 OHOS_CAMERA_DPS_PHOTO_JOB_REPOSITORY_H
17 #define OHOS_CAMERA_DPS_PHOTO_JOB_REPOSITORY_H
18 
19 #include "deferred_photo_job.h"
20 #include "iphoto_job_repository_listener.h"
21 #include "deferred_processing_service_ipc_interface_code.h"
22 #include "safe_map.h"
23 
24 namespace OHOS {
25 namespace CameraStandard {
26 namespace DeferredProcessing {
27 
28 class PhotoJobRepository {
29 public:
30     PhotoJobRepository(const int32_t userId);
31     ~PhotoJobRepository();
32     void AddDeferredJob(const std::string& imageId, bool discardable, DpsMetadata& metadata);
33     void RemoveDeferredJob(const std::string& imageId, bool restorable);
34     bool RequestJob(const std::string& imageId);
35     void CancelJob(const std::string& imageId);
36     void RestoreJob(const std::string& imageId);
37     void SetJobPending(const std::string imageId);
38     void SetJobRunning(const std::string imageId);
39     void SetJobCompleted(const std::string imageId);
40     void SetJobFailed(const std::string imageId);
41 
42     PhotoJobStatus GetJobStatus(const std::string& imageId);
43     DeferredPhotoJobPtr GetLowPriorityJob();
44     DeferredPhotoJobPtr GetNormalPriorityJob();
45     DeferredPhotoJobPtr GetHighPriorityJob();
46     int GetRunningJobCounts();
47     PhotoJobPriority GetJobPriority(std::string imageId);
48     PhotoJobPriority GetJobRunningPriority(std::string imageId);
49     void RegisterJobListener(std::weak_ptr<IPhotoJobRepositoryListener> listener);
50     int GetBackgroundJobSize();
51     int GetOfflineJobSize();
52     bool IsOfflineJob(std::string imageId);
53     bool HasUnCompletedBackgroundJob();
54 
55 private:
56     void NotifyJobChanged(bool priorityChanged, bool statusChanged, DeferredPhotoJobPtr jobPtr);
57     void UpdateRunningCountUnLocked(bool statusChanged, DeferredPhotoJobPtr jobPtr);
58     void UpdateJobQueueUnLocked(bool saved, DeferredPhotoJobPtr jobPtr);
59     DeferredPhotoJobPtr GetJobUnLocked(const std::string& imageId);
60     void RecordPriotyNum(bool priorityChanged, const DeferredPhotoJobPtr& jobPtr);
61     void ReportEvent(DeferredPhotoJobPtr jobPtr, DeferredProcessingServiceInterfaceCode event);
62 
63     std::recursive_mutex mutex_;
64     std::recursive_mutex jobListenersMutex_;
65     const int32_t userId_;
66     int runningNum_;
67     std::unordered_map<std::string, DeferredPhotoJobPtr> offlineJobMap_;
68     std::unordered_map<std::string, DeferredPhotoJobPtr> backgroundJobMap_;
69     std::list<DeferredPhotoJobPtr> offlineJobList_;
70     std::deque<DeferredPhotoJobPtr> jobQueue_;
71     std::vector<std::weak_ptr<IPhotoJobRepositoryListener>> jobListeners_;
72     SafeMap<PhotoJobPriority, int32_t> priotyToNum_ = {};
73 };
74 } // namespace DeferredProcessing
75 } // namespace CameraStandard
76 } // namespace OHOS
77 #endif // OHOS_CAMERA_DPS_PHOTO_JOB_REPOSITORY_H