1 /* 2 * Copyright (c) 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_APPLICATION_STATE_CHANGE_CALLBACK_H 17 #define OHOS_ABILITY_RUNTIME_APPLICATION_STATE_CHANGE_CALLBACK_H 18 19 #include <memory> 20 #include <set> 21 22 class NativeEngine; 23 class NativeValue; 24 class NativeReference; 25 struct NativeCallbackInfo; 26 27 namespace OHOS { 28 namespace AbilityRuntime { 29 class ApplicationStateChangeCallback { 30 public: ~ApplicationStateChangeCallback()31 virtual ~ApplicationStateChangeCallback() {} 32 33 /** 34 * Called back when the application in foreground. 35 * 36 * @since 10 37 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 38 */ 39 virtual void NotifyApplicationForeground() = 0; 40 41 /** 42 * Called back when the application in background. 43 * 44 * @since 10 45 * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore 46 */ 47 virtual void NotifyApplicationBackground() = 0; 48 }; 49 50 class JsApplicationStateChangeCallback : public ApplicationStateChangeCallback, 51 public std::enable_shared_from_this<JsApplicationStateChangeCallback> { 52 public: 53 explicit JsApplicationStateChangeCallback(NativeEngine* engine); 54 virtual ~JsApplicationStateChangeCallback() = default; 55 void NotifyApplicationForeground() override; 56 void NotifyApplicationBackground() override; 57 void Register(NativeValue *jsCallback); 58 59 /** 60 * @brief Unregister application state change callback. 61 * @param jsCallback, if jscallback is nullptr, delete all register jscallback. 62 * or if jscallback is specified, delete prescribed jscallback. 63 * @return Returns true on unregister success, others return false. 64 */ 65 bool UnRegister(NativeValue *jsCallback = nullptr); 66 bool IsEmpty() const; 67 private: 68 void CallJsMethodInnerCommon( 69 const std::string &methodName, const std::set<std::shared_ptr<NativeReference>> callbacks); 70 void CallJsMethod(const std::string &methodName); 71 void CallApplicationForegroundInner(const std::string &methodName); 72 NativeEngine* engine_ = nullptr; 73 std::set<std::shared_ptr<NativeReference>> callbacks_; 74 }; 75 } // namespace AbilityRuntime 76 } // namespace OHOS 77 #endif // OHOS_ABILITY_RUNTIME_APPLICATION_STATE_CHANGE_CALLBACK_H 78