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