• 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             return (this->*requestFunc)(data, reply);
48         }
49     }
50     PRINT_HILOGW("default case, need check.");
51     return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
52 }
53 
HandlePrintTaskEvent(MessageParcel & data,MessageParcel & reply)54 bool PrintCallbackStub::HandlePrintTaskEvent(MessageParcel &data, MessageParcel &reply)
55 {
56     bool result = OnCallback();
57     reply.WriteBool(result);
58     return true;
59 }
60 
HandlePrinterEvent(MessageParcel & data,MessageParcel & reply)61 bool PrintCallbackStub::HandlePrinterEvent(MessageParcel &data, MessageParcel &reply)
62 {
63     uint32_t state = data.ReadUint32();
64     auto info = PrinterInfo::Unmarshalling(data);
65     bool result = false;
66     if (info != nullptr) {
67         result = OnCallback(state, *info);
68         reply.WriteBool(result);
69     }
70     return result;
71 }
72 
HandlePrintJobEvent(MessageParcel & data,MessageParcel & reply)73 bool PrintCallbackStub::HandlePrintJobEvent(MessageParcel &data, MessageParcel &reply)
74 {
75     uint32_t state = data.ReadUint32();
76     auto info = PrintJob::Unmarshalling(data);
77     bool result = false;
78     if (info != nullptr) {
79         result = OnCallback(state, *info);
80         reply.WriteBool(result);
81     }
82     return result;
83 }
84 
HandleExtEvent(MessageParcel & data,MessageParcel & reply)85 bool PrintCallbackStub::HandleExtEvent(MessageParcel &data, MessageParcel &reply)
86 {
87     std::string extensionId = data.ReadString();
88     std::string info = data.ReadString();
89     bool result = OnCallback(extensionId, info);
90     reply.WriteBool(result);
91     return true;
92 }
93 
HandlePrintAdapterJobEvent(MessageParcel & data,MessageParcel & reply)94 bool PrintCallbackStub::HandlePrintAdapterJobEvent(MessageParcel &data, MessageParcel &reply)
95 {
96     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent start");
97     std::string jobId = data.ReadString();
98     auto oldAttrs = PrintAttributes::Unmarshalling(data);
99     auto newAttrs = PrintAttributes::Unmarshalling(data);
100     if (newAttrs == nullptr) {
101         PRINT_HILOGE("invalid print attributes object");
102         return false;
103     }
104     uint32_t fd = static_cast<uint32_t>(data.ReadFileDescriptor());
105 
106     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent jobId:%{public}s, fd:%{public}d", jobId.c_str(), fd);
107     bool result = OnCallbackAdapterLayout(jobId, *oldAttrs, *newAttrs, fd);
108     reply.WriteBool(result);
109     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobEvent end");
110     return true;
111 }
112 
HandlePrintAdapterJobChangedEvent(MessageParcel & data,MessageParcel & reply)113 bool PrintCallbackStub::HandlePrintAdapterJobChangedEvent(MessageParcel &data, MessageParcel &reply)
114 {
115     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent start");
116     std::string jobId = data.ReadString();
117     uint32_t state = data.ReadUint32();
118     uint32_t subState = data.ReadUint32();
119 
120     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent jobId:%{public}s, subState:%{public}d",
121         jobId.c_str(), subState);
122     bool result = onCallbackAdapterJobStateChanged(jobId, state, subState);
123     reply.WriteBool(result);
124     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterJobChangedEvent end");
125     return true;
126 }
127 
HandlePrintAdapterGetFileEvent(MessageParcel & data,MessageParcel & reply)128 bool PrintCallbackStub::HandlePrintAdapterGetFileEvent(MessageParcel &data, MessageParcel &reply)
129 {
130     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent start");
131     uint32_t state = data.ReadUint32();
132 
133     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent state:%{public}d",
134         state);
135     bool result = OnCallbackAdapterGetFile(state);
136     reply.WriteBool(result);
137     PRINT_HILOGI("PrintCallbackStub HandlePrintAdapterGetFileEvent end");
138     return true;
139 }
140 } // namespace OHOS::Print
141