1 /*
2 * Copyright (c) 2023 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 "ecological_rule_mgr_service_proxy.h"
17 #include "iremote_broker.h"
18 #include "ecological_rule_mgr_service_logger.h"
19
20 namespace OHOS {
21 namespace EcologicalRuleMgrService {
22 #define TAG "erms_proxy"
23 static inline const std::u16string ERMS_INTERFACE_TOKEN =
24 u"ohos.cloud.ecologicalrulemgrservice.IEcologicalRuleMgrService";
25
EcologicalRuleMgrServiceProxy(const sptr<IRemoteObject> & object)26 EcologicalRuleMgrServiceProxy::EcologicalRuleMgrServiceProxy(const sptr<IRemoteObject> &object)
27 : IRemoteProxy<EcologicalRuleMgrServiceInterface>(object)
28 {}
29
QueryFreeInstallExperience(const Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)30 int32_t EcologicalRuleMgrServiceProxy::QueryFreeInstallExperience(const Want &want, const CallerInfo &callerInfo,
31 ExperienceRule &rule)
32 {
33 LOG_INFO("QueryFreeInstallExperience called");
34 MessageParcel data;
35 if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
36 LOG_ERROR("write token failed");
37 return ERR_FAILED;
38 }
39 bool res = data.WriteParcelable(&want) && data.WriteParcelable(&callerInfo);
40 if (!res) {
41 LOG_ERROR("write data failed");
42 return ERR_FAILED;
43 }
44 MessageOption option = { MessageOption::TF_SYNC };
45 MessageParcel reply;
46 auto remote = Remote();
47 if (remote == nullptr) {
48 LOG_ERROR("get Remote failed");
49 return ERR_FAILED;
50 }
51 int32_t ret = remote->SendRequest(QUERY_FREE_INSTALL_EXPERIENCE_CMD, data, reply, option);
52 if (ret != ERR_NONE) {
53 LOG_ERROR("SendRequest error, ret = %{public}d", ret);
54 return ERR_FAILED;
55 }
56 sptr<ExperienceRule> sptrRule = reply.ReadParcelable<ExperienceRule>();
57 if (sptrRule == nullptr) {
58 LOG_ERROR("readParcelable sptrRule error");
59 return ERR_FAILED;
60 }
61 rule = *sptrRule;
62 LOG_INFO("QueryFreeInstallExperience end");
63 return ERR_OK;
64 }
65
EvaluateResolveInfos(const Want & want,const CallerInfo & callerInfo,int32_t type,std::vector<AbilityInfo> & abilityInfos)66 int32_t EcologicalRuleMgrServiceProxy::EvaluateResolveInfos(const Want &want, const CallerInfo &callerInfo,
67 int32_t type, std::vector<AbilityInfo> &abilityInfos)
68 {
69 LOG_INFO("EvaluateResolveInfos called");
70 MessageParcel data;
71 if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
72 LOG_ERROR("write token failed");
73 return ERR_FAILED;
74 }
75 bool res = data.WriteParcelable(&want) && data.WriteInt32(type) &&
76 data.WriteInt32(abilityInfos.size());
77 if (!res) {
78 LOG_ERROR("write data failed");
79 return ERR_FAILED;
80 }
81 for (auto &abilityInfo : abilityInfos) {
82 if (!data.WriteParcelable(&abilityInfo)) {
83 LOG_ERROR("write abilityInfo failed");
84 return ERR_FAILED;
85 }
86 }
87 if (!data.WriteParcelable(&callerInfo)) {
88 LOG_ERROR("write callerInfo failed");
89 return ERR_FAILED;
90 }
91 MessageOption option = { MessageOption::TF_SYNC };
92 MessageParcel reply;
93 auto remote = Remote();
94 if (remote == nullptr) {
95 LOG_ERROR("get Remote failed.");
96 return ERR_FAILED;
97 }
98 int32_t ret = remote->SendRequest(EVALUATE_RESOLVE_INFO_CMD, data, reply, option);
99 if (ret != ERR_NONE) {
100 LOG_ERROR("SendRequest error, ret = %{public}d", ret);
101 return ERR_FAILED;
102 }
103 LOG_INFO("EvaluateResolveInfos end");
104 return ERR_OK;
105 }
106
QueryStartExperience(const Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)107 int32_t EcologicalRuleMgrServiceProxy::QueryStartExperience(const Want &want, const CallerInfo &callerInfo,
108 ExperienceRule &rule)
109 {
110 LOG_INFO("QueryStartExperience called");
111 MessageParcel data;
112 if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
113 LOG_ERROR("write token failed");
114 return ERR_FAILED;
115 }
116 bool res = data.WriteParcelable(&want) && data.WriteParcelable(&callerInfo);
117 if (!res) {
118 LOG_ERROR("write data failed");
119 return ERR_FAILED;
120 }
121 MessageOption option = { MessageOption::TF_SYNC };
122 MessageParcel reply;
123 auto remote = Remote();
124 if (remote == nullptr) {
125 LOG_ERROR("get Remote failed");
126 return ERR_FAILED;
127 }
128 int32_t ret = remote->SendRequest(QUERY_START_EXPERIENCE_CMD, data, reply, option);
129 if (ret != ERR_NONE) {
130 LOG_ERROR("SendRequest error, ret = %{public}d", ret);
131 return ERR_FAILED;
132 }
133 sptr<ExperienceRule> sptrRule = reply.ReadParcelable<ExperienceRule>();
134 if (sptrRule == nullptr) {
135 LOG_ERROR("ReadParcelable sptrRule error");
136 return ERR_FAILED;
137 }
138 rule = *sptrRule;
139 LOG_INFO("QueryStartExperience end");
140 return ERR_OK;
141 }
142
IsSupportPublishForm(const std::vector<Want> & wants,const CallerInfo & callerInfo,bool & bSupport)143 int32_t EcologicalRuleMgrServiceProxy::IsSupportPublishForm(const std::vector<Want> &wants,
144 const CallerInfo &callerInfo, bool &bSupport)
145 {
146 LOG_INFO("IsSupportPublishForm called");
147 MessageParcel data;
148 if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
149 LOG_ERROR("write token failed");
150 return ERR_FAILED;
151 }
152 if (!data.WriteInt32(wants.size())) {
153 LOG_ERROR("write wants size failed");
154 return ERR_FAILED;
155 }
156 for (auto &want : wants) {
157 if (!data.WriteParcelable(&want)) {
158 LOG_ERROR("write want failed");
159 return ERR_FAILED;
160 }
161 }
162 if (!data.WriteParcelable(&callerInfo)) {
163 LOG_ERROR("write callerInfo failed");
164 return ERR_FAILED;
165 }
166 MessageOption option = { MessageOption::TF_SYNC };
167 MessageParcel reply;
168 auto remote = Remote();
169 if (remote == nullptr) {
170 LOG_ERROR("get Remote failed");
171 return ERR_FAILED;
172 }
173 int32_t ret = remote->SendRequest(IS_SUPPORT_PUBLISH_FORM_CMD, data, reply, option);
174 if (ret != ERR_NONE) {
175 LOG_ERROR("SendRequest error, ret = %{public}d", ret);
176 return ERR_FAILED;
177 }
178 bSupport = reply.ReadBool();
179 LOG_INFO("IsSupportPublishForm end, bSupport=%{public}d", bSupport);
180 return ERR_OK;
181 }
182
183 template <typename T>
ReadParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)184 bool EcologicalRuleMgrServiceProxy::ReadParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
185 {
186 int32_t infoSize = reply.ReadInt32();
187 parcelableVector.clear();
188 for (int32_t i = 0; i < infoSize; i++) {
189 sptr<T> info = reply.ReadParcelable<T>();
190 if (info == nullptr) {
191 LOG_ERROR("read Parcelable infos failed");
192 return false;
193 }
194 parcelableVector.emplace_back(*info);
195 }
196 return true;
197 }
198 } // namespace EcologicalRuleMgrService
199 } // namespace OHOS
200