• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 (!reply.WriteInt32(ret)) {
46                 VLOGE("IVSYNC_CONNECTION_GET_RECEIVE_FD Write ret failed");
47                 return VSYNC_ERROR_INVALID_ARGUMENTS;
48             }
49             if (ret != VSYNC_ERROR_OK) {
50                 // check add log
51                 return ret;
52             }
53             if (!reply.WriteFileDescriptor(fd)) {
54                 VLOGE("IVSYNC_CONNECTION_GET_RECEIVE_FD Write fd failed");
55                 return VSYNC_ERROR_INVALID_ARGUMENTS;
56             }
57             break;
58         }
59         case IVSYNC_CONNECTION_SET_RATE: {
60             int32_t rate{0};
61             if (!data.ReadInt32(rate)) {
62                 VLOGE("IVSYNC_CONNECTION_SET_RATE Read rate failed");
63                 return VSYNC_ERROR_INVALID_ARGUMENTS;
64             }
65             int32_t ret = SetVSyncRate(rate);
66             if (!reply.WriteInt32(ret)) {
67                 VLOGE("IVSYNC_CONNECTION_SET_RATE Write ret failed");
68                 return VSYNC_ERROR_INVALID_ARGUMENTS;
69             }
70             if (ret != VSYNC_ERROR_OK) {
71                 // check add log
72                 return ret;
73             }
74             break;
75         }
76         case IVSYNC_CONNECTION_DESTROY: {
77             int32_t ret = Destroy();
78             if (!reply.WriteInt32(ret)) {
79                 VLOGE("IVSYNC_CONNECTION_DESTROY Write ret failed");
80                 return VSYNC_ERROR_INVALID_ARGUMENTS;
81             }
82             return ret;
83         }
84         case IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH: {
85             bool dvsyncOn{false};
86             if (!data.ReadBool(dvsyncOn)) {
87                 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH Read dvsyncOn failed");
88                 return VSYNC_ERROR_INVALID_ARGUMENTS;
89             }
90             int32_t ret = SetUiDvsyncSwitch(dvsyncOn);
91             if (!reply.WriteInt32(ret)) {
92                 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH Write ret failed");
93                 return VSYNC_ERROR_INVALID_ARGUMENTS;
94             }
95             return ret;
96         }
97         case IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG: {
98             if (!CheckCallingPermission()) {
99                 return VSYNC_ERROR_INVALID_ARGUMENTS;
100             }
101             int32_t bufferCount{0};
102             if (!data.ReadInt32(bufferCount)) {
103                 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Read bufferCount failed");
104                 return VSYNC_ERROR_INVALID_ARGUMENTS;
105             }
106             int32_t ret = SetUiDvsyncConfig(bufferCount);
107             if (!reply.WriteInt32(ret)) {
108                 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Write ret failed");
109                 return VSYNC_ERROR_INVALID_ARGUMENTS;
110             }
111             return ret;
112         }
113         case IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH: {
114             bool dvsyncOn{false};
115             if (!data.ReadBool(dvsyncOn)) {
116                 VLOGE("IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH Read dvsyncOn failed");
117                 return VSYNC_ERROR_INVALID_ARGUMENTS;
118             }
119             int32_t ret = SetNativeDVSyncSwitch(dvsyncOn);
120             if (!reply.WriteInt32(ret)) {
121                 VLOGE("IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH Write ret failed");
122                 return VSYNC_ERROR_INVALID_ARGUMENTS;
123             }
124             return ret;
125         }
126         default: {
127             // check add log
128             return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
129         }
130     }
131     return 0;
132 }
133 
CheckCallingPermission()134 bool VSyncConnectionStub::CheckCallingPermission()
135 {
136     Security::AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
137     Security::AccessToken::AccessTokenID rssToken =
138         Security::AccessToken::AccessTokenKit::GetNativeTokenId(RSS_PROCESS_NAME);
139     if (tokenId != rssToken) {
140         VLOGE("CheckPermissionFailed, calling process illegal");
141         return false;
142     }
143     return true;
144 }
145 } // namespace Rosen
146 } // namespace OHOS
147