• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2021-2023 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 "account_proxy.h"
17 #include <ipc_types.h>
18 #include <string_ex.h>
19 #include "account_error_no.h"
20 #include "account_info_parcel.h"
21 #include "account_log_wrapper.h"
22 
23 namespace OHOS {
24 namespace AccountSA {
UpdateOhosAccountInfo(const std::string & accountName,const std::string & uid,const std::string & eventStr)25 bool AccountProxy::UpdateOhosAccountInfo(
26     const std::string &accountName, const std::string &uid, const std::string &eventStr)
27 {
28     MessageParcel data;
29     MessageParcel reply;
30     MessageOption option;
31 
32     if (!data.WriteInterfaceToken(GetDescriptor())) {
33         ACCOUNT_LOGE("Write descriptor failed!");
34         return false;
35     }
36     if (!data.WriteString16(Str8ToStr16(accountName))) {
37         ACCOUNT_LOGE("Write accountName failed!");
38         return false;
39     }
40     if (!data.WriteString16(Str8ToStr16(uid))) {
41         ACCOUNT_LOGE("Write uid failed!");
42         return false;
43     }
44     if (!data.WriteString16(Str8ToStr16(eventStr))) {
45         ACCOUNT_LOGE("Write eventStr failed!");
46         return false;
47     }
48 
49     auto ret = Remote()->SendRequest(UPDATE_OHOS_ACCOUNT_INFO, data, reply, option);
50     if (ret != ERR_NONE) {
51         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
52         return false;
53     }
54 
55     std::int32_t result = ERR_OK;
56     if (!reply.ReadInt32(result)) {
57         ACCOUNT_LOGE("reply ReadInt32 failed");
58         return false;
59     }
60 
61     if (result != ERR_OK) {
62         ACCOUNT_LOGE("UpdateOhosAccountInfo failed: %{public}d", result);
63         return false;
64     }
65 
66     ACCOUNT_LOGD("UpdateOhosAccountInfo exit");
67     return true;
68 }
69 
SetOhosAccountInfo(const OhosAccountInfo & ohosAccountInfo,const std::string & eventStr)70 std::int32_t AccountProxy::SetOhosAccountInfo(const OhosAccountInfo &ohosAccountInfo, const std::string &eventStr)
71 {
72     MessageParcel data;
73     MessageParcel reply;
74     MessageOption option;
75 
76     if (!data.WriteInterfaceToken(GetDescriptor())) {
77         ACCOUNT_LOGE("Write descriptor failed!");
78         return ERR_ACCOUNT_ZIDL_WRITE_PARCEL_DATA_ERROR;
79     }
80     if (!WriteOhosAccountInfo(data, ohosAccountInfo)) {
81         ACCOUNT_LOGE("Write ohosAccountInfo failed!");
82         return ERR_ACCOUNT_ZIDL_WRITE_PARCEL_DATA_ERROR;
83     }
84     if (!data.WriteString16(Str8ToStr16(eventStr))) {
85         ACCOUNT_LOGE("Write eventStr failed!");
86         return ERR_ACCOUNT_ZIDL_WRITE_PARCEL_DATA_ERROR;
87     }
88     auto ret = Remote()->SendRequest(SET_OHOS_ACCOUNT_INFO, data, reply, option);
89     if (ret != ERR_NONE) {
90         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
91         return ret;
92     }
93 
94     std::int32_t result = ERR_OK;
95     if (!reply.ReadInt32(result)) {
96         ACCOUNT_LOGE("reply ReadInt32 failed");
97         return ERR_ACCOUNT_ZIDL_READ_RESULT_ERROR;
98     }
99 
100     if (result != ERR_OK) {
101         ACCOUNT_LOGE("SetOhosAccountInfo failed: %{public}d", result);
102     }
103     ACCOUNT_LOGD("SetOhosAccountInfo exit");
104     return result;
105 }
106 
QueryOhosAccountInfo(void)107 std::pair<bool, OhosAccountInfo> AccountProxy::QueryOhosAccountInfo(void)
108 {
109     MessageParcel data;
110     MessageParcel reply;
111     MessageOption option;
112 
113     if (!data.WriteInterfaceToken(GetDescriptor())) {
114         ACCOUNT_LOGE("Write descriptor failed");
115         return std::make_pair(false, OhosAccountInfo());
116     }
117 
118     auto ret = Remote()->SendRequest(QUERY_OHOS_ACCOUNT_INFO, data, reply, option);
119     if (ret != ERR_NONE) {
120         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
121         return std::make_pair(false, OhosAccountInfo());
122     }
123 
124     std::u16string name = reply.ReadString16();
125     std::u16string uid = reply.ReadString16();
126     std::int32_t status = reply.ReadInt32();
127     ACCOUNT_LOGD("QueryOhosAccountInfo exit");
128     return std::make_pair(true, OhosAccountInfo(Str16ToStr8(name), Str16ToStr8(uid), status));
129 }
130 
GetOhosAccountInfo(OhosAccountInfo & ohosAccountInfo)131 ErrCode AccountProxy::GetOhosAccountInfo(OhosAccountInfo &ohosAccountInfo)
132 {
133     MessageParcel data;
134     MessageParcel reply;
135     MessageOption option;
136 
137     if (!data.WriteInterfaceToken(GetDescriptor())) {
138         ACCOUNT_LOGE("Write descriptor failed");
139         return ERR_ACCOUNT_ZIDL_WRITE_DESCRIPTOR_ERROR;
140     }
141 
142     auto ret = Remote()->SendRequest(GET_OHOS_ACCOUNT_INFO, data, reply, option);
143     if (ret != ERR_NONE) {
144         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
145         return ret;
146     }
147     ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
148     if (ret != ERR_OK) {
149         return ret;
150     }
151 
152     ACCOUNT_LOGD("QueryOhosAccountInfo exit");
153     return ERR_OK;
154 }
155 
GetOhosAccountInfoByUserId(int32_t userId,OhosAccountInfo & ohosAccountInfo)156 ErrCode AccountProxy::GetOhosAccountInfoByUserId(int32_t userId, OhosAccountInfo &ohosAccountInfo)
157 {
158     MessageParcel data;
159     if (!data.WriteInterfaceToken(GetDescriptor())) {
160         ACCOUNT_LOGE("Write descriptor failed");
161         return ERR_ACCOUNT_ZIDL_WRITE_PARCEL_DATA_ERROR;
162     }
163     if (!data.WriteInt32(userId)) {
164         ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
165         return ERR_ACCOUNT_ZIDL_WRITE_PARCEL_DATA_ERROR;
166     }
167     MessageParcel reply;
168     MessageOption option;
169     auto ret = Remote()->SendRequest(GET_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply, option);
170     if (ret != ERR_NONE) {
171         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
172         return ret;
173     }
174     ret = ReadOhosAccountInfo(reply, ohosAccountInfo);
175     if (ret != ERR_OK) {
176         return ret;
177     }
178     return ERR_OK;
179 }
180 
QueryOhosAccountInfoByUserId(std::int32_t userId)181 std::pair<bool, OhosAccountInfo> AccountProxy::QueryOhosAccountInfoByUserId(std::int32_t userId)
182 {
183     MessageParcel data;
184     MessageParcel reply;
185     MessageOption option;
186 
187     if (!data.WriteInterfaceToken(GetDescriptor())) {
188         ACCOUNT_LOGE("Write descriptor failed");
189         return std::make_pair(false, OhosAccountInfo());
190     }
191 
192     if (!data.WriteInt32(userId)) {
193         ACCOUNT_LOGE("failed to write int for userId %{public}d.", userId);
194         return std::make_pair(false, OhosAccountInfo());
195     }
196 
197     auto ret = Remote()->SendRequest(QUERY_OHOS_ACCOUNT_INFO_BY_USER_ID, data, reply, option);
198     if (ret != ERR_NONE) {
199         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
200         return std::make_pair(false, OhosAccountInfo());
201     }
202 
203     std::u16string name;
204     std::u16string uid;
205     std::int32_t status;
206     if ((!reply.ReadString16(name)) || (!reply.ReadString16(uid)) || (!reply.ReadInt32(status))) {
207         ACCOUNT_LOGE("failed to read from parcel");
208         return std::make_pair(false, OhosAccountInfo());
209     }
210     return std::make_pair(true, OhosAccountInfo(Str16ToStr8(name), Str16ToStr8(uid), status));
211 }
212 
QueryDeviceAccountId(std::int32_t & accountId)213 std::int32_t AccountProxy::QueryDeviceAccountId(std::int32_t &accountId)
214 {
215     MessageParcel data;
216     MessageParcel reply;
217     MessageOption option;
218 
219     if (!data.WriteInterfaceToken(GetDescriptor())) {
220         ACCOUNT_LOGE("Write descriptor failed");
221         return ERR_ACCOUNT_ZIDL_WRITE_DESCRIPTOR_ERROR;
222     }
223 
224     auto ret = Remote()->SendRequest(QUERY_DEVICE_ACCOUNT_ID, data, reply, option);
225     if (ret != ERR_NONE) {
226         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
227         return ERR_ACCOUNT_ZIDL_ACCOUNT_SEND_REQUEST_ERROR;
228     }
229 
230     accountId = reply.ReadInt32();
231 
232     return ERR_OK;
233 }
234 
GetAppAccountService()235 sptr<IRemoteObject> AccountProxy::GetAppAccountService()
236 {
237     MessageParcel data;
238     MessageParcel reply;
239     MessageOption option;
240 
241     if (!data.WriteInterfaceToken(GetDescriptor())) {
242         ACCOUNT_LOGE("Write descriptor failed");
243         return nullptr;
244     }
245 
246     auto ret = Remote()->SendRequest(GET_APP_ACCOUNT_SERVICE, data, reply, option);
247     if (ret != ERR_NONE) {
248         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
249         return nullptr;
250     }
251 
252     return reply.ReadRemoteObject();
253 }
254 
GetOsAccountService()255 sptr<IRemoteObject> AccountProxy::GetOsAccountService()
256 {
257     MessageParcel data;
258     MessageParcel reply;
259     MessageOption option;
260 
261     if (!data.WriteInterfaceToken(GetDescriptor())) {
262         ACCOUNT_LOGE("Write descriptor failed");
263         return nullptr;
264     }
265 
266     auto ret = Remote()->SendRequest(GET_OS_ACCOUNT_SERVICE, data, reply, option);
267     if (ret != ERR_NONE) {
268         ACCOUNT_LOGE("SendRequest failed %{public}d", ret);
269         return nullptr;
270     }
271 
272     return reply.ReadRemoteObject();
273 }
274 
GetAccountIAMService()275 sptr<IRemoteObject> AccountProxy::GetAccountIAMService()
276 {
277     MessageParcel data;
278     MessageParcel reply;
279     MessageOption option;
280 
281     if (!data.WriteInterfaceToken(GetDescriptor())) {
282         ACCOUNT_LOGE("Write descriptor failed");
283         return nullptr;
284     }
285 
286     auto ret = Remote()->SendRequest(GET_ACCOUNT_IAM_SERVICE, data, reply, option);
287     if (ret != ERR_NONE) {
288         ACCOUNT_LOGE("SendRequest failed %d", ret);
289         return nullptr;
290     }
291 
292     return reply.ReadRemoteObject();
293 }
294 
GetDomainAccountService()295 sptr<IRemoteObject> AccountProxy::GetDomainAccountService()
296 {
297     MessageParcel data;
298     if (!data.WriteInterfaceToken(GetDescriptor())) {
299         ACCOUNT_LOGE("Write descriptor failed");
300         return nullptr;
301     }
302     MessageParcel reply;
303     MessageOption option;
304     auto ret = Remote()->SendRequest(GET_DOMAIN_ACCOUNT_SERVICE, data, reply, option);
305     if (ret != ERR_NONE) {
306         ACCOUNT_LOGE("SendRequest failed %d", ret);
307         return nullptr;
308     }
309     return reply.ReadRemoteObject();
310 }
311 }  // namespace AccountSA
312 }  // namespace OHOS
313