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