1 /*
2  * Copyright (c) 2023 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 #include "key_session_service_callback_stub.h"
16 #include "remote_request_code.h"
17 #include "drm_log.h"
18 #include "drm_error_code.h"
19 
20 namespace OHOS {
21 namespace DrmStandard {
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)22 int32_t MediaKeySessionServiceCallbackStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
23     MessageOption &option)
24 {
25     int32_t ret = DRM_ERROR;
26     if (data.ReadInterfaceToken() != GetDescriptor()) {
27         return ret;
28     }
29     switch (code) {
30         case MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT:
31             ret = MediaKeySessionServiceCallbackStub::HandleSendEvent(data);
32             break;
33         case MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT_KEY_CHANGED:
34             ret = MediaKeySessionServiceCallbackStub::HandleSendEventKeyChanged(data);
35             break;
36         default:
37             DRM_ERR_LOG("request code %{public}u not handled", code);
38             ret = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
39             break;
40     }
41     return ret;
42 }
43 
HandleSendEvent(MessageParcel & data)44 int32_t MediaKeySessionServiceCallbackStub::HandleSendEvent(MessageParcel &data)
45 {
46     DRM_INFO_LOG("HandleSendEvent enter.");
47     int32_t event = data.ReadInt32();
48     int32_t extra = data.ReadInt32();
49     uint32_t dataSize = data.ReadUint32();
50     std::vector<uint8_t> customizedData;
51     if (dataSize != 0) {
52         const uint8_t *dataBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(dataSize));
53         if (dataBuf == nullptr) {
54             DRM_ERR_LOG("HandleSendEvent read data failed.");
55             return IPC_STUB_WRITE_PARCEL_ERR;
56         }
57         customizedData.assign(dataBuf, dataBuf + dataSize);
58     }
59 
60     DrmEventType eventType = static_cast<DrmEventType>(event);
61     return SendEvent(eventType, extra, customizedData);
62 }
63 
HandleSendEventKeyChanged(MessageParcel & data)64 int32_t MediaKeySessionServiceCallbackStub::HandleSendEventKeyChanged(MessageParcel &data)
65 {
66     DRM_INFO_LOG("HandleSendEventKeyChanged enter.");
67     std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable;
68     uint32_t mapSize = data.ReadUint32();
69     DRM_CHECK_AND_RETURN_RET_LOG(mapSize < DATA_MAX_LEN, DRM_MEMORY_ERROR,
70         "The size of event data is too large.");
71     for (uint32_t index = 0; index < mapSize; index++) {
72         std::vector<uint8_t> item;
73         uint32_t idSize = data.ReadUint32();
74         if (idSize != 0) {
75             const uint8_t *idBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(idSize));
76             if (idBuf == nullptr) {
77                 DRM_ERR_LOG("HandleSendEventKeyChanged read data failed.");
78                 return IPC_STUB_WRITE_PARCEL_ERR;
79             }
80             item.assign(idBuf, idBuf + idSize);
81         }
82         MediaKeySessionKeyStatus licenseStatus = static_cast<MediaKeySessionKeyStatus>(data.ReadInt32());
83         statusTable[item] = licenseStatus;
84     }
85 
86     bool hasNewGoodLicense = data.ReadBool();
87     return SendEventKeyChanged(statusTable, hasNewGoodLicense);
88 }
89 } // namespace DrmStandard
90 } // namespace OHOS