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