1 /*
2 * Copyright (c) 2021-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 "component_interception_proxy.h"
17
18 #include "hilog_wrapper.h"
19 #include "ipc_types.h"
20
21
22 namespace OHOS {
23 namespace AppExecFwk {
ComponentInterceptionProxy(const sptr<IRemoteObject> & impl)24 ComponentInterceptionProxy::ComponentInterceptionProxy(
25 const sptr<IRemoteObject> &impl) : IRemoteProxy<IComponentInterception>(impl)
26 {}
27
WriteInterfaceToken(MessageParcel & data)28 bool ComponentInterceptionProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30 if (!data.WriteInterfaceToken(ComponentInterceptionProxy::GetDescriptor())) {
31 HILOG_ERROR("write interface token failed");
32 return false;
33 }
34 return true;
35 }
36
AllowComponentStart(const Want & want,const sptr<IRemoteObject> & callerToken,int requestCode,int componentStatus,sptr<Want> & extraParam)37 bool ComponentInterceptionProxy::AllowComponentStart(const Want &want, const sptr<IRemoteObject> &callerToken,
38 int requestCode, int componentStatus, sptr<Want> &extraParam)
39 {
40 MessageParcel data;
41 MessageParcel reply;
42 MessageOption option(MessageOption::TF_SYNC);
43 if (!WriteInterfaceToken(data)) {
44 return true;
45 }
46 data.WriteParcelable(&want);
47
48 if (callerToken == nullptr) {
49 data.WriteBool(false);
50 } else {
51 data.WriteBool(true);
52 data.WriteRemoteObject(callerToken);
53 }
54
55 data.WriteInt32(requestCode);
56 data.WriteInt32(componentStatus);
57 sptr<IRemoteObject> remote = Remote();
58 if (remote == nullptr) {
59 HILOG_ERROR("Remote() is NULL");
60 return true;
61 }
62 int32_t ret = remote->SendRequest(
63 static_cast<uint32_t>(IComponentInterception::Message::TRANSACT_ON_ALLOW_COMPONENT_START),
64 data, reply, option);
65 if (ret != NO_ERROR) {
66 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
67 return true;
68 }
69
70 bool hasExtraParam = reply.ReadBool();
71 if (hasExtraParam) {
72 sptr<Want> tempWant = reply.ReadParcelable<Want>();
73 if (tempWant != nullptr) {
74 SetExtraParam(tempWant, extraParam);
75 }
76 }
77 return reply.ReadBool();
78 }
79
SetExtraParam(const sptr<Want> & want,sptr<Want> & extraParam)80 void ComponentInterceptionProxy::SetExtraParam(const sptr<Want> &want, sptr<Want> &extraParam)
81 {
82 if (extraParam == nullptr) {
83 return;
84 }
85 int requestResult = want->GetIntParam("requestResult", ERR_OK);
86 extraParam->SetParam("requestResult", requestResult == ERR_OK ? ERR_OK : ERR_WOULD_BLOCK);
87
88 sptr<IRemoteObject> tempCallBack = want->GetRemoteObject(Want::PARAM_RESV_ABILITY_INFO_CALLBACK);
89 if (tempCallBack == nullptr) {
90 return;
91 }
92 extraParam->SetParam(Want::PARAM_RESV_REQUEST_PROC_CODE,
93 want->GetIntParam(Want::PARAM_RESV_REQUEST_PROC_CODE, 0));
94 extraParam->SetParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE,
95 want->GetIntParam(Want::PARAM_RESV_REQUEST_TOKEN_CODE, 0));
96 extraParam->SetParam(Want::PARAM_RESV_ABILITY_INFO_CALLBACK, tempCallBack);
97 }
98
NotifyHandleAbilityStateChange(const sptr<IRemoteObject> & abilityToken,int opCode)99 void ComponentInterceptionProxy::NotifyHandleAbilityStateChange(const sptr<IRemoteObject> &abilityToken, int opCode)
100 {
101 MessageParcel data;
102 MessageParcel reply;
103 MessageOption option(MessageOption::TF_SYNC);
104 if (!WriteInterfaceToken(data)) {
105 return;
106 }
107
108 if (abilityToken == nullptr) {
109 data.WriteBool(false);
110 } else {
111 data.WriteBool(true);
112 data.WriteRemoteObject(abilityToken);
113 }
114 data.WriteInt32(opCode);
115 sptr<IRemoteObject> remote = Remote();
116 if (remote == nullptr) {
117 HILOG_ERROR("Remote() is NULL");
118 return;
119 }
120 int32_t ret = remote->SendRequest(
121 static_cast<uint32_t>(IComponentInterception::Message::TRANSACT_ON_HANDLE_MOVE_ABILITY),
122 data, reply, option);
123 if (ret != NO_ERROR) {
124 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
125 }
126 }
127
ReleaseCallInterception(const sptr<IRemoteObject> & connect,const AppExecFwk::ElementName & element,sptr<Want> & extraParam)128 bool ComponentInterceptionProxy::ReleaseCallInterception(const sptr<IRemoteObject> &connect,
129 const AppExecFwk::ElementName &element, sptr<Want> &extraParam)
130 {
131 MessageParcel data;
132 MessageParcel reply;
133 MessageOption option(MessageOption::TF_SYNC);
134 if (!WriteInterfaceToken(data)) {
135 return false;
136 }
137
138 data.WriteRemoteObject(connect);
139 data.WriteParcelable(&element);
140 sptr<IRemoteObject> remote = Remote();
141 if (remote == nullptr) {
142 HILOG_ERROR("Remote() is NULL");
143 return false;
144 }
145 int32_t ret = remote->SendRequest(
146 static_cast<uint32_t>(IComponentInterception::Message::TRANSACT_ON_RELEASE_CALL),
147 data, reply, option);
148 if (ret != NO_ERROR) {
149 HILOG_WARN("SendRequest is failed, error code: %{public}d", ret);
150 return false;
151 }
152
153 bool hasExtraParam = reply.ReadBool();
154 if (hasExtraParam) {
155 sptr<Want> tempWant = reply.ReadParcelable<Want>();
156 if (tempWant != nullptr) {
157 SetExtraParam(tempWant, extraParam);
158 }
159 }
160 return reply.ReadBool();
161 }
162 } // namespace AppExecFwk
163 } // namespace OHOS
164