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 
Init(const std::shared_ptr<BundleActiveGroupController> & bundleActiveController)51 void BundleActiveGroupHandler::Init(const std::shared_ptr<BundleActiveGroupController>& bundleActiveController)
52 {
53     BUNDLE_ACTIVE_LOGI("Init called");
54     if (bundleActiveController == nullptr) {
55         BUNDLE_ACTIVE_LOGE("Init failed bundleActiveController is null");
56         return;
57     }
58     bundleActiveGroupController_ = bundleActiveController;
59     ffrtQueue_ = std::make_shared<ffrt::queue>(DEVICE_GROUP_HANDLE_QUEUE.c_str(),
60         ffrt::queue_attr().qos(ffrt::qos_default));
61     if (ffrtQueue_ == nullptr) {
62         BUNDLE_ACTIVE_LOGE("Init failed ffrtQueue is null");
63         return;
64     }
65     isInited_ = true;
66 }
67 
SendCheckBundleMsg(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)68 void BundleActiveGroupHandler::SendCheckBundleMsg(const int32_t& eventId,
69     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
70 {
71     if (!isInited_) {
72         BUNDLE_ACTIVE_LOGE("init failed");
73         return;
74     }
75     std::string msgKey = GetMsgKey(eventId, handlerobj, delayTime);
76     if (msgKey == "") {
77         return;
78     }
79     int64_t ffrtDelayTime = BundleActiveUtil::GetFFRTDelayTime(delayTime);
80     std::lock_guard<ffrt::mutex> lock(checkBundleTaskMutex_);
81     if (checkBundleTaskMap_.find(msgKey) != checkBundleTaskMap_.end()) {
82         RemoveCheckBundleMsg(msgKey);
83     }
84     auto groupHandler = shared_from_this();
85     checkBundleTaskMap_[msgKey] = ffrtQueue_->submit_h([groupHandler, eventId, handlerobj, msgKey]() {
86         groupHandler->ProcessEvent(eventId, handlerobj);
87         std::lock_guard<ffrt::mutex> lock(groupHandler->checkBundleTaskMutex_);
88         if (groupHandler->checkBundleTaskMap_.find(msgKey) == groupHandler->checkBundleTaskMap_.end()) {
89             return;
90         }
91         groupHandler->checkBundleTaskMap_.erase(msgKey);
92     }, ffrt::task_attr().delay(ffrtDelayTime));
93 }
94 
RemoveCheckBundleMsg(const std::string & msgKey)95 void BundleActiveGroupHandler::RemoveCheckBundleMsg(const std::string& msgKey)
96 {
97     if (!isInited_) {
98         BUNDLE_ACTIVE_LOGE("init failed");
99         return;
100     }
101     if (checkBundleTaskMap_.find(msgKey) == checkBundleTaskMap_.end()) {
102         return;
103     }
104     ffrtQueue_->cancel(checkBundleTaskMap_[msgKey]);
105     checkBundleTaskMap_.erase(msgKey);
106 }
107 
GetMsgKey(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)108 std::string BundleActiveGroupHandler::GetMsgKey(const int32_t& eventId,
109     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
110 {
111     if (handlerobj == nullptr) {
112         BUNDLE_ACTIVE_LOGE("handlerobj is null, GetMsgKey failed");
113         return "";
114     }
115     BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
116     return std::to_string(eventId) + "_" + std::to_string(tmpHandlerobj.userId_) + "_" +
117         std::to_string(tmpHandlerobj.uid_) + "_" + tmpHandlerobj.bundleName_ + "_" + std::to_string(delayTime);
118 }
119 
SendEvent(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj,const int64_t & delayTime)120 void BundleActiveGroupHandler::SendEvent(const int32_t& eventId,
121     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj, const int64_t& delayTime)
122 {
123     if (!isInited_) {
124         BUNDLE_ACTIVE_LOGE("init failed");
125         return;
126     }
127     auto groupHandler = shared_from_this();
128     int64_t ffrtDelayTime = BundleActiveUtil::GetFFRTDelayTime(delayTime);
129     std::lock_guard<ffrt::mutex> lock(taskHandlerMutex_);
130     if (taskHandlerMap_.find(eventId) == taskHandlerMap_.end()) {
131         taskHandlerMap_[eventId] = std::queue<ffrt::task_handle>();
132     }
133     ffrt::task_handle taskHandle = ffrtQueue_->submit_h([groupHandler, eventId, handlerobj]() {
134         groupHandler->ProcessEvent(eventId, handlerobj);
135         std::lock_guard<ffrt::mutex> lock(groupHandler->taskHandlerMutex_);
136         if (groupHandler->taskHandlerMap_.find(eventId) == groupHandler->taskHandlerMap_.end()) {
137             return;
138         }
139         if (!groupHandler->taskHandlerMap_[eventId].empty()) {
140             groupHandler->taskHandlerMap_[eventId].pop();
141         }
142     }, ffrt::task_attr().delay(ffrtDelayTime));
143     taskHandlerMap_[eventId].push(std::move(taskHandle));
144 }
145 
RemoveEvent(const int32_t & eventId)146 void BundleActiveGroupHandler::RemoveEvent(const int32_t& eventId)
147 {
148     if (!isInited_) {
149         BUNDLE_ACTIVE_LOGE("init failed");
150         return;
151     }
152     std::lock_guard<ffrt::mutex> lock(taskHandlerMutex_);
153     if (taskHandlerMap_.find(eventId) == taskHandlerMap_.end()) {
154         return;
155     }
156     while (!taskHandlerMap_[eventId].empty()) {
157         ffrtQueue_->cancel(taskHandlerMap_[eventId].front());
158         taskHandlerMap_[eventId].pop();
159     }
160     taskHandlerMap_.erase(eventId);
161 }
162 
PostSyncTask(const std::function<void ()> & fuc)163 void BundleActiveGroupHandler::PostSyncTask(const std::function<void()>& fuc)
164 {
165     if (!isInited_) {
166         BUNDLE_ACTIVE_LOGE("init failed");
167         return;
168     }
169     auto taskHandle = ffrtQueue_->submit_h(fuc);
170     ffrtQueue_->wait(taskHandle);
171 }
172 
PostTask(const std::function<void ()> & fuc)173 void BundleActiveGroupHandler::PostTask(const std::function<void()>& fuc)
174 {
175     if (!isInited_) {
176         BUNDLE_ACTIVE_LOGE("init failed");
177         return;
178     }
179     ffrtQueue_->submit(fuc);
180 }
181 
ProcessEvent(const int32_t & eventId,const std::shared_ptr<BundleActiveGroupHandlerObject> & handlerobj)182 void BundleActiveGroupHandler::ProcessEvent(const int32_t& eventId,
183     const std::shared_ptr<BundleActiveGroupHandlerObject>& handlerobj)
184 {
185     switch (eventId) {
186         case MSG_CHECK_DEFAULT_BUNDLE_STATE:
187         case MSG_CHECK_NOTIFICATION_SEEN_BUNDLE_STATE:
188         case MSG_CHECK_SYSTEM_INTERACTIVE_BUNDLE_STATE: {
189             if (handlerobj == nullptr) {
190                 return;
191             }
192             BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
193             sptr<MiscServices::TimeServiceClient> timer = MiscServices::TimeServiceClient::GetInstance();
194             int64_t bootBasedTimeStamp = timer->GetBootTimeMs();
195             bundleActiveGroupController_->CheckAndUpdateGroup(
196                 tmpHandlerobj.bundleName_, tmpHandlerobj.userId_, tmpHandlerobj.uid_, bootBasedTimeStamp);
197             bundleActiveGroupController_->RestoreToDatabase(tmpHandlerobj.userId_);
198             break;
199         }
200         case MSG_ONE_TIME_CHECK_BUNDLE_STATE: {
201             std::vector<int32_t> activatedOsAccountIds;
202             BundleActiveAccountHelper::GetActiveUserId(activatedOsAccountIds);
203             for (uint32_t i = 0; i < activatedOsAccountIds.size(); i++) {
204                 bundleActiveGroupController_->CheckEachBundleState(activatedOsAccountIds[i]);
205                 bundleActiveGroupController_->RestoreToDatabase(activatedOsAccountIds[i]);
206             }
207             RemoveEvent(MSG_ONE_TIME_CHECK_BUNDLE_STATE);
208             break;
209         }
210         case MSG_CHECK_IDLE_STATE: {
211             if (handlerobj == nullptr) {
212                 return;
213             }
214             BundleActiveGroupHandlerObject tmpHandlerobj = *handlerobj;
215             if (bundleActiveGroupController_->CheckEachBundleState(tmpHandlerobj.userId_) &&
216                 bundleActiveGroupController_->bundleGroupEnable_) {
217                 BundleActiveGroupHandlerObject GroupHandlerObj;
218                 GroupHandlerObj.userId_ = tmpHandlerobj.userId_;
219                 std::shared_ptr<BundleActiveGroupHandlerObject> handlerobjToPtr =
220                     std::make_shared<BundleActiveGroupHandlerObject>(GroupHandlerObj);
221                 bundleActiveGroupController_->RestoreToDatabase(GroupHandlerObj.userId_);
222                 SendEvent(BundleActiveGroupHandler::MSG_CHECK_IDLE_STATE, handlerobjToPtr, checkIdleInterval_);
223             }
224             break;
225         }
226         default: {
227             break;
228         }
229     }
230 }
231 }  // namespace DeviceUsageStats
232 }  // namespace OHOS
233 
234