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 "form_refresh_limiter.h"
21 #include "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 if (info->second.refreshCount < Constants::LIMIT_COUNT) {
84 FormRecordReport::GetInstance().IncreaseUpdateTimes(formId, HiSysEventPointType::TYPE_HIGH_FREQUENCY);
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_DEBUG("end");
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("start");
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("end");
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("start");
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("end");
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("start");
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("end");
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("start");
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("end");
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("start");
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("end");
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