1 /*
2  * Copyright (c) 2021-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 "ui_service_mgr_proxy.h"
17 
18 #include "errors.h"
19 #include "string_ex.h"
20 #include "ui_service_hilog.h"
21 #include "ui_service_mgr_errors.h"
22 #include "ui_service_proxy.h"
23 #include "ui_service_stub.h"
24 
25 namespace OHOS::Ace {
WriteInterfaceToken(MessageParcel & data)26 bool UIServiceMgrProxy::WriteInterfaceToken(MessageParcel& data)
27 {
28     if (!data.WriteInterfaceToken(UIServiceMgrProxy::GetDescriptor())) {
29         return false;
30     }
31     return true;
32 }
33 
RegisterCallBack(const AAFwk::Want & want,const sptr<IUIService> & uiService)34 int32_t UIServiceMgrProxy::RegisterCallBack(const AAFwk::Want& want, const sptr<IUIService>& uiService)
35 {
36     MessageParcel data;
37     MessageParcel reply;
38     MessageOption option(MessageOption::TF_ASYNC);
39     if (!WriteInterfaceToken(data)) {
40         return UI_SERVICE_PROXY_INNER_ERR;
41     }
42     if (!data.WriteParcelable(&want)) {
43         return ERR_INVALID_VALUE;
44     }
45     if (uiService == nullptr) {
46         return ERR_INVALID_VALUE;
47     }
48     if (!data.WriteRemoteObject(uiService->AsObject())) {
49         return ERR_INVALID_VALUE;
50     }
51     int32_t error = Remote()->SendRequest(IUIServiceMgr::REGISTER_CALLBACK, data, reply, option);
52     if (error != NO_ERROR) {
53         LOGW("register callback fail, error: %d", error);
54         return error;
55     }
56     return reply.ReadInt32();
57 }
58 
UnregisterCallBack(const AAFwk::Want & want)59 int32_t UIServiceMgrProxy::UnregisterCallBack(const AAFwk::Want& want)
60 {
61     MessageParcel data;
62     MessageParcel reply;
63     MessageOption option(MessageOption::TF_ASYNC);
64 
65     if (!WriteInterfaceToken(data)) {
66         return UI_SERVICE_PROXY_INNER_ERR;
67     }
68     data.WriteParcelable(&want);
69     int32_t error = Remote()->SendRequest(IUIServiceMgr::UNREGISTER_CALLBACK, data, reply, option);
70     if (error != NO_ERROR) {
71         LOGW("unregister callback fail, error: %d", error);
72         return error;
73     }
74     return reply.ReadInt32();
75 }
76 
Push(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)77 int32_t UIServiceMgrProxy::Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
78     const std::string& data, const std::string& extraData)
79 {
80     MessageParcel dataParcel;
81     MessageParcel reply;
82     MessageOption option(MessageOption::TF_ASYNC);
83     if (!WriteInterfaceToken(dataParcel)) {
84         return UI_SERVICE_PROXY_INNER_ERR;
85     }
86     dataParcel.WriteParcelable(&want);
87     if (!dataParcel.WriteString(name)) {
88         return INVALID_DATA;
89     }
90     if (!dataParcel.WriteString(jsonPath)) {
91         return INVALID_DATA;
92     }
93     if (!dataParcel.WriteString(data)) {
94         return INVALID_DATA;
95     }
96     if (!dataParcel.WriteString(extraData)) {
97         return INVALID_DATA;
98     }
99     int32_t error = Remote()->SendRequest(IUIServiceMgr::PUSH, dataParcel, reply, option);
100     if (error != NO_ERROR) {
101         return error;
102     }
103     return reply.ReadInt32();
104 }
105 
Request(const AAFwk::Want & want,const std::string & name,const std::string & data)106 int32_t UIServiceMgrProxy::Request(const AAFwk::Want& want, const std::string& name, const std::string& data)
107 {
108     MessageParcel dataParcel;
109     MessageParcel reply;
110     MessageOption option(MessageOption::TF_ASYNC);
111 
112     if (!WriteInterfaceToken(dataParcel)) {
113         return UI_SERVICE_PROXY_INNER_ERR;
114     }
115     dataParcel.WriteParcelable(&want);
116 
117     if (!dataParcel.WriteString(name)) {
118         return INVALID_DATA;
119     }
120 
121     if (!dataParcel.WriteString(data)) {
122         return INVALID_DATA;
123     }
124 
125     int32_t error = Remote()->SendRequest(IUIServiceMgr::REQUEST, dataParcel, reply, option);
126     if (error != NO_ERROR) {
127         return error;
128     }
129     return reply.ReadInt32();
130 }
131 
ReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)132 int32_t UIServiceMgrProxy::ReturnRequest(
133     const AAFwk::Want& want, const std::string& source, const std::string& data, const std::string& extraData)
134 {
135     MessageParcel dataParcel;
136     MessageParcel reply;
137     MessageOption option(MessageOption::TF_ASYNC);
138 
139     if (!WriteInterfaceToken(dataParcel)) {
140         return UI_SERVICE_PROXY_INNER_ERR;
141     }
142     dataParcel.WriteParcelable(&want);
143 
144     if (!dataParcel.WriteString(source)) {
145         return INVALID_DATA;
146     }
147 
148     if (!dataParcel.WriteString(data)) {
149         return INVALID_DATA;
150     }
151     if (!dataParcel.WriteString(extraData)) {
152         return INVALID_DATA;
153     }
154     int32_t error = Remote()->SendRequest(IUIServiceMgr::RETURN_REQUEST, dataParcel, reply, option);
155     if (error != NO_ERROR) {
156         return error;
157     }
158     return reply.ReadInt32();
159 }
160 } // namespace OHOS::Ace
161