1 /*
2 * Copyright (c) 2021 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 "ui_service_mgr_proxy.h"
17
18 #include "errors.h"
19 #include "string_ex.h"
20 #include "ui_service_mgr_errors.h"
21 #include "ui_service_proxy.h"
22 #include "ui_service_stub.h"
23
24 namespace OHOS {
25 namespace Ace {
WriteInterfaceToken(MessageParcel & data)26 bool UIServiceMgrProxy::WriteInterfaceToken(MessageParcel& data)
27 {
28 if (!data.WriteInterfaceToken(UIServiceMgrProxy::GetDescriptor())) {
29 HILOG_ERROR("write interface token failed");
30 return false;
31 }
32 return true;
33 }
34
RegisterCallBack(const AAFwk::Want & want,const sptr<IUIService> & uiService)35 int UIServiceMgrProxy::RegisterCallBack(const AAFwk::Want& want, const sptr<IUIService>& uiService)
36 {
37 MessageParcel data;
38 MessageParcel reply;
39 MessageOption option;
40 if (!WriteInterfaceToken(data)) {
41 return UI_SERVICE_PROXY_INNER_ERR;
42 }
43 if (!data.WriteParcelable(&want)) {
44 HILOG_ERROR("register callback fail, want error");
45 return ERR_INVALID_VALUE;
46 }
47 if (uiService == nullptr) {
48 HILOG_ERROR("register callback fail, uiService is nullptr");
49 return ERR_INVALID_VALUE;
50 }
51 if (!data.WriteRemoteObject(uiService->AsObject())) {
52 HILOG_ERROR("register callback fail, uiService error");
53 return ERR_INVALID_VALUE;
54 }
55 int error = Remote()->SendRequest(IUIServiceMgr::REGISTER_CALLBACK, data, reply, option);
56 if (error != NO_ERROR) {
57 HILOG_ERROR("register callback fail, error: %d", error);
58 return error;
59 }
60 return reply.ReadInt32();
61 }
62
UnregisterCallBack(const AAFwk::Want & want)63 int UIServiceMgrProxy::UnregisterCallBack(const AAFwk::Want& want)
64 {
65 MessageParcel data;
66 MessageParcel reply;
67 MessageOption option;
68
69 if (!WriteInterfaceToken(data)) {
70 return UI_SERVICE_PROXY_INNER_ERR;
71 }
72 data.WriteParcelable(&want);
73 int error = Remote()->SendRequest(IUIServiceMgr::UNREGISTER_CALLBACK, data, reply, option);
74 if (error != NO_ERROR) {
75 HILOG_ERROR("unregister callback fail, error: %d", error);
76 return error;
77 }
78 return reply.ReadInt32();
79 }
80
Push(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)81 int UIServiceMgrProxy::Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
82 const std::string& data, const std::string& extraData)
83 {
84 MessageParcel dataParcel;
85 MessageParcel reply;
86 MessageOption option;
87 if (!WriteInterfaceToken(dataParcel)) {
88 return UI_SERVICE_PROXY_INNER_ERR;
89 }
90 dataParcel.WriteParcelable(&want);
91 if (!dataParcel.WriteString(name)) {
92 HILOG_ERROR("fail to WriteString name");
93 return INVALID_DATA;
94 }
95 if (!dataParcel.WriteString(jsonPath)) {
96 HILOG_ERROR("fail to WriteString jsonPath");
97 return INVALID_DATA;
98 }
99 if (!dataParcel.WriteString(data)) {
100 HILOG_ERROR("fail to WriteString data");
101 return INVALID_DATA;
102 }
103 if (!dataParcel.WriteString(extraData)) {
104 HILOG_ERROR("fail to WriteString extraData");
105 return INVALID_DATA;
106 }
107 int error = Remote()->SendRequest(IUIServiceMgr::PUSH, dataParcel, reply, option);
108 if (error != NO_ERROR) {
109 HILOG_ERROR("Push fail, error: %d", error);
110 return error;
111 }
112 return reply.ReadInt32();
113 }
114
Request(const AAFwk::Want & want,const std::string & name,const std::string & data)115 int UIServiceMgrProxy::Request(const AAFwk::Want& want, const std::string& name, const std::string& data)
116 {
117 MessageParcel dataParcel;
118 MessageParcel reply;
119 MessageOption option;
120
121 if (!WriteInterfaceToken(dataParcel)) {
122 return UI_SERVICE_PROXY_INNER_ERR;
123 }
124 dataParcel.WriteParcelable(&want);
125
126 if (!dataParcel.WriteString(name)) {
127 HILOG_ERROR("fail to WriteString name");
128 return INVALID_DATA;
129 }
130
131 if (!dataParcel.WriteString(data)) {
132 HILOG_ERROR("fail to WriteString data");
133 return INVALID_DATA;
134 }
135
136 int error = Remote()->SendRequest(IUIServiceMgr::REQUEST, dataParcel, reply, option);
137 if (error != NO_ERROR) {
138 HILOG_ERROR("Request fail, error: %d", error);
139 return error;
140 }
141 return reply.ReadInt32();
142 }
143
ReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)144 int UIServiceMgrProxy::ReturnRequest(const AAFwk::Want& want, const std::string& source, const std::string& data,
145 const std::string& extraData)
146 {
147 MessageParcel dataParcel;
148 MessageParcel reply;
149 MessageOption option;
150
151 if (!WriteInterfaceToken(dataParcel)) {
152 return UI_SERVICE_PROXY_INNER_ERR;
153 }
154 dataParcel.WriteParcelable(&want);
155
156 if (!dataParcel.WriteString(source)) {
157 HILOG_ERROR("fail to WriteString source");
158 return INVALID_DATA;
159 }
160
161 if (!dataParcel.WriteString(data)) {
162 HILOG_ERROR("fail to WriteString data");
163 return INVALID_DATA;
164 }
165 if (!dataParcel.WriteString(extraData)) {
166 HILOG_ERROR("fail to WriteString extraData");
167 return INVALID_DATA;
168 }
169 int error = Remote()->SendRequest(IUIServiceMgr::RETURN_REQUEST, dataParcel, reply, option);
170 if (error != NO_ERROR) {
171 HILOG_ERROR("Request fail, error: %d", error);
172 return error;
173 }
174 return reply.ReadInt32();
175 }
176
ShowDialog(const std::string & name,const std::string & params,OHOS::Rosen::WindowType windowType,int x,int y,int width,int height,const sptr<OHOS::Ace::IDialogCallback> & dialogCallback,int * id)177 int UIServiceMgrProxy::ShowDialog(const std::string& name,
178 const std::string& params,
179 OHOS::Rosen::WindowType windowType,
180 int x,
181 int y,
182 int width,
183 int height,
184 const sptr<OHOS::Ace::IDialogCallback>& dialogCallback,
185 int* id)
186 {
187 MessageParcel dataParcel;
188 MessageParcel reply;
189 MessageOption option;
190
191 if (!WriteInterfaceToken(dataParcel)) {
192 return UI_SERVICE_PROXY_INNER_ERR;
193 }
194
195 if (!dataParcel.WriteString(name)) {
196 HILOG_ERROR("fail to WriteString name");
197 return INVALID_DATA;
198 }
199
200 if (!dataParcel.WriteString(params)) {
201 HILOG_ERROR("fail to WriteString params");
202 return INVALID_DATA;
203 }
204
205 if (!dataParcel.WriteUint32(static_cast<uint32_t>(windowType))) {
206 HILOG_ERROR("fail to WriteUInt32 windowType");
207 return INVALID_DATA;
208 }
209
210 if (!dataParcel.WriteInt32(x)) {
211 HILOG_ERROR("fail to WriteInt32 x");
212 return INVALID_DATA;
213 }
214
215 if (!dataParcel.WriteInt32(y)) {
216 HILOG_ERROR("fail to WriteInt32 y");
217 return INVALID_DATA;
218 }
219 if (!dataParcel.WriteInt32(width)) {
220 HILOG_ERROR("fail to WriteInt32 width");
221 return INVALID_DATA;
222 }
223 if (!dataParcel.WriteInt32(height)) {
224 HILOG_ERROR("fail to WriteInt32 height");
225 return INVALID_DATA;
226 }
227
228 if (dialogCallback == nullptr) {
229 HILOG_ERROR("dialogCallback is nullptr");
230 return ERR_INVALID_VALUE;
231 }
232 if (!dataParcel.WriteRemoteObject(dialogCallback->AsObject())) {
233 HILOG_ERROR("dialogCallback error");
234 return ERR_INVALID_VALUE;
235 }
236
237 int error = Remote()->SendRequest(IUIServiceMgr::SHOW_DIALOG, dataParcel, reply, option);
238 if (error != NO_ERROR) {
239 HILOG_ERROR("Request fail, error: %{public}d", error);
240 return error;
241 }
242
243 if (id != nullptr) {
244 *id = reply.ReadInt32();
245 }
246 return reply.ReadInt32();
247 }
248
CancelDialog(int id)249 int UIServiceMgrProxy::CancelDialog(int id)
250 {
251 MessageParcel dataParcel;
252 MessageParcel reply;
253 MessageOption option;
254
255 if (!WriteInterfaceToken(dataParcel)) {
256 return UI_SERVICE_PROXY_INNER_ERR;
257 }
258
259 if (!dataParcel.WriteInt32(id)) {
260 HILOG_ERROR("fail to WriteString id");
261 return INVALID_DATA;
262 }
263
264 int error = Remote()->SendRequest(IUIServiceMgr::CANCEL_DIALOG, dataParcel, reply, option);
265 if (error != NO_ERROR) {
266 HILOG_ERROR("Request fail, error: %{public}d", error);
267 return error;
268 }
269 return reply.ReadInt32();
270 }
271 } // namespace Ace
272 } // namespace OHOS
273