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