1 /* 2 * Copyright (c) 2021-2022 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_ABILITY_H 17 #define OHOS_ABILITY_RUNTIME_JS_ABILITY_H 18 19 #include "ability.h" 20 #include "ability_delegator_infos.h" 21 #include "freeze_util.h" 22 23 class NativeReference; 24 25 namespace OHOS { 26 namespace AbilityRuntime { 27 class JsRuntime; 28 29 using Ability = AppExecFwk::Ability; 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 37 class JsAbility : public Ability { 38 public: 39 static Ability *Create(const std::unique_ptr<Runtime> &runtime); 40 41 explicit JsAbility(JsRuntime &jsRuntime); 42 ~JsAbility() override; 43 44 void Init(const std::shared_ptr<AbilityInfo> &abilityInfo, const std::shared_ptr<OHOSApplication> application, 45 std::shared_ptr<AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 46 47 void OnStart(const Want &want, sptr<AAFwk::SessionInfo> sessionInfo = nullptr) override; 48 void OnStop() override; 49 void OnStop(AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, bool &isAsyncCallback) override; 50 void OnStopCallback() override; 51 int32_t OnContinue(WantParams &wantParams) override; 52 void OnConfigurationUpdated(const Configuration &configuration) override; 53 void UpdateContextConfiguration() override; 54 void OnMemoryLevel(int level) override; 55 void OnNewWant(const Want &want) override; 56 int32_t OnSaveState(int32_t reason, WantParams &wantParams) override; 57 58 void OnAbilityResult(int requestCode, int resultCode, const Want &resultData) override; 59 60 sptr<IRemoteObject> CallRequest() override; 61 62 /** 63 * @brief dump ability info 64 * 65 * @param params dump params that indicate different dump targets 66 * @param info dump ability info 67 */ 68 virtual void Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info) override; 69 70 std::shared_ptr<NativeReference> GetJsAbility(); 71 int32_t OnShare(WantParams &wantParams) override; 72 73 #ifdef SUPPORT_GRAPHICS 74 public: 75 void OnSceneCreated() override; 76 void onSceneDestroyed() override; 77 void OnSceneRestored() override; 78 79 void OnForeground(const Want &want) override; 80 void OnBackground() override; 81 bool OnBackPress() override; 82 bool OnPrepareTerminate() override; 83 84 std::shared_ptr<NativeReference> GetJsWindowStage(); 85 const JsRuntime& GetJsRuntime(); 86 87 protected: 88 void DoOnForeground(const Want &want) override; 89 void RequestFocus(const Want &want) override; 90 void ContinuationRestore(const Want &want) override; 91 92 private: 93 bool IsRestorePageStack(const Want &want); 94 void RestorePageStack(const Want &want); 95 void GetPageStackFromWant(const Want &want, std::string &pageStack); 96 void AbilityContinuationOrRecover(const Want &want); 97 std::shared_ptr<NativeReference> jsWindowStageObj_; 98 #endif 99 100 private: 101 napi_value CallObjectMethod(const char *name, napi_value const *argv = nullptr, size_t argc = 0, 102 bool withResult = false); 103 bool CheckPromise(napi_value result); 104 bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo); 105 std::unique_ptr<NativeReference> CreateAppWindowStage(); 106 std::shared_ptr<AppExecFwk::ADelegatorAbilityProperty> CreateADelegatorAbilityProperty(); 107 sptr<IRemoteObject> SetNewRuleFlagToCallee(napi_env env, napi_value remoteJsObj); 108 void AddLifecycleEventBeforeJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const; 109 void AddLifecycleEventAfterJSCall(FreezeUtil::TimeoutState state, const std::string &methodName) const; 110 std::string GenerateSrcPath(std::shared_ptr<AbilityInfo> abilityInfo) const; 111 void BindContext(); 112 bool InitWindowScene(const Want &want); 113 bool AddDumpInfo(napi_env env, napi_value obj, const std::vector<std::string> ¶ms, 114 std::vector<std::string> &info, const std::string &methodName) const; 115 116 JsRuntime &jsRuntime_; 117 std::shared_ptr<NativeReference> shellContextRef_; 118 std::shared_ptr<NativeReference> jsAbilityObj_; 119 sptr<IRemoteObject> remoteCallee_; 120 }; 121 } // namespace AbilityRuntime 122 } // namespace OHOS 123 #endif // OHOS_ABILITY_RUNTIME_JS_ABILITY_H 124