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 "system_ability.h"
17
18 #include <cinttypes>
19
20 #include "datetime_ex.h"
21 #include "errors.h"
22 #include "hitrace_meter.h"
23 #include "if_system_ability_manager.h"
24 #include "iservice_registry.h"
25 #include "local_ability_manager.h"
26 #include "nlohmann/json.hpp"
27 #include "safwk_log.h"
28 #include "string_ex.h"
29
30 namespace OHOS {
31 namespace {
32 const std::string TAG = "SystemAbility";
33 }
34
SystemAbility(bool runOnCreate)35 SystemAbility::SystemAbility(bool runOnCreate)
36 {
37 isRunning_ = false;
38 abilityState_ = SystemAbilityState::NOT_LOADED;
39 isRunOnCreate_ = runOnCreate;
40 isDistributed_ = false;
41 dumpLevel_ = 0;
42 // timeout for waiting dependency ready, which configed in json, with DEFAULT_DEPENDENCY_TIMEOUT(6s) by default
43 dependTimeout_ = DEFAULT_DEPENDENCY_TIMEOUT;
44 capability_ = u"";
45 }
46
SystemAbility(int32_t systemAbilityId,bool runOnCreate)47 SystemAbility::SystemAbility(int32_t systemAbilityId, bool runOnCreate) : SystemAbility(runOnCreate)
48 {
49 saId_ = systemAbilityId;
50 }
51
~SystemAbility()52 SystemAbility::~SystemAbility()
53 {
54 HILOGI(TAG, "SA:%{public}d destroyed", saId_);
55 }
56
MakeAndRegisterAbility(SystemAbility * systemAbility)57 bool SystemAbility::MakeAndRegisterAbility(SystemAbility* systemAbility)
58 {
59 HILOGD(TAG, "registering system ability...");
60 return LocalAbilityManager::GetInstance().AddAbility(systemAbility);
61 }
62
AddSystemAbilityListener(int32_t systemAbilityId)63 bool SystemAbility::AddSystemAbilityListener(int32_t systemAbilityId)
64 {
65 HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
66 return LocalAbilityManager::GetInstance().AddSystemAbilityListener(systemAbilityId, saId_);
67 }
68
RemoveSystemAbilityListener(int32_t systemAbilityId)69 bool SystemAbility::RemoveSystemAbilityListener(int32_t systemAbilityId)
70 {
71 HILOGD(TAG, "SA:%{public}d, listenerSA:%{public}d", systemAbilityId, saId_);
72 return LocalAbilityManager::GetInstance().RemoveSystemAbilityListener(systemAbilityId, saId_);
73 }
74
Publish(sptr<IRemoteObject> systemAbility)75 bool SystemAbility::Publish(sptr<IRemoteObject> systemAbility)
76 {
77 if (systemAbility == nullptr) {
78 HILOGE(TAG, "systemAbility is nullptr");
79 return false;
80 }
81 HILOGD(TAG, "[PerformanceTest] SAFWK Publish systemAbilityId:%{public}d", saId_);
82 // Avoid automatic destruction of system ability caused by failure of publishing ability
83 publishObj_ = systemAbility;
84 int64_t begin = GetTickCount();
85 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
86 if (samgrProxy == nullptr) {
87 HILOGE(TAG, "failed to get samgrProxy");
88 return false;
89 }
90
91 ISystemAbilityManager::SAExtraProp saExtra(GetDistributed(), GetDumpLevel(), capability_, permission_);
92 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
93 int32_t result = samgrProxy->AddSystemAbility(saId_, publishObj_, saExtra);
94 HILOGI(TAG, "[PerformanceTest] SAFWK Publish SA:%{public}d result : %{public}d, spend:%{public}" PRId64 " ms",
95 saId_, result, (GetTickCount() - begin));
96 if (result == ERR_OK) {
97 abilityState_ = SystemAbilityState::ACTIVE;
98 return true;
99 }
100 return false;
101 }
102
CancelIdle()103 bool SystemAbility::CancelIdle()
104 {
105 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
106 if (abilityState_ != SystemAbilityState::IDLE) {
107 return true;
108 }
109 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
110 if (samgrProxy == nullptr) {
111 HILOGE(TAG, "failed to get samgrProxy");
112 return false;
113 }
114 int32_t result = samgrProxy->CancelUnloadSystemAbility(saId_);
115 return result == ERR_OK;
116 }
117
StopAbility(int32_t systemAbilityId)118 void SystemAbility::StopAbility(int32_t systemAbilityId)
119 {
120 HILOGD(TAG, "SA:%{public}d", systemAbilityId);
121
122 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
123 if (samgrProxy == nullptr) {
124 HILOGE(TAG, "failed to get samgrProxy");
125 return;
126 }
127
128 int32_t ret = samgrProxy->RemoveSystemAbility(systemAbilityId);
129 HILOGI(TAG, "%{public}s to remove ability", (ret == ERR_OK) ? "success" : "failed");
130 }
131
GetOnDemandReasonExtraData(SystemAbilityOnDemandReason & onDemandStartReason)132 void SystemAbility::GetOnDemandReasonExtraData(SystemAbilityOnDemandReason& onDemandStartReason)
133 {
134 if (!onDemandStartReason.HasExtraData()) {
135 return;
136 }
137 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
138 if (samgrProxy == nullptr) {
139 HILOGE(TAG, "failed to get samgrProxy");
140 return;
141 }
142 HILOGI(TAG, "get extra data id: %{public}d", static_cast<int32_t>(onDemandStartReason.GetExtraDataId()));
143 MessageParcel extraDataParcel;
144 int32_t ret = samgrProxy->GetOnDemandReasonExtraData(onDemandStartReason.GetExtraDataId(), extraDataParcel);
145 if (ret != ERR_OK) {
146 HILOGE(TAG, "get extra data failed");
147 return;
148 }
149 auto extraData = extraDataParcel.ReadParcelable<OnDemandReasonExtraData>();
150 if (extraData == nullptr) {
151 HILOGE(TAG, "read extra data failed");
152 return;
153 }
154 onDemandStartReason.SetExtraData(*extraData);
155 HILOGD(TAG, "get extra data: %{public}d, %{public}s", onDemandStartReason.GetExtraData().GetCode(),
156 onDemandStartReason.GetExtraData().GetData().c_str());
157 delete extraData;
158 }
159
Start()160 void SystemAbility::Start()
161 {
162 // Ensure that the lifecycle is sequentially called by SAMGR
163 HILOGD(TAG, "starting system ability...");
164 {
165 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
166 if (abilityState_ != SystemAbilityState::NOT_LOADED) {
167 return;
168 }
169 }
170 HILOGD(TAG, "[PerformanceTest] SAFWK OnStart systemAbilityId:%{public}d", saId_);
171 int64_t begin = GetTickCount();
172 HITRACE_METER_NAME(HITRACE_TAG_SAMGR, ToString(saId_) + "_OnStart");
173 nlohmann::json startReason = LocalAbilityManager::GetInstance().GetStartReason(saId_);
174 SystemAbilityOnDemandReason onDemandStartReason =
175 LocalAbilityManager::GetInstance().JsonToOnDemandReason(startReason);
176 GetOnDemandReasonExtraData(onDemandStartReason);
177 OnStart(onDemandStartReason);
178 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
179 isRunning_ = true;
180 HILOGI(TAG, "[PerformanceTest] SAFWK OnStart systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
181 saId_, (GetTickCount() - begin));
182 }
183
Idle(const SystemAbilityOnDemandReason & idleReason,int32_t & delayTime)184 void SystemAbility::Idle(const SystemAbilityOnDemandReason& idleReason,
185 int32_t& delayTime)
186 {
187 {
188 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
189 if (abilityState_ != SystemAbilityState::ACTIVE) {
190 HILOGI(TAG, "cannot idle systemAbilityId:%{public}d", saId_);
191 return;
192 }
193 }
194 HILOGD(TAG, "[PerformanceTest] SAFWK Idle systemAbilityId:%{public}d", saId_);
195 int64_t begin = GetTickCount();
196 delayTime = OnIdle(idleReason);
197 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
198 if (delayTime == 0) {
199 abilityState_ = SystemAbilityState::IDLE;
200 }
201 HILOGI(TAG, "[PerformanceTest] SAFWK Idle systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
202 saId_, (GetTickCount() - begin));
203 }
204
Active(const SystemAbilityOnDemandReason & activeReason)205 void SystemAbility::Active(const SystemAbilityOnDemandReason& activeReason)
206 {
207 {
208 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
209 if (abilityState_ != SystemAbilityState::IDLE) {
210 HILOGI(TAG, "cannot active systemAbilityId:%{public}d", saId_);
211 return;
212 }
213 }
214 HILOGD(TAG, "[PerformanceTest] SAFWK Active systemAbilityId:%{public}d", saId_);
215 int64_t begin = GetTickCount();
216 OnActive(activeReason);
217 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
218 abilityState_ = SystemAbilityState::ACTIVE;
219 HILOGI(TAG, "[PerformanceTest] SAFWK Active systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
220 saId_, (GetTickCount() - begin));
221 }
222
Stop()223 void SystemAbility::Stop()
224 {
225 HILOGD(TAG, "stopping system ability...");
226 {
227 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
228 if (abilityState_ == SystemAbilityState::NOT_LOADED) {
229 return;
230 }
231 }
232 HILOGD(TAG, "[PerformanceTest] SAFWK OnStop systemAbilityId:%{public}d", saId_);
233 int64_t begin = GetTickCount();
234 nlohmann::json stopReason = LocalAbilityManager::GetInstance().GetStopReason(saId_);
235 SystemAbilityOnDemandReason onDemandStopReason =
236 LocalAbilityManager::GetInstance().JsonToOnDemandReason(stopReason);
237 OnStop(onDemandStopReason);
238 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
239 abilityState_ = SystemAbilityState::NOT_LOADED;
240 isRunning_ = false;
241 HILOGI(TAG, "[PerformanceTest] SAFWK OnStop systemAbilityId:%{public}d finished, spend:%{public}" PRId64 " ms",
242 saId_, (GetTickCount() - begin));
243 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
244 if (samgrProxy == nullptr) {
245 HILOGE(TAG, "failed to get samgrProxy");
246 return;
247 }
248
249 int32_t ret = samgrProxy->RemoveSystemAbility(saId_);
250 HILOGI(TAG, "%{public}s to remove ability", (ret == ERR_OK) ? "success" : "failed");
251 }
252
SADump()253 void SystemAbility::SADump()
254 {
255 OnDump();
256 }
257
GetSystemAbilitId() const258 int32_t SystemAbility::GetSystemAbilitId() const
259 {
260 return saId_;
261 }
262
SetLibPath(const std::string & libPath)263 void SystemAbility::SetLibPath(const std::string& libPath)
264 {
265 libPath_ = libPath;
266 }
267
GetLibPath() const268 const std::string& SystemAbility::GetLibPath() const
269 {
270 return libPath_;
271 }
272
SetDependSa(const std::vector<std::int32_t> & dependSa)273 void SystemAbility::SetDependSa(const std::vector<std::int32_t>& dependSa)
274 {
275 dependSa_ = dependSa;
276 }
277
GetDependSa() const278 const std::vector<std::int32_t>& SystemAbility::GetDependSa() const
279 {
280 return dependSa_;
281 }
282
SetRunOnCreate(bool isRunOnCreate)283 void SystemAbility::SetRunOnCreate(bool isRunOnCreate)
284 {
285 isRunOnCreate_ = isRunOnCreate;
286 }
287
IsRunOnCreate() const288 bool SystemAbility::IsRunOnCreate() const
289 {
290 return isRunOnCreate_;
291 }
292
SetDistributed(bool isDistributed)293 void SystemAbility::SetDistributed(bool isDistributed)
294 {
295 isDistributed_ = isDistributed;
296 }
297
GetDistributed() const298 bool SystemAbility::GetDistributed() const
299 {
300 return isDistributed_;
301 }
302
GetRunningStatus() const303 bool SystemAbility::GetRunningStatus() const
304 {
305 return isRunning_;
306 }
307
GetAbilityState()308 SystemAbilityState SystemAbility::GetAbilityState()
309 {
310 std::lock_guard<std::recursive_mutex> autoLock(abilityLock);
311 return abilityState_;
312 }
313
SetDumpLevel(uint32_t dumpLevel)314 void SystemAbility::SetDumpLevel(uint32_t dumpLevel)
315 {
316 dumpLevel_ = dumpLevel;
317 }
318
GetDumpLevel() const319 uint32_t SystemAbility::GetDumpLevel() const
320 {
321 return dumpLevel_;
322 }
323
SetDependTimeout(int32_t dependTimeout)324 void SystemAbility::SetDependTimeout(int32_t dependTimeout)
325 {
326 if (dependTimeout >= MIN_DEPENDENCY_TIMEOUT && dependTimeout <= MAX_DEPENDENCY_TIMEOUT) {
327 dependTimeout_ = dependTimeout;
328 }
329 HILOGD(TAG, "new dependTimeout:%{public}d, original dependTimeout:%{public}d", dependTimeout, dependTimeout_);
330 }
331
GetDependTimeout() const332 int32_t SystemAbility::GetDependTimeout() const
333 {
334 return dependTimeout_;
335 }
336
337 // The details should be implemented by subclass
OnDump()338 void SystemAbility::OnDump()
339 {
340 }
341
342 // The details should be implemented by subclass
OnStart()343 void SystemAbility::OnStart()
344 {
345 }
346
347 // The details should be implemented by subclass
OnStart(const SystemAbilityOnDemandReason & startReason)348 void SystemAbility::OnStart(const SystemAbilityOnDemandReason& startReason)
349 {
350 OnStart();
351 }
352
OnIdle(const SystemAbilityOnDemandReason & idleReason)353 int32_t SystemAbility::OnIdle(const SystemAbilityOnDemandReason& idleReason)
354 {
355 HILOGD(TAG, "OnIdle system ability, idle reason %{public}d, %{public}s, %{public}s",
356 idleReason.GetId(), idleReason.GetName().c_str(), idleReason.GetValue().c_str());
357 return 0;
358 }
359
OnActive(const SystemAbilityOnDemandReason & activeReason)360 void SystemAbility::OnActive(const SystemAbilityOnDemandReason& activeReason)
361 {
362 HILOGD(TAG, "OnActive system ability, active reason %{public}d, %{public}s, %{public}s",
363 activeReason.GetId(), activeReason.GetName().c_str(), activeReason.GetValue().c_str());
364 }
365
366 // The details should be implemented by subclass
OnStop()367 void SystemAbility::OnStop()
368 {
369 }
370
371 // The details should be implemented by subclass
OnStop(const SystemAbilityOnDemandReason & stopReason)372 void SystemAbility::OnStop(const SystemAbilityOnDemandReason& stopReason)
373 {
374 OnStop();
375 }
376
377 // The details should be implemented by subclass
OnAddSystemAbility(int32_t systemAbilityId,const std::string & deviceId)378 void SystemAbility::OnAddSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
379 {
380 }
381
382 // The details should be implemented by subclass
OnRemoveSystemAbility(int32_t systemAbilityId,const std::string & deviceId)383 void SystemAbility::OnRemoveSystemAbility(int32_t systemAbilityId, const std::string& deviceId)
384 {
385 }
386
GetSystemAbility(int32_t systemAbilityId)387 sptr<IRemoteObject> SystemAbility::GetSystemAbility(int32_t systemAbilityId)
388 {
389 sptr<ISystemAbilityManager> samgrProxy = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
390 if (samgrProxy == nullptr) {
391 HILOGE(TAG, "failed to get samgrProxy");
392 return nullptr;
393 }
394
395 return samgrProxy->GetSystemAbility(systemAbilityId);
396 }
397
SetCapability(const std::u16string & capability)398 void SystemAbility::SetCapability(const std::u16string& capability)
399 {
400 capability_ = capability;
401 }
402
GetCapability() const403 const std::u16string& SystemAbility::GetCapability() const
404 {
405 return capability_;
406 }
407
SetPermission(const std::u16string & permission)408 void SystemAbility::SetPermission(const std::u16string& permission)
409 {
410 permission_ = permission;
411 }
412 }
413