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_callback_stub.h"
17 
18 #include "print_constant.h"
19 #include "print_log.h"
20 
21 namespace OHOS::Print {
PrintCallbackStub()22 PrintCallbackStub::PrintCallbackStub()
23 {
24     cmdMap_[PRINT_CALLBACK_TASK] = &PrintCallbackStub::HandlePrintTaskEvent;
25     cmdMap_[PRINT_CALLBACK_PRINTER] = &PrintCallbackStub::HandlePrinterEvent;
26     cmdMap_[PRINT_CALLBACK_PRINT_JOB] = &PrintCallbackStub::HandlePrintJobEvent;
27     cmdMap_[PRINT_CALLBACK_EXTINFO] = &PrintCallbackStub::HandleExtEvent;
28     cmdMap_[PRINT_CALLBACK_PRINT_JOB_ADAPTER] = &PrintCallbackStub::HandlePrintAdapterJobEvent;
29     cmdMap_[PRINT_CALLBACK_PRINT_JOB_CHANGED_ADAPTER] = &PrintCallbackStub::HandlePrintAdapterJobChangedEvent;
30     cmdMap_[PRINT_CALLBACK_PRINT_GET_FILE_ADAPTER] = &PrintCallbackStub::HandlePrintAdapterGetFileEvent;
31 }
32 
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)33 int32_t PrintCallbackStub::OnRemoteRequest(
34     uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)
35 {
36     PRINT_HILOGD("OnRemoteRequest started, code = %{public}d", code);
37     auto descriptorToken = data.ReadInterfaceToken();
38     if (descriptorToken != GetDescriptor()) {
39         PRINT_HILOGE("Remote descriptor not the same as local descriptor.");
40         return E_PRINT_RPC_FAILURE;
41     }
42 
43     auto itFunc = cmdMap_.find(code);
44     if (itFunc != cmdMap_.end()) {
45         auto requestFunc = itFunc->second;
46         if (requestFunc != nullptr) {
47             bool result = (this->*requestFunc)(data, reply);
48             return result ? E_PRINT_NONE : E_PRINT_SERVER_FAILURE;
49         }
50     }
51     PRINT_HILOGW("default case, need check.");
52     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
53 }
54 
HandlePrintTaskEvent(MessageParcel & data,MessageParcel & reply)55 bool PrintCallbackStub::HandlePrintTaskEvent(MessageParcel &data, MessageParcel &reply)
56 {
57     bool result = OnCallback();
58     reply.WriteBool(result);
59     return true;
60 }
61 
HandlePrinterEvent(MessageParcel & data,MessageParcel & reply)62 bool PrintCallbackStub::HandlePrinterEvent(MessageParcel &data, MessageParcel &reply)
63 {
64     uint32_t state = data.ReadUint32();
65     auto info = PrinterInfo::Unmarshalling(data);
66     bool result = false;
67     if (info != nullptr) {
68         result = OnCallback(state, *info);
69         reply.WriteBool(result);
70     }
71     return result;
72 }
73 
HandlePrintJobEvent(MessageParcel & data,MessageParcel & reply)74 bool PrintCallbackStub::HandlePrintJobEvent(MessageParcel &data, MessageParcel &reply)
75 {
76     uint32_t state = data.ReadUint32();
77     auto info = PrintJob::Unmarshalling(data);
78     bool result = false;
79     if (info != nullptr) {
80         result = OnCallback(state, *info);
81         reply.WriteBool(result);
82     }
83     return result;
84 }
85 
HandleExtEvent(MessageParcel & data,MessageParcel & reply)86 bool PrintCallbackStub::HandleExtEvent(MessageParcel &data, MessageParcel &reply)
87 {
88     std::string extensionId = data.ReadString();
89     std::string info = data.ReadString();
90     bool result = OnCallback(extensionId, info);
91     reply.WriteBool(result);
92     return true;
93 }
94 
HandlePrintAdapterJobEvent(MessageParcel & data,MessageParcel & reply)95 bool PrintCallbackStub::HandlePrintAdapterJobEvent(MessageParcel &data, MessageParcel &reply)
96 {
97     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent start");
98     std::string jobId = data.ReadString();
99     auto oldAttrs = PrintAttributes::Unmarshalling(data);
100     auto newAttrs = PrintAttributes::Unmarshalling(data);
101     if (newAttrs == nullptr || oldAttrs == nullptr) {
102         PRINT_HILOGE("invalid print attributes object");
103         return false;
104     }
105     uint32_t fd = static_cast<uint32_t>(data.ReadFileDescriptor());
106 
107     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent jobId:%{public}s, fd:%{public}d", jobId.c_str(), fd);
108     bool result = OnCallbackAdapterLayout(jobId, *oldAttrs, *newAttrs, fd);
109     reply.WriteBool(result);
110     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent end");
111     return true;
112 }
113 
HandlePrintAdapterJobChangedEvent(MessageParcel & data,MessageParcel & reply)114 bool PrintCallbackStub::HandlePrintAdapterJobChangedEvent(MessageParcel &data, MessageParcel &reply)
115 {
116     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent start");
117     std::string jobId = data.ReadString();
118     uint32_t state = data.ReadUint32();
119     uint32_t subState = data.ReadUint32();
120 
121     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent jobId:%{public}s, subState:%{public}d",
122         jobId.c_str(), subState);
123     bool result = onCallbackAdapterJobStateChanged(jobId, state, subState);
124     reply.WriteBool(result);
125     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent end");
126     return true;
127 }
128 
HandlePrintAdapterGetFileEvent(MessageParcel & data,MessageParcel & reply)129 bool PrintCallbackStub::HandlePrintAdapterGetFileEvent(MessageParcel &data, MessageParcel &reply)
130 {
131     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent start");
132     uint32_t state = data.ReadUint32();
133 
134     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent state:%{public}d",
135         state);
136     bool result = OnCallbackAdapterGetFile(state);
137     reply.WriteBool(result);
138     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent end");
139     return true;
140 }
141 } // namespace OHOS::Print
142