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 #ifdef SUPPORT_GRAPHICS 21 #include "display_manager.h" 22 #include "system_ability_status_change_stub.h" 23 #endif 24 #include "service_extension.h" 25 26 class NativeReference; 27 class NativeValue; 28 class NativeObject; 29 30 namespace OHOS { 31 namespace AbilityRuntime { 32 class ServiceExtension; 33 class JsRuntime; 34 /** 35 * @brief Basic service components. 36 */ 37 class JsServiceExtension : public ServiceExtension { 38 public: 39 explicit JsServiceExtension(JsRuntime& jsRuntime); 40 virtual ~JsServiceExtension() override; 41 42 /** 43 * @brief Create JsServiceExtension. 44 * 45 * @param runtime The runtime. 46 * @return The JsServiceExtension instance. 47 */ 48 static JsServiceExtension* 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 this Service extension is connected for the first time. 84 * 85 * You can override this function to implement your own processing logic. 86 * 87 * @param want Indicates the {@link Want} structure containing connection information about the Service extension. 88 * @param callbackInfo Indicates the lifecycle transaction callback information 89 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 90 * @return Returns a pointer to the <b>sid</b> of the connected Service extension. 91 */ 92 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want, 93 AppExecFwk::AbilityTransactionCallbackInfo<sptr<IRemoteObject>> *callbackInfo, bool &isAsyncCallback) override; 94 95 /** 96 * @brief Called when all abilities connected to this Service extension are disconnected. 97 * 98 * You can override this function to implement your own processing logic. 99 * 100 */ 101 virtual void OnDisconnect(const AAFwk::Want &want) override; 102 103 /** 104 * @brief Called when all abilities connected to this Service extension are disconnected. 105 * 106 * You can override this function to implement your own processing logic. 107 * @param callbackInfo Indicates the lifecycle transaction callback information 108 * @param isAsyncCallback Indicates whether it is an asynchronous lifecycle callback 109 */ 110 void OnDisconnect(const AAFwk::Want &want, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo, 111 bool &isAsyncCallback) override; 112 113 /** 114 * @brief Called back when Service is started. 115 * This method can be called only by Service. You can use the StartAbility(ohos.aafwk.content.Want) method to start 116 * Service. Then the system calls back the current method to use the transferred want parameter to execute its own 117 * logic. 118 * 119 * @param want Indicates the want of Service to start. 120 * @param restart Indicates the startup mode. The value true indicates that Service is restarted after being 121 * destroyed, and the value false indicates a normal startup. 122 * @param startId Indicates the number of times the Service extension has been started. The startId is incremented 123 * by 1 every time the extension is started. For example, if the extension has been started for six times, the 124 * value of startId is 6. 125 */ 126 virtual void OnCommand(const AAFwk::Want &want, bool restart, int startId) override; 127 128 /** 129 * @brief Called when this extension enters the <b>STATE_STOP</b> state. 130 * 131 * The extension in the <b>STATE_STOP</b> is being destroyed. 132 * You can override this function to implement your own processing logic. 133 */ 134 virtual void OnStop() override; 135 136 /** 137 * @brief Called when the system configuration is updated. 138 * 139 * @param configuration Indicates the updated configuration information. 140 */ 141 void OnConfigurationUpdated(const AppExecFwk::Configuration& configuration) override; 142 143 /** 144 * @brief Called when configuration changed, including system configuration and window configuration. 145 * 146 */ 147 void ConfigurationUpdated(); 148 149 /** 150 * @brief Called when extension need dump info. 151 * 152 * @param params The params from service. 153 * @param info The dump info to show. 154 */ 155 virtual void Dump(const std::vector<std::string> ¶ms, std::vector<std::string> &info) override; 156 157 private: 158 NativeValue* CallObjectMethod(const char* name, NativeValue* const* argv = nullptr, size_t argc = 0); 159 160 void BindContext(NativeEngine& engine, NativeObject* obj); 161 162 void GetSrcPath(std::string &srcPath); 163 164 NativeValue *CallOnConnect(const AAFwk::Want &want); 165 166 NativeValue *CallOnDisconnect(const AAFwk::Want &want, bool withResult = false); 167 168 bool CheckPromise(NativeValue *result); 169 170 bool CallPromise(NativeValue *result, AppExecFwk::AbilityTransactionCallbackInfo<> *callbackInfo); 171 172 void ListenWMS(); 173 174 JsRuntime& jsRuntime_; 175 std::unique_ptr<NativeReference> jsObj_; 176 std::shared_ptr<NativeReference> shellContextRef_ = nullptr; 177 std::shared_ptr<AbilityHandler> handler_ = nullptr; 178 179 #ifdef SUPPORT_GRAPHICS 180 protected: 181 class JsServiceExtensionDisplayListener : public Rosen::DisplayManager::IDisplayListener { 182 public: JsServiceExtensionDisplayListener(const std::weak_ptr<JsServiceExtension> & jsServiceExtension)183 explicit JsServiceExtensionDisplayListener(const std::weak_ptr<JsServiceExtension>& jsServiceExtension) 184 { 185 jsServiceExtension_ = jsServiceExtension; 186 } 187 OnCreate(Rosen::DisplayId displayId)188 void OnCreate(Rosen::DisplayId displayId) override 189 { 190 auto sptr = jsServiceExtension_.lock(); 191 if (sptr != nullptr) { 192 sptr->OnCreate(displayId); 193 } 194 } 195 OnDestroy(Rosen::DisplayId displayId)196 void OnDestroy(Rosen::DisplayId displayId) override 197 { 198 auto sptr = jsServiceExtension_.lock(); 199 if (sptr != nullptr) { 200 sptr->OnDestroy(displayId); 201 } 202 } 203 OnChange(Rosen::DisplayId displayId)204 void OnChange(Rosen::DisplayId displayId) override 205 { 206 auto sptr = jsServiceExtension_.lock(); 207 if (sptr != nullptr) { 208 sptr->OnChange(displayId); 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 220 private: 221 class SystemAbilityStatusChangeListener : public OHOS::SystemAbilityStatusChangeStub { 222 public: SystemAbilityStatusChangeListener(sptr<JsServiceExtensionDisplayListener> displayListener)223 SystemAbilityStatusChangeListener(sptr<JsServiceExtensionDisplayListener> displayListener) 224 : tmpDisplayListener_(displayListener) {}; 225 virtual void OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override; OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)226 virtual void OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId) override {} 227 228 private: 229 sptr<JsServiceExtensionDisplayListener> tmpDisplayListener_ = nullptr; 230 }; 231 232 sptr<JsServiceExtensionDisplayListener> displayListener_ = nullptr; 233 #endif 234 }; 235 } // namespace AbilityRuntime 236 } // namespace OHOS 237 #endif // OHOS_ABILITY_RUNTIME_JS_SERVICE_EXTENSION_H 238