• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-2025 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_manager_errors.h"
17 #include "ability_manager_ipc_interface_code.h"
18 #include "extension_manager_proxy.h"
19 #include "extension_running_info.h"
20 #include "hilog_tag_wrapper.h"
21 #include "hitrace_chain_utils.h"
22 #include "hitrace_meter.h"
23 #include "message_parcel.h"
24 #include "want.h"
25 
26 namespace OHOS::AAFwk {
27 constexpr int32_t CYCLE_LIMIT = 1000;
WriteInterfaceToken(MessageParcel & data)28 bool ExtensionManagerProxy::WriteInterfaceToken(MessageParcel &data)
29 {
30     if (!data.WriteInterfaceToken(ExtensionManagerProxy::GetDescriptor())) {
31         TAG_LOGE(AAFwkTag::EXTMGR, "write token failed");
32         return false;
33     }
34     return true;
35 }
36 
ConnectAbilityCommon(const Want & want,sptr<IRemoteObject> connect,const sptr<IRemoteObject> & callerToken,AppExecFwk::ExtensionAbilityType extensionType,int32_t userId,bool isQueryExtensionOnly)37 int ExtensionManagerProxy::ConnectAbilityCommon(const Want &want, sptr<IRemoteObject> connect,
38     const sptr<IRemoteObject> &callerToken, AppExecFwk::ExtensionAbilityType extensionType, int32_t userId,
39     bool isQueryExtensionOnly)
40 {
41     Ability_MANAGER_HITRACE_CHAIN_NAME("ConnectAbilityCommon", HITRACE_FLAG_INCLUDE_ASYNC);
42     if (connect == nullptr) {
43         TAG_LOGE(AAFwkTag::EXTMGR, "null connect");
44         return CONNECTION_NOT_EXIST;
45     }
46 
47     MessageParcel data;
48     if (!WriteInterfaceToken(data)) {
49         return INVALID_PARAMETERS_ERR;
50     }
51     if (!data.WriteParcelable(&want)) {
52         TAG_LOGE(AAFwkTag::EXTMGR, "want write failed");
53         return INVALID_PARAMETERS_ERR;
54     }
55     if (!data.WriteBool(true) || !data.WriteRemoteObject(connect)) {
56         TAG_LOGE(AAFwkTag::EXTMGR, "flag or connect write failed");
57         return INVALID_PARAMETERS_ERR;
58     }
59     if (callerToken) {
60         if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
61             TAG_LOGE(AAFwkTag::EXTMGR, "flag or callerToken write failed");
62             return INVALID_PARAMETERS_ERR;
63         }
64     } else {
65         if (!data.WriteBool(false)) {
66             TAG_LOGE(AAFwkTag::EXTMGR, "flag write failed");
67             return INVALID_PARAMETERS_ERR;
68         }
69     }
70     if (!data.WriteInt32(userId)) {
71         TAG_LOGE(AAFwkTag::EXTMGR, "userId write failed.");
72         return INVALID_PARAMETERS_ERR;
73     }
74     if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
75         TAG_LOGE(AAFwkTag::EXTMGR, "extensionType write failed.");
76         return INVALID_PARAMETERS_ERR;
77     }
78     if (!data.WriteBool(isQueryExtensionOnly)) {
79         TAG_LOGE(AAFwkTag::EXTMGR, "isQueryExtensionOnly write failed");
80         return INVALID_PARAMETERS_ERR;
81     }
82 
83     MessageParcel reply;
84     MessageOption option;
85     int error = SendRequest(AbilityManagerInterfaceCode::CONNECT_ABILITY_WITH_TYPE, data, reply, option);
86     if (error != NO_ERROR) {
87         TAG_LOGE(AAFwkTag::EXTMGR, "Send request error: %{public}d", error);
88         return error;
89     }
90     return reply.ReadInt32();
91 }
92 
DisconnectAbility(const sptr<IRemoteObject> & connect)93 int ExtensionManagerProxy::DisconnectAbility(const sptr<IRemoteObject> &connect)
94 {
95     Ability_MANAGER_HITRACE_CHAIN_NAME("DisconnectAbility", HITRACE_FLAG_INCLUDE_ASYNC);
96     if (connect == nullptr) {
97         TAG_LOGE(AAFwkTag::EXTMGR, "disconnect ability failed");
98         return INVALID_PARAMETERS_ERR;
99     }
100 
101     MessageParcel data;
102     if (!WriteInterfaceToken(data)) {
103         return INVALID_PARAMETERS_ERR;
104     }
105     if (!data.WriteRemoteObject(connect)) {
106         TAG_LOGE(AAFwkTag::EXTMGR, "connect write failed");
107         return INVALID_PARAMETERS_ERR;
108     }
109 
110     MessageParcel reply;
111     MessageOption option;
112     auto error = SendRequest(AbilityManagerInterfaceCode::DISCONNECT_ABILITY, data, reply, option);
113     if (error != NO_ERROR) {
114         TAG_LOGE(AAFwkTag::EXTMGR, "Send request error: %{public}d", error);
115         return error;
116     }
117     return reply.ReadInt32();
118 }
119 
SendRequest(AbilityManagerInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)120 ErrCode ExtensionManagerProxy::SendRequest(AbilityManagerInterfaceCode code, MessageParcel &data,
121     MessageParcel &reply, MessageOption &option)
122 {
123     auto remote = Remote();
124     if (remote == nullptr) {
125         TAG_LOGE(AAFwkTag::EXTMGR, "null remote");
126         return INVALID_REMOTE_PARAMETERS_ERR;
127     }
128 
129     return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
130 }
131 
132 template <typename T>
GetParcelableInfos(MessageParcel & reply,std::vector<T> & parcelableInfos)133 int ExtensionManagerProxy::GetParcelableInfos(MessageParcel &reply, std::vector<T> &parcelableInfos)
134 {
135     int32_t infoSize = reply.ReadInt32();
136     if (infoSize > CYCLE_LIMIT) {
137         TAG_LOGE(AAFwkTag::EXTMGR, "infoSize large");
138         return INVALID_PARAMETERS_ERR;
139     }
140 
141     for (int32_t i = 0; i < infoSize; i++) {
142         std::unique_ptr<T> info(reply.ReadParcelable<T>());
143         if (!info) {
144             TAG_LOGE(AAFwkTag::EXTMGR, "readParcelableInfos fail");
145             return INVALID_PARAMETERS_ERR;
146         }
147         parcelableInfos.emplace_back(*info);
148     }
149     return NO_ERROR;
150 }
151 
GetExtensionRunningInfos(int upperLimit,std::vector<ExtensionRunningInfo> & info)152 int ExtensionManagerProxy::GetExtensionRunningInfos(int upperLimit, std::vector<ExtensionRunningInfo> &info)
153 {
154     MessageParcel data;
155     MessageParcel reply;
156     MessageOption option;
157 
158     if (!WriteInterfaceToken(data)) {
159         return INVALID_PARAMETERS_ERR;
160     }
161 
162     if (!data.WriteInt32(upperLimit)) {
163         TAG_LOGE(AAFwkTag::EXTMGR, "upperLimit write fail");
164         return INVALID_PARAMETERS_ERR;
165     }
166 
167     auto error = SendRequest(AbilityManagerInterfaceCode::GET_EXTENSION_RUNNING_INFO, data, reply, option);
168     if (error != NO_ERROR) {
169         TAG_LOGE(AAFwkTag::EXTMGR, "request error:%{public}d", error);
170         return error;
171     }
172     error = GetParcelableInfos<ExtensionRunningInfo>(reply, info);
173     if (error != NO_ERROR) {
174         TAG_LOGE(AAFwkTag::EXTMGR, "getParcelableInfos fail, error: %{public}d", error);
175         return error;
176     }
177     return reply.ReadInt32();
178 }
179 
TransferAbilityResultForExtension(const sptr<IRemoteObject> & callerToken,int32_t resultCode,const Want & want)180 int32_t ExtensionManagerProxy::TransferAbilityResultForExtension(const sptr<IRemoteObject> &callerToken,
181     int32_t resultCode, const Want &want)
182 {
183     if (callerToken == nullptr) {
184         TAG_LOGE(AAFwkTag::EXTMGR, "null callerToken");
185         return INVALID_PARAMETERS_ERR;
186     }
187     MessageParcel data;
188     MessageParcel reply;
189     MessageOption option;
190     if (!WriteInterfaceToken(data)) {
191         return INVALID_PARAMETERS_ERR;
192     }
193     if (!data.WriteRemoteObject(callerToken) || !data.WriteInt32(resultCode)) {
194         TAG_LOGE(AAFwkTag::EXTMGR, "callerToken or resultCode write fail");
195         return INVALID_PARAMETERS_ERR;
196     }
197     if (!data.WriteParcelable(&want)) {
198         TAG_LOGE(AAFwkTag::EXTMGR, "want write fail");
199         return INVALID_PARAMETERS_ERR;
200     }
201     auto error = SendRequest(AbilityManagerInterfaceCode::TRANSFER_ABILITY_RESULT, data, reply, option);
202     if (error != NO_ERROR) {
203         TAG_LOGE(AAFwkTag::EXTMGR, "request error:%{public}d", error);
204         return error;
205     }
206     return NO_ERROR;
207 }
208 }  // namespace OHOS::AAFwk
209