1 /*
2 * Copyright (c) 2022 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 "token_sync_manager_proxy.h"
17
18 #include "accesstoken_log.h"
19 #include "parcel.h"
20 #include "string_ex.h"
21
22 namespace OHOS {
23 namespace Security {
24 namespace AccessToken {
25 namespace {
26 static constexpr OHOS::HiviewDFX::HiLogLabel LABEL = {LOG_CORE, SECURITY_DOMAIN_ACCESSTOKEN, "TokenSyncManagerProxy"};
27 }
28
TokenSyncManagerProxy(const sptr<IRemoteObject> & impl)29 TokenSyncManagerProxy::TokenSyncManagerProxy(const sptr<IRemoteObject>& impl) : IRemoteProxy<ITokenSyncManager>(impl)
30 {}
31
~TokenSyncManagerProxy()32 TokenSyncManagerProxy::~TokenSyncManagerProxy()
33 {}
34
GetRemoteHapTokenInfo(const std::string & deviceID,AccessTokenID tokenID)35 int TokenSyncManagerProxy::GetRemoteHapTokenInfo(const std::string& deviceID, AccessTokenID tokenID)
36 {
37 MessageParcel data;
38 data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor());
39 if (!data.WriteString(deviceID)) {
40 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write deviceID");
41 return TOKEN_SYNC_PARAMS_INVALID;
42 }
43 if (!data.WriteUint32(tokenID)) {
44 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
45 return TOKEN_SYNC_PARAMS_INVALID;
46 }
47
48 MessageParcel reply;
49 MessageOption option(MessageOption::TF_SYNC);
50 sptr<IRemoteObject> remote = Remote();
51 if (remote == nullptr) {
52 ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
53 return TOKEN_SYNC_IPC_ERROR;
54 }
55 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
56 TokenSyncInterfaceCode::GET_REMOTE_HAP_TOKEN_INFO), data, reply, option);
57 if (requestResult != NO_ERROR) {
58 ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
59 return TOKEN_SYNC_IPC_ERROR;
60 }
61
62 int32_t result = reply.ReadInt32();
63 ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
64 return result;
65 }
66
DeleteRemoteHapTokenInfo(AccessTokenID tokenID)67 int TokenSyncManagerProxy::DeleteRemoteHapTokenInfo(AccessTokenID tokenID)
68 {
69 MessageParcel data;
70 data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor());
71 if (!data.WriteUint32(tokenID)) {
72 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenID");
73 return TOKEN_SYNC_PARAMS_INVALID;
74 }
75
76 MessageParcel reply;
77 MessageOption option(MessageOption::TF_SYNC);
78 sptr<IRemoteObject> remote = Remote();
79 if (remote == nullptr) {
80 ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
81 return TOKEN_SYNC_IPC_ERROR;
82 }
83 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
84 TokenSyncInterfaceCode::DELETE_REMOTE_HAP_TOKEN_INFO), data, reply, option);
85 if (requestResult != NO_ERROR) {
86 ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
87 return TOKEN_SYNC_IPC_ERROR;
88 }
89
90 int32_t result = reply.ReadInt32();
91 ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
92 return result;
93 }
94
UpdateRemoteHapTokenInfo(const HapTokenInfoForSync & tokenInfo)95 int TokenSyncManagerProxy::UpdateRemoteHapTokenInfo(const HapTokenInfoForSync& tokenInfo)
96 {
97 MessageParcel data;
98 data.WriteInterfaceToken(ITokenSyncManager::GetDescriptor());
99
100 HapTokenInfoForSyncParcel tokenInfoParcel;
101 tokenInfoParcel.hapTokenInfoForSyncParams = tokenInfo;
102
103 if (!data.WriteParcelable(&tokenInfoParcel)) {
104 ACCESSTOKEN_LOG_ERROR(LABEL, "Failed to write tokenInfo");
105 return TOKEN_SYNC_PARAMS_INVALID;
106 }
107
108 MessageParcel reply;
109 MessageOption option(MessageOption::TF_SYNC);
110 sptr<IRemoteObject> remote = Remote();
111 if (remote == nullptr) {
112 ACCESSTOKEN_LOG_ERROR(LABEL, "remote service null.");
113 return TOKEN_SYNC_IPC_ERROR;
114 }
115 int32_t requestResult = remote->SendRequest(static_cast<uint32_t>(
116 TokenSyncInterfaceCode::UPDATE_REMOTE_HAP_TOKEN_INFO), data, reply, option);
117 if (requestResult != NO_ERROR) {
118 ACCESSTOKEN_LOG_ERROR(LABEL, "send request fail, result: %{public}d", requestResult);
119 return TOKEN_SYNC_IPC_ERROR;
120 }
121
122 int32_t result = reply.ReadInt32();
123 ACCESSTOKEN_LOG_DEBUG(LABEL, "get result from server data = %{public}d", result);
124 return result;
125 }
126 } // namespace AccessToken
127 } // namespace Security
128 } // namespace OHOS
129