1 /* 2 * Copyright (c) 2021 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 FOUNDATION_ABILITYRUNTIME_OHOS_JS_WALLPAPER_EXTENSION_ABILITY_H 17 #define FOUNDATION_ABILITYRUNTIME_OHOS_JS_WALLPAPER_EXTENSION_ABILITY_H 18 19 #include <mutex> 20 21 #include "wallpaper_extension_ability.h" 22 23 class NativeReference; 24 25 namespace OHOS { 26 namespace AbilityRuntime { 27 class WallpaperExtensionAbility; 28 class JsRuntime; 29 30 /** 31 * @brief Basic wallpaper components. 32 */ 33 class JsWallpaperExtensionAbility : public WallpaperExtensionAbility, 34 public std::enable_shared_from_this<JsWallpaperExtensionAbility> { 35 public: 36 JsWallpaperExtensionAbility(JsRuntime &jsRuntime); 37 virtual ~JsWallpaperExtensionAbility() override; 38 static JsWallpaperExtensionAbility *jsWallpaperExtensionAbility; 39 static std::mutex mtx; 40 41 /** 42 * @brief Create JsWallpaperExtensionAbility. 43 * 44 * @param runtime The runtime. 45 * @return The JsWallpaperExtensionAbility instance. 46 */ 47 static JsWallpaperExtensionAbility *Create(const std::unique_ptr<Runtime> &runtime); 48 49 /** 50 * @brief Init the extension. 51 * 52 * @param record the extension record. 53 * @param application the application info. 54 * @param handler the extension handler. 55 * @param token the remote token. 56 */ 57 virtual void Init(const std::shared_ptr<AppExecFwk::AbilityLocalRecord> &record, 58 const std::shared_ptr<AppExecFwk::OHOSApplication> &application, 59 std::shared_ptr<AppExecFwk::AbilityHandler> &handler, const sptr<IRemoteObject> &token) override; 60 61 /** 62 * @brief Called when this extension is started. You must override this function if you want to perform some 63 * initialization operations during extension startup. 64 * 65 * This function can be called only once in the entire lifecycle of an extension. 66 * @param Want Indicates the {@link Want} structure containing startup information about the extension. 67 */ 68 virtual void OnStart(const AAFwk::Want &want) override; 69 70 /** 71 * @brief Called when this Wallpaper extension is connected for the first time. 72 * 73 * You can override this function to implement your own processing logic. 74 * 75 * @param want Indicates the {@link Want} structure containing connection information about the Wallpaper extension. 76 * @return Returns a pointer to the <b>sid</b> of the connected Wallpaper extension. 77 */ 78 virtual sptr<IRemoteObject> OnConnect(const AAFwk::Want &want) override; 79 80 /** 81 * @brief Called when all abilities connected to this Wallpaper extension are disconnected. 82 * 83 * You can override this function to implement your own processing logic. 84 * 85 */ 86 virtual void OnDisconnect(const AAFwk::Want &want) override; 87 88 /** 89 * @brief Called back when Wallpaper is started. 90 * This method can be called only by Wallpaper. You can use the StartAbility(ohos.aafwk.content.Want) method 91 * to start Wallpaper. Then the system calls back the current method to use the transferred want parameter 92 * to execute its own logic. 93 * 94 * @param want Indicates the want of Wallpaper to start. 95 * @param restart Indicates the startup mode. The value true indicates that Wallpaper is restarted after being 96 * destroyed, and the value false indicates a normal startup. 97 * @param startId Indicates the number of times the Wallpaper extension has been started. The startId is incremented 98 * by 1 every time the extension is started. For example, if the extension has been started for six times, the 99 * value of startId is 6. 100 */ 101 virtual void OnCommand(const AAFwk::Want &want, bool restart, int32_t startId) override; 102 103 /** 104 * @brief Called when this extension enters the <b>STATE_STOP</b> state. 105 * 106 * The extension in the <b>STATE_STOP</b> is being destroyed. 107 * You can override this function to implement your own processing logic. 108 */ 109 virtual void OnStop() override; 110 111 private: 112 void RegisterWallpaperCallback(); 113 114 private: 115 napi_value CallObjectMethod(const std::string &name, napi_value const *argv = nullptr, size_t argc = 0); 116 117 void GetSrcPath(std::string &srcPath); 118 void InitMoudle(std::string srcPath); 119 120 JsRuntime &jsRuntime_; 121 std::unique_ptr<NativeReference> jsObj_; 122 }; 123 } // namespace AbilityRuntime 124 } // namespace OHOS 125 #endif // FOUNDATION_ABILITYRUNTIME_OHOS_JS_WALLPAPER_EXTENSION_ABILITY_H