1 /*
2  * Copyright (c) 2021-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 "fms_log_wrapper.h"
17 #include "ipc_types.h"
18 #include "message_parcel.h"
19 #include "provider_connect_proxy.h"
20 
21 namespace OHOS {
22 namespace AppExecFwk {
23 /**
24  * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
25  * @param element service ability's ElementName.
26  * @param remoteObject the session proxy of service ability.
27  * @param resultCode ERR_OK on success, others on failure.
28  */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)29 void ProviderConnectProxy::OnAbilityConnectDone(
30     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
31 {
32     HILOG_DEBUG("abilityName:%{public}s,resultCode:%{public}d",
33         element.GetAbilityName().c_str(), resultCode);
34     MessageParcel data;
35     MessageParcel reply;
36     MessageOption option;
37 
38     if (!WriteInterfaceToken(data)) {
39         HILOG_ERROR("write interface token failed");
40         return;
41     }
42 
43     if (!data.WriteParcelable(&element)) {
44         HILOG_ERROR("fail write element");
45         return;
46     }
47 
48     if (remoteObject) {
49         if (!data.WriteBool(true) || !data.WriteRemoteObject(remoteObject)) {
50             HILOG_ERROR("fail write flag and remote object");
51             return;
52         }
53     } else {
54         if (!data.WriteBool(false)) {
55             HILOG_ERROR("fail write flag");
56             return;
57         }
58     }
59 
60     if (!data.WriteInt32(resultCode)) {
61         HILOG_ERROR("fail write resultCode");
62         return;
63     }
64 
65     if (!SendTransactCmd(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option)) {
66         HILOG_ERROR("fail SendRequest");
67         return;
68     }
69 }
70 /**
71  * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
72  * @param element service ability's ElementName.
73  * @param resultCode ERR_OK on success, others on failure.
74  */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)75 void ProviderConnectProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
76 {
77     HILOG_DEBUG(
78         "element:%{public}s, resultCode:%{public}d", element.GetURI().c_str(), resultCode);
79     MessageParcel data;
80     MessageParcel reply;
81     MessageOption option;
82 
83     if (!WriteInterfaceToken(data)) {
84         HILOG_ERROR("write interface token failed");
85         return;
86     }
87     if (!data.WriteParcelable(&element)) {
88         HILOG_ERROR("fail write element");
89         return;
90     }
91     if (!data.WriteInt32(resultCode)) {
92         HILOG_ERROR("fail write resultCode");
93         return;
94     }
95 
96     if (!SendTransactCmd(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option)) {
97         HILOG_ERROR("fail SendRequest");
98         return;
99     }
100 }
101 
WriteInterfaceToken(MessageParcel & data)102 bool ProviderConnectProxy::WriteInterfaceToken(MessageParcel &data)
103 {
104     if (!data.WriteInterfaceToken(ProviderConnectProxy::GetDescriptor())) {
105         HILOG_ERROR("write interface token failed");
106         return false;
107     }
108     return true;
109 }
110 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)111 bool ProviderConnectProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
112     MessageParcel &reply, MessageOption &option)
113 {
114     sptr<IRemoteObject> remote = Remote();
115     if (!remote) {
116         HILOG_ERROR("get remoteObject failed,cmd:%{public}d", code);
117         return false;
118     }
119     int32_t result = remote->SendRequest(static_cast<uint32_t>(code), data, reply, option);
120     if (result != ERR_OK) {
121         HILOG_ERROR("SendRequest failed:%{public}d,cmd:%{public}d", result, code);
122         return false;
123     }
124     return true;
125 }
126 }  // namespace AppExecFwk
127 }  // namespace OHOS
128