1 /*
2  * Copyright (c) 2023-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 "atomic_service_status_callback_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AAFwk {
22 using OHOS::AAFwk::IAtomicServiceStatusCallback;
23 
AtomicServiceStatusCallbackProxy(const sptr<IRemoteObject> & impl)24 AtomicServiceStatusCallbackProxy::AtomicServiceStatusCallbackProxy(const sptr<IRemoteObject>& impl)
25     : IRemoteProxy<IAtomicServiceStatusCallback>(impl)
26 {
27 }
OnInstallFinished(int resultCode,const Want & want,int32_t userId)28 void AtomicServiceStatusCallbackProxy::OnInstallFinished(int resultCode, const Want &want, int32_t userId)
29 {
30     MessageParcel data;
31     MessageParcel reply;
32     MessageOption option;
33 
34     if (!data.WriteInterfaceToken(IAtomicServiceStatusCallback::GetDescriptor())) {
35         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed.");
36         return;
37     }
38 
39     if (!data.WriteInt32(resultCode)) {
40         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write resultCode failed.");
41         return;
42     }
43 
44     if (!data.WriteParcelable(&want)) {
45         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write want failed.");
46         return;
47     }
48 
49     if (!data.WriteInt32(userId)) {
50         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write userId failed.");
51         return;
52     }
53 
54     int32_t error = SendTransactCmd(IAtomicServiceStatusCallbackCmd::ON_FREE_INSTALL_DONE, data,
55         reply, option);
56     if (error != NO_ERROR) {
57         TAG_LOGE(AAFwkTag::ABILITYMGR, "OnFinished fail, error: %{public}d", error);
58         return;
59     }
60 }
61 
OnRemoteInstallFinished(int resultCode,const Want & want,int32_t userId)62 void AtomicServiceStatusCallbackProxy::OnRemoteInstallFinished(int resultCode, const Want &want, int32_t userId)
63 {
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option;
67 
68     if (!data.WriteInterfaceToken(IAtomicServiceStatusCallback::GetDescriptor())) {
69         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write interface token failed.");
70         return;
71     }
72 
73     if (!data.WriteInt32(resultCode)) {
74         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write resultCode failed.");
75         return;
76     }
77 
78     if (!data.WriteParcelable(&want)) {
79         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write want error.");
80         return;
81     }
82 
83     if (!data.WriteInt32(userId)) {
84         TAG_LOGE(AAFwkTag::ABILITYMGR, "Write userId failed.");
85         return;
86     }
87 
88     int32_t error = SendTransactCmd(ON_REMOTE_FREE_INSTALL_DONE, data, reply, option);
89     if (error != NO_ERROR) {
90         TAG_LOGE(AAFwkTag::ABILITYMGR, "OnFinished fail, error: %{public}d", error);
91         return;
92     }
93 }
94 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)95 int32_t AtomicServiceStatusCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
96     MessageParcel &reply, MessageOption &option)
97 {
98     sptr<IRemoteObject> remote = Remote();
99     if (remote == nullptr) {
100         TAG_LOGE(AAFwkTag::ABILITYMGR, "remote object is nullptr.");
101         return ERR_NULL_OBJECT;
102     }
103 
104     int32_t ret = remote->SendRequest(code, data, reply, option);
105     if (ret != NO_ERROR) {
106         TAG_LOGE(AAFwkTag::ABILITYMGR, "SendRequest failed. code is %{public}d, ret is %{public}d.", code, ret);
107         return ret;
108     }
109     return NO_ERROR;
110 }
111 }  // namespace AAFwk
112 }  // namespace OHOS
113