1 /* 2 * Copyright (c) 2021-2023 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_ABILITY_RUNTIME_EXTENSION_H 17 #define OHOS_ABILITY_RUNTIME_EXTENSION_H 18 19 #include <string> 20 21 #include "ability_transaction_callback_info.h" 22 #include "napi_remote_object.h" 23 #include "iremote_object.h" 24 #include "session_info.h" 25 #include "foundation/window/window_manager/interfaces/innerkits/wm/window.h" 26 #include "ui_extension_window_command.h" 27 #include "want.h" 28 29 namespace OHOS { 30 namespace AppExecFwk { 31 struct AbilityInfo; 32 class OHOSApplication; 33 class AbilityHandler; 34 class AbilityLocalRecord; 35 class Configuration; 36 } 37 namespace AbilityRuntime { 38 using Want = OHOS::AAFwk::Want; 39 /** 40 * @brief The Extension component to schedule task with no pages. 41 */ 42 class Extension : public std::enable_shared_from_this<Extension> { 43 public: 44 Extension() = default; 45 virtual ~Extension() = default; 46 47 /** 48 * @brief Init the extension. 49 * 50 * @param record the extension record. 51 * @param application the application info. 52 * @param handler the extension handler. 53 * @param token the remote token. 54 */ 55 virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 56 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 57 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, 58 const sptr<IRemoteObject> &token); 59 60 /** 61 * @brief Sets the first want object. 62 * 63 * @param Want information of other ability or extension. 64 */ 65 void SetLaunchWant(const AAFwk::Want &want); 66 67 /** 68 * @brief Obtains the first want object. 69 * 70 * @return Returns the first want object. 71 */ 72 std::shared_ptr<AAFwk::Want> GetLaunchWant(); 73 74 /** 75 * @brief Sets the last want object. 76 * 77 * @param Want information of other ability or extension. 78 */ 79 void SetLastRequestWant(const AAFwk::Want &want); 80 81 /** 82 * @brief Sets the callingInfo from IPC. 83 * 84 * @param CallingInfo information of caller. 85 */ 86 void SetCallingInfo(const CallingInfo &callingInfo); 87 88 /** 89 * @return std::shared_ptr<CallingInfo> the pointer of callingInfo. 90 */ 91 std::shared_ptr<CallingInfo> GetCallingInfo(); 92 93 /** 94 * @brief Called when this extension is started. You must override this function if you want to perform some 95 * initialization operations during extension startup. 96 * 97 * This function can be called only once in the entire lifecycle of an extension. 98 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 99 */ 100 virtual void OnStart(const AAFwk::Want &want); 101 virtual void OnStart(const AAFwk::Want &want, sptr<AAFwk::SessionInfo> sessionInfo); 102 103 /** 104 * @brief Called when this Service extension is connected for the first time. 105 * 106 * You can override this function to implement your own processing logic. 107 * 108 * @param want Indicates the {@link Want} structure containing connection information about the Service extension. 109 * @return Returns a pointer to the <b>sid</b> of the connected Service extension. 110 */ 111 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want); 112 113 /** 114 * @brief Called when this Service extension is connected for the first time. 115 * 116 * You can override this function to implement your own processing logic. 117 * 118 * @param want Indicates the {@link Want} structure containing connection information about the Service extension. 119 * @param callbackInfo Indicates the lifecycle transaction callback information 120 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 121 * @return Returns a pointer to the <b>sid</b> of the connected Service extension. 122 */ 123 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want, 124 AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback); 125 126 /** 127 * @brief Called when all abilities connected to this Service extension are disconnected. 128 * 129 * You can override this function to implement your own processing logic. 130 * 131 */ 132 virtual void OnDisconnect(const AAFwk::Want &want); 133 134 /** 135 * @brief Called when all abilities connected to this Service extension are disconnected. 136 * 137 * You can override this function to implement your own processing logic. 138 * @param callbackInfo Indicates the lifecycle transaction callback information 139 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 140 */ 141 virtual void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, 142 bool &isAsyncCallback); 143 144 /** 145 * @brief Called back when Service is started. 146 * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start 147 * Service. Then the system calls back the current method to use the transferred want parameter to execute its own 148 * logic. 149 * 150 * @param want Indicates the want of Service to start. 151 * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 152 * destroyed, and the value false indicates a normal startup. 153 * @param startId Indicates the number of times the Service extension has been started. The startId is 154 * incremented by 1 every time the extension is started. For example, if the extension has been started 155 * for six times, the value of startId is 6. 156 */ 157 virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId); 158 159 virtual void OnCommandWindow(const AAFwk::Want &want, const sptr<AAFwk::SessionInfo> &sessionInfo, 160 AAFwk::WindowCommand winCmd); 161 /** 162 * @brief Called when this extension enters the <b>STATE_STOP</b> state. 163 * 164 * The extension in the <b>STATE_STOP</b> is being destroyed. 165 * You can override this function to implement your own processing logic. 166 */ 167 virtual void OnStop(); 168 /** 169 * @brief Called when this extension enters the <b>STATE_STOP</b> state. 170 * 171 * The ability in the <b>STATE_STOP</b> is being destroyed. 172 * You can override this function to implement your own processing logic. 173 * 174 * @param callbackInfo Indicates the lifecycle transaction callback information 175 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 176 */ 177 virtual void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback); 178 179 /** 180 * @brief Called when the system configuration is updated. 181 * 182 * @param configuration Indicates the updated configuration information. 183 */ 184 185 /** 186 * @brief The callback of OnStop. 187 */ 188 virtual void OnStopCallBack(); 189 190 virtual void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration); 191 192 /** 193 * @brief Notify current memory level. 194 * 195 * @param level Current memory level. 196 */ 197 virtual void OnMemoryLevel(int level); 198 199 /** 200 * @brief Called when this extension enters the <b>STATE_FOREGROUND</b> state. 201 * 202 * 203 * The extension in the <b>STATE_FOREGROUND</b> state is visible. 204 * You can override this function to implement your own processing logic. 205 */ 206 virtual void OnForeground(const Want &want); 207 208 /** 209 * @brief Called when this extension enters the <b>STATE_BACKGROUND</b> state. 210 * 211 * 212 * The extension in the <b>STATE_BACKGROUND</b> state is invisible. 213 * You can override this function to implement your own processing logic. 214 */ 215 virtual void OnBackground(); 216 217 /** 218 * @brief Called when extension need dump info. 219 * 220 * @param params The params from service. 221 * @param info The dump info to show. 222 */ 223 virtual void Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info); 224 225 void SetExtensionWindowLifeCycleListener(const sptr<Rosen::IWindowLifeCycle> &listener); 226 227 /** 228 * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an extension ability 229 * and the result is returned. 230 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 231 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 232 * @param resultCode Indicates the result code returned after the ability is started. You can define the result 233 * code to identify an error. 234 * @param want Indicates the data returned after the ability is started. You can define the data returned. The 235 * value can be null. 236 */ 237 virtual void OnAbilityResult(int requestCode, int resultCode, const Want &want); 238 239 std::shared_ptr<AppExecFwk::AbilityInfo> abilityInfo_ = nullptr; 240 protected: 241 std::shared_ptr<AppExecFwk::AbilityHandler> handler_ = nullptr; 242 243 // window scene 244 sptr<Rosen::IWindowLifeCycle> extensionWindowLifeCycleListener_ = nullptr; 245 private: 246 std::shared_ptr<AppExecFwk::OHOSApplication> application_ = nullptr; 247 std::shared_ptr<AAFwk::Want> launchWant_ = nullptr; 248 std::shared_ptr<AAFwk::Want> lastRequestWant_ = nullptr; 249 std::shared_ptr<CallingInfo> callingInfo_ = nullptr; 250 }; 251 } // namespace AbilityRuntime 252 } // namespace OHOS 253 #endif // OHOS_ABILITY_RUNTIME_EXTENSION_H 254