1 /*
2 * Copyright (c) 2021-2022 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 <cinttypes>
17
18 #include "form_constants.h"
19 #include "hilog_wrapper.h"
20 #include "form_refresh_limiter.h"
21
22 namespace OHOS {
23 namespace AppExecFwk {
24 /**
25 * @brief Add form limit info by formId.
26 * @param formId The id of the form.
27 * @return Returns true on success, false on failure.
28 */
AddItem(const int64_t formId)29 bool FormRefreshLimiter::AddItem(const int64_t formId)
30 {
31 HILOG_INFO("%{public}s start", __func__);
32 std::lock_guard<std::mutex> lock(limiterMutex_);
33 auto info = limiterMap_.find(formId);
34 if (info == limiterMap_.end()) {
35 LimitInfo limitInfo;
36 auto retVal = limiterMap_.emplace(formId, limitInfo);
37 HILOG_INFO("%{public}s end", __func__);
38 return retVal.second;
39 } else {
40 HILOG_INFO("%{public}s end, already exist", __func__);
41 return true;
42 }
43 }
44 /**
45 * @brief Delete form limit info by formId.
46 * @param formId The form id.
47 */
DeleteItem(const int64_t formId)48 void FormRefreshLimiter::DeleteItem(const int64_t formId)
49 {
50 HILOG_INFO("%{public}s start", __func__);
51 std::lock_guard<std::mutex> lock(limiterMutex_);
52 auto info = limiterMap_.find(formId);
53 if (info != limiterMap_.end()) {
54 limiterMap_.erase(formId);
55 }
56 HILOG_INFO("%{public}s end", __func__);
57 }
58 /**
59 * @brief Reset limit info.
60 */
ResetLimit()61 void FormRefreshLimiter::ResetLimit()
62 {
63 HILOG_INFO("%{public}s start", __func__);
64 std::lock_guard<std::mutex> lock(limiterMutex_);
65 for (auto &infoPair : limiterMap_) {
66 infoPair.second.refreshCount = 0;
67 infoPair.second.isReported = false;
68 infoPair.second.remindFlag = false;
69 }
70 HILOG_INFO("%{public}s end", __func__);
71 }
72 /**
73 * @brief Refresh enable or not.
74 * @param formId The form id.
75 * @return Returns ERR_OK on success, others on failure.
76 */
IsEnableRefresh(const int64_t formId)77 bool FormRefreshLimiter::IsEnableRefresh(const int64_t formId)
78 {
79 HILOG_INFO("%{public}s start", __func__);
80 bool isEnable = false;
81 std::lock_guard<std::mutex> lock(limiterMutex_);
82 auto info = limiterMap_.find(formId);
83 if (info != limiterMap_.end()) {
84 if (info->second.refreshCount < Constants::LIMIT_COUNT) {
85 isEnable = true;
86 }
87
88 if (info->second.refreshCount == Constants::LIMIT_COUNT && !info->second.isReported) {
89 info->second.isReported = true;
90 HILOG_INFO("report refresh to 50 count, formId:%{public}" PRId64 "", formId);
91 }
92 }
93 HILOG_INFO("%{public}s end", __func__);
94 return isEnable;
95 }
96 /**
97 * @brief Get refresh count.
98 * @param formId The form id.
99 * @return refresh count.
100 */
GetRefreshCount(const int64_t formId) const101 int FormRefreshLimiter::GetRefreshCount(const int64_t formId) const
102 {
103 HILOG_INFO("%{public}s start", __func__);
104 // -1 means not added or already removed.
105 std::lock_guard<std::mutex> lock(limiterMutex_);
106 auto info = limiterMap_.find(formId);
107 if (info != limiterMap_.end()) {
108 return info->second.refreshCount;
109 }
110
111 HILOG_INFO("%{public}s end", __func__);
112 return -1;
113 }
114 /**
115 * @brief Increase refresh count.
116 * @param formId The form id.
117 */
Increase(const int64_t formId)118 void FormRefreshLimiter::Increase(const int64_t formId)
119 {
120 HILOG_INFO("%{public}s start", __func__);
121 std::lock_guard<std::mutex> lock(limiterMutex_);
122 auto info = limiterMap_.find(formId);
123 if (info != limiterMap_.end()) {
124 info->second.refreshCount++;
125 HILOG_INFO("increase,formId:%{public}" PRId64 ", count:%{public}d", formId, info->second.refreshCount);
126 if (info->second.refreshCount == Constants::LIMIT_COUNT && !info->second.isReported) {
127 info->second.isReported = true;
128 HILOG_INFO("report refresh to 50 count,formId:%{public}" PRId64 "", formId);
129 }
130 }
131 HILOG_INFO("%{public}s end", __func__);
132 }
133 /**
134 * @brief Mark remind flag.
135 * @param formId The form id.
136 */
MarkRemind(const int64_t formId)137 void FormRefreshLimiter::MarkRemind(const int64_t formId)
138 {
139 HILOG_INFO("%{public}s start", __func__);
140 std::lock_guard<std::mutex> lock(limiterMutex_);
141 auto info = limiterMap_.find(formId);
142 if (info != limiterMap_.end()) {
143 if (info->second.refreshCount >= Constants::LIMIT_COUNT) {
144 info->second.remindFlag = true;
145 }
146 }
147 HILOG_INFO("%{public}s end", __func__);
148 }
149 /**
150 * @brief Get remind list.
151 * @return remind list.
152 */
GetRemindList() const153 std::vector<int64_t> FormRefreshLimiter::GetRemindList() const
154 {
155 HILOG_INFO("%{public}s start", __func__);
156 std::vector<int64_t> result;
157 std::lock_guard<std::mutex> lock(limiterMutex_);
158 for (auto &infoPair : limiterMap_) {
159 if (infoPair.second.remindFlag) {
160 result.emplace_back(infoPair.first);
161 }
162 }
163 HILOG_INFO("%{public}s end", __func__);
164 return result;
165 }
166 /**
167 * @brief Get remind list and reset limit.
168 * @return remind list.
169 */
GetRemindListAndResetLimit()170 std::vector<int64_t> FormRefreshLimiter::GetRemindListAndResetLimit()
171 {
172 HILOG_INFO("%{public}s start", __func__);
173 std::vector<int64_t> result;
174 std::lock_guard<std::mutex> lock(limiterMutex_);
175 for (auto &infoPair : limiterMap_) {
176 if (infoPair.second.remindFlag) {
177 result.emplace_back(infoPair.first);
178 }
179
180 infoPair.second.refreshCount = 0;
181 infoPair.second.isReported = false;
182 infoPair.second.remindFlag = false;
183 }
184 HILOG_INFO("%{public}s end", __func__);
185 return result;
186 }
187 /**
188 * @brief Get item count.
189 * @return Item count.
190 */
GetItemCount() const191 int FormRefreshLimiter::GetItemCount() const
192 {
193 std::lock_guard<std::mutex> lock(limiterMutex_);
194 return limiterMap_.size();
195 }
196 } // namespace AppExecFwk
197 } // namespace OHOS
198