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