1 /*
2 * Copyright (c) 2022-2024 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 "accesstoken_callback_proxys.h"
17
18 #include "access_token.h"
19 #include "accesstoken_common_log.h"
20 #ifdef TOKEN_SYNC_ENABLE
21 #include "hap_token_info_for_sync_parcel.h"
22 #endif // TOKEN_SYNC_ENABLE
23 #include "permission_state_change_info_parcel.h"
24 #ifdef TOKEN_SYNC_ENABLE
25 #include "token_sync_kit_interface.h"
26 #endif // TOKEN_SYNC_ENABLE
27
28 namespace OHOS {
29 namespace Security {
30 namespace AccessToken {
31
PermissionStateChangeCallbackProxy(const sptr<IRemoteObject> & impl)32 PermissionStateChangeCallbackProxy::PermissionStateChangeCallbackProxy(const sptr<IRemoteObject>& impl)
33 : IRemoteProxy<IPermissionStateCallback>(impl) {
34 }
35
~PermissionStateChangeCallbackProxy()36 PermissionStateChangeCallbackProxy::~PermissionStateChangeCallbackProxy()
37 {}
38
PermStateChangeCallback(PermStateChangeInfo & result)39 void PermissionStateChangeCallbackProxy::PermStateChangeCallback(PermStateChangeInfo& result)
40 {
41 MessageParcel data;
42 if (!data.WriteInterfaceToken(IPermissionStateCallback::GetDescriptor())) {
43 LOGE(ATM_DOMAIN, ATM_TAG, "write interfacetoken failed.");
44 return;
45 }
46
47 PermissionStateChangeInfoParcel resultParcel;
48 resultParcel.changeInfo = result;
49 if (!data.WriteParcelable(&resultParcel)) {
50 LOGE(ATM_DOMAIN, ATM_TAG, "WriteParcelable failed.");
51 return;
52 }
53
54 MessageParcel reply;
55 MessageOption option(MessageOption::TF_ASYNC);
56 sptr<IRemoteObject> remote = Remote();
57 if (remote == nullptr) {
58 LOGE(ATM_DOMAIN, ATM_TAG, "Remote service null.");
59 return;
60 }
61 int32_t requestResult = remote->SendRequest(
62 static_cast<uint32_t>(AccesstokenStateChangeInterfaceCode::PERMISSION_STATE_CHANGE), data, reply, option);
63 if (requestResult != NO_ERROR) {
64 LOGE(ATM_DOMAIN, ATM_TAG, "Send request fail, result: %{public}d", requestResult);
65 return;
66 }
67
68 LOGI(ATM_DOMAIN, ATM_TAG, "SendRequest success");
69 }
70
71 #ifdef TOKEN_SYNC_ENABLE
TokenSyncCallbackProxy(const sptr<IRemoteObject> & impl)72 TokenSyncCallbackProxy::TokenSyncCallbackProxy(const sptr<IRemoteObject>& impl)
73 : IRemoteProxy<ITokenSyncCallback>(impl) {
74 }
75
~TokenSyncCallbackProxy()76 TokenSyncCallbackProxy::~TokenSyncCallbackProxy()
77 {}
78
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID)79 int32_t TokenSyncCallbackProxy::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID)
80 {
81 MessageParcel data;
82 if (!data.WriteInterfaceToken(ITokenSyncCallback::GetDescriptor())) {
83 LOGE(ATM_DOMAIN, ATM_TAG, "write interfacetoken failed.");
84 return TOKEN_SYNC_PARAMS_INVALID;
85 }
86
87 if (!data.WriteString(deviceID)) {
88 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write deviceID.");
89 return TOKEN_SYNC_PARAMS_INVALID;
90 }
91 if (!data.WriteUint32(tokenID)) {
92 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write tokenID.");
93 return TOKEN_SYNC_PARAMS_INVALID;
94 }
95
96 MessageParcel reply;
97 MessageOption option(MessageOption::TF_SYNC);
98 sptr<IRemoteObject> remote = Remote();
99 if (remote == nullptr) {
100 LOGE(ATM_DOMAIN, ATM_TAG, "Remote service null.");
101 return TOKEN_SYNC_IPC_ERROR;
102 }
103 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
104 TokenSyncCallbackInterfaceCode::GET_REMOTE_HAP_TOKEN_INFO), data, reply, option);
105 if (requestResult != NO_ERROR) {
106 LOGE(ATM_DOMAIN, ATM_TAG, "Send request fail, result = %{public}d.", requestResult);
107 return TOKEN_SYNC_IPC_ERROR;
108 }
109
110 int32_t result = reply.ReadInt32();
111 LOGI(ATM_DOMAIN, ATM_TAG, "Get result from callback, data = %{public}d.", result);
112 return result;
113 }
114
DeleteRemoteHapTokenInfo(AccessTokenID tokenID)115 int32_t TokenSyncCallbackProxy::DeleteRemoteHapTokenInfo(AccessTokenID tokenID)
116 {
117 MessageParcel data;
118 if (!data.WriteInterfaceToken(ITokenSyncCallback::GetDescriptor())) {
119 LOGE(ATM_DOMAIN, ATM_TAG, "write interfacetoken failed.");
120 return TOKEN_SYNC_PARAMS_INVALID;
121 }
122 if (!data.WriteUint32(tokenID)) {
123 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write tokenID.");
124 return TOKEN_SYNC_PARAMS_INVALID;
125 }
126
127 MessageParcel reply;
128 MessageOption option(MessageOption::TF_SYNC);
129 sptr<IRemoteObject> remote = Remote();
130 if (remote == nullptr) {
131 LOGE(ATM_DOMAIN, ATM_TAG, "Remote service null.");
132 return TOKEN_SYNC_IPC_ERROR;
133 }
134 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
135 TokenSyncCallbackInterfaceCode::DELETE_REMOTE_HAP_TOKEN_INFO), data, reply, option);
136 if (requestResult != NO_ERROR) {
137 LOGE(ATM_DOMAIN, ATM_TAG, "Send request fail, result: %{public}d", requestResult);
138 return TOKEN_SYNC_IPC_ERROR;
139 }
140
141 int32_t result = reply.ReadInt32();
142 LOGI(ATM_DOMAIN, ATM_TAG, "Get result from callback, data = %{public}d", result);
143 return result;
144 }
145
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo)146 int32_t TokenSyncCallbackProxy::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo)
147 {
148 MessageParcel data;
149 if (!data.WriteInterfaceToken(ITokenSyncCallback::GetDescriptor())) {
150 LOGE(ATM_DOMAIN, ATM_TAG, "write interfacetoken failed.");
151 return TOKEN_SYNC_PARAMS_INVALID;
152 }
153
154 HapTokenInfoForSyncParcel tokenInfoParcel;
155 tokenInfoParcel.hapTokenInfoForSyncParams = tokenInfo;
156
157 if (!data.WriteParcelable(&tokenInfoParcel)) {
158 LOGE(ATM_DOMAIN, ATM_TAG, "Failed to write tokenInfo.");
159 return TOKEN_SYNC_PARAMS_INVALID;
160 }
161
162 MessageParcel reply;
163 MessageOption option(MessageOption::TF_SYNC);
164 sptr<IRemoteObject> remote = Remote();
165 if (remote == nullptr) {
166 LOGE(ATM_DOMAIN, ATM_TAG, "Remote service null.");
167 return TOKEN_SYNC_IPC_ERROR;
168 }
169 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
170 TokenSyncCallbackInterfaceCode::UPDATE_REMOTE_HAP_TOKEN_INFO), data, reply, option);
171 if (requestResult != NO_ERROR) {
172 LOGE(ATM_DOMAIN, ATM_TAG, "Send request fail, result = %{public}d", requestResult);
173 return TOKEN_SYNC_IPC_ERROR;
174 }
175
176 int32_t result = reply.ReadInt32();
177 LOGI(ATM_DOMAIN, ATM_TAG, "Get result from callback, data = %{public}d", result);
178 return result;
179 }
180 #endif // TOKEN_SYNC_ENABLE
181 } // namespace AccessToken
182 } // namespace Security
183 } // namespace OHOS
184