• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2021-2022 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  *     http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "hilog_wrapper.h"
18 #include "ipc_types.h"
19 #include "message_parcel.h"
20 #include "provider_connect_proxy.h"
21 
22 namespace OHOS {
23 namespace AppExecFwk {
24 /**
25  * @brief OnAbilityConnectDone, AbilityMs notify caller ability the result of connect.
26  * @param element service ability's ElementName.
27  * @param remoteObject the session proxy of service ability.
28  * @param resultCode ERR_OK on success, others on failure.
29  */
OnAbilityConnectDone(const AppExecFwk::ElementName & element,const sptr<IRemoteObject> & remoteObject,int resultCode)30 void ProviderConnectProxy::OnAbilityConnectDone(
31     const AppExecFwk::ElementName &element, const sptr<IRemoteObject> &remoteObject, int resultCode)
32 {
33     HILOG_DEBUG("%{public}s, abilityName:%{public}s,resultCode:%{public}d",
34         __func__, element.GetAbilityName().c_str(), resultCode);
35     int error;
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option;
39 
40     if (!WriteInterfaceToken(data)) {
41         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
42         return;
43     }
44 
45     if (!data.WriteParcelable(&element)) {
46         HILOG_ERROR("%{public}s, failed to write element", __func__);
47         return;
48     }
49 
50     if (remoteObject) {
51         if (!data.WriteBool(true) || !data.WriteRemoteObject(remoteObject)) {
52             HILOG_ERROR("%{public}s, failed to write flag and remote object", __func__);
53             return;
54         }
55     } else {
56         if (!data.WriteBool(false)) {
57             HILOG_ERROR("%{public}s, failed to write flag", __func__);
58             return;
59         }
60     }
61 
62     if (!data.WriteInt32(resultCode)) {
63         HILOG_ERROR("%{public}s, failed to write resultCode", __func__);
64         return;
65     }
66 
67     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_CONNECT_DONE, data, reply, option);
68     if (error != ERR_OK) {
69         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
70         return;
71     }
72 }
73 /**
74  * @brief OnAbilityDisconnectDone, AbilityMs notify caller ability the result of disconnect.
75  * @param element service ability's ElementName.
76  * @param resultCode ERR_OK on success, others on failure.
77  */
OnAbilityDisconnectDone(const AppExecFwk::ElementName & element,int resultCode)78 void ProviderConnectProxy::OnAbilityDisconnectDone(const AppExecFwk::ElementName &element, int resultCode)
79 {
80     HILOG_DEBUG(
81         "%{public}s, element:%{public}s, resultCode:%{public}d", __func__, element.GetURI().c_str(), resultCode);
82     int error;
83     MessageParcel data;
84     MessageParcel reply;
85     MessageOption option;
86 
87     if (!WriteInterfaceToken(data)) {
88         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
89         return;
90     }
91     if (!data.WriteParcelable(&element)) {
92         HILOG_ERROR("%{public}s, failed to write element", __func__);
93         return;
94     }
95     if (!data.WriteInt32(resultCode)) {
96         HILOG_ERROR("%{public}s, failed to write resultCode", __func__);
97         return;
98     }
99 
100     error = Remote()->SendRequest(IAbilityConnection::ON_ABILITY_DISCONNECT_DONE, data, reply, option);
101     if (error != ERR_OK) {
102         HILOG_ERROR("%{public}s, failed to SendRequest: %{public}d", __func__, error);
103         return;
104     }
105 }
106 
WriteInterfaceToken(MessageParcel & data)107 bool ProviderConnectProxy::WriteInterfaceToken(MessageParcel &data)
108 {
109     if (!data.WriteInterfaceToken(ProviderConnectProxy::GetDescriptor())) {
110         HILOG_ERROR("%{public}s, failed to write interface token", __func__);
111         return false;
112     }
113     return true;
114 }
115 }  // namespace AppExecFwk
116 }  // namespace OHOS
117