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