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_JS_DRIVER_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_JS_DRIVER_EXTENSION_H
18 
19 #include "configuration.h"
20 #include "driver_extension.h"
21 
22 class NativeReference;
23 
24 namespace OHOS {
25 namespace AbilityRuntime {
26 class DriverExtension;
27 class JsRuntime;
28 /**
29  * @brief Basic driver components.
30  */
31 class JsDriverExtension : public DriverExtension,
32                            public std::enable_shared_from_this<JsDriverExtension> {
33 public:
34     explicit JsDriverExtension(JsRuntime& jsRuntime);
35     virtual ~JsDriverExtension() override;
36 
37     /**
38      * @brief Create JsDriverExtension.
39      *
40      * @param runtime The runtime.
41      * @return The JsDriverExtension instance.
42      */
43     static JsDriverExtension* Create(const std::unique_ptr<Runtime>& runtime);
44 
45     /**
46      * @brief Init the extension.
47      *
48      * @param record the extension record.
49      * @param application the application info.
50      * @param handler the extension handler.
51      * @param token the remote token.
52      */
53     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
54         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
55         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
56         const sptr<IRemoteObject> &token) override;
57 
58     /**
59      * @brief Called when this extension is started. You must override this function if you want to perform some
60      *        initialization operations during extension startup.
61      *
62      * This function can be called only once in the entire lifecycle of an extension.
63      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
64      */
65     virtual void OnStart(const AAFwk::Want &want) override;
66 
67     /**
68      * @brief Called when this Driver extension is connected for the first time.
69      *
70      * You can override this function to implement your own processing logic.
71      *
72      * @param want Indicates the {@link Want} structure containing connection information about the Driver extension.
73      * @return Returns a pointer to the <b>sid</b> of the connected Driver extension.
74      */
75     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
76 
77     /**
78      * @brief Called when this Driver extension is connected for the first time.
79      *
80      * You can override this function to implement your own processing logic.
81      *
82      * @param want Indicates the {@link Want} structure containing connection information about the Driver extension.
83      * @param callbackInfo Indicates the lifecycle transaction callback information
84      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
85      * @return Returns a pointer to the <b>sid</b> of the connected Driver extension.
86      */
87     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want,
88         AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback) override;
89 
90     /**
91      * @brief Called when all abilities connected to this Driver extension are disconnected.
92      *
93      * You can override this function to implement your own processing logic.
94      *
95      */
96     virtual void OnDisconnect(const AAFwk::Want &want) override;
97 
98     /**
99      * @brief Called when all abilities connected to this Driver extension are disconnected.
100      *
101      * You can override this function to implement your own processing logic.
102      * @param callbackInfo Indicates the lifecycle transaction callback information
103      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
104      */
105     void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo,
106         bool &isAsyncCallback) override;
107 
108     /**
109      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
110      *
111      * The extension in the <b>STATE_STOP</b> is being destroyed.
112      * You can override this function to implement your own processing logic.
113      */
114     virtual void OnStop() override;
115 
116     /**
117      * @brief Called when extension need dump info.
118      *
119      * @param params The params from driver.
120      * @param info The dump info to show.
121      */
122     virtual void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
123 
124 private:
125     napi_value CallObjectMethod(napi_env env, const char *name, const napi_value *argv = nullptr, size_t argc = 0);
126 
127     void BindContext(napi_env env, napi_value obj);
128 
129     void GetSrcPath(std::string &srcPath);
130 
131     napi_value CallOnConnect(const AAFwk::Want &want);
132 
133     napi_value CallOnDisconnect(const AAFwk::Want &want, bool withResult = false);
134 
135     bool CheckPromise(napi_value result);
136 
137     bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
138 
139     JsRuntime& jsRuntime_;
140     std::unique_ptr<NativeReference> jsObj_;
141     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
142 };
143 }  // namespace AbilityRuntime
144 }  // namespace OHOS
145 #endif  // OHOS_ABILITY_RUNTIME_JS_DRIVER_EXTENSION_H
146