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 "load_sa_service.h"
17
18 #include <thread>
19
20 #include "update_log.h"
21
22 namespace OHOS::UpdateEngine {
23 LoadSaService::LoadSaService() = default;
24
25 LoadSaService::~LoadSaService() = default;
26
27 sptr<LoadSaService> LoadSaService::instance_ = nullptr;
28 std::mutex LoadSaService::instanceLock_;
29
GetInstance()30 sptr<LoadSaService> LoadSaService::GetInstance()
31 {
32 if (instance_ == nullptr) {
33 std::lock_guard<std::mutex> lock(instanceLock_);
34 if (instance_ == nullptr) {
35 instance_ = new LoadSaService();
36 }
37 }
38 return instance_;
39 }
40
TryLoadSa(int systemAbilityId)41 bool LoadSaService::TryLoadSa(int systemAbilityId)
42 {
43 InitStatus();
44 return LoadSa(systemAbilityId);
45 }
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)46 void LoadSaService::OnLoadSystemAbilitySuccess(int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
47 {
48 ENGINE_LOGI("SystemAbility load Success systemAbilityId: %{public}d, IRemoteObject result: %{public}s",
49 systemAbilityId, (remoteObject != nullptr) ? "succeed" : "failed");
50 loadSaStatus_ = (remoteObject != nullptr) ? LoadSaStatus::SUCCESS : LoadSaStatus::FAIL;
51 }
52
OnLoadSystemAbilityFail(int32_t systemAbilityId)53 void LoadSaService::OnLoadSystemAbilityFail(int32_t systemAbilityId)
54 {
55 ENGINE_LOGE("SystemAbility Load fail the systemAbilityId is: %{public}d", systemAbilityId);
56 loadSaStatus_ = LoadSaStatus::FAIL;
57 }
58
InitStatus()59 void LoadSaService::InitStatus()
60 {
61 loadSaStatus_ = LoadSaStatus::WAIT_RESULT;
62 }
63
CheckSaLoaded()64 bool LoadSaService::CheckSaLoaded()
65 {
66 constexpr int64_t sleepTime = 50; // 睡眠时间 单位:毫秒
67 constexpr int32_t retryTimes = 100; // 重试次数
68 int32_t retry = retryTimes;
69 ENGINE_LOGI("Waiting for CheckSaLoaded");
70 do {
71 std::this_thread::sleep_for(std::chrono::milliseconds(sleepTime));
72 LoadSaStatus loadSaStatus = loadSaStatus_;
73 if (loadSaStatus != LoadSaStatus::WAIT_RESULT) {
74 bool isSaLoaded = loadSaStatus == LoadSaStatus::SUCCESS;
75 ENGINE_LOGI("found OnLoad result: %{public}s", isSaLoaded ? "succeed" : "failed");
76 return isSaLoaded;
77 }
78 } while (--retry >= 0);
79 ENGINE_LOGE("CheckSaLoaded didn't get OnLoad result");
80 return false;
81 }
82
LoadSa(int systemAbilityId)83 bool LoadSaService::LoadSa(int systemAbilityId)
84 {
85 sptr<ISystemAbilityManager> sm = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86 if (sm == nullptr) {
87 ENGINE_LOGE("samgr object null!");
88 return false;
89 }
90 int32_t result = sm->LoadSystemAbility(systemAbilityId, this);
91 if (result != ERR_OK) {
92 ENGINE_LOGE("systemAbilityId: %{public}d, load failed, result code: %{public}d", systemAbilityId, result);
93 return false;
94 }
95 if (!CheckSaLoaded()) {
96 ENGINE_LOGE("systemAbilityId: %{public}d, CheckSaLoaded failed", systemAbilityId);
97 return false;
98 }
99 ENGINE_LOGI("systemAbilityId: %{public}d, load succeed", systemAbilityId);
100 return true;
101 }
102 } // namespace OHOS::UpdateEngine