• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "cj_app_manager.h"
17 
18 #include <vector>
19 
20 #include "ability_manager_interface.h"
21 #include "ability_manager_errors.h"
22 #include "hilog_tag_wrapper.h"
23 #include "app_mgr_interface.h"
24 #include "if_system_ability_manager.h"
25 #include "iservice_registry.h"
26 #include "system_ability_definition.h"
27 #include "cj_ability_runtime_error.h"
28 #include "cj_application_context.h"
29 
30 using namespace OHOS::AbilityRuntime;
31 
32 namespace OHOS {
33 namespace AbilityRuntime {
34 enum CjAppProcessState {
35     STATE_CREATE,
36     STATE_FOREGROUND,
37     STATE_ACTIVE,
38     STATE_BACKGROUND,
39     STATE_DESTROY,
40 };
41 
GetAppManagerInstance()42 OHOS::sptr<OHOS::AppExecFwk::IAppMgr> GetAppManagerInstance()
43 {
44     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
45         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
46     OHOS::sptr<OHOS::IRemoteObject> appObject = systemAbilityManager->GetSystemAbility(OHOS::APP_MGR_SERVICE_ID);
47     return OHOS::iface_cast<OHOS::AppExecFwk::IAppMgr>(appObject);
48 }
49 
GetAbilityManagerInstance()50 OHOS::sptr<OHOS::AAFwk::IAbilityManager> GetAbilityManagerInstance()
51 {
52     OHOS::sptr<OHOS::ISystemAbilityManager> systemAbilityManager =
53         OHOS::SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
54     OHOS::sptr<OHOS::IRemoteObject> abilityObject =
55         systemAbilityManager->GetSystemAbility(OHOS::ABILITY_MGR_SERVICE_ID);
56     return OHOS::iface_cast<OHOS::AAFwk::IAbilityManager>(abilityObject);
57 }
58 
ConvertToCJAppProcessState(const AppExecFwk::AppProcessState & appProcessState,const bool & isFocused)59 CjAppProcessState ConvertToCJAppProcessState(
60     const AppExecFwk::AppProcessState &appProcessState, const bool &isFocused)
61 {
62     CjAppProcessState processState;
63     switch (appProcessState) {
64         case AppExecFwk::AppProcessState::APP_STATE_CREATE:
65         case AppExecFwk::AppProcessState::APP_STATE_READY:
66             processState = STATE_CREATE;
67             break;
68         case AppExecFwk::AppProcessState::APP_STATE_FOREGROUND:
69             processState = isFocused ? STATE_ACTIVE : STATE_FOREGROUND;
70             break;
71         case AppExecFwk::AppProcessState::APP_STATE_BACKGROUND:
72             processState = STATE_BACKGROUND;
73             break;
74         case AppExecFwk::AppProcessState::APP_STATE_TERMINATED:
75         case AppExecFwk::AppProcessState::APP_STATE_END:
76             processState = STATE_DESTROY;
77             break;
78         default:
79             TAG_LOGE(AAFwkTag::APPKIT, "Process state is invalid.");
80             processState = STATE_DESTROY;
81             break;
82     }
83     return processState;
84 }
85 }  // namespace AbilityRuntime
86 }  // namespace OHOS
87 
FfiAppMgrIsRunningInStabilityTest(int32_t * err)88 CJ_EXPORT bool FfiAppMgrIsRunningInStabilityTest(int32_t* err)
89 {
90     if (err == nullptr) {
91         TAG_LOGE(AAFwkTag::APPKIT, "param err is nullptr!");
92         return false;
93     }
94     auto abilityManager = GetAbilityManagerInstance();
95     if (abilityManager == nullptr) {
96         TAG_LOGE(AAFwkTag::APPKIT, "abilityManager is nullptr!");
97         *err = ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR;
98         return false;
99     }
100     return abilityManager->IsRunningInStabilityTest();
101 }
102 
FfiAppMgrIsRamConstrainedDevice(int32_t * err)103 CJ_EXPORT bool FfiAppMgrIsRamConstrainedDevice(int32_t* err)
104 {
105     if (err == nullptr) {
106         TAG_LOGE(AAFwkTag::APPKIT, "param err is nullptr!");
107         return false;
108     }
109     auto abilityManager = GetAbilityManagerInstance();
110     if (abilityManager == nullptr) {
111         TAG_LOGE(AAFwkTag::APPKIT, "abilityManager is nullptr!");
112         *err = ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR;
113         return false;
114     }
115     return abilityManager->IsRamConstrainedDevice();
116 }
117 
FfiAppMgrGetAppMemorySize(int32_t * err)118 CJ_EXPORT int32_t FfiAppMgrGetAppMemorySize(int32_t* err)
119 {
120     if (err == nullptr) {
121         TAG_LOGE(AAFwkTag::APPKIT, "param err is nullptr!");
122         return 0;
123     }
124     auto abilityManager = GetAbilityManagerInstance();
125     if (abilityManager == nullptr) {
126         TAG_LOGE(AAFwkTag::APPKIT, "abilityManager is nullptr!");
127         *err = ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR;
128         return 0;
129     }
130     return abilityManager->GetAppMemorySize();
131 }
132 
FfiAppMgrGetRunningProcessInformation(int32_t * err)133 CJ_EXPORT CArrProcessInformation FfiAppMgrGetRunningProcessInformation(int32_t* err)
134 {
135     CArrProcessInformation processInfos {};
136     if (err == nullptr) {
137         TAG_LOGE(AAFwkTag::APPKIT, "param err is nullptr!");
138         return processInfos;
139     }
140     auto appManager = GetAppManagerInstance();
141     if (appManager == nullptr) {
142         TAG_LOGE(AAFwkTag::APPKIT, "appManager is nullptr!");
143         *err = ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR;
144         return processInfos;
145     }
146     std::vector<OHOS::AppExecFwk::RunningProcessInfo> infos;
147     auto ret = appManager->GetAllRunningProcesses(infos);
148     if (ret != 0) {
149         *err = ret;
150         TAG_LOGE(AAFwkTag::APPKIT, "GetAllRunningProcesses failed!");
151         return processInfos;
152     }
153     // convert result
154     if (!infos.empty()) {
155         CProcessInformation* head = static_cast<CProcessInformation*>(malloc(sizeof(*head) * infos.size()));
156         if (head == nullptr) {
157             *err = ERR_ABILITY_RUNTIME_EXTERNAL_INTERNAL_ERROR;
158             return processInfos;
159         }
160         for (size_t i = 0; i < infos.size(); i++) {
161             auto& processInfo = infos[i];
162             head[i].processName = CreateCStringFromString(processInfo.processName_);
163             head[i].pid = processInfo.pid_;
164             head[i].uid = processInfo.uid_;
165             head[i].bundleNames.head = VectorToCArrString(processInfo.bundleNames);
166             head[i].bundleNames.size = static_cast<int64_t>((processInfo.bundleNames).size());
167             head[i].state = ConvertToCJAppProcessState(processInfo.state_, processInfo.isFocused);
168             head[i].bundleType = processInfo.bundleType;
169             head[i].appCloneIndex = processInfo.appCloneIndex;
170         }
171         processInfos.size = static_cast<int64_t>(infos.size());
172         processInfos.head = head;
173     }
174     return processInfos;
175 }
176