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 22 class NativeReference; 23 class NativeValue; 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 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) 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 72 #ifdef SUPPORT_GRAPHICS 73 public: 74 void OnSceneCreated() override; 75 void onSceneDestroyed() override; 76 void OnSceneRestored() override; 77 78 void OnForeground(const Want &want) override; 79 void OnBackground() override; 80 81 std::shared_ptr<NativeReference> GetJsWindowStage(); 82 83 protected: 84 void DoOnForeground(const Want &want) override; 85 void RequestFocus(const Want &want) override; 86 void ContinuationRestore(const Want &want) override; 87 88 private: 89 bool IsRestorePageStack(const Want &want); 90 void RestorePageStack(const Want &want); 91 void GetPageStackFromWant(const Want &want, std::string &pageStack); 92 void AbilityContinuationOrRecover(const Want &want); 93 std::shared_ptr<NativeReference> jsWindowStageObj_; 94 #endif 95 96 private: 97 NativeValue *CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0, 98 bool withResult = false); 99 bool CheckPromise(NativeValue* result); 100 bool CallPromise(NativeValue* result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo); 101 std::unique_ptr<NativeReference> CreateAppWindowStage(); 102 std::shared_ptr<AppExecFwk::ADelegatorAbilityProperty> CreateADelegatorAbilityProperty(); 103 sptr<IRemoteObject> SetNewRuleFlagToCallee(NativeEngine &nativeEngine, NativeValue *remoteJsObj); 104 105 JsRuntime &jsRuntime_; 106 std::shared_ptr<NativeReference> shellContextRef_; 107 std::shared_ptr<NativeReference> jsAbilityObj_; 108 sptr<IRemoteObject> remoteCallee_; 109 }; 110 } // namespace AbilityRuntime 111 } // namespace OHOS 112 #endif // OHOS_ABILITY_RUNTIME_JS_ABILITY_H 113