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