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