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 "vsync_connection_stub.h"
17 #include <unistd.h>
18 #include "graphic_common.h"
19 #include "accesstoken_kit.h"
20 #include "ipc_skeleton.h"
21 #include "vsync_log.h"
22 
23 namespace OHOS {
24 namespace Rosen {
25 namespace {
26     const std::string RSS_PROCESS_NAME = "resource_schedule_service";
27 }
28 
OnRemoteRequest(uint32_t code, MessageParcel &data, MessageParcel &reply, MessageOption &option)29 int32_t VSyncConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
30     MessageParcel &reply, MessageOption &option)
31 {
32     auto remoteDescriptor = data.ReadInterfaceToken();
33     if (GetDescriptor() != remoteDescriptor) {
34         return ERR_INVALID_STATE;
35     }
36 
37     switch (code) {
38         case IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC: {
39             RequestNextVSync();
40             break;
41         }
42         case IVSYNC_CONNECTION_GET_RECEIVE_FD: {
43             int32_t fd = -1;
44             int32_t ret = GetReceiveFd(fd);
45             reply.WriteInt32(ret);
46             if (ret != VSYNC_ERROR_OK) {
47                 // check add log
48                 return ret;
49             }
50             reply.WriteFileDescriptor(fd);
51             break;
52         }
53         case IVSYNC_CONNECTION_SET_RATE: {
54             int32_t rate = data.ReadInt32();
55             int32_t ret = SetVSyncRate(rate);
56             reply.WriteInt32(ret);
57             if (ret != VSYNC_ERROR_OK) {
58                 // check add log
59                 return ret;
60             }
61             break;
62         }
63         case IVSYNC_CONNECTION_DESTROY: {
64             int32_t ret = Destroy();
65             reply.WriteInt32(ret);
66             return ret;
67         }
68         case IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH: {
69             auto dvsyncOn = data.ReadBool();
70             int32_t ret = SetUiDvsyncSwitch(dvsyncOn);
71             reply.WriteInt32(ret);
72             return ret;
73         }
74         case IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG: {
75             if (!CheckCallingPermission()) {
76                 return VSYNC_ERROR_UNKOWN;
77             }
78             int32_t bufferCount = data.ReadInt32();
79             int32_t ret = SetUiDvsyncConfig(bufferCount);
80             reply.WriteInt32(ret);
81             return ret;
82         }
83         case IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH: {
84             auto dvsyncOn = data.ReadBool();
85             int32_t ret = SetNativeDVSyncSwitch(dvsyncOn);
86             reply.WriteInt32(ret);
87             return ret;
88         }
89         default: {
90             // check add log
91             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
92         }
93     }
94     return 0;
95 }
96 
CheckCallingPermission()97 bool VSyncConnectionStub::CheckCallingPermission()
98 {
99     Security::AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
100     Security::AccessToken::AccessTokenID rssToken =
101         Security::AccessToken::AccessTokenKit::GetNativeTokenId(RSS_PROCESS_NAME);
102     if (tokenId != rssToken) {
103         VLOGE("CheckPermissionFailed, calling process illegal");
104         return false;
105     }
106     return true;
107 }
108 } // namespace Rosen
109 } // namespace OHOS
110