1 /*
2  * Copyright (c) 2022-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 "connection_observer_proxy.h"
17 
18 #include "hilog_tag_wrapper.h"
19 #include "ipc_types.h"
20 #include "message_parcel.h"
21 
22 namespace OHOS {
23 namespace AbilityRuntime {
OnExtensionConnected(const ConnectionData & connectionData)24 void ConnectionObserverProxy::OnExtensionConnected(const ConnectionData& connectionData)
25 {
26     MessageParcel data;
27     MessageParcel reply;
28     MessageOption option(MessageOption::TF_ASYNC);
29 
30     TAG_LOGD(AAFwkTag::CONNECTION, "called");
31     if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
32         TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
33         return;
34     }
35 
36     if (!data.WriteParcelable(&connectionData)) {
37         TAG_LOGE(AAFwkTag::CONNECTION, "Write ConnectionData error");
38         return;
39     }
40 
41     int error = SendTransactCmd(IConnectionObserver::ON_EXTENSION_CONNECTED, data, reply, option);
42     if (error != NO_ERROR) {
43         TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
44         return;
45     }
46 }
47 
OnExtensionDisconnected(const ConnectionData & connectionData)48 void ConnectionObserverProxy::OnExtensionDisconnected(const ConnectionData& connectionData)
49 {
50     MessageParcel data;
51     MessageParcel reply;
52     MessageOption option(MessageOption::TF_ASYNC);
53 
54     TAG_LOGD(AAFwkTag::CONNECTION, "called");
55     if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
56         TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
57         return;
58     }
59 
60     if (!data.WriteParcelable(&connectionData)) {
61         TAG_LOGE(AAFwkTag::CONNECTION, "Write ConnectionData error");
62         return;
63     }
64 
65     int error = SendTransactCmd(IConnectionObserver::ON_EXTENSION_DISCONNECTED, data, reply, option);
66     if (error != NO_ERROR) {
67         TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
68         return;
69     }
70 }
71 
72 #ifdef WITH_DLP
OnDlpAbilityOpened(const DlpStateData & dlpData)73 void ConnectionObserverProxy::OnDlpAbilityOpened(const DlpStateData& dlpData)
74 {
75     MessageParcel data;
76     MessageParcel reply;
77     MessageOption option(MessageOption::TF_ASYNC);
78 
79     TAG_LOGI(AAFwkTag::CONNECTION, "called");
80     if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
81         TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
82         return;
83     }
84 
85     if (!data.WriteParcelable(&dlpData)) {
86         TAG_LOGE(AAFwkTag::CONNECTION, "Write DlpStateData error");
87         return;
88     }
89 
90     int error = SendTransactCmd(IConnectionObserver::ON_DLP_ABILITY_OPENED, data, reply, option);
91     if (error != NO_ERROR) {
92         TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
93         return;
94     }
95 }
96 
OnDlpAbilityClosed(const DlpStateData & dlpData)97 void ConnectionObserverProxy::OnDlpAbilityClosed(const DlpStateData& dlpData)
98 {
99     MessageParcel data;
100     MessageParcel reply;
101     MessageOption option(MessageOption::TF_ASYNC);
102 
103     TAG_LOGI(AAFwkTag::CONNECTION, "called");
104     if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
105         TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
106         return;
107     }
108 
109     if (!data.WriteParcelable(&dlpData)) {
110         TAG_LOGE(AAFwkTag::CONNECTION, "Write DlpStateData error");
111         return;
112     }
113 
114     int error = SendTransactCmd(IConnectionObserver::ON_DLP_ABILITY_CLOSED, data, reply, option);
115     if (error != NO_ERROR) {
116         TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
117         return;
118     }
119 }
120 #endif // WITH_DLP
121 
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)122 int32_t ConnectionObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
123     MessageParcel &reply, MessageOption &option)
124 {
125     sptr<IRemoteObject> remote = Remote();
126     if (remote == nullptr) {
127         TAG_LOGE(AAFwkTag::CONNECTION, "null remote");
128         return ERR_NULL_OBJECT;
129     }
130 
131     int32_t ret = remote->SendRequest(code, data, reply, option);
132     if (ret != NO_ERROR) {
133         TAG_LOGE(AAFwkTag::CONNECTION, "SendRequest failed. code: %{public}d, ret: %{public}d.", code, ret);
134         return ret;
135     }
136     return NO_ERROR;
137 }
138 }  // namespace AAFwk
139 }  // namespace OHOS
140