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