• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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     int32_t fd = data.ReadFileDescriptor();
106     bool result = false;
107     if (fd >= 0) {
108         PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent jobId:%{public}s, fd:%{public}d", jobId.c_str(), fd);
109         result = OnCallbackAdapterLayout(jobId, *oldAttrs, *newAttrs, static_cast<uint32_t>(fd));
110         if (!result) {
111             close(fd);
112         }
113     }
114     reply.WriteBool(result);
115     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent end");
116     return true;
117 }
118 
HandlePrintAdapterJobChangedEvent(MessageParcel & data,MessageParcel & reply)119 bool PrintCallbackStub::HandlePrintAdapterJobChangedEvent(MessageParcel &data, MessageParcel &reply)
120 {
121     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent start");
122     std::string jobId = data.ReadString();
123     uint32_t state = data.ReadUint32();
124     uint32_t subState = data.ReadUint32();
125 
126     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent jobId:%{public}s, subState:%{public}d",
127         jobId.c_str(), subState);
128     bool result = OnCallbackAdapterJobStateChanged(jobId, state, subState);
129     reply.WriteBool(result);
130     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent end");
131     return true;
132 }
133 
HandlePrintAdapterGetFileEvent(MessageParcel & data,MessageParcel & reply)134 bool PrintCallbackStub::HandlePrintAdapterGetFileEvent(MessageParcel &data, MessageParcel &reply)
135 {
136     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent start");
137     uint32_t state = data.ReadUint32();
138 
139     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent state:%{public}d",
140         state);
141     bool result = OnCallbackAdapterGetFile(state);
142     reply.WriteBool(result);
143     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent end");
144     return true;
145 }
146 } // namespace OHOS::Print
147