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 #include "photo_job_repository.h"
17 
18 #include "dp_log.h"
19 #include "dps_event_report.h"
20 #include "events_monitor.h"
21 #include "steady_clock.h"
22 
23 namespace OHOS {
24 namespace CameraStandard {
25 namespace DeferredProcessing {
26 
PhotoJobRepository(const int32_t userId)27 PhotoJobRepository::PhotoJobRepository(const int32_t userId)
28     : userId_(userId),
29       runningNum_(0),
30       offlineJobMap_(),
31       backgroundJobMap_(),
32       offlineJobList_(),
33       jobQueue_(),
34       jobListeners_()
35 {
36     DP_DEBUG_LOG("entered, userid: %{public}d", userId_);
37     priotyToNum_.Insert(PhotoJobPriority::HIGH, 0);
38     priotyToNum_.Insert(PhotoJobPriority::LOW, 0);
39     priotyToNum_.Insert(PhotoJobPriority::NORMAL, 0);
40 }
41 
~PhotoJobRepository()42 PhotoJobRepository::~PhotoJobRepository()
43 {
44     DP_DEBUG_LOG("entered, userid: %{public}d", userId_);
45     offlineJobMap_.clear();
46     backgroundJobMap_.clear();
47     offlineJobList_.clear();
48     jobQueue_.clear();
49     jobListeners_.clear();
50     priotyToNum_.Clear();
51 }
52 
AddDeferredJob(const std::string & imageId,bool discardable,DpsMetadata & metadata)53 void PhotoJobRepository::AddDeferredJob(const std::string& imageId, bool discardable, DpsMetadata& metadata)
54 {
55     bool priorityChanged = false;
56     bool statusChanged = false;
57     DeferredPhotoJobPtr jobPtr = nullptr;
58     {
59         DP_INFO_LOG("entered, imageId: %{public}s, discardable: %{public}d", imageId.c_str(), discardable);
60         std::lock_guard<std::recursive_mutex> lock(mutex_);
61         DeferredPhotoJobPtr jobPtrFind = GetJobUnLocked(imageId);
62         if (jobPtrFind != nullptr) {
63             DP_INFO_LOG("already existed, imageId: %s", imageId.c_str());
64             return;
65         }
66 
67         jobPtr = std::make_shared<DeferredPhotoJob>(imageId, discardable, metadata);
68         int type;
69         metadata.Get(DEFERRED_PROCESSING_TYPE_KEY, type);
70         if (type == DeferredProcessingType::DPS_BACKGROUND) {
71             backgroundJobMap_.emplace(imageId, jobPtr);
72         } else {
73             DP_INFO_LOG("add offline job, imageId: %s", imageId.c_str());
74             offlineJobList_.push_back(jobPtr);
75             offlineJobMap_.emplace(imageId, jobPtr);
76             EventsMonitor::GetInstance().NotifyPhotoProcessSize(offlineJobList_.size());
77         }
78         jobPtr->SetPhotoJobType(type);
79         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::NORMAL);
80         statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::PENDING);
81         UpdateRunningCountUnLocked(statusChanged, jobPtr);
82     }
83 
84     NotifyJobChanged(priorityChanged, statusChanged, jobPtr);
85     RecordPriotyNum(priorityChanged, jobPtr);
86     ReportEvent(jobPtr, DeferredProcessingServiceInterfaceCode::DPS_ADD_IMAGE);
87     int32_t imageSize = static_cast<int32_t>(offlineJobList_.size() + backgroundJobMap_.size());
88     EventsMonitor::GetInstance().NotifyPhotoProcessSize(imageSize);
89 }
90 
RemoveDeferredJob(const std::string & imageId,bool restorable)91 void PhotoJobRepository::RemoveDeferredJob(const std::string& imageId, bool restorable)
92 {
93     bool priorityChanged = false;
94     bool statusChanged = false;
95     DeferredPhotoJobPtr jobPtr = nullptr;
96     {
97         DP_INFO_LOG("entered, imageId: %{public}s, restorable: %{public}d", imageId.c_str(), restorable);
98         std::lock_guard<std::recursive_mutex> lock(mutex_);
99         jobPtr = GetJobUnLocked(imageId);
100         if (jobPtr == nullptr) {
101             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
102             return;
103         }
104 
105         UpdateJobQueueUnLocked(false, jobPtr);
106         if (restorable) {
107             priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::LOW);
108             statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::PENDING);
109         } else {
110             priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::DELETED);
111             statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::DELETED);
112             if (backgroundJobMap_.count(imageId) == 1) {
113                 backgroundJobMap_.erase(imageId);
114                 DP_INFO_LOG("background job removed, imageId: %{public}s", imageId.c_str());
115             } else if (offlineJobMap_.count(imageId) == 1) {
116                 auto it = std::find_if(offlineJobList_.begin(), offlineJobList_.end(),
117                     [jobPtr](const auto& ptr) { return ptr == jobPtr; });
118                 if (it != offlineJobList_.end()) {
119                     offlineJobList_.erase(it);
120                 }
121                 offlineJobMap_.erase(imageId);
122                 DP_INFO_LOG("offline job removed, imageId: %{public}s", imageId.c_str());
123             }
124         }
125         UpdateRunningCountUnLocked(statusChanged, jobPtr);
126     }
127 
128     RecordPriotyNum(priorityChanged, jobPtr);
129     ReportEvent(jobPtr, DeferredProcessingServiceInterfaceCode::DPS_REMOVE_IMAGE);
130     int32_t imageSize = static_cast<int32_t>(offlineJobList_.size() + backgroundJobMap_.size());
131     EventsMonitor::GetInstance().NotifyPhotoProcessSize(imageSize);
132 }
133 
RequestJob(const std::string & imageId)134 bool PhotoJobRepository::RequestJob(const std::string& imageId)
135 {
136     bool priorityChanged = false;
137     bool statusChanged = false;
138     DeferredPhotoJobPtr jobPtr = nullptr;
139     {
140         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
141         std::lock_guard<std::recursive_mutex> lock(mutex_);
142         jobPtr = GetJobUnLocked(imageId);
143         if (jobPtr == nullptr) {
144             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
145             return false;
146         }
147 
148         UpdateJobQueueUnLocked(true, jobPtr);
149         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::HIGH);
150         if (jobPtr->GetCurStatus() == PhotoJobStatus::FAILED) {
151             DP_INFO_LOG("FAILED to PENDING, imageId: %{public}s", imageId.c_str());
152             statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::PENDING);
153         }
154     }
155 
156     NotifyJobChanged(priorityChanged, statusChanged, jobPtr);
157     RecordPriotyNum(priorityChanged, jobPtr);
158     return true;
159 }
160 
CancelJob(const std::string & imageId)161 void PhotoJobRepository::CancelJob(const std::string& imageId)
162 {
163     bool priorityChanged = false;
164     DeferredPhotoJobPtr jobPtr = nullptr;
165     {
166         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
167         std::lock_guard<std::recursive_mutex> lock(mutex_);
168         jobPtr = GetJobUnLocked(imageId);
169         if (jobPtr == nullptr) {
170             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
171             return;
172         }
173 
174         UpdateJobQueueUnLocked(false, jobPtr);
175         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::NORMAL);
176     }
177 
178     NotifyJobChanged(priorityChanged, false, jobPtr);
179     RecordPriotyNum(priorityChanged, jobPtr);
180     ReportEvent(jobPtr, DeferredProcessingServiceInterfaceCode::DPS_CANCEL_PROCESS_IMAGE);
181 }
182 
RestoreJob(const std::string & imageId)183 void PhotoJobRepository::RestoreJob(const std::string& imageId)
184 {
185     bool priorityChanged = false;
186     DeferredPhotoJobPtr jobPtr = nullptr;
187     {
188         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
189         std::lock_guard<std::recursive_mutex> lock(mutex_);
190         jobPtr = GetJobUnLocked(imageId);
191         if (jobPtr == nullptr) {
192             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
193             return;
194         }
195 
196         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::NORMAL);
197     }
198 
199     NotifyJobChanged(priorityChanged, false, jobPtr);
200     RecordPriotyNum(priorityChanged, jobPtr);
201     ReportEvent(jobPtr, DeferredProcessingServiceInterfaceCode::DPS_RESTORE_IMAGE);
202 }
203 
SetJobPending(const std::string imageId)204 void PhotoJobRepository::SetJobPending(const std::string imageId)
205 {
206     bool statusChanged = false;
207     DeferredPhotoJobPtr jobPtr = nullptr;
208     {
209         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
210         std::lock_guard<std::recursive_mutex> lock(mutex_);
211         jobPtr = GetJobUnLocked(imageId);
212         if (jobPtr == nullptr) {
213             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
214             return;
215         }
216 
217         statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::PENDING);
218         UpdateRunningCountUnLocked(statusChanged, jobPtr);
219     }
220 
221     NotifyJobChanged(false, statusChanged, jobPtr);
222 }
223 
SetJobRunning(const std::string imageId)224 void PhotoJobRepository::SetJobRunning(const std::string imageId)
225 {
226     bool statusChanged = false;
227     DeferredPhotoJobPtr jobPtr = nullptr;
228     {
229         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
230         std::lock_guard<std::recursive_mutex> lock(mutex_);
231         jobPtr = GetJobUnLocked(imageId);
232         if (jobPtr == nullptr) {
233             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
234             return;
235         }
236 
237         statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::RUNNING);
238         jobPtr->RecordJobRunningPriority();
239         UpdateRunningCountUnLocked(statusChanged, jobPtr);
240     }
241 
242     ReportEvent(jobPtr, DeferredProcessingServiceInterfaceCode::DPS_PROCESS_IMAGE);
243 }
244 
SetJobCompleted(const std::string imageId)245 void PhotoJobRepository::SetJobCompleted(const std::string imageId)
246 {
247     bool priorityChanged = false;
248     bool statusChanged = false;
249     DeferredPhotoJobPtr jobPtr = nullptr;
250     {
251         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
252         std::lock_guard<std::recursive_mutex> lock(mutex_);
253         jobPtr = GetJobUnLocked(imageId);
254         if (jobPtr == nullptr) {
255             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
256             return;
257         }
258 
259         UpdateJobQueueUnLocked(false, jobPtr);
260         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::NORMAL);
261         statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::COMPLETED);
262         UpdateRunningCountUnLocked(statusChanged, jobPtr);
263     }
264 
265     NotifyJobChanged(priorityChanged, statusChanged, jobPtr);
266     RecordPriotyNum(priorityChanged, jobPtr);
267 }
268 
SetJobFailed(const std::string imageId)269 void PhotoJobRepository::SetJobFailed(const std::string imageId)
270 {
271     bool priorityChanged = false;
272     bool statusChanged = false;
273     DeferredPhotoJobPtr jobPtr = nullptr;
274     {
275         DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
276         std::lock_guard<std::recursive_mutex> lock(mutex_);
277         jobPtr = GetJobUnLocked(imageId);
278         if (jobPtr == nullptr) {
279             DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
280             return;
281         }
282 
283         UpdateJobQueueUnLocked(false, jobPtr);
284         priorityChanged = jobPtr->SetJobPriority(PhotoJobPriority::NORMAL);
285         statusChanged = jobPtr->SetJobStatus(PhotoJobStatus::FAILED);
286         UpdateRunningCountUnLocked(statusChanged, jobPtr);
287     }
288 
289     NotifyJobChanged(priorityChanged, statusChanged, jobPtr);
290     RecordPriotyNum(priorityChanged, jobPtr);
291 }
292 
GetJobStatus(const std::string & imageId)293 PhotoJobStatus PhotoJobRepository::GetJobStatus(const std::string& imageId)
294 {
295     DP_INFO_LOG("entered, imageId: %{public}s", imageId.c_str());
296     std::lock_guard<std::recursive_mutex> lock(mutex_);
297     DeferredPhotoJobPtr jobPtr = GetJobUnLocked(imageId);
298     if (jobPtr == nullptr) {
299         DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
300         return PhotoJobStatus::NONE;
301     } else {
302         return jobPtr->GetCurStatus();
303     }
304 }
305 
GetLowPriorityJob()306 DeferredPhotoJobPtr PhotoJobRepository::GetLowPriorityJob()
307 {
308     DP_INFO_LOG("entered, job queue size: %{public}d, offline job list size: %{public}d,"
309         "background job size: %{public}d, running num: %{public}d",
310         static_cast<int>(jobQueue_.size()), static_cast<int>(offlineJobList_.size()),
311         static_cast<int>(backgroundJobMap_.size()), runningNum_);
312     std::lock_guard<std::recursive_mutex> lock(mutex_);
313     auto it = std::find_if(offlineJobList_.begin(), offlineJobList_.end(), [](auto& jobPtr) {
314         return (jobPtr->GetCurPriority() == PhotoJobPriority::LOW) &&
315             (jobPtr->GetCurStatus() == PhotoJobStatus::PENDING);
316     });
317     if (it != offlineJobList_.end()) {
318         return *it;
319     }
320     it = std::find_if(offlineJobList_.begin(), offlineJobList_.end(), [](auto& jobPtr) {
321         return jobPtr->GetCurPriority() == PhotoJobPriority::LOW &&
322             jobPtr->GetCurStatus() == PhotoJobStatus::FAILED;
323     });
324     return it != offlineJobList_.end() ? *it : nullptr;
325 }
326 
GetNormalPriorityJob()327 DeferredPhotoJobPtr PhotoJobRepository::GetNormalPriorityJob()
328 {
329     DP_INFO_LOG("entered, job queue size: %{public}d, offline job list size: %{public}d,"
330         "background job size: %{public}d, running num: %{public}d",
331         static_cast<int>(jobQueue_.size()), static_cast<int>(offlineJobList_.size()),
332         static_cast<int>(backgroundJobMap_.size()), runningNum_);
333     std::lock_guard<std::recursive_mutex> lock(mutex_);
334     auto it = std::find_if(offlineJobList_.begin(), offlineJobList_.end(), [](auto& jobPtr) {
335         return (jobPtr->GetCurPriority() == PhotoJobPriority::NORMAL) &&
336             (jobPtr->GetCurStatus() == PhotoJobStatus::PENDING);
337     });
338     if (it != offlineJobList_.end()) {
339         return *it;
340     }
341     DP_INFO_LOG("no job pending, try reset failed to pending");
342     for (auto& jobPtr : offlineJobList_) {
343         if ((jobPtr->GetCurPriority() == PhotoJobPriority::NORMAL) &&
344             (jobPtr->GetCurStatus() == PhotoJobStatus::FAILED)) {
345             jobPtr->SetJobStatus(PhotoJobStatus::PENDING);
346         }
347     }
348     it = std::find_if(offlineJobList_.begin(), offlineJobList_.end(), [](auto& jobPtr) {
349         return (jobPtr->GetCurPriority() == PhotoJobPriority::NORMAL) &&
350             (jobPtr->GetCurStatus() == PhotoJobStatus::PENDING);
351     });
352     return it != offlineJobList_.end() ? *it : nullptr;
353 }
354 
GetHighPriorityJob()355 DeferredPhotoJobPtr PhotoJobRepository::GetHighPriorityJob()
356 {
357     DP_INFO_LOG("entered, job queue size: %{public}d, offline job list size: %{public}d,"
358         "background job size: %{public}d, running num: %{public}d",
359         static_cast<int>(jobQueue_.size()), static_cast<int>(offlineJobList_.size()),
360         static_cast<int>(backgroundJobMap_.size()), runningNum_);
361     std::lock_guard<std::recursive_mutex> lock(mutex_);
362     auto it = std::find_if(jobQueue_.begin(), jobQueue_.end(), [](auto& jobPtr) {
363         return jobPtr->GetCurStatus() == PhotoJobStatus::PENDING;
364     });
365     return it != jobQueue_.end() ? *it : nullptr;
366 }
367 
GetRunningJobCounts()368 int PhotoJobRepository::GetRunningJobCounts()
369 {
370     std::lock_guard<std::recursive_mutex> lock(mutex_);
371     DP_INFO_LOG("running jobs num: %{public}d", runningNum_);
372     return runningNum_;
373 }
374 
GetJobPriority(std::string imageId)375 PhotoJobPriority PhotoJobRepository::GetJobPriority(std::string imageId)
376 {
377     DP_INFO_LOG("entered");
378     std::lock_guard<std::recursive_mutex> lock(mutex_);
379     DeferredPhotoJobPtr jobPtr = GetJobUnLocked(imageId);
380     if (jobPtr == nullptr) {
381         DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
382         return PhotoJobPriority::NONE;
383     } else {
384         return jobPtr->GetCurPriority();
385     }
386 }
387 
GetJobRunningPriority(std::string imageId)388 PhotoJobPriority PhotoJobRepository::GetJobRunningPriority(std::string imageId)
389 {
390     DP_INFO_LOG("entered");
391     std::lock_guard<std::recursive_mutex> lock(mutex_);
392     DeferredPhotoJobPtr jobPtr = GetJobUnLocked(imageId);
393     if (jobPtr == nullptr) {
394         DP_INFO_LOG("does not existed, imageId: %{public}s", imageId.c_str());
395         return PhotoJobPriority::NONE;
396     } else {
397         return jobPtr->GetRunningPriority();
398     }
399 }
400 
NotifyJobChanged(bool priorityChanged,bool statusChanged,DeferredPhotoJobPtr jobPtr)401 void PhotoJobRepository::NotifyJobChanged(bool priorityChanged, bool statusChanged, DeferredPhotoJobPtr jobPtr)
402 {
403     DP_INFO_LOG("entered, priorityChanged: %{public}d, statusChanged: %{public}d, imageId: %{public}s",
404         priorityChanged, statusChanged, jobPtr->GetImageId().c_str());
405     DP_INFO_LOG("jobListenersMutex begin");
406     std::lock_guard<std::recursive_mutex> lock(jobListenersMutex_);
407     for (auto& listenerWptr : jobListeners_) {
408         if (auto listenerSptr = listenerWptr.lock()) {
409             listenerSptr->OnPhotoJobChanged(priorityChanged, statusChanged, jobPtr);
410         }
411     }
412     DP_INFO_LOG("jobListenersMutex end");
413 }
414 
UpdateRunningCountUnLocked(bool statusChanged,DeferredPhotoJobPtr jobPtr)415 void PhotoJobRepository::UpdateRunningCountUnLocked(bool statusChanged, DeferredPhotoJobPtr jobPtr)
416 {
417     if (statusChanged && (jobPtr->GetPreStatus() == PhotoJobStatus::RUNNING)) {
418         runningNum_ = runningNum_ - 1;
419     }
420     if (statusChanged && (jobPtr->GetCurStatus() == PhotoJobStatus::RUNNING)) {
421         runningNum_ = runningNum_ + 1;
422     }
423     DP_INFO_LOG("running jobs num: %{public}d, imageId: %s", runningNum_, jobPtr->GetImageId().c_str());
424     return;
425 }
426 
UpdateJobQueueUnLocked(bool saved,DeferredPhotoJobPtr jobPtr)427 void PhotoJobRepository::UpdateJobQueueUnLocked(bool saved, DeferredPhotoJobPtr jobPtr)
428 {
429     if (saved) {
430         auto it = std::find_if(jobQueue_.begin(), jobQueue_.end(), [jobPtr](const auto& ptr) { return ptr == jobPtr; });
431         if (it != jobQueue_.end()) {
432             jobQueue_.erase(it);
433             DP_INFO_LOG("already existed, move to front, imageId: %s", jobPtr->GetImageId().c_str());
434         }
435         //最新的请求最先处理,所以要放到队首。GetHighPriorityJob取任务从队首取。如果确认是这样顺序,则应该用栈保存
436         jobQueue_.push_front(jobPtr);
437     } else {
438         auto it = std::find_if(jobQueue_.begin(), jobQueue_.end(), [jobPtr](const auto& ptr) { return ptr == jobPtr; });
439         if (it != jobQueue_.end()) {
440             DP_INFO_LOG("erase high priority, imageId: %s", jobPtr->GetImageId().c_str());
441             jobQueue_.erase(it);
442         } else {
443             DP_INFO_LOG("already not high priority, imageId: %s", jobPtr->GetImageId().c_str());
444         }
445     }
446 }
447 
RegisterJobListener(std::weak_ptr<IPhotoJobRepositoryListener> listener)448 void PhotoJobRepository::RegisterJobListener(std::weak_ptr<IPhotoJobRepositoryListener> listener)
449 {
450     DP_INFO_LOG("entered, jobListenersMutex begin");
451     std::lock_guard<std::recursive_mutex> lock(jobListenersMutex_);
452     jobListeners_.emplace_back(listener);
453     DP_INFO_LOG("jobListenersMutex end");
454 }
455 
GetJobUnLocked(const std::string & imageId)456 DeferredPhotoJobPtr PhotoJobRepository::GetJobUnLocked(const std::string& imageId)
457 {
458     DeferredPhotoJobPtr jobPtr = nullptr;
459     if (backgroundJobMap_.count(imageId) == 1) {
460         DP_INFO_LOG("background job, imageId: %s", imageId.c_str());
461         jobPtr = backgroundJobMap_.find(imageId)->second;
462     } else if (offlineJobMap_.count(imageId) == 1) {
463         DP_INFO_LOG("offline job, imageId: %s", imageId.c_str());
464         jobPtr = offlineJobMap_.find(imageId)->second;
465     }
466     return jobPtr;
467 }
468 
GetBackgroundJobSize()469 int PhotoJobRepository::GetBackgroundJobSize()
470 {
471     std::lock_guard<std::recursive_mutex> lock(mutex_);
472     int size = static_cast<int>(backgroundJobMap_.size());
473     DP_DEBUG_LOG("background job size: %{public}d", size);
474     return size;
475 }
476 
GetOfflineJobSize()477 int PhotoJobRepository::GetOfflineJobSize()
478 {
479     std::lock_guard<std::recursive_mutex> lock(mutex_);
480     int size = static_cast<int>(offlineJobMap_.size());
481     DP_DEBUG_LOG("offline job size: %{public}d", size);
482     return size;
483 }
484 
IsOfflineJob(std::string imageId)485 bool PhotoJobRepository::IsOfflineJob(std::string imageId)
486 {
487     DP_DEBUG_LOG("entered");
488     std::lock_guard<std::recursive_mutex> lock(mutex_);
489     if (offlineJobMap_.count(imageId) == 1) {
490         return true;
491     } else {
492         return false;
493     }
494 }
495 
HasUnCompletedBackgroundJob()496 bool PhotoJobRepository::HasUnCompletedBackgroundJob()
497 {
498     auto it = std::find_if(backgroundJobMap_.begin(), backgroundJobMap_.end(), [](auto& ptr) {
499         return ptr.second->GetCurStatus() == PhotoJobStatus::PENDING;
500     });
501     return it != backgroundJobMap_.end();
502 }
503 
RecordPriotyNum(bool priorityChanged,const DeferredPhotoJobPtr & jobPtr)504 void PhotoJobRepository::RecordPriotyNum(bool priorityChanged, const DeferredPhotoJobPtr& jobPtr)
505 {
506     DP_CHECK_RETURN(!priorityChanged);
507     int32_t oldNum;
508     priotyToNum_.FindOldAndSetNew(jobPtr->GetCurPriority(), oldNum, oldNum + 1);
509     priotyToNum_.FindOldAndSetNew(jobPtr->GetPrePriority(), oldNum, oldNum - 1);
510 }
511 
ReportEvent(DeferredPhotoJobPtr jobPtr,DeferredProcessingServiceInterfaceCode event)512 void PhotoJobRepository::ReportEvent(DeferredPhotoJobPtr jobPtr, DeferredProcessingServiceInterfaceCode event)
513 {
514     int32_t highJobNum = priotyToNum_.ReadVal(PhotoJobPriority::HIGH);
515     int32_t normalJobNum = priotyToNum_.ReadVal(PhotoJobPriority::NORMAL);
516     int32_t lowJobNum = priotyToNum_.ReadVal(PhotoJobPriority::LOW);
517     std::string imageId = jobPtr->GetImageId();
518     DPSEventInfo dpsEventInfo;
519     dpsEventInfo.imageId = imageId;
520     dpsEventInfo.userId = userId_;
521     dpsEventInfo.lowJobNum = lowJobNum;
522     dpsEventInfo.normalJobNum = normalJobNum;
523     dpsEventInfo.highJobNum = highJobNum;
524     dpsEventInfo.discardable = jobPtr->GetDiscardable();
525     dpsEventInfo.photoJobType = static_cast<PhotoJobType>(jobPtr->GetPhotoJobType());
526     dpsEventInfo.operatorStage = event;
527     uint64_t endTime = SteadyClock::GetTimestampMilli();
528     switch (static_cast<int32_t>(event)) {
529         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_ADD_IMAGE): {
530             dpsEventInfo.dispatchTimeEndTime = endTime;
531             break;
532         }
533         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_REMOVE_IMAGE): {
534             dpsEventInfo.removeTimeBeginTime = endTime;
535             break;
536         }
537         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_RESTORE_IMAGE): {
538             dpsEventInfo.restoreTimeBeginTime = endTime;
539             break;
540         }
541         case static_cast<int32_t>(DeferredProcessingServiceInterfaceCode::DPS_PROCESS_IMAGE): {
542             dpsEventInfo.processTimeBeginTime = endTime;
543             break;
544         }
545     }
546     DPSEventReport::GetInstance().ReportOperateImage(imageId, userId_, dpsEventInfo);
547 }
548 } // namespace DeferredProcessing
549 } // namespace CameraStandard
550 } // namespace OHOS