• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_mgr_errors.h"
21 #include "ui_service_proxy.h"
22 #include "ui_service_stub.h"
23 
24 namespace OHOS::Ace {
WriteInterfaceToken(MessageParcel & data)25 bool UIServiceMgrProxy::WriteInterfaceToken(MessageParcel& data)
26 {
27     if (!data.WriteInterfaceToken(UIServiceMgrProxy::GetDescriptor())) {
28         HILOG_ERROR("write interface token failed");
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;
39     if (!WriteInterfaceToken(data)) {
40         return UI_SERVICE_PROXY_INNER_ERR;
41     }
42     if (!data.WriteParcelable(&want)) {
43         HILOG_ERROR("register callback fail, want error");
44         return ERR_INVALID_VALUE;
45     }
46     if (uiService == nullptr) {
47         HILOG_ERROR("register callback fail, uiService is nullptr");
48         return ERR_INVALID_VALUE;
49     }
50     if (!data.WriteRemoteObject(uiService->AsObject())) {
51         HILOG_ERROR("register callback fail, uiService error");
52         return ERR_INVALID_VALUE;
53     }
54     int32_t error = Remote()->SendRequest(IUIServiceMgr::REGISTER_CALLBACK, data, reply, option);
55     if (error != NO_ERROR) {
56         HILOG_ERROR("register callback fail, error: %d", error);
57         return error;
58     }
59     return reply.ReadInt32();
60 }
61 
UnregisterCallBack(const AAFwk::Want & want)62 int32_t UIServiceMgrProxy::UnregisterCallBack(const AAFwk::Want& want)
63 {
64     MessageParcel data;
65     MessageParcel reply;
66     MessageOption option;
67 
68     if (!WriteInterfaceToken(data)) {
69         return UI_SERVICE_PROXY_INNER_ERR;
70     }
71     data.WriteParcelable(&want);
72     int32_t error = Remote()->SendRequest(IUIServiceMgr::UNREGISTER_CALLBACK, data, reply, option);
73     if (error != NO_ERROR) {
74         HILOG_ERROR("unregister callback fail, error: %d", error);
75         return error;
76     }
77     return reply.ReadInt32();
78 }
79 
Push(const AAFwk::Want & want,const std::string & name,const std::string & jsonPath,const std::string & data,const std::string & extraData)80 int32_t UIServiceMgrProxy::Push(const AAFwk::Want& want, const std::string& name, const std::string& jsonPath,
81     const std::string& data, const std::string& extraData)
82 {
83     MessageParcel dataParcel;
84     MessageParcel reply;
85     MessageOption option;
86     if (!WriteInterfaceToken(dataParcel)) {
87         return UI_SERVICE_PROXY_INNER_ERR;
88     }
89     dataParcel.WriteParcelable(&want);
90     if (!dataParcel.WriteString(name)) {
91         HILOG_ERROR("fail to WriteString name");
92         return INVALID_DATA;
93     }
94     if (!dataParcel.WriteString(jsonPath)) {
95         HILOG_ERROR("fail to WriteString jsonPath");
96         return INVALID_DATA;
97     }
98     if (!dataParcel.WriteString(data)) {
99         HILOG_ERROR("fail to WriteString data");
100         return INVALID_DATA;
101     }
102     if (!dataParcel.WriteString(extraData)) {
103         HILOG_ERROR("fail to WriteString extraData");
104         return INVALID_DATA;
105     }
106     int32_t error = Remote()->SendRequest(IUIServiceMgr::PUSH, dataParcel, reply, option);
107     if (error != NO_ERROR) {
108         HILOG_ERROR("Push fail, error: %d", error);
109         return error;
110     }
111     return reply.ReadInt32();
112 }
113 
Request(const AAFwk::Want & want,const std::string & name,const std::string & data)114 int32_t UIServiceMgrProxy::Request(const AAFwk::Want& want, const std::string& name, const std::string& data)
115 {
116     MessageParcel dataParcel;
117     MessageParcel reply;
118     MessageOption option;
119 
120     if (!WriteInterfaceToken(dataParcel)) {
121         return UI_SERVICE_PROXY_INNER_ERR;
122     }
123     dataParcel.WriteParcelable(&want);
124 
125     if (!dataParcel.WriteString(name)) {
126         HILOG_ERROR("fail to WriteString name");
127         return INVALID_DATA;
128     }
129 
130     if (!dataParcel.WriteString(data)) {
131         HILOG_ERROR("fail to WriteString data");
132         return INVALID_DATA;
133     }
134 
135     int32_t error = Remote()->SendRequest(IUIServiceMgr::REQUEST, dataParcel, reply, option);
136     if (error != NO_ERROR) {
137         HILOG_ERROR("Request fail, error: %d", error);
138         return error;
139     }
140     return reply.ReadInt32();
141 }
142 
ReturnRequest(const AAFwk::Want & want,const std::string & source,const std::string & data,const std::string & extraData)143 int32_t UIServiceMgrProxy::ReturnRequest(const AAFwk::Want& want, const std::string& source, const std::string& data,
144     const std::string& extraData)
145 {
146     MessageParcel dataParcel;
147     MessageParcel reply;
148     MessageOption option;
149 
150     if (!WriteInterfaceToken(dataParcel)) {
151         return UI_SERVICE_PROXY_INNER_ERR;
152     }
153     dataParcel.WriteParcelable(&want);
154 
155     if (!dataParcel.WriteString(source)) {
156         HILOG_ERROR("fail to WriteString source");
157         return INVALID_DATA;
158     }
159 
160     if (!dataParcel.WriteString(data)) {
161         HILOG_ERROR("fail to WriteString data");
162         return INVALID_DATA;
163     }
164     if (!dataParcel.WriteString(extraData)) {
165         HILOG_ERROR("fail to WriteString extraData");
166         return INVALID_DATA;
167     }
168     int32_t error = Remote()->SendRequest(IUIServiceMgr::RETURN_REQUEST, dataParcel, reply, option);
169     if (error != NO_ERROR) {
170         HILOG_ERROR("Request fail, error: %d", error);
171         return error;
172     }
173     return reply.ReadInt32();
174 }
175 }
176