• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2022-2024 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 "time_service_client.h"
17 #include "bundle_active_account_helper.h"
18 #include "bundle_active_group_handler.h"
19 #include "bundle_active_log.h"
20 #include "bundle_active_util.h"
21 
22 namespace OHOS {
23 namespace DeviceUsageStats {
24 const std::string DEVICE_GROUP_HANDLE_QUEUE = "DeviceUsageGroupHandleQueue";
25 const int32_t BundleActiveGroupHandler::MSG_CHECK_DEFAULT_BUNDLE_STATE = 0;
26 const int32_t BundleActiveGroupHandler::MSG_ONE_TIME_CHECK_BUNDLE_STATE = 1;
27 const int32_t BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE = 2;
28 const int32_t BundleActiveGroupHandler::MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE = 3;
29 const int32_t BundleActiveGroupHandler::MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE = 4;
30 #ifndef OS_ACCOUNT_PART_ENABLED
31 namespace {
32 constexpr int32_t DEFAULT_OS_ACCOUNT_ID = 0; // 0 is the default id when there is no os_account part
33 } // namespace
34 #endif // OS_ACCOUNT_PART_ENABLED
BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject & orig)35 BundleActiveGroupHandlerObject::BundleActiveGroupHandlerObject(const BundleActiveGroupHandlerObject& orig)
36 {
37     bundleName_ = orig.bundleName_;
38     userId_ = orig.userId_;
39     uid_ = orig.uid_;
40 }
41 
BundleActiveGroupHandler(const bool debug)42 BundleActiveGroupHandler::BundleActiveGroupHandler(const bool debug)
43 {
44     if (debug) {
45         checkIdleInterval_ = ONE_MINUTE;
46     } else {
47         checkIdleInterval_ = THREE_HOUR;
48     }
49 }
50 
DeInit()51 void BundleActiveGroupHandler::DeInit()
52 {
53     isInited_ = false;
54     for (auto& iter : taskHandlerMap_) {
55         auto& queue = iter.second;
56         while (!queue.empty()) {
57             ffrtQueue_->cancel(queue.front());
58             queue.pop();
59         }
60     }
61     taskHandlerMap_.clear();
62     for (const auto& iter: checkBundleTaskMap_) {
63         ffrtQueue_->cancel(iter.second);
64     }
65     checkBundleTaskMap_.clear();
66 }
67 
Init()68 void BundleActiveGroupHandler::Init()
69 {
70     BUNDLE_ACTIVE_LOGI("Init called");
71     ffrtQueue_ = std::make_shared<ffrt::queue>(DEVICE_GROUP_HANDLE_QUEUE.c_str(),
72         ffrt::queue_attr().qos(ffrt::qos_default));
73     if (ffrtQueue_ == nullptr) {
74         BUNDLE_ACTIVE_LOGE("Init failed ffrtQueue is null");
75         return;
76     }
77     isInited_ = true;
78 }
79 
SendCheckBundleMsg(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)80 void BundleActiveGroupHandler::SendCheckBundleMsg(const int32_t& eventId,
81     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
82 {
83     if (!isInited_) {
84         BUNDLE_ACTIVE_LOGE("init failed");
85         return;
86     }
87     std::string msgKey = GetMsgKey(eventId, handlerobj, delayTime);
88     if (msgKey == "") {
89         return;
90     }
91     int64_t ffrtDelayTime = BundleActiveUtil::GetFFRTDelayTime(delayTime);
92     std::lock_guard<ffrt::mutex> lock(checkBundleTaskMutex_);
93     if (checkBundleTaskMap_.find(msgKey) != checkBundleTaskMap_.end()) {
94         RemoveCheckBundleMsg(msgKey);
95     }
96     checkBundleTaskMap_[msgKey] = ffrtQueue_->submit_h([eventId, handlerobj, msgKey]() {
97         auto groupHandler = BundleActiveGroupController::GetInstance().GetBundleGroupHandler();
98         if (groupHandler == nullptr) {
99             return;
100         }
101         groupHandler->ProcessEvent(eventId, handlerobj);
102         std::lock_guard<ffrt::mutex> lock(groupHandler->checkBundleTaskMutex_);
103         if (groupHandler->checkBundleTaskMap_.find(msgKey) == groupHandler->checkBundleTaskMap_.end()) {
104             return;
105         }
106         groupHandler->checkBundleTaskMap_.erase(msgKey);
107     }, ffrt::task_attr().delay(ffrtDelayTime));
108 }
109 
RemoveCheckBundleMsg(const std::string & msgKey)110 void BundleActiveGroupHandler::RemoveCheckBundleMsg(const std::string& msgKey)
111 {
112     if (!isInited_) {
113         BUNDLE_ACTIVE_LOGE("init failed");
114         return;
115     }
116     if (checkBundleTaskMap_.find(msgKey) == checkBundleTaskMap_.end()) {
117         return;
118     }
119     ffrtQueue_->cancel(checkBundleTaskMap_[msgKey]);
120     checkBundleTaskMap_.erase(msgKey);
121 }
122 
GetMsgKey(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)123 std::string BundleActiveGroupHandler::GetMsgKey(const int32_t& eventId,
124     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
125 {
126     if (handlerobj == nullptr) {
127         BUNDLE_ACTIVE_LOGE("handlerobj is null, GetMsgKey failed");
128         return "";
129     }
130     BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
131     return std::to_string(eventId) + "_" + std::to_string(tmpHandlerobj.userId_) + "_" +
132         std::to_string(tmpHandlerobj.uid_) + "_" + tmpHandlerobj.bundleName_ + "_" + std::to_string(delayTime);
133 }
134 
SendEvent(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)135 void BundleActiveGroupHandler::SendEvent(const int32_t& eventId,
136     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
137 {
138     if (!isInited_) {
139         BUNDLE_ACTIVE_LOGE("init failed");
140         return;
141     }
142     int64_t ffrtDelayTime = BundleActiveUtil::GetFFRTDelayTime(delayTime);
143     std::lock_guard<ffrt::mutex> lock(taskHandlerMutex_);
144     if (taskHandlerMap_.find(eventId) == taskHandlerMap_.end()) {
145         taskHandlerMap_[eventId] = std::queue<ffrt::task_handle>();
146     }
147     ffrt::task_handle taskHandle = ffrtQueue_->submit_h([eventId, handlerobj]() {
148         auto groupHandler = BundleActiveGroupController::GetInstance().GetBundleGroupHandler();
149         if (groupHandler == nullptr) {
150             return;
151         }
152         if (!groupHandler->isInited_) {
153             BUNDLE_ACTIVE_LOGE("init failed");
154             return;
155         }
156         groupHandler->ProcessEvent(eventId, handlerobj);
157         std::lock_guard<ffrt::mutex> lock(groupHandler->taskHandlerMutex_);
158         if (groupHandler->taskHandlerMap_.find(eventId) == groupHandler->taskHandlerMap_.end()) {
159             return;
160         }
161         if (!groupHandler->taskHandlerMap_[eventId].empty()) {
162             groupHandler->taskHandlerMap_[eventId].pop();
163         }
164     }, ffrt::task_attr().delay(ffrtDelayTime));
165     taskHandlerMap_[eventId].push(std::move(taskHandle));
166 }
167 
RemoveEvent(const int32_t & eventId)168 void BundleActiveGroupHandler::RemoveEvent(const int32_t& eventId)
169 {
170     if (!isInited_) {
171         BUNDLE_ACTIVE_LOGE("init failed");
172         return;
173     }
174     std::lock_guard<ffrt::mutex> lock(taskHandlerMutex_);
175     if (taskHandlerMap_.find(eventId) == taskHandlerMap_.end()) {
176         return;
177     }
178     while (!taskHandlerMap_[eventId].empty()) {
179         ffrtQueue_->cancel(taskHandlerMap_[eventId].front());
180         taskHandlerMap_[eventId].pop();
181     }
182     taskHandlerMap_.erase(eventId);
183 }
184 
PostSyncTask(const std::function<void ()> & fuc)185 void BundleActiveGroupHandler::PostSyncTask(const std::function<void()>& fuc)
186 {
187     if (!isInited_) {
188         BUNDLE_ACTIVE_LOGE("init failed");
189         return;
190     }
191     auto taskHandle = ffrtQueue_->submit_h(fuc);
192     ffrtQueue_->wait(taskHandle);
193 }
194 
PostTask(const std::function<void ()> & fuc)195 void BundleActiveGroupHandler::PostTask(const std::function<void()>& fuc)
196 {
197     if (!isInited_) {
198         BUNDLE_ACTIVE_LOGE("init failed");
199         return;
200     }
201     ffrtQueue_->submit(fuc);
202 }
203 
ProcessEvent(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj)204 void BundleActiveGroupHandler::ProcessEvent(const int32_t& eventId,
205     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj)
206 {
207     if (!isInited_) {
208         return;
209     }
210     switch (eventId) {
211         case MSG_CHECK_DEFAULT_BUNDLE_STATE:
212         case MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE:
213         case MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE: {
214             if (handlerobj == nullptr) {
215                 return;
216             }
217             BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
218             sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
219             int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
220             BundleActiveGroupController::GetInstance().CheckAndUpdateGroup(
221                 tmpHandlerobj.bundleName_, tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp);
222                 BundleActiveGroupController::GetInstance().RestoreToDatabase(tmpHandlerobj.userId_);
223             break;
224         }
225         case MSG_ONE_TIME_CHECK_BUNDLE_STATE: {
226             std::vector<int32_t> activatedOsAccountIds;
227             BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds);
228             for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) {
229                 BundleActiveGroupController::GetInstance().CheckEachBundleState(activatedOsAccountIds[i]);
230                 BundleActiveGroupController::GetInstance().RestoreToDatabase(activatedOsAccountIds[i]);
231             }
232             RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE);
233             break;
234         }
235         case MSG_CHECK_IDLE_STATE: {
236             if (handlerobj == nullptr) {
237                 return;
238             }
239             BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
240             if (BundleActiveGroupController::GetInstance().CheckEachBundleState(tmpHandlerobj.userId_) &&
241             BundleActiveGroupController::GetInstance().GetBundleGroupEnable()) {
242                 BundleActiveGroupHandlerObject GroupHandlerObj;
243                 GroupHandlerObj.userId_ = tmpHandlerobj.userId_;
244                 std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
245                     std::make_shared<BundleActiveGroupHandlerObject>(GroupHandlerObj);
246                     BundleActiveGroupController::GetInstance().RestoreToDatabase(GroupHandlerObj.userId_);
247                 SendEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, handlerobjToPtr, checkIdleInterval_);
248             }
249             break;
250         }
251         default: {
252             break;
253         }
254     }
255 }
256 }  // namespace DeviceUsageStats
257 }  // namespace OHOS
258 
259