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             if (ret != VSYNC_ERROR_OK) {
46                 // check add log
47                 return ret;
48             }
49             reply.WriteFileDescriptor(fd);
50             break;
51         }
52         case IVSYNC_CONNECTION_SET_RATE: {
53             int32_t rate = data.ReadInt32();
54             int32_t ret = SetVSyncRate(rate);
55             if (ret != VSYNC_ERROR_OK) {
56                 // check add log
57                 return ret;
58             }
59             break;
60         }
61         case IVSYNC_CONNECTION_DESTROY: {
62             return Destroy();
63         }
64         case IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH: {
65             auto dvsyncOn = data.ReadBool();
66             return SetUiDvsyncSwitch(dvsyncOn);
67         }
68         case IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG: {
69             if (!CheckCallingPermission()) {
70                 return VSYNC_ERROR_UNKOWN;
71             }
72             int32_t bufferCount = data.ReadInt32();
73             return SetUiDvsyncConfig(bufferCount);
74         }
75         case IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH: {
76             auto dvsyncOn = data.ReadBool();
77             int32_t ret = SetNativeDVSyncSwitch(dvsyncOn);
78             reply.WriteInt32(ret);
79             return ret;
80         }
81         default: {
82             // check add log
83             return VSYNC_ERROR_INVALID_OPERATING;
84         }
85     }
86     return 0;
87 }
88 
CheckCallingPermission()89 bool VSyncConnectionStub::CheckCallingPermission()
90 {
91     Security::AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
92     Security::AccessToken::AccessTokenID rssToken =
93         Security::AccessToken::AccessTokenKit::GetNativeTokenId(RSS_PROCESS_NAME);
94     if (tokenId != rssToken) {
95         VLOGE("CheckPermissionFailed, calling process illegal");
96         return false;
97     }
98     return true;
99 }
100 } // namespace Rosen
101 } // namespace OHOS
102