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 errCode = DRM_ERROR;
26 if (data.ReadInterfaceToken() != GetDescriptor()) {
27 return errCode;
28 }
29 switch (code) {
30 case MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT:
31 errCode = MediaKeySessionServiceCallbackStub::HandleSendEvent(data);
32 break;
33 case MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT_KEY_CHANGED:
34 errCode = MediaKeySessionServiceCallbackStub::HandleSendEventKeyChanged(data);
35 break;
36 default:
37 DRM_ERR_LOG("MediaKeySessionServiceCallbackStub request code %{public}u not handled", code);
38 errCode = IPCObjectStub::OnRemoteRequest(code, data, reply, option);
39 break;
40 }
41 return errCode;
42 }
43
HandleSendEvent(MessageParcel & data)44 int32_t MediaKeySessionServiceCallbackStub::HandleSendEvent(MessageParcel &data)
45 {
46 DRM_INFO_LOG("MediaKeySessionServiceCallbackStub 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.ReadBuffer(dataSize));
53 if (dataBuf == nullptr) {
54 DRM_ERR_LOG("MediaKeySessionServiceCallbackStub::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("MediaKeySessionServiceCallbackStub HandleSendEventKeyChanged enter.");
67 std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable;
68 uint32_t mapSize = data.ReadUint32();
69 for (uint32_t index = 0; index < mapSize; index++) {
70 std::vector<uint8_t> item;
71 uint32_t idSize = data.ReadUint32();
72 if (idSize != 0) {
73 const uint8_t *idBuf = static_cast<const uint8_t *>(data.ReadBuffer(idSize));
74 if (idBuf == nullptr) {
75 DRM_ERR_LOG("MediaKeySessionServiceCallbackStub::HandleSendEventKeyChanged read data failed");
76 return IPC_STUB_WRITE_PARCEL_ERR;
77 }
78 item.assign(idBuf, idBuf + idSize);
79 }
80 MediaKeySessionKeyStatus licenseStatus = static_cast<MediaKeySessionKeyStatus>(data.ReadInt32());
81 statusTable[item] = licenseStatus;
82 }
83
84 bool hasNewGoodLicense = data.ReadBool();
85 return SendEventKeyChanged(statusTable, hasNewGoodLicense);
86 }
87 } // namespace DrmStandard
88 } // namespace OHOS