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 <chrono>
17 #include <string>
18 #include <cinttypes>
19 #include "ecological_rule_mgr_service_logger.h"
20 #include "iservice_registry.h"
21 #include "system_ability_definition.h"
22 #include "ecological_rule_mgr_service_client.h"
23 
24 namespace OHOS {
25 namespace EcologicalRuleMgrService {
26 using namespace std::chrono;
27 
28 #define TAG "ERMS_CLIENT"
29 
30 std::mutex EcologicalRuleMgrServiceClient::instanceLock_;
31 sptr<EcologicalRuleMgrServiceClient> EcologicalRuleMgrServiceClient::instance_;
32 sptr<EcologicalRuleMgrServiceInterface> EcologicalRuleMgrServiceClient::ecologicalRuleMgrServiceProxy_;
33 sptr<EcologicalRuleMgrServiceDeathRecipient> EcologicalRuleMgrServiceClient::deathRecipient_;
34 
35 std::string EcologicalRuleMgrServiceClient::ERMS_ORIGINAL_TARGET = "ecological_experience_original_target";
36 
GetCurrentTimeMicro()37 inline int64_t GetCurrentTimeMicro()
38 {
39     return duration_cast<microseconds>(system_clock::now().time_since_epoch()).count();
40 }
41 
EcologicalRuleMgrServiceClient()42 EcologicalRuleMgrServiceClient::EcologicalRuleMgrServiceClient() {}
43 
~EcologicalRuleMgrServiceClient()44 EcologicalRuleMgrServiceClient::~EcologicalRuleMgrServiceClient()
45 {
46     if (ecologicalRuleMgrServiceProxy_ != nullptr) {
47         auto remoteObj = ecologicalRuleMgrServiceProxy_->AsObject();
48         if (remoteObj != nullptr) {
49             remoteObj->RemoveDeathRecipient(deathRecipient_);
50         }
51     }
52 }
53 
GetInstance()54 sptr<EcologicalRuleMgrServiceClient> EcologicalRuleMgrServiceClient::GetInstance()
55 {
56     if (instance_ == nullptr) {
57         std::lock_guard<std::mutex> autoLock(instanceLock_);
58         if (instance_ == nullptr) {
59             instance_ = new EcologicalRuleMgrServiceClient;
60         }
61     }
62     return instance_;
63 }
64 
ConnectService()65 sptr<EcologicalRuleMgrServiceInterface> EcologicalRuleMgrServiceClient::ConnectService()
66 {
67     sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
68     if (samgr == nullptr) {
69         LOG_ERROR("GetSystemAbilityManager error");
70         return nullptr;
71     }
72     auto systemAbility = samgr->CheckSystemAbility(6105);
73     if (systemAbility == nullptr) {
74         LOG_ERROR("CheckSystemAbility error, ECOLOGICALRULEMANAGERSERVICE_ID = 6105");
75         return nullptr;
76     }
77     deathRecipient_ = new EcologicalRuleMgrServiceDeathRecipient();
78     systemAbility->AddDeathRecipient(deathRecipient_);
79     return iface_cast<EcologicalRuleMgrServiceInterface>(systemAbility);
80 }
81 
CheckConnectService()82 bool EcologicalRuleMgrServiceClient::CheckConnectService()
83 {
84     if (ecologicalRuleMgrServiceProxy_ == nullptr) {
85         LOG_WARN("redo ConnectService");
86         ecologicalRuleMgrServiceProxy_ = ConnectService();
87     }
88     if (ecologicalRuleMgrServiceProxy_ == nullptr) {
89         LOG_ERROR("Connect SA Failed");
90         return false;
91     }
92     return true;
93 }
94 
OnRemoteSaDied(const wptr<IRemoteObject> & object)95 void EcologicalRuleMgrServiceClient::OnRemoteSaDied(const wptr<IRemoteObject> &object)
96 {
97     ecologicalRuleMgrServiceProxy_ = ConnectService();
98 }
99 
QueryFreeInstallExperience(const OHOS::AAFwk::Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)100 int32_t EcologicalRuleMgrServiceClient::QueryFreeInstallExperience(const OHOS::AAFwk::Want &want,
101     const CallerInfo &callerInfo, ExperienceRule &rule)
102 {
103     int64_t start = GetCurrentTimeMicro();
104     LOG_DEBUG("QueryFreeInstallExperience want = %{public}s, callerInfo = %{public}s", want.ToString().c_str(),
105         callerInfo.ToString().c_str());
106     if (callerInfo.packageName.find_first_not_of(' ') == std::string::npos) {
107         rule.isAllow = true;
108         LOG_DEBUG("callerInfo packageName is empty, allow = true");
109         return 0;
110     }
111     if (!CheckConnectService()) {
112         return -1;
113     }
114     int32_t res = ecologicalRuleMgrServiceProxy_->QueryFreeInstallExperience(want, callerInfo, rule);
115     int64_t cost = GetCurrentTimeMicro() - start;
116     LOG_DEBUG("[ERMS-DFX] QueryFreeInstallExperience interface cost %{public}" PRId64 " mirco seconds.", cost);
117     return res;
118 }
119 
EvaluateResolveInfos(const AAFwk::Want & want,const CallerInfo & callerInfo,int32_t type,vector<AbilityInfo> & abilityInfos,const vector<ExtensionAbilityInfo> & extInfos)120 int32_t EcologicalRuleMgrServiceClient::EvaluateResolveInfos(const AAFwk::Want &want, const CallerInfo &callerInfo,
121     int32_t type, vector<AbilityInfo> &abilityInfos, const vector<ExtensionAbilityInfo> &extInfos)
122 {
123     int64_t start = GetCurrentTimeMicro();
124     LOG_DEBUG("want: %{public}s, callerInfo: %{public}s, type: %{public}d", want.ToString().c_str(),
125         callerInfo.ToString().c_str(), type);
126     if (!CheckConnectService()) {
127         return -1;
128     }
129     int32_t res = ecologicalRuleMgrServiceProxy_->EvaluateResolveInfos(want, callerInfo, type, abilityInfos);
130     int64_t cost = GetCurrentTimeMicro() - start;
131     LOG_DEBUG("[ERMS-DFX] EvaluateResolveInfos interface cost %{public}" PRId64 " mirco seconds.", cost);
132     return res;
133 }
134 
QueryStartExperience(const OHOS::AAFwk::Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)135 int32_t EcologicalRuleMgrServiceClient::QueryStartExperience(const OHOS::AAFwk::Want &want,
136     const CallerInfo &callerInfo, ExperienceRule &rule)
137 {
138     int64_t start = GetCurrentTimeMicro();
139     LOG_DEBUG("callerInfo: %{public}s, want: %{public}s", callerInfo.ToString().c_str(), want.ToString().c_str());
140     if (callerInfo.packageName.find_first_not_of(' ') == std::string::npos) {
141         rule.isAllow = true;
142         LOG_DEBUG("callerInfo packageName is empty, allow = true");
143         return 0;
144     }
145     if (!CheckConnectService()) {
146         return -1;
147     }
148     int32_t res = ecologicalRuleMgrServiceProxy_->QueryStartExperience(want, callerInfo, rule);
149     if (rule.replaceWant != nullptr) {
150         rule.replaceWant->SetParam(ERMS_ORIGINAL_TARGET, want.ToString());
151     }
152     int64_t cost = GetCurrentTimeMicro() - start;
153     LOG_DEBUG("[ERMS-DFX] QueryStartExperience interface cost %{public}" PRId64 " mirco seconds.", cost);
154     return res;
155 }
156 
IsSupportPublishForm(const std::vector<OHOS::AAFwk::Want> & wants,const CallerInfo & callerInfo,bool & bSupport)157 int32_t EcologicalRuleMgrServiceClient::IsSupportPublishForm(const std::vector<OHOS::AAFwk::Want> &wants,
158     const CallerInfo &callerInfo, bool &bSupport)
159 {
160     int64_t start = GetCurrentTimeMicro();
161     LOG_DEBUG("callerInfo = %{public}s", callerInfo.ToString().c_str());
162     if (callerInfo.packageName.find_first_not_of(' ') == std::string::npos) {
163         bSupport = true;
164         LOG_DEBUG("callerInfo packageName is empty, bSupport = true");
165         return 0;
166     }
167     if (!CheckConnectService()) {
168         return -1;
169     }
170     int32_t res = ecologicalRuleMgrServiceProxy_->IsSupportPublishForm(wants, callerInfo, bSupport);
171     LOG_DEBUG("IsSupportPublishForm end, bSupport = %{public}d", bSupport);
172     int64_t cost = GetCurrentTimeMicro() - start;
173     LOG_DEBUG("[ERMS-DFX] IsSupportPublishForm interface cost %{public}" PRId64 " mirco seconds.", cost);
174     return res;
175 }
176 
EcologicalRuleMgrServiceDeathRecipient()177 EcologicalRuleMgrServiceDeathRecipient::EcologicalRuleMgrServiceDeathRecipient() {}
178 
OnRemoteDied(const wptr<IRemoteObject> & object)179 void EcologicalRuleMgrServiceDeathRecipient::OnRemoteDied(const wptr<IRemoteObject> &object)
180 {
181     EcologicalRuleMgrServiceClient::GetInstance()->OnRemoteSaDied(object);
182 }
183 } // namespace EcologicalRuleMgrService
184 } // namespace OHOS