1 /*
2 * Copyright (c) 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 "ability_info_callback_proxy.h"
17
18 #include "hilog_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 HILOG_ERROR("write interface 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_ASYNC);
41 if (!WriteInterfaceToken(data)) {
42 return;
43 }
44
45 data.WriteRemoteObject(token);
46 data.WriteParcelable(&want);
47 sptr<IRemoteObject> remote = Remote();
48 if (remote == nullptr) {
49 HILOG_ERROR("Remote() is NULL");
50 return;
51 }
52 int32_t ret = remote->SendRequest(IAbilityInfoCallback::Notify_ABILITY_TOKEN, data, reply, option);
53 if (ret != NO_ERROR) {
54 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
55 return;
56 }
57 }
58
NotifyRestartSpecifiedAbility(const sptr<IRemoteObject> & token)59 void AbilityInfoCallbackProxy::NotifyRestartSpecifiedAbility(const sptr<IRemoteObject> &token)
60 {
61 MessageParcel data;
62 MessageParcel reply;
63 MessageOption option(MessageOption::TF_SYNC);
64 if (!WriteInterfaceToken(data)) {
65 return;
66 }
67
68 data.WriteRemoteObject(token);
69 sptr<IRemoteObject> remote = Remote();
70 if (remote == nullptr) {
71 HILOG_ERROR("Remote() is NULL");
72 return;
73 }
74 int32_t ret = remote->SendRequest(IAbilityInfoCallback::Notify_RESTART_SPECIFIED_ABILITY, data, reply, option);
75 if (ret != NO_ERROR) {
76 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
77 }
78 }
79
NotifyStartSpecifiedAbility(const sptr<IRemoteObject> & callerToken,const Want & want,int requestCode,sptr<Want> & extraParam)80 void AbilityInfoCallbackProxy::NotifyStartSpecifiedAbility(const sptr<IRemoteObject> &callerToken,
81 const Want &want, int requestCode, sptr<Want> &extraParam)
82 {
83 MessageParcel data;
84 MessageParcel reply;
85 MessageOption option(MessageOption::TF_SYNC);
86 if (!WriteInterfaceToken(data)) {
87 return;
88 }
89
90 data.WriteRemoteObject(callerToken);
91 data.WriteParcelable(&want);
92 data.WriteInt32(requestCode);
93 sptr<IRemoteObject> remote = Remote();
94 if (remote == nullptr) {
95 HILOG_ERROR("Remote() is NULL");
96 return;
97 }
98 int32_t ret = remote->SendRequest(IAbilityInfoCallback::Notify_START_SPECIFIED_ABILITY, data, reply, option);
99 if (ret != NO_ERROR) {
100 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
101 return;
102 }
103 sptr<Want> tempWant = reply.ReadParcelable<Want>();
104 if (tempWant != nullptr) {
105 SetExtraParam(tempWant, extraParam);
106 }
107 }
108
SetExtraParam(const sptr<Want> & want,sptr<Want> & extraParam)109 void AbilityInfoCallbackProxy::SetExtraParam(const sptr<Want> &want, sptr<Want> &extraParam)
110 {
111 if (!want || !extraParam) {
112 HILOG_ERROR("want or extraParam is invalid.");
113 return;
114 }
115
116 sptr<IRemoteObject> tempCallBack = want->GetRemoteObject(Want::PARAM_RESV_ABILITY_INFO_CALLBACK);
117 if (tempCallBack == nullptr) {
118 return;
119 }
120 extraParam->SetParam(Want::PARAM_RESV_REQUEST_PROC_CODE,
121 want->GetIntParam(Want::PARAM_RESV_REQUEST_PROC_CODE, 0));
122 extraParam->SetParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE,
123 want->GetIntParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE, 0));
124 extraParam->SetParam(Want::PARAM_RESV_ABILITY_INFO_CALLBACK, tempCallBack);
125 }
126 } // namespace AppExecFwk
127 } // namespace OHOS
128