1 /*
2 * Copyright (c) 2025 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 "status_mgr_center/form_event_retry_mgr.h"
17 #include "fms_log_wrapper.h"
18
19 namespace OHOS {
20 namespace AppExecFwk {
FormEventRetryMgr()21 FormEventRetryMgr::FormEventRetryMgr()
22 {
23 HILOG_DEBUG("create FormEventRetryMgr");
24 }
25
~FormEventRetryMgr()26 FormEventRetryMgr::~FormEventRetryMgr()
27 {
28 HILOG_DEBUG("destroy FormEventRetryMgr");
29 }
30
GetLastFormEvent(const int64_t formId,FormEventTaskInfo & formEventInfo)31 bool FormEventRetryMgr::GetLastFormEvent(const int64_t formId, FormEventTaskInfo &formEventInfo)
32 {
33 std::shared_lock<std::shared_mutex> lock(lastFormEventMutex_);
34 auto iter = lastFormEventMap_.find(formId);
35 if (iter == lastFormEventMap_.end()) {
36 HILOG_ERROR("lastFormEvent not exist, formId:%{public}" PRId64 ". ", formId);
37 return false;
38 }
39
40 formEventInfo = iter->second;
41 return true;
42 }
43
SetLastFormEvent(const int64_t formId,const FormEventTaskInfo formEventInfo)44 void FormEventRetryMgr::SetLastFormEvent(const int64_t formId, const FormEventTaskInfo formEventInfo)
45 {
46 std::unique_lock<std::shared_mutex> lock(lastFormEventMutex_);
47 auto iter = lastFormEventMap_.find(formId);
48 if (iter == lastFormEventMap_.end()) {
49 HILOG_INFO("set new lastFormEvent, formId:%{public}" PRId64 ".", formId);
50 lastFormEventMap_.emplace(formId, formEventInfo);
51 return;
52 }
53
54 iter->second = formEventInfo;
55 HILOG_INFO("set lastFormEvent, formId:%{public}" PRId64 ".", formId);
56 }
57
DeleteLastFormEvent(const int64_t formId)58 void FormEventRetryMgr::DeleteLastFormEvent(const int64_t formId)
59 {
60 std::unique_lock<std::shared_mutex> lock(lastFormEventMutex_);
61 auto iter = lastFormEventMap_.find(formId);
62 if (iter != lastFormEventMap_.end()) {
63 HILOG_INFO("delete last form event, formId:%{public}" PRId64 ". ", formId);
64 lastFormEventMap_.erase(iter);
65 }
66 }
67
GetRetryCount(const int64_t formId,int32_t & retryCount)68 bool FormEventRetryMgr::GetRetryCount(const int64_t formId, int32_t &retryCount)
69 {
70 std::shared_lock<std::shared_mutex> lock(retryCountMutex_);
71 auto iter = retryCountMap_.find(formId);
72 if (iter == retryCountMap_.end()) {
73 HILOG_ERROR("retryCount not exist, formId:%{public}" PRId64 ". ", formId);
74 return false;
75 }
76
77 retryCount = iter->second;
78 return true;
79 }
80
SetRetryCount(const int64_t formId,const int32_t retryCount)81 void FormEventRetryMgr::SetRetryCount(const int64_t formId, const int32_t retryCount)
82 {
83 std::unique_lock<std::shared_mutex> lock(retryCountMutex_);
84 auto iter = retryCountMap_.find(formId);
85 if (iter == retryCountMap_.end()) {
86 HILOG_INFO("set new retryCount, formId:%{public}" PRId64 ".", formId);
87 retryCountMap_.emplace(formId, retryCount);
88 return;
89 }
90
91 iter->second = retryCount;
92 HILOG_INFO("set retryCount, formId:%{public}" PRId64 ".", formId);
93 }
94
DeleteRetryCount(const int64_t formId)95 void FormEventRetryMgr::DeleteRetryCount(const int64_t formId)
96 {
97 std::unique_lock<std::shared_mutex> lock(retryCountMutex_);
98 auto iter = retryCountMap_.find(formId);
99 if (iter != retryCountMap_.end()) {
100 HILOG_INFO("delete retry count, formId:%{public}" PRId64 ". ", formId);
101 retryCountMap_.erase(iter);
102 }
103 }
104 } // namespace AppExecFwk
105 } // namespace OHOS
106