1 /*
2 * Copyright (c) 2021-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 "app_account_event_proxy.h"
17
18 #include <thread>
19 #include "account_constants.h"
20 #include "account_error_no.h"
21 #include "account_hisysevent_adapter.h"
22 #include "account_log_wrapper.h"
23
24 namespace OHOS {
25 namespace AccountSA {
AppAccountEventProxy(const sptr<IRemoteObject> & object)26 AppAccountEventProxy::AppAccountEventProxy(const sptr<IRemoteObject> &object) : IRemoteProxy<IAppAccountEvent>(object)
27 {}
28
~AppAccountEventProxy()29 AppAccountEventProxy::~AppAccountEventProxy()
30 {}
31
OnAccountsChanged(const std::vector<AppAccountInfo> & accounts,const std::string & ownerName)32 void AppAccountEventProxy::OnAccountsChanged(const std::vector<AppAccountInfo> &accounts, const std::string &ownerName)
33 {
34 MessageParcel data;
35 MessageParcel reply;
36
37 if (!data.WriteInterfaceToken(GetDescriptor())) {
38 ACCOUNT_LOGE("failed to write descriptor!");
39 return;
40 }
41
42 if (!WriteParcelableVector(accounts, data)) {
43 ACCOUNT_LOGE("failed to write WriteVector accounts");
44 return;
45 }
46
47 if (!data.WriteString(ownerName)) {
48 ACCOUNT_LOGE("failed to write string for str1 %{public}s", ownerName.c_str());
49 return;
50 }
51
52 ErrCode result = SendRequest(AppAccountEventInterfaceCode::ACCOUNT_CHANGED, data, reply);
53 if (result != ERR_OK) {
54 ACCOUNT_LOGE("SendRequest failed! error code %{public}d.", result);
55 REPORT_APP_ACCOUNT_FAIL("", "", Constants::OPERATION_EVENT_PUBLISH,
56 result, "Send OnAccountsChanged failed.");
57 return;
58 }
59 }
60
SendRequest(AppAccountEventInterfaceCode code,MessageParcel & data,MessageParcel & reply)61 ErrCode AppAccountEventProxy::SendRequest(AppAccountEventInterfaceCode code, MessageParcel &data, MessageParcel &reply)
62 {
63 sptr<IRemoteObject> remoteEvent = Remote();
64 if (remoteEvent == nullptr) {
65 ACCOUNT_LOGE("remote is nullptr, code = %{public}d", code);
66 return ERR_ACCOUNT_COMMON_NULL_PTR_ERROR;
67 }
68 int32_t retryTimes = 0;
69 int32_t result;
70 MessageOption option(MessageOption::TF_SYNC);
71 while (retryTimes < Constants::MAX_RETRY_TIMES) {
72 result = remoteEvent->SendRequest(static_cast<uint32_t>(code), data, reply, option);
73 if (result == ERR_OK || (result != Constants::E_IPC_ERROR &&
74 result != Constants::E_IPC_SA_DIED)) {
75 break;
76 }
77 retryTimes++;
78 ACCOUNT_LOGE("Failed to SendRequest, code = %{public}d, retryTimes = %{public}d",
79 result, retryTimes);
80 std::this_thread::sleep_for(std::chrono::milliseconds(Constants::DELAY_FOR_EXCEPTION));
81 }
82 if (result != ERR_OK) {
83 return ERR_APPACCOUNT_KIT_SEND_REQUEST;
84 }
85 return ERR_OK;
86 }
87
88 template<typename T>
WriteParcelableVector(const std::vector<T> & parcelableVector,Parcel & data)89 bool AppAccountEventProxy::WriteParcelableVector(const std::vector<T> &parcelableVector, Parcel &data)
90 {
91 if (!data.WriteUint32(parcelableVector.size())) {
92 ACCOUNT_LOGE("failed to WriteInt32 for parcelableVector.size()");
93 return false;
94 }
95
96 for (const auto &parcelable : parcelableVector) {
97 if (!data.WriteParcelable(&parcelable)) {
98 ACCOUNT_LOGE("failed to WriteParcelable for parcelable");
99 return false;
100 }
101 }
102
103 return true;
104 }
105 } // namespace AccountSA
106 } // namespace OHOS
107