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                 ret = ERR_INVALID_STATE;
52                 break;
53             }
54 
55             auto remoteObj = data.ReadRemoteObject();
56             if (remoteObj == nullptr) {
57                 ret = ERR_NULL_OBJECT;
58                 break;
59             }
60 
61             if (!remoteObj->IsProxyObject()) {
62                 ret = ERR_UNKNOWN_OBJECT;
63                 break;
64             }
65 
66             auto token = iface_cast<RSIConnectionToken>(remoteObj);
67             auto newConn = CreateConnection(token);
68             reply.WriteRemoteObject(newConn->AsObject());
69             break;
70         }
71         default: {
72             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
73         }
74     }
75 
76     return ret;
77 }
78 
79 const RSInterfaceCodeSecurityManager RSRenderServiceStub::securityManager_ = \
80     RSInterfaceCodeSecurityManager::CreateInstance<RSIRenderServiceInterfaceCodeAccessVerifier>();
81 } // namespace Rosen
82 } // namespace OHOS
83