1 /*
2 * Copyright (c) 2024 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 "font_service_load_manager.h"
17
18 #include <thread>
19 #include "font_define.h"
20 #include "font_hilog.h"
21 #include "font_sa_load_callback.h"
22
23 namespace OHOS {
24 namespace Global {
25 namespace FontManager {
26 FontServiceLoadManager::FontServiceLoadManager() = default;
27
28 FontServiceLoadManager::~FontServiceLoadManager() = default;
29
GetFontServiceAbility(int32_t systemAbilityId)30 sptr<IFontService> FontServiceLoadManager::GetFontServiceAbility(int32_t systemAbilityId)
31 {
32 sptr<ISystemAbilityManager> manager = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
33 if (manager == nullptr) {
34 FONT_LOGE("manager is nullptr");
35 return nullptr;
36 }
37 {
38 std::lock_guard<std::mutex> lock(serviceLock_);
39 sptr<IRemoteObject> object = manager->CheckSystemAbility(systemAbilityId);
40 if (object != nullptr) {
41 return iface_cast<IFontService>(object);
42 }
43 }
44
45 if (!LoadSa(systemAbilityId)) {
46 FONT_LOGE("loadSA failed");
47 return nullptr;
48 }
49 sptr<IRemoteObject> object = manager->GetSystemAbility(systemAbilityId);
50 if (object == nullptr) {
51 FONT_LOGE("Get remote object from samgr failed");
52 return nullptr;
53 }
54 return iface_cast<IFontService>(object);
55 }
56
OnLoadSystemAbilitySuccess()57 void FontServiceLoadManager::OnLoadSystemAbilitySuccess()
58 {
59 loadSaStatus_ = LoadSaStatus::SUCCESS;
60 proxyConVar_.notify_one();
61 }
62
OnLoadSystemAbilityFail()63 void FontServiceLoadManager::OnLoadSystemAbilityFail()
64 {
65 FONT_LOGE("SystemAbility Load fail");
66 loadSaStatus_ = LoadSaStatus::FAIL;
67 proxyConVar_.notify_one();
68 }
69
InitStatus()70 void FontServiceLoadManager::InitStatus()
71 {
72 loadSaStatus_ = LoadSaStatus::WAIT_RESULT;
73 }
74
LoadSa(int systemAbilityId)75 bool FontServiceLoadManager::LoadSa(int systemAbilityId)
76 {
77 InitStatus();
78 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
79 if (samgr == nullptr) {
80 FONT_LOGE("samgr object null!");
81 return false;
82 }
83 sptr<FontSALoadCallback> fontSaLoadCallback = new FontSALoadCallback();
84 if (fontSaLoadCallback == nullptr) {
85 FONT_LOGE("systemAbilityId: %{public}d, create load callback failed", systemAbilityId);
86 return false;
87 }
88 int32_t result = samgr->LoadSystemAbility(systemAbilityId, fontSaLoadCallback);
89 if (result != ERR_OK) {
90 FONT_LOGE("systemAbilityId: %{public}d, load failed, result code: %{public}d", systemAbilityId, result);
91 return false;
92 }
93
94 {
95 std::unique_lock<std::mutex> lock(serviceLock_);
96 constexpr int64_t sleepTime = 5000;
97 auto waitStatus = proxyConVar_.wait_for(lock, std::chrono::milliseconds(sleepTime),
98 [this]() { return loadSaStatus_ == LoadSaStatus::SUCCESS; });
99 if (!waitStatus) {
100 FONT_LOGE("systemAbilityId: %{public}d, CheckSaLoaded failed", systemAbilityId);
101 return false;
102 }
103 }
104 FONT_LOGI("systemAbilityId: %{public}d, load succeed", systemAbilityId);
105 return true;
106 }
107
UnloadFontService(int32_t systemAbilityId)108 bool FontServiceLoadManager::UnloadFontService(int32_t systemAbilityId)
109 {
110 std::lock_guard<std::mutex> lock(serviceLock_);
111 sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager();
112 if (samgr == nullptr) {
113 FONT_LOGE("FontServiceLoadManager::UnloadFontService can't get samgr.");
114 return false;
115 }
116 int32_t ret = samgr->UnloadSystemAbility(systemAbilityId);
117 if (ret != ERR_OK) {
118 FONT_LOGE("FontServiceLoadManager::UnloadFontService sa unload failed.");
119 return false;
120 }
121 return true;
122 }
123 } // namespace FontManager
124 } // namespace Global
125 } // namespace OHOS