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