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 "location_sa_load_manager.h"
17
18 #include "if_system_ability_manager.h"
19 #include "iservice_registry.h"
20 #include "system_ability_definition.h"
21
22 #include "common_utils.h"
23 #include "location_log.h"
24 #include "common_hisysevent.h"
25 #include "location_log_event_ids.h"
26 #ifdef INIT_SUPPORT
27 #include "parameters.h"
28 #endif
29
30 namespace OHOS {
31 namespace Location {
GetInstance()32 LocationSaLoadManager* LocationSaLoadManager::GetInstance()
33 {
34 static LocationSaLoadManager data;
35 return &data;
36 }
37
LocationSaLoadManager()38 LocationSaLoadManager::LocationSaLoadManager()
39 {
40 }
41
~LocationSaLoadManager()42 LocationSaLoadManager::~LocationSaLoadManager()
43 {
44 }
45
LoadLocationSa(int32_t systemAbilityId)46 LocationErrCode LocationSaLoadManager::LoadLocationSa(int32_t systemAbilityId)
47 {
48 LBSLOGD(LOCATOR, "%{public}s enter, systemAbilityId = [%{public}d] loading", __func__, systemAbilityId);
49 InitLoadState();
50 sptr<ISystemAbilityManager> samgr =
51 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
52 if (samgr == nullptr) {
53 LBSLOGE(LOCATOR, "%{public}s: get system ability manager failed!", __func__);
54 return ERRCODE_SERVICE_UNAVAILABLE;
55 }
56
57 auto locationSaLoadCallback = sptr<LocationSaLoadCallback>(new LocationSaLoadCallback());
58 int32_t ret = samgr->LoadSystemAbility(systemAbilityId, locationSaLoadCallback);
59 if (ret != ERR_OK) {
60 LBSLOGE(LOCATOR, "%{public}s: Failed to load system ability, SA Id = [%{public}d], ret = [%{public}d].",
61 __func__, systemAbilityId, ret);
62 return ERRCODE_SERVICE_UNAVAILABLE;
63 }
64 return WaitLoadStateChange(systemAbilityId);
65 }
66
InitLoadState()67 void LocationSaLoadManager::InitLoadState()
68 {
69 std::unique_lock<std::mutex> lock(locatorMutex_);
70 state_ = false;
71 }
72
WaitLoadStateChange(int32_t systemAbilityId)73 LocationErrCode LocationSaLoadManager::WaitLoadStateChange(int32_t systemAbilityId)
74 {
75 std::unique_lock<std::mutex> lock(locatorMutex_);
76 auto wait = locatorCon_.wait_for(lock, std::chrono::milliseconds(LOCATION_LOADSA_TIMEOUT_MS), [this] {
77 return state_ == true;
78 });
79 if (!wait) {
80 LBSLOGE(LOCATOR_STANDARD, "locator sa [%{public}d] start time out.", systemAbilityId);
81 return ERRCODE_SERVICE_UNAVAILABLE;
82 }
83 return ERRCODE_SUCCESS;
84 }
85
UnloadLocationSa(int32_t systemAbilityId)86 LocationErrCode LocationSaLoadManager::UnloadLocationSa(int32_t systemAbilityId)
87 {
88 LBSLOGD(LOCATOR, "%{public}s enter, systemAbilityId = [%{public}d] unloading", __func__, systemAbilityId);
89 sptr<ISystemAbilityManager> samgr =
90 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
91 if (samgr == nullptr) {
92 LBSLOGE(LOCATOR, "%{public}s: get system ability manager failed!", __func__);
93 return ERRCODE_SERVICE_UNAVAILABLE;
94 }
95 #ifdef INIT_SUPPORT
96 if (system::GetBoolParameter("persist.location.process_resident", false)) {
97 LBSLOGE(LOCATOR, "sa does not need to be unloaded");
98 return ERRCODE_SERVICE_UNAVAILABLE;
99 }
100 #endif
101 int32_t ret = samgr->UnloadSystemAbility(systemAbilityId);
102 if (ret != ERR_OK) {
103 LBSLOGE(LOCATOR, "%{public}s: Failed to unload system ability, SA Id = [%{public}d], ret = [%{public}d].",
104 __func__, systemAbilityId, ret);
105 return ERRCODE_SERVICE_UNAVAILABLE;
106 }
107 return ERRCODE_SUCCESS;
108 }
109
LoadSystemAbilitySuccess()110 void LocationSaLoadManager::LoadSystemAbilitySuccess()
111 {
112 std::unique_lock<std::mutex> lock(locatorMutex_);
113 state_ = true;
114 locatorCon_.notify_one();
115 }
116
LoadSystemAbilityFail()117 void LocationSaLoadManager::LoadSystemAbilityFail()
118 {
119 std::unique_lock<std::mutex> lock(locatorMutex_);
120 state_ = false;
121 locatorCon_.notify_one();
122 }
123
124
CheckIfSystemAbilityAvailable(int32_t systemAbilityId)125 bool LocationSaLoadManager::CheckIfSystemAbilityAvailable(int32_t systemAbilityId)
126 {
127 sptr<ISystemAbilityManager> samgr =
128 SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
129 if (samgr == nullptr) {
130 LBSLOGE(COMMON_UTILS, "%{public}s: get system ability manager failed!", __func__);
131 return false;
132 }
133 return (samgr->CheckSystemAbility(systemAbilityId) != nullptr);
134 }
135
InitLocationSa(int32_t systemAbilityId)136 bool LocationSaLoadManager::InitLocationSa(int32_t systemAbilityId)
137 {
138 if (LocationSaLoadManager::CheckIfSystemAbilityAvailable(systemAbilityId)) {
139 LBSLOGD(COMMON_UTILS, "sa has been loaded");
140 return true;
141 }
142 auto instance = LocationSaLoadManager::GetInstance();
143 if (instance == nullptr || instance->LoadLocationSa(systemAbilityId) != ERRCODE_SUCCESS) {
144 LBSLOGE(LOCATOR, "sa load failed.");
145 return false;
146 }
147 return true;
148 }
149
UnInitLocationSa(int32_t systemAbilityId)150 bool LocationSaLoadManager::UnInitLocationSa(int32_t systemAbilityId)
151 {
152 if (!LocationSaLoadManager::CheckIfSystemAbilityAvailable(systemAbilityId)) {
153 LBSLOGD(LOCATOR, "sa has been unloaded");
154 return true;
155 }
156 auto instance = LocationSaLoadManager::GetInstance();
157 if (instance == nullptr || instance->UnloadLocationSa(systemAbilityId) != ERRCODE_SUCCESS) {
158 LBSLOGE(LOCATOR, "sa unload failed.");
159 return false;
160 }
161 return true;
162 }
163
OnLoadSystemAbilitySuccess(int32_t systemAbilityId,const sptr<IRemoteObject> & remoteObject)164 void LocationSaLoadCallback::OnLoadSystemAbilitySuccess(
165 int32_t systemAbilityId, const sptr<IRemoteObject> &remoteObject)
166 {
167 LBSLOGD(LOCATOR, "LocationSaLoadManager Load SA success, systemAbilityId = [%{public}d]", systemAbilityId);
168 auto instance = LocationSaLoadManager::GetInstance();
169 if (instance == nullptr) {
170 LBSLOGE(LOCATOR, "LocationSaLoadManager GetInstance return null");
171 return;
172 }
173 instance->LoadSystemAbilitySuccess();
174 }
175
OnLoadSystemAbilityFail(int32_t systemAbilityId)176 void LocationSaLoadCallback::OnLoadSystemAbilityFail(int32_t systemAbilityId)
177 {
178 LBSLOGD(LOCATOR, "LocationSaLoadManager Load SA failed, systemAbilityId = [%{public}d]", systemAbilityId);
179 auto instance = LocationSaLoadManager::GetInstance();
180 if (instance == nullptr) {
181 LBSLOGE(LOCATOR, "LocationSaLoadManager GetInstance return null");
182 return;
183 }
184 instance->LoadSystemAbilityFail();
185 }
186 }; // namespace Location
187 }; // namespace OHOS
188