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