1 /* 2 * Copyright (c) 2021 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 "bundle_mgr_service.h" 17 18 #include "datetime_ex.h" 19 #include "app_log_wrapper.h" 20 #include "perf_profile.h" 21 #include "system_ability_definition.h" 22 #include "bundle_constants.h" 23 #include "system_ability_helper.h" 24 25 namespace OHOS { 26 namespace AppExecFwk { 27 28 const bool REGISTER_RESULT = 29 SystemAbility::MakeAndRegisterAbility(DelayedSingleton<BundleMgrService>::GetInstance().get()); 30 BundleMgrService()31BundleMgrService::BundleMgrService() : SystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, true) 32 { 33 APP_LOGI("instance is created"); 34 PerfProfile::GetInstance().SetBmsLoadStartTime(GetTickCount()); 35 } 36 ~BundleMgrService()37BundleMgrService::~BundleMgrService() 38 { 39 if (host_ != nullptr) { 40 host_ = nullptr; 41 } 42 if (installer_ != nullptr) { 43 installer_ = nullptr; 44 } 45 if (handler_) { 46 handler_.reset(); 47 } 48 if (runner_) { 49 runner_.reset(); 50 } 51 if (dataMgr_) { 52 dataMgr_.reset(); 53 } 54 if (perChangeSub_) { 55 perChangeSub_.reset(); 56 } 57 APP_LOGI("instance is destroyed"); 58 } 59 OnStart()60void BundleMgrService::OnStart() 61 { 62 APP_LOGD("start is triggered"); 63 if (!Init()) { 64 APP_LOGE("init fail"); 65 return; 66 } 67 68 if (!registerToService_) { 69 if (!SystemAbilityHelper::AddSystemAbility(BUNDLE_MGR_SERVICE_SYS_ABILITY_ID, host_)) { 70 APP_LOGE("fail to register to system ability manager"); 71 return; 72 } 73 APP_LOGI("register to sam success"); 74 registerToService_ = true; 75 } 76 77 PerfProfile::GetInstance().SetBmsLoadEndTime(GetTickCount()); 78 if (!needToScan_) { 79 PerfProfile::GetInstance().Dump(); 80 } 81 } 82 OnStop()83void BundleMgrService::OnStop() 84 { 85 APP_LOGI("OnStop is called"); 86 SelfClean(); 87 if (perChangeSub_) { 88 EventFwk::CommonEventManager::UnSubscribeCommonEvent(perChangeSub_); 89 } 90 } 91 IsServiceReady() const92bool BundleMgrService::IsServiceReady() const 93 { 94 return ready_; 95 } 96 Init()97bool BundleMgrService::Init() 98 { 99 if (ready_) { 100 APP_LOGW("init more than one time"); 101 return false; 102 } 103 104 if (host_ == nullptr) { 105 host_ = new (std::nothrow) BundleMgrHostImpl(); 106 if (!host_) { 107 APP_LOGE("create host instance fail"); 108 return false; 109 } 110 } 111 112 APP_LOGI("init begin"); 113 114 if (!runner_) { 115 runner_ = EventRunner::Create(Constants::BMS_SERVICE_NAME); 116 if (!runner_) { 117 APP_LOGE("create runner fail"); 118 return false; 119 } 120 } 121 APP_LOGD("create runner success"); 122 123 if (!handler_) { 124 handler_ = std::make_shared<BMSEventHandler>(runner_); 125 if (!handler_) { 126 APP_LOGE("create bms event handler fail"); 127 return false; 128 } 129 } 130 APP_LOGD("create handler success"); 131 132 if (!installer_) { 133 installer_ = new (std::nothrow) BundleInstallerHost(); 134 if (!installer_ || !installer_->Init()) { 135 APP_LOGE("init installer fail"); 136 return false; 137 } 138 } 139 APP_LOGD("create installer host success"); 140 141 if (!dataMgr_) { 142 APP_LOGI("Create BundledataMgr"); 143 dataMgr_ = std::make_shared<BundleDataMgr>(); 144 } 145 APP_LOGD("create dataManager success"); 146 147 if (!(dataMgr_->LoadDataFromPersistentStorage())) { 148 APP_LOGW("load data from persistent storage fail"); 149 handler_->SendEvent(BMSEventHandler::BUNDLE_SCAN_START); 150 needToScan_ = true; 151 } 152 if (!perChangeSub_) { 153 EventFwk::MatchingSkills matchingSkills; 154 matchingSkills.AddEvent("PERMISSIONS_CHANGED_EVENT"); 155 EventFwk::CommonEventSubscribeInfo subscribeInfo(matchingSkills); 156 perChangeSub_ = std::make_shared<BundlePermissionsChangedMonitor>(dataMgr_, subscribeInfo); 157 EventFwk::CommonEventManager::SubscribeCommonEvent(perChangeSub_); 158 } 159 ready_ = true; 160 APP_LOGI("init end success"); 161 return true; 162 } 163 GetBundleInstaller() const164sptr<IBundleInstaller> BundleMgrService::GetBundleInstaller() const 165 { 166 return installer_; 167 } 168 GetDataMgr() const169const std::shared_ptr<BundleDataMgr> BundleMgrService::GetDataMgr() const 170 { 171 return dataMgr_; 172 } 173 SelfClean()174void BundleMgrService::SelfClean() 175 { 176 if (ready_) { 177 ready_ = false; 178 if (registerToService_) { 179 registerToService_ = false; 180 } 181 if (needToScan_) { 182 needToScan_ = false; 183 } 184 } 185 } 186 187 } // namespace AppExecFwk 188 } // namespace OHOS 189