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 <codecvt>
17 #include "ipc_skeleton.h"
18 #include "xcollie/xcollie.h"
19 #include "xcollie/xcollie_define.h"
20 #include "drm_error_code.h"
21 #include "drm_log.h"
22 #include "mediakeysystemfactory_service_stub.h"
23
24 namespace {
25 constexpr uint32_t MAX_LISTNER_NUM = 64;
26 }
27
28 namespace OHOS {
29 namespace DrmStandard {
MediaKeySystemFactoryServiceStub()30 MediaKeySystemFactoryServiceStub::MediaKeySystemFactoryServiceStub()
31 {
32 deathRecipientMap_.clear();
33 }
34
~MediaKeySystemFactoryServiceStub()35 MediaKeySystemFactoryServiceStub::~MediaKeySystemFactoryServiceStub()
36 {
37 }
38
MediaKeySystemFactoryClientDied(pid_t pid)39 void MediaKeySystemFactoryServiceStub::MediaKeySystemFactoryClientDied(pid_t pid)
40 {
41 DRM_ERR_LOG("MediaKeySystemFactory client has died, pid:%{public}d", pid);
42 std::lock_guard<std::recursive_mutex> lock(factoryServiceStubMutex_);
43 if (clientListenerMap_.find(pid) != clientListenerMap_.end()) {
44 if (clientListenerMap_[pid] != nullptr && clientListenerMap_[pid]->AsObject() != nullptr &&
45 deathRecipientMap_.find(pid) != deathRecipientMap_.end() && deathRecipientMap_[pid] != nullptr) {
46 (void)clientListenerMap_[pid]->AsObject()->RemoveDeathRecipient(deathRecipientMap_[pid]);
47 }
48 deathRecipientMap_.erase(pid);
49 clientListenerMap_.erase(pid);
50 }
51 DistroyForClientDied(pid);
52 }
53
SetListenerObject(const sptr<IRemoteObject> & object)54 int32_t MediaKeySystemFactoryServiceStub::SetListenerObject(const sptr<IRemoteObject> &object)
55 {
56 pid_t pid = IPCSkeleton::GetCallingPid();
57 std::lock_guard<std::recursive_mutex> lock(factoryServiceStubMutex_);
58 if (clientListenerMap_.find(pid) != clientListenerMap_.end()) {
59 if (clientListenerMap_[pid] != nullptr && clientListenerMap_[pid]->AsObject() != nullptr &&
60 deathRecipientMap_.find(pid) != deathRecipientMap_.end() && deathRecipientMap_[pid] != nullptr) {
61 (void)clientListenerMap_[pid]->AsObject()->RemoveDeathRecipient(deathRecipientMap_[pid]);
62 }
63 deathRecipientMap_.erase(pid);
64 clientListenerMap_.erase(pid);
65 }
66 DRM_CHECK_AND_RETURN_RET_LOG(clientListenerMap_.size() < MAX_LISTNER_NUM,
67 DRM_INNER_ERR_OPERATION_NOT_PERMITTED, "the number of listeners exceeds MAX_LISTNER_NUM: 64");
68 DRM_CHECK_AND_RETURN_RET_LOG(object != nullptr, DRM_INNER_ERR_MEMORY_ERROR, "set listener object is nullptr");
69 sptr<IDrmListener> clientListener = iface_cast<IDrmListener>(object);
70 DRM_CHECK_AND_RETURN_RET_LOG(
71 clientListener != nullptr, DRM_INNER_ERR_MEMORY_ERROR, "failed to convert IDrmListener");
72 sptr<DrmDeathRecipient> deathRecipient = new (std::nothrow) DrmDeathRecipient(pid);
73 DRM_CHECK_AND_RETURN_RET_LOG(deathRecipient != nullptr, DRM_INNER_ERR_MEMORY_ERROR,
74 "failed to new DrmDeathRecipient");
75 deathRecipient->SetNotifyCb([this] (pid_t pid) {
76 this->MediaKeySystemFactoryClientDied(pid);
77 });
78 if (clientListener->AsObject() != nullptr) {
79 (void)clientListener->AsObject()->AddDeathRecipient(deathRecipient);
80 }
81 DRM_DEBUG_LOG("MediaKeySystem client pid:%{public}d", pid);
82 deathRecipientMap_[pid] = deathRecipient;
83 clientListenerMap_[pid] = clientListener;
84 return 0;
85 }
86
IsListenerObjectSet()87 bool MediaKeySystemFactoryServiceStub::IsListenerObjectSet()
88 {
89 std::lock_guard<std::recursive_mutex> lock(factoryServiceStubMutex_);
90 pid_t pid = IPCSkeleton::GetCallingPid();
91 if (clientListenerMap_.find(pid) != clientListenerMap_.end()) {
92 return true;
93 }
94 return false;
95 }
96
ProcessCreateMediaKeySystem(MediaKeySystemFactoryServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)97 static int32_t ProcessCreateMediaKeySystem(MediaKeySystemFactoryServiceStub *stub, MessageParcel &data,
98 MessageParcel &reply, MessageOption &option)
99 {
100 bool res = stub->IsListenerObjectSet();
101 if (!res) {
102 DRM_ERR_LOG("Not Set Listener.");
103 return DRM_INNER_ERR_OPERATION_NOT_PERMITTED;
104 }
105 sptr<IMediaKeySystemService> mediaKeysystemProxy = nullptr;
106 std::string name = data.ReadString();
107 int32_t ret = stub->CreateMediaKeySystem(name, mediaKeysystemProxy);
108 if (ret != 0) {
109 DRM_ERR_LOG("CreateMediaKeySystem failed, errCode: %{public}d", ret);
110 return ret;
111 }
112 if (!reply.WriteRemoteObject(mediaKeysystemProxy->AsObject())) {
113 DRM_ERR_LOG("CreateMediaKeySystem Write MediaKeySession object failed.");
114 return DRM_INNER_ERR_OPERATION_NOT_PERMITTED;
115 }
116 return ret;
117 }
118
ProcessMediaKeySystemSupportedRequest(MediaKeySystemFactoryServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)119 static int32_t ProcessMediaKeySystemSupportedRequest(MediaKeySystemFactoryServiceStub *stub, MessageParcel &data,
120 MessageParcel &reply, MessageOption &option)
121 {
122 int32_t paramNum = data.ReadInt32();
123 bool isSupported = false;
124
125 DRM_CHECK_AND_RETURN_RET_LOG((paramNum <= (int32_t)ARGS_NUM_THREE) && (paramNum >= (int32_t)ARGS_NUM_ONE),
126 IPC_STUB_WRITE_PARCEL_ERR, "paramNum is invalid");
127 std::string name = data.ReadString();
128 if (paramNum == ARGS_NUM_ONE) {
129 int32_t ret = stub->IsMediaKeySystemSupported(name, &isSupported);
130 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "IsMediaKeySystemSupported faild, errCode:%{public}d", ret);
131 DRM_CHECK_AND_RETURN_RET_LOG(reply.WriteBool(isSupported), IPC_STUB_WRITE_PARCEL_ERR,
132 "Write isSupported failed.");
133 return ret;
134 }
135 std::string mimeType = data.ReadString();
136 if (paramNum == ARGS_NUM_TWO) {
137 int32_t ret = stub->IsMediaKeySystemSupported(name, mimeType, &isSupported);
138 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "IsMediaKeySystemSupported faild, errCode:%{public}d", ret);
139 DRM_CHECK_AND_RETURN_RET_LOG(reply.WriteBool(isSupported), IPC_STUB_WRITE_PARCEL_ERR,
140 "Write isSupported failed.");
141 return ret;
142 }
143
144 int32_t securityLevel = data.ReadInt32();
145 if (paramNum == ARGS_NUM_THREE) {
146 int32_t ret = stub->IsMediaKeySystemSupported(name, mimeType, securityLevel, &isSupported);
147 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "IsMediaKeySystemSupported faild, errCode:%{public}d", ret);
148 DRM_CHECK_AND_RETURN_RET_LOG(reply.WriteBool(isSupported), IPC_STUB_WRITE_PARCEL_ERR,
149 "Write isSupported failed.");
150 return ret;
151 }
152 return 0;
153 }
154
ProcessSetListenerObject(MediaKeySystemFactoryServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)155 static int32_t ProcessSetListenerObject(MediaKeySystemFactoryServiceStub *stub, MessageParcel &data,
156 MessageParcel &reply, MessageOption &option)
157 {
158 DRM_INFO_LOG("ProcessSetListenerObject enter.");
159 (void)reply;
160 sptr<IRemoteObject> object = data.ReadRemoteObject();
161 int32_t ret = stub->SetListenerObject(object);
162 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret,
163 "ProcessSetListenerObject faild, errCode:%{public}d", ret);
164 return ret;
165 }
166
ProcessGetMediaKeySystems(MediaKeySystemFactoryServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)167 static int32_t ProcessGetMediaKeySystems(MediaKeySystemFactoryServiceStub *stub, MessageParcel &data,
168 MessageParcel &reply, MessageOption &option)
169 {
170 DRM_INFO_LOG("ProcessGetMediaKeySystems enter.");
171 std::map<std::string, std::string> mediaKeySystemNames;
172 int32_t ret = stub->GetMediaKeySystems(mediaKeySystemNames);
173 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "GetMediaKeySystems faild, errCode:%{public}d", ret);
174 reply.WriteInt32(mediaKeySystemNames.size());
175 for (auto mediaKeySystemName : mediaKeySystemNames) {
176 DRM_CHECK_AND_RETURN_RET_LOG(reply.WriteString(mediaKeySystemName.first), IPC_STUB_WRITE_PARCEL_ERR,
177 "Write mediaKeySystem name failed.");
178 DRM_CHECK_AND_RETURN_RET_LOG(reply.WriteString(mediaKeySystemName.second), IPC_STUB_WRITE_PARCEL_ERR,
179 "Write mediaKeySystem uuid failed.");
180 }
181 return 0;
182 }
183
ProcessGetMediaKeySystemUuid(MediaKeySystemFactoryServiceStub * stub,MessageParcel & data,MessageParcel & reply,MessageOption & option)184 static int32_t ProcessGetMediaKeySystemUuid(MediaKeySystemFactoryServiceStub *stub, MessageParcel &data,
185 MessageParcel &reply, MessageOption &option)
186 {
187 DRM_INFO_LOG("ProcessGetMediaKeySystemUuid enter.");
188 std::string name = data.ReadString();
189 std::string uuid;
190 int32_t ret = stub->GetMediaKeySystemUuid(name, uuid);
191 DRM_CHECK_AND_RETURN_RET_LOG(ret == 0, ret, "ProcessGetMediaKeySystemUuid faild, errCode:%{public}d", ret);
192 if (!reply.WriteString(uuid)) {
193 DRM_ERR_LOG("ProcessGetMediaKeySystemUuid write value failed.");
194 return IPC_STUB_WRITE_PARCEL_ERR;
195 }
196 return 0;
197 }
198
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)199 int32_t MediaKeySystemFactoryServiceStub::OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply,
200 MessageOption &option)
201 {
202 int32_t ret = 0;
203 if (data.ReadInterfaceToken() != GetDescriptor()) {
204 DRM_DEBUG_LOG("ReadInterfaceToken failed.");
205 return -1;
206 }
207
208 switch (code) {
209 case MEDIA_KEY_SYSTEM_FACTORY_IS_MEDIA_KEY_SYSTEM_SUPPORTED: {
210 DRM_INFO_LOG("IS_MEDIA_KEY_SYSTEM_SUPPORTED enter.");
211 return ProcessMediaKeySystemSupportedRequest(this, data, reply, option);
212 }
213 case MEDIA_KEY_SYSTEM_FACTORY_CREATE_MEDIA_KEYSYSTEM: {
214 DRM_INFO_LOG("IS_MEDIA_KEY_SYSTEM_SUPPORTED enter.");
215 return ProcessCreateMediaKeySystem(this, data, reply, option);
216 }
217 case MEDIA_KEY_SYSTEM_FACTORY_SET_LISTENER_OBJ: {
218 DRM_INFO_LOG("MEDIA_KEY_SYSTEM_FACTORY_SET_LISTENER_OBJ enter.");
219 return ProcessSetListenerObject(this, data, reply, option);
220 }
221 case MEDIA_KEY_SYSTEM_FACTORY_GET_MEDIA_KEYSYSTEM_NAME: {
222 DRM_INFO_LOG("MEDIA_KEY_SYSTEM_FACTORY_GST_MEDIA_KEYSYSTEM_NAME enter.");
223 return ProcessGetMediaKeySystems(this, data, reply, option);
224 }
225 case MEDIA_KEY_SYSTEM_FACTORY_GET_MEDIA_KEYSYSTEM_UUID: {
226 DRM_INFO_LOG("MEDIA_KEY_SYSTEM_FACTORY_GET_MEDIA_KEYSYSTEM_UUID enter.");
227 return ProcessGetMediaKeySystemUuid(this, data, reply, option);
228 }
229 }
230 return ret;
231 }
232 } // namespace DrmStandard
233 } // namespace OHOS