• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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 "free_install_manager.h"
17 
18 #include <chrono>
19 
20 #include "ability_info.h"
21 #include "ability_manager_errors.h"
22 #include "ability_manager_service.h"
23 #include "ability_util.h"
24 #include "atomic_service_status_callback.h"
25 #include "distributed_client.h"
26 #include "free_install_observer_manager.h"
27 #include "hilog_wrapper.h"
28 #include "hitrace_meter.h"
29 #include "in_process_call_wrapper.h"
30 
31 namespace OHOS {
32 namespace AAFwk {
33 const std::u16string DMS_FREE_INSTALL_CALLBACK_TOKEN = u"ohos.DistributedSchedule.IDmsFreeInstallCallback";
34 const std::string DMS_MISSION_ID = "dmsMissionId";
35 const std::string PARAM_FREEINSTALL_APPID = "ohos.freeinstall.params.callingAppId";
36 const std::string PARAM_FREEINSTALL_BUNDLENAMES = "ohos.freeinstall.params.callingBundleNames";
37 const std::string PARAM_FREEINSTALL_UID = "ohos.freeinstall.params.callingUid";
38 constexpr uint32_t IDMS_CALLBACK_ON_FREE_INSTALL_DONE = 0;
39 constexpr uint32_t UPDATE_ATOMOIC_SERVICE_TASK_TIMER = 24 * 60 * 60 * 1000; /* 24h */
40 
FreeInstallManager(const std::weak_ptr<AbilityManagerService> & server)41 FreeInstallManager::FreeInstallManager(const std::weak_ptr<AbilityManagerService> &server)
42     : server_(server)
43 {
44 }
45 
IsTopAbility(const sptr<IRemoteObject> & callerToken)46 bool FreeInstallManager::IsTopAbility(const sptr<IRemoteObject> &callerToken)
47 {
48     HILOG_INFO("%{public}s", __func__);
49     auto server = server_.lock();
50     CHECK_POINTER_AND_RETURN_LOG(server, false, "Get server failed!");
51     AppExecFwk::ElementName elementName = IN_PROCESS_CALL(server->GetTopAbility());
52     if (elementName.GetBundleName().empty() || elementName.GetAbilityName().empty()) {
53         HILOG_ERROR("GetBundleName or GetAbilityName empty!");
54         return false;
55     }
56 
57     auto caller = Token::GetAbilityRecordByToken(callerToken);
58     if (caller == nullptr) {
59         HILOG_ERROR("Caller is null!");
60         return false;
61     }
62 
63     auto type = caller->GetAbilityInfo().type;
64     if (type == AppExecFwk::AbilityType::SERVICE || type == AppExecFwk::AbilityType::EXTENSION) {
65         HILOG_INFO("The ability is service or extension ability.");
66         return true;
67     }
68 
69     AppExecFwk::ElementName callerElementName = caller->GetWant().GetElement();
70     std::string callerBundleName = callerElementName.GetBundleName();
71     std::string callerAbilityName = callerElementName.GetAbilityName();
72     std::string callerModuleName = callerElementName.GetModuleName();
73     if (elementName.GetBundleName().compare(callerBundleName) == 0 &&
74         elementName.GetAbilityName().compare(callerAbilityName) == 0 &&
75         elementName.GetModuleName().compare(callerModuleName) == 0) {
76         HILOG_INFO("The ability is top ability.");
77         return true;
78     }
79 
80     return false;
81 }
82 
StartFreeInstall(const Want & want,int32_t userId,int requestCode,const sptr<IRemoteObject> & callerToken,bool isAsync)83 int FreeInstallManager::StartFreeInstall(const Want &want, int32_t userId, int requestCode,
84     const sptr<IRemoteObject> &callerToken, bool isAsync)
85 {
86     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
87     HILOG_INFO("StartFreeInstall called");
88     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
89     auto isGatewayCall = AAFwk::PermissionVerification::GetInstance()->IsGatewayCall();
90     if (!isSaCall && !isGatewayCall && !IsTopAbility(callerToken)) {
91         return NOT_TOP_ABILITY;
92     }
93     FreeInstallInfo info = BuildFreeInstallInfo(want, userId, requestCode, callerToken, isAsync);
94     {
95         std::lock_guard<ffrt::mutex> lock(freeInstallListLock_);
96         freeInstallList_.push_back(info);
97     }
98     sptr<AtomicServiceStatusCallback> callback = new AtomicServiceStatusCallback(weak_from_this(), isAsync);
99     auto bms = AbilityUtil::GetBundleManager();
100     CHECK_POINTER_AND_RETURN(bms, GET_ABILITY_SERVICE_FAILED);
101     AppExecFwk::AbilityInfo abilityInfo = {};
102     constexpr auto flag = AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION;
103     info.want.SetParam(PARAM_FREEINSTALL_UID, IPCSkeleton::GetCallingUid());
104 
105     if (isAsync) {
106         PostTimeoutTask(want);
107     }
108 
109     if (IN_PROCESS_CALL(bms->QueryAbilityInfo(info.want, flag, info.userId, abilityInfo, callback))) {
110         HILOG_INFO("The app has installed.");
111     }
112     std::string callingAppId = info.want.GetStringParam(PARAM_FREEINSTALL_APPID);
113     std::vector<std::string> callingBundleNames = info.want.GetStringArrayParam(PARAM_FREEINSTALL_BUNDLENAMES);
114     if (callingAppId.empty() && callingBundleNames.empty()) {
115         HILOG_INFO("callingAppId and callingBundleNames are empty");
116     }
117     info.want.RemoveParam(PARAM_FREEINSTALL_APPID);
118     info.want.RemoveParam(PARAM_FREEINSTALL_BUNDLENAMES);
119 
120     if (isAsync) {
121         return ERR_OK;
122     } else {
123         auto future = info.promise->get_future();
124         std::future_status status = future.wait_for(std::chrono::milliseconds(DELAY_LOCAL_FREE_INSTALL_TIMEOUT));
125         if (status == std::future_status::timeout) {
126             RemoveFreeInstallInfo(info.want.GetElement().GetBundleName(), info.want.GetElement().GetAbilityName(),
127                 info.want.GetStringParam(Want::PARAM_RESV_START_TIME));
128             return FREE_INSTALL_TIMEOUT;
129         }
130         return future.get();
131     }
132 }
133 
RemoteFreeInstall(const Want & want,int32_t userId,int requestCode,const sptr<IRemoteObject> & callerToken)134 int FreeInstallManager::RemoteFreeInstall(const Want &want, int32_t userId, int requestCode,
135     const sptr<IRemoteObject> &callerToken)
136 {
137     HILOG_INFO("RemoteFreeInstall called");
138     bool isFromRemote = want.GetBoolParam(FROM_REMOTE_KEY, false);
139     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
140     if (!isSaCall && !isFromRemote && !IsTopAbility(callerToken)) {
141         return NOT_TOP_ABILITY;
142     }
143     FreeInstallInfo info = BuildFreeInstallInfo(want, userId, requestCode, callerToken, false);
144     {
145         std::lock_guard<ffrt::mutex> lock(freeInstallListLock_);
146         freeInstallList_.push_back(info);
147     }
148     sptr<AtomicServiceStatusCallback> callback = new AtomicServiceStatusCallback(weak_from_this(), false);
149     int32_t callerUid = IPCSkeleton::GetCallingUid();
150     uint32_t accessToken = IPCSkeleton::GetCallingTokenID();
151     DistributedClient dmsClient;
152     auto result = dmsClient.StartRemoteFreeInstall(info.want, callerUid, info.requestCode, accessToken, callback);
153     if (result != ERR_NONE) {
154         return result;
155     }
156     auto remoteFuture = info.promise->get_future();
157     std::future_status remoteStatus = remoteFuture.wait_for(std::chrono::milliseconds(
158         DELAY_REMOTE_FREE_INSTALL_TIMEOUT));
159     if (remoteStatus == std::future_status::timeout) {
160         return FREE_INSTALL_TIMEOUT;
161     }
162     return remoteFuture.get();
163 }
164 
BuildFreeInstallInfo(const Want & want,int32_t userId,int requestCode,const sptr<IRemoteObject> & callerToken,bool isAsync)165 FreeInstallInfo FreeInstallManager::BuildFreeInstallInfo(const Want &want, int32_t userId, int requestCode,
166     const sptr<IRemoteObject> &callerToken, bool isAsync)
167 {
168     FreeInstallInfo info = {
169         .want = want,
170         .userId = userId,
171         .requestCode = requestCode,
172         .callerToken = callerToken
173     };
174     if (!isAsync) {
175         auto promise = std::make_shared<std::promise<int32_t>>();
176         info.promise = promise;
177     }
178     auto identity = IPCSkeleton::ResetCallingIdentity();
179     info.identity = identity;
180     IPCSkeleton::SetCallingIdentity(identity);
181     return info;
182 }
183 
StartRemoteFreeInstall(const Want & want,int requestCode,int32_t validUserId,const sptr<IRemoteObject> & callerToken)184 int FreeInstallManager::StartRemoteFreeInstall(const Want &want, int requestCode, int32_t validUserId,
185     const sptr<IRemoteObject> &callerToken)
186 {
187     HILOG_INFO("%{public}s", __func__);
188     if (!want.GetBoolParam(Want::PARAM_RESV_FOR_RESULT, false)) {
189         HILOG_INFO("%{public}s: StartAbility freeInstall", __func__);
190         return RemoteFreeInstall(want, validUserId, requestCode, callerToken);
191     }
192     int32_t missionId = DelayedSingleton<AbilityManagerService>::GetInstance()->
193         GetMissionIdByAbilityToken(callerToken);
194     if (missionId < 0) {
195         return ERR_INVALID_VALUE;
196     }
197     Want* newWant = const_cast<Want*>(&want);
198     newWant->SetParam(DMS_MISSION_ID, missionId);
199     HILOG_INFO("%{public}s: StartAbilityForResult freeInstall", __func__);
200     return RemoteFreeInstall(*newWant, validUserId, requestCode, callerToken);
201 }
202 
NotifyDmsCallback(const Want & want,int resultCode)203 int FreeInstallManager::NotifyDmsCallback(const Want &want, int resultCode)
204 {
205     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
206     std::lock_guard<ffrt::mutex> autoLock(distributedFreeInstallLock_);
207     if (dmsFreeInstallCbs_.empty()) {
208         HILOG_ERROR("Has no dms callback.");
209         return ERR_INVALID_VALUE;
210     }
211 
212     MessageParcel reply;
213     MessageOption option;
214 
215     for (auto it = dmsFreeInstallCbs_.begin(); it != dmsFreeInstallCbs_.end();) {
216         std::string abilityName = (*it).want.GetElement().GetAbilityName();
217         if (want.GetElement().GetAbilityName().compare(abilityName) == 0) {
218             HILOG_INFO("Handle DMS.");
219             MessageParcel data;
220             if (!data.WriteInterfaceToken(DMS_FREE_INSTALL_CALLBACK_TOKEN)) {
221                 HILOG_ERROR("Write interface token failed.");
222                 return ERR_INVALID_VALUE;
223             }
224 
225             if (!data.WriteInt32(resultCode)) {
226                 HILOG_ERROR("Write resultCode error.");
227                 return ERR_INVALID_VALUE;
228             }
229 
230             if (!data.WriteParcelable(&((*it).want))) {
231                 HILOG_ERROR("want write failed.");
232                 return INNER_ERR;
233             }
234 
235             if (!data.WriteInt32((*it).requestCode)) {
236                 HILOG_ERROR("Write resultCode error.");
237                 return ERR_INVALID_VALUE;
238             }
239 
240             (*it).dmsCallback->SendRequest(IDMS_CALLBACK_ON_FREE_INSTALL_DONE, data, reply, option);
241             it = dmsFreeInstallCbs_.erase(it);
242         } else {
243             it++;
244         }
245     }
246 
247     return reply.ReadInt32();
248 }
249 
NotifyFreeInstallResult(const Want & want,int resultCode,bool isAsync)250 void FreeInstallManager::NotifyFreeInstallResult(const Want &want, int resultCode, bool isAsync)
251 {
252     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
253     std::lock_guard<ffrt::mutex> lock(freeInstallListLock_);
254     if (freeInstallList_.empty()) {
255         HILOG_INFO("Has no app callback.");
256         return;
257     }
258 
259     bool isFromRemote = want.GetBoolParam(FROM_REMOTE_KEY, false);
260     HILOG_INFO("isFromRemote = %{public}d", isFromRemote);
261     for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) {
262         std::string bundleName = (*it).want.GetElement().GetBundleName();
263         std::string abilityName = (*it).want.GetElement().GetAbilityName();
264         std::string startTime = (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME);
265         if (want.GetElement().GetBundleName().compare(bundleName) != 0 ||
266             want.GetElement().GetAbilityName().compare(abilityName) != 0 ||
267             want.GetStringParam(Want::PARAM_RESV_START_TIME).compare(startTime) != 0) {
268             it++;
269             continue;
270         }
271 
272         if (!isAsync && (*it).promise == nullptr) {
273             it++;
274             continue;
275         }
276 
277         if (resultCode == ERR_OK) {
278             HILOG_INFO("FreeInstall success.");
279             if (isAsync) {
280                 Want newWant((*it).want);
281                 newWant.SetFlags(want.GetFlags() ^ Want::FLAG_INSTALL_ON_DEMAND);
282                 auto identity = IPCSkeleton::ResetCallingIdentity();
283                 IPCSkeleton::SetCallingIdentity((*it).identity);
284                 int result = AbilityManagerClient::GetInstance()->StartAbility(newWant, (*it).callerToken,
285                     (*it).requestCode, (*it).userId);
286                 IPCSkeleton::SetCallingIdentity(identity);
287                 HILOG_INFO("The result of StartAbility is %{public}d.", result);
288                 DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished(
289                     bundleName, abilityName, startTime, result);
290             } else {
291                 (*it).promise->set_value(resultCode);
292             }
293         } else {
294             HILOG_INFO("FreeInstall failed.");
295             if (isAsync) {
296                 DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished(
297                     bundleName, abilityName, startTime, resultCode);
298             } else {
299                 (*it).promise->set_value(resultCode);
300             }
301         }
302 
303         it = freeInstallList_.erase(it);
304     }
305 }
306 
FreeInstallAbilityFromRemote(const Want & want,const sptr<IRemoteObject> & callback,int32_t userId,int requestCode)307 int FreeInstallManager::FreeInstallAbilityFromRemote(const Want &want, const sptr<IRemoteObject> &callback,
308     int32_t userId, int requestCode)
309 {
310     HILOG_INFO("%{public}s", __func__);
311     if (callback == nullptr) {
312         HILOG_ERROR("FreeInstallAbilityFromRemote callback is nullptr.");
313         return ERR_INVALID_VALUE;
314     }
315 
316     FreeInstallInfo info = {
317         .want = want,
318         .userId = userId,
319         .requestCode = requestCode,
320         .dmsCallback = callback
321     };
322 
323     {
324         std::lock_guard<ffrt::mutex> autoLock(distributedFreeInstallLock_);
325         dmsFreeInstallCbs_.push_back(info);
326     }
327 
328     auto result = StartFreeInstall(info.want, info.userId, info.requestCode, nullptr);
329     if (result != ERR_OK) {
330         HILOG_ERROR("StartFreeInstall failed, errCode: %{public}d", result);
331         NotifyDmsCallback(info.want, result);
332     }
333     return result;
334 }
335 
ConnectFreeInstall(const Want & want,int32_t userId,const sptr<IRemoteObject> & callerToken,const std::string & localDeviceId)336 int FreeInstallManager::ConnectFreeInstall(const Want &want, int32_t userId,
337     const sptr<IRemoteObject> &callerToken, const std::string& localDeviceId)
338 {
339     auto bms = AbilityUtil::GetBundleManager();
340     CHECK_POINTER_AND_RETURN(bms, GET_ABILITY_SERVICE_FAILED);
341     std::string wantDeviceId = want.GetElement().GetDeviceID();
342     if (!(localDeviceId == wantDeviceId || wantDeviceId.empty())) {
343         HILOG_ERROR("AbilityManagerService::ConnectFreeInstall. wantDeviceId error");
344         return INVALID_PARAMETERS_ERR;
345     }
346 
347     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
348     if (!isSaCall) {
349         std::string wantAbilityName = want.GetElement().GetAbilityName();
350         std::string wantBundleName = want.GetElement().GetBundleName();
351         if (wantBundleName.empty() || wantAbilityName.empty()) {
352             HILOG_ERROR("AbilityManagerService::ConnectFreeInstall. wantBundleName or wantAbilityName is empty");
353             return INVALID_PARAMETERS_ERR;
354         }
355         int callerUid = IPCSkeleton::GetCallingUid();
356         std::string localBundleName;
357         auto res = IN_PROCESS_CALL(bms->GetNameForUid(callerUid, localBundleName));
358         if (res != ERR_OK || localBundleName != wantBundleName) {
359             HILOG_ERROR("AbilityManagerService::ConnectFreeInstall. wantBundleName is not local BundleName");
360             return INVALID_PARAMETERS_ERR;
361         }
362     }
363 
364     AppExecFwk::AbilityInfo abilityInfo;
365     std::vector<AppExecFwk::ExtensionAbilityInfo> extensionInfos;
366     if (!IN_PROCESS_CALL(bms->QueryAbilityInfo(want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION,
367         userId, abilityInfo)) && !IN_PROCESS_CALL(bms->QueryExtensionAbilityInfos(
368             want, AppExecFwk::AbilityInfoFlag::GET_ABILITY_INFO_WITH_APPLICATION, userId, extensionInfos))) {
369         HILOG_INFO("AbilityManagerService::ConnectFreeInstall. try to StartFreeInstall");
370         int result = StartFreeInstall(want, userId, DEFAULT_INVAL_VALUE, callerToken);
371         if (result) {
372             HILOG_ERROR("AbilityManagerService::ConnectFreeInstall. StartFreeInstall error");
373             return result;
374         }
375         HILOG_INFO("AbilityManagerService::ConnectFreeInstall. StartFreeInstall success");
376     }
377     return ERR_OK;
378 }
379 
GetTimeStamp()380 std::time_t FreeInstallManager::GetTimeStamp()
381 {
382     std::chrono::time_point<std::chrono::system_clock, std::chrono::milliseconds> tp =
383         std::chrono::time_point_cast<std::chrono::milliseconds>(std::chrono::system_clock::now());
384     std::time_t timestamp = tp.time_since_epoch().count();
385     return timestamp;
386 }
387 
OnInstallFinished(int resultCode,const Want & want,int32_t userId,bool isAsync)388 void FreeInstallManager::OnInstallFinished(int resultCode, const Want &want, int32_t userId, bool isAsync)
389 {
390     HITRACE_METER_NAME(HITRACE_TAG_ABILITY_MANAGER, __PRETTY_FUNCTION__);
391     HILOG_INFO("%{public}s resultCode = %{public}d", __func__, resultCode);
392     if (isAsync) {
393         // remove timeout task
394         std::string bundleName = want.GetElement().GetBundleName();
395         std::string abilityName = want.GetElement().GetAbilityName();
396         std::string startTime = want.GetStringParam(Want::PARAM_RESV_START_TIME);
397         RemoveTimeoutTask(bundleName, abilityName, startTime);
398     }
399     NotifyDmsCallback(want, resultCode);
400     NotifyFreeInstallResult(want, resultCode, isAsync);
401 
402     PostUpgradeAtomicServiceTask(resultCode, want, userId);
403 }
404 
PostUpgradeAtomicServiceTask(int resultCode,const Want & want,int32_t userId)405 void FreeInstallManager::PostUpgradeAtomicServiceTask(int resultCode, const Want &want, int32_t userId)
406 {
407     HILOG_INFO("PostUpgradeAtomicServiceTask begin.");
408     std::weak_ptr<FreeInstallManager> thisWptr(shared_from_this());
409     if (resultCode == ERR_OK) {
410         auto updateAtmoicServiceTask = [want, userId, thisWptr, &timeStampMap = timeStampMap_]() {
411             auto sptr = thisWptr.lock();
412             HILOG_DEBUG("bundleName: %{public}s, moduleName: %{public}s", want.GetElement().GetBundleName().c_str(),
413                 want.GetElement().GetModuleName().c_str());
414             std::string nameKey = want.GetElement().GetBundleName() + want.GetElement().GetModuleName();
415             if (timeStampMap.find(nameKey) == timeStampMap.end() ||
416                 sptr->GetTimeStamp() - timeStampMap[nameKey] > UPDATE_ATOMOIC_SERVICE_TASK_TIMER) {
417                 auto bms = AbilityUtil::GetBundleManager();
418                 CHECK_POINTER(bms);
419                 bms->UpgradeAtomicService(want, userId);
420                 timeStampMap.emplace(nameKey, sptr->GetTimeStamp());
421             }
422         };
423 
424         auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
425         CHECK_POINTER_LOG(handler, "Fail to get Ability task handler.");
426         handler->SubmitTask(updateAtmoicServiceTask, "UpdateAtmoicServiceTask");
427     }
428 }
429 
OnRemoteInstallFinished(int resultCode,const Want & want,int32_t userId)430 void FreeInstallManager::OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId)
431 {
432     HILOG_INFO("%{public}s resultCode = %{public}d", __func__, resultCode);
433     NotifyFreeInstallResult(want, resultCode);
434 }
435 
AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> & observer)436 int FreeInstallManager::AddFreeInstallObserver(const sptr<AbilityRuntime::IFreeInstallObserver> &observer)
437 {
438     HILOG_INFO("Add FreeInstallObserver");
439     return DelayedSingleton<FreeInstallObserverManager>::GetInstance()->AddObserver(observer);
440 }
441 
PostTimeoutTask(const Want & want)442 void FreeInstallManager::PostTimeoutTask(const Want &want)
443 {
444     HILOG_INFO("PostTimeoutTask begin.");
445     std::string bundleName = want.GetElement().GetBundleName();
446     std::string abilityName = want.GetElement().GetAbilityName();
447     std::string startTime = want.GetStringParam(Want::PARAM_RESV_START_TIME);
448     auto task = [weak = weak_from_this(), bundleName, abilityName, startTime]() {
449         auto self = weak.lock();
450         if (!self) {
451             HILOG_ERROR("this is nullptr");
452             return;
453         }
454         DelayedSingleton<FreeInstallObserverManager>::GetInstance()->OnInstallFinished(bundleName, abilityName,
455             startTime, FREE_INSTALL_TIMEOUT);
456         self->RemoveFreeInstallInfo(bundleName, abilityName, startTime);
457     };
458     std::string taskName = std::string("FreeInstallTimeout_") + bundleName + std::string("_") +
459         abilityName + std::string("_") + startTime;
460     auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
461     CHECK_POINTER_LOG(handler, "Fail to get AbilityTaskHandler.");
462     handler->SubmitTask(task, taskName, DELAY_LOCAL_FREE_INSTALL_TIMEOUT);
463 }
464 
RemoveTimeoutTask(const std::string & bundleName,const std::string & abilityName,const std::string & startTime)465 void FreeInstallManager::RemoveTimeoutTask(const std::string &bundleName, const std::string &abilityName,
466     const std::string &startTime)
467 {
468     // remove timeout task
469     std::string taskName = std::string("FreeInstallTimeout_") + bundleName + std::string("_") +
470         abilityName + std::string("_") + startTime;
471     HILOG_INFO("RemoveTimeoutTask task name:%{public}s", taskName.c_str());
472     auto handler = DelayedSingleton<AbilityManagerService>::GetInstance()->GetTaskHandler();
473     CHECK_POINTER_LOG(handler, "Fail to get AbilityTaskHandler.");
474     handler->CancelTask(taskName);
475 }
476 
OnRemoveTimeoutTask(const Want & want)477 void FreeInstallManager::OnRemoveTimeoutTask(const Want &want)
478 {
479     // only SA can call this interface
480     HILOG_INFO("OnRemoveTimeoutTask begin.");
481     auto isSaCall = AAFwk::PermissionVerification::GetInstance()->IsSACall();
482     if (!isSaCall) {
483         HILOG_ERROR("Permission verification failed.");
484         return;
485     }
486     std::string bundleName = want.GetElement().GetBundleName();
487     std::string abilityName = want.GetElement().GetAbilityName();
488     std::string startTime = want.GetStringParam(Want::PARAM_RESV_START_TIME);
489     if (bundleName.empty() || abilityName.empty()) {
490         HILOG_ERROR("wantBundleName or wantAbilityName is empty");
491         return;
492     }
493     RemoveTimeoutTask(bundleName, abilityName, startTime);
494 }
495 
RemoveFreeInstallInfo(const std::string & bundleName,const std::string & abilityName,const std::string & startTime)496 void FreeInstallManager::RemoveFreeInstallInfo(const std::string &bundleName, const std::string &abilityName,
497     const std::string &startTime)
498 {
499     std::lock_guard<ffrt::mutex> lock(freeInstallListLock_);
500     for (auto it = freeInstallList_.begin(); it != freeInstallList_.end();) {
501         if ((*it).want.GetElement().GetBundleName() == bundleName &&
502             (*it).want.GetElement().GetAbilityName() == abilityName &&
503             (*it).want.GetStringParam(Want::PARAM_RESV_START_TIME) == startTime) {
504             it = freeInstallList_.erase(it);
505         } else {
506             it++;
507         }
508     }
509 }
510 }  // namespace AAFwk
511 }  // namespace OHOS
512