1 /*
2 * Copyright (c) 2023 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 "extension_manager_proxy.h"
17
18 #include "ability_manager_errors.h"
19 #include "ability_manager_ipc_interface_code.h"
20 #include "hilog_wrapper.h"
21 #include "message_parcel.h"
22 #include "want.h"
23
24 namespace OHOS::AAFwk {
WriteInterfaceToken(MessageParcel & data)25 bool ExtensionManagerProxy::WriteInterfaceToken(MessageParcel &data)
26 {
27 if (!data.WriteInterfaceToken(ExtensionManagerProxy::GetDescriptor())) {
28 HILOG_ERROR("write interface token failed.");
29 return false;
30 }
31 return true;
32 }
33
ConnectAbilityCommon(const Want & want,const sptr<IRemoteObject> & connect,const sptr<IRemoteObject> & callerToken,AppExecFwk::ExtensionAbilityType extensionType,int32_t userId)34 int ExtensionManagerProxy::ConnectAbilityCommon(const Want &want, const sptr<IRemoteObject> &connect,
35 const sptr<IRemoteObject> &callerToken, AppExecFwk::ExtensionAbilityType extensionType, int32_t userId)
36 {
37 if (connect == nullptr) {
38 HILOG_ERROR("connect is nullptr");
39 return ERR_INVALID_VALUE;
40 }
41
42 MessageParcel data;
43 if (!WriteInterfaceToken(data)) {
44 return INNER_ERR;
45 }
46 if (!data.WriteParcelable(&want)) {
47 HILOG_ERROR("want write failed.");
48 return ERR_INVALID_VALUE;
49 }
50 if (!data.WriteBool(true) || !data.WriteRemoteObject(connect)) {
51 HILOG_ERROR("flag and connect write failed.");
52 return ERR_INVALID_VALUE;
53 }
54 if (callerToken) {
55 if (!data.WriteBool(true) || !data.WriteRemoteObject(callerToken)) {
56 HILOG_ERROR("flag and callerToken write failed.");
57 return ERR_INVALID_VALUE;
58 }
59 } else {
60 if (!data.WriteBool(false)) {
61 HILOG_ERROR("flag write failed.");
62 return ERR_INVALID_VALUE;
63 }
64 }
65 if (!data.WriteInt32(userId)) {
66 HILOG_ERROR("%{public}s, userId write failed.", __func__);
67 return INNER_ERR;
68 }
69 if (!data.WriteInt32(static_cast<int32_t>(extensionType))) {
70 HILOG_ERROR("%{public}s, extensionType write failed.", __func__);
71 return INNER_ERR;
72 }
73
74 MessageParcel reply;
75 MessageOption option;
76 int error = SendRequest(AbilityManagerInterfaceCode::CONNECT_ABILITY_WITH_TYPE, data, reply, option);
77 if (error != NO_ERROR) {
78 HILOG_ERROR("%{public}s, Send request error: %{public}d", __func__, error);
79 return error;
80 }
81 return reply.ReadInt32();
82 }
83
DisconnectAbility(const sptr<IRemoteObject> & connect)84 int ExtensionManagerProxy::DisconnectAbility(const sptr<IRemoteObject> &connect)
85 {
86 if (connect == nullptr) {
87 HILOG_ERROR("disconnect ability fail, connect is nullptr");
88 return ERR_INVALID_VALUE;
89 }
90
91 MessageParcel data;
92 if (!WriteInterfaceToken(data)) {
93 return INNER_ERR;
94 }
95 if (!data.WriteRemoteObject(connect)) {
96 HILOG_ERROR("connect write failed.");
97 return ERR_INVALID_VALUE;
98 }
99
100 MessageParcel reply;
101 MessageOption option;
102 auto error = SendRequest(AbilityManagerInterfaceCode::DISCONNECT_ABILITY, data, reply, option);
103 if (error != NO_ERROR) {
104 HILOG_ERROR("Send request error: %{public}d", error);
105 return error;
106 }
107 return reply.ReadInt32();
108 }
109
SendRequest(AbilityManagerInterfaceCode code,MessageParcel & data,MessageParcel & reply,MessageOption & option)110 ErrCode ExtensionManagerProxy::SendRequest(AbilityManagerInterfaceCode code, MessageParcel &data,
111 MessageParcel &reply, MessageOption& option)
112 {
113 auto remote = Remote();
114 if (remote == nullptr) {
115 HILOG_ERROR("Remote() is NULL");
116 return INNER_ERR;
117 }
118
119 return remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
120 }
121 } // namespace OHOS::AAFwk
122