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
OnExtensionSuspended(const ConnectionData & connectionData)72 void ConnectionObserverProxy::OnExtensionSuspended(const ConnectionData& connectionData)
73 {
74 MessageParcel data;
75 MessageParcel reply;
76 MessageOption option(MessageOption::TF_ASYNC);
77
78 TAG_LOGD(AAFwkTag::CONNECTION, "called");
79 if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
80 TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
81 return;
82 }
83
84 if (!data.WriteParcelable(&connectionData)) {
85 TAG_LOGE(AAFwkTag::CONNECTION, "Write ConnectionData error");
86 return;
87 }
88
89 int error = SendTransactCmd(IConnectionObserver::ON_EXTENSION_SUSPENDED, data, reply, option);
90 if (error != NO_ERROR) {
91 TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
92 }
93 }
94
OnExtensionResumed(const ConnectionData & connectionData)95 void ConnectionObserverProxy::OnExtensionResumed(const ConnectionData& connectionData)
96 {
97 MessageParcel data;
98 MessageParcel reply;
99 MessageOption option(MessageOption::TF_ASYNC);
100
101 TAG_LOGD(AAFwkTag::CONNECTION, "called");
102 if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
103 TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
104 return;
105 }
106
107 if (!data.WriteParcelable(&connectionData)) {
108 TAG_LOGE(AAFwkTag::CONNECTION, "Write ConnectionData error");
109 return;
110 }
111
112 int error = SendTransactCmd(IConnectionObserver::ON_EXTENSION_RESUMED, data, reply, option);
113 if (error != NO_ERROR) {
114 TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
115 }
116 }
117
118 #ifdef WITH_DLP
OnDlpAbilityOpened(const DlpStateData & dlpData)119 void ConnectionObserverProxy::OnDlpAbilityOpened(const DlpStateData& dlpData)
120 {
121 MessageParcel data;
122 MessageParcel reply;
123 MessageOption option(MessageOption::TF_ASYNC);
124
125 TAG_LOGI(AAFwkTag::CONNECTION, "called");
126 if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
127 TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
128 return;
129 }
130
131 if (!data.WriteParcelable(&dlpData)) {
132 TAG_LOGE(AAFwkTag::CONNECTION, "Write DlpStateData error");
133 return;
134 }
135
136 int error = SendTransactCmd(IConnectionObserver::ON_DLP_ABILITY_OPENED, data, reply, option);
137 if (error != NO_ERROR) {
138 TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
139 return;
140 }
141 }
142
OnDlpAbilityClosed(const DlpStateData & dlpData)143 void ConnectionObserverProxy::OnDlpAbilityClosed(const DlpStateData& dlpData)
144 {
145 MessageParcel data;
146 MessageParcel reply;
147 MessageOption option(MessageOption::TF_ASYNC);
148
149 TAG_LOGI(AAFwkTag::CONNECTION, "called");
150 if (!data.WriteInterfaceToken(IConnectionObserver::GetDescriptor())) {
151 TAG_LOGE(AAFwkTag::CONNECTION, "Write token failed");
152 return;
153 }
154
155 if (!data.WriteParcelable(&dlpData)) {
156 TAG_LOGE(AAFwkTag::CONNECTION, "Write DlpStateData error");
157 return;
158 }
159
160 int error = SendTransactCmd(IConnectionObserver::ON_DLP_ABILITY_CLOSED, data, reply, option);
161 if (error != NO_ERROR) {
162 TAG_LOGE(AAFwkTag::CONNECTION, "send request error: %{public}d", error);
163 return;
164 }
165 }
166 #endif // WITH_DLP
167
SendTransactCmd(uint32_t code,MessageParcel & data,MessageParcel & reply,MessageOption & option)168 int32_t ConnectionObserverProxy::SendTransactCmd(uint32_t code, MessageParcel &data,
169 MessageParcel &reply, MessageOption &option)
170 {
171 sptr<IRemoteObject> remote = Remote();
172 if (remote == nullptr) {
173 TAG_LOGE(AAFwkTag::CONNECTION, "null remote");
174 return ERR_NULL_OBJECT;
175 }
176
177 int32_t ret = remote->SendRequest(code, data, reply, option);
178 if (ret != NO_ERROR) {
179 TAG_LOGE(AAFwkTag::CONNECTION, "SendRequest failed. code: %{public}d, ret: %{public}d.", code, ret);
180 return ret;
181 }
182 return NO_ERROR;
183 }
184 } // namespace AAFwk
185 } // namespace OHOS
186