1 /*
2 * Copyright (c) 2022 Huawei Device Co., Ltd.
3 * Licensed under the Apache License, Version 2.0 (the "License");
4 * you may not use this file except in compliance with the License.
5 * You may obtain a copy of the License at
6 *
7 * http://www.apache.org/licenses/LICENSE-2.0
8 *
9 * Unless required by applicable law or agreed to in writing, software
10 * distributed under the License is distributed on an "AS IS" BASIS,
11 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12 * See the License for the specific language governing permissions and
13 * limitations under the License.
14 */
15
16 #include "print_task.h"
17 #include "napi/native_common.h"
18 #include "napi_print_utils.h"
19 #include "print_callback.h"
20 #include "print_log.h"
21 #include "print_manager_client.h"
22 #include "print_utils.h"
23
24 namespace OHOS::Print {
25 const std::string EVENT_BLOCK = "block";
26 const std::string EVENT_SUCCESS = "succeed";
27 const std::string EVENT_FAIL = "fail";
28 const std::string EVENT_CANCEL = "cancel";
29
PrintTask(const std::vector<std::string> & innerList,const sptr<IRemoteObject> & innerCallerToken_)30 PrintTask::PrintTask(const std::vector<std::string> &innerList, const sptr<IRemoteObject> &innerCallerToken_)
31 : taskId_("")
32 {
33 if (innerList.begin()->find("fd://") == 0) {
34 PRINT_HILOGD("list type: fdlist");
35 for (auto fdPath : innerList) {
36 pathType_ = FD_PATH;
37 uint32_t fd = PrintUtils::GetIdFromFdPath(fdPath);
38 fdList_.emplace_back(fd);
39 }
40 } else {
41 PRINT_HILOGD("list type: filelist");
42 fileList_.assign(innerList.begin(), innerList.end());
43 pathType_ = FILE_PATH_ABSOLUTED;
44 if (fileList_.size() > 0) {
45 if (fileList_.begin()->find("file://") == 0) {
46 pathType_ = FILE_PATH;
47 }
48 }
49 }
50
51 supportEvents_[EVENT_BLOCK] = true;
52 supportEvents_[EVENT_SUCCESS] = true;
53 supportEvents_[EVENT_FAIL] = true;
54 supportEvents_[EVENT_CANCEL] = true;
55 callerToken_ = innerCallerToken_;
56 }
57
PrintTask(const std::string & innerPrintJobName_,const sptr<IPrintCallback> & innerPrintAdapterCallback_,const std::shared_ptr<PrintAttributes> & innerPrintAttributes_,const sptr<IRemoteObject> & innerCallerToken_)58 PrintTask::PrintTask(const std::string &innerPrintJobName_, const sptr<IPrintCallback> &innerPrintAdapterCallback_,
59 const std::shared_ptr<PrintAttributes> &innerPrintAttributes_, const sptr<IRemoteObject> &innerCallerToken_)
60 : taskId_("")
61 {
62 supportEvents_[EVENT_BLOCK] = true;
63 supportEvents_[EVENT_SUCCESS] = true;
64 supportEvents_[EVENT_FAIL] = true;
65 supportEvents_[EVENT_CANCEL] = true;
66 printJobName_ = innerPrintJobName_;
67 printAdapterCallback_ = innerPrintAdapterCallback_;
68 printAttributes_ = innerPrintAttributes_;
69 callerToken_ = innerCallerToken_;
70 }
71
~PrintTask()72 PrintTask::~PrintTask()
73 {
74 supportEvents_.clear();
75 Stop();
76 }
77
Start()78 uint32_t PrintTask::Start()
79 {
80 if (fileList_.empty() && fdList_.empty()) {
81 PRINT_HILOGE("fileList and fdList are both empty");
82 return E_PRINT_INVALID_PARAMETER;
83 }
84 if (pathType_ == FILE_PATH_ABSOLUTED) {
85 for (auto file : fileList_) {
86 int32_t fd = PrintUtils::OpenFile(file);
87 if (fd < 0) {
88 PRINT_HILOGE("file[%{private}s] is invalid", file.c_str());
89 return E_PRINT_INVALID_PARAMETER;
90 }
91 fdList_.emplace_back(fd);
92 }
93 }
94 if (callerToken_ != nullptr && NEW_PRINT_INTERFACE_SWITCH) {
95 PRINT_HILOGI("call client's new StartPrint interface.");
96 return PrintManagerClient::GetInstance()->StartPrint(fileList_, fdList_, taskId_, callerToken_);
97 } else {
98 PRINT_HILOGI("call client's old StartPrint interface.");
99 return PrintManagerClient::GetInstance()->StartPrint(fileList_, fdList_, taskId_);
100 }
101 }
102
StartPrintAdapter()103 uint32_t PrintTask::StartPrintAdapter()
104 {
105 if (printAdapterCallback_ != nullptr && printAttributes_ != nullptr) {
106 PRINT_HILOGI("call client's StartPrintAdapter interface.");
107 return PrintManagerClient::GetInstance()->Print(
108 printJobName_, printAdapterCallback_, *printAttributes_, taskId_, static_cast<void*>(callerToken_));
109 }
110 return E_PRINT_INVALID_PARAMETER;
111 }
112
Stop()113 void PrintTask::Stop()
114 {
115 PrintManagerClient::GetInstance()->StopPrint(taskId_);
116 taskId_ = "";
117 }
118
GetId() const119 const std::string &PrintTask::GetId() const
120 {
121 return taskId_;
122 }
123
On(napi_env env,napi_callback_info info)124 napi_value PrintTask::On(napi_env env, napi_callback_info info)
125 {
126 PRINT_HILOGD("Enter ---->");
127 size_t argc = NapiPrintUtils::MAX_ARGC;
128 napi_value argv[NapiPrintUtils::MAX_ARGC] = { nullptr };
129 napi_value thisVal = nullptr;
130 void *data = nullptr;
131 PRINT_CALL(env, napi_get_cb_info(env, info, &argc, argv, &thisVal, &data));
132 PRINT_ASSERT(env, argc == NapiPrintUtils::ARGC_TWO, "need 2 parameter!");
133
134 napi_valuetype valuetype;
135 PRINT_CALL(env, napi_typeof(env, argv[0], &valuetype));
136 PRINT_ASSERT(env, valuetype == napi_string, "type is not a string");
137 std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[NapiPrintUtils::INDEX_ZERO]);
138 PRINT_HILOGD("type : %{public}s", type.c_str());
139
140 valuetype = napi_undefined;
141 napi_typeof(env, argv[1], &valuetype);
142 PRINT_ASSERT(env, valuetype == napi_function, "callback is not a function");
143
144 PrintTask *task;
145 PRINT_CALL(env, napi_unwrap(env, thisVal, reinterpret_cast<void **>(&task)));
146 if (task == nullptr || !task->IsSupportType(type)) {
147 PRINT_HILOGE("Event On type : %{public}s not support", type.c_str());
148 return nullptr;
149 }
150
151 napi_ref callbackRef = NapiPrintUtils::CreateReference(env, argv[1]);
152 sptr<IPrintCallback> callback = new (std::nothrow) PrintCallback(env, callbackRef);
153 if (callback == nullptr) {
154 PRINT_HILOGE("create print callback object fail");
155 return nullptr;
156 }
157 int32_t ret = PrintManagerClient::GetInstance()->On(task->taskId_, type, callback);
158 if (ret != E_PRINT_NONE) {
159 PRINT_HILOGE("Failed to register event");
160 return nullptr;
161 }
162 return nullptr;
163 }
164
Off(napi_env env,napi_callback_info info)165 napi_value PrintTask::Off(napi_env env, napi_callback_info info)
166 {
167 PRINT_HILOGD("Enter ---->");
168 auto context = std::make_shared<TaskEventContext>();
169 auto input = [context](napi_env env, size_t argc, napi_value *argv, napi_value self) -> napi_status {
170 PRINT_ASSERT_BASE(env, argc == NapiPrintUtils::ARGC_ONE, "need 1 parameter!", napi_invalid_arg);
171 napi_valuetype valuetype;
172 PRINT_CALL_BASE(env, napi_typeof(env, argv[NapiPrintUtils::INDEX_ZERO], &valuetype), napi_invalid_arg);
173 PRINT_ASSERT_BASE(env, valuetype == napi_string, "type is not a string", napi_string_expected);
174 std::string type = NapiPrintUtils::GetStringFromValueUtf8(env, argv[0]);
175 PrintTask *task;
176 PRINT_CALL_BASE(env, napi_unwrap(env, self, reinterpret_cast<void **>(&task)), napi_invalid_arg);
177 if (task == nullptr || !task->IsSupportType(type)) {
178 PRINT_HILOGE("Event On type : %{public}s not support", type.c_str());
179 context->SetErrorIndex(E_PRINT_INVALID_PARAMETER);
180 return napi_invalid_arg;
181 }
182
183 context->type = type;
184 context->taskId = task->taskId_;
185 PRINT_HILOGD("event type : %{public}s", context->type.c_str());
186 return napi_ok;
187 };
188 auto output = [context](napi_env env, napi_value *result) -> napi_status {
189 napi_status status = napi_get_boolean(env, context->result, result);
190 PRINT_HILOGD("context->result = %{public}d", context->result);
191 return status;
192 };
193 auto exec = [context](PrintAsyncCall::Context *ctx) {
194 int32_t ret = PrintManagerClient::GetInstance()->Off(context->taskId, context->type);
195 context->result = ret == E_PRINT_NONE;
196 if (ret != E_PRINT_NONE) {
197 PRINT_HILOGE("Failed to unregistered event");
198 context->SetErrorIndex(ret);
199 }
200 };
201 context->SetAction(std::move(input), std::move(output));
202 PrintAsyncCall asyncCall(env, info, std::dynamic_pointer_cast<PrintAsyncCall::Context>(context));
203 return asyncCall.Call(env, exec);
204 }
205
IsSupportType(const std::string & type) const206 bool PrintTask::IsSupportType(const std::string &type) const
207 {
208 return supportEvents_.find(type) != supportEvents_.end();
209 }
210 } // namespace OHOS::Print
211