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_JS_EXTENSION_COMMON_H
17 #define OHOS_ABILITY_RUNTIME_JS_EXTENSION_COMMON_H
18 
19 #include "configuration.h"
20 #include "service_extension.h"
21 
22 class NativeReference;
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 class ServiceExtension;
27 class JsRuntime;
28 /**
29  * @brief Basic js extension common components.
30  */
31 class JsExtensionCommon : public ExtensionCommon,
32                             public std::enable_shared_from_this<JsExtensionCommon> {
33 public:
34     JsExtensionCommon(JsRuntime &jsRuntime, NativeReference &jsObj,
35         const std::shared_ptr<NativeReference> &shellContextRef);
36 
37     virtual ~JsExtensionCommon() override;
38 
39     /**
40      * @brief Create JsServiceExtension.
41      *
42      * @param jsRuntime The runtime.
43      * @param jsObj The js object instance.
44      * @return The JsServiceExtension instance.
45      */
46     static std::shared_ptr<JsExtensionCommon> Create(JsRuntime &jsRuntime, NativeReference &jsObj,
47         const std::shared_ptr<NativeReference> &shellContextRef);
48 
49     /**
50      * @brief Called when the system configuration is updated.
51      *
52      * @param configuration Indicates the updated configuration information.
53      */
54     void OnConfigurationUpdated(const std::shared_ptr<AppExecFwk::Configuration> &fullConfig) override;
55 
56     /**
57      * @brief Notify current memory level.
58      *
59      * @param level Current memory level.
60      */
61     virtual void OnMemoryLevel(int level) override;
62 
63 private:
64     napi_value CallObjectMethod(const char* name, napi_value const * argv, size_t argc);
65 
66 private:
67     JsRuntime& jsRuntime_;
68     NativeReference& jsObj_;
69     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
70 };
71 }  // namespace AbilityRuntime
72 }  // namespace OHOS
73 #endif  // OHOS_ABILITY_RUNTIME_JS_EXTENSION_COMMON_H
74