1 /*
2 * Copyright (c) 2022 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_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 constexpr uint32_t GET_DLP_CONNECTION_INFOS = 2504;
28 constexpr int32_t CYCLE_LIMIT = 1000;
29 }
RegisterObserver(const sptr<IConnectionObserver> & observer)30 int32_t ServiceProxyAdapter::RegisterObserver(const sptr<IConnectionObserver> &observer)
31 {
32 if (!observer) {
33 HILOG_ERROR("IConnectObserver is invalid.");
34 return ERR_INVALID_OBSERVER;
35 }
36
37 if (!remoteObj_) {
38 HILOG_ERROR("no abilityms proxy.");
39 return ERR_NO_PROXY;
40 }
41
42 int error;
43 MessageParcel data;
44 MessageParcel reply;
45 MessageOption option;
46 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
47 HILOG_ERROR("register observer write interface token failed.");
48 return ERR_INVALID_VALUE;
49 }
50
51 if (!data.WriteRemoteObject(observer->AsObject())) {
52 HILOG_ERROR("register observer write observer remote obj failed.");
53 return ERR_INVALID_VALUE;
54 }
55
56 error = remoteObj_->SendRequest(REGISTER_CONNECTION_OBSERVER, data, reply, option);
57 if (error != NO_ERROR) {
58 HILOG_ERROR("register observer Send request error: %{public}d", error);
59 return error;
60 }
61 return reply.ReadInt32();
62 }
63
UnregisterObserver(const sptr<IConnectionObserver> & observer)64 int32_t ServiceProxyAdapter::UnregisterObserver(const sptr<IConnectionObserver> &observer)
65 {
66 if (!observer) {
67 HILOG_ERROR("unregister observer, IConnectObserver is invalid.");
68 return ERR_INVALID_OBSERVER;
69 }
70
71 if (!remoteObj_) {
72 HILOG_ERROR("unregister observer, no abilityms proxy.");
73 return ERR_NO_PROXY;
74 }
75
76 int error;
77 MessageParcel data;
78 MessageParcel reply;
79 MessageOption option;
80 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
81 HILOG_ERROR("unregister observer, write interface token failed.");
82 return ERR_INVALID_VALUE;
83 }
84
85 if (!data.WriteRemoteObject(observer->AsObject())) {
86 HILOG_ERROR("unregister observer, write observer remote obj failed.");
87 return ERR_INVALID_VALUE;
88 }
89
90 error = remoteObj_->SendRequest(UNREGISTER_CONNECTION_OBSERVER, data, reply, option);
91 if (error != NO_ERROR) {
92 HILOG_ERROR("unregister observer, Send request error: %{public}d", error);
93 return error;
94 }
95 return reply.ReadInt32();
96 }
97
GetDlpConnectionInfos(std::vector<DlpConnectionInfo> & infos)98 int32_t ServiceProxyAdapter::GetDlpConnectionInfos(std::vector<DlpConnectionInfo> &infos)
99 {
100 if (!remoteObj_) {
101 HILOG_ERROR("GetDlpConnectionInfos, no abilityms proxy.");
102 return ERR_NO_PROXY;
103 }
104
105 int error;
106 MessageParcel data;
107 MessageParcel reply;
108 MessageOption option;
109 if (!data.WriteInterfaceToken(ABILITY_MGR_DESCRIPTOR)) {
110 HILOG_ERROR("GetDlpConnectionInfos, write interface token failed.");
111 return ERR_INVALID_VALUE;
112 }
113
114 error = remoteObj_->SendRequest(GET_DLP_CONNECTION_INFOS, data, reply, option);
115 if (error != NO_ERROR) {
116 HILOG_ERROR("GetDlpConnectionInfos, Send request error: %{public}d", error);
117 return error;
118 }
119
120 auto result = reply.ReadInt32();
121 if (result != 0) {
122 HILOG_ERROR("GetDlpConnectionInfos fail, result: %{public}d", result);
123 return result;
124 }
125
126 int32_t infoSize = reply.ReadInt32();
127 if (infoSize > CYCLE_LIMIT) {
128 HILOG_ERROR("infoSize is too large");
129 return ERR_INVALID_VALUE;
130 }
131
132 for (int32_t i = 0; i < infoSize; i++) {
133 std::unique_ptr<DlpConnectionInfo> info(reply.ReadParcelable<DlpConnectionInfo>());
134 if (info == nullptr) {
135 HILOG_ERROR("Read GetDlpConnectionInfo infos failed");
136 return ERR_READ_INFO_FAILED;
137 }
138 infos.emplace_back(*info);
139 }
140
141 return result;
142 }
143
GetProxyObject() const144 sptr<IRemoteObject> ServiceProxyAdapter::GetProxyObject() const
145 {
146 return remoteObj_;
147 }
148 } // namespace AbilityRuntime
149 } // namespace OHOS
150