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