1 /* 2 * Copyright (c) 2021 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 #ifndef OHOS_AAFWK_APP_SCHEDULER_H 17 #define OHOS_AAFWK_APP_SCHEDULER_H 18 19 #include <memory> 20 #include <unordered_set> 21 22 #include "ability_info.h" 23 // #include "app_process_data.h" 24 #include "appmgr/app_mgr_client.h" 25 #include "appmgr/app_state_callback_host.h" 26 #include "application_info.h" 27 #include "iremote_object.h" 28 #include "refbase.h" 29 #include "singleton.h" 30 31 namespace OHOS { 32 namespace AAFwk { 33 /** 34 * @enum AppAbilityState 35 * AppAbilityState defines the life cycle state of app ability. 36 */ 37 enum class AppAbilityState { 38 ABILITY_STATE_UNDEFINED = 0, 39 ABILITY_STATE_FOREGROUND, 40 ABILITY_STATE_BACKGROUND, 41 ABILITY_STATE_END, 42 }; 43 44 enum class AppState { 45 BEGIN = 0, 46 READY, 47 FOREGROUND, 48 BACKGROUND, 49 SUSPENDED, 50 TERMINATED, 51 END, 52 }; 53 54 struct AppInfo { 55 std::string appName; 56 std::string processName; 57 int32_t uid; 58 AppState state; 59 }; 60 /** 61 * @class AppStateCallback 62 * AppStateCallback. 63 */ 64 class AppStateCallback { 65 public: AppStateCallback()66 AppStateCallback() 67 {} ~AppStateCallback()68 virtual ~AppStateCallback() 69 {} 70 71 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const int32_t state) = 0; 72 73 virtual void OnAppStateChanged(const AppInfo &info) = 0; 74 }; 75 76 /** 77 * @class AppScheduler 78 * AppScheduler , access app manager service. 79 */ 80 class AppScheduler : virtual RefBase, public AppExecFwk::AppStateCallbackHost { 81 82 DECLARE_DELAYED_SINGLETON(AppScheduler) 83 public: 84 /** 85 * init app scheduler. 86 * @param callback, app state call back. 87 * @return true on success ,false on failure. 88 */ 89 bool Init(const std::weak_ptr<AppStateCallback> &callback); 90 91 /** 92 * load ability with token, ability info and application info. 93 * 94 * @param token, the token of ability. 95 * @param preToken, the token of ability's caller. 96 * @param abilityinfo, ability info. 97 * @param application, application info. 98 * @return true on success ,false on failure. 99 */ 100 int LoadAbility(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 101 const AppExecFwk::AbilityInfo &abilityinfo, const AppExecFwk::ApplicationInfo &application); 102 103 /** 104 * terminate ability with token. 105 * 106 * @param token, the token of ability. 107 * @return true on success ,false on failure. 108 */ 109 int TerminateAbility(const sptr<IRemoteObject> &token); 110 111 /** 112 * move ability to forground. 113 * 114 * @param token, the token of ability. 115 */ 116 void MoveToForground(const sptr<IRemoteObject> &token); 117 118 /** 119 * move ability to background. 120 * 121 * @param token, the token of ability. 122 */ 123 void MoveToBackground(const sptr<IRemoteObject> &token); 124 125 /** 126 * AbilityBehaviorAnalysis, ability behavior analysis assistant process optimization. 127 * 128 * @param token, the unique identification to start the ability. 129 * @param preToken, the unique identification to call the ability. 130 * @param visibility, the visibility information about windows info. 131 * @param perceptibility, the Perceptibility information about windows info. 132 * @param connectionState, the service ability connection state. 133 * @return Returns RESULT_OK on success, others on failure. 134 */ 135 void AbilityBehaviorAnalysis(const sptr<IRemoteObject> &token, const sptr<IRemoteObject> &preToken, 136 const int32_t visibility, const int32_t perceptibility, const int32_t connectionState); 137 138 /** 139 * KillProcessByAbilityToken, call KillProcessByAbilityToken() through proxy object, 140 * kill the process by ability token. 141 * 142 * @param token, the unique identification to the ability. 143 */ 144 void KillProcessByAbilityToken(const sptr<IRemoteObject> &token); 145 146 /** 147 * convert ability state to app ability state. 148 * 149 * @param state, the state of ability. 150 */ 151 AppAbilityState ConvertToAppAbilityState(const int32_t state); 152 153 /** 154 * get ability state. 155 * 156 * @return state, the state of app ability. 157 */ 158 AppAbilityState GetAbilityState() const; 159 160 /** 161 * kill the application 162 * 163 * @param bundleName. 164 */ 165 int KillApplication(const std::string &bundleName); 166 167 void AttachTimeOut(const sptr<IRemoteObject> &token); 168 169 /** 170 * Checks whether a specified permission has been granted to the process identified by pid and uid 171 * 172 * @param permission Indicates the permission to check. 173 * @param pid Indicates the ID of the process to check. 174 * @param uid Indicates the UID of the process to check. 175 * @param message Describe success or failure 176 * 177 * @return Returns ERR_OK on success, others on failure. 178 */ 179 int CompelVerifyPermission(const std::string &permission, int pid, int uid, std::string &message); 180 181 protected: 182 /** 183 * OnAbilityRequestDone, app manager service call this interface after ability request done. 184 * 185 * @param token,ability's token. 186 * @param state,the state of ability lift cycle. 187 */ 188 virtual void OnAbilityRequestDone(const sptr<IRemoteObject> &token, const AppExecFwk::AbilityState state) override; 189 190 /** 191 * Application state changed callback. 192 * 193 * @param appProcessData Process data 194 */ 195 virtual void OnAppStateChanged(const AppExecFwk::AppProcessData &appData) override; 196 197 private: 198 std::weak_ptr<AppStateCallback> callback_; 199 std::unique_ptr<AppExecFwk::AppMgrClient> appMgrClient_; 200 AppAbilityState appAbilityState_ = AppAbilityState::ABILITY_STATE_UNDEFINED; 201 }; 202 } // namespace AAFwk 203 } // namespace OHOS 204 #endif // OHOS_AAFWK_APP_SCHEDULER_H 205