• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_INNER_ERR_BASE;
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     DRM_CHECK_AND_RETURN_RET_LOG(dataSize < MAX_EVENT_NAME_LEN, DRM_INNER_ERR_MEMORY_ERROR,
51         "The size of event name is too large.");
52     std::vector<uint8_t> customizedData;
53     if (dataSize != 0) {
54         const uint8_t *dataBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(dataSize));
55         if (dataBuf == nullptr) {
56             DRM_ERR_LOG("HandleSendEvent read data failed.");
57             return IPC_STUB_WRITE_PARCEL_ERR;
58         }
59         customizedData.assign(dataBuf, dataBuf + dataSize);
60     }
61 
62     DrmEventType eventType = static_cast<DrmEventType>(event);
63     return SendEvent(eventType, extra, customizedData);
64 }
65 
HandleSendEventKeyChanged(MessageParcel & data)66 int32_t MediaKeySessionServiceCallbackStub::HandleSendEventKeyChanged(MessageParcel &data)
67 {
68     DRM_INFO_LOG("HandleSendEventKeyChanged enter.");
69     std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable;
70     uint32_t mapSize = data.ReadUint32();
71     DRM_CHECK_AND_RETURN_RET_LOG(mapSize < DATA_MAX_LEN, DRM_INNER_ERR_MEMORY_ERROR,
72         "The size of event data is too large.");
73     for (uint32_t index = 0; index < mapSize; index++) {
74         std::vector<uint8_t> item;
75         uint32_t idSize = data.ReadUint32();
76         if (idSize > 0 && idSize <= MAX_KEY_ID_LEN) {
77             const uint8_t *idBuf = static_cast<const uint8_t *>(data.ReadUnpadBuffer(idSize));
78             if (idBuf == nullptr) {
79                 DRM_ERR_LOG("HandleSendEventKeyChanged read data failed.");
80                 return IPC_STUB_WRITE_PARCEL_ERR;
81             }
82             item.assign(idBuf, idBuf + idSize);
83         }
84         MediaKeySessionKeyStatus licenseStatus = static_cast<MediaKeySessionKeyStatus>(data.ReadInt32());
85         statusTable[item] = licenseStatus;
86     }
87 
88     bool hasNewGoodLicense = data.ReadBool();
89     return SendEventKeyChanged(statusTable, hasNewGoodLicense);
90 }
91 } // namespace DrmStandard
92 } // namespace OHOS