1 /*
2 * Copyright (c) 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 "ability_keep_alive_service.h"
17
18 #include "ability_keep_alive_data_manager.h"
19 #include "ability_manager_service.h"
20 #include "hilog_tag_wrapper.h"
21
22 namespace OHOS {
23 namespace AbilityRuntime {
24 using namespace OHOS::AAFwk;
GetInstance()25 AbilityKeepAliveService &AbilityKeepAliveService::GetInstance()
26 {
27 static AbilityKeepAliveService instance;
28 return instance;
29 }
30
AbilityKeepAliveService()31 AbilityKeepAliveService::AbilityKeepAliveService() {}
32
~AbilityKeepAliveService()33 AbilityKeepAliveService::~AbilityKeepAliveService() {}
34
SetApplicationKeepAlive(KeepAliveInfo & info,bool flag)35 int32_t AbilityKeepAliveService::SetApplicationKeepAlive(KeepAliveInfo &info, bool flag)
36 {
37 TAG_LOGD(AAFwkTag::KEEP_ALIVE, "SetApplicationKeepAlive is called,"
38 " bundleName: %{public}s, userId: %{public}d, flag: %{public}d",
39 info.bundleName.c_str(), info.userId, static_cast<int>(flag));
40
41 GetValidUserId(info.userId);
42
43 if (flag) {
44 return SetKeepAliveTrue(info);
45 }
46 return CancelKeepAlive(info);
47 }
48
SetKeepAliveTrue(const KeepAliveInfo & info)49 int32_t AbilityKeepAliveService::SetKeepAliveTrue(const KeepAliveInfo &info)
50 {
51 KeepAliveStatus status = AbilityKeepAliveDataManager::GetInstance().QueryKeepAliveData(info);
52 if (status.code != ERR_OK && status.code != ERR_NAME_NOT_FOUND) {
53 TAG_LOGE(AAFwkTag::KEEP_ALIVE, "QueryKeepAliveData fail");
54 return status.code;
55 }
56
57 if (status.code == ERR_NAME_NOT_FOUND) {
58 return AbilityKeepAliveDataManager::GetInstance().InsertKeepAliveData(info);
59 }
60
61 if (static_cast<int32_t>(status.setter) <= static_cast<int32_t>(info.setter)) {
62 TAG_LOGI(AAFwkTag::KEEP_ALIVE, "app is already set");
63 return ERR_OK;
64 }
65
66 (void)AbilityKeepAliveDataManager::GetInstance().DeleteKeepAliveData(info);
67 return AbilityKeepAliveDataManager::GetInstance().InsertKeepAliveData(info);
68 }
69
CancelKeepAlive(const KeepAliveInfo & info)70 int32_t AbilityKeepAliveService::CancelKeepAlive(const KeepAliveInfo &info)
71 {
72 KeepAliveStatus status = AbilityKeepAliveDataManager::GetInstance().QueryKeepAliveData(info);
73 if (status.code != ERR_OK && status.code != ERR_NAME_NOT_FOUND) {
74 TAG_LOGE(AAFwkTag::KEEP_ALIVE, "QueryKeepAliveData fail");
75 return status.code;
76 }
77
78 if (status.code == ERR_NAME_NOT_FOUND) {
79 return ERR_TARGET_BUNDLE_NOT_EXIST;
80 }
81
82 if (static_cast<int32_t>(status.setter) < static_cast<int32_t>(info.setter)) {
83 TAG_LOGE(AAFwkTag::KEEP_ALIVE, "app is set keep-alive by system, cannot unset by user");
84 return CHECK_PERMISSION_FAILED;
85 }
86
87 return AbilityKeepAliveDataManager::GetInstance().DeleteKeepAliveData(info);
88 }
89
QueryKeepAliveApplications(int32_t userId,int32_t appType,std::vector<KeepAliveInfo> & infoList)90 int32_t AbilityKeepAliveService::QueryKeepAliveApplications(int32_t userId,
91 int32_t appType, std::vector<KeepAliveInfo> &infoList)
92 {
93 GetValidUserId(userId);
94 KeepAliveInfo queryParam;
95 queryParam.userId = userId;
96 queryParam.appType = KeepAliveAppType(appType);
97 return AbilityKeepAliveDataManager::GetInstance().QueryKeepAliveApplications(
98 queryParam, infoList);
99 }
100
GetValidUserId(int32_t & userId)101 void AbilityKeepAliveService::GetValidUserId(int32_t &userId)
102 {
103 if (userId >= 0) {
104 return;
105 }
106 auto abilityMgr = DelayedSingleton<AbilityManagerService>::GetInstance();
107 if (abilityMgr == nullptr) {
108 TAG_LOGE(AAFwkTag::KEEP_ALIVE, "null abilityMgr");
109 return;
110 }
111
112 if (userId < 0) {
113 userId = abilityMgr->GetUserId();
114 }
115 }
116
IsKeepAliveApp(const std::string & bundleName,int32_t userId)117 bool AbilityKeepAliveService::IsKeepAliveApp(const std::string &bundleName, int32_t userId)
118 {
119 if (bundleName.empty()) {
120 TAG_LOGE(AAFwkTag::KEEP_ALIVE, "bundle name empty");
121 return false;
122 }
123
124 GetValidUserId(userId);
125 KeepAliveInfo info;
126 info.bundleName = bundleName;
127 info.userId = userId;
128 KeepAliveStatus status = AbilityKeepAliveDataManager::GetInstance().QueryKeepAliveData(info);
129 return (status.code == ERR_OK);
130 }
131
GetKeepAliveApplications(int32_t userId,std::vector<KeepAliveInfo> & infoList)132 int32_t AbilityKeepAliveService::GetKeepAliveApplications(int32_t userId, std::vector<KeepAliveInfo> &infoList)
133 {
134 KeepAliveInfo queryParam;
135 queryParam.userId = userId;
136 return AbilityKeepAliveDataManager::GetInstance().QueryKeepAliveApplications(
137 queryParam, infoList);
138 }
139 } // namespace AbilityRuntime
140 } // namespace OHOS
141