• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2025 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_APP_SERVICE_EXTENSION_H
17 #define OHOS_ABILITY_RUNTIME_JS_APP_SERVICE_EXTENSION_H
18 
19 #include "app_service_extension.h"
20 #include "configuration.h"
21 
22 #ifdef SUPPORT_GRAPHICS
23 #include "display_manager.h"
24 #include "system_ability_status_change_stub.h"
25 #include "window_manager.h"
26 #endif
27 
28 class NativeReference;
29 
30 namespace OHOS {
31 namespace AbilityRuntime {
32 class ServiceExtension;
33 class JsRuntime;
34 /**
35  * @brief Basic service components.
36  */
37 class JsAppServiceExtension : public AppServiceExtension {
38 public:
39     explicit JsAppServiceExtension(JsRuntime& jsRuntime);
40     virtual ~JsAppServiceExtension() override;
41 
42     /**
43      * @brief Create JsAppServiceExtension.
44      *
45      * @param runtime The runtime.
46      * @return The JsAppServiceExtension instance.
47      */
48     static JsAppServiceExtension* Create(const std::unique_ptr<Runtime>& runtime);
49 
50     /**
51      * @brief Init the extension.
52      *
53      * @param record the extension record.
54      * @param application the application info.
55      * @param handler the extension handler.
56      * @param token the remote token.
57      */
58     virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record,
59         const std::shared_ptr<AppExecFwk::OHOSApplication> &application,
60         std::shared_ptr<AppExecFwk::AbilityHandler> &handler,
61         const sptr<IRemoteObject> &token) override;
62 
63     /**
64      * @brief Called when this extension is started. You must override this function if you want to perform some
65      *        initialization operations during extension startup.
66      *
67      * This function can be called only once in the entire lifecycle of an extension.
68      * @param Want Indicates the {@link Want} structure containing startup information about the extension.
69      */
70     virtual void OnStart(const AAFwk::Want &want) override;
71 
72     /**
73      * @brief Called when this Service extension is connected for the first time.
74      *
75      * You can override this function to implement your own processing logic.
76      *
77      * @param want Indicates the {@link Want} structure containing connection information about the Service extension.
78      * @return Returns a pointer to the <b>sid</b> of the connected Service extension.
79      */
80     virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override;
81 
82     /**
83      * @brief Called when all abilities connected to this Service extension are disconnected.
84      *
85      * You can override this function to implement your own processing logic.
86      *
87      */
88     virtual void OnDisconnect(const AAFwk::Want &want) override;
89 
90     /**
91      * @brief Called back when Service is started.
92      * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start
93      * Service. Then the system calls back the current method to use the transferred want parameter to execute its own
94      * logic.
95      *
96      * @param want Indicates the want of Service to start.
97      * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being
98      * destroyed, and the value false indicates a normal startup.
99      * @param startId Indicates the number of times the Service extension has been started. The startId is incremented
100      * by 1 every time the extension is started. For example, if the extension has been started for six times, the
101      * value of startId is 6.
102      */
103     virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override;
104 
105     /**
106      * @brief Called when this extension enters the <b>STATE_STOP</b> state.
107      *
108      * The extension in the <b>STATE_STOP</b> is being destroyed.
109      * You can override this function to implement your own processing logic.
110      */
111     virtual void OnStop() override;
112 
113     /**
114      * @brief Called when the system configuration is updated.
115      *
116      * @param configuration Indicates the updated configuration information.
117      */
118     void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override;
119 
120     /**
121      * @brief Called when configuration changed, including system configuration and window configuration.
122      *
123      */
124     void ConfigurationUpdated();
125 
126 private:
127     napi_value CallObjectMethod(const char* name, napi_value const *argv = nullptr, size_t argc = 0);
128 
129     void BindContext(napi_env env, napi_value obj);
130 
131     void GetSrcPath(std::string &srcPath);
132 
133     napi_value CallOnConnect(const AAFwk::Want &want);
134 
135     napi_value CallOnDisconnect(const AAFwk::Want &want, bool withResult = false);
136 
137     void ListenWMS();
138 
139     void AddLifecycleEventForJSCall(const std::string &eventStr);
140 
141     JsRuntime& jsRuntime_;
142     std::unique_ptr<NativeReference> jsObj_;
143     std::shared_ptr<NativeReference> shellContextRef_ = nullptr;
144 #ifdef SUPPORT_GRAPHICS
145 protected:
146     class JsAppServiceExtensionDisplayListener : public Rosen::IDisplayInfoChangedListener {
147     public:
JsAppServiceExtensionDisplayListener(const std::weak_ptr<JsAppServiceExtension> & jsAppServiceExtension)148         explicit JsAppServiceExtensionDisplayListener(const std::weak_ptr<JsAppServiceExtension>& jsAppServiceExtension)
149         {
150             jsAppServiceExtension_ = jsAppServiceExtension;
151         }
152 
OnDisplayInfoChange(const sptr<IRemoteObject> & token,Rosen::DisplayId displayId,float density,Rosen::DisplayOrientation orientation)153         void OnDisplayInfoChange(const sptr<IRemoteObject>& token, Rosen::DisplayId displayId, float density,
154             Rosen::DisplayOrientation orientation) override
155             {
156                 auto sptr = jsAppServiceExtension_.lock();
157                 if (sptr != nullptr) {
158                     sptr->OnDisplayInfoChange(token, displayId, density, orientation);
159                 }
160             }
161 
162     private:
163         std::weak_ptr<JsAppServiceExtension> jsAppServiceExtension_;
164     };
165 
166     void OnCreate(Rosen::DisplayId displayId);
167     void OnDestroy(Rosen::DisplayId displayId);
168     void OnChange(Rosen::DisplayId displayId);
169     void OnDisplayInfoChange(const sptr<IRemoteObject>& token, Rosen::DisplayId displayId, float density,
170         Rosen::DisplayOrientation orientation);
171 
172 private:
173     class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub {
174     public:
SystemAbilityStatusChangeListener(sptr<JsAppServiceExtensionDisplayListener> displayListener,const sptr<IRemoteObject> & token)175         SystemAbilityStatusChangeListener(sptr<JsAppServiceExtensionDisplayListener> displayListener,
176             const sptr<IRemoteObject> & token): tmpDisplayListener_(displayListener), token_(token) {};
177         virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override;
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)178         virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {}
179 
180     private:
181         sptr<JsAppServiceExtensionDisplayListener> tmpDisplayListener_ = nullptr;
182         sptr<IRemoteObject> token_ = nullptr;
183     };
184 
185     sptr<JsAppServiceExtensionDisplayListener> displayListener_ = nullptr;
186     sptr<SystemAbilityStatusChangeListener> saStatusChangeListener_ = nullptr;
187 #endif
188 };
189 }  // namespace AbilityRuntime
190 }  // namespace OHOS
191 #endif  // OHOS_ABILITY_RUNTIME_JS_APP_SERVICE_EXTENSION_H