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_ENVIRONMENT_CALLBACK_H
17 #define OHOS_ABILITY_RUNTIME_ENVIRONMENT_CALLBACK_H
18 
19 #include <map>
20 #include <memory>
21 
22 #include "configuration.h"
23 
24 class NativeReference;
25 typedef struct napi_env__* napi_env;
26 typedef struct napi_value__* napi_value;
27 
28 namespace OHOS {
29 namespace AbilityRuntime {
30 class EnvironmentCallback {
31 public:
~EnvironmentCallback()32     virtual ~EnvironmentCallback() {}
33     /**
34      * Called when the system configuration is updated.
35      *
36      * @since 9
37      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
38      * @param config: Indicates the updated configuration.
39      * @StageModelOnly
40      */
41     virtual void OnConfigurationUpdated(const AppExecFwk::Configuration &config) = 0;
42 
43     /**
44      * Called when the system has determined to trim the memory, for example,
45      * when the ability is running in the background and there is no enough memory for
46      * running as many background processes as possible.
47      *
48      * @since 9
49      * @syscap SystemCapability.Ability.AbilityRuntime.AbilityCore
50      * @param level: Indicates the memory trim level, which shows the current memory usage status.
51      * @StageModelOnly
52      */
53     virtual void OnMemoryLevel(const int level) = 0;
54 };
55 
56 class JsEnvironmentCallback : public EnvironmentCallback,
57     public std::enable_shared_from_this<JsEnvironmentCallback> {
58 public:
59     explicit JsEnvironmentCallback(napi_env env);
60     void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override;
61     void OnMemoryLevel(const int level) override;
62     int32_t Register(napi_value jsCallback, bool isSync = false);
63     bool UnRegister(int32_t callbackId, bool isSync = false);
64     bool IsEmpty() const;
65     static int32_t serialNumber_;
66 
67 private:
68     napi_env env_ = nullptr;
69     std::shared_ptr<NativeReference> jsCallback_;
70     std::map<int32_t, std::shared_ptr<NativeReference>> callbacks_;
71     std::map<int32_t, std::shared_ptr<NativeReference>> callbacksSync_;
72     void CallConfigurationUpdatedInner(const std::string &methodName, const AppExecFwk::Configuration &config,
73         const std::map<int32_t, std::shared_ptr<NativeReference>> &callbacks);
74     void CallMemoryLevelInner(const std::string &methodName, const int level,
75         const std::map<int32_t, std::shared_ptr<NativeReference>> &callbacks);
76 };
77 }  // namespace AbilityRuntime
78 }  // namespace OHOS
79 #endif  // OHOS_ABILITY_RUNTIME_ENVIRONMENT_CALLBACK_H
80