1 /*
2  * Copyright (c) 2021-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_SERVICE_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_JS_SERVICE_EXTENSION_H
18 
19 #include "configuration.h"
20 #include "insight_intent_execute_param.h"
21 #include "insight_intent_execute_result.h"
22 #include "insight_intent_executor_info.h"
23 #ifdef SUPPORT_GRAPHICS
24 #include "display_manager.h"
25 #include "system_ability_status_change_stub.h"
26 #include "window_manager.h"
27 #endif
28 #include "service_extension.h"
29 
30 class NativeReference;
31 
32 namespace OHOS {
33 namespace AbilityRuntime {
34 class ServiceExtension;
35 class JsRuntime;
36 /**
37  * @brief Basic service components.
38  */
39 class JsServiceExtension : public ServiceExtension {
40 public:
41     explicit JsServiceExtension(JsRuntime& jsRuntime);
42     virtual ~JsServiceExtension() override;
43 
44     /**
45      * @brief Create JsServiceExtension.
46      *
47      * @param runtime The runtime.
48      * @return The JsServiceExtension instance.
49      */
50     static JsServiceExtension* Create(const std::unique_ptr<Runtime>& runtime);
51 
52     /**
53      * @brief Init the extension.
54      *
55      * @param record the extension record.
56      * @param application the application info.
57      * @param handler the extension handler.
58      * @param token the remote token.
59      */
60     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
61         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
62         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
63         const sptr<IRemoteObject> &token) override;
64 
65     /**
66      * @brief Called when this extension is started. You must override this function if you want to perform some
67      *        initialization operations during extension startup.
68      *
69      * This function can be called only once in the entire lifecycle of an extension.
70      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
71      */
72     virtual void OnStart(const AAFwk::Want &want) override;
73 
74     /**
75      * @brief Called when this Service extension is connected for the first time.
76      *
77      * You can override this function to implement your own processing logic.
78      *
79      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
80      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
81      */
82     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
83 
84     /**
85      * @brief Called when this Service extension is connected for the first time.
86      *
87      * You can override this function to implement your own processing logic.
88      *
89      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
90      * @param callbackInfo Indicates the lifecycle transaction callback information
91      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
92      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
93      */
94     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want,
95         AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback) override;
96 
97     /**
98      * @brief Called when all abilities connected to this Service extension are disconnected.
99      *
100      * You can override this function to implement your own processing logic.
101      *
102      */
103     virtual void OnDisconnect(const AAFwk::Want &want) override;
104 
105     /**
106      * @brief Called when all abilities connected to this Service extension are disconnected.
107      *
108      * You can override this function to implement your own processing logic.
109      * @param callbackInfo Indicates the lifecycle transaction callback information
110      * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback
111      */
112     void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo,
113         bool &isAsyncCallback) override;
114 
115     /**
116      * @brief Called back when Service is started.
117      * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start
118      * Service. Then the system calls back the current method to use the transferred want parameter to execute its own
119      * logic.
120      *
121      * @param want Indicates the want of Service to start.
122      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
123      * destroyed, and the value false indicates a normal startup.
124      * @param startId Indicates the number of times the Service extension has been started. The startId is incremented
125      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
126      * value of startId is 6.
127      */
128     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
129 
130     /**
131      * @brief Called back when Service is started by intent driver.
132      *
133      * @param want Indicates the want of intent to handle.
134      */
135     bool HandleInsightIntent(const AAFwk::Want &want) override;
136 
137     /**
138      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
139      *
140      * The extension in the <b>STATE_STOP</b> is being destroyed.
141      * You can override this function to implement your own processing logic.
142      */
143     virtual void OnStop() override;
144 
145     /**
146      * @brief Called when the system configuration is updated.
147      *
148      * @param configuration Indicates the updated configuration information.
149      */
150     void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
151 
152     /**
153      * @brief Called when configuration changed, including system configuration and window configuration.
154      *
155      */
156     void ConfigurationUpdated();
157 
158     /**
159      * @brief Called when extension need dump info.
160      *
161      * @param params The params from service.
162      * @param info The dump info to show.
163      */
164     virtual void Dump(const std::vector<std::string> &params, std::vector<std::string> &info) override;
165 
166 private:
167     napi_value CallObjectMethod(const char* name, napi_value const *argv = nullptr, size_t argc = 0);
168 
169     void BindContext(napi_env env, napi_value obj);
170 
171     void GetSrcPath(std::string &srcPath);
172 
173     napi_value CallOnConnect(const AAFwk::Want &want);
174 
175     napi_value CallOnDisconnect(const AAFwk::Want &want, bool withResult = false);
176 
177     bool CheckPromise(napi_value result);
178 
179     bool CallPromise(napi_value result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo);
180 
181     void ListenWMS();
182 
183     bool GetInsightIntentExecutorInfo(const Want &want,
184         const std::shared_ptr<AppExecFwk::InsightIntentExecuteParam> &executeParam,
185         InsightIntentExecutorInfo &executorInfo);
186 
187     bool OnInsightIntentExecuteDone(uint64_t intentId, const AppExecFwk::InsightIntentExecuteResult &result) override;
188 
189     JsRuntime& jsRuntime_;
190     std::unique_ptr<NativeReference> jsObj_;
191     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
192     std::shared_ptr<AbilityHandler> handler_ = nullptr;
193 
194 #ifdef SUPPORT_GRAPHICS
195 protected:
196     class JsServiceExtensionDisplayListener : public Rosen::IDisplayInfoChangedListener {
197     public:
JsServiceExtensionDisplayListener(const std::weak_ptr<JsServiceExtension> & jsServiceExtension)198         explicit JsServiceExtensionDisplayListener(const std::weak_ptr<JsServiceExtension>& jsServiceExtension)
199         {
200             jsServiceExtension_ = jsServiceExtension;
201         }
202 
OnDisplayInfoChange(const sptr<IRemoteObject> & token,Rosen::DisplayId displayId,float density,Rosen::DisplayOrientation orientation)203         void OnDisplayInfoChange(const sptr<IRemoteObject>& token, Rosen::DisplayId displayId, float density,
204             Rosen::DisplayOrientation orientation) override
205             {
206                 auto sptr = jsServiceExtension_.lock();
207                 if (sptr != nullptr) {
208                     sptr->OnDisplayInfoChange(token, displayId, density, orientation);
209                 }
210             }
211 
212     private:
213         std::weak_ptr<JsServiceExtension> jsServiceExtension_;
214     };
215 
216     void OnCreate(Rosen::DisplayId displayId);
217     void OnDestroy(Rosen::DisplayId displayId);
218     void OnChange(Rosen::DisplayId displayId);
219     void OnDisplayInfoChange(const sptr<IRemoteObject>& token, Rosen::DisplayId displayId, float density,
220         Rosen::DisplayOrientation orientation);
221 
222 private:
223     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
224     public:
SystemAbilityStatusChangeListener(sptr<JsServiceExtensionDisplayListener> displayListener,const sptr<IRemoteObject> & token)225         SystemAbilityStatusChangeListener(sptr<JsServiceExtensionDisplayListener> displayListener,
226             const sptr<IRemoteObject> & token): tmpDisplayListener_(displayListener), token_(token) {};
227         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)228         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
229 
230     private:
231         sptr<JsServiceExtensionDisplayListener> tmpDisplayListener_ = nullptr;
232         sptr<IRemoteObject> token_ = nullptr;
233     };
234 
235     sptr<JsServiceExtensionDisplayListener> displayListener_ = nullptr;
236     sptr<SystemAbilityStatusChangeListener> saStatusChangeListener_ = nullptr;
237 #endif
238 };
239 }  // namespace AbilityRuntime
240 }  // namespace OHOS
241 #endif  // OHOS_ABILITY_RUNTIME_JS_SERVICE_EXTENSION_H
242