19596a2c1Sopenharmony_ci/* 29596a2c1Sopenharmony_ci * Copyright (c) 2023 Huawei Device Co., Ltd. 39596a2c1Sopenharmony_ci * Licensed under the Apache License, Version 2.0 (the "License"); 49596a2c1Sopenharmony_ci * you may not use this file except in compliance with the License. 59596a2c1Sopenharmony_ci * You may obtain a copy of the License at 69596a2c1Sopenharmony_ci * 79596a2c1Sopenharmony_ci * http://www.apache.org/licenses/LICENSE-2.0 89596a2c1Sopenharmony_ci * 99596a2c1Sopenharmony_ci * Unless required by applicable law or agreed to in writing, software 109596a2c1Sopenharmony_ci * distributed under the License is distributed on an "AS IS" BASIS, 119596a2c1Sopenharmony_ci * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 129596a2c1Sopenharmony_ci * See the License for the specific language governing permissions and 139596a2c1Sopenharmony_ci * limitations under the License. 149596a2c1Sopenharmony_ci */ 159596a2c1Sopenharmony_ci 169596a2c1Sopenharmony_ci#include "i18n_hilog.h" 179596a2c1Sopenharmony_ci#include "i18n_service_ability_load_callback.h" 189596a2c1Sopenharmony_ci#include "i18n_service_ability_load_manager.h" 199596a2c1Sopenharmony_ci 209596a2c1Sopenharmony_cinamespace OHOS { 219596a2c1Sopenharmony_cinamespace Global { 229596a2c1Sopenharmony_cinamespace I18n { 239596a2c1Sopenharmony_cistatic constexpr int32_t I18N_LOAD_SA_TIMEOUT_MS = 1000; 249596a2c1Sopenharmony_ci 259596a2c1Sopenharmony_ciI18nServiceAbilityLoadManager::I18nServiceAbilityLoadManager() 269596a2c1Sopenharmony_ci{ 279596a2c1Sopenharmony_ci} 289596a2c1Sopenharmony_ci 299596a2c1Sopenharmony_ciI18nServiceAbilityLoadManager::~I18nServiceAbilityLoadManager() 309596a2c1Sopenharmony_ci{ 319596a2c1Sopenharmony_ci} 329596a2c1Sopenharmony_ci 339596a2c1Sopenharmony_cisptr<IRemoteObject> I18nServiceAbilityLoadManager::GetI18nServiceAbility(int32_t systemAbilityId) 349596a2c1Sopenharmony_ci{ 359596a2c1Sopenharmony_ci sptr<ISystemAbilityManager> samgr = LoadI18nServiceAbility(systemAbilityId); 369596a2c1Sopenharmony_ci if (samgr == nullptr) { 379596a2c1Sopenharmony_ci // try again 389596a2c1Sopenharmony_ci samgr = LoadI18nServiceAbility(systemAbilityId); 399596a2c1Sopenharmony_ci } 409596a2c1Sopenharmony_ci if (samgr == nullptr) { 419596a2c1Sopenharmony_ci return nullptr; 429596a2c1Sopenharmony_ci } 439596a2c1Sopenharmony_ci return samgr->GetSystemAbility(systemAbilityId, ""); 449596a2c1Sopenharmony_ci} 459596a2c1Sopenharmony_ci 469596a2c1Sopenharmony_cisptr<ISystemAbilityManager> I18nServiceAbilityLoadManager::LoadI18nServiceAbility(int32_t systemAbilityId) 479596a2c1Sopenharmony_ci{ 489596a2c1Sopenharmony_ci InitLoadState(); 499596a2c1Sopenharmony_ci sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 509596a2c1Sopenharmony_ci if (samgr == nullptr) { 519596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility can't get samgr"); 529596a2c1Sopenharmony_ci return nullptr; 539596a2c1Sopenharmony_ci } 549596a2c1Sopenharmony_ci sptr<I18nServiceAbilityLoadCallback> i18nSaLoadCallback = new I18nServiceAbilityLoadCallback(); 559596a2c1Sopenharmony_ci int32_t ret = samgr->LoadSystemAbility(systemAbilityId, i18nSaLoadCallback); 569596a2c1Sopenharmony_ci if (ret != ERR_OK) { 579596a2c1Sopenharmony_ci HILOG_ERROR_I18N( 589596a2c1Sopenharmony_ci "I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility LoadSystemAbility failed."); 599596a2c1Sopenharmony_ci return nullptr; 609596a2c1Sopenharmony_ci } 619596a2c1Sopenharmony_ci bool status = WaitLoadStateChange(systemAbilityId); 629596a2c1Sopenharmony_ci if (!status) { 639596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::LoadAndGetI18nServiceAbility wait overtime."); 649596a2c1Sopenharmony_ci return nullptr; 659596a2c1Sopenharmony_ci } 669596a2c1Sopenharmony_ci return samgr; 679596a2c1Sopenharmony_ci} 689596a2c1Sopenharmony_ci 699596a2c1Sopenharmony_civoid I18nServiceAbilityLoadManager::InitLoadState() 709596a2c1Sopenharmony_ci{ 719596a2c1Sopenharmony_ci std::unique_lock<std::mutex> lock(loadStateMutex); 729596a2c1Sopenharmony_ci loadState = false; 739596a2c1Sopenharmony_ci} 749596a2c1Sopenharmony_ci 759596a2c1Sopenharmony_cibool I18nServiceAbilityLoadManager::WaitLoadStateChange(int32_t systemAbilityId) 769596a2c1Sopenharmony_ci{ 779596a2c1Sopenharmony_ci std::unique_lock<std::mutex> lock(loadStateMutex); 789596a2c1Sopenharmony_ci auto isLoadSuccess = loadStateCondition.wait_for(lock, std::chrono::milliseconds(I18N_LOAD_SA_TIMEOUT_MS), [this] { 799596a2c1Sopenharmony_ci return loadState; 809596a2c1Sopenharmony_ci }); 819596a2c1Sopenharmony_ci if (!isLoadSuccess) { 829596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::WaitLoadStateChange timeout."); 839596a2c1Sopenharmony_ci } 849596a2c1Sopenharmony_ci return isLoadSuccess; 859596a2c1Sopenharmony_ci} 869596a2c1Sopenharmony_ci 879596a2c1Sopenharmony_cibool I18nServiceAbilityLoadManager::UnloadI18nService(int32_t systemAbilityId) 889596a2c1Sopenharmony_ci{ 899596a2c1Sopenharmony_ci sptr<ISystemAbilityManager> samgr = SystemAbilityManagerClient::GetInstance().GetSystemAbilityManager(); 909596a2c1Sopenharmony_ci if (samgr == nullptr) { 919596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService can't get samgr."); 929596a2c1Sopenharmony_ci return false; 939596a2c1Sopenharmony_ci } 949596a2c1Sopenharmony_ci int32_t ret = samgr->UnloadSystemAbility(systemAbilityId); 959596a2c1Sopenharmony_ci if (ret != ERR_OK) { 969596a2c1Sopenharmony_ci HILOG_ERROR_I18N("I18nServiceAbilityLoadManager::UnloadI18nService sa unload failed."); 979596a2c1Sopenharmony_ci return false; 989596a2c1Sopenharmony_ci } 999596a2c1Sopenharmony_ci return true; 1009596a2c1Sopenharmony_ci} 1019596a2c1Sopenharmony_ci 1029596a2c1Sopenharmony_civoid I18nServiceAbilityLoadManager::LoadSystemAbilitySuccess() 1039596a2c1Sopenharmony_ci{ 1049596a2c1Sopenharmony_ci std::unique_lock<std::mutex> lock(loadStateMutex); 1059596a2c1Sopenharmony_ci loadState = true; 1069596a2c1Sopenharmony_ci loadStateCondition.notify_one(); 1079596a2c1Sopenharmony_ci} 1089596a2c1Sopenharmony_ci 1099596a2c1Sopenharmony_civoid I18nServiceAbilityLoadManager::LoadSystemAbilityFail() 1109596a2c1Sopenharmony_ci{ 1119596a2c1Sopenharmony_ci std::unique_lock<std::mutex> lock(loadStateMutex); 1129596a2c1Sopenharmony_ci loadState = false; 1139596a2c1Sopenharmony_ci loadStateCondition.notify_one(); 1149596a2c1Sopenharmony_ci} 1159596a2c1Sopenharmony_ci} // namespace I18n 1169596a2c1Sopenharmony_ci} // namespace Global 1179596a2c1Sopenharmony_ci} // namespace OHOS