1 /*
2  * Copyright (c) 2022-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 "ability_info_callback_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
AbilityInfoCallbackProxy(const sptr<IRemoteObject> & impl)23 AbilityInfoCallbackProxy::AbilityInfoCallbackProxy(
24     const sptr<IRemoteObject> &impl) : IRemoteProxy<IAbilityInfoCallback>(impl)
25 {}
26 
WriteInterfaceToken(MessageParcel & data)27 bool AbilityInfoCallbackProxy::WriteInterfaceToken(MessageParcel &data)
28 {
29     if (!data.WriteInterfaceToken(AbilityInfoCallbackProxy::GetDescriptor())) {
30         TAG_LOGE(AAFwkTag::APPMGR, "write token failed");
31         return false;
32     }
33     return true;
34 }
35 
NotifyAbilityToken(const sptr<IRemoteObject> token,const Want & want)36 void AbilityInfoCallbackProxy::NotifyAbilityToken(const sptr<IRemoteObject> token, const Want &want)
37 {
38     MessageParcel data;
39     MessageParcel reply;
40     MessageOption option(MessageOption::TF_SYNC);
41     if (!WriteInterfaceToken(data)) {
42         return;
43     }
44 
45     data.WriteRemoteObject(token);
46     data.WriteParcelable(&want);
47     int32_t ret = SendTransactCmd(IAbilityInfoCallback::Notify_ABILITY_TOKEN, data, reply, option);
48     if (ret != NO_ERROR) {
49         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
50         return;
51     }
52 }
53 
NotifyRestartSpecifiedAbility(const sptr<IRemoteObject> & token)54 void AbilityInfoCallbackProxy::NotifyRestartSpecifiedAbility(const sptr<IRemoteObject> &token)
55 {
56     MessageParcel data;
57     MessageParcel reply;
58     MessageOption option(MessageOption::TF_SYNC);
59     if (!WriteInterfaceToken(data)) {
60         return;
61     }
62 
63     data.WriteRemoteObject(token);
64     int32_t ret = SendTransactCmd(IAbilityInfoCallback::Notify_RESTART_SPECIFIED_ABILITY, data, reply, option);
65     if (ret != NO_ERROR) {
66         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest err: %{public}d", ret);
67     }
68 }
69 
NotifyStartSpecifiedAbility(const sptr<IRemoteObject> & callerToken,const Want & want,int requestCode,sptr<Want> & extraParam)70 void AbilityInfoCallbackProxy::NotifyStartSpecifiedAbility(const sptr<IRemoteObject> &callerToken,
71     const Want &want, int requestCode, sptr<Want> &extraParam)
72 {
73     MessageParcel data;
74     MessageParcel reply;
75     MessageOption option(MessageOption::TF_SYNC);
76     if (!WriteInterfaceToken(data)) {
77         return;
78     }
79 
80     data.WriteRemoteObject(callerToken);
81     data.WriteParcelable(&want);
82     data.WriteInt32(requestCode);
83     int32_t ret = SendTransactCmd(IAbilityInfoCallback::Notify_START_SPECIFIED_ABILITY, data, reply, option);
84     if (ret != NO_ERROR) {
85         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest failed, err: %{public}d", ret);
86         return;
87     }
88     sptr<Want> tempWant = reply.ReadParcelable<Want>();
89     if (tempWant != nullptr) {
90         SetExtraParam(tempWant, extraParam);
91     }
92 }
93 
SetExtraParam(const sptr<Want> & want,sptr<Want> & extraParam)94 void AbilityInfoCallbackProxy::SetExtraParam(const sptr<Want> &want, sptr<Want> &extraParam)
95 {
96     if (!want || !extraParam) {
97         TAG_LOGE(AAFwkTag::APPMGR, "invalid param");
98         return;
99     }
100 
101     sptr<IRemoteObject> tempCallBack = want->GetRemoteObject(Want::PARAM_RESV_ABILITY_INFO_CALLBACK);
102     if (tempCallBack == nullptr) {
103         return;
104     }
105     extraParam->SetParam(Want::PARAM_RESV_REQUEST_PROC_CODE,
106         want->GetIntParam(Want::PARAM_RESV_REQUEST_PROC_CODE, 0));
107     extraParam->SetParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE,
108         want->GetIntParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE, 0));
109     extraParam->SetParam(Want::PARAM_RESV_ABILITY_INFO_CALLBACK, tempCallBack);
110 }
111 
NotifyStartAbilityResult(const Want & want,int result)112 void AbilityInfoCallbackProxy::NotifyStartAbilityResult(const Want &want, int result)
113 {
114     MessageParcel data;
115     MessageParcel reply;
116     MessageOption option(MessageOption::TF_ASYNC);
117     if (!WriteInterfaceToken(data)) {
118         return;
119     }
120 
121     data.WriteParcelable(&want);
122     data.WriteInt32(result);
123     int32_t ret = SendTransactCmd(IAbilityInfoCallback::Notify_START_ABILITY_RESULT, data, reply, option);
124     if (ret != NO_ERROR) {
125         TAG_LOGW(AAFwkTag::APPMGR, "err: %{public}d", ret);
126         return;
127     }
128 }
129 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)130 int32_t AbilityInfoCallbackProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
131     MessageParcel &reply, MessageOption &option)
132 {
133     sptr<IRemoteObject> remote = Remote();
134     if (remote == nullptr) {
135         TAG_LOGE(AAFwkTag::APPMGR, "null remote");
136         return ERR_NULL_OBJECT;
137     }
138 
139     return remote->SendRequest(code, data, reply, option);
140 }
141 }  // namespace AppExecFwk
142 }  // namespace OHOS
143