1 /*
2  * Copyright (c) 2023 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 #include "print_manager_adapter_impl.h"
17 
18 #include "nweb_log.h"
19 
20 namespace OHOS::NWeb {
21 // static
GetInstance()22 PrintManagerAdapterImpl& PrintManagerAdapterImpl::GetInstance()
23 {
24     static PrintManagerAdapterImpl instance;
25     return instance;
26 }
27 
StartPrint(const std::vector<std::string> & fileList,const std::vector<uint32_t> & fdList,std::string & taskId)28 int32_t PrintManagerAdapterImpl::StartPrint(
29     const std::vector<std::string>& fileList, const std::vector<uint32_t>& fdList, std::string& taskId)
30 {
31 #if defined(NWEB_PRINT_ENABLE)
32     int32_t ret = OHOS::Print::PrintManagerClient::GetInstance()->StartPrint(fileList, fdList, taskId);
33     if (ret != 0) {
34         WVLOG_E("StartPrint failed, failed id = %{public}d", ret);
35         return -1;
36     }
37     return ret;
38 #else
39     return -1;
40 #endif
41 }
42 
Print(const std::string & printJobName,const std::shared_ptr<PrintDocumentAdapterAdapter> listener,const PrintAttributesAdapter & printAttributes)43 int32_t PrintManagerAdapterImpl::Print(const std::string& printJobName,
44     const std::shared_ptr<PrintDocumentAdapterAdapter> listener, const PrintAttributesAdapter& printAttributes)
45 {
46 #if defined(NWEB_PRINT_ENABLE)
47     OHOS::Print::PrintDocumentAdapter* adapter = new (std::nothrow) PrintDocumentAdapterImpl(listener);
48     if (!adapter) {
49         WVLOG_E("adapter get failed");
50         return -1;
51     }
52     sptr<OHOS::Print::IPrintCallback> iCallback = new (std::nothrow) OHOS::Print::PrintCallback(adapter);
53     if (!iCallback) {
54         WVLOG_E("iCallback get failed");
55         return -1;
56     }
57     auto attributes = std::make_shared<OHOS::Print::PrintAttributes>();
58     if (!attributes) {
59         WVLOG_E("attributes get failed");
60         return -1;
61     }
62     int32_t ret = OHOS::Print::PrintManagerClient::GetInstance()->Print(printJobName, iCallback, attributes);
63     if (ret != 0) {
64         WVLOG_E("print failed, failed id = %{public}d", ret);
65         return -1;
66     }
67     return ret;
68 #else
69     return -1;
70 #endif
71 }
72 
Print(const std::string & printJobName,const std::shared_ptr<PrintDocumentAdapterAdapter> listener,const PrintAttributesAdapter & printAttributes,void * contextToken)73 int32_t PrintManagerAdapterImpl::Print(const std::string& printJobName,
74     const std::shared_ptr<PrintDocumentAdapterAdapter> listener, const PrintAttributesAdapter& printAttributes,
75     void* contextToken)
76 {
77 #if defined(NWEB_PRINT_ENABLE)
78     OHOS::Print::PrintDocumentAdapter* adapter = new (std::nothrow) PrintDocumentAdapterImpl(listener);
79     if (!adapter) {
80         WVLOG_E("adapter get failed");
81         return -1;
82     }
83     sptr<OHOS::Print::IPrintCallback> iCallback = new (std::nothrow) OHOS::Print::PrintCallback(adapter);
84     if (!iCallback) {
85         WVLOG_E("iCallback get failed");
86         return -1;
87     }
88     auto attributes = std::make_shared<OHOS::Print::PrintAttributes>();
89     if (!attributes) {
90         WVLOG_E("attributes get failed");
91         return -1;
92     }
93     int32_t ret =
94         OHOS::Print::PrintManagerClient::GetInstance()->Print(printJobName, iCallback, attributes, contextToken);
95     if (ret != 0) {
96         WVLOG_E("print failed, failed id = %{public}d", ret);
97         return -1;
98     }
99     return ret;
100 #else
101     return -1;
102 #endif
103 }
104 
105 #if defined(NWEB_PRINT_ENABLE)
PrintDocumentAdapterImpl(const std::shared_ptr<PrintDocumentAdapterAdapter> cb)106 PrintDocumentAdapterImpl::PrintDocumentAdapterImpl(const std::shared_ptr<PrintDocumentAdapterAdapter> cb)
107 {
108     cb_ = cb;
109 }
110 
ConvertPrintingParameters(OHOS::Print::PrintAttributes attrs)111 PrintAttributesAdapter PrintDocumentAdapterImpl::ConvertPrintingParameters(OHOS::Print::PrintAttributes attrs)
112 {
113     PrintAttributesAdapter printAttributesAdapter;
114     printAttributesAdapter.copyNumber = attrs.GetCopyNumber();
115     PrintRangeAdapter printRangeAdapter;
116     Print::PrintRange range;
117     attrs.GetPageRange(range);
118     printRangeAdapter.startPage = range.GetStartPage();
119     printRangeAdapter.endPage = range.GetEndPage();
120     std::vector<uint32_t> pages;
121     range.GetPages(pages);
122     printRangeAdapter.pages = pages;
123     printAttributesAdapter.pageRange = printRangeAdapter;
124     printAttributesAdapter.isSequential = attrs.GetIsSequential();
125     PrintPageSizeAdapter printPageSizeAdapter;
126     Print::PrintPageSize printPageSize;
127     attrs.GetPageSize(printPageSize);
128     printPageSizeAdapter.width = printPageSize.GetWidth();
129     printPageSizeAdapter.height = printPageSize.GetHeight();
130     printAttributesAdapter.pageSize = printPageSizeAdapter;
131     printAttributesAdapter.isLandscape = attrs.GetIsLandscape();
132     printAttributesAdapter.colorMode = attrs.GetColorMode();
133     printAttributesAdapter.duplexMode = attrs.GetDuplexMode();
134     PrintMarginAdapter printMarginAdapter;
135     Print::PrintMargin printMargin;
136     attrs.GetMargin(printMargin);
137     printMarginAdapter.top = printMargin.GetTop();
138     printMarginAdapter.bottom = printMargin.GetBottom();
139     printMarginAdapter.left = printMargin.GetLeft();
140     printMarginAdapter.right = printMargin.GetRight();
141     return printAttributesAdapter;
142 }
143 
onStartLayoutWrite(const std::string & jobId,const OHOS::Print::PrintAttributes & oldAttrs,const OHOS::Print::PrintAttributes & newAttrs,uint32_t fd,std::function<void (std::string,uint32_t)> writeResultCallback)144 void PrintDocumentAdapterImpl::onStartLayoutWrite(const std::string& jobId,
145     const OHOS::Print::PrintAttributes& oldAttrs, const OHOS::Print::PrintAttributes& newAttrs, uint32_t fd,
146     std::function<void(std::string, uint32_t)> writeResultCallback)
147 {
148     if (!cb_) {
149         return;
150     }
151 
152     std::shared_ptr<PrintWriteResultCallbackAdapter> callback =
153         std::make_shared<PrintWriteResultCallbackAdapterImpl>(writeResultCallback);
154     if (!callback) {
155         return;
156     }
157 
158     cb_->OnStartLayoutWrite(
159         jobId, ConvertPrintingParameters(oldAttrs), ConvertPrintingParameters(newAttrs), fd, callback);
160 }
161 
onJobStateChanged(const std::string & jobId,uint32_t state)162 void PrintDocumentAdapterImpl::onJobStateChanged(const std::string& jobId, uint32_t state)
163 {
164     if (!cb_) {
165         return;
166     }
167     cb_->OnJobStateChanged(jobId, state);
168 }
169 #endif
170 
PrintWriteResultCallbackAdapterImpl(std::function<void (std::string,uint32_t)> & cb)171 PrintWriteResultCallbackAdapterImpl::PrintWriteResultCallbackAdapterImpl(
172     std::function<void(std::string, uint32_t)>& cb)
173 {
174     cb_ = cb;
175 }
176 
WriteResultCallback(std::string jobId,uint32_t code)177 void PrintWriteResultCallbackAdapterImpl::WriteResultCallback(std::string jobId, uint32_t code)
178 {
179     cb_(jobId, code);
180 }
181 
182 } // namespace OHOS::NWeb
183