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 constexpr unsigned int DVSYNC_ANIMATION_LIST_SIZE_MAX = 20;
28 }
29
OnRemoteRequest(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)30 int32_t VSyncConnectionStub::OnRemoteRequest(uint32_t code, MessageParcel &data,
31 MessageParcel &reply, MessageOption &option)
32 {
33 auto remoteDescriptor = data.ReadInterfaceToken();
34 if (GetDescriptor() != remoteDescriptor) {
35 return ERR_INVALID_STATE;
36 }
37
38 switch (code) {
39 case IVSYNC_CONNECTION_REQUEST_NEXT_VSYNC: {
40 RequestNextVSync();
41 break;
42 }
43 case IVSYNC_CONNECTION_GET_RECEIVE_FD: {
44 int32_t fd = -1;
45 int32_t ret = GetReceiveFd(fd);
46 if (!reply.WriteInt32(ret)) {
47 VLOGE("IVSYNC_CONNECTION_GET_RECEIVE_FD Write ret failed");
48 return VSYNC_ERROR_INVALID_ARGUMENTS;
49 }
50 if (ret != VSYNC_ERROR_OK) {
51 // check add log
52 return ret;
53 }
54 if (!reply.WriteFileDescriptor(fd)) {
55 VLOGE("IVSYNC_CONNECTION_GET_RECEIVE_FD Write fd failed");
56 return VSYNC_ERROR_INVALID_ARGUMENTS;
57 }
58 break;
59 }
60 case IVSYNC_CONNECTION_SET_RATE: {
61 int32_t rate{0};
62 if (!data.ReadInt32(rate)) {
63 VLOGE("IVSYNC_CONNECTION_SET_RATE Read rate failed");
64 return VSYNC_ERROR_INVALID_ARGUMENTS;
65 }
66 int32_t ret = SetVSyncRate(rate);
67 if (!reply.WriteInt32(ret)) {
68 VLOGE("IVSYNC_CONNECTION_SET_RATE Write ret failed");
69 return VSYNC_ERROR_INVALID_ARGUMENTS;
70 }
71 if (ret != VSYNC_ERROR_OK) {
72 // check add log
73 return ret;
74 }
75 break;
76 }
77 case IVSYNC_CONNECTION_DESTROY: {
78 int32_t ret = Destroy();
79 if (!reply.WriteInt32(ret)) {
80 VLOGE("IVSYNC_CONNECTION_DESTROY Write ret failed");
81 return VSYNC_ERROR_INVALID_ARGUMENTS;
82 }
83 return ret;
84 }
85 case IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH: {
86 bool dvsyncOn{false};
87 if (!data.ReadBool(dvsyncOn)) {
88 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH Read dvsyncOn failed");
89 return VSYNC_ERROR_INVALID_ARGUMENTS;
90 }
91 int32_t ret = SetUiDvsyncSwitch(dvsyncOn);
92 if (!reply.WriteInt32(ret)) {
93 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_SWITCH Write ret failed");
94 return VSYNC_ERROR_INVALID_ARGUMENTS;
95 }
96 return ret;
97 }
98 case IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG: {
99 if (!CheckCallingPermission()) {
100 return VSYNC_ERROR_INVALID_ARGUMENTS;
101 }
102 int32_t bufferCount{0};
103 if (!data.ReadInt32(bufferCount)) {
104 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Read bufferCount failed");
105 return VSYNC_ERROR_INVALID_ARGUMENTS;
106 }
107 bool compositeSceneEnable{false};
108 if (!data.ReadBool(compositeSceneEnable)) {
109 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Read compositeSceneEnable failed");
110 return VSYNC_ERROR_INVALID_ARGUMENTS;
111 }
112 bool nativeDelayEnable{false};
113 if (!data.ReadBool(nativeDelayEnable)) {
114 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Read nativeDelayEnable failed");
115 return VSYNC_ERROR_INVALID_ARGUMENTS;
116 }
117 std::vector<std::string> rsDvsyncAnimationList = {};
118 if (!data.ReadStringVector(&rsDvsyncAnimationList)) {
119 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Read rsDvsyncAnimationList failed");
120 return VSYNC_ERROR_INVALID_ARGUMENTS;
121 }
122 if (rsDvsyncAnimationList.size() > DVSYNC_ANIMATION_LIST_SIZE_MAX) {
123 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG DvsyncAnimationList size exceeds maximum allowed size");
124 return VSYNC_ERROR_INVALID_ARGUMENTS;
125 }
126 int32_t ret = SetUiDvsyncConfig(bufferCount, compositeSceneEnable,
127 nativeDelayEnable, rsDvsyncAnimationList);
128 if (!reply.WriteInt32(ret)) {
129 VLOGE("IVSYNC_CONNECTION_SET_UI_DVSYNC_CONFIG Write ret failed");
130 return VSYNC_ERROR_INVALID_ARGUMENTS;
131 }
132 return ret;
133 }
134 case IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH: {
135 bool dvsyncOn{false};
136 if (!data.ReadBool(dvsyncOn)) {
137 VLOGE("IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH Read dvsyncOn failed");
138 return VSYNC_ERROR_INVALID_ARGUMENTS;
139 }
140 int32_t ret = SetNativeDVSyncSwitch(dvsyncOn);
141 if (!reply.WriteInt32(ret)) {
142 VLOGE("IVSYNC_CONNECTION_SET_NATIVE_DVSYNC_SWITCH Write ret failed");
143 return VSYNC_ERROR_INVALID_ARGUMENTS;
144 }
145 return ret;
146 }
147 default: {
148 // check add log
149 return IPCObjectStub::OnRemoteRequest(code, data, reply, option);
150 }
151 }
152 return 0;
153 }
154
CheckCallingPermission()155 bool VSyncConnectionStub::CheckCallingPermission()
156 {
157 Security::AccessToken::AccessTokenID tokenId = OHOS::IPCSkeleton::GetCallingTokenID();
158 Security::AccessToken::AccessTokenID rssToken =
159 Security::AccessToken::AccessTokenKit::GetNativeTokenId(RSS_PROCESS_NAME);
160 if (tokenId != rssToken) {
161 VLOGE("CheckPermissionFailed, calling process illegal");
162 return false;
163 }
164 return true;
165 }
166 } // namespace Rosen
167 } // namespace OHOS
168