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 <thread>
18 #include "ecological_rule_mgr_service.h"
19 #include "system_ability_definition.h"
20 #include "iservice_registry.h"
21 #include "if_system_ability_manager.h"
22 #include "uri.h"
23 #include "want.h"
24 #include "ecological_rule_mgr_service_logger.h"
25 
26 namespace OHOS {
27 namespace EcologicalRuleMgrService {
28 #define TAG "ERMS_MAIN"
29 
30 REGISTER_SYSTEM_ABILITY_BY_ID(EcologicalRuleMgrService, 6105, true);
31 
32 std::mutex EcologicalRuleMgrService::instanceLock_;
33 sptr<EcologicalRuleMgrService> EcologicalRuleMgrService::instance_;
34 
EcologicalRuleMgrService()35 EcologicalRuleMgrService::EcologicalRuleMgrService() : state_(ServiceRunningState::STATE_NOT_START)
36 {
37 }
38 
EcologicalRuleMgrService(int32_t saId,bool runOnCreate)39 EcologicalRuleMgrService::EcologicalRuleMgrService(int32_t saId, bool runOnCreate)
40     : SystemAbility(saId, runOnCreate), state_(ServiceRunningState::STATE_NOT_START)
41 {
42 }
43 
~EcologicalRuleMgrService()44 EcologicalRuleMgrService::~EcologicalRuleMgrService() {}
45 
GetInstance()46 sptr<EcologicalRuleMgrService> EcologicalRuleMgrService::GetInstance()
47 {
48     if (instance_ == nullptr) {
49         std::lock_guard<std::mutex> autoLock(instanceLock_);
50         if (instance_ == nullptr) {
51             instance_ = new EcologicalRuleMgrService;
52         }
53     }
54     return instance_;
55 }
56 
QueryFreeInstallExperience(const Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)57 int32_t EcologicalRuleMgrService::QueryFreeInstallExperience(const Want &want, const CallerInfo &callerInfo,
58     ExperienceRule &rule)
59 {
60     LOG_DEBUG("QueryFreeInstallExperience want name = %{public}s, caller name = %{public}s",
61         want.GetBundle().c_str(), callerInfo.packageName.c_str());
62     return SUCCESS;
63 }
64 
EvaluateResolveInfos(const Want & want,const CallerInfo & callerInfo,int32_t type,std::vector<AbilityInfo> & abilityInfos)65 int32_t EcologicalRuleMgrService::EvaluateResolveInfos(const Want &want, const CallerInfo &callerInfo, int32_t type,
66     std::vector<AbilityInfo> &abilityInfos)
67 {
68     LOG_DEBUG("EvaluateResolveInfos want name = %{public}s, caller name = %{public}s, type = %{public}d",
69         want.GetBundle().c_str(), callerInfo.packageName.c_str(), type);
70     return SUCCESS;
71 }
72 
QueryStartExperience(const Want & want,const CallerInfo & callerInfo,ExperienceRule & rule)73 int32_t EcologicalRuleMgrService::QueryStartExperience(const Want &want, const CallerInfo &callerInfo,
74     ExperienceRule &rule)
75 {
76     std::string bundleName = want.GetBundle();
77     LOG_INFO("want bundle name = %{public}s, uri = %{public}s", bundleName.c_str(), want.GetUriString().c_str());
78     LOG_INFO("callerInfo bundle name = %{public}s, callerAppType = %{public}d, targetAppType = %{public}d",
79         callerInfo.packageName.c_str(), callerInfo.callerAppType, callerInfo.targetAppType);
80 
81     return SUCCESS;
82 }
83 
IsSupportPublishForm(const std::vector<Want> & wants,const CallerInfo & callerInfo,bool & bSupport)84 int32_t EcologicalRuleMgrService::IsSupportPublishForm(const std::vector<Want> &wants, const CallerInfo &callerInfo,
85     bool &bSupport)
86 {
87     std::string callerName = callerInfo.packageName;
88     LOG_DEBUG("IsSupportPublishForm callerName = %s, callerType = %d", callerName.c_str(), callerInfo.callerAppType);
89     return SUCCESS;
90 }
91 
OnStart()92 void EcologicalRuleMgrService::OnStart()
93 {
94     LOG_INFO("EcologicalRuleMgrService OnStart");
95     if (state_ == ServiceRunningState::STATE_RUNNING) {
96         LOG_WARN("EcologicalRuleMgrService is already running");
97         return;
98     }
99 
100     bool ret = Publish(EcologicalRuleMgrService::GetInstance());
101     if (!ret) {
102         LOG_ERROR("Publish ecological rule mgr service failed: %{public}d", ret);
103         return;
104     }
105     state_ = ServiceRunningState::STATE_RUNNING;
106     LOG_INFO("EcologicalRuleMgrService OnStart finish");
107     return;
108 }
109 
OnStop()110 void EcologicalRuleMgrService::OnStop()
111 {
112     if (state_ != ServiceRunningState::STATE_RUNNING) {
113         LOG_WARN("EcologicalRuleMgrService is not running");
114         return;
115     }
116     state_ = ServiceRunningState::STATE_NOT_START;
117     LOG_INFO("EcologicalRuleMgrService OnStop finish");
118 }
119 } // namespace EcologicalRuleMgrService
120 } // namespace OHOS