1 /*
2 * Copyright (c) 2025 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_entry.h"
17 #include "print_log.h"
18 #include "print_constant.h"
19 #include "print_manager_client.h"
20 #include "print_utils.h"
21 #include "ability.h"
22 #include "ui_ability_thread.h"
23
24 namespace OHOS::Print {
25 static const std::string SPOOLER_BUNDLE_NAME = "com.ohos.spooler";
26 static const std::string SPOOLER_PREVIEW_ABILITY_NAME = "PrintServiceExtAbility";
27 static const std::string LAUNCH_PARAMETER_JOB_ID = "jobId";
28 static const std::string LAUNCH_PARAMETER_FILE_LIST = "fileList";
29 static const std::string UI_EXTENSION_TYPE_NAME = "ability.want.params.uiExtensionType";
30 static const std::string PRINT_UI_EXTENSION_TYPE = "sysDialog/print";
31 static const std::string CALLER_PKG_NAME = "caller.pkgName";
32 static const std::string ABILITY_PARAMS_STREAM = "ability.params.stream";
33
PrintEntry()34 PrintEntry::PrintEntry() {}
35
~PrintEntry()36 PrintEntry::~PrintEntry() {}
37
GetInstance()38 std::shared_ptr<PrintEntry> PrintEntry::GetInstance()
39 {
40 static std::shared_ptr<PrintEntry> instance = std::make_shared<PrintEntry>();
41 return instance;
42 }
43
StartPrint(const std::vector<std::string> & files)44 int32_t PrintEntry::StartPrint(const std::vector<std::string>& files)
45 {
46 std::vector<uint32_t> fdList;
47 if (files.empty()) {
48 PRINT_HILOGE("fileList and fdList are both empty");
49 return E_PRINT_INVALID_PARAMETER;
50 }
51 for (auto file : files) {
52 int32_t fd = PrintUtils::OpenFile(file);
53 if (fd < 0) {
54 PRINT_HILOGE("file[%{private}s] is invalid", file.c_str());
55 return E_PRINT_INVALID_PARAMETER;
56 }
57 fdList.emplace_back(fd);
58 }
59
60 PRINT_HILOGI("call client's StartPrint interface.");
61 std::string jobId = PrintUtils::GetPrintJobId();
62 std::shared_ptr<AdapterParam> adapterParam = std::make_shared<AdapterParam>();
63 adapterParam->isCheckFdList = true;
64 adapterParam->jobId = jobId;
65 int32_t ret = CallSpooler(adapterParam, files);
66 if (ret != E_PRINT_NONE) {
67 PRINT_HILOGE("CallSpooler error");
68 return ret;
69 }
70 return PrintManagerClient::GetInstance()->StartPrint(files, fdList, jobId);
71 }
72
On(const std::string & type)73 int32_t PrintEntry::On(const std::string& type)
74 {
75 PRINT_HILOGI("PrintEntry On");
76 return E_PRINT_NONE;
77 }
78
Off(const std::string & type)79 int32_t PrintEntry::Off(const std::string& type)
80 {
81 PRINT_HILOGI("PrintEntry Off");
82 return E_PRINT_NONE;
83 }
84
CallSpooler(const std::shared_ptr<AdapterParam> & adapterParam,const std::vector<std::string> & files)85 int32_t PrintEntry::CallSpooler(const std::shared_ptr<AdapterParam> &adapterParam,
86 const std::vector<std::string>& files)
87 {
88 PRINT_HILOGI("enter CallSpooler.");
89 AAFwk::Want want;
90 want.SetElementName(SPOOLER_BUNDLE_NAME, SPOOLER_PREVIEW_ABILITY_NAME);
91 want.SetParam(LAUNCH_PARAMETER_JOB_ID, adapterParam->jobId);
92 want.SetParam(LAUNCH_PARAMETER_FILE_LIST, files);
93 PrintUtils::BuildAdapterParam(adapterParam, want);
94 int32_t callerTokenId = static_cast<int32_t>(IPCSkeleton::GetCallingTokenID());
95 int32_t callerUid = IPCSkeleton::GetCallingUid();
96 int32_t callerPid = IPCSkeleton::GetCallingPid();
97 std::string callerPkg = PrintUtils::GetBundleNameForUid(callerUid);
98 want.SetParam(AAFwk::Want::PARAM_RESV_CALLER_TOKEN, callerTokenId);
99 want.SetParam(AAFwk::Want::PARAM_RESV_CALLER_UID, callerUid);
100 want.SetParam(AAFwk::Want::PARAM_RESV_CALLER_PID, callerPid);
101 want.SetParam(CALLER_PKG_NAME, callerPkg);
102 want.SetParam(UI_EXTENSION_TYPE_NAME, PRINT_UI_EXTENSION_TYPE);
103 want.SetParam(ABILITY_PARAMS_STREAM, files);
104 want.SetFlags(AAFwk::Want::FLAG_AUTH_READ_URI_PERMISSION);
105 return E_PRINT_NONE;
106 }
107
108 } // namespace OHOS::Print
109