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 <cinttypes>
17 #include <chrono>
18
19 #include "form_ecological_rule_service.h"
20 #include "fms_log_wrapper.h"
21 #include "iremote_broker.h"
22 #include "iservice_registry.h"
23 #include "system_ability_definition.h"
24 #include "ipc_skeleton.h"
25
26 namespace OHOS {
27 namespace AppExecFwk {
28 using namespace std::chrono;
29 static inline const std::u16string ERMS_INTERFACE_TOKEN =
30 u"ohos.cloud.ecologicalrulemgrservice.IEcologicalRuleMgrService";
31 std::mutex FormEcologicalRuleClient::instanceLock_;
32 sptr<IFormEcologicalRule> FormEcologicalRuleClient::ecologicalRuleMgrServiceProxy_;
33 sptr<IRemoteObject::DeathRecipient> FormEcologicalRuleClient::deathRecipient_;
34
35
FormEcologicalRuleClient()36 FormEcologicalRuleClient::FormEcologicalRuleClient()
37 {}
38
~FormEcologicalRuleClient()39 FormEcologicalRuleClient::~FormEcologicalRuleClient()
40 {
41 if (ecologicalRuleMgrServiceProxy_ != nullptr) {
42 auto remoteObj = ecologicalRuleMgrServiceProxy_->AsObject();
43 if (remoteObj != nullptr) {
44 remoteObj->RemoveDeathRecipient(deathRecipient_);
45 }
46 }
47 }
GetCurrentTimeMicro()48 inline int64_t GetCurrentTimeMicro()
49 {
50 return duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
51 }
52
ConnectService()53 sptr<IFormEcologicalRule> FormEcologicalRuleClient::ConnectService()
54 {
55 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
56 if (samgr == nullptr) {
57 HILOG_ERROR("GetSystemAbilityManager error");
58 return nullptr;
59 }
60
61 auto systemAbility = samgr->CheckSystemAbility(6105);
62 if (systemAbility == nullptr) {
63 HILOG_ERROR("CheckSystemAbility error, ECOLOGICALRULEMANAGERSERVICE_ID = 6105");
64 return nullptr;
65 }
66
67 deathRecipient_ = new (std::nothrow) FormEcologicalRuleDeathRecipient();
68 systemAbility->AddDeathRecipient(deathRecipient_);
69
70 sptr<IFormEcologicalRule> it = iface_cast<IFormEcologicalRule>(systemAbility);
71 if (it == nullptr) {
72 HILOG_ERROR("IFormEcologicalRule cast failed");
73 it = new FormEcologicalRuleProxy(systemAbility);
74 }
75
76 return it;
77 }
78
CheckConnectService()79 bool FormEcologicalRuleClient::CheckConnectService()
80 {
81 if (ecologicalRuleMgrServiceProxy_ == nullptr) {
82 HILOG_DEBUG("redo ConnectService");
83 ecologicalRuleMgrServiceProxy_ = ConnectService();
84 }
85 if (ecologicalRuleMgrServiceProxy_ == nullptr) {
86 HILOG_ERROR("Connect SA Failed");
87 return false;
88 }
89 return true;
90 }
91
OnRemoteSaDied(const wptr<IRemoteObject> & object)92 void FormEcologicalRuleClient::OnRemoteSaDied(const wptr<IRemoteObject> &object)
93 {
94 ecologicalRuleMgrServiceProxy_ = ConnectService();
95 }
96
IsSupportPublishForm(const std::vector<OHOS::AAFwk::Want> & wants,const CallerInfo & callerInfo,bool & bSupport)97 int32_t FormEcologicalRuleClient::IsSupportPublishForm(const std::vector<OHOS::AAFwk::Want> &wants,
98 const CallerInfo &callerInfo, bool &bSupport)
99 {
100 HILOG_DEBUG("callerInfo = %{public}s", callerInfo.ToString().c_str());
101 if (callerInfo.packageName.find_first_not_of(' ') == std::string::npos) {
102 bSupport = true;
103 HILOG_DEBUG("empty callerInfoPackageName,bSupport=true");
104 return 0;
105 }
106 if (!CheckConnectService()) {
107 return -1;
108 }
109 int32_t res = ecologicalRuleMgrServiceProxy_->IsSupportPublishForm(wants, callerInfo, bSupport);
110 HILOG_DEBUG("IsSupportPublishForm end, bSupport = %{public}d", bSupport);
111 return res;
112 }
113
OnRemoteDied(const wptr<IRemoteObject> & object)114 void FormEcologicalRuleDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
115 {
116 FormEcologicalRuleClient::GetInstance().OnRemoteSaDied(object);
117 }
118
IsSupportPublishForm(const std::vector<Want> & wants,const CallerInfo & callerInfo,bool & bSupport)119 int32_t FormEcologicalRuleProxy::IsSupportPublishForm(const std::vector<Want> &wants,
120 const CallerInfo &callerInfo, bool &bSupport)
121 {
122 HILOG_DEBUG("call");
123 MessageParcel data;
124 if (!data.WriteInterfaceToken(ERMS_INTERFACE_TOKEN)) {
125 HILOG_ERROR("write token failed");
126 return ERR_FAILED;
127 }
128 if (!data.WriteInt32(wants.size())) {
129 HILOG_ERROR("write wants size failed");
130 return ERR_FAILED;
131 }
132 for (auto &want : wants) {
133 if (!data.WriteParcelable(&want)) {
134 HILOG_ERROR("write want failed");
135 return ERR_FAILED;
136 }
137 }
138 if (!data.WriteParcelable(&callerInfo)) {
139 HILOG_ERROR("write callerInfo failed");
140 return ERR_FAILED;
141 }
142 MessageOption option = { MessageOption::TF_SYNC };
143 MessageParcel reply;
144 auto remote = Remote();
145 if (remote == nullptr) {
146 HILOG_ERROR("get Remote failed");
147 return ERR_FAILED;
148 }
149
150 std::string originId = IPCSkeleton::ResetCallingIdentity();
151 int32_t ret = remote->SendRequest(IS_SUPPORT_PUBLISH_FORM_CMD, data, reply, option);
152 IPCSkeleton::SetCallingIdentity(originId);
153 if (ret != ERR_NONE) {
154 HILOG_ERROR("SendRequest error, ret = %{public}d", ret);
155 return ERR_FAILED;
156 }
157 bSupport = reply.ReadBool();
158 HILOG_INFO("end, bSupport=%{public}d", bSupport);
159 return ERR_OK;
160 }
161 } // namespace AppExecFwk
162 } // namespace OHOS
163