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 "service_proxy_adapter.h"
17
18 #include "connection_observer_errors.h"
19 #include "hilog_tag_wrapper.h"
20
21 namespace OHOS {
22 namespace AbilityRuntime {
23 namespace {
24 const std::u16string ABILITY_MGR_DESCRIPTOR = u"ohos.aafwk.AbilityManager";
25 constexpr uint32_t REGISTER_CONNECTION_OBSERVER = 2502;
26 constexpr uint32_t UNREGISTER_CONNECTION_OBSERVER = 2503;
27 #ifdef WITH_DLP
28 constexpr uint32_t GET_DLP_CONNECTION_INFOS = 2504;
29 #endif // WITH_DLP
30 constexpr uint32_t GET_CONNECTION_DATA = 2505;
31 constexpr int32_t CYCLE_LIMIT = 1000;
32 }
RegisterObserver(const sptr<IConnectionObserver> & observer)33 int32_t ServiceProxyAdapter::RegisterObserver(const sptr<IConnectionObserver> &observer)
34 {
35 if (!observer) {
36 TAG_LOGE(AAFwkTag::CONNECTION, "invalid IConnectObserver");
37 return ERR_INVALID_OBSERVER;
38 }
39
40 if (!remoteObj_) {
41 TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
42 return ERR_NO_PROXY;
43 }
44
45 int error;
46 MessageParcel data;
47 MessageParcel reply;
48 MessageOption option;
49 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
50 TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
51 return ERR_INVALID_VALUE;
52 }
53
54 if (!data.WriteRemoteObject(observer->AsObject())) {
55 TAG_LOGE(AAFwkTag::CONNECTION, "write remote obj failed");
56 return ERR_INVALID_VALUE;
57 }
58
59 error = remoteObj_->SendRequest(REGISTER_CONNECTION_OBSERVER, data, reply, option);
60 if (error != NO_ERROR) {
61 TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
62 return error;
63 }
64 return reply.ReadInt32();
65 }
66
UnregisterObserver(const sptr<IConnectionObserver> & observer)67 int32_t ServiceProxyAdapter::UnregisterObserver(const sptr<IConnectionObserver> &observer)
68 {
69 if (!observer) {
70 TAG_LOGE(AAFwkTag::CONNECTION, "IConnectObserver invalid");
71 return ERR_INVALID_OBSERVER;
72 }
73
74 if (!remoteObj_) {
75 TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
76 return ERR_NO_PROXY;
77 }
78
79 int error;
80 MessageParcel data;
81 MessageParcel reply;
82 MessageOption option;
83 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
84 TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
85 return ERR_INVALID_VALUE;
86 }
87
88 if (!data.WriteRemoteObject(observer->AsObject())) {
89 TAG_LOGE(AAFwkTag::CONNECTION, "write remote obj failed");
90 return ERR_INVALID_VALUE;
91 }
92
93 error = remoteObj_->SendRequest(UNREGISTER_CONNECTION_OBSERVER, data, reply, option);
94 if (error != NO_ERROR) {
95 TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
96 return error;
97 }
98 return reply.ReadInt32();
99 }
100
101 #ifdef WITH_DLP
GetDlpConnectionInfos(std::vector<DlpConnectionInfo> & infos)102 int32_t ServiceProxyAdapter::GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos)
103 {
104 if (!remoteObj_) {
105 TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
106 return ERR_NO_PROXY;
107 }
108
109 int error;
110 MessageParcel data;
111 MessageParcel reply;
112 MessageOption option;
113 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
114 TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
115 return ERR_INVALID_VALUE;
116 }
117
118 error = remoteObj_->SendRequest(GET_DLP_CONNECTION_INFOS, data, reply, option);
119 if (error != NO_ERROR) {
120 TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
121 return error;
122 }
123
124 auto result = reply.ReadInt32();
125 if (result != 0) {
126 TAG_LOGE(AAFwkTag::CONNECTION, "fail, result: %{public}d", result);
127 return result;
128 }
129
130 int32_t infoSize = reply.ReadInt32();
131 if (infoSize > CYCLE_LIMIT) {
132 TAG_LOGE(AAFwkTag::CONNECTION, "infoSize too large");
133 return ERR_INVALID_VALUE;
134 }
135
136 for (int32_t i = 0; i < infoSize; i++) {
137 std::unique_ptr<DlpConnectionInfo> info(reply.ReadParcelable<DlpConnectionInfo>());
138 if (info == nullptr) {
139 TAG_LOGE(AAFwkTag::CONNECTION, "Read infos failed");
140 return ERR_READ_INFO_FAILED;
141 }
142 infos.emplace_back(*info);
143 }
144
145 return result;
146 }
147 #endif // WITH_DLP
148
GetConnectionData(std::vector<ConnectionData> & connectionData)149 int32_t ServiceProxyAdapter::GetConnectionData(std::vector<ConnectionData> &connectionData)
150 {
151 if (!remoteObj_) {
152 TAG_LOGE(AAFwkTag::CONNECTION, "no abilityms proxy");
153 return ERR_NO_PROXY;
154 }
155
156 int error;
157 MessageParcel data;
158 MessageParcel reply;
159 MessageOption option;
160 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
161 TAG_LOGE(AAFwkTag::CONNECTION, "write token failed");
162 return ERR_INVALID_VALUE;
163 }
164
165 error = remoteObj_->SendRequest(GET_CONNECTION_DATA, data, reply, option);
166 if (error != NO_ERROR) {
167 TAG_LOGE(AAFwkTag::CONNECTION, "Send request error: %{public}d", error);
168 return error;
169 }
170
171 auto result = reply.ReadInt32();
172 if (result != 0) {
173 TAG_LOGE(AAFwkTag::CONNECTION, "fail, result: %{public}d", result);
174 return result;
175 }
176
177 int32_t infoSize = reply.ReadInt32();
178 if (infoSize > CYCLE_LIMIT) {
179 TAG_LOGE(AAFwkTag::CONNECTION, "infoSize too large");
180 return ERR_INVALID_VALUE;
181 }
182
183 for (int32_t i = 0; i < infoSize; i++) {
184 std::unique_ptr<ConnectionData> item(reply.ReadParcelable<ConnectionData>());
185 if (item == nullptr) {
186 TAG_LOGE(AAFwkTag::CONNECTION, "Read infos failed");
187 return ERR_READ_INFO_FAILED;
188 }
189 connectionData.emplace_back(*item);
190 }
191
192 return result;
193 }
194
GetProxyObject() const195 sptr<IRemoteObject> ServiceProxyAdapter::GetProxyObject() const
196 {
197 return remoteObj_;
198 }
199 } // namespace AbilityRuntime
200 } // namespace OHOS
201