1 /* 2 * Copyright (c) 2023-2025 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_JS_UI_ABILITY_H 17 #define OHOS_ABILITY_RUNTIME_JS_UI_ABILITY_H 18 19 #include "ability_delegator_infos.h" 20 #include "freeze_util.h" 21 #include "js_embeddable_ui_ability_context.h" 22 #include "ui_ability.h" 23 24 class NativeReference; 25 26 namespace OHOS { 27 namespace AbilityRuntime { 28 class JsRuntime; 29 struct InsightIntentExecutorInfo; 30 using AbilityHandler = AppExecFwk::AbilityHandler; 31 using AbilityInfo = AppExecFwk::AbilityInfo; 32 using OHOSApplication = AppExecFwk::OHOSApplication; 33 using Want = AppExecFwk::Want; 34 using AbilityStartSetting = AppExecFwk::AbilityStartSetting; 35 using Configuration = AppExecFwk::Configuration; 36 using InsightIntentExecuteResult = AppExecFwk::InsightIntentExecuteResult; 37 using InsightIntentExecuteParam = AppExecFwk::InsightIntentExecuteParam; 38 using InsightIntentExecutorAsyncCallback = AppExecFwk::InsightIntentExecutorAsyncCallback; 39 40 class JsUIAbility : public UIAbility { 41 public: 42 /** 43 * @brief Create a JsUIAbility instance through the singleton pattern 44 * @param runtime The runtime of the ability 45 * @return Returns the JsUIability Instance point 46 */ 47 static UIAbility *Create(const std::unique_ptr<Runtime> &runtime); 48 49 explicit JsUIAbility(JsRuntime &jsRuntime); 50 ~JsUIAbility() override; 51 52 /** 53 * @brief Init the UIability 54 * @param abilityInfo Indicate the Ability information 55 * @param application Indicates the main process 56 * @param handler the UIability EventHandler object 57 * @param token the remote token 58 */ 59 void Init(std::shared_ptr<AppExecFwk::AbilityLocalRecord> record, 60 const std::shared_ptr<OHOSApplication> application, 61 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 62 63 /** 64 * @brief OnStart,Start JsUIability 65 * @param want Indicates the {@link Want} structure containing startup information about the ability 66 * @param sessionInfo Indicates the sessionInfo 67 */ 68 void OnStart(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr) override; 69 70 /** 71 * @brief Called when this ability enters the <b>STATE_STOP</b> state. 72 * The ability in the <b>STATE_STOP</b> is being destroyed. 73 * You can override this function to implement your own processing logic. 74 */ 75 void OnStop() override; 76 77 /** 78 * @brief Called when this ability enters the <b>STATE_STOP</b> state. 79 * The ability in the <b>STATE_STOP</b> is being destroyed. 80 * You can override this function to implement your own processing logic. 81 * @param callbackInfo Indicates the lifecycle transaction callback information 82 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 83 */ 84 void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override; 85 86 /** 87 * @brief The callback of OnStop. 88 */ 89 void OnStopCallback() override; 90 91 /** 92 * @brief The sync callback of OnContinue. 93 */ 94 int32_t OnContinueSyncCB(napi_value result, WantParams &wantParams, napi_value jsWantParams); 95 96 /** 97 * @brief The async callback of OnContinue. 98 */ 99 int32_t OnContinueAsyncCB(napi_ref jsWantParams, int32_t status, 100 const AppExecFwk::AbilityInfo &abilityInfo) override; 101 102 /** 103 * @brief Prepare user data of local Ability. 104 * @param wantParams Indicates the user data to be saved. 105 * @return If the ability is willing to continue and data saved successfully, it returns 0; 106 * otherwise, it returns errcode. 107 */ 108 int32_t OnContinue(WantParams &wantParams, bool &isAsyncOnContinue, 109 const AppExecFwk::AbilityInfo &abilityInfo) override; 110 111 /** 112 * @brief Update configuration 113 * @param configuration Indicates the updated configuration information. 114 */ 115 void OnConfigurationUpdated(const Configuration &configuration) override; 116 117 /** 118 * @brief Update Contextconfiguration 119 */ 120 void UpdateContextConfiguration() override; 121 122 /** 123 * @brief Called when the system configuration is updated. 124 * @param level Indicates the memory trim level, which shows the current memory usage status. 125 */ 126 void OnMemoryLevel(int level) override; 127 128 /** 129 * @brief Called when the launch mode of an ability is set to singleInstance. This happens when you re-launch an 130 * ability that has been at the top of the ability stack. 131 * @param want Indicates the new Want containing information about the ability. 132 */ 133 void OnNewWant(const Want &want) override; 134 135 /** 136 * @brief Prepare user data of local Ability. 137 * @param reason the reason why framework invoke this function 138 * @param wantParams Indicates the user data to be saved. 139 * @return result code defined in abilityConstants 140 */ 141 int32_t OnSaveState(int32_t reason, WantParams &wantParams) override; 142 143 /** 144 * @brief Called when startAbilityForResult(ohos.aafwk.content.Want,int) is called to start an ability and the 145 * result is returned. This method is called only on Page abilities. You can start a new ability to perform some 146 * calculations and use setResult (int,ohos.aafwk.content.Want) to return the calculation result. Then the system 147 * calls back the current method to use the returned data to execute its own logic. 148 * @param requestCode Indicates the request code returned after the ability is started. You can define the request 149 * code to identify the results returned by abilities. The value ranges from 0 to 65535. 150 * @param resultCode Indicates the result code returned after the ability is started. You can define the result code 151 * to identify an error. 152 * @param want Indicates the data returned after the ability is started. You can define the data returned. The 153 * value can be null. 154 */ 155 void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override; 156 157 /** 158 * @brief request a remote object of callee from this ability. 159 * @return Returns the remote object of callee. 160 */ 161 sptr<IRemoteObject> CallRequest() override; 162 163 /** 164 * @brief dump ability info 165 * @param params dump params that indicate different dump targets 166 * @param info dump ability info 167 */ 168 void Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info) override; 169 170 void OnAfterFocusedCommon(bool isFocused) override; 171 172 /** 173 * @brief Get JsAbility 174 * @return Return the JsAbility 175 */ 176 std::shared_ptr<NativeReference> GetJsAbility(); 177 178 /** 179 * @brief Callback when the ability is shared.You can override this function to implement your own sharing logic. 180 * @param wantParams Indicates the user data to be saved. 181 * @return the result of OnShare 182 */ 183 int32_t OnShare(WantParams &wantParams) override; 184 185 /** 186 * @brief Set the continueState of an application to window. 187 * @param state Indicates the continueState of an application. 188 */ 189 void SetContinueState(int32_t state) override; 190 191 #ifdef SUPPORT_SCREEN 192 public: 193 /** 194 * @brief Called after instantiating WindowScene. 195 * You can override this function to implement your own processing logic. 196 */ 197 void OnSceneCreated() override; 198 199 /** 200 * @brief Called after ability stoped. 201 * You can override this function to implement your own processing logic. 202 */ 203 void OnSceneWillDestroy() override; 204 205 /** 206 * @brief Called after ability stoped. 207 * You can override this function to implement your own processing logic. 208 */ 209 void onSceneDestroyed() override; 210 211 /** 212 * @brief Called after ability restored. 213 * You can override this function to implement your own processing logic. 214 */ 215 void OnSceneRestored() override; 216 217 /** 218 * @brief Called when this ability enters the <b>STATE_FOREGROUND</b> state. 219 * The ability in the <b>STATE_FOREGROUND</b> state is visible. 220 * You can override this function to implement your own processing logic. 221 */ 222 void OnForeground(const Want &want) override; 223 224 /** 225 * @brief Call "onForeground" js function barely. 226 * 227 * @param want Want 228 */ 229 void CallOnForegroundFunc(const Want &want) override; 230 231 /** 232 * @brief Request focus for current window, can be override. 233 * 234 * @param want Want 235 */ 236 void RequestFocus(const Want &want) override; 237 238 /** 239 * @brief Called when this ability enters the <b>STATE_BACKGROUND</b> state. 240 * The ability in the <b>STATE_BACKGROUND</b> state is invisible. 241 * You can override this function to implement your own processing logic. 242 */ 243 void OnBackground() override; 244 245 /** 246 * @brief Called before this ability enters the <b>STATE_FOREGROUND</b> state. 247 * The ability in the <b>STATE_FOREGROUND</b> state is invisible. 248 * You can override this function to implement your own processing logic. 249 */ 250 void OnWillForeground() override; 251 252 /** 253 * @brief Called after wms show event. 254 * The ability in the <b>STATE_FOREGROUND</b> state is invisible. 255 * You can override this function to implement your own processing logic. 256 */ 257 void OnDidForeground() override; 258 259 /** 260 * @brief Called before OnBackground. 261 * The ability in the <b>STATE_BACKGROUND</b> state is invisible. 262 * You can override this function to implement your own processing logic. 263 */ 264 void OnWillBackground() override; 265 266 /** 267 * @brief Called after wms hiden event. 268 * The ability in the <b>STATE_BACKGROUND</b> state is invisible. 269 * You can override this function to implement your own processing logic. 270 */ 271 void OnDidBackground() override; 272 273 /** 274 * Called when back press is dispatched. 275 * Return true if ability will be moved to background; return false if will be terminated 276 */ 277 bool OnBackPress() override; 278 279 /** 280 * @brief Called when ability prepare terminate. 281 * @param callbackInfo The callbackInfo is used when onPrepareToTerminateAsync is implemented. 282 * @param isAsync The returned flag indicates if onPrepareToTerminateAsync is implemented. 283 */ 284 void OnPrepareTerminate(AppExecFwk::AbilityTransactionCallbackInfo<bool> *callbackInfo, bool &isAsync) override; 285 286 /** 287 * @brief Get JsWindow Stage 288 * @return Returns the current NativeReference 289 */ 290 std::shared_ptr<NativeReference> GetJsWindowStage(); 291 292 /** 293 * @brief Get JsRuntime 294 * @return Returns the current JsRuntime 295 */ 296 const JsRuntime &GetJsRuntime(); 297 298 /** 299 * @brief Execute insight intent when an ability is in foreground, schedule it to foreground repeatly. 300 * 301 * @param want Want. 302 * @param executeParam insight intent execute param. 303 * @param callback insight intent async callback. 304 */ 305 void ExecuteInsightIntentRepeateForeground(const Want &want, 306 const std::shared_ptr<InsightIntentExecuteParam> &executeParam, 307 std::unique_ptr<InsightIntentExecutorAsyncCallback> callback) override; 308 309 /** 310 * @brief Execute insight intent when an ability didn't started or in background, schedule it to foreground. 311 * 312 * @param want Want. 313 * @param executeParam insight intent execute param. 314 * @param callback insight intent async callback. 315 */ 316 void ExecuteInsightIntentMoveToForeground(const Want &want, 317 const std::shared_ptr<InsightIntentExecuteParam> &executeParam, 318 std::unique_ptr<InsightIntentExecutorAsyncCallback> callback) override; 319 320 /** 321 * @brief Execute insight intent when an ability didn't started, schedule it to background. 322 * 323 * @param want Want. 324 * @param executeParam insight intent execute param. 325 * @param callback insight intent async callback. 326 */ 327 virtual void ExecuteInsightIntentBackground(const AAFwk::Want &want, 328 const std::shared_ptr<InsightIntentExecuteParam> &executeParam, 329 std::unique_ptr<InsightIntentExecutorAsyncCallback> callback) override; 330 331 /** 332 * @brief Called when distributed system trying to collaborate remote ability. 333 * @param want want with collaborative info. 334 */ 335 void HandleCollaboration(const Want &want) override; 336 337 protected: 338 void DoOnForeground(const Want &want) override; 339 void ContinuationRestore(const Want &want) override; 340 341 private: 342 bool IsRestorePageStack(const Want &want); 343 void RestorePageStack(const Want &want); 344 void GetPageStackFromWant(const Want &want, std::string &pageStack); 345 void AbilityContinuationOrRecover(const Want &want); 346 void UpdateJsWindowStage(napi_value windowStage); 347 inline bool GetInsightIntentExecutorInfo(const Want &want, 348 const std::shared_ptr<InsightIntentExecuteParam> &executeParam, 349 InsightIntentExecutorInfo& executeInfo); 350 int32_t OnCollaborate(WantParams &wantParams); 351 352 std::shared_ptr<NativeReference> jsWindowStageObj_; 353 int32_t windowMode_ = 0; 354 #endif 355 356 private: 357 napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0, 358 bool withResult = false, bool showMethodNotFoundLog = true); 359 bool CheckPromise(napi_value result); 360 bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo); 361 bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<int32_t> *callbackInfo); 362 bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<bool> *callbackInfo); 363 std::unique_ptr<NativeReference> CreateAppWindowStage(); 364 std::shared_ptr<AppExecFwk::ADelegatorAbilityProperty> CreateADelegatorAbilityProperty(); 365 sptr<IRemoteObject> SetNewRuleFlagToCallee(napi_env env, napi_value remoteJsObj); 366 void SetAbilityContext(std::shared_ptr<AbilityInfo> abilityInfo, 367 std::shared_ptr<AAFwk::Want> want, const std::string &moduleName, const std::string &srcPath); 368 void DoOnForegroundForSceneIsNull(const Want &want); 369 void GetDumpInfo( 370 napi_env env, napi_value dumpInfo, napi_value onDumpInfo, std::vector<std::string> &info); 371 void AddLifecycleEventBeforeJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const; 372 void AddLifecycleEventAfterJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const; 373 void CreateJSContext(napi_env env, napi_value &contextObj, int32_t screenMode); 374 bool CheckSatisfyTargetAPIVersion(int32_t targetAPIVersion); 375 bool BackPressDefaultValue(); 376 void UpdateAbilityObj(std::shared_ptr<AbilityInfo> abilityInfo, 377 const std::string &moduleName, const std::string &srcPath); 378 void ReleaseOnContinueAsset(const napi_env env, napi_value &promise, 379 napi_ref &jsWantParamsRef, AppExecFwk::AbilityTransactionCallbackInfo<int32_t> *callbackInfo); 380 381 JsRuntime &jsRuntime_; 382 std::shared_ptr<NativeReference> shellContextRef_; 383 std::shared_ptr<NativeReference> jsAbilityObj_; 384 std::shared_ptr<int32_t> screenModePtr_; 385 sptr<IRemoteObject> remoteCallee_; 386 }; 387 } // namespace AbilityRuntime 388 } // namespace OHOS 389 #endif // OHOS_ABILITY_RUNTIME_JS_UI_ABILITY_H 390