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 
16 #include "key_session_service_callback_proxy.h"
17 #include "drm_log.h"
18 #include "remote_request_code.h"
19 #include "drm_error_code.h"
20 
21 namespace OHOS {
22 namespace DrmStandard {
MediaKeySessionServiceCallbackProxy(const sptr<IRemoteObject> & impl)23 MediaKeySessionServiceCallbackProxy::MediaKeySessionServiceCallbackProxy(const sptr<IRemoteObject> &impl)
24     : IRemoteProxy<IMediaKeySessionServiceCallback>(impl) {};
25 
SendEvent(DrmEventType event,int32_t extra,const std::vector<uint8_t> & data)26 int32_t MediaKeySessionServiceCallbackProxy::SendEvent(DrmEventType event, int32_t extra,
27     const std::vector<uint8_t> &data)
28 {
29     MessageParcel parcelData;
30     MessageParcel reply;
31     MessageOption option;
32     DRM_INFO_LOG("SendEvent called, event:%{public}d", event);
33     if (!parcelData.WriteInterfaceToken(GetDescriptor())) {
34         DRM_ERR_LOG("SendEvent Write interface token failed.");
35         return IPC_PROXY_ERR;
36     }
37     if (!parcelData.WriteInt32(event)) {
38         DRM_ERR_LOG("SendEvent Write event failed.");
39         return IPC_PROXY_ERR;
40     }
41     if (!parcelData.WriteInt32(extra)) {
42         DRM_ERR_LOG("SendEvent Write extra failed.");
43         return IPC_PROXY_ERR;
44     }
45     if (!parcelData.WriteUint32(data.size())) {
46         DRM_ERR_LOG("SendEvent Write data size failed.");
47         return IPC_PROXY_ERR;
48     }
49     DRM_CHECK_AND_RETURN_RET_LOG(data.size() < DATA_MAX_LEN, DRM_MEMORY_ERROR, "The size of data is too large.");
50     if (data.size() != 0) {
51         if (!parcelData.WriteBuffer(data.data(), data.size())) {
52             DRM_ERR_LOG("SendEvent write data failed.");
53             return IPC_PROXY_ERR;
54         }
55     }
56 
57     int32_t ret = Remote()->SendRequest(MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT, parcelData, reply, option);
58     if (ret != DRM_OK) {
59         DRM_ERR_LOG("SendEvent failed, errorcode: %{public}d", ret);
60     }
61     return ret;
62 }
63 
SendEventKeyChanged(std::map<std::vector<uint8_t>,MediaKeySessionKeyStatus> statusTable,bool hasNewGoodLicense)64 int32_t MediaKeySessionServiceCallbackProxy::SendEventKeyChanged(
65     std::map<std::vector<uint8_t>, MediaKeySessionKeyStatus> statusTable, bool hasNewGoodLicense)
66 {
67     MessageParcel parcelData;
68     MessageParcel reply;
69     MessageOption option;
70     DRM_INFO_LOG("SendEventKeyChanged enter.");
71     if (!parcelData.WriteInterfaceToken(GetDescriptor())) {
72         DRM_ERR_LOG("Write interface token failed.");
73         return IPC_PROXY_ERR;
74     }
75 
76     uint32_t mapSize = statusTable.size();
77     if (!parcelData.WriteUint32(mapSize)) {
78         DRM_ERR_LOG("Write mapSize failed.");
79         return IPC_PROXY_ERR;
80     }
81     for (auto item : statusTable) {
82         uint32_t idSize = item.first.size();
83         (void)parcelData.WriteUint32(idSize);
84         DRM_CHECK_AND_RETURN_RET_LOG(idSize < DATA_MAX_LEN, DRM_MEMORY_ERROR, "The size of data is too large.");
85         if (idSize != 0) {
86             if (!parcelData.WriteBuffer(item.first.data(), idSize)) {
87                 DRM_ERR_LOG("write data failed.");
88                 return IPC_PROXY_ERR;
89             }
90         }
91         (void)parcelData.WriteInt32(static_cast<int32_t>(item.second));
92     }
93 
94     if (!parcelData.WriteBool(hasNewGoodLicense)) {
95         DRM_ERR_LOG("Write extra failed.");
96         return IPC_PROXY_ERR;
97     }
98 
99     int32_t ret =
100         Remote()->SendRequest(MEDIA_KEY_SESSION_SERVICE_CALLBACK_SEND_EVENT_KEY_CHANGED, parcelData, reply, option);
101     if (ret != ERR_NONE) {
102         DRM_ERR_LOG("failed, errorcode: %{public}d", ret);
103     }
104     return ret;
105 }
106 } // DrmStandard
107 } // namespace OHOS