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 PRINT_CALLBACK_H 17 #define PRINT_CALLBACK_H 18 19 #include <mutex> 20 #include <uv.h> 21 #include <functional> 22 #include "napi/native_api.h" 23 #include "napi/native_node_api.h" 24 #include "print_callback_stub.h" 25 #include "iprint_adapter.h" 26 #include "print_manager_client.h" 27 28 namespace OHOS::Print { 29 struct CallbackParam { 30 napi_env env; 31 napi_ref ref; 32 std::mutex* mutexPtr; 33 uint32_t state; 34 PrinterInfo printerInfo; 35 PrintJob jobInfo; 36 37 std::string extensionId; 38 std::string info; 39 40 std::string jobId; 41 PrintAttributes oldAttrs; 42 PrintAttributes newAttrs; 43 uint32_t fd = 0; 44 }; 45 46 struct Param { 47 napi_env env; 48 napi_ref callbackRef; 49 }; 50 51 class PrintCallback : public PrintCallbackStub { 52 public: 53 PrintCallback(napi_env env, napi_ref ref); 54 explicit PrintCallback(PrintDocumentAdapter *adapter); // This interface is invoked by other domains. PrintCallback()55 PrintCallback(){}; // This interface is invoked by ndk C++ Object 56 ~PrintCallback(); 57 bool OnCallback() override; 58 bool OnCallback(uint32_t state, const PrinterInfo &info) override; 59 bool OnCallback(uint32_t state, const PrintJob &info) override; 60 bool OnCallback(const std::string &extensionId, const std::string &info) override; 61 bool OnCallbackAdapterLayout(const std::string &jobId, const PrintAttributes &oldAttrs, 62 const PrintAttributes &newAttrs, uint32_t fd) override; 63 bool OnCallbackAdapterJobStateChanged(const std::string jobId, const uint32_t state, 64 const uint32_t subState) override; 65 bool OnCallbackAdapterGetFile(uint32_t state) override; 66 SetNativePrinterChangeCallback(NativePrinterChangeCallback cb)67 void SetNativePrinterChangeCallback(NativePrinterChangeCallback cb) 68 { 69 nativePrinterChange_cb = cb; 70 } 71 72 private: 73 bool OnBaseCallback(std::function<void(CallbackParam*)> paramFun, std::function<void(CallbackParam*)> workCb); 74 75 private: 76 napi_env env_ = nullptr; 77 napi_ref ref_ = nullptr; 78 std::mutex mutex_; 79 PrintDocumentAdapter *adapter_ = nullptr; 80 NativePrinterChangeCallback nativePrinterChange_cb = nullptr; 81 }; 82 } // namespace OHOS::Print 83 #endif // IPRINT_CALLBACK_H 84