1 /* 2 * Copyright (c) 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_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H 17 #define OHOS_ABILITY_RUNTIME_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H 18 19 #include <map> 20 #include <memory> 21 22 class NativeEngine; 23 class NativeValue; 24 class NativeReference; 25 struct NativeCallbackInfo; 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 class AbilityLifecycleCallback { 30 public: ~AbilityLifecycleCallback()31 virtual ~AbilityLifecycleCallback() {} 32 /** 33 * Called back when the ability is started for initialization. 34 * 35 * @since 9 36 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 37 * @param ability: Indicates the ability to register for listening. 38 * @StageModelOnly 39 */ 40 virtual void OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) = 0; 41 42 /** 43 * Called back when the window stage is created. 44 * 45 * @since 9 46 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 47 * @param ability: Indicates the ability to register for listening. 48 * @param windowStage: Indicates the window stage to create. 49 * @StageModelOnly 50 */ 51 virtual void OnWindowStageCreate(const std::shared_ptr<NativeReference> &ability, 52 const std::shared_ptr<NativeReference> &windowStage) = 0; 53 54 /** 55 * Called back when the window stage is destroy. 56 * 57 * @since 9 58 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 59 * @param ability: Indicates the ability to register for listening. 60 * @param windowStage: Indicates the window stage to destroy. 61 * @StageModelOnly 62 */ 63 virtual void OnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability, 64 const std::shared_ptr<NativeReference> &windowStage) = 0; 65 66 /** 67 * Called back when the window stage is active. 68 * 69 * @since 9 70 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 71 * @param ability: Indicates the ability to register for listening. 72 * @param windowStage: Indicates the window stage to active. 73 * @StageModelOnly 74 */ 75 virtual void OnWindowStageActive(const std::shared_ptr<NativeReference> &ability, 76 const std::shared_ptr<NativeReference> &windowStage) = 0; 77 78 /** 79 * Called back when the window stage is inactive. 80 * 81 * @since 9 82 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 83 * @param ability: Indicates the ability to register for listening. 84 * @param windowStage: Indicates the window stage to inactive. 85 * @StageModelOnly 86 */ 87 virtual void OnWindowStageInactive(const std::shared_ptr<NativeReference> &ability, 88 const std::shared_ptr<NativeReference> &windowStage) = 0; 89 90 /** 91 * Called back when the ability is destroy. 92 * 93 * @since 9 94 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 95 * @param ability: Indicates the ability to register for listening. 96 * @StageModelOnly 97 */ 98 virtual void OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) = 0; 99 100 /** 101 * Called back when the ability is foreground. 102 * 103 * @since 9 104 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 105 * @param ability: Indicates the ability to register for listening. 106 * @StageModelOnly 107 */ 108 virtual void OnAbilityForeground(const std::shared_ptr<NativeReference> &ability) = 0; 109 110 /** 111 * Called back when the ability is background. 112 * 113 * @since 9 114 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 115 * @param ability: Indicates the ability to register for listening. 116 * @StageModelOnly 117 */ 118 virtual void OnAbilityBackground(const std::shared_ptr<NativeReference> &ability) = 0; 119 120 /** 121 * Called back when the ability is continue. 122 * 123 * @since 9 124 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 125 * @param ability: Indicates the ability to register for listening. 126 * @StageModelOnly 127 */ 128 virtual void OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) = 0; 129 }; 130 131 class JsAbilityLifecycleCallback : public AbilityLifecycleCallback, 132 public std::enable_shared_from_this<JsAbilityLifecycleCallback> { 133 public: 134 explicit JsAbilityLifecycleCallback(NativeEngine* engine); 135 void OnAbilityCreate(const std::shared_ptr<NativeReference> &ability) override; 136 void OnWindowStageCreate(const std::shared_ptr<NativeReference> &ability, 137 const std::shared_ptr<NativeReference> &windowStage) override; 138 void OnWindowStageDestroy(const std::shared_ptr<NativeReference> &ability, 139 const std::shared_ptr<NativeReference> &windowStage) override; 140 void OnWindowStageActive(const std::shared_ptr<NativeReference> &ability, 141 const std::shared_ptr<NativeReference> &windowStage) override; 142 void OnWindowStageInactive(const std::shared_ptr<NativeReference> &ability, 143 const std::shared_ptr<NativeReference> &windowStage) override; 144 void OnAbilityDestroy(const std::shared_ptr<NativeReference> &ability) override; 145 void OnAbilityForeground(const std::shared_ptr<NativeReference> &ability) override; 146 void OnAbilityBackground(const std::shared_ptr<NativeReference> &ability) override; 147 void OnAbilityContinue(const std::shared_ptr<NativeReference> &ability) override; 148 int32_t Register(NativeValue *jsCallback); 149 bool UnRegister(int32_t callbackId); 150 bool IsEmpty() const; 151 static int32_t serialNumber_; 152 153 private: 154 NativeEngine* engine_ = nullptr; 155 std::shared_ptr<NativeReference> jsCallback_; 156 std::map<int32_t, std::shared_ptr<NativeReference>> callbacks_; 157 void CallJsMethod(const std::string &methodName, const std::shared_ptr<NativeReference> &ability); 158 void CallWindowStageJsMethod(const std::string &methodName, const std::shared_ptr<NativeReference> &ability, 159 const std::shared_ptr<NativeReference> &windowStage); 160 void CallJsMethodInnerCommon(const std::string &methodName, const std::shared_ptr<NativeReference> &ability, 161 const std::shared_ptr<NativeReference> &windowStage, 162 const std::map<int32_t, std::shared_ptr<NativeReference>> callbacks); 163 }; 164 } // namespace AbilityRuntime 165 } // namespace OHOS 166 #endif // OHOS_ABILITY_RUNTIME_CONTEXT_ABILITY_LIFECYCLE_CALLBACK_H 167