From 7f6c4a62ce7b18595f5ea1bb5feb4df999197166 Mon Sep 17 00:00:00 2001 From: "@chuangda_1" Date: Tue, 6 Sep 2022 11:10:06 +0800 Subject: [PATCH] Add 0906 code Signed-off-by: @chuangda_1 --- BUILD.gn | 2 +- OAT.xml | 2 +- README.en.md | 36 - README.md | 39 -- bundle.json | 2 +- etc/init/BUILD.gn | 2 +- etc/init/printservice.cfg | 11 +- etc/init/printservice.rc | 2 +- frameworks/innerkitsimpl/BUILD.gn | 30 +- ...t_notify_interface.h => iprint_callback.h} | 10 +- ...nterface.h => iprint_extension_callback.h} | 12 +- ...t_service_interface.h => iprint_service.h} | 14 +- ...{print_napi_utils.h => napi_print_utils.h} | 8 +- .../innerkitsimpl/include/print_base_notify.h | 7 +- ...nt_notify_stub.h => print_callback_stub.h} | 14 +- .../include/print_extension_Info.h | 4 +- ...stub.h => print_extension_callback_stub.h} | 27 +- .../innerkitsimpl/include/print_fail_notify.h | 2 +- frameworks/innerkitsimpl/include/print_job.h | 54 +- .../include/print_manager_client.h | 49 +- .../innerkitsimpl/include/print_notify.h | 6 +- .../innerkitsimpl/include/print_page_size.h | 193 +++++- ..._attribute.h => print_preview_attribute.h} | 6 +- .../include/print_progress_notify.h | 2 +- .../include/print_sa_death_recipient.h | 30 + .../include/print_service_proxy.h | 11 +- .../include/print_sync_load_callback.h | 28 + .../include/printer_capability.h | 30 +- .../innerkitsimpl/include/printer_info.h | 6 +- ...nt_napi_utils.cpp => napi_print_utils.cpp} | 6 +- .../innerkitsimpl/src/print_base_notify.cpp | 6 +- ...otify_stub.cpp => print_callback_stub.cpp} | 6 +- .../innerkitsimpl/src/print_extcb_stub.cpp | 104 --- .../src/print_extension_Info.cpp | 4 +- .../src/print_extension_callback_stub.cpp | 188 ++++++ .../innerkitsimpl/src/print_fail_notify.cpp | 4 +- frameworks/innerkitsimpl/src/print_job.cpp | 265 +++++++- .../src/print_manager_client.cpp | 133 +++- frameworks/innerkitsimpl/src/print_notify.cpp | 4 +- .../innerkitsimpl/src/print_page_size.cpp | 136 +++- ...ribute.cpp => print_preview_attribute.cpp} | 2 +- .../src/print_progress_notify.cpp | 4 +- frameworks/innerkitsimpl/src/print_range.cpp | 2 +- .../src/print_sa_death_recipient.cpp | 29 + .../innerkitsimpl/src/print_service_proxy.cpp | 14 +- .../src/print_sync_load_callback.cpp | 42 ++ .../innerkitsimpl/src/printer_capability.cpp | 384 ++++++++++- frameworks/innerkitsimpl/src/printer_info.cpp | 3 +- frameworks/kits/extension/BUILD.gn | 11 +- .../extension/include/js_print_callback.h | 63 ++ .../extension/include/js_print_extension.h | 3 +- .../include/js_print_extension_context.h | 2 +- .../kits/extension/include/print_extension.h | 2 +- .../include/print_extension_context.h | 2 +- .../include/print_extension_module_loader.h | 2 +- .../kits/extension/src/js_print_callback.cpp | 111 ++++ .../kits/extension/src/js_print_extension.cpp | 192 +++--- .../src/js_print_extension_context.cpp | 2 +- .../kits/extension/src/print_extension.cpp | 2 +- .../extension/src/print_extension_context.cpp | 2 +- .../src/print_extension_module_loader.cpp | 2 +- interfaces/kits/jskits/printextsample.js | 2 +- interfaces/kits/jskits/printsample.js | 2 +- interfaces/kits/jsnapi/extension/BUILD.gn | 2 +- .../kits/jsnapi/extension/print_extension.js | 2 +- .../extension/print_extension_module.cpp | 11 +- interfaces/kits/jsnapi/extensionctx/BUILD.gn | 2 +- .../extensionctx/print_extension_context.js | 2 +- .../print_extension_context_module.cpp | 13 +- interfaces/kits/napi/BUILD.gn | 1 + interfaces/kits/napi/include/base_context.h | 3 +- .../kits/napi/include/napi_inner_print.h | 10 +- interfaces/kits/napi/include/napi_print_ext.h | 10 - .../kits/napi/include/print_parse_type.h | 57 ++ interfaces/kits/napi/include/print_task.h | 5 - interfaces/kits/napi/src/async_call.cpp | 2 +- interfaces/kits/napi/src/napi_inner_print.cpp | 248 ++----- interfaces/kits/napi/src/napi_print_ext.cpp | 372 +---------- interfaces/kits/napi/src/napi_print_task.cpp | 6 +- interfaces/kits/napi/src/print_module.cpp | 4 +- interfaces/kits/napi/src/print_parse_type.cpp | 616 ++++++++++++++++++ profile/3707.xml | 4 +- profile/BUILD.gn | 2 +- services/BUILD.gn | 6 +- ..._notify_proxy.h => print_callback_proxy.h} | 10 +- ...oxy.h => print_extension_callback_proxy.h} | 17 +- services/include/print_service_ability.h | 15 +- services/include/print_service_manager.h | 4 +- services/include/print_service_stub.h | 6 +- ...ify_proxy.cpp => print_callback_proxy.cpp} | 14 +- ...cpp => print_extension_callback_proxy.cpp} | 41 +- services/src/print_service_ability.cpp | 16 +- services/src/print_service_manager.cpp | 4 +- services/src/print_service_stub.cpp | 31 +- utils/include/print_common.h | 2 +- utils/include/print_constant.h | 24 +- utils/include/print_log.h | 34 +- 97 files changed, 2787 insertions(+), 1184 deletions(-) delete mode 100644 README.en.md delete mode 100644 README.md rename frameworks/innerkitsimpl/include/{print_notify_interface.h => iprint_callback.h} (78%) rename frameworks/innerkitsimpl/include/{print_extcb_interface.h => iprint_extension_callback.h} (79%) rename frameworks/innerkitsimpl/include/{print_service_interface.h => iprint_service.h} (87%) rename frameworks/innerkitsimpl/include/{print_napi_utils.h => napi_print_utils.h} (96%) rename frameworks/innerkitsimpl/include/{print_notify_stub.h => print_callback_stub.h} (76%) rename frameworks/innerkitsimpl/include/{print_extcb_stub.h => print_extension_callback_stub.h} (58%) rename frameworks/innerkitsimpl/include/{preview_attribute.h => print_preview_attribute.h} (92%) create mode 100644 frameworks/innerkitsimpl/include/print_sa_death_recipient.h create mode 100644 frameworks/innerkitsimpl/include/print_sync_load_callback.h rename frameworks/innerkitsimpl/src/{print_napi_utils.cpp => napi_print_utils.cpp} (98%) rename frameworks/innerkitsimpl/src/{print_notify_stub.cpp => print_callback_stub.cpp} (89%) delete mode 100644 frameworks/innerkitsimpl/src/print_extcb_stub.cpp create mode 100644 frameworks/innerkitsimpl/src/print_extension_callback_stub.cpp rename frameworks/innerkitsimpl/src/{preview_attribute.cpp => print_preview_attribute.cpp} (96%) create mode 100644 frameworks/innerkitsimpl/src/print_sa_death_recipient.cpp create mode 100644 frameworks/innerkitsimpl/src/print_sync_load_callback.cpp create mode 100644 frameworks/kits/extension/include/js_print_callback.h create mode 100644 frameworks/kits/extension/src/js_print_callback.cpp create mode 100644 interfaces/kits/napi/include/print_parse_type.h create mode 100644 interfaces/kits/napi/src/print_parse_type.cpp rename services/include/{print_notify_proxy.h => print_callback_proxy.h} (73%) rename services/include/{print_extcb_proxy.h => print_extension_callback_proxy.h} (62%) rename services/src/{print_notify_proxy.cpp => print_callback_proxy.cpp} (74%) rename services/src/{print_extcb_proxy.cpp => print_extension_callback_proxy.cpp} (59%) diff --git a/BUILD.gn b/BUILD.gn index 4d9425f4..d7e7bb06 100644 --- a/BUILD.gn +++ b/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (C) 2022 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/OAT.xml b/OAT.xml index c2bb8cba..ba10ef3e 100644 --- a/OAT.xml +++ b/OAT.xml @@ -1,5 +1,5 @@ - %{public}u", code); + PRINT_HILOGD("PrintCallbackStub code----> %{public}u", code); switch (code) { case PRINT_NOTIFY: { OnCallBack(data); diff --git a/frameworks/innerkitsimpl/src/print_extcb_stub.cpp b/frameworks/innerkitsimpl/src/print_extcb_stub.cpp deleted file mode 100644 index 612d48c6..00000000 --- a/frameworks/innerkitsimpl/src/print_extcb_stub.cpp +++ /dev/null @@ -1,104 +0,0 @@ -/* - * Copyright (c) 2022 Huawei Device Co., Ltd. - * Licensed under the Apache License, Version 2.0 (the "License"); - * you may not use this file except in compliance with the License. - * You may obtain a copy of the License at - * - * http://www.apache.org/licenses/LICENSE-2.0 - * - * Unless required by applicable law or agreed to in writing, software - * distributed under the License is distributed on an "AS IS" BASIS, - * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. - * See the License for the specific language governing permissions and - * limitations under the License. - */ - -#include "print_extcb_stub.h" - -#include "print_common.h" -#include "print_log.h" - -namespace OHOS::Print { -PrintExtcbStub::PrintExtcbStub() : extCb_(nullptr), jobCb_(nullptr), cb_(nullptr) -{ - cmdMap_[PRINT_EXTCB] = &PrintExtcbStub::HandleExtCallback; - cmdMap_[PRINT_EXTCB_PRINTER] = &PrintExtcbStub::HandlePrinterCallback; - cmdMap_[PRINT_EXTCB_PRINTJOB] = &PrintExtcbStub::HandlePrintJobCallback; -} - -int32_t PrintExtcbStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) -{ - PRINT_HILOGD("OnRemoteRequest started, code = %{public}d", code); - auto descriptorToken = data.ReadInterfaceToken(); - if (descriptorToken != GetDescriptor()) { - PRINT_HILOGE("Remote descriptor not the same as local descriptor."); - return E_PRINT_IPC_ERROR; - } - - auto itFunc = cmdMap_.find(code); - if (itFunc != cmdMap_.end()) { - auto requestFunc = itFunc->second; - if (requestFunc != nullptr) { - return (this->*requestFunc)(data); - } - } - PRINT_HILOGW("default case, need check."); - return IPCObjectStub::OnRemoteRequest(code, data, reply, option); -} - -bool PrintExtcbStub::OnCallback() -{ - if (extCb_ != nullptr) { - return extCb_(); - } - return false; -} - -bool PrintExtcbStub::OnCallback(uint32_t printerId) -{ - if (cb_ != nullptr) { - return cb_(printerId); - } - return false; -} - -bool PrintExtcbStub::OnCallback(const PrintJob &job) -{ - if (jobCb_ != nullptr) { - return jobCb_(job); - } - return false; -} - -void PrintExtcbStub::SetExtCallback(PrintExtCallback cb) -{ - extCb_ = cb; -} - -void PrintExtcbStub::SetPrintJobCallback(PrintJobCallback cb) -{ - jobCb_ = cb; -} - -void PrintExtcbStub::SetPrinterCallback(PrinterCallback cb) -{ - cb_ = cb; -} - -bool PrintExtcbStub::HandleExtCallback(MessageParcel &data) -{ - return OnCallback(); -} - -bool PrintExtcbStub::HandlePrinterCallback(MessageParcel &data) -{ - uint32_t printerId = data.ReadUint32(); - return OnCallback(printerId); -} - -bool PrintExtcbStub::HandlePrintJobCallback(MessageParcel &data) -{ - PrintJob job; // = data.readuint32(); - return OnCallback(job); -} -} // namespace OHOS::Print diff --git a/frameworks/innerkitsimpl/src/print_extension_Info.cpp b/frameworks/innerkitsimpl/src/print_extension_Info.cpp index afba6e0e..f73f5a27 100644 --- a/frameworks/innerkitsimpl/src/print_extension_Info.cpp +++ b/frameworks/innerkitsimpl/src/print_extension_Info.cpp @@ -58,7 +58,7 @@ uint32_t PrinterExtensionInfo::GetVendorId() const return vendorId_; } -std::string &PrinterExtensionInfo::GetVendorName() +const std::string &PrinterExtensionInfo::GetVendorName() const { return vendorName_; } @@ -68,7 +68,7 @@ uint32_t PrinterExtensionInfo::GetVendorIcon() const return vendorIcon_; } -std::string &PrinterExtensionInfo::GetVersion() +const std::string &PrinterExtensionInfo::GetVersion() const { return version_; } diff --git a/frameworks/innerkitsimpl/src/print_extension_callback_stub.cpp b/frameworks/innerkitsimpl/src/print_extension_callback_stub.cpp new file mode 100644 index 00000000..a73bbb48 --- /dev/null +++ b/frameworks/innerkitsimpl/src/print_extension_callback_stub.cpp @@ -0,0 +1,188 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "print_extension_callback_stub.h" + +#include "print_common.h" +#include "print_log.h" +#include "print_resolution.h" + +namespace OHOS::Print { +PrintExtensionCallbackStub::PrintExtensionCallbackStub() : extCb_(nullptr), jobCb_(nullptr), cb_(nullptr) +{ + cmdMap_[PRINT_EXTCB] = &PrintExtensionCallbackStub::HandleExtCallback; + cmdMap_[PRINT_EXTCB_PRINTER] = &PrintExtensionCallbackStub::HandlePrinterCallback; + cmdMap_[PRINT_EXTCB_PRINTJOB] = &PrintExtensionCallbackStub::HandlePrintJobCallback; + cmdMap_[PRINT_EXTCB_PRINTCAPABILITY] = &PrintExtensionCallbackStub::HandleCapabilityCallback; +} + +int32_t PrintExtensionCallbackStub::OnRemoteRequest( + uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option) +{ + PRINT_HILOGD("OnRemoteRequest started, code = %{public}d", code); + auto descriptorToken = data.ReadInterfaceToken(); + if (descriptorToken != GetDescriptor()) { + PRINT_HILOGE("Remote descriptor not the same as local descriptor."); + return E_PRINT_IPC_ERROR; + } + + auto itFunc = cmdMap_.find(code); + if (itFunc != cmdMap_.end()) { + auto requestFunc = itFunc->second; + if (requestFunc != nullptr) { + return (this->*requestFunc)(data, reply); + } + } + PRINT_HILOGW("default case, need check."); + return IPCObjectStub::OnRemoteRequest(code, data, reply, option); +} + +bool PrintExtensionCallbackStub::OnCallback() +{ + if (extCb_ != nullptr) { + return extCb_(); + } + return false; +} + +bool PrintExtensionCallbackStub::OnCallback(uint32_t printerId) +{ + if (cb_ != nullptr) { + return cb_(printerId); + } + return false; +} + +bool PrintExtensionCallbackStub::OnCallback(const PrintJob &job) +{ + if (jobCb_ != nullptr) { + return jobCb_(job); + } + return false; +} + +bool PrintExtensionCallbackStub::OnCallback(uint32_t printerId, MessageParcel &reply) // PrinterCapability& reply +{ + PrinterCapability capability; + capability.ConvertToParcel(reply); + + return true; +} + +void PrintExtensionCallbackStub::SetExtCallback(PrintExtCallback cb) +{ + extCb_ = cb; +} + +void PrintExtensionCallbackStub::SetPrintJobCallback(PrintJobCallback cb) +{ + jobCb_ = cb; +} + +void PrintExtensionCallbackStub::SetPrinterCallback(PrinterCallback cb) +{ + cb_ = cb; +} + +void PrintExtensionCallbackStub::SetCapabilityCallback(PrinterCapabilityCallback cb) +{ + capability_ = cb; +} + +bool PrintExtensionCallbackStub::HandleExtCallback(MessageParcel &data, MessageParcel &reply) +{ + return OnCallback(); +} + +bool PrintExtensionCallbackStub::HandlePrinterCallback(MessageParcel &data, MessageParcel &reply) +{ + uint32_t printerId = data.ReadUint32(); + return OnCallback(printerId); +} + +void PrintExtensionCallbackStub::dataReadJob(MessageParcel &data, PrintJob &job) +{ + uint32_t fileLength = data.ReadUint32(); + std::vector files; + for (uint32_t i = 0; i < fileLength; i++) { + files.push_back(data.ReadString()); + } + job.SetFiles(files); + job.SetJobId(data.ReadUint32()); + job.SetPrintId(data.ReadUint32()); + job.SetJobState(data.ReadUint32()); + job.SetCopyNumber(data.ReadUint32()); + PrintRange range; + std::vector pages; + range.SetStartPage(data.ReadUint32()); + range.SetEndPage(data.ReadUint32()); + uint32_t pageLength = data.ReadUint32(); + for (uint32_t i = 0; i < pageLength; i++) { + pages.push_back(data.ReadUint32()); + } + range.SetPages(pages); + job.SetPageRange(range); + + job.SetIsSequential(data.ReadUint32()); + PrintPageSize pageSize; + pageSize.SetId(data.ReadString()); + pageSize.SetName(data.ReadString()); + pageSize.SetWidth(data.ReadUint32()); + pageSize.SetHeight(data.ReadUint32()); + job.SetPageSize(pageSize); + + job.SetIsLandscape(data.ReadUint32()); + job.SetColorMode(data.ReadUint32()); + job.SetDuplexMode(data.ReadUint32()); + + PrintMargin margin; + margin.SetTop(data.ReadUint32()); + margin.SetBottom(data.ReadUint32()); + margin.SetLeft(data.ReadUint32()); + margin.SetRight(data.ReadUint32()); + job.SetMargin(margin); + + PreviewAttribute previewAttr; + PrintRange previewRange; + previewAttr.SetResult(data.ReadString()); + previewRange.SetStartPage(data.ReadUint32()); + previewRange.SetEndPage(data.ReadUint32()); + uint32_t previewPageLength = data.ReadUint32(); + std::vector arrayPages; + for (uint32_t i = 0; i < previewPageLength; i++) { + arrayPages.push_back(data.ReadUint32()); + } + previewRange.SetPages(arrayPages); + previewAttr.SetPreviewRange(previewRange); + job.SetPreview(previewAttr); +} + +bool PrintExtensionCallbackStub::HandlePrintJobCallback(MessageParcel &data, MessageParcel &reply) +{ + PrintJob job; + job.BuildFromParcel(data); + // dataReadJob(data, job); + return OnCallback(job); +} + +bool PrintExtensionCallbackStub::HandleCapabilityCallback(MessageParcel &data, MessageParcel &reply) +{ + uint32_t printerId = data.ReadUint32(); + // PrinterCapability cap; + // OnCallabck(printerId, cap); + // cap.BuildParcel(reply); + return OnCallback(printerId, reply); +} +} // namespace OHOS::Print diff --git a/frameworks/innerkitsimpl/src/print_fail_notify.cpp b/frameworks/innerkitsimpl/src/print_fail_notify.cpp index c30eaaae..4123b067 100644 --- a/frameworks/innerkitsimpl/src/print_fail_notify.cpp +++ b/frameworks/innerkitsimpl/src/print_fail_notify.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,8 +17,8 @@ #include +#include "napi_print_utils.h" #include "print_log.h" -#include "print_napi_utils.h" namespace OHOS::Print { PrintFailNotify::PrintFailNotify(napi_env env, const std::string &type, napi_ref ref) : PrintBaseNotify(env, type, ref) diff --git a/frameworks/innerkitsimpl/src/print_job.cpp b/frameworks/innerkitsimpl/src/print_job.cpp index ac2a447d..f3d659e3 100644 --- a/frameworks/innerkitsimpl/src/print_job.cpp +++ b/frameworks/innerkitsimpl/src/print_job.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -14,19 +14,20 @@ */ #include "print_job.h" + +#include "napi_print_utils.h" #include "print_log.h" -namespace OHOS::Print{ -PrintJob::PrintJob() : jobId_(0), jobState_(1), copyNumber_(1), isSequential_(true), - isLandscape_(1), colorMode_(1), duplexMode_(1) -{ -} +namespace OHOS::Print { +PrintJob::PrintJob() + : jobId_(0), jobState_(1), copyNumber_(1), isSequential_(true), isLandscape_(1), colorMode_(1), duplexMode_(1) +{} -PrintJob::PrintJob(const PrintJob& right) +PrintJob::PrintJob(const PrintJob &right) { files_.clear(); files_.assign(right.files_.begin(), right.files_.end()); - + printerId_ = right.printerId_; jobId_ = right.jobId_; jobState_ = right.jobState_; @@ -41,9 +42,9 @@ PrintJob::PrintJob(const PrintJob& right) preview_ = right.preview_; } -PrintJob& PrintJob::operator=(PrintJob& right) +PrintJob &PrintJob::operator=(PrintJob &right) { - if(this != &right){ + if (this != &right) { files_.clear(); files_.assign(right.files_.begin(), right.files_.end()); @@ -63,12 +64,9 @@ PrintJob& PrintJob::operator=(PrintJob& right) return *this; } +PrintJob::~PrintJob() {} -PrintJob::~PrintJob() -{ -} - -void PrintJob::SetFiles(const std::vector& files) +void PrintJob::SetFiles(const std::vector &files) { files_.clear(); files_.assign(files.begin(), files.end()); @@ -94,7 +92,7 @@ void PrintJob::SetCopyNumber(uint32_t copyNumber) copyNumber_ = copyNumber; } -void PrintJob::SetPageRange(PrintRange pageRange) +void PrintJob::SetPageRange(const PrintRange &pageRange) { pageRange_ = pageRange; } @@ -104,7 +102,7 @@ void PrintJob::SetIsSequential(bool isSequential) isSequential_ = isSequential; } -void PrintJob::SetPageSize(PrintPageSize pageSize) +void PrintJob::SetPageSize(const PrintPageSize &pageSize) { pageSize_ = pageSize; } @@ -124,17 +122,17 @@ void PrintJob::SetDuplexMode(uint32_t duplexmode) duplexMode_ = duplexmode; } -void PrintJob::SetMargin(PrintMargin margin) +void PrintJob::SetMargin(const PrintMargin &margin) { margin_ = margin; } -void PrintJob::SetPreview(PreviewAttribute preview) +void PrintJob::SetPreview(const PreviewAttribute &preview) { preview_ = preview; } -void PrintJob::GetFiles(std::vector& files) const +void PrintJob::GetFiles(std::vector &files) const { files.clear(); files.assign(files_.begin(), files_.end()); @@ -160,7 +158,7 @@ uint32_t PrintJob::GetCopyNumber() const return copyNumber_; } -void PrintJob::GetPageRange(PrintRange& range) const +void PrintJob::GetPageRange(PrintRange &range) const { range = pageRange_; } @@ -169,7 +167,7 @@ bool PrintJob::GetIsSequential() const { return isSequential_; } -void PrintJob::GetPageSize(PrintPageSize& pageSize) const +void PrintJob::GetPageSize(PrintPageSize &pageSize) const { pageSize = pageSize_; } @@ -189,21 +187,234 @@ uint32_t PrintJob::GetDuplexMode() const return duplexMode_; } -void PrintJob::GetMargin(PrintMargin& margin) const +void PrintJob::GetMargin(PrintMargin &margin) const { margin = margin_; } -void PrintJob::GetPreview(PreviewAttribute& previewAttr) const +void PrintJob::GetPreview(PreviewAttribute &previewAttr) const { previewAttr = preview_; } +void PrintJob::ConvertToParcel(MessageParcel &reply) const +{ + std::vector files; + GetFiles(files); + uint32_t fileLength = files.size(); + reply.WriteUint32(fileLength); + for (uint32_t i = 0; i < fileLength; i++) { + reply.WriteString(files[i]); + } + reply.WriteUint32(GetJobId()); + reply.WriteUint32(GetPrinterId()); + reply.WriteUint32(GetJobState()); + reply.WriteUint32(GetCopyNumber()); + + PrintRange range; + GetPageRange(range); + reply.WriteUint32(range.GetStartPage()); + reply.WriteUint32(range.GetEndPage()); + + std::vector pages; + range.GetPages(pages); + uint32_t pageLength = pages.size(); + reply.WriteUint32(pageLength); + for (uint32_t i = 0; i < pageLength; i++) { + reply.WriteUint32(pages[i]); + } + + reply.WriteUint32(GetIsSequential()); + + PrintPageSize pageSize; + GetPageSize(pageSize); + reply.WriteString(pageSize.GetId()); + reply.WriteString(pageSize.GetName()); + reply.WriteUint32(pageSize.GetWidth()); + reply.WriteUint32(pageSize.GetHeight()); + + reply.WriteUint32(GetIsLandscape()); + reply.WriteUint32(GetColorMode()); + reply.WriteUint32(GetDuplexMode()); + + PrintMargin margin; + GetMargin(margin); + reply.WriteUint32(margin.GetTop()); + reply.WriteUint32(margin.GetBottom()); + reply.WriteUint32(margin.GetLeft()); + reply.WriteUint32(margin.GetRight()); + + PreviewAttribute preview; + GetPreview(preview); + reply.WriteString(preview.GetResult()); + + range.Reset(); + preview.GetPreviewRange(range); + reply.WriteUint32(range.GetStartPage()); + reply.WriteUint32(range.GetEndPage()); + + range.GetPages(pages); + uint32_t previewPageLength = pages.size(); + reply.WriteUint32(previewPageLength); + for (uint32_t i = 0; i < previewPageLength; i++) { + reply.WriteUint32(pages[i]); + } +} + +void PrintJob::BuildFromParcel(MessageParcel &data) +{ + uint32_t fileLength = data.ReadUint32(); + std::vector files; + uint32_t index = 0; + for (index = 0; index < fileLength; index++) { + files.push_back(data.ReadString()); + } + SetFiles(files); + SetJobId(data.ReadUint32()); + SetPrintId(data.ReadUint32()); + SetJobState(data.ReadUint32()); + SetCopyNumber(data.ReadUint32()); + + PrintRange range; + range.SetStartPage(data.ReadUint32()); + range.SetEndPage(data.ReadUint32()); + uint32_t pageLength = data.ReadUint32(); + if (pageLength > 0) { + std::vector rangePages; + for (index = 0; index < pageLength; index++) { + rangePages.push_back(data.ReadUint32()); + } + range.SetPages(rangePages); + } + SetPageRange(range); + + SetIsSequential(data.ReadUint32()); + + PrintPageSize pageSize; + pageSize.SetId(data.ReadString()); + pageSize.SetName(data.ReadString()); + pageSize.SetWidth(data.ReadUint32()); + pageSize.SetHeight(data.ReadUint32()); + SetPageSize(pageSize); + + SetIsLandscape(data.ReadUint32()); + SetColorMode(data.ReadUint32()); + SetDuplexMode(data.ReadUint32()); + + PrintMargin minMargin; + minMargin.SetTop(data.ReadUint32()); + minMargin.SetBottom(data.ReadUint32()); + minMargin.SetLeft(data.ReadUint32()); + minMargin.SetRight(data.ReadUint32()); + SetMargin(minMargin); + + PreviewAttribute previewAttr; + + previewAttr.SetResult(data.ReadString()); + range.Reset(); + range.SetStartPage(data.ReadUint32()); + range.SetEndPage(data.ReadUint32()); + uint32_t previewPageLength = data.ReadUint32(); + if (previewPageLength > 0) { + std::vector previewRangePages; + for (index = 0; index < previewPageLength; index++) { + previewRangePages.push_back(data.ReadUint32()); + } + range.SetPages(previewRangePages); + } + previewAttr.SetPreviewRange(range); + SetPreview(previewAttr); + Dump(); +} + +void PrintJob::ConvertToJs(napi_env env, napi_value *result) const +{ + napi_create_object(env, result); + napi_value arrFiles; + napi_status status = napi_create_array(env, &arrFiles); + uint32_t arrFilesLength = files_.size(); + + for (uint32_t i = 0; i < arrFilesLength; i++) { + napi_value value; + status = napi_create_string_utf8(env, files_[i].c_str(), NAPI_AUTO_LENGTH, &value); + status = napi_set_element(env, arrFiles, i, value); + } + napi_set_named_property(env, *result, "files", arrFiles); + + PrintNapiUtils::SetUint32Property(env, *result, "jobId", GetJobId()); + PrintNapiUtils::SetUint32Property(env, *result, "printerId", GetPrinterId()); + PrintNapiUtils::SetUint32Property(env, *result, "jobState", GetJobState()); + PrintNapiUtils::SetUint32Property(env, *result, "copyNumber", GetCopyNumber()); + napi_value pageRange; + napi_create_object(env, &pageRange); + PrintNapiUtils::SetUint32Property(env, pageRange, "startPage", pageRange_.GetStartPage()); + PrintNapiUtils::SetUint32Property(env, pageRange, "endPage", pageRange_.GetEndPage()); + napi_value arrPages; + status = napi_create_array(env, &arrPages); + std::vector pages; + pageRange_.GetPages(pages); + uint32_t arrPagesLength = pages.size(); + for (uint32_t i = 0; i < arrPagesLength; i++) { + napi_value value; + napi_create_uint32(env, pages[i], &value); + status = napi_set_element(env, arrPages, i, value); + } + napi_set_named_property(env, pageRange, "files", arrPages); + + PrintNapiUtils::SetUint32Property(env, *result, "isSequential", GetIsSequential()); + napi_value pageSize; + napi_create_object(env, &pageSize); + PrintNapiUtils::SetStringPropertyUtf8(env, pageSize, "id", pageSize_.GetId()); + PrintNapiUtils::SetStringPropertyUtf8(env, pageSize, "name", pageSize_.GetName().c_str()); + PrintNapiUtils::SetUint32Property(env, pageSize, "width", pageSize_.GetWidth()); + PrintNapiUtils::SetUint32Property(env, pageSize, "height", pageSize_.GetHeight()); + + PrintNapiUtils::SetUint32Property(env, *result, "isLandscape", GetIsLandscape()); + PrintNapiUtils::SetUint32Property(env, *result, "colorMode", GetColorMode()); + PrintNapiUtils::SetUint32Property(env, *result, "duplexMode", GetDuplexMode()); + + napi_value margin; + napi_create_object(env, &margin); + PrintNapiUtils::SetUint32Property(env, margin, "top", margin_.GetTop()); + PrintNapiUtils::SetUint32Property(env, margin, "bottom", margin_.GetBottom()); + PrintNapiUtils::SetUint32Property(env, margin, "left", margin_.GetLeft()); + PrintNapiUtils::SetUint32Property(env, margin, "right", margin_.GetRight()); + + napi_value preview; + napi_create_object(env, &preview); + PrintNapiUtils::SetStringPropertyUtf8(env, preview, "result", preview_.GetResult().c_str()); + napi_value subPageRange; + napi_create_object(env, &subPageRange); + PrintNapiUtils::SetUint32Property(env, subPageRange, "startPage", pageRange_.GetStartPage()); + PrintNapiUtils::SetUint32Property(env, subPageRange, "endPage", pageRange_.GetEndPage()); + napi_value arrPreviewPages; + status = napi_create_array(env, &arrPreviewPages); + PrintRange previewPrintRange; + std::vector previewRangePages; + preview_.GetPreviewRange(previewPrintRange); + previewPrintRange.GetPages(previewRangePages); + uint32_t arrPreviewPagesLength = previewRangePages.size(); + + for (uint32_t i = 0; i < arrPreviewPagesLength; i++) { + napi_value value; + napi_create_uint32(env, previewRangePages[i], &value); + status = napi_set_element(env, arrPreviewPages, i, value); + } + napi_set_named_property(env, subPageRange, "files", arrPreviewPages); + napi_set_named_property(env, preview, "pageRange", subPageRange); + + napi_set_named_property(env, *result, "pageRange", pageRange); + napi_set_named_property(env, *result, "pageSize", pageSize); + napi_set_named_property(env, *result, "margin", margin); + napi_set_named_property(env, *result, "preview", preview); +} + +void PrintJob::BuildFromJs(napi_env env, napi_value capValue) {} + void PrintJob::Dump() { uint32_t fileLength = files_.size(); - for(uint32_t i = 0; i < fileLength; i++) - { + for (uint32_t i = 0; i < fileLength; i++) { PRINT_HILOGD("files_ = %{public}s", files_[i].c_str()); } @@ -221,4 +432,4 @@ void PrintJob::Dump() margin_.Dump(); preview_.Dump(); } -} +} // namespace OHOS::Print diff --git a/frameworks/innerkitsimpl/src/print_manager_client.cpp b/frameworks/innerkitsimpl/src/print_manager_client.cpp index d15efa7c..a8b7a906 100644 --- a/frameworks/innerkitsimpl/src/print_manager_client.cpp +++ b/frameworks/innerkitsimpl/src/print_manager_client.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,8 +16,9 @@ #include "print_manager_client.h" #include "iservice_registry.h" -#include "print_extcb_stub.h" +#include "print_extension_callback_stub.h" #include "print_log.h" +#include "print_sync_load_callback.h" #include "system_ability_definition.h" namespace OHOS::Print { @@ -40,7 +41,7 @@ sptr PrintManagerClient::GetInstance() } bool PrintManagerClient::On( - const std::string &type, uint32_t &state, PrinterInfo &info, const sptr &listener) + const std::string &type, uint32_t &state, PrinterInfo &info, const sptr &listener) { if (printServiceProxy_ == nullptr) { PRINT_HILOGW("Redo GetPrintServiceProxy"); @@ -52,7 +53,6 @@ bool PrintManagerClient::On( } PRINT_HILOGD("PrintManagerClient On succeeded."); return true; - // return printServiceProxy_->On(type, state, info); } bool PrintManagerClient::Off(const std::string &type) @@ -84,10 +84,10 @@ bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrintExtCallba PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId); return false; } - sptr callbackStub = nullptr; + sptr callbackStub = nullptr; auto it = extCallbackMap_.find(callbackId); if (it == extCallbackMap_.end()) { - callbackStub = new PrintExtcbStub; + callbackStub = new PrintExtensionCallbackStub; if (callbackStub == nullptr) { PRINT_HILOGE("Invalid callback stub object."); return false; @@ -119,10 +119,10 @@ bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrintJobCallba PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId); return false; } - sptr callbackStub = nullptr; + sptr callbackStub = nullptr; auto it = extCallbackMap_.find(callbackId); if (it == extCallbackMap_.end()) { - callbackStub = new PrintExtcbStub; + callbackStub = new PrintExtensionCallbackStub; if (callbackStub == nullptr) { PRINT_HILOGE("Invalid callback stub object."); return false; @@ -139,6 +139,41 @@ bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrintJobCallba return ret; } +bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrinterCapabilityCallback cb) +{ + if (printServiceProxy_ == nullptr) { + PRINT_HILOGW("Redo GetPrintServiceProxy"); + printServiceProxy_ = GetPrintServiceProxy(); + } + if (printServiceProxy_ == nullptr) { + PRINT_HILOGE("Resume quit because redoing GetPrintServiceProxy failed."); + return false; + } + + if (callbackId >= PRINT_EXTCB_MAX) { + PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId); + return false; + } + sptr callbackStub = nullptr; + auto it = extCallbackMap_.find(callbackId); + if (it == extCallbackMap_.end()) { + callbackStub = new PrintExtensionCallbackStub; + if (callbackStub == nullptr) { + PRINT_HILOGE("Invalid callback stub object."); + return false; + } + callbackStub->SetCapabilityCallback(cb); + extCallbackMap_.insert(std::make_pair(callbackId, callbackStub)); + } else { + callbackStub = it->second; + callbackStub->SetCapabilityCallback(cb); + } + + bool ret = printServiceProxy_->RegisterExtCallback(callbackId, callbackStub); + PRINT_HILOGD("PrintManagerClient RegisterExtCallback %{public}s.", ret ? "success" : "failed"); + return ret; +} + bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrinterCallback cb) { if (printServiceProxy_ == nullptr) { @@ -154,10 +189,10 @@ bool PrintManagerClient::RegisterExtCallback(uint32_t callbackId, PrinterCallbac PRINT_HILOGE("Invalid callback id [%{public}d].", callbackId); return false; } - sptr callbackStub = nullptr; + sptr callbackStub = nullptr; auto it = extCallbackMap_.find(callbackId); if (it == extCallbackMap_.end()) { - callbackStub = new PrintExtcbStub; + callbackStub = new PrintExtensionCallbackStub; if (callbackStub == nullptr) { PRINT_HILOGE("Invalid callback stub object."); return false; @@ -193,7 +228,7 @@ bool PrintManagerClient::UnregisterAllExtCallback() return ret; } -sptr PrintManagerClient::GetPrintServiceProxy() +sptr PrintManagerClient::GetPrintServiceProxy() { sptr systemAbilityManager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); @@ -208,7 +243,7 @@ sptr PrintManagerClient::GetPrintServiceProxy() } deathRecipient_ = new PrintSaDeathRecipient(); systemAbility->AddDeathRecipient(deathRecipient_); - sptr serviceProxy = iface_cast(systemAbility); + sptr serviceProxy = iface_cast(systemAbility); if (serviceProxy == nullptr) { PRINT_HILOGE("Get PrintManagerClientProxy from SA failed."); return nullptr; @@ -222,14 +257,6 @@ void PrintManagerClient::OnRemoteSaDied(const wptr &remote) printServiceProxy_ = GetPrintServiceProxy(); } -PrintSaDeathRecipient::PrintSaDeathRecipient() {} - -void PrintSaDeathRecipient::OnRemoteDied(const wptr &object) -{ - PRINT_HILOGE("PrintSaDeathRecipient on remote systemAbility died."); - PrintManagerClient::GetInstance()->OnRemoteSaDied(object); -} - int32_t PrintManagerClient::StartPrint() { PRINT_HILOGD("PrintManagerClient EnqueueTask start."); @@ -239,10 +266,10 @@ int32_t PrintManagerClient::StartPrint() printServiceProxy_ = GetPrintServiceProxy(); } if (printServiceProxy_ == nullptr) { - PRINT_HILOGE("AddPrinters quit because redoing GetPrintServiceProxy failed."); + PRINT_HILOGE("StartPrint quit because redoing GetPrintServiceProxy failed."); return false; } - PRINT_HILOGD("PrintManagerClient AddPrinters succeeded."); + PRINT_HILOGD("PrintManagerClient StartPrint succeeded."); return printServiceProxy_->StartPrint(); } @@ -316,7 +343,7 @@ bool PrintManagerClient::StopDiscoverPrinter() return printServiceProxy_->StopDiscoverPrinter(); } -bool PrintManagerClient::StartPrintJob(PrintJob jobinfo) +bool PrintManagerClient::StartPrintJob(const PrintJob &jobinfo) { if (printServiceProxy_ == nullptr) { PRINT_HILOGW("Redo GetPrintServiceProxy"); @@ -330,7 +357,7 @@ bool PrintManagerClient::StartPrintJob(PrintJob jobinfo) return printServiceProxy_->StartPrintJob(jobinfo); } -bool PrintManagerClient::CancelPrintJob(PrintJob jobinfo) +bool PrintManagerClient::CancelPrintJob(const PrintJob &jobinfo) { if (printServiceProxy_ == nullptr) { PRINT_HILOGW("Redo GetPrintServiceProxy"); @@ -342,7 +369,7 @@ bool PrintManagerClient::CancelPrintJob(PrintJob jobinfo) } PRINT_HILOGD("PrintManagerClient CancelPrintJob succeeded."); return printServiceProxy_->CancelPrintJob(jobinfo); -} //// +} bool PrintManagerClient::AddPrinters(std::vector arrayPrintInfo) { @@ -400,7 +427,7 @@ bool PrintManagerClient::UpdatePrinterJobState(uint32_t jobId, uint32_t state) return printServiceProxy_->UpdatePrinterJobState(jobId, state); } -bool PrintManagerClient::RequestPreview(PrintJob jobinfo, std::string &previewResult) +bool PrintManagerClient::RequestPreview(const PrintJob &jobinfo, std::string &previewResult) { if (printServiceProxy_ == nullptr) { PRINT_HILOGW("Redo GetPrintServiceProxy"); @@ -442,4 +469,58 @@ bool PrintManagerClient::CheckPermission() PRINT_HILOGD("Check Permission enable"); return printServiceProxy_->CheckPermission(); } + +bool PrintManagerClient::LoadServer() +{ + if (ready_) { + return true; + } + std::lock_guard lock(loadMutex_); + if (ready_) { + return true; + } + + auto sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); + if (sm == nullptr) { + PRINT_HILOGE("GetSystemAbilityManager return null"); + return false; + } + + sptr loadCallback_ = new (std::nothrow) PrintSyncLoadCallback(); + if (loadCallback_ == nullptr) { + PRINT_HILOGE("new PrintSyncLoadCallback fail"); + return false; + } + + int32_t result = sm->LoadSystemAbility(PRINT_SERVICE_ID, loadCallback_); + if (result != ERR_OK) { + PRINT_HILOGE("LoadSystemAbility %{public}d failed, result: %{public}d", PRINT_SERVICE_ID, result); + return false; + } + + { + std::unique_lock conditionLock(conditionMutex_); + auto waitStatus = syncCon_.wait_for( + conditionLock, std::chrono::milliseconds(LOAD_SA_TIMEOUT_MS), [this]() { return ready_; }); + if (!waitStatus) { + PRINT_HILOGE("print server load sa timeout"); + return false; + } + } + return true; +} + +void PrintManagerClient::LoadServerSuccess() +{ + std::unique_lock lock(conditionMutex_); + ready_ = true; + syncCon_.notify_one(); + PRINT_HILOGE("load print server success"); +} + +void PrintManagerClient::LoadServerFail() +{ + ready_ = false; + PRINT_HILOGE("load print server fail"); +} } // namespace OHOS::Print diff --git a/frameworks/innerkitsimpl/src/print_notify.cpp b/frameworks/innerkitsimpl/src/print_notify.cpp index dd025ad7..c4bd1676 100644 --- a/frameworks/innerkitsimpl/src/print_notify.cpp +++ b/frameworks/innerkitsimpl/src/print_notify.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,8 +17,8 @@ #include +#include "napi_print_utils.h" #include "print_log.h" -#include "print_napi_utils.h" namespace OHOS::Print { PrintNotify::PrintNotify() {} diff --git a/frameworks/innerkitsimpl/src/print_page_size.cpp b/frameworks/innerkitsimpl/src/print_page_size.cpp index 01995cd2..7e928cb7 100644 --- a/frameworks/innerkitsimpl/src/print_page_size.cpp +++ b/frameworks/innerkitsimpl/src/print_page_size.cpp @@ -18,7 +18,135 @@ #include "print_log.h" namespace OHOS::Print { -PrintPageSize::PrintPageSize() : id_(0), name_("PrintPageSize"), width_(0), height_(0) {} +std::map> PrintPageSize::pageSize_; +void PrintPageSize::BuildPageSizeMap() +{ + pageSize_.insert(std::make_pair("ISO_A0", std::make_shared("ISO_A0", "Harmony", 33110, 46810))); + pageSize_.insert(std::make_pair("ISO_A1", std::make_shared("ISO_A1", "Harmony", 23390, 33110))); + pageSize_.insert(std::make_pair("ISO_A2", std::make_shared("ISO_A2", "Harmony", 16540, 23390))); + pageSize_.insert(std::make_pair("ISO_A3", std::make_shared("ISO_A3", "Harmony", 11690, 16540))); + pageSize_.insert(std::make_pair("ISO_A4", std::make_shared("ISO_A4", "Harmony", 8270, 11690))); + pageSize_.insert(std::make_pair("ISO_A5", std::make_shared("ISO_A5", "Harmony", 5830, 8270))); + pageSize_.insert(std::make_pair("ISO_A6", std::make_shared("ISO_A6", "Harmony", 4130, 5830))); + pageSize_.insert(std::make_pair("ISO_A7", std::make_shared("ISO_A7", "Harmony", 2910, 4130))); + pageSize_.insert(std::make_pair("ISO_A8", std::make_shared("ISO_A8", "Harmony", 2050, 2910))); + pageSize_.insert(std::make_pair("ISO_A9", std::make_shared("ISO_A9", "Harmony", 1460, 2050))); + pageSize_.insert(std::make_pair("ISO_A10", std::make_shared("ISO_A10", "Harmony", 1020, 1460))); + + pageSize_.insert(std::make_pair("ISO_B0", std::make_shared("ISO_B0", "Harmony", 39370, 55670))); + pageSize_.insert(std::make_pair("ISO_B1", std::make_shared("ISO_B1", "Harmony", 27830, 39370))); + pageSize_.insert(std::make_pair("ISO_B2", std::make_shared("ISO_B2", "Harmony", 19690, 27830))); + pageSize_.insert(std::make_pair("ISO_B3", std::make_shared("ISO_B3", "Harmony", 13900, 19690))); + pageSize_.insert(std::make_pair("ISO_B4", std::make_shared("ISO_B4", "Harmony", 39840, 13900))); + pageSize_.insert(std::make_pair("ISO_B5", std::make_shared("ISO_B5", "Harmony", 6930, 9840))); + pageSize_.insert(std::make_pair("ISO_B6", std::make_shared("ISO_B6", "Harmony", 4920, 6930))); + pageSize_.insert(std::make_pair("ISO_B7", std::make_shared("ISO_B7", "Harmony", 3460, 4920))); + pageSize_.insert(std::make_pair("ISO_B8", std::make_shared("ISO_B8", "Harmony", 2440, 3460))); + pageSize_.insert(std::make_pair("ISO_B9", std::make_shared("ISO_B9", "Harmony", 1730, 2440))); + pageSize_.insert(std::make_pair("ISO_B10", std::make_shared("ISO_B10", "Harmony", 1220, 1730))); + + pageSize_.insert(std::make_pair("ISO_C0", std::make_shared("ISO_C0", "Harmony", 36100, 51060))); + pageSize_.insert(std::make_pair("ISO_C1", std::make_shared("ISO_C1", "Harmony", 25510, 36100))); + pageSize_.insert(std::make_pair("ISO_C2", std::make_shared("ISO_C2", "Harmony", 18030, 25510))); + pageSize_.insert(std::make_pair("ISO_C3", std::make_shared("ISO_C3", "Harmony", 12760, 18030))); + pageSize_.insert(std::make_pair("ISO_C4", std::make_shared("ISO_C4", "Harmony", 9020, 12760))); + pageSize_.insert(std::make_pair("ISO_C5", std::make_shared("ISO_C5", "Harmony", 6380, 9020))); + pageSize_.insert(std::make_pair("ISO_C6", std::make_shared("ISO_C6", "Harmony", 4490, 6380))); + pageSize_.insert(std::make_pair("ISO_C7", std::make_shared("ISO_C7", "Harmony", 3190, 4490))); + pageSize_.insert(std::make_pair("ISO_C8", std::make_shared("ISO_C8", "Harmony", 2240, 3190))); + pageSize_.insert(std::make_pair("ISO_C9", std::make_shared("ISO_C9", "Harmony", 1570, 2240))); + pageSize_.insert(std::make_pair("ISO_C10", std::make_shared("ISO_C10", "Harmony", 1100, 1570))); + + // North America + pageSize_.insert(std::make_pair("NA_LETTER", std::make_shared("NA_LETTER", "Harmony", 8500, 11000))); + pageSize_.insert( + std::make_pair("NA_GOVT_LETTER", std::make_shared("NA_GOVT_LETTER", "Harmony", 8000, 10500))); + pageSize_.insert(std::make_pair("NA_LEGAL", std::make_shared("NA_LEGAL", "Harmony", 8500, 14000))); + pageSize_.insert( + std::make_pair("NA_JUNIOR_LEGAL", std::make_shared("NA_JUNIOR_LEGAL", "Harmony", 8000, 5000))); + pageSize_.insert( + std::make_pair("NA_LEDGER", std::make_shared("NA_LEDGER", "Harmony", 17000, 11000))); + pageSize_.insert( + std::make_pair("NA_TABLOID", std::make_shared("NA_TABLOID", "Harmony", 11000, 17000))); + pageSize_.insert( + std::make_pair("NA_INDEX_3X5", std::make_shared("NA_INDEX_3X5", "Harmony", 3000, 5000))); + pageSize_.insert( + std::make_pair("NA_INDEX_4X6", std::make_shared("NA_INDEX_4X6", "Harmony", 4000, 6000))); + pageSize_.insert( + std::make_pair("NA_INDEX_5X8", std::make_shared("NA_INDEX_5X8", "Harmony", 5000, 8000))); + pageSize_.insert(std::make_pair("NA_QUARTO", std::make_shared("NA_QUARTO", "Harmony", 8000, 10000))); + pageSize_.insert( + std::make_pair("NA_FOOLSCAP", std::make_shared("NA_FOOLSCAP", "Harmony", 8000, 13000))); + + // Chinese + pageSize_.insert(std::make_pair("ROC_8K", std::make_shared("ROC_8K", "Harmony", 10629, 15354))); + pageSize_.insert(std::make_pair("ROC_16K", std::make_shared("ROC_16K", "Harmony", 7677, 10629))); + pageSize_.insert(std::make_pair("PRC_1", std::make_shared("PRC_1", "Harmony", 4015, 6496))); + pageSize_.insert(std::make_pair("PRC_2", std::make_shared("PRC_2", "Harmony", 4015, 6929))); + pageSize_.insert(std::make_pair("PRC_3", std::make_shared("PRC_3", "Harmony", 4921, 6929))); + pageSize_.insert(std::make_pair("PRC_4", std::make_shared("PRC_4", "Harmony", 4330, 8189))); + pageSize_.insert(std::make_pair("PRC_5", std::make_shared("PRC_5", "Harmony", 4330, 8661))); + pageSize_.insert(std::make_pair("PRC_6", std::make_shared("PRC_6", "Harmony", 4724, 12599))); + pageSize_.insert(std::make_pair("PRC_7", std::make_shared("PRC_7", "Harmony", 6299, 9055))); + pageSize_.insert(std::make_pair("PRC_8", std::make_shared("PRC_8", "Harmony", 4724, 12165))); + pageSize_.insert(std::make_pair("PRC_9", std::make_shared("PRC_9", "Harmony", 9016, 12756))); + pageSize_.insert(std::make_pair("PRC_10", std::make_shared("PRC_10", "Harmony", 12756, 18032))); + pageSize_.insert(std::make_pair("PRC_16K", std::make_shared("PRC_16K", "Harmony", 5749, 8465))); + pageSize_.insert( + std::make_pair("OM_PA_KAI", std::make_shared("OM_PA_KAI", "Harmony", 10512, 15315))); + pageSize_.insert( + std::make_pair("OM_DAI_PA_KAI", std::make_shared("OM_DAI_PA_KAI", "Harmony", 10827, 15551))); + pageSize_.insert( + std::make_pair("OM_JUURO_KU_KAI", std::make_shared("OM_JUURO_KU_KAI", "Harmony", 7796, 10827))); + + // Japanese + pageSize_.insert(std::make_pair("JIS_B10", std::make_shared("JIS_B10", "Harmony", 1259, 1772))); + pageSize_.insert(std::make_pair("JIS_B9", std::make_shared("JIS_B9", "Harmony", 1772, 2520))); + pageSize_.insert(std::make_pair("JIS_B8", std::make_shared("JIS_B8", "Harmony", 2520, 3583))); + pageSize_.insert(std::make_pair("JIS_B7", std::make_shared("JIS_B7", "Harmony", 3583, 5049))); + pageSize_.insert(std::make_pair("JIS_B6", std::make_shared("JIS_B6", "Harmony", 5049, 7165))); + pageSize_.insert(std::make_pair("JIS_B5", std::make_shared("JIS_B5", "Harmony", 7165, 10118))); + pageSize_.insert(std::make_pair("JIS_B4", std::make_shared("JIS_B4", "Harmony", 10118, 14331))); + pageSize_.insert(std::make_pair("JIS_B3", std::make_shared("JIS_B3", "Harmony", 14331, 20276))); + pageSize_.insert(std::make_pair("JIS_B2", std::make_shared("JIS_B2", "Harmony", 20276, 28661))); + pageSize_.insert(std::make_pair("JIS_B1", std::make_shared("JIS_B1", "Harmony", 28661, 40551))); + pageSize_.insert(std::make_pair("JIS_B0", std::make_shared("JIS_B0", "Harmony", 40551, 57323))); + pageSize_.insert(std::make_pair("JIS_EXEC", std::make_shared("JIS_EXEC", "Harmony", 8504, 12992))); + pageSize_.insert(std::make_pair("JPN_CHOU4", std::make_shared("JPN_CHOU4", "Harmony", 3543, 8071))); + pageSize_.insert(std::make_pair("JPN_CHOU3", std::make_shared("JPN_CHOU3", "Harmony", 4724, 9252))); + pageSize_.insert(std::make_pair("JPN_CHOU2", std::make_shared("JPN_CHOU2", "Harmony", 4374, 5748))); + pageSize_.insert( + std::make_pair("JPN_HAGAKI", std::make_shared("JPN_HAGAKI", "Harmony", 3937, 5827))); + pageSize_.insert( + std::make_pair("JPN_OUFUKU", std::make_shared("JPN_OUFUKU", "Harmony", 5827, 7874))); + pageSize_.insert(std::make_pair("JPN_KAHU", std::make_shared("JPN_KAHU", "Harmony", 9449, 12681))); + pageSize_.insert(std::make_pair("JPN_KAKU2", std::make_shared("JPN_KAKU2", "Harmony", 9449, 13071))); + pageSize_.insert(std::make_pair("JPN_YOU4", std::make_shared("JPN_YOU4", "Harmony", 4134, 9252))); +} + +PrintPageSize::PrintPageSize() : id_(""), name_("PrintPageSize"), width_(0), height_(0) {} + +PrintPageSize::PrintPageSize(PAGE_SIZE_ID id, DiscretePageName name, uint32_t width, uint32_t height) +{ + id_ = id; + name_ = name; + width_ = width; + height_ = height; +} + +PrintPageSize PrintPageSize::GetPageSize(PageSizeId id) +{ + uint32_t pageSizeId = (uint32_t)id; + if (pageSizeId < (uint32_t)sizeof(PAGE_SIZE_TABLE)) { + std::map>::iterator iter; + iter = pageSize_.find(PAGE_SIZE_TABLE[pageSizeId]); + if (iter != pageSize_.end()) { + return *(iter->second); + } + } + PrintPageSize printPageSize; + return printPageSize; +} PrintPageSize::PrintPageSize(const PrintPageSize &right) { @@ -41,7 +169,7 @@ PrintPageSize &PrintPageSize::operator=(const PrintPageSize &right) PrintPageSize::~PrintPageSize() {} -void PrintPageSize::SetId(uint32_t id) +void PrintPageSize::SetId(const std::string &id) { id_ = id; } @@ -61,7 +189,7 @@ void PrintPageSize::SetHeight(uint32_t height) height_ = height; } -uint32_t PrintPageSize::GetId() const +const std::string &PrintPageSize::GetId() const { return id_; } @@ -83,7 +211,7 @@ uint32_t PrintPageSize::GetHeight() const void PrintPageSize::Dump() { - PRINT_HILOGD("top_ = %{public}d", id_); + PRINT_HILOGD("top_ = %{public}s", id_.c_str()); PRINT_HILOGD("name_ = %{public}s", name_.c_str()); PRINT_HILOGD("width_ = %{public}d", width_); PRINT_HILOGD("height_ = %{public}d", height_); diff --git a/frameworks/innerkitsimpl/src/preview_attribute.cpp b/frameworks/innerkitsimpl/src/print_preview_attribute.cpp similarity index 96% rename from frameworks/innerkitsimpl/src/preview_attribute.cpp rename to frameworks/innerkitsimpl/src/print_preview_attribute.cpp index b6c20b87..bad1a2c9 100644 --- a/frameworks/innerkitsimpl/src/preview_attribute.cpp +++ b/frameworks/innerkitsimpl/src/print_preview_attribute.cpp @@ -1,4 +1,4 @@ -#include "preview_attribute.h" +#include "print_preview_attribute.h" #include #include diff --git a/frameworks/innerkitsimpl/src/print_progress_notify.cpp b/frameworks/innerkitsimpl/src/print_progress_notify.cpp index 5b3e91e8..86ee78d9 100644 --- a/frameworks/innerkitsimpl/src/print_progress_notify.cpp +++ b/frameworks/innerkitsimpl/src/print_progress_notify.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -17,9 +17,9 @@ #include +#include "napi_print_utils.h" #include "print_job.h" #include "print_log.h" -#include "print_napi_utils.h" #include "print_page_size.h" #include "print_resolution.h" #include "printer_capability.h" diff --git a/frameworks/innerkitsimpl/src/print_range.cpp b/frameworks/innerkitsimpl/src/print_range.cpp index 97fc0157..06788f4e 100644 --- a/frameworks/innerkitsimpl/src/print_range.cpp +++ b/frameworks/innerkitsimpl/src/print_range.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/innerkitsimpl/src/print_sa_death_recipient.cpp b/frameworks/innerkitsimpl/src/print_sa_death_recipient.cpp new file mode 100644 index 00000000..138e9f9e --- /dev/null +++ b/frameworks/innerkitsimpl/src/print_sa_death_recipient.cpp @@ -0,0 +1,29 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "print_sa_death_recipient.h" + +#include "print_log.h" +#include "print_manager_client.h" + +namespace OHOS::Print { +PrintSaDeathRecipient::PrintSaDeathRecipient() {} + +void PrintSaDeathRecipient::OnRemoteDied(const wptr &object) +{ + PRINT_HILOGE("PrintSaDeathRecipient on remote systemAbility died."); + PrintManagerClient::GetInstance()->OnRemoteSaDied(object); +} +} // namespace OHOS::Print diff --git a/frameworks/innerkitsimpl/src/print_service_proxy.cpp b/frameworks/innerkitsimpl/src/print_service_proxy.cpp index 5dfea48e..5b57f9d4 100644 --- a/frameworks/innerkitsimpl/src/print_service_proxy.cpp +++ b/frameworks/innerkitsimpl/src/print_service_proxy.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -28,7 +28,7 @@ namespace OHOS::Print { using namespace OHOS::HiviewDFX; -PrintServiceProxy::PrintServiceProxy(const sptr &object) : IRemoteProxy(object) {} +PrintServiceProxy::PrintServiceProxy(const sptr &object) : IRemoteProxy(object) {} int32_t PrintServiceProxy::StartPrint() { @@ -289,7 +289,7 @@ bool PrintServiceProxy::QueryPrinterCapability(uint32_t printerId, PrinterCapabi std::vector pageSizeList; for (uint32_t i = 0; i < arraypageSizeLength; i++) { PrintPageSize pageSize; - pageSize.SetId(reply.ReadUint32()); + pageSize.SetId(reply.ReadString()); pageSize.SetName(reply.ReadString()); pageSize.SetWidth(reply.ReadUint32()); pageSize.SetHeight(reply.ReadUint32()); @@ -339,7 +339,7 @@ bool PrintServiceProxy::CheckPermission() } bool PrintServiceProxy::On( - const std::string &type, uint32_t &state, PrinterInfo &info, const sptr &listener) + const std::string &type, uint32_t &state, PrinterInfo &info, const sptr &listener) { PRINT_HILOGD("PrintServiceProxy::On listener=%{public}p", listener.GetRefPtr()); MessageParcel data, reply; @@ -412,7 +412,7 @@ bool PrintServiceProxy::Off(const std::string &type) return true; } -bool PrintServiceProxy::RegisterExtCallback(uint32_t callbackId, const sptr &listener) +bool PrintServiceProxy::RegisterExtCallback(uint32_t callbackId, const sptr &listener) { PRINT_HILOGD("PrintServiceProxy::RegisterExtCallback in"); MessageParcel data, reply; @@ -493,7 +493,7 @@ void PrintServiceProxy::BuildParcelFromPrintJob(MessageParcel &data, const Print PrintPageSize pageSize; jobinfo.GetPageSize(pageSize); - data.WriteUint32(pageSize.GetId()); + data.WriteString(pageSize.GetId()); data.WriteString(pageSize.GetName()); data.WriteUint32(pageSize.GetWidth()); data.WriteUint32(pageSize.GetHeight()); @@ -554,7 +554,7 @@ void PrintServiceProxy::BuildParcelFromPrinterInfo(MessageParcel &data, const Pr data.WriteUint32(margin.GetRight()); for (uint32_t i = 0; i < pageSizeLength; i++) { - data.WriteUint32(pageSizeList[i].GetId()); + data.WriteString(pageSizeList[i].GetId()); data.WriteString(pageSizeList[i].GetName()); data.WriteUint32(pageSizeList[i].GetWidth()); data.WriteUint32(pageSizeList[i].GetHeight()); diff --git a/frameworks/innerkitsimpl/src/print_sync_load_callback.cpp b/frameworks/innerkitsimpl/src/print_sync_load_callback.cpp new file mode 100644 index 00000000..d5fa7b9c --- /dev/null +++ b/frameworks/innerkitsimpl/src/print_sync_load_callback.cpp @@ -0,0 +1,42 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "print_sync_load_callback.h" + +#include "iservice_registry.h" +#include "isystem_ability_load_callback.h" +#include "print_log.h" +#include "print_manager_client.h" +#include "system_ability_definition.h" + +namespace OHOS::Print { +void PrintSyncLoadCallback::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr &remoteObject) +{ + if (systemAbilityId != PRINT_SERVICE_ID) { + PRINT_HILOGE("start systemAbilityId is not print server"); + return; + } + PrintManagerClient::GetInstance()->LoadServerSuccess(); +} + +void PrintSyncLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId) +{ + if (systemAbilityId != PRINT_SERVICE_ID) { + PRINT_HILOGE("start systemAbilityId is not print server"); + return; + } + PrintManagerClient::GetInstance()->LoadServerFail(); +} +} // namespace OHOS::Print \ No newline at end of file diff --git a/frameworks/innerkitsimpl/src/printer_capability.cpp b/frameworks/innerkitsimpl/src/printer_capability.cpp index 069dda0c..105d0777 100644 --- a/frameworks/innerkitsimpl/src/printer_capability.cpp +++ b/frameworks/innerkitsimpl/src/printer_capability.cpp @@ -15,9 +15,30 @@ #include "printer_capability.h" +#include "napi_print_utils.h" #include "print_log.h" namespace OHOS::Print { +static constexpr const char *PARAM_CAPABILITY_MINMARGIN = "minMargin"; +static constexpr const char *PARAM_CAPABILITY_PAGESIZE = "pageSize"; +static constexpr const char *PARAM_CAPABILITY_RESOLUTION = "resolution"; +static constexpr const char *PARAM_CAPABILITY_COLORMODE = "colorMode"; +static constexpr const char *PARAM_CAPABILITY_DUPLEXMODE = "duplexMode"; + +static constexpr const char *PARAM_PAGESIZE_ID = "id"; +static constexpr const char *PARAM_PAGESIZE_NAME = "name"; +static constexpr const char *PARAM_PAGESIZE_WIDTH = "width"; +static constexpr const char *PARAM_PAGESIZE_HEIGHT = "height"; + +static constexpr const char *PARAM_RESOLUTION_ID = "id"; +static constexpr const char *PARAM_RESOLUTION_HORIZONTALDPI = "horizontalDpi"; +static constexpr const char *PARAM_RESOLUTION_VERTICALDPI = "verticalDpi"; + +static constexpr const char *PARAM_MARGIN_TOP = "top"; +static constexpr const char *PARAM_MARGIN_BOTTOM = "bottom"; +static constexpr const char *PARAM_MARGIN_LEFT = "left"; +static constexpr const char *PARAM_MARGIN_RIGHT = "right"; + PrinterCapability::PrinterCapability() : minMargin_(), colorMode_(0), duplexMode_(0) { pageSizeList_.clear(); @@ -47,7 +68,7 @@ PrinterCapability &PrinterCapability::operator=(const PrinterCapability &right) PrinterCapability::~PrinterCapability() {} -void PrinterCapability::SetMinMargin(PrintMargin &minMargin) +void PrinterCapability::SetMinMargin(const PrintMargin &minMargin) { minMargin_ = minMargin; } @@ -101,6 +122,367 @@ uint32_t PrinterCapability::GetDuplexMode() const return duplexMode_; } +void PrinterCapability::ConvertToParcel(MessageParcel &reply) const +{ + std::vector pageSizeList; + std::vector resolutionList; + + GetPageSize(pageSizeList); + GetResolution(resolutionList); + uint32_t arraypageSizeLength = pageSizeList.size(); + reply.WriteUint32(arraypageSizeLength); + uint32_t resolutionLength = resolutionList.size(); + reply.WriteUint32(resolutionLength); + + PrintMargin minMargin; + GetMinMargin(minMargin); + reply.WriteUint32(minMargin.GetTop()); + reply.WriteUint32(minMargin.GetBottom()); + reply.WriteUint32(minMargin.GetLeft()); + reply.WriteUint32(minMargin.GetRight()); + + for (uint32_t i = 0; i < arraypageSizeLength; i++) { + reply.WriteString(pageSizeList[i].GetId()); + reply.WriteString(pageSizeList[i].GetName()); + reply.WriteUint32(pageSizeList[i].GetWidth()); + reply.WriteUint32(pageSizeList[i].GetHeight()); + } + + for (uint32_t i = 0; i < resolutionLength; i++) { + reply.WriteUint32(resolutionList[i].GetId()); + reply.WriteUint32(resolutionList[i].GetHorizontalDpi()); + reply.WriteUint32(resolutionList[i].GetVerticalDpi()); + } + + reply.WriteUint32(GetColorMode()); + reply.WriteUint32(GetDuplexMode()); +} + +void PrinterCapability::BuildFromParcel(MessageParcel &data) +{ + uint32_t arraypageSizeLength = data.ReadUint32(); + uint32_t resolutionLength = data.ReadUint32(); + PRINT_HILOGD("PrintServiceProxy, arraypageSizeLength = %{public}d", arraypageSizeLength); + PRINT_HILOGD("PrintServiceProxy, resolutionLength = %{public}d", resolutionLength); + + PrintMargin margin; + margin.SetTop(data.ReadUint32()); + margin.SetBottom(data.ReadUint32()); + margin.SetLeft(data.ReadUint32()); + margin.SetRight(data.ReadUint32()); + SetMinMargin(margin); + + if (arraypageSizeLength > 0) { + std::vector pageSizeList; + for (uint32_t i = 0; i < arraypageSizeLength; i++) { + PrintPageSize pageSize; + pageSize.SetId(data.ReadString()); + pageSize.SetName(data.ReadString()); + pageSize.SetWidth(data.ReadUint32()); + pageSize.SetHeight(data.ReadUint32()); + pageSizeList.push_back(pageSize); + } + SetPageSize(pageSizeList); + } + + if (resolutionLength > 0) { + std::vector resolutionList; + for (uint32_t i = 0; i < resolutionLength; i++) { + PrintResolution res; + res.SetId(data.ReadUint32()); + res.SetHorizontalDpi(data.ReadUint32()); + res.SetVerticalDpi(data.ReadUint32()); + resolutionList.push_back(res); + } + SetResolution(resolutionList); + } + + SetColorMode(data.ReadUint32()); + SetDuplexMode(data.ReadUint32()); + Dump(); +} + +void PrinterCapability::ConvertToJs(napi_env env, napi_value *result) const +{ + PRINT_HILOGD("Enter ConvertToJs---->"); + napi_value resultPrintMargin; + napi_create_object(env, result); + napi_create_object(env, &resultPrintMargin); + napi_value arrPageSize, arrResolution; + napi_status status = napi_create_array(env, &arrPageSize); + status = napi_create_array(env, &arrResolution); + + PrintNapiUtils::SetUint32Property(env, *result, "colorMode", GetColorMode()); + PrintNapiUtils::SetUint32Property(env, *result, "duplexMode", GetDuplexMode()); + + PrintMargin margin; + GetMinMargin(margin); + PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "top", margin.GetTop()); + PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "bottom", margin.GetBottom()); + PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "left", margin.GetLeft()); + PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "right", margin.GetRight()); + + std::vector pageSize; + GetPageSize(pageSize); + uint32_t printerCapabilityLength = pageSize.size(); + + for (uint32_t i = 0; i < printerCapabilityLength; i++) { + napi_value resultPrinterPageSize; + napi_create_object(env, &resultPrinterPageSize); + PrintNapiUtils::SetStringPropertyUtf8(env, resultPrinterPageSize, "id", pageSize[i].GetId().c_str()); + PrintNapiUtils::SetStringPropertyUtf8(env, resultPrinterPageSize, "name", pageSize[i].GetName().c_str()); + PrintNapiUtils::SetUint32Property(env, resultPrinterPageSize, "width", pageSize[i].GetWidth()); + PrintNapiUtils::SetUint32Property(env, resultPrinterPageSize, "height", pageSize[i].GetHeight()); + status = napi_set_element(env, arrPageSize, i, resultPrinterPageSize); + } + + std::vector resolutionList; + GetResolution(resolutionList); + uint32_t printerCapabilityresolutionLength = resolutionList.size(); + for (uint32_t i = 0; i < printerCapabilityresolutionLength; i++) { + napi_value resultPrinterResolution; + napi_create_object(env, &resultPrinterResolution); + PrintNapiUtils::SetUint32Property(env, resultPrinterResolution, "id", resolutionList[i].GetId()); + PrintNapiUtils::SetUint32Property( + env, resultPrinterResolution, "horizontalDpi", resolutionList[i].GetHorizontalDpi()); + PrintNapiUtils::SetUint32Property( + env, resultPrinterResolution, "verticalDpi", resolutionList[i].GetVerticalDpi()); + status = napi_set_element(env, arrResolution, i, resultPrinterResolution); + } + + status = napi_set_named_property(env, *result, "minMargin", resultPrintMargin); + PRINT_HILOGD("output ---- status[%{public}d]", status); + napi_set_named_property(env, *result, "pageSize", arrPageSize); + napi_set_named_property(env, *result, "resolution", arrResolution); + PRINT_HILOGD("ouput over---->"); +} + +void PrinterCapability::BuildFromJs(napi_env env, napi_value capValue) +{ + ParseCapability(env, capValue); +} + +bool PrinterCapability::ParseCapability(napi_env env, napi_value capValue) +{ + if (!ParseCapabilityParam(env, capValue)) { + PRINT_HILOGD("ParseCapabilityParam is error!"); + return false; + } + SetColorMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_COLORMODE)); + SetDuplexMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_DUPLEXMODE)); + PRINT_HILOGD("capability_value colorMode value is %{public}d", GetColorMode()); + PRINT_HILOGD("capability_value duplexMode value is %{public}d", GetDuplexMode()); + return true; +} + +bool PrinterCapability::ParseCapabilityParam(napi_env env, napi_value capValue) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_MINMARGIN); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_object) { + PRINT_HILOGD("error param_one"); + return false; + } else { + PrintMargin margin; + if (!ParseMargin(env, param_one, margin)) { + PRINT_HILOGD("ParseCapability type error!"); + return false; + } + SetMinMargin(margin); + } + + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_PAGESIZE); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_object) { + PRINT_HILOGD("error param_two"); + return false; + } else { + bool isArray = false; + napi_is_array(env, param_two, &isArray); + if (!isArray) { + PRINT_HILOGD("ParsePageSize type error!"); + return false; + } + + std::vector pageSizeList; + uint32_t arrayLength = 0; + napi_get_array_length(env, param_two, &arrayLength); + for (uint32_t i = 0; i < arrayLength; i++) { + napi_value pageSizeValue; + PrintPageSize pageSize; + napi_get_element(env, param_two, i, &pageSizeValue); + if (!ParsePageSize(env, pageSizeValue, pageSize)) { + PRINT_HILOGD("ParsePageSize type error!"); + return false; + } + pageSizeList.push_back(pageSize); + } + SetPageSize(pageSizeList); + } + + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_RESOLUTION); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_object) { + PRINT_HILOGD("error param_three"); + return false; + } else { + bool isReArray = false; + napi_is_array(env, param_three, &isReArray); + if (!isReArray) { + PRINT_HILOGD("PrintResolution type error!"); + return false; + } + std::vector resolutionList; + uint32_t arrayReLength = 0; + napi_get_array_length(env, param_three, &arrayReLength); + for (uint32_t i = 0; i < arrayReLength; i++) { + napi_value reValue; + PrintResolution resolution; + napi_get_element(env, param_three, i, &reValue); + if (!ParseResolution(env, reValue, resolution)) { + PRINT_HILOGD("PrintResolution type error!"); + return false; + } + resolutionList.push_back(resolution); + } + SetResolution(resolutionList); + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_COLORMODE); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + napi_value param_five = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_DUPLEXMODE); + if (PrintNapiUtils::GetValueType(env, param_five) != napi_number) { + PRINT_HILOGD("error param_five"); + return false; + } + auto names = PrintNapiUtils::GetPropertyNames(env, capValue); + return true; +} + +bool PrinterCapability::ParseMargin(napi_env env, napi_value marginValue, PrintMargin &margin) +{ + if (!ParseMarginParam(env, marginValue, margin)) { + PRINT_HILOGD("ParseResolutionParam is error!"); + return false; + } + margin.SetTop(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_TOP)); + margin.SetBottom(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_BOTTOM)); + margin.SetLeft(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_LEFT)); + margin.SetRight(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_RIGHT)); + + PRINT_HILOGD("printerMargin_value GetTop value is %{public}d", margin.GetTop()); + PRINT_HILOGD("printerMargin_value GetBottom value is %{public}d", margin.GetBottom()); + PRINT_HILOGD("printerMargin_value GetLeft value is %{public}d", margin.GetLeft()); + PRINT_HILOGD("printerMargin_value GetRight value is %{public}d", margin.GetRight()); + + return true; +} + +bool PrinterCapability::ParseMarginParam(napi_env env, napi_value marginValue, PrintMargin &margin) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_TOP); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_BOTTOM); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_LEFT); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_RIGHT); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + auto names = PrintNapiUtils::GetPropertyNames(env, marginValue); + PRINT_HILOGD("current margin paramster name list size = %{public}zu", names.size()); + return true; +} + +bool PrinterCapability::ParsePageSize(napi_env env, napi_value capValue, PrintPageSize &pageSize) +{ + if (!ParsePageSizeParam(env, capValue, pageSize)) { + PRINT_HILOGD("ParsePageSizeParam is error!"); + return false; + } + pageSize.SetId(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_PAGESIZE_ID)); + pageSize.SetName(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_PAGESIZE_NAME)); + pageSize.SetWidth(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_WIDTH)); + pageSize.SetHeight(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_HEIGHT)); + + PRINT_HILOGD("printerPageSize_value GetId value is %{public}s", pageSize.GetId().c_str()); + PRINT_HILOGD("printerPageSize_value GetName value is %{public}s", pageSize.GetName().c_str()); + PRINT_HILOGD("printerPageSize_value GetWidth value is %{public}d", pageSize.GetWidth()); + PRINT_HILOGD("printerPageSize_value GetHeight value is %{public}d", pageSize.GetHeight()); + return true; +} + +bool PrinterCapability::ParsePageSizeParam(napi_env env, napi_value capValue, PrintPageSize &pageSize) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_ID); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_NAME); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_WIDTH); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_HEIGHT); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + return true; +} + +bool PrinterCapability::ParseResolution(napi_env env, napi_value reValue, PrintResolution &resolution) +{ + if (!ParseResolutionParam(env, reValue, resolution)) { + PRINT_HILOGD("ParseResolutionParam is error!"); + return false; + } + resolution.SetId(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_ID)); + resolution.SetHorizontalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI)); + resolution.SetVerticalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_VERTICALDPI)); + + PRINT_HILOGD("printerResolution_value GetId value is %{public}d", resolution.GetId()); + PRINT_HILOGD("printerResolution_value GetHorizontalDpi value is %{public}d", resolution.GetHorizontalDpi()); + PRINT_HILOGD("printerResolution_value GetVerticalDpi value is %{public}d", resolution.GetVerticalDpi()); + return true; +} + +bool PrinterCapability::ParseResolutionParam(napi_env env, napi_value reValue, PrintResolution &resolution) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_ID); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_VERTICALDPI); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + return true; +} + void PrinterCapability::Dump() { PRINT_HILOGD("colorMode_ = %{public}d", colorMode_); diff --git a/frameworks/innerkitsimpl/src/printer_info.cpp b/frameworks/innerkitsimpl/src/printer_info.cpp index 8e8f5e37..4f2405fb 100644 --- a/frameworks/innerkitsimpl/src/printer_info.cpp +++ b/frameworks/innerkitsimpl/src/printer_info.cpp @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -18,7 +18,6 @@ #include "print_constant.h" #include "print_log.h" - namespace OHOS::Print { PrinterInfo::PrinterInfo() : printerId_(0), printerName_(""), printerIcon_(0), printerState_(PRINTER_UNKNOWN), description_("") diff --git a/frameworks/kits/extension/BUILD.gn b/frameworks/kits/extension/BUILD.gn index b9082cc5..17368bb2 100644 --- a/frameworks/kits/extension/BUILD.gn +++ b/frameworks/kits/extension/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at @@ -92,10 +92,11 @@ ohos_shared_library("print_extension") { ] sources = [ - "//base/print/print/frameworks/kits/extension/src/js_print_extension.cpp", - "//base/print/print/frameworks/kits/extension/src/js_print_extension_context.cpp", - "//base/print/print/frameworks/kits/extension/src/print_extension.cpp", - "//base/print/print/frameworks/kits/extension/src/print_extension_context.cpp", + "src/js_print_extension.cpp", + "src/js_print_extension_context.cpp", + "src/print_extension.cpp", + "src/print_extension_context.cpp", + "src/js_print_callback.cpp", ] configs = [ ":ability_config" ] public_configs = [ diff --git a/frameworks/kits/extension/include/js_print_callback.h b/frameworks/kits/extension/include/js_print_callback.h new file mode 100644 index 00000000..1ae52bdc --- /dev/null +++ b/frameworks/kits/extension/include/js_print_callback.h @@ -0,0 +1,63 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef JS_PRINT_CALLBACK_H +#define JS_PRINT_CALLBACK_H + +#include +#include +#include +#include + +#include "napi/native_api.h" + +class NativeValue; +class NativeEngine; + +namespace OHOS { +namespace AbilityRuntime { +class JsRuntime; + +class JsPrintCallback : public std::enable_shared_from_this { +public: + explicit JsPrintCallback(JsRuntime &jsRutime); + ~JsPrintCallback() = default; + NativeValue *Exec(NativeValue *jsObj, const std::string &name, NativeValue *const *argv = nullptr, size_t argc = 0, + bool isSync = true); + +private: + struct Container { + std::shared_ptr self; + NativeEngine *nativeEngine; + NativeValue *jsObj; + NativeValue *jsMethod; + NativeValue *const *argv; + size_t argc; + NativeValue *jsResult; + bool isSync; + bool isCompleted; + }; + JsRuntime &jsRuntime_; + uv_work_t *jsWorker_; + + JsPrintCallback::Container container_; + + std::mutex conditionMutex_; + std::condition_variable syncCon_; + static constexpr int SYNC_TIME_OUT = 1000; +}; +} // namespace AbilityRuntime +} // namespace OHOS +#endif // JS_PRINT_CALLBACK_H diff --git a/frameworks/kits/extension/include/js_print_extension.h b/frameworks/kits/extension/include/js_print_extension.h index bcf4f690..b31e12a7 100644 --- a/frameworks/kits/extension/include/js_print_extension.h +++ b/frameworks/kits/extension/include/js_print_extension.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -108,6 +108,7 @@ public: private: NativeValue *CallObjectMethod(const char *name, NativeValue *const *argv = nullptr, size_t argc = 0); + void RegisterAllCallback(); void GetSrcPath(std::string &srcPath); diff --git a/frameworks/kits/extension/include/js_print_extension_context.h b/frameworks/kits/extension/include/js_print_extension_context.h index 1a8e68c9..b57ae013 100644 --- a/frameworks/kits/extension/include/js_print_extension_context.h +++ b/frameworks/kits/extension/include/js_print_extension_context.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/include/print_extension.h b/frameworks/kits/extension/include/print_extension.h index cb94dd45..546cf706 100644 --- a/frameworks/kits/extension/include/print_extension.h +++ b/frameworks/kits/extension/include/print_extension.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/include/print_extension_context.h b/frameworks/kits/extension/include/print_extension_context.h index 22acf032..169dbb60 100644 --- a/frameworks/kits/extension/include/print_extension_context.h +++ b/frameworks/kits/extension/include/print_extension_context.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/include/print_extension_module_loader.h b/frameworks/kits/extension/include/print_extension_module_loader.h index 088f61bc..c36aafc9 100644 --- a/frameworks/kits/extension/include/print_extension_module_loader.h +++ b/frameworks/kits/extension/include/print_extension_module_loader.h @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/src/js_print_callback.cpp b/frameworks/kits/extension/src/js_print_callback.cpp new file mode 100644 index 00000000..9df2b92c --- /dev/null +++ b/frameworks/kits/extension/src/js_print_callback.cpp @@ -0,0 +1,111 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "js_print_callback.h" + +#include "ability_info.h" +#include "iprint_extension_callback.h" +#include "js_print_extension_context.h" +#include "js_runtime.h" +#include "js_runtime_utils.h" +#include "napi/native_api.h" +#include "napi/native_node_api.h" +#include "napi_common_util.h" +#include "napi_common_want.h" +#include "napi_print_utils.h" +#include "napi_remote_object.h" +#include "print_log.h" +#include "print_manager_client.h" + +namespace OHOS { +namespace AbilityRuntime { + +using namespace OHOS::AppExecFwk; +using namespace OHOS::Print; + +JsPrintCallback::JsPrintCallback(JsRuntime &jsRuntime) : jsRuntime_(jsRuntime) {} + +NativeValue *JsPrintCallback::Exec( + NativeValue *jsObj, const std::string &name, NativeValue *const *argv, size_t argc, bool isSync) +{ + HandleScope handleScope(jsRuntime_); + NativeObject *obj = ConvertNativeValueTo(jsObj); + if (obj == nullptr) { + PRINT_HILOGE("Failed to get PrintExtension object"); + return nullptr; + } + + NativeValue *method = obj->GetProperty(name.c_str()); + if (method == nullptr) { + PRINT_HILOGE("Failed to get '%{public}s' from PrintExtension object", name.c_str()); + return nullptr; + } + + PRINT_HILOGD("%{public}s callback in", name.c_str()); + + NativeEngine *nativeEngine = &jsRuntime_.GetNativeEngine(); + uv_loop_s *loop = nullptr; + napi_get_uv_event_loop(reinterpret_cast(nativeEngine), &loop); + if (loop == nullptr) { + PRINT_HILOGE("Failed to get uv event loop"); + return nullptr; + } + jsWorker_ = new (std::nothrow) uv_work_t; + if (jsWorker_ == nullptr) { + PRINT_HILOGE("Failed to create uv work"); + return nullptr; + } + + container_.self = shared_from_this(); + container_.nativeEngine = nativeEngine; + container_.jsObj = jsObj; + container_.jsMethod = method; + container_.argv = argv; + container_.argc = argc; + container_.jsResult = nullptr; + container_.isSync = isSync; + container_.isCompleted = false; + jsWorker_->data = &container_; + + uv_queue_work( + loop, jsWorker_, [](uv_work_t *work) {}, + [](uv_work_t *work, int statusInt) { + auto container = reinterpret_cast(work->data); + if (container != nullptr) { + container->jsResult = container->nativeEngine->CallFunction( + container->jsObj, container->jsMethod, container->argv, container->argc); + container->isCompleted = true; + if (container->isSync) { + container->self = nullptr; + } else { + std::unique_lock lock(container->self->conditionMutex_); + container->self->syncCon_.notify_one(); + } + } + }); + if (isSync) { + std::unique_lock conditionLock(conditionMutex_); + auto waitStatus = syncCon_.wait_for( + conditionLock, std::chrono::milliseconds(SYNC_TIME_OUT), [this]() { return container_.isCompleted; }); + if (!waitStatus) { + PRINT_HILOGE("print server load sa timeout"); + return nullptr; + } + return container_.jsResult; + } + return nullptr; +} +} // namespace AbilityRuntime +} // namespace OHOS diff --git a/frameworks/kits/extension/src/js_print_extension.cpp b/frameworks/kits/extension/src/js_print_extension.cpp index 84243a66..1180f541 100644 --- a/frameworks/kits/extension/src/js_print_extension.cpp +++ b/frameworks/kits/extension/src/js_print_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -16,6 +16,8 @@ #include "js_print_extension.h" #include "ability_info.h" +#include "iprint_extension_callback.h" +#include "js_print_callback.h" #include "js_print_extension_context.h" #include "js_runtime.h" #include "js_runtime_utils.h" @@ -23,11 +25,10 @@ #include "napi/native_node_api.h" #include "napi_common_util.h" #include "napi_common_want.h" +#include "napi_print_utils.h" #include "napi_remote_object.h" -#include "print_extcb_interface.h" #include "print_log.h" #include "print_manager_client.h" -#include "print_napi_utils.h" namespace OHOS { namespace AbilityRuntime { @@ -123,88 +124,7 @@ void JsPrintExtension::OnStart(const AAFwk::Want &want) NativeValue *argv[] = { nativeWant }; CallObjectMethod("onCreated", argv, ARGC_ONE); PRINT_HILOGD("%{public}s end.", __func__); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_START_DISCOVERY, []() -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - jsExtension_->CallObjectMethod("onStartDiscoverPrinter"); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_STOP_DISCOVERY, []() -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - jsExtension_->CallObjectMethod("onStopDiscoverPrinter"); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_CONNECT_PRINTER, [](uint32_t printId) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; - - jsExtension_->CallObjectMethod("onConnectPrinter", arg, ARGC_ONE); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback( - PRINT_EXTCB_DISCONNECT_PRINTER, [](uint32_t printId) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; - - jsExtension_->CallObjectMethod("onDisconnectPrinter", arg, ARGC_ONE); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_START_PRINT, [](const PrintJob &job) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; - - jsExtension_->CallObjectMethod("onConnectPrinter", arg, ARGC_ONE); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_CANCEL_PRINT, [](const PrintJob &job) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; - - jsExtension_->CallObjectMethod("onCancelPrintJob", arg, ARGC_ONE); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_REQUEST_CAP, [](uint32_t printId) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jsPrintId = - OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); - NativeValue *nativePrintId = reinterpret_cast(jsPrintId); - NativeValue *arg[] = { nativePrintId }; - jsExtension_->CallObjectMethod("onRequestPrinterCapability", arg, ARGC_ONE); - return true; - }); - PrintManagerClient::GetInstance()->RegisterExtCallback( - PRINT_EXTCB_REQUEST_PREVIEW, [](const PrintJob &job) -> bool { - PRINT_HILOGD("jsExtension_->CallObjectMethod"); - HandleScope handleScope(jsExtension_->jsRuntime_); - NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); - napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); - NativeValue *nativeJob = reinterpret_cast(jobObject); - NativeValue *arg[] = { nativeJob }; - - jsExtension_->CallObjectMethod("onRequestPreview", arg, ARGC_ONE); - return true; - }); + RegisterAllCallback(); } void JsPrintExtension::OnStop() @@ -217,6 +137,7 @@ void JsPrintExtension::OnStop() PRINT_HILOGD("The Print extension connection is not disconnected."); } PRINT_HILOGD("%{public}s end.", __func__); + RegisterAllCallback(); } sptr JsPrintExtension::OnConnect(const AAFwk::Want &want) @@ -348,5 +269,106 @@ void JsPrintExtension::GetSrcPath(std::string &srcPath) srcPath.append(".abc"); } } + +void JsPrintExtension::RegisterAllCallback() +{ + PRINT_HILOGD("Register Print Extension Callback"); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_START_DISCOVERY, []() -> bool { + PRINT_HILOGD("Start Print Discovery"); + HandleScope handleScope(jsExtension_->jsRuntime_); + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onStartDiscoverPrinter"); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_STOP_DISCOVERY, []() -> bool { + PRINT_HILOGD("Stop Print Discovery"); + HandleScope handleScope(jsExtension_->jsRuntime_); + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onStopDiscoverPrinter"); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_CONNECT_PRINTER, [](uint32_t printId) -> bool { + PRINT_HILOGD("Connect Printer"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jsPrintId = + OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); + NativeValue *nativePrintId = reinterpret_cast(jsPrintId); + NativeValue *arg[] = { nativePrintId }; + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onConnectPrinter", arg, ARGC_ONE); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback( + PRINT_EXTCB_DISCONNECT_PRINTER, [](uint32_t printId) -> bool { + PRINT_HILOGD("Disconnect Printer"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jsPrintId = + OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); + NativeValue *nativePrintId = reinterpret_cast(jsPrintId); + NativeValue *arg[] = { nativePrintId }; + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onDisconnectPrinter", arg, ARGC_ONE); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_START_PRINT, [](const PrintJob &job) -> bool { + PRINT_HILOGD("Start Print Job"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); + NativeValue *nativeJob = reinterpret_cast(jobObject); + NativeValue *arg[] = { nativeJob }; + + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onStartPrint", arg, ARGC_ONE); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_CANCEL_PRINT, [](const PrintJob &job) -> bool { + PRINT_HILOGD("Cancel Print Job"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); + NativeValue *nativeJob = reinterpret_cast(jobObject); + NativeValue *arg[] = { nativeJob }; + + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onCancelPrintJob", arg, ARGC_ONE); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback(PRINT_EXTCB_REQUEST_CAP, [](uint32_t printId) -> bool { + PRINT_HILOGD("Request Capability"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jsPrintId = + OHOS::AppExecFwk::WrapInt32ToJS(reinterpret_cast(nativeEng), static_cast(printId)); + NativeValue *nativePrintId = reinterpret_cast(jsPrintId); + NativeValue *arg[] = { nativePrintId }; + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onRequestPrinterCapability", arg, ARGC_ONE); + return true; + }); + PrintManagerClient::GetInstance()->RegisterExtCallback( + PRINT_EXTCB_REQUEST_PREVIEW, [](const PrintJob &job) -> bool { + PRINT_HILOGD("Requet preview"); + HandleScope handleScope(jsExtension_->jsRuntime_); + NativeEngine *nativeEng = &(jsExtension_->jsRuntime_).GetNativeEngine(); + napi_value jobObject = PrintNapiUtils::Convert2JsObj(reinterpret_cast(nativeEng), job); + NativeValue *nativeJob = reinterpret_cast(jobObject); + NativeValue *arg[] = { nativeJob }; + + auto callback = std::make_shared(jsExtension_->jsRuntime_); + NativeValue *value = jsExtension_->jsObj_->Get(); + callback->Exec(value, "onRequestPreview", arg, ARGC_ONE); + return true; + }); +} } // namespace AbilityRuntime } // namespace OHOS diff --git a/frameworks/kits/extension/src/js_print_extension_context.cpp b/frameworks/kits/extension/src/js_print_extension_context.cpp index f6375b66..8b8a43f7 100644 --- a/frameworks/kits/extension/src/js_print_extension_context.cpp +++ b/frameworks/kits/extension/src/js_print_extension_context.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/src/print_extension.cpp b/frameworks/kits/extension/src/print_extension.cpp index ebf8e262..1ade8d43 100644 --- a/frameworks/kits/extension/src/print_extension.cpp +++ b/frameworks/kits/extension/src/print_extension.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/src/print_extension_context.cpp b/frameworks/kits/extension/src/print_extension_context.cpp index ed25264d..044c584d 100644 --- a/frameworks/kits/extension/src/print_extension_context.cpp +++ b/frameworks/kits/extension/src/print_extension_context.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/frameworks/kits/extension/src/print_extension_module_loader.cpp b/frameworks/kits/extension/src/print_extension_module_loader.cpp index c64066d4..fae09280 100644 --- a/frameworks/kits/extension/src/print_extension_module_loader.cpp +++ b/frameworks/kits/extension/src/print_extension_module_loader.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/jskits/printextsample.js b/interfaces/kits/jskits/printextsample.js index b7e80efb..d1ead97d 100644 --- a/interfaces/kits/jskits/printextsample.js +++ b/interfaces/kits/jskits/printextsample.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/jskits/printsample.js b/interfaces/kits/jskits/printsample.js index 5e2139ca..7f9f57a5 100644 --- a/interfaces/kits/jskits/printsample.js +++ b/interfaces/kits/jskits/printsample.js @@ -1,5 +1,5 @@ /* - * Copyright (C) 2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/jsnapi/extension/BUILD.gn b/interfaces/kits/jsnapi/extension/BUILD.gn index 9fb875b9..35a07684 100644 --- a/interfaces/kits/jsnapi/extension/BUILD.gn +++ b/interfaces/kits/jsnapi/extension/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/interfaces/kits/jsnapi/extension/print_extension.js b/interfaces/kits/jsnapi/extension/print_extension.js index 3492528a..74ae33a0 100644 --- a/interfaces/kits/jsnapi/extension/print_extension.js +++ b/interfaces/kits/jsnapi/extension/print_extension.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/jsnapi/extension/print_extension_module.cpp b/interfaces/kits/jsnapi/extension/print_extension_module.cpp index 03a08e5a..0943ea33 100644 --- a/interfaces/kits/jsnapi/extension/print_extension_module.cpp +++ b/interfaces/kits/jsnapi/extension/print_extension_module.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,8 +20,7 @@ extern const char _binary_print_extension_js_end[]; extern const char _binary_print_extension_abc_start[]; extern const char _binary_print_extension_abc_end[]; -extern "C" __attribute__((constructor)) -void NAPI_PrintExtension_AutoRegister() +extern "C" __attribute__((constructor)) void NAPI_PrintExtension_AutoRegister() { auto moduleManager = NativeModuleManager::GetInstance(); NativeModule newModuleInfo = { @@ -32,8 +31,7 @@ void NAPI_PrintExtension_AutoRegister() moduleManager->Register(&newModuleInfo); } -extern "C" __attribute__((visibility("default"))) -void NAPI_PrintExtension_GetJSCode(const char **buf, int *bufLen) +extern "C" __attribute__((visibility("default"))) void NAPI_PrintExtension_GetJSCode(const char **buf, int *bufLen) { if (buf != nullptr) { *buf = _binary_print_extension_js_start; @@ -45,8 +43,7 @@ void NAPI_PrintExtension_GetJSCode(const char **buf, int *bufLen) } // print_extension JS register -extern "C" __attribute__((visibility("default"))) -void NAPI_PrintExtension_GetABCCode(const char **buf, int *buflen) +extern "C" __attribute__((visibility("default"))) void NAPI_PrintExtension_GetABCCode(const char **buf, int *buflen) { if (buf != nullptr) { *buf = _binary_print_extension_abc_start; diff --git a/interfaces/kits/jsnapi/extensionctx/BUILD.gn b/interfaces/kits/jsnapi/extensionctx/BUILD.gn index 6e6a73ef..cced39f0 100644 --- a/interfaces/kits/jsnapi/extensionctx/BUILD.gn +++ b/interfaces/kits/jsnapi/extensionctx/BUILD.gn @@ -1,4 +1,4 @@ -# Copyright (c) 2021 Huawei Device Co., Ltd. +# Copyright (c) 2022 Huawei Device Co., Ltd. # Licensed under the Apache License, Version 2.0 (the "License"); # you may not use this file except in compliance with the License. # You may obtain a copy of the License at diff --git a/interfaces/kits/jsnapi/extensionctx/print_extension_context.js b/interfaces/kits/jsnapi/extensionctx/print_extension_context.js index 489a6ff1..cfa67e31 100644 --- a/interfaces/kits/jsnapi/extensionctx/print_extension_context.js +++ b/interfaces/kits/jsnapi/extensionctx/print_extension_context.js @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at diff --git a/interfaces/kits/jsnapi/extensionctx/print_extension_context_module.cpp b/interfaces/kits/jsnapi/extensionctx/print_extension_context_module.cpp index 4aac6592..1ffc88c3 100644 --- a/interfaces/kits/jsnapi/extensionctx/print_extension_context_module.cpp +++ b/interfaces/kits/jsnapi/extensionctx/print_extension_context_module.cpp @@ -1,5 +1,5 @@ /* - * Copyright (c) 2021 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -20,8 +20,7 @@ extern const char _binary_print_extension_context_js_end[]; extern const char _binary_print_extension_context_abc_start[]; extern const char _binary_print_extension_context_abc_end[]; -extern "C" __attribute__((constructor)) -void NAPI_PrintExtensionContext_AutoRegister() +extern "C" __attribute__((constructor)) void NAPI_PrintExtensionContext_AutoRegister() { auto moduleManager = NativeModuleManager::GetInstance(); NativeModule newModuleInfo = { @@ -32,8 +31,8 @@ void NAPI_PrintExtensionContext_AutoRegister() moduleManager->Register(&newModuleInfo); } -extern "C" __attribute__((visibility("default"))) -void NAPI_PrintExtensionContext_GetJSCode(const char **buf, int *bufLen) +extern "C" __attribute__((visibility("default"))) void NAPI_PrintExtensionContext_GetJSCode( + const char **buf, int *bufLen) { if (buf != nullptr) { *buf = _binary_print_extension_context_js_start; @@ -45,8 +44,8 @@ void NAPI_PrintExtensionContext_GetJSCode(const char **buf, int *bufLen) } // print_extension_context JS register -extern "C" __attribute__((visibility("default"))) -void NAPI_PrintExtensionContext_GetABCCode(const char **buf, int *buflen) +extern "C" __attribute__((visibility("default"))) void NAPI_PrintExtensionContext_GetABCCode( + const char **buf, int *buflen) { if (buf != nullptr) { *buf = _binary_print_extension_context_abc_start; diff --git a/interfaces/kits/napi/BUILD.gn b/interfaces/kits/napi/BUILD.gn index a538d95c..caad8d1a 100644 --- a/interfaces/kits/napi/BUILD.gn +++ b/interfaces/kits/napi/BUILD.gn @@ -47,6 +47,7 @@ ohos_shared_library("print_napi") { "src/napi_print_ext.cpp", "src/async_call.cpp", "src/print_task.cpp", + "src/print_parse_type.cpp", ] deps = [ "//foundation/arkui/napi:ace_napi", diff --git a/interfaces/kits/napi/include/base_context.h b/interfaces/kits/napi/include/base_context.h index a576f44f..f20c0d71 100644 --- a/interfaces/kits/napi/include/base_context.h +++ b/interfaces/kits/napi/include/base_context.h @@ -1,5 +1,5 @@ /* - * Copyright (C) 2021-2022 Huawei Device Co., Ltd. + * Copyright (c) 2022 Huawei Device Co., Ltd. * Licensed under the Apache License, Version 2.0 (the "License"); * you may not use this file except in compliance with the License. * You may obtain a copy of the License at @@ -28,7 +28,6 @@ typedef void (*AsyncWorkExecutor)(napi_env env, void *data); typedef void (*AsyncWorkCallback)(napi_env env, napi_status status, void *data); -// support no argument class BaseContext { public: ACE_DISALLOW_COPY_AND_MOVE(BaseContext); diff --git a/interfaces/kits/napi/include/napi_inner_print.h b/interfaces/kits/napi/include/napi_inner_print.h index a6a19828..7923f552 100644 --- a/interfaces/kits/napi/include/napi_inner_print.h +++ b/interfaces/kits/napi/include/napi_inner_print.h @@ -20,12 +20,12 @@ #include #include "async_call.h" +#include "iprint_callback.h" #include "napi/native_api.h" #include "napi_print_ext.h" #include "noncopyable.h" #include "print_extension_info.h" #include "print_job.h" -#include "print_notify_stub.h" #include "print_page_size.h" #include "print_resolution.h" #include "print_task.h" @@ -50,18 +50,16 @@ public: static napi_value StopDiscovery(napi_env env, napi_callback_info info); static napi_value ConnectPrint(napi_env env, napi_callback_info info); static napi_value DisconnectPrint(napi_env env, napi_callback_info info); - static napi_value StartPrint(napi_env env, napi_callback_info info); - static napi_value CancelPrint(napi_env env, napi_callback_info info); + static napi_value StartPrintJob(napi_env env, napi_callback_info info); + static napi_value CancelPrintJob(napi_env env, napi_callback_info info); static napi_value RequestPreview(napi_env env, napi_callback_info info); static napi_value QueryCapability(napi_env env, napi_callback_info info); static napi_value On(napi_env env, napi_callback_info info); static napi_value Off(napi_env env, napi_callback_info info); static int32_t GetEventType(const std::string &type); - static bool ParseJob(napi_env env, napi_value jobValue, PrintJob &printJob); - static bool ParseJobParam(napi_env env, napi_value jobValue, PrintJob &printJob); private: - static sptr CreateNotify(napi_env env, const std::string &type, napi_ref callbackRef); + static sptr CreateNotify(napi_env env, const std::string &type, napi_ref callbackRef); struct OperationContext : public AsyncCall::Context { int32_t dummy = 0; diff --git a/interfaces/kits/napi/include/napi_print_ext.h b/interfaces/kits/napi/include/napi_print_ext.h index 5e3898e3..fe5d5852 100644 --- a/interfaces/kits/napi/include/napi_print_ext.h +++ b/interfaces/kits/napi/include/napi_print_ext.h @@ -33,16 +33,6 @@ public: static napi_value RemovePrinters(napi_env env, napi_callback_info info); static napi_value UpdatePrinterState(napi_env env, napi_callback_info info); static napi_value UpdatePrintJobState(napi_env env, napi_callback_info info); - static bool ParseInfo(napi_env env, napi_value InfoValue, PrinterInfo &Info); - static bool ParseInfoParam(napi_env env, napi_value InfoValue, PrinterInfo &info); - static bool ParseCapability(napi_env env, napi_value InfoValue, PrinterCapability &capability); - static bool ParseCapabilityParam(napi_env env, napi_value InfoValue, PrinterCapability &capability); - static bool ParsePageSize(napi_env env, napi_value capValue, PrintPageSize &PrintPageSize); - static bool ParsePageSizeParam(napi_env env, napi_value capValue, PrintPageSize &PrintPageSize); - static bool ParseResolution(napi_env env, napi_value reValue, PrintResolution &PrintResolution); - static bool ParseResolutionParam(napi_env env, napi_value reValue, PrintResolution &PrintResolution); - static bool ParseMargin(napi_env env, napi_value marginValue, PrintMargin &PrintMargin); - static bool ParseMarginParam(napi_env env, napi_value marginValue, PrintMargin &PrintMargin); private: struct NapiPrintExtContext : public AsyncCall::Context { diff --git a/interfaces/kits/napi/include/print_parse_type.h b/interfaces/kits/napi/include/print_parse_type.h new file mode 100644 index 00000000..bafb5615 --- /dev/null +++ b/interfaces/kits/napi/include/print_parse_type.h @@ -0,0 +1,57 @@ +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#ifndef PARSE_TYPE_H +#define PARSE_TYPE_H + +#include +#include + +#include "async_call.h" +#include "napi/native_api.h" +#include "noncopyable.h" +#include "print_job.h" +#include "print_page_size.h" +#include "print_resolution.h" +#include "print_task.h" +#include "printer_capability.h" +#include "printer_info.h" + +namespace OHOS::Print { +class ParseType { +public: + static bool ParseInfo(napi_env env, napi_value InfoValue, PrinterInfo &Info); + static bool ParseCapability(napi_env env, napi_value InfoValue, PrinterCapability &capability); + static bool ParsePageSize(napi_env env, napi_value capValue, PrintPageSize &PrintPageSize); + static bool ParseResolution(napi_env env, napi_value reValue, PrintResolution &PrintResolution); + static bool ParseMargin(napi_env env, napi_value marginValue, PrintMargin &PrintMargin); + static bool ParseJob(napi_env env, napi_value jobValue, PrintJob &printJob); + static bool ParsePreviewAttribute(napi_env env, napi_value preAttValue, PreviewAttribute &preAtt); + static bool ParseRange(napi_env env, napi_value preAttValue, PrintRange &range); + + static bool ParseInfoParam(napi_env env, napi_value InfoValue, PrinterInfo &info); + static bool ParseCapabilityParam(napi_env env, napi_value InfoValue, PrinterCapability &capability); + static bool ParsePageSizeParam(napi_env env, napi_value capValue, PrintPageSize &PrintPageSize); + static bool ParseResolutionParam(napi_env env, napi_value reValue, PrintResolution &PrintResolution); + static bool ParseMarginParam(napi_env env, napi_value marginValue, PrintMargin &PrintMargin); + static bool ParseJobParam(napi_env env, napi_value jobValue, PrintJob &printJob); + static bool ParsePreviewAttributeParam(napi_env env, napi_value preAttValue, PreviewAttribute &preAtt); + static bool ParseRangeParam(napi_env env, napi_value preAttValue, PrintRange &range); + +private: + struct ParseTypeContext : public AsyncCall::Context {}; +}; +} // namespace OHOS::Print +#endif // PARSE_TYPE_H \ No newline at end of file diff --git a/interfaces/kits/napi/include/print_task.h b/interfaces/kits/napi/include/print_task.h index 5c203ce8..c542e060 100644 --- a/interfaces/kits/napi/include/print_task.h +++ b/interfaces/kits/napi/include/print_task.h @@ -53,10 +53,6 @@ public: static napi_value On(napi_env env, napi_callback_info info); static napi_value Off(napi_env env, napi_callback_info info); - // bool AddListener(const std::string &type, sptr listener); - // void RemoveListener(const std::string &type, sptr listener); - // void RemoveListener(const std::string &type); - bool IsSupportType(const std::string &type); bool IsSupportPrinterStateType(const std::string &type); bool IsSupportJobStateType(const std::string &type); @@ -64,7 +60,6 @@ public: private: int taskId_; std::mutex mutex_; - // std::map> listenerMap_; std::map supportEvents_; std::map supportPrinterState_; std::map supportJobState_; diff --git a/interfaces/kits/napi/src/async_call.cpp b/interfaces/kits/napi/src/async_call.cpp index 9568f460..586c13ef 100644 --- a/interfaces/kits/napi/src/async_call.cpp +++ b/interfaces/kits/napi/src/async_call.cpp @@ -15,8 +15,8 @@ #include "async_call.h" +#include "napi_print_utils.h" #include "print_log.h" -#include "print_napi_utils.h" namespace OHOS::Print { AsyncCall::AsyncCall(napi_env env, napi_callback_info info, std::shared_ptr context, size_t pos) : env_(env) diff --git a/interfaces/kits/napi/src/napi_inner_print.cpp b/interfaces/kits/napi/src/napi_inner_print.cpp index 28f70014..7da16bb7 100644 --- a/interfaces/kits/napi/src/napi_inner_print.cpp +++ b/interfaces/kits/napi/src/napi_inner_print.cpp @@ -16,24 +16,22 @@ #include "napi_inner_print.h" #include "async_call.h" +#include "napi_print_utils.h" #include "print_log.h" #include "print_manager_client.h" -#include "print_napi_utils.h" +#include "print_parse_type.h" #include "print_task.h" -static constexpr const char *PARAM_JOB_FILES = "files"; -static constexpr const char *PARAM_JOB_JOBID = "jobId"; -static constexpr const char *PARAM_JOB_PRINTERID = "printerId"; -static constexpr const char *PARAM_JOB_JOBSTATE = "jobState"; -static constexpr const char *PARAM_JOB_COPYNUMBER = "copyNumber"; -// static constexpr const char *PARAM_JOB_PAGERANGE = "pageRange"; - namespace OHOS::Print { napi_value NapiInnerPrint::QueryExtensionInfo(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -92,8 +90,12 @@ napi_value NapiInnerPrint::QueryExtensionInfo(napi_env env, napi_callback_info i napi_value NapiInnerPrint::StartDiscovery(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter StartDiscovery---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -122,10 +124,6 @@ napi_value NapiInnerPrint::StartDiscovery(napi_env env, napi_callback_info info) return status; }; auto exec = [context](AsyncCall::Context *ctx) { - /*context->extensionList.push_back(5); - context->extensionList.push_back(55); - context->extensionList.push_back(555); - context->extensionList.push_back(5555);*/ context->result = PrintManagerClient::GetInstance()->StartDiscoverPrinter(context->extensionVector); if (context->result == true) { context->status = napi_ok; @@ -139,8 +137,12 @@ napi_value NapiInnerPrint::StartDiscovery(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::StopDiscovery(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter StopDiscovery---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -168,8 +170,12 @@ napi_value NapiInnerPrint::StopDiscovery(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::ConnectPrint(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ConnectPrint---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -204,8 +210,12 @@ napi_value NapiInnerPrint::ConnectPrint(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::DisconnectPrint(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter DisconnectPrint---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -237,11 +247,15 @@ napi_value NapiInnerPrint::DisconnectPrint(napi_env env, napi_callback_info info return asyncCall.Call(env, exec); } -napi_value NapiInnerPrint::StartPrint(napi_env env, napi_callback_info info) +napi_value NapiInnerPrint::StartPrintJob(napi_env env, napi_callback_info info) { - PRINT_HILOGD("Enter StartPrint---->"); + PRINT_HILOGD("Enter StartPrintJob---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -250,8 +264,8 @@ napi_value NapiInnerPrint::StartPrint(napi_env env, napi_callback_info info) NAPI_ASSERT_BASE(env, argc == 1, " should 1 parameter!", napi_invalid_arg); napi_value value = argv[PrintNapiUtils::FIRST_ARGV]; PrintJob printJob; - if (!ParseJob(env, value, printJob)) { - PRINT_HILOGD("PrintResolution type error!"); + if (!ParseType::ParseJob(env, value, printJob)) { + PRINT_HILOGD("ParseJob type error!"); } context->printStartJob = printJob; return napi_ok; @@ -262,10 +276,6 @@ napi_value NapiInnerPrint::StartPrint(napi_env env, napi_callback_info info) return status; }; auto exec = [context](AsyncCall::Context *ctx) { - PrintRange range; - range.SetStartPage(1); - range.SetEndPage(2); - context->printStartJob.SetPageRange(range); context->result = PrintManagerClient::GetInstance()->StartPrintJob(context->printStartJob); if (context->result == true) { context->status = napi_ok; @@ -276,11 +286,15 @@ napi_value NapiInnerPrint::StartPrint(napi_env env, napi_callback_info info) return asyncCall.Call(env, exec); } -napi_value NapiInnerPrint::CancelPrint(napi_env env, napi_callback_info info) +napi_value NapiInnerPrint::CancelPrintJob(napi_env env, napi_callback_info info) { - PRINT_HILOGD("Enter ---->"); + PRINT_HILOGD("Enter CancelPrintJob---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -289,8 +303,8 @@ napi_value NapiInnerPrint::CancelPrint(napi_env env, napi_callback_info info) NAPI_ASSERT_BASE(env, argc == 1, " should 1 parameter!", napi_invalid_arg); napi_value value = argv[PrintNapiUtils::FIRST_ARGV]; PrintJob printJob; - if (!ParseJob(env, value, printJob)) { - PRINT_HILOGD("PrintResolution type error!"); + if (!ParseType::ParseJob(env, value, printJob)) { + PRINT_HILOGD("ParseJob type error!"); } context->printCancelJob = printJob; return napi_ok; @@ -301,10 +315,6 @@ napi_value NapiInnerPrint::CancelPrint(napi_env env, napi_callback_info info) return status; }; auto exec = [context](AsyncCall::Context *ctx) { - PrintRange range; - range.SetStartPage(1); - range.SetEndPage(2); - context->printCancelJob.SetPageRange(range); context->result = PrintManagerClient::GetInstance()->CancelPrintJob(context->printCancelJob); if (context->result == true) { context->status = napi_ok; @@ -318,8 +328,12 @@ napi_value NapiInnerPrint::CancelPrint(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -328,8 +342,8 @@ napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info) NAPI_ASSERT_BASE(env, argc == 1, " should 1 parameter!", napi_invalid_arg); napi_value value = argv[PrintNapiUtils::FIRST_ARGV]; PrintJob printJob; - if (!ParseJob(env, value, printJob)) { - PRINT_HILOGD("PrintResolution type error!"); + if (!ParseType::ParseJob(env, value, printJob)) { + PRINT_HILOGD("ParseJob type error!"); } context->printReqPreviewJob = printJob; return napi_ok; @@ -340,10 +354,6 @@ napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info) return status; }; auto exec = [context](AsyncCall::Context *ctx) { - PrintRange range; - range.SetStartPage(1); - range.SetEndPage(2); - context->printReqPreviewJob.SetPageRange(range); context->result = PrintManagerClient::GetInstance()->RequestPreview(context->printReqPreviewJob, context->previewResult); if (context->result == true) { @@ -358,8 +368,12 @@ napi_value NapiInnerPrint::RequestPreview(napi_env env, napi_callback_info info) napi_value NapiInnerPrint::QueryCapability(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter QueryCapability---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -377,59 +391,7 @@ napi_value NapiInnerPrint::QueryCapability(napi_env env, napi_callback_info info return napi_ok; }; auto output = [context](napi_env env, napi_value *result) -> napi_status { - PRINT_HILOGD("ouput enter---->"); - napi_value resultPrintMargin; - napi_create_object(env, result); - napi_create_object(env, &resultPrintMargin); - napi_value arrPageSize, arrResolution; - napi_status status = napi_create_array(env, &arrPageSize); - status = napi_create_array(env, &arrResolution); - - PrintNapiUtils::SetUint32Property(env, *result, "colorMode", context->printerCapability.GetColorMode()); - PrintNapiUtils::SetUint32Property(env, *result, "duplexMode", context->printerCapability.GetDuplexMode()); - - PrintMargin margin; - context->printerCapability.GetMinMargin(margin); - PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "top", margin.GetTop()); - PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "bottom", margin.GetBottom()); - PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "left", margin.GetLeft()); - PrintNapiUtils::SetUint32Property(env, resultPrintMargin, "right", margin.GetRight()); - - std::vector pageSize; - context->printerCapability.GetPageSize(pageSize); - uint32_t printerCapabilityLength = pageSize.size(); - - for (uint32_t i = 0; i < printerCapabilityLength; i++) { - napi_value resultPrinterPageSize; - napi_create_object(env, &resultPrinterPageSize); - PrintNapiUtils::SetUint32Property(env, resultPrinterPageSize, "id", pageSize[i].GetId()); - PrintNapiUtils::SetStringPropertyUtf8(env, resultPrinterPageSize, "name", pageSize[i].GetName().c_str()); - PrintNapiUtils::SetUint32Property(env, resultPrinterPageSize, "width", pageSize[i].GetWidth()); - PrintNapiUtils::SetUint32Property(env, resultPrinterPageSize, "height", pageSize[i].GetHeight()); - status = napi_set_element(env, arrPageSize, i, resultPrinterPageSize); - } - - std::vector resolutionList; - context->printerCapability.GetResolution(resolutionList); - - uint32_t printerCapabilityresolutionLength = resolutionList.size(); - - for (uint32_t i = 0; i < printerCapabilityresolutionLength; i++) { - napi_value resultPrinterResolution; - napi_create_object(env, &resultPrinterResolution); - PrintNapiUtils::SetUint32Property(env, resultPrinterResolution, "id", resolutionList[i].GetId()); - PrintNapiUtils::SetUint32Property( - env, resultPrinterResolution, "horizontalDpi", resolutionList[i].GetHorizontalDpi()); - PrintNapiUtils::SetUint32Property( - env, resultPrinterResolution, "verticalDpi", resolutionList[i].GetVerticalDpi()); - status = napi_set_element(env, arrResolution, i, resultPrinterResolution); - } - - status = napi_set_named_property(env, *result, "minMargin", resultPrintMargin); - PRINT_HILOGD("output ---- status[%{public}d]", status); - napi_set_named_property(env, *result, "pageSize", arrPageSize); - napi_set_named_property(env, *result, "resolution", arrResolution); - PRINT_HILOGD("ouput over---->"); + context->printerCapability.ConvertToJs(env, result); return napi_ok; }; auto exec = [context](AsyncCall::Context *ctx) { @@ -447,8 +409,12 @@ napi_value NapiInnerPrint::QueryCapability(napi_env env, napi_callback_info info napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -487,21 +453,18 @@ napi_value NapiInnerPrint::On(napi_env env, napi_callback_info info) PRINT_HILOGD("state judge end."); napi_ref callbackRef = nullptr; napi_create_reference(env, argv[argc - 1], 1, &callbackRef); - - // sptr listener = CreateNotify(env, type, callbackRef); - // if (listener == nullptr) { - // PRINT_HILOGD("PrintPause create callback object fail"); - // return result; - // } - // return result; return nullptr; } napi_value NapiInnerPrint::Off(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter Off---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -544,21 +507,18 @@ int32_t NapiInnerPrint::GetEventType(const std::string &type) return NO_ARG_EVENT; } -sptr NapiInnerPrint::CreateNotify(napi_env env, const std::string &type, napi_ref callbackRef) +sptr NapiInnerPrint::CreateNotify(napi_env env, const std::string &type, napi_ref callbackRef) { - sptr listener = nullptr; + sptr listener = nullptr; int32_t eventType = GetEventType(type); switch (eventType) { case NO_ARG_EVENT: - // listener = new PrintBaseNotify(env, type, callbackRef); break; case ONE_ARG_EVENT: - // listener = new PrintPrinterStateNotify(env, type, callbackRef); break; case TWO_ARG_EVENT: - // listener = new PrintPrintJobNotify(env, type, callbackRef); break; default: @@ -567,72 +527,4 @@ sptr NapiInnerPrint::CreateNotify(napi_env env, const std: } return listener; } - -bool NapiInnerPrint::ParseJob(napi_env env, napi_value jobValue, PrintJob &printJob) -{ - if (!ParseJobParam(env, jobValue, printJob)) { - PRINT_HILOGD("ParseJobParam is error!"); - return false; - } - printJob.SetJobId(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_JOBID)); - printJob.SetPrintId(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_PRINTERID)); - printJob.SetJobState(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_JOBSTATE)); - printJob.SetCopyNumber(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_COPYNUMBER)); - // printJob.SetPageRange(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_PAGERANGE)); - - PRINT_HILOGD("printJob_value GetJobId value is %{public}d", printJob.GetJobId()); - PRINT_HILOGD("printJob_value GetPrinterId value is %{public}d", printJob.GetPrinterId()); - PRINT_HILOGD("printJob_value GetJobState value is %{public}d", printJob.GetJobState()); - PRINT_HILOGD("printJob_value GetCopyNumber value is %{public}d", printJob.GetCopyNumber()); - // PRINT_HILOGD("printJob_value GetPageRange value is %{public}d", printJob.GetPageRange()); - return true; -} - -bool NapiInnerPrint::ParseJobParam(napi_env env, napi_value jobValue, PrintJob &printJob) -{ - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_FILES); - bool isFileArray = false; - napi_is_array(env, param_one, &isFileArray); - if (!isFileArray) { - PRINT_HILOGD("PrinterJobFile type error!"); - return false; - } - std::vector printfiles; - uint32_t arrayReLength = 0; - napi_get_array_length(env, param_one, &arrayReLength); - for (uint32_t i = 0; i < arrayReLength; i++) { - napi_value filesValue; - napi_get_element(env, param_one, i, &filesValue); - std::string files = PrintNapiUtils::GetStringFromValueUtf8(env, filesValue); - PRINT_HILOGD("printJob_value param_one %{public}s", files.c_str()); - printfiles.push_back(files); - } - - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_JOBID); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { - PRINT_HILOGD("error param_two"); - return false; - } - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PRINTERID); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { - PRINT_HILOGD("error param_three"); - return false; - } - napi_value param_four = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_JOBSTATE); - if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { - PRINT_HILOGD("error param_four"); - return false; - } - napi_value param_five = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_COPYNUMBER); - if (PrintNapiUtils::GetValueType(env, param_five) != napi_number) { - PRINT_HILOGD("error param_five"); - return false; - } - // napi_value param_six = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PAGERANGE); - // if (PrintNapiUtils::GetValueType(env, param_six) != napi_number) { - // PRINT_HILOGD("error param_six"); - // return false; - // } - return true; -} } // namespace OHOS::Print diff --git a/interfaces/kits/napi/src/napi_print_ext.cpp b/interfaces/kits/napi/src/napi_print_ext.cpp index 8f3478c1..dacfb2fd 100644 --- a/interfaces/kits/napi/src/napi_print_ext.cpp +++ b/interfaces/kits/napi/src/napi_print_ext.cpp @@ -16,43 +16,21 @@ #include "napi_print_ext.h" #include "async_call.h" +#include "napi_print_utils.h" #include "print_log.h" #include "print_manager_client.h" -#include "print_napi_utils.h" - -static constexpr const char *PARAM_INFO_PRINTID = "printerId"; -static constexpr const char *PARAM_INFO_PRINTERNAME = "printerName"; -static constexpr const char *PARAM_INFO_PRINTERICON = "printerIcon"; -static constexpr const char *PARAM_INFO_PRINTERSTATE = "printerState"; -static constexpr const char *PARAM_INFO_DESCRIPTION = "description"; -static constexpr const char *PARAM_INFO_CAPABILITY = "capability"; - -static constexpr const char *PARAM_CAPABILITY_MINMARGIN = "minMargin"; -static constexpr const char *PARAM_CAPABILITY_PAGESIZE = "pageSize"; -static constexpr const char *PARAM_CAPABILITY_RESOLUTION = "resolution"; -static constexpr const char *PARAM_CAPABILITY_COLORMODE = "colorMode"; -static constexpr const char *PARAM_CAPABILITY_DUPLEXMODE = "duplexMode"; - -static constexpr const char *PARAM_PAGESIZE_ID = "id"; -static constexpr const char *PARAM_PAGESIZE_NAME = "name"; -static constexpr const char *PARAM_PAGESIZE_WIDTH = "width"; -static constexpr const char *PARAM_PAGESIZE_HEIGHT = "height"; - -static constexpr const char *PARAM_RESOLUTION_ID = "id"; -static constexpr const char *PARAM_RESOLUTION_HORIZONTALDPI = "horizontalDpi"; -static constexpr const char *PARAM_RESOLUTION_VERTICALDPI = "verticalDpi"; - -static constexpr const char *PARAM_MARGIN_TOP = "top"; -static constexpr const char *PARAM_MARGIN_BOTTOM = "bottom"; -static constexpr const char *PARAM_MARGIN_LEFT = "left"; -static constexpr const char *PARAM_MARGIN_RIGHT = "right"; +#include "print_parse_type.h" namespace OHOS::Print { napi_value NapiPrintExt::AddPrinters(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -71,7 +49,7 @@ napi_value NapiPrintExt::AddPrinters(napi_env env, napi_callback_info info) for (uint32_t i = 0; i < result_size; i++) { napi_value value; napi_get_element(env, array, i, &value); - if (!ParseInfo(env, value, info_)) { + if (!ParseType::ParseInfo(env, value, info_)) { PRINT_HILOGD("PrinterInfo is format error!"); } context->printAddInfoVector.push_back(info_); @@ -97,6 +75,15 @@ napi_value NapiPrintExt::AddPrinters(napi_env env, napi_callback_info info) napi_value NapiPrintExt::RemovePrinters(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } + if (!PrintManagerClient::GetInstance()->CheckPermission()) { + PRINT_HILOGW("no permission to access print service"); + return nullptr; + } + auto context = std::make_shared(); auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status { NAPI_ASSERT_BASE(env, argc == PrintNapiUtils::ONE_ARG, " should 1 parameter!", napi_invalid_arg); @@ -112,7 +99,7 @@ napi_value NapiPrintExt::RemovePrinters(napi_env env, napi_callback_info info) for (uint32_t i = 0; i < result_size; i++) { napi_value value; napi_get_element(env, array, i, &value); - if (!ParseInfo(env, value, info_)) { + if (!ParseType::ParseInfo(env, value, info_)) { PRINT_HILOGD("PrinterInfo is format error!"); } context->printRemoveInfoVector.push_back(info_); @@ -137,8 +124,12 @@ napi_value NapiPrintExt::RemovePrinters(napi_env env, napi_callback_info info) napi_value NapiPrintExt::UpdatePrinterState(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -180,8 +171,12 @@ napi_value NapiPrintExt::UpdatePrinterState(napi_env env, napi_callback_info inf napi_value NapiPrintExt::UpdatePrintJobState(napi_env env, napi_callback_info info) { PRINT_HILOGD("Enter ---->"); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } if (!PrintManagerClient::GetInstance()->CheckPermission()) { - PRINT_HILOGD("no permission to access print service"); + PRINT_HILOGW("no permission to access print service"); return nullptr; } @@ -220,315 +215,4 @@ napi_value NapiPrintExt::UpdatePrintJobState(napi_env env, napi_callback_info in AsyncCall asyncCall(env, info, std::dynamic_pointer_cast(context)); return asyncCall.Call(env, exec); } - -bool NapiPrintExt::ParseInfo(napi_env env, napi_value InfoValue, PrinterInfo &info) -{ - if (!ParseInfoParam(env, InfoValue, info)) { - PRINT_HILOGD("ParseInfoParam is error!"); - return false; - } - info.SetPrinterName(PrintNapiUtils::GetStringPropertyUtf8(env, InfoValue, PARAM_INFO_PRINTERNAME)); - info.SetPrinterIcon(PrintNapiUtils::GetUint32Property(env, InfoValue, PARAM_INFO_PRINTERICON)); - info.SetPrinterState(PrintNapiUtils::GetUint32Property(env, InfoValue, PARAM_INFO_PRINTERSTATE)); - info.SetDescription(PrintNapiUtils::GetStringPropertyUtf8(env, InfoValue, PARAM_INFO_DESCRIPTION)); - - PRINT_HILOGD("info_value printId value is: %{public}d", info.GetPrintId()); - PRINT_HILOGD("info_value printerName value is %{public}s", info.GetPrinterName().c_str()); - PRINT_HILOGD("info_value printerIcon value is %{public}d", info.GetPrinterIcon()); - PRINT_HILOGD("info_value printerState value is %{public}d", info.GetPrinterState()); - PRINT_HILOGD("info_value description value is %{public}s", info.GetDescription().c_str()); - return true; -} - -bool NapiPrintExt::ParseInfoParam(napi_env env, napi_value InfoValue, PrinterInfo &info) -{ - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTID); - if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { - PRINT_HILOGD("error param_one"); - return false; - } - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERNAME); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { - PRINT_HILOGD("error param_two"); - return false; - } - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERICON); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { - PRINT_HILOGD("error param_three"); - return false; - } - napi_value param_four = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERSTATE); - if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { - PRINT_HILOGD("error param_four"); - return false; - } - napi_value param_five = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_DESCRIPTION); - if (PrintNapiUtils::GetValueType(env, param_five) != napi_string) { - PRINT_HILOGD("error param_five"); - return false; - } - napi_value param_six = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_CAPABILITY); - if (PrintNapiUtils::GetValueType(env, param_six) != napi_object) { - PRINT_HILOGD("error param_six"); - return false; - } else { - PrinterCapability capability; - if (!ParseCapability(env, param_six, capability)) { - PRINT_HILOGD("ParseCapability type error!"); - } - info.SetCapability(capability); - } - auto names = PrintNapiUtils::GetPropertyNames(env, InfoValue); - - std::vector::iterator iter; - PRINT_HILOGD("current name list size = %{public}zu", names.size()); - for (iter = names.begin(); iter != names.end(); ++iter) { - uint32_t value = PrintNapiUtils::GetUint32Property(env, param_one, *iter); - if (value) { - info.SetPrinterId(value); - } - } - return true; -} -bool NapiPrintExt::ParseCapability(napi_env env, napi_value capValue, PrinterCapability &capability) -{ - if (!ParseCapabilityParam(env, capValue, capability)) { - PRINT_HILOGD("ParseCapabilityParam is error!"); - return false; - } - // capability.SetMinMargin(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_CAPABILITY_MINMARGIN)); - capability.SetColorMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_COLORMODE)); - capability.SetDuplexMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_DUPLEXMODE)); - PRINT_HILOGD("capability_value colorMode value is %{public}d", capability.GetColorMode()); - PRINT_HILOGD("capability_value duplexMode value is %{public}d", capability.GetDuplexMode()); - return true; -} -bool NapiPrintExt::ParseCapabilityParam(napi_env env, napi_value capValue, PrinterCapability &capability) -{ - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_MINMARGIN); - if (PrintNapiUtils::GetValueType(env, param_one) != napi_object) { - PRINT_HILOGD("error param_one"); - return false; - } else { - PrintMargin margin; - if (!ParseMargin(env, param_one, margin)) { - PRINT_HILOGD("ParseCapability type error!"); - } - capability.SetMinMargin(margin); - } - - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_PAGESIZE); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_object) { - PRINT_HILOGD("error param_two"); - return false; - } else { - bool isArray = false; - napi_is_array(env, param_two, &isArray); - if (!isArray) { - PRINT_HILOGD("ParsePageSize type error!"); - return false; - } - - std::vector pageSizeList; - uint32_t arrayLength = 0; - napi_get_array_length(env, param_two, &arrayLength); - for (uint32_t i = 0; i < arrayLength; i++) { - napi_value pageSizeValue; - PrintPageSize pageSize; - napi_get_element(env, param_two, i, &pageSizeValue); - // status = napi_get_property(env, result, param_six, &pageResult); - if (!ParsePageSize(env, pageSizeValue, pageSize)) { - PRINT_HILOGD("ParsePageSize type error!"); - return false; - } - pageSizeList.push_back(pageSize); - } - capability.SetPageSize(pageSizeList); - } - - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_RESOLUTION); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_object) { - PRINT_HILOGD("error param_three"); - return false; - } else { - bool isReArray = false; - napi_is_array(env, param_three, &isReArray); - if (!isReArray) { - PRINT_HILOGD("PrintResolution type error!"); - return false; - } - std::vector resolutionList; - uint32_t arrayReLength = 0; - napi_get_array_length(env, param_three, &arrayReLength); - for (uint32_t i = 0; i < arrayReLength; i++) { - napi_value reValue; - PrintResolution resolution; - napi_get_element(env, param_three, i, &reValue); - if (!ParseResolution(env, reValue, resolution)) { - PRINT_HILOGD("PrintResolution type error!"); - return false; - } - resolutionList.push_back(resolution); - } - capability.SetResolution(resolutionList); - } - napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_COLORMODE); - if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { - PRINT_HILOGD("error param_four"); - return false; - } - napi_value param_five = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_DUPLEXMODE); - if (PrintNapiUtils::GetValueType(env, param_five) != napi_number) { - // ParseCapabilityParam() - PRINT_HILOGD("error param_five"); - return false; - } - auto names = PrintNapiUtils::GetPropertyNames(env, capValue); - - std::vector::iterator iter; - PRINT_HILOGD("current name list size = %{public}zu", names.size()); - for (iter = names.begin(); iter != names.end(); ++iter) { - uint32_t value = PrintNapiUtils::GetUint32Property(env, param_four, *iter); - if (value) { - capability.SetColorMode(value); - } - } - return true; -} - -bool NapiPrintExt::ParsePageSize(napi_env env, napi_value capValue, PrintPageSize &pageSize) -{ - if (!ParsePageSizeParam(env, capValue, pageSize)) { - PRINT_HILOGD("ParsePageSizeParam is error!"); - return false; - } - pageSize.SetName(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_PAGESIZE_NAME)); - pageSize.SetWidth(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_WIDTH)); - pageSize.SetHeight(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_HEIGHT)); - - PRINT_HILOGD("printerPageSize_value GetId value is %{public}d", pageSize.GetId()); - PRINT_HILOGD("printerPageSize_value GetName value is %{public}s", pageSize.GetName().c_str()); - PRINT_HILOGD("printerPageSize_value GetWidth value is %{public}d", pageSize.GetWidth()); - PRINT_HILOGD("printerPageSize_value GetHeight value is %{public}d", pageSize.GetHeight()); - return true; -} - -bool NapiPrintExt::ParsePageSizeParam(napi_env env, napi_value capValue, PrintPageSize &pageSize) -{ - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_ID); - if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { - PRINT_HILOGD("error param_one"); - return false; - } - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_NAME); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { - PRINT_HILOGD("error param_two"); - return false; - } - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_WIDTH); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { - PRINT_HILOGD("error param_three"); - return false; - } - napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_HEIGHT); - if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { - PRINT_HILOGD("error param_four"); - return false; - } - auto names = PrintNapiUtils::GetPropertyNames(env, capValue); - - std::vector::iterator iter; - PRINT_HILOGD("current name list size = %{public}zu", names.size()); - for (iter = names.begin(); iter != names.end(); ++iter) { - uint32_t value = PrintNapiUtils::GetUint32Property(env, param_four, *iter); - if (value) { - pageSize.SetId(value); - } - } - return true; -} -bool NapiPrintExt::ParseResolution(napi_env env, napi_value reValue, PrintResolution &resolution) -{ - if (!ParseResolutionParam(env, reValue, resolution)) { - PRINT_HILOGD("ParseResolutionParam is error!"); - return false; - } - resolution.SetId(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_ID)); - resolution.SetHorizontalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI)); - resolution.SetVerticalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_VERTICALDPI)); - - PRINT_HILOGD("printerResolution_value GetId value is %{public}d", resolution.GetId()); - PRINT_HILOGD("printerResolution_value GetHorizontalDpi value is %{public}d", resolution.GetHorizontalDpi()); - PRINT_HILOGD("printerResolution_value GetVerticalDpi value is %{public}d", resolution.GetVerticalDpi()); - return true; -} - -bool NapiPrintExt::ParseResolutionParam(napi_env env, napi_value reValue, PrintResolution &resolution) -{ - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_ID); - if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { - PRINT_HILOGD("error param_one"); - return false; - } - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { - PRINT_HILOGD("error param_two"); - return false; - } - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_VERTICALDPI); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { - PRINT_HILOGD("error param_three"); - return false; - } - auto names = PrintNapiUtils::GetPropertyNames(env, reValue); - PRINT_HILOGD("current resolution paramster name list size = %{public}zu", names.size()); - return true; -} - -bool NapiPrintExt::ParseMargin(napi_env env, napi_value marginValue, PrintMargin &margin) -{ - if (!ParseMarginParam(env, marginValue, margin)) { - PRINT_HILOGD("ParseResolutionParam is error!"); - return false; - } - margin.SetTop(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_TOP)); - margin.SetBottom(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_BOTTOM)); - margin.SetLeft(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_LEFT)); - margin.SetRight(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_RIGHT)); - - PRINT_HILOGD("printerMargin_value GetTop value is %{public}d", margin.GetTop()); - PRINT_HILOGD("printerMargin_value GetBottom value is %{public}d", margin.GetBottom()); - PRINT_HILOGD("printerMargin_value GetLeft value is %{public}d", margin.GetLeft()); - PRINT_HILOGD("printerMargin_value GetRight value is %{public}d", margin.GetRight()); - - return true; -} - -bool NapiPrintExt::ParseMarginParam(napi_env env, napi_value marginValue, PrintMargin &margin) -{ - //±ØÒªÊôÐÔУÑé - napi_value param_one = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_TOP); - if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { - PRINT_HILOGD("error param_one"); - return false; - } - napi_value param_two = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_BOTTOM); - if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { - PRINT_HILOGD("error param_two"); - return false; - } - napi_value param_three = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_LEFT); - if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { - PRINT_HILOGD("error param_three"); - return false; - } - napi_value param_four = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_RIGHT); - if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { - PRINT_HILOGD("error param_four"); - return false; - } - auto names = PrintNapiUtils::GetPropertyNames(env, marginValue); - PRINT_HILOGD("current margin paramster name list size = %{public}zu", names.size()); - return true; -} } // namespace OHOS::Print diff --git a/interfaces/kits/napi/src/napi_print_task.cpp b/interfaces/kits/napi/src/napi_print_task.cpp index df46c696..bc91e95f 100644 --- a/interfaces/kits/napi/src/napi_print_task.cpp +++ b/interfaces/kits/napi/src/napi_print_task.cpp @@ -18,9 +18,9 @@ #include #include "async_call.h" +#include "napi_print_utils.h" #include "print_log.h" #include "print_manager_client.h" -#include "print_napi_utils.h" #include "print_task.h" static constexpr const char *FUNCTION_ON = "on"; @@ -32,6 +32,10 @@ std::mutex mutex_; napi_value NapiPrintTask::Print(napi_env env, napi_callback_info info) // PrintTask { PRINT_HILOGD("Enter print JsMain."); + if (!PrintManagerClient::GetInstance()->LoadServer()) { + PRINT_HILOGE("load print server fail"); + return nullptr; + } struct ContextInfo { napi_ref ref = nullptr; }; diff --git a/interfaces/kits/napi/src/print_module.cpp b/interfaces/kits/napi/src/print_module.cpp index 852cfee7..93165f66 100644 --- a/interfaces/kits/napi/src/print_module.cpp +++ b/interfaces/kits/napi/src/print_module.cpp @@ -54,8 +54,8 @@ static napi_value Init(napi_env env, napi_value exports) DECLARE_NAPI_METHOD(FUNCTION_STOP_DISCOVERY, NapiInnerPrint::StopDiscovery), DECLARE_NAPI_METHOD(FUNCTION_CONNECT_PRINT, NapiInnerPrint::ConnectPrint), DECLARE_NAPI_METHOD(FUNCTION_DISCONNECT_PRINT, NapiInnerPrint::DisconnectPrint), - DECLARE_NAPI_METHOD(FUNCTION_START_PRINT, NapiInnerPrint::StartPrint), - DECLARE_NAPI_METHOD(FUNCTION_CANCEL_PRINT, NapiInnerPrint::CancelPrint), + DECLARE_NAPI_METHOD(FUNCTION_START_PRINT, NapiInnerPrint::StartPrintJob), + DECLARE_NAPI_METHOD(FUNCTION_CANCEL_PRINT, NapiInnerPrint::CancelPrintJob), DECLARE_NAPI_METHOD(FUNCTION_REQUEST_PREVIEW, NapiInnerPrint::RequestPreview), DECLARE_NAPI_METHOD(FUNCTION_QUERY_CAPABILITY, NapiInnerPrint::QueryCapability), DECLARE_NAPI_METHOD(FUNCTION_REGISTER_EVENT, NapiInnerPrint::On), diff --git a/interfaces/kits/napi/src/print_parse_type.cpp b/interfaces/kits/napi/src/print_parse_type.cpp new file mode 100644 index 00000000..0895d999 --- /dev/null +++ b/interfaces/kits/napi/src/print_parse_type.cpp @@ -0,0 +1,616 @@ + +/* + * Copyright (c) 2022 Huawei Device Co., Ltd. + * Licensed under the Apache License, Version 2.0 (the "License"); + * you may not use this file except in compliance with the License. + * You may obtain a copy of the License at + * + * http://www.apache.org/licenses/LICENSE-2.0 + * + * Unless required by applicable law or agreed to in writing, software + * distributed under the License is distributed on an "AS IS" BASIS, + * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. + * See the License for the specific language governing permissions and + * limitations under the License. + */ + +#include "print_parse_type.h" + +#include "async_call.h" +#include "napi_print_ext.h" +#include "napi_print_utils.h" +#include "print_log.h" +#include "print_manager_client.h" + +namespace OHOS::Print { +static constexpr const char *PARAM_INFO_PRINTID = "printerId"; +static constexpr const char *PARAM_INFO_PRINTERNAME = "printerName"; +static constexpr const char *PARAM_INFO_PRINTERICON = "printerIcon"; +static constexpr const char *PARAM_INFO_PRINTERSTATE = "printerState"; +static constexpr const char *PARAM_INFO_DESCRIPTION = "description"; +static constexpr const char *PARAM_INFO_CAPABILITY = "capability"; + +static constexpr const char *PARAM_CAPABILITY_MINMARGIN = "minMargin"; +static constexpr const char *PARAM_CAPABILITY_PAGESIZE = "pageSize"; +static constexpr const char *PARAM_CAPABILITY_RESOLUTION = "resolution"; +static constexpr const char *PARAM_CAPABILITY_COLORMODE = "colorMode"; +static constexpr const char *PARAM_CAPABILITY_DUPLEXMODE = "duplexMode"; + +static constexpr const char *PARAM_PAGESIZE_ID = "id"; +static constexpr const char *PARAM_PAGESIZE_NAME = "name"; +static constexpr const char *PARAM_PAGESIZE_WIDTH = "width"; +static constexpr const char *PARAM_PAGESIZE_HEIGHT = "height"; + +static constexpr const char *PARAM_RESOLUTION_ID = "id"; +static constexpr const char *PARAM_RESOLUTION_HORIZONTALDPI = "horizontalDpi"; +static constexpr const char *PARAM_RESOLUTION_VERTICALDPI = "verticalDpi"; + +static constexpr const char *PARAM_MARGIN_TOP = "top"; +static constexpr const char *PARAM_MARGIN_BOTTOM = "bottom"; +static constexpr const char *PARAM_MARGIN_LEFT = "left"; +static constexpr const char *PARAM_MARGIN_RIGHT = "right"; + +static constexpr const char *PARAM_JOB_FILES = "files"; +static constexpr const char *PARAM_JOB_JOBID = "jobId"; +static constexpr const char *PARAM_JOB_PRINTERID = "printerId"; +static constexpr const char *PARAM_JOB_JOBSTATE = "jobState"; +static constexpr const char *PARAM_JOB_COPYNUMBER = "copyNumber"; +static constexpr const char *PARAM_JOB_PAGERANGE = "pageRange"; +static constexpr const char *PARAM_JOB_ISSEQUENTIAL = "isSequential"; +static constexpr const char *PARAM_JOB_PAGESIZE = "pageSize"; +static constexpr const char *PARAM_JOB_ISLANDSCAPE = "isLandscape"; +static constexpr const char *PARAM_JOB_COLORMODE = "colorMode"; +static constexpr const char *PARAM_JOB_DUPLEXMODE = "duplexMode"; +static constexpr const char *PARAM_JOB_MARGIN = "margin"; +static constexpr const char *PARAM_JOB_PREVIEW = "preview"; + +static constexpr const char *PARAM_PREATTRIBUTE_RANGE = "previewRange"; +static constexpr const char *PARAM_PREATTRIBUTE_RESULT = "result"; + +static constexpr const char *PARAM_RANGE_STARTPAGE = "startPage"; +static constexpr const char *PARAM_RANGE_ENDPAGE = "endPage"; +static constexpr const char *PARAM_RANGE_PAGES = "pages"; + +bool ParseType::ParseInfo(napi_env env, napi_value InfoValue, PrinterInfo &info) +{ + if (!ParseInfoParam(env, InfoValue, info)) { + PRINT_HILOGD("ParseInfoParam is error!"); + return false; + } + info.SetPrinterId(PrintNapiUtils::GetUint32Property(env, InfoValue, PARAM_INFO_PRINTID)); + info.SetPrinterName(PrintNapiUtils::GetStringPropertyUtf8(env, InfoValue, PARAM_INFO_PRINTERNAME)); + info.SetPrinterIcon(PrintNapiUtils::GetUint32Property(env, InfoValue, PARAM_INFO_PRINTERICON)); + info.SetPrinterState(PrintNapiUtils::GetUint32Property(env, InfoValue, PARAM_INFO_PRINTERSTATE)); + info.SetDescription(PrintNapiUtils::GetStringPropertyUtf8(env, InfoValue, PARAM_INFO_DESCRIPTION)); + + PRINT_HILOGD("info_value printId value is: %{public}d", info.GetPrintId()); + PRINT_HILOGD("info_value printerName value is %{public}s", info.GetPrinterName().c_str()); + PRINT_HILOGD("info_value printerIcon value is %{public}d", info.GetPrinterIcon()); + PRINT_HILOGD("info_value printerState value is %{public}d", info.GetPrinterState()); + PRINT_HILOGD("info_value description value is %{public}s", info.GetDescription().c_str()); + return true; +} + +bool ParseType::ParseCapability(napi_env env, napi_value capValue, PrinterCapability &capability) +{ + if (!ParseCapabilityParam(env, capValue, capability)) { + PRINT_HILOGD("ParseCapabilityParam is error!"); + return false; + } + // capability.SetMinMargin(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_CAPABILITY_MINMARGIN)); + capability.SetColorMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_COLORMODE)); + capability.SetDuplexMode(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_CAPABILITY_DUPLEXMODE)); + PRINT_HILOGD("capability_value colorMode value is %{public}d", capability.GetColorMode()); + PRINT_HILOGD("capability_value duplexMode value is %{public}d", capability.GetDuplexMode()); + return true; +} + +bool ParseType::ParsePageSize(napi_env env, napi_value capValue, PrintPageSize &pageSize) +{ + if (!ParsePageSizeParam(env, capValue, pageSize)) { + PRINT_HILOGD("ParsePageSizeParam is error!"); + return false; + } + pageSize.SetId(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_PAGESIZE_ID)); + pageSize.SetName(PrintNapiUtils::GetStringPropertyUtf8(env, capValue, PARAM_PAGESIZE_NAME)); + pageSize.SetWidth(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_WIDTH)); + pageSize.SetHeight(PrintNapiUtils::GetUint32Property(env, capValue, PARAM_PAGESIZE_HEIGHT)); + + PRINT_HILOGD("printerPageSize_value GetId value is %{public}s", pageSize.GetId().c_str()); + PRINT_HILOGD("printerPageSize_value GetName value is %{public}s", pageSize.GetName().c_str()); + PRINT_HILOGD("printerPageSize_value GetWidth value is %{public}d", pageSize.GetWidth()); + PRINT_HILOGD("printerPageSize_value GetHeight value is %{public}d", pageSize.GetHeight()); + return true; +} + +bool ParseType::ParseResolution(napi_env env, napi_value reValue, PrintResolution &resolution) +{ + if (!ParseResolutionParam(env, reValue, resolution)) { + PRINT_HILOGD("ParseResolutionParam is error!"); + return false; + } + resolution.SetId(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_ID)); + resolution.SetHorizontalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI)); + resolution.SetVerticalDpi(PrintNapiUtils::GetUint32Property(env, reValue, PARAM_RESOLUTION_VERTICALDPI)); + + PRINT_HILOGD("printerResolution_value GetId value is %{public}d", resolution.GetId()); + PRINT_HILOGD("printerResolution_value GetHorizontalDpi value is %{public}d", resolution.GetHorizontalDpi()); + PRINT_HILOGD("printerResolution_value GetVerticalDpi value is %{public}d", resolution.GetVerticalDpi()); + return true; +} + +bool ParseType::ParseMargin(napi_env env, napi_value marginValue, PrintMargin &margin) +{ + if (!ParseMarginParam(env, marginValue, margin)) { + PRINT_HILOGD("ParseResolutionParam is error!"); + return false; + } + margin.SetTop(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_TOP)); + margin.SetBottom(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_BOTTOM)); + margin.SetLeft(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_LEFT)); + margin.SetRight(PrintNapiUtils::GetUint32Property(env, marginValue, PARAM_MARGIN_RIGHT)); + + PRINT_HILOGD("printerMargin_value GetTop value is %{public}d", margin.GetTop()); + PRINT_HILOGD("printerMargin_value GetBottom value is %{public}d", margin.GetBottom()); + PRINT_HILOGD("printerMargin_value GetLeft value is %{public}d", margin.GetLeft()); + PRINT_HILOGD("printerMargin_value GetRight value is %{public}d", margin.GetRight()); + + return true; +} + +bool ParseType::ParseJob(napi_env env, napi_value jobValue, PrintJob &printJob) +{ + if (!ParseJobParam(env, jobValue, printJob)) { + PRINT_HILOGD("ParseJobParam is error!"); + return false; + } + printJob.SetJobId(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_JOBID)); + printJob.SetPrintId(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_PRINTERID)); + printJob.SetJobState(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_JOBSTATE)); + printJob.SetCopyNumber(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_COPYNUMBER)); + + PRINT_HILOGD("printJob_value GetJobId value is %{public}d", printJob.GetJobId()); + PRINT_HILOGD("printJob_value GetPrinterId value is %{public}d", printJob.GetPrinterId()); + PRINT_HILOGD("printJob_value GetJobState value is %{public}d", printJob.GetJobState()); + PRINT_HILOGD("printJob_value GetCopyNumber value is %{public}d", printJob.GetCopyNumber()); + return true; +} + +bool ParseType::ParsePreviewAttribute(napi_env env, napi_value preAttValue, PreviewAttribute &preAtt) +{ + if (!ParsePreviewAttributeParam(env, preAttValue, preAtt)) { + PRINT_HILOGD("ParsePreviewAttributeParam is error!"); + return false; + } + preAtt.SetResult(PrintNapiUtils::GetStringPropertyUtf8(env, preAttValue, PARAM_PREATTRIBUTE_RESULT)); + PRINT_HILOGD("preAtt_value GetResult value is %{public}s", preAtt.GetResult().c_str()); + return true; +} + +bool ParseType::ParseRange(napi_env env, napi_value rangeValue, PrintRange &range) +{ + if (!ParseRangeParam(env, rangeValue, range)) { + PRINT_HILOGD("ParseRangeParam is error!"); + return false; + } + range.SetStartPage(PrintNapiUtils::GetUint32Property(env, rangeValue, PARAM_RANGE_STARTPAGE)); + range.SetEndPage(PrintNapiUtils::GetUint32Property(env, rangeValue, PARAM_RANGE_ENDPAGE)); + PRINT_HILOGD("printRange_value GetStartPage value is %{public}d", range.GetStartPage()); + PRINT_HILOGD("printRange_value GetEndPage value is %{public}d", range.GetEndPage()); + return true; +} + +bool ParseType::ParseInfoParam(napi_env env, napi_value InfoValue, PrinterInfo &info) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTID); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERNAME); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERICON); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_PRINTERSTATE); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + napi_value param_five = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_DESCRIPTION); + if (PrintNapiUtils::GetValueType(env, param_five) != napi_string) { + PRINT_HILOGD("error param_five"); + return false; + } + napi_value param_six = PrintNapiUtils::GetNamedProperty(env, InfoValue, PARAM_INFO_CAPABILITY); + if (PrintNapiUtils::GetValueType(env, param_six) != napi_object) { + PRINT_HILOGD("error param_six"); + return false; + } else { + PrinterCapability capability; + if (!ParseCapability(env, param_six, capability)) { + PRINT_HILOGD("ParseCapability type error!"); + return false; + } + info.SetCapability(capability); + } + return true; +} + +bool ParseType::ParseCapabilityParam(napi_env env, napi_value capValue, PrinterCapability &capability) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_MINMARGIN); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_object) { + PRINT_HILOGD("error param_one"); + return false; + } else { + PrintMargin margin; + if (!ParseMargin(env, param_one, margin)) { + PRINT_HILOGD("ParseCapability type error!"); + return false; + } + capability.SetMinMargin(margin); + } + + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_PAGESIZE); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_object) { + PRINT_HILOGD("error param_two"); + return false; + } else { + bool isArray = false; + napi_is_array(env, param_two, &isArray); + if (!isArray) { + PRINT_HILOGD("ParsePageSize type error!"); + return false; + } + + std::vector pageSizeList; + uint32_t arrayLength = 0; + napi_get_array_length(env, param_two, &arrayLength); + for (uint32_t i = 0; i < arrayLength; i++) { + napi_value pageSizeValue; + PrintPageSize pageSize; + napi_get_element(env, param_two, i, &pageSizeValue); + if (!ParsePageSize(env, pageSizeValue, pageSize)) { + PRINT_HILOGD("ParsePageSize type error!"); + return false; + } + pageSizeList.push_back(pageSize); + } + capability.SetPageSize(pageSizeList); + } + + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_RESOLUTION); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_object) { + PRINT_HILOGD("error param_three"); + return false; + } else { + bool isReArray = false; + napi_is_array(env, param_three, &isReArray); + if (!isReArray) { + PRINT_HILOGD("PrintResolution type error!"); + return false; + } + std::vector resolutionList; + uint32_t arrayReLength = 0; + napi_get_array_length(env, param_three, &arrayReLength); + for (uint32_t i = 0; i < arrayReLength; i++) { + napi_value reValue; + PrintResolution resolution; + napi_get_element(env, param_three, i, &reValue); + if (!ParseResolution(env, reValue, resolution)) { + PRINT_HILOGD("PrintResolution type error!"); + return false; + } + resolutionList.push_back(resolution); + } + capability.SetResolution(resolutionList); + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_COLORMODE); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + napi_value param_five = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_CAPABILITY_DUPLEXMODE); + if (PrintNapiUtils::GetValueType(env, param_five) != napi_number) { + // ParseCapabilityParam() + PRINT_HILOGD("error param_five"); + return false; + } + auto names = PrintNapiUtils::GetPropertyNames(env, capValue); + return true; +} + +bool ParseType::ParsePageSizeParam(napi_env env, napi_value capValue, PrintPageSize &pageSize) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_ID); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_NAME); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_WIDTH); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, capValue, PARAM_PAGESIZE_HEIGHT); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + return true; +} + +bool ParseType::ParseResolutionParam(napi_env env, napi_value reValue, PrintResolution &resolution) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_ID); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_HORIZONTALDPI); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, reValue, PARAM_RESOLUTION_VERTICALDPI); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + return true; +} + +bool ParseType::ParseMarginParam(napi_env env, napi_value marginValue, PrintMargin &margin) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_TOP); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_BOTTOM); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_LEFT); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, marginValue, PARAM_MARGIN_RIGHT); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + auto names = PrintNapiUtils::GetPropertyNames(env, marginValue); + PRINT_HILOGD("current margin paramster name list size = %{public}zu", names.size()); + return true; +} + +bool ParseType::ParseJobParam(napi_env env, napi_value jobValue, PrintJob &printJob) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_FILES); + bool isFileArray = false; + napi_is_array(env, param_one, &isFileArray); + if (!isFileArray) { + PRINT_HILOGD("PrinterJobFile type error!"); + return false; + } + std::vector printfiles; + uint32_t arrayReLength = 0; + napi_get_array_length(env, param_one, &arrayReLength); + for (uint32_t i = 0; i < arrayReLength; i++) { + napi_value filesValue; + napi_get_element(env, param_one, i, &filesValue); + std::string files = PrintNapiUtils::GetStringFromValueUtf8(env, filesValue); + PRINT_HILOGD("printJob_value param_one %{public}s", files.c_str()); + printfiles.push_back(files); + } + + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_JOBID); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + napi_value param_three = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PRINTERID); + if (PrintNapiUtils::GetValueType(env, param_three) != napi_number) { + PRINT_HILOGD("error param_three"); + return false; + } + napi_value param_four = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_JOBSTATE); + // napi_value jobState = PrintNapiUtils::GetNamedProperty(env, param_four, PARAM_JOB_PRINTERSTATE); + if (PrintNapiUtils::GetValueType(env, param_four) != napi_number) { + PRINT_HILOGD("error param_four"); + return false; + } + napi_value param_five = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_COPYNUMBER); + if (PrintNapiUtils::GetValueType(env, param_five) != napi_number) { + PRINT_HILOGD("error param_five"); + return false; + } + napi_value param_six = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PAGERANGE); + if (PrintNapiUtils::GetValueType(env, param_six) != napi_object) { + PRINT_HILOGD("error param_six"); + return false; + } else { + PrintRange range; + if (!ParseRange(env, param_six, range)) { + PRINT_HILOGD("ParseRange type error!"); + return false; + } + printJob.SetPageRange(range); + } + PRINT_HILOGD("param_six END"); + // ·Ç±ØÒª²ÎÊý + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_ISSEQUENTIAL)) { + PRINT_HILOGD("No isSequential present, ignore it"); + } else { + napi_value param_seven = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_ISSEQUENTIAL); + if (PrintNapiUtils::GetValueType(env, param_seven) != napi_boolean) { + PRINT_HILOGD("error param_seven"); + return false; + } else { + printJob.SetIsSequential(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_ISSEQUENTIAL)); + PRINT_HILOGD( + "printJob_value IsSequential value is %{public}s", printJob.GetIsSequential() ? "true" : "false"); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_PAGESIZE)) { + PRINT_HILOGD("No pageSize present, ignore it"); + } else { + napi_value param_eight = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PAGESIZE); + if (PrintNapiUtils::GetValueType(env, param_eight) != napi_object) { + PRINT_HILOGD("error param_eight"); + return false; + } else { + PrintPageSize pagesize; + if (!ParsePageSize(env, param_eight, pagesize)) { + PRINT_HILOGD("ParsePageSize type error!"); + return false; + } + printJob.SetPageSize(pagesize); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_ISLANDSCAPE)) { + PRINT_HILOGD("No isLandscape present, ignore it"); + } else { + napi_value param_nine = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_ISLANDSCAPE); + if (PrintNapiUtils::GetValueType(env, param_nine) != napi_boolean) { + PRINT_HILOGD("error param_nine"); + return false; + } else { + printJob.SetIsLandscape(PrintNapiUtils::GetBooleanProperty(env, jobValue, PARAM_JOB_ISLANDSCAPE)); + PRINT_HILOGD( + "printJob_value SetIsLandscape value is %{public}s", printJob.GetIsLandscape() ? "true" : "false"); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_COLORMODE)) { + PRINT_HILOGD("No colorMode present, ignore it"); + } else { + napi_value param_ten = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_COLORMODE); + if (PrintNapiUtils::GetValueType(env, param_ten) != napi_number) { + PRINT_HILOGD("error param_ten"); + return false; + } else { + printJob.SetColorMode(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_COLORMODE)); + PRINT_HILOGD("printJob_value ColorMode value is %{public}d", printJob.GetIsSequential()); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_DUPLEXMODE)) { + PRINT_HILOGD("No duplexMode present, ignore it"); + } else { + napi_value param_eleven = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_DUPLEXMODE); + if (PrintNapiUtils::GetValueType(env, param_eleven) != napi_number) { + PRINT_HILOGD("error param_eleven"); + return false; + } else { + printJob.SetDuplexMode(PrintNapiUtils::GetUint32Property(env, jobValue, PARAM_JOB_DUPLEXMODE)); + PRINT_HILOGD("printJob_value DuplexMode value is %{public}d", printJob.GetDuplexMode()); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_MARGIN)) { + PRINT_HILOGD("No isSequential present, ignore it"); + } else { + napi_value param_twelve = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_MARGIN); + if (PrintNapiUtils::GetValueType(env, param_twelve) != napi_object) { + PRINT_HILOGD("error param_twelve"); + return false; + } else { + PrintMargin margin; + if (!ParseMargin(env, param_twelve, margin)) { + PRINT_HILOGD("PrintMargin type error!"); + } + printJob.SetMargin(margin); + } + } + + if (!PrintNapiUtils::HasNamedProperty(env, jobValue, PARAM_JOB_PREVIEW)) { + PRINT_HILOGD("No PreviewAttribute present, ignore it"); + } else { + napi_value param_thirteen = PrintNapiUtils::GetNamedProperty(env, jobValue, PARAM_JOB_PREVIEW); + PreviewAttribute previewAttribute; + if (!ParsePreviewAttribute(env, param_thirteen, previewAttribute)) { + PRINT_HILOGD("ParsePreviewAttribute type error!"); + return false; + } + printJob.SetPreview(previewAttribute); + } + return true; +} + +bool ParseType::ParsePreviewAttributeParam(napi_env env, napi_value preAttValue, PreviewAttribute &preAtt) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, preAttValue, PARAM_PREATTRIBUTE_RANGE); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_object) { + PRINT_HILOGD("error param_one"); + return false; + } else { + PrintRange range; + if (!ParseRange(env, param_one, range)) { + PRINT_HILOGD("ParseRange type error!"); + return false; + } + preAtt.SetPreviewRange(range); + } + + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, preAttValue, PARAM_PREATTRIBUTE_RESULT); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_string) { + PRINT_HILOGD("error param_two"); + return false; + } + return true; +} +bool ParseType::ParseRangeParam(napi_env env, napi_value rangeValue, PrintRange &range) +{ + napi_value param_one = PrintNapiUtils::GetNamedProperty(env, rangeValue, PARAM_RANGE_STARTPAGE); + if (PrintNapiUtils::GetValueType(env, param_one) != napi_number) { + PRINT_HILOGD("error param_one"); + return false; + } + napi_value param_two = PrintNapiUtils::GetNamedProperty(env, rangeValue, PARAM_RANGE_ENDPAGE); + if (PrintNapiUtils::GetValueType(env, param_two) != napi_number) { + PRINT_HILOGD("error param_two"); + return false; + } + PRINT_HILOGD("Set startPage, endPage value begin."); + napi_value startPage, endPage, param_three = PrintNapiUtils::GetNamedProperty(env, rangeValue, PARAM_RANGE_PAGES); + bool isArray = false; + napi_is_array(env, param_three, &isArray); + uint32_t arrayLength = 0, startPageNum, endPageNum; + napi_get_array_length(env, param_three, &arrayLength); + if (!isArray || arrayLength != 2) { + PRINT_HILOGD("ParseRangeParam pages array error!"); + return false; + } + PRINT_HILOGD("Set startPageNum value begin."); + napi_get_element(env, param_three, 0, &startPage); + napi_get_element(env, param_three, 1, &endPage); + napi_get_value_uint32(env, startPage, &startPageNum); + napi_get_value_uint32(env, endPage, &endPageNum); + if (startPageNum > endPageNum) { + PRINT_HILOGD("startPage is less than endPage, Param Pages is illegal!"); + return false; + } + PRINT_HILOGD("Set page value begin."); + std::vector page = { startPageNum, endPageNum }; + range.SetPages(page); + PRINT_HILOGD("SetPage value is : %{public}d, %{public}d.", startPageNum, endPageNum); + return true; +} +} // namespace OHOS::Print diff --git a/profile/3707.xml b/profile/3707.xml index 024df5ea..1da159ca 100644 --- a/profile/3707.xml +++ b/profile/3707.xml @@ -1,5 +1,5 @@ -