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