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 "start_specified_ability_response_proxy.h"
17 #include "ipc_types.h"
18 #include "hilog_tag_wrapper.h"
19 
20 namespace OHOS {
21 namespace AppExecFwk {
StartSpecifiedAbilityResponseProxy(const sptr<IRemoteObject> & impl)22 StartSpecifiedAbilityResponseProxy::StartSpecifiedAbilityResponseProxy(const sptr<IRemoteObject> &impl)
23     : IRemoteProxy<IStartSpecifiedAbilityResponse>(impl)
24 {}
25 
WriteInterfaceToken(MessageParcel & data)26 bool StartSpecifiedAbilityResponseProxy::WriteInterfaceToken(MessageParcel &data)
27 {
28     if (!data.WriteInterfaceToken(StartSpecifiedAbilityResponseProxy::GetDescriptor())) {
29         TAG_LOGE(AAFwkTag::APPMGR, "write interface token failed");
30         return false;
31     }
32     return true;
33 }
34 
OnAcceptWantResponse(const AAFwk::Want & want,const std::string & flag,int32_t requestId)35 void StartSpecifiedAbilityResponseProxy::OnAcceptWantResponse(
36     const AAFwk::Want &want, const std::string &flag, int32_t requestId)
37 {
38     TAG_LOGD(AAFwkTag::APPMGR, "On accept want by proxy.");
39     MessageParcel data;
40     MessageParcel reply;
41     MessageOption option(MessageOption::TF_SYNC);
42     if (!WriteInterfaceToken(data)) {
43         return;
44     }
45     if (!data.WriteParcelable(&want) || !data.WriteString(flag) ||
46         !data.WriteInt32(requestId)) {
47         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
48         return;
49     }
50 
51     int32_t ret = SendTransactCmd(
52         static_cast<uint32_t>(IStartSpecifiedAbilityResponse::Message::ON_ACCEPT_WANT_RESPONSE), data, reply, option);
53     if (ret != NO_ERROR) {
54         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
55     }
56 }
57 
OnTimeoutResponse(const AAFwk::Want & want,int32_t requestId)58 void StartSpecifiedAbilityResponseProxy::OnTimeoutResponse(const AAFwk::Want &want, int32_t requestId)
59 {
60     TAG_LOGD(AAFwkTag::APPMGR, "On timeout response by proxy.");
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_SYNC);
64     if (!WriteInterfaceToken(data)) {
65         return;
66     }
67     if (!data.WriteParcelable(&want) || !data.WriteInt32(requestId)) {
68         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
69         return;
70     }
71 
72     int32_t ret = SendTransactCmd(static_cast<uint32_t>(
73         IStartSpecifiedAbilityResponse::Message::ON_TIMEOUT_RESPONSE), data, reply, option);
74     if (ret != NO_ERROR) {
75         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
76     }
77 }
78 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)79 int32_t StartSpecifiedAbilityResponseProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
80     MessageParcel &reply, MessageOption &option)
81 {
82     sptr<IRemoteObject> remote = Remote();
83     if (remote == nullptr) {
84         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
85         return ERR_NULL_OBJECT;
86     }
87 
88     return remote->SendRequest(code, data, reply, option);
89 }
90 
OnNewProcessRequestResponse(const AAFwk::Want & want,const std::string & flag,int32_t requestId)91 void StartSpecifiedAbilityResponseProxy::OnNewProcessRequestResponse(const AAFwk::Want &want, const std::string &flag,
92     int32_t requestId)
93 {
94     TAG_LOGD(AAFwkTag::APPMGR, "On satrt specified process response by proxy.");
95     MessageParcel data;
96     MessageParcel reply;
97     MessageOption option(MessageOption::TF_SYNC);
98     if (!WriteInterfaceToken(data)) {
99         return;
100     }
101     if (!data.WriteParcelable(&want) || !data.WriteString(flag) ||
102         !data.WriteInt32(requestId)) {
103         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
104         return;
105     }
106 
107     sptr<IRemoteObject> remote = Remote();
108     if (remote == nullptr) {
109         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
110         return;
111     }
112     int32_t ret = remote->SendRequest(
113         static_cast<uint32_t>(IStartSpecifiedAbilityResponse::Message::ON_NEW_PROCESS_REQUEST_RESPONSE),
114         data, reply, option);
115     if (ret != NO_ERROR) {
116         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
117     }
118 }
119 
OnNewProcessRequestTimeoutResponse(const AAFwk::Want & want,int32_t requestId)120 void StartSpecifiedAbilityResponseProxy::OnNewProcessRequestTimeoutResponse(const AAFwk::Want &want,
121     int32_t requestId)
122 {
123     TAG_LOGD(AAFwkTag::APPMGR, "On start specified process timeout response by proxy.");
124     MessageParcel data;
125     MessageParcel reply;
126     MessageOption option(MessageOption::TF_SYNC);
127     if (!WriteInterfaceToken(data)) {
128         return;
129     }
130     if (!data.WriteParcelable(&want) || data.WriteInt32(requestId)) {
131         TAG_LOGE(AAFwkTag::APPMGR, "Write data failed.");
132         return;
133     }
134 
135     sptr<IRemoteObject> remote = Remote();
136     if (remote == nullptr) {
137         TAG_LOGE(AAFwkTag::APPMGR, "Remote is nullptr.");
138         return;
139     }
140     int32_t ret = remote->SendRequest(static_cast<uint32_t>(
141         IStartSpecifiedAbilityResponse::Message::ON_NEW_PROCESS_REQUEST_TIMEOUT_RESPONSE),
142         data, reply, option);
143     if (ret != NO_ERROR) {
144         TAG_LOGW(AAFwkTag::APPMGR, "SendRequest is failed, error code: %{public}d", ret);
145     }
146 }
147 }  // namespace AppExecFwk
148 }  // namespace OHOS
149