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_proxy.h"
17 #include "hilog_wrapper.h"
18 #include "message_parcel.h"
19 #include "want.h"
20
21 namespace OHOS {
22 namespace Ace {
UIServiceProxy(const sptr<IRemoteObject> & remote)23 UIServiceProxy::UIServiceProxy(const sptr<IRemoteObject>& remote)
24 : IRemoteProxy<IUIService>(remote)
25 {}
~UIServiceProxy()26 UIServiceProxy::~UIServiceProxy()
27 {}
28
OnPushCallBack(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)29 void UIServiceProxy::OnPushCallBack(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
30 const std::string& data, const std::string& extraData)
31 {
32 auto remote = Remote();
33 if (remote == nullptr) {
34 HILOG_ERROR("%{public}s remote is nullptr", __func__);
35 return;
36 }
37
38 OHOS::MessageParcel dataParcel;
39 OHOS::MessageParcel reply;
40 OHOS::MessageOption option;
41
42 if (!dataParcel.WriteInterfaceToken(UIServiceProxy::GetDescriptor())) {
43 HILOG_ERROR("%{public}s dataParcel.WriteInterfaceToken(GetDescriptor()) return false", __func__);
44 return;
45 }
46 if (!dataParcel.WriteParcelable(&want)) {
47 HILOG_ERROR("want write failed.");
48 return;
49 }
50 if (!dataParcel.WriteString(name)) {
51 HILOG_ERROR("fail to WriteString name");
52 return;
53 }
54 if (!dataParcel.WriteString(jsonPath)) {
55 HILOG_ERROR("fail to WriteString jsonPath");
56 return;
57 }
58 if (!dataParcel.WriteString(data)) {
59 HILOG_ERROR("fail to WriteString data");
60 return;
61 }
62 if (!dataParcel.WriteString(extraData)) {
63 HILOG_ERROR("fail to WriteString extraData");
64 return;
65 }
66 int32_t result = remote->SendRequest(IUIService::UI_SERVICE_PUSH_CALL_BACK, dataParcel, reply, option);
67 if (result == ERR_NONE) {
68 HILOG_INFO("%{public}s SendRequest ok, retval is %d", __func__, reply.ReadInt32());
69 return;
70 } else {
71 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
72 return;
73 }
74 }
75
OnRequestCallBack(const AAFwk::Want & want,const std::string & name,const std::string & data)76 void UIServiceProxy::OnRequestCallBack(const AAFwk::Want& want, const std::string& name, const std::string& data)
77 {
78 auto remote = Remote();
79 if (remote == nullptr) {
80 HILOG_ERROR("%{public}s remote is nullptr", __func__);
81 return;
82 }
83
84 OHOS::MessageParcel dataParcel;
85 OHOS::MessageParcel reply;
86 OHOS::MessageOption option;
87
88 if (!dataParcel.WriteInterfaceToken(UIServiceProxy::GetDescriptor())) {
89 HILOG_ERROR("%{public}s dataParcel.WriteInterfaceToken(GetDescriptor()) return false", __func__);
90 return;
91 }
92 if (!dataParcel.WriteParcelable(&want)) {
93 HILOG_ERROR("want write failed.");
94 return;
95 }
96 if (!dataParcel.WriteString(name)) {
97 HILOG_ERROR("fail to WriteString name");
98 return;
99 }
100 if (!dataParcel.WriteString(data)) {
101 HILOG_ERROR("fail to WriteString data");
102 return;
103 }
104 int32_t result = remote->SendRequest(IUIService::UI_SERVICE_REQUEST_CALL_BACK, dataParcel, reply, option);
105 if (result == ERR_NONE) {
106 HILOG_INFO("%{public}s SendRequest ok, retval is %d", __func__, reply.ReadInt32());
107 return;
108 } else {
109 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
110 return;
111 }
112 }
113
OnReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)114 void UIServiceProxy::OnReturnRequest(
115 const AAFwk::Want& want, const std::string& source, const std::string& data, const std::string& extraData)
116 {
117 auto remote = Remote();
118 if (remote == nullptr) {
119 HILOG_ERROR("%{public}s remote is nullptr", __func__);
120 return;
121 }
122
123 OHOS::MessageParcel dataParcel;
124 OHOS::MessageParcel reply;
125 OHOS::MessageOption option;
126
127 if (!dataParcel.WriteInterfaceToken(UIServiceProxy::GetDescriptor())) {
128 HILOG_ERROR("%{public}s dataParcel.WriteInterfaceToken(GetDescriptor()) return false", __func__);
129 return;
130 }
131 if (!dataParcel.WriteParcelable(&want)) {
132 HILOG_ERROR("want write failed.");
133 return;
134 }
135 if (!dataParcel.WriteString(source)) {
136 HILOG_ERROR("fail to WriteString source");
137 return;
138 }
139 if (!dataParcel.WriteString(data)) {
140 HILOG_ERROR("fail to WriteString data");
141 return;
142 }
143 if (!dataParcel.WriteString(extraData)) {
144 HILOG_ERROR("fail to WriteString extraData");
145 return;
146 }
147 int32_t result = remote->SendRequest(IUIService::UI_SERVICE_RETURN_REQUEST, dataParcel, reply, option);
148 if (result == ERR_NONE) {
149 HILOG_INFO("%{public}s SendRequest ok, retval is %d", __func__, reply.ReadInt32());
150 return;
151 } else {
152 HILOG_ERROR("%{public}s SendRequest error, result=%{public}d", __func__, result);
153 return;
154 }
155 }
156 } // namespace Ace
157 } // namespace OHOS
158