1 /*
2  * Copyright (c) 2024 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_CJ_ENVIRONMENT_CALLBACK_H
17 #define OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_CALLBACK_H
18 
19 #include <map>
20 #include <memory>
21 
22 #include "cj_utils_ffi.h"
23 #include "configuration.h"
24 #include "environment_callback.h"
25 
26 namespace OHOS {
27 namespace AbilityRuntime {
28 
29 class CjEnvironmentCallback : public EnvironmentCallback,
30     public std::enable_shared_from_this<CjEnvironmentCallback> {
31 public:
32     explicit CjEnvironmentCallback();
33     void OnConfigurationUpdated(const AppExecFwk::Configuration &config) override;
34     void OnMemoryLevel(const int level) override;
35     int32_t Register(std::function<void(CConfiguration)> cfgCallback,
36         std::function<void(int32_t)> memCallback, bool isSync);
37     bool UnRegister(int32_t callbackId, bool isSync = false);
38     bool IsEmpty() const;
39     static int32_t serialNumber_;
40 
41 private:
42     std::map<int32_t, std::function<void(CConfiguration)>> onConfigurationUpdatedCallbacks_;
43     std::map<int32_t, std::function<void(int32_t)>> onMemoryLevelCallbacks_;
44     void CallConfigurationUpdatedInner(const AppExecFwk::Configuration &config,
45         const std::map<int32_t, std::function<void(CConfiguration)>> &callbacks);
46     void CallMemoryLevelInner(const int level,
47         const std::map<int32_t, std::function<void(int32_t)>> &callbacks);
48 };
49 }  // namespace AbilityRuntime
50 }  // namespace OHOS
51 #endif  // OHOS_ABILITY_RUNTIME_CJ_ENVIRONMENT_CALLBACK_H
52