1 /*
2 * Copyright (c) 2021 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 "rs_render_service_stub.h"
17
18 #include <iremote_proxy.h>
19
20 namespace OHOS {
21 namespace Rosen {
22 class RSConnectionTokenProxy : public IRemoteProxy<RSIConnectionToken> {
23 public:
RSConnectionTokenProxy(const sptr<IRemoteObject> & impl)24 explicit RSConnectionTokenProxy(const sptr<IRemoteObject>& impl)
25 : IRemoteProxy<RSIConnectionToken>(impl) {}
26 virtual ~RSConnectionTokenProxy() noexcept = default;
27
28 private:
29 static inline BrokerDelegator<RSConnectionTokenProxy> delegator_;
30 };
31
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)32 int RSRenderServiceStub::OnRemoteRequest(
33 uint32_t code, MessageParcel& data, MessageParcel& reply, MessageOption& option)
34 {
35 #ifdef ENABLE_IPC_SECURITY_ACCESS_COUNTER
36 auto accessCount = securityUtils_.GetCodeAccessCounter(code);
37 if (!securityManager_.IsAccessTimesRestricted(code, accessCount)) {
38 RS_LOGE("RSRenderServiceStub::OnRemoteRequest This Function[ID=%{public}u] invoke times:%{public}d"
39 "by pid[%{public}d]", code, accessCount, GetCallingPid());
40 return ERR_INVALID_STATE;
41 }
42 #endif
43 #ifdef ENABLE_IPC_SECURITY_ACCESS_COUNTER
44 securityUtils_.IncreaseAccessCounter(code);
45 #endif
46 int ret = ERR_NONE;
47 switch (code) {
48 case static_cast<uint32_t>(RSIRenderServiceInterfaceCode::CREATE_CONNECTION): {
49 auto interfaceToken = data.ReadInterfaceToken();
50 if (interfaceToken != RSIRenderService::GetDescriptor()) {
51 RS_LOGE("RSRenderServiceStub::CREATE_CONNECTION Read interfaceToken failed!");
52 ret = ERR_INVALID_STATE;
53 break;
54 }
55
56 auto remoteObj = data.ReadRemoteObject();
57 if (remoteObj == nullptr) {
58 RS_LOGE("RSRenderServiceStub::CREATE_CONNECTION Read remoteObj failed!");
59 ret = ERR_NULL_OBJECT;
60 break;
61 }
62
63 if (!remoteObj->IsProxyObject()) {
64 ret = ERR_UNKNOWN_OBJECT;
65 break;
66 }
67
68 auto token = iface_cast<RSIConnectionToken>(remoteObj);
69 auto newConn = CreateConnection(token);
70 if (newConn != nullptr) {
71 if (!reply.WriteBool(true)) {
72 RS_LOGE("RSRenderServiceStub::CREATE_CONNECTION Write failed!");
73 ret = ERR_INVALID_REPLY;
74 break;
75 }
76 if (!reply.WriteRemoteObject(newConn->AsObject())) {
77 RS_LOGE("RSRenderServiceStub::CREATE_CONNECTION Write remoteObj failed!");
78 ret = ERR_INVALID_REPLY;
79 }
80 } else {
81 if (!reply.WriteBool(false)) {
82 RS_LOGE("RSRenderServiceStub::CREATE_CONNECTION Write failed and newConn == nullptr");
83 ret = ERR_INVALID_REPLY;
84 }
85 }
86 break;
87 }
88 default: {
89 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
90 }
91 }
92
93 return ret;
94 }
95
96 const RSInterfaceCodeSecurityManager RSRenderServiceStub::securityManager_ = \
97 RSInterfaceCodeSecurityManager::CreateInstance<RSIRenderServiceInterfaceCodeAccessVerifier>();
98 } // namespace Rosen
99 } // namespace OHOS
100