• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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<BmsExperienceRule> sptrRule = reply.ReadParcelable<BmsExperienceRule>();
57     if (sptrRule == nullptr) {
58         LOG_ERROR("readParcelable sptrRule error");
59         return ERR_FAILED;
60     }
61     rule.replaceWant = sptrRule->replaceWant;
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<AmsExperienceRule> sptrRule = reply.ReadParcelable<AmsExperienceRule>();
134     if (sptrRule == nullptr) {
135         LOG_ERROR("ReadParcelable sptrRule error");
136         return ERR_FAILED;
137     }
138     rule.resultCode = sptrRule->resultCode;
139     rule.replaceWant = sptrRule->replaceWant;
140     LOG_INFO("QueryStartExperience end");
141     return ERR_OK;
142 }
143 
IsSupportPublishForm(const std::vector<Want> & wants,const CallerInfo & callerInfo,bool & bSupport)144 int32_t EcologicalRuleMgrServiceProxy::IsSupportPublishForm(const std::vector<Want> &wants,
145     const CallerInfo &callerInfo, bool &bSupport)
146 {
147     LOG_INFO("IsSupportPublishForm called");
148     MessageParcel data;
149     if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
150         LOG_ERROR("write token failed");
151         return ERR_FAILED;
152     }
153     if (!data.WriteInt32(wants.size())) {
154         LOG_ERROR("write wants size failed");
155         return ERR_FAILED;
156     }
157     for (auto &want : wants) {
158         if (!data.WriteParcelable(&want)) {
159             LOG_ERROR("write want failed");
160             return ERR_FAILED;
161         }
162     }
163     if (!data.WriteParcelable(&callerInfo)) {
164         LOG_ERROR("write callerInfo failed");
165         return ERR_FAILED;
166     }
167     MessageOption option = { MessageOption::TF_SYNC };
168     MessageParcel reply;
169     auto remote = Remote();
170     if (remote == nullptr) {
171         LOG_ERROR("get Remote failed");
172         return ERR_FAILED;
173     }
174     int32_t ret = remote->SendRequest(IS_SUPPORT_PUBLISH_FORM_CMD, data, reply, option);
175     if (ret != ERR_NONE) {
176         LOG_ERROR("SendRequest error, ret = %{public}d", ret);
177         return ERR_FAILED;
178     }
179     bSupport = reply.ReadBool();
180     LOG_INFO("IsSupportPublishForm end, bSupport=%{public}d", bSupport);
181     return ERR_OK;
182 }
183 
184 template <typename T>
ReadParcelableVector(std::vector<T> & parcelableVector,MessageParcel & reply)185 bool EcologicalRuleMgrServiceProxy::ReadParcelableVector(std::vector<T> &parcelableVector, MessageParcel &reply)
186 {
187     int32_t infoSize = reply.ReadInt32();
188     parcelableVector.clear();
189     for (int32_t i = 0; i < infoSize; i++) {
190         sptr<T> info = reply.ReadParcelable<T>();
191         if (info == nullptr) {
192             LOG_ERROR("read Parcelable infos failed");
193             return false;
194         }
195         parcelableVector.emplace_back(*info);
196     }
197     return true;
198 }
199 } // namespace EcologicalRuleMgrService
200 } // namespace OHOS
201