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