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 NAPI_INNER_PRINT_H
17 #define NAPI_INNER_PRINT_H
18 
19 #include <string>
20 #include <vector>
21 
22 #include "iprint_callback.h"
23 #include "napi/native_api.h"
24 #include "print_async_call.h"
25 #include "print_extension_info.h"
26 #include "print_job.h"
27 #include "print_task.h"
28 #include "printer_info.h"
29 #include "print_utils.h"
30 #include "print_constant.h"
31 
32 namespace OHOS::Print {
33 class NapiInnerPrint {
34 public:
35     static napi_value QueryExtensionInfo(napi_env env, napi_callback_info info);
36     static napi_value StartDiscovery(napi_env env, napi_callback_info info);
37     static napi_value StopDiscovery(napi_env env, napi_callback_info info);
38     static napi_value ConnectPrinter(napi_env env, napi_callback_info info);
39     static napi_value DisconnectPrinter(napi_env env, napi_callback_info info);
40     static napi_value StartPrintJob(napi_env env, napi_callback_info info);
41     static napi_value CancelPrintJob(napi_env env, napi_callback_info info);
42     static napi_value RequestPreview(napi_env env, napi_callback_info info);
43     static napi_value QueryCapability(napi_env env, napi_callback_info info);
44     static napi_value QueryAllPrintJob(napi_env env, napi_callback_info info);
45     static napi_value QueryPrintJobById(napi_env env, napi_callback_info info);
46     static napi_value On(napi_env env, napi_callback_info info);
47     static napi_value Off(napi_env env, napi_callback_info info);
48     static napi_value StartGetPrintFile(napi_env env, napi_callback_info info);
49     static napi_value NotifyPrintService(napi_env env, napi_callback_info info);
50     static napi_value QueryAddedPrinter(napi_env env, napi_callback_info info);
51     static napi_value QueryPrinterInfoByPrinterId(napi_env env, napi_callback_info info);
52     static napi_value NotifyPrintServiceEvent(napi_env env, napi_callback_info info);
53     static napi_value GetPrinterPreference(napi_env env, napi_callback_info info);
54     static napi_value SetPrinterPreference(napi_env env, napi_callback_info info);
55     static napi_value SetDefaultPrinter(napi_env env, napi_callback_info info);
56     static napi_value GetAddedPrinterInfoById(napi_env env, napi_callback_info info);
57 
58 private:
59     static bool IsSupportType(const std::string& type);
60     static bool IsValidApplicationEvent(uint32_t event);
61     static bool IsValidDefaultPrinterType(uint32_t type);
62     static void NapiThrowError(napi_env env, int32_t errCode);
63     static bool CheckCallerIsSystemApp();
64 
65 private:
66     struct InnerPrintContext : public PrintAsyncCall::Context {
67         std::vector<PrintExtensionInfo> allExtensionInfos;
68         std::vector<PrintJob> allPrintJobs;
69         std::vector<std::string> allPrinters;
70         std::string previewResult = "";
71         std::string type = "";
72         sptr<IPrintCallback> callback = nullptr;
73         std::string fileUri = "";
74         uint32_t fileOffset = 0;
75         uint32_t fileMaxRead = -1;
76         std::vector<uint8_t> fileRead;
77         PrinterCapability printerCapability;
78         bool result = false;
79         std::string stateType_ = "";
80         PrintJob printJob;
81         PrinterInfo printerInfo;
82         std::string jobId = "";
83         std::string printerId = "";
84         std::vector<std::string> extensionList;
85         uint32_t applicationEvent = -1;
86         std::string printerPreference;
87         uint32_t defaultPrinterType = -1;
88 
InnerPrintContextInnerPrintContext89         InnerPrintContext() : Context(nullptr, nullptr) {};
InnerPrintContextInnerPrintContext90         InnerPrintContext(InputAction input, OutputAction output) : Context(std::move(input), std::move(output)) {};
~InnerPrintContextInnerPrintContext91         virtual ~InnerPrintContext() {};
92     };
93 };
94 }  // namespace OHOS::Print
95 #endif  // NAPI_INNER_PRINT_H
96