• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023-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 "accesstoken_kit.h"
17 #include "i18n_hilog.h"
18 #include "i18n_service_ability_load_manager.h"
19 #include "ipc_skeleton.h"
20 #include "locale_config.h"
21 #include "mem_mgr_client.h"
22 #include "mem_mgr_proxy.h"
23 #include "multi_users.h"
24 #include "os_account_manager.h"
25 #include "preferred_language.h"
26 #include "preferences.h"
27 #include "preferences_helper.h"
28 #include "system_ability_definition.h"
29 #include "tokenid_kit.h"
30 #include "i18n_service_ability.h"
31 
32 namespace OHOS {
33 namespace Global {
34 namespace I18n {
35 REGISTER_SYSTEM_ABILITY_BY_ID(I18nServiceAbility, I18N_SA_ID, false);
36 static const std::string UNLOAD_TASK = "i18n_service_unload";
37 static const uint32_t DELAY_MILLISECONDS_FOR_UNLOAD_SA = 10000;
38 static const int32_t UID_TRANSFORM_DIVISOR = 200000;
39 static const int32_t USER_DEFAULT = 100;
40 
I18nServiceAbility(int32_t saId,bool runOnCreate)41 I18nServiceAbility::I18nServiceAbility(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate), taskNumber(0)
42 {
43     HILOG_INFO_I18N("I18nServiceAbility object init success.");
44 }
45 
~I18nServiceAbility()46 I18nServiceAbility::~I18nServiceAbility()
47 {
48     HILOG_INFO_I18N("I18nServiceAbility object release.");
49 }
50 
SetSystemLanguage(const std::string & language,int32_t & code)51 ErrCode I18nServiceAbility::SetSystemLanguage(const std::string& language, int32_t& code)
52 {
53     int pid = getpid();
54     StartTask(pid);
55     I18nErrorCode errCode = CheckPermission();
56     if (errCode != I18nErrorCode::SUCCESS) {
57         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemLanguage: Check permission failed.");
58         code = static_cast<int32_t>(errCode);
59         FinishTask(pid);
60         return ERR_OK;
61     }
62 #ifdef SUPPORT_MULTI_USER
63     int32_t userId = GetCallingUserId();
64     errCode = LocaleConfig::SetSystemLanguage(language, userId);
65 #else
66     errCode = LocaleConfig::SetSystemLanguage(language);
67 #endif
68     code = static_cast<int32_t>(errCode);
69     FinishTask(pid);
70     return ERR_OK;
71 }
72 
SetSystemRegion(const std::string & region,int32_t & code)73 ErrCode I18nServiceAbility::SetSystemRegion(const std::string& region, int32_t& code)
74 {
75     int pid = getpid();
76     StartTask(pid);
77     I18nErrorCode errCode = CheckPermission();
78     if (errCode != I18nErrorCode::SUCCESS) {
79         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemRegion: Check permission failed.");
80         code = static_cast<int32_t>(errCode);
81         FinishTask(pid);
82         return ERR_OK;
83     }
84 #ifdef SUPPORT_MULTI_USER
85     int32_t userId = GetCallingUserId();
86     errCode = LocaleConfig::SetSystemRegion(region, userId);
87 #else
88     errCode = LocaleConfig::SetSystemRegion(region);
89 #endif
90     code = static_cast<int32_t>(errCode);
91     FinishTask(pid);
92     return ERR_OK;
93 }
94 
SetSystemLocale(const std::string & locale,int32_t & code)95 ErrCode I18nServiceAbility::SetSystemLocale(const std::string& locale, int32_t& code)
96 {
97     int pid = getpid();
98     StartTask(pid);
99     I18nErrorCode errCode = CheckPermission();
100     if (errCode != I18nErrorCode::SUCCESS) {
101         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemLocale: Check permission failed.");
102         code = static_cast<int32_t>(errCode);
103         FinishTask(pid);
104         return ERR_OK;
105     }
106 #ifdef SUPPORT_MULTI_USER
107     int32_t userId = GetCallingUserId();
108     errCode = LocaleConfig::SetSystemLocale(locale, userId);
109 #else
110     errCode = LocaleConfig::SetSystemLocale(locale);
111 #endif
112     code = static_cast<int32_t>(errCode);
113     FinishTask(pid);
114     return ERR_OK;
115 }
116 
Set24HourClock(const std::string & flag,int32_t & code)117 ErrCode I18nServiceAbility::Set24HourClock(const std::string& flag, int32_t& code)
118 {
119     int pid = getpid();
120     StartTask(pid);
121     I18nErrorCode errCode = CheckPermission();
122     if (errCode != I18nErrorCode::SUCCESS) {
123         HILOG_ERROR_I18N("I18nServiceAbility::Set24HourClock: Check permission failed.");
124         code = static_cast<int32_t>(errCode);
125         FinishTask(pid);
126         return ERR_OK;
127     }
128 #ifdef SUPPORT_MULTI_USER
129     int32_t userId = GetCallingUserId();
130     errCode = LocaleConfig::Set24HourClock(flag, userId);
131 #else
132     errCode = LocaleConfig::Set24HourClock(flag);
133 #endif
134     code = static_cast<int32_t>(errCode);
135     FinishTask(pid);
136     return ERR_OK;
137 }
138 
SetUsingLocalDigit(bool flag,int32_t & code)139 ErrCode I18nServiceAbility::SetUsingLocalDigit(bool flag, int32_t& code)
140 {
141     int pid = getpid();
142     StartTask(pid);
143     I18nErrorCode errCode = CheckPermission();
144     if (errCode != I18nErrorCode::SUCCESS) {
145         HILOG_ERROR_I18N("I18nServiceAbility::SetUsingLocalDigit: Check permission failed.");
146         code = static_cast<int32_t>(errCode);
147         FinishTask(pid);
148         return ERR_OK;
149     }
150 #ifdef SUPPORT_MULTI_USER
151     int32_t userId = GetCallingUserId();
152     errCode = LocaleConfig::SetUsingLocalDigit(flag, userId);
153 #else
154     errCode = LocaleConfig::SetUsingLocalDigit(flag);
155 #endif
156     code = static_cast<int32_t>(errCode);
157     FinishTask(pid);
158     return ERR_OK;
159 }
160 
AddPreferredLanguage(const std::string & language,int32_t index,int32_t & code)161 ErrCode I18nServiceAbility::AddPreferredLanguage(const std::string& language, int32_t index, int32_t& code)
162 {
163     int pid = getpid();
164     StartTask(pid);
165     I18nErrorCode errCode = CheckPermission();
166     if (errCode != I18nErrorCode::SUCCESS) {
167         HILOG_ERROR_I18N("I18nServiceAbility::AddPreferredLanguage: Check permission failed.");
168         code = static_cast<int32_t>(errCode);
169         FinishTask(pid);
170         return ERR_OK;
171     }
172     errCode = PreferredLanguage::AddPreferredLanguage(language, index);
173     code = static_cast<int32_t>(errCode);
174     FinishTask(pid);
175     return ERR_OK;
176 }
177 
RemovePreferredLanguage(int32_t index,int32_t & code)178 ErrCode I18nServiceAbility::RemovePreferredLanguage(int32_t index, int32_t& code)
179 {
180     int pid = getpid();
181     StartTask(pid);
182     I18nErrorCode errCode = CheckPermission();
183     if (errCode != I18nErrorCode::SUCCESS) {
184         HILOG_ERROR_I18N("I18nServiceAbility::RemovePreferredLanguage: Check permission failed.");
185         code = static_cast<int32_t>(errCode);
186         FinishTask(pid);
187         return ERR_OK;
188     }
189     errCode = PreferredLanguage::RemovePreferredLanguage(index);
190     code = static_cast<int32_t>(errCode);
191     FinishTask(pid);
192     return ERR_OK;
193 }
194 
SetTemperatureType(int32_t type,int32_t & code)195 ErrCode I18nServiceAbility::SetTemperatureType(int32_t type, int32_t& code)
196 {
197     int pid = getpid();
198     StartTask(pid);
199     I18nErrorCode errCode = CheckPermission();
200     if (errCode != I18nErrorCode::SUCCESS) {
201         HILOG_ERROR_I18N("I18nServiceAbility::SetTemperatureType: Check permission failed.");
202         code = static_cast<int32_t>(errCode);
203         FinishTask(pid);
204         return ERR_OK;
205     }
206     TemperatureType temperatureType = static_cast<TemperatureType>(type);
207 #ifdef SUPPORT_MULTI_USER
208     int32_t userId = GetCallingUserId();
209     errCode = LocaleConfig::SetTemperatureType(temperatureType, userId);
210 #else
211     errCode = LocaleConfig::SetTemperatureType(temperatureType);
212 #endif
213     code = static_cast<int32_t>(errCode);
214     FinishTask(pid);
215     return ERR_OK;
216 }
217 
SetFirstDayOfWeek(int32_t type,int32_t & code)218 ErrCode I18nServiceAbility::SetFirstDayOfWeek(int32_t type, int32_t& code)
219 {
220     int pid = getpid();
221     StartTask(pid);
222     I18nErrorCode errCode = CheckPermission();
223     if (errCode != I18nErrorCode::SUCCESS) {
224         HILOG_ERROR_I18N("I18nServiceAbility::SetFirstDayOfWeek: Check permission failed.");
225         code = static_cast<int32_t>(errCode);
226         FinishTask(pid);
227         return ERR_OK;
228     }
229     WeekDay weekDay = static_cast<WeekDay>(type);
230 #ifdef SUPPORT_MULTI_USER
231     int32_t userId = GetCallingUserId();
232     errCode = LocaleConfig::SetFirstDayOfWeek(weekDay, userId);
233 #else
234     errCode = LocaleConfig::SetFirstDayOfWeek(weekDay);
235 #endif
236     code = static_cast<int32_t>(errCode);
237     FinishTask(pid);
238     return ERR_OK;
239 }
240 
GetSystemCollations(std::unordered_map<std::string,std::string> & systemCollations,int32_t & code)241 ErrCode I18nServiceAbility::GetSystemCollations(std::unordered_map<std::string, std::string>& systemCollations,
242     int32_t& code)
243 {
244     int pid = getpid();
245     StartTask(pid);
246     I18nErrorCode errCode = CheckSystemPermission();
247     if (errCode != I18nErrorCode::SUCCESS) {
248         HILOG_ERROR_I18N("I18nServiceAbility::GetSystemCollations: Check system permission failed.");
249         code = static_cast<int32_t>(errCode);
250         FinishTask(pid);
251         return ERR_OK;
252     }
253     errCode = LocaleConfig::GetSystemCollations(systemCollations);
254     code = static_cast<int32_t>(errCode);
255     FinishTask(pid);
256     return ERR_OK;
257 }
258 
GetUsingCollation(std::string & usingCollation,int32_t & code)259 ErrCode I18nServiceAbility::GetUsingCollation(std::string& usingCollation, int32_t& code)
260 {
261     int pid = getpid();
262     StartTask(pid);
263     I18nErrorCode errCode = CheckSystemPermission();
264     if (errCode != I18nErrorCode::SUCCESS) {
265         HILOG_ERROR_I18N("I18nServiceAbility::GetUsingCollation: Check system permission failed.");
266         code = static_cast<int32_t>(errCode);
267         FinishTask(pid);
268         return ERR_OK;
269     }
270     errCode = LocaleConfig::GetUsingCollation(usingCollation);
271     code = static_cast<int32_t>(errCode);
272     FinishTask(pid);
273     return ERR_OK;
274 }
275 
SetSystemCollation(const std::string & identifier,int32_t & code)276 ErrCode I18nServiceAbility::SetSystemCollation(const std::string& identifier, int32_t& code)
277 {
278     int pid = getpid();
279     StartTask(pid);
280     I18nErrorCode errCode = CheckPermission();
281     if (errCode != I18nErrorCode::SUCCESS) {
282         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemCollation: Check permission failed.");
283         code = static_cast<int32_t>(errCode);
284         FinishTask(pid);
285         return ERR_OK;
286     }
287 #ifdef SUPPORT_MULTI_USER
288     int32_t userId = GetCallingUserId();
289     errCode = LocaleConfig::SetSystemCollation(identifier, userId);
290 #else
291     errCode = LocaleConfig::SetSystemCollation(identifier);
292 #endif
293     code = static_cast<int32_t>(errCode);
294     FinishTask(pid);
295     return ERR_OK;
296 }
297 
GetSystemNumberingSystems(std::unordered_map<std::string,std::string> & systemNumberingSystems,int32_t & code)298 ErrCode I18nServiceAbility::GetSystemNumberingSystems(
299     std::unordered_map<std::string, std::string>& systemNumberingSystems, int32_t& code)
300 {
301     int pid = getpid();
302     StartTask(pid);
303     I18nErrorCode errCode = CheckSystemPermission();
304     if (errCode != I18nErrorCode::SUCCESS) {
305         HILOG_ERROR_I18N("I18nServiceAbility::GetSystemNumberingSystems: Check system permission failed.");
306         code = static_cast<int32_t>(errCode);
307         FinishTask(pid);
308         return ERR_OK;
309     }
310     errCode = LocaleConfig::GetSystemNumberingSystems(systemNumberingSystems);
311     code = static_cast<int32_t>(errCode);
312     FinishTask(pid);
313     return ERR_OK;
314 }
315 
GetUsingNumberingSystem(std::string & usingNumberingSystem,int32_t & code)316 ErrCode I18nServiceAbility::GetUsingNumberingSystem(std::string& usingNumberingSystem, int32_t& code)
317 {
318     int pid = getpid();
319     StartTask(pid);
320     I18nErrorCode errCode = CheckSystemPermission();
321     if (errCode != I18nErrorCode::SUCCESS) {
322         HILOG_ERROR_I18N("I18nServiceAbility::GetUsingNumberingSystem: Check system permission failed.");
323         code = static_cast<int32_t>(errCode);
324         FinishTask(pid);
325         return ERR_OK;
326     }
327     errCode = LocaleConfig::GetUsingNumberingSystem(usingNumberingSystem);
328     code = static_cast<int32_t>(errCode);
329     FinishTask(pid);
330     return ERR_OK;
331 }
332 
SetSystemNumberingSystem(const std::string & identifier,int32_t & code)333 ErrCode I18nServiceAbility::SetSystemNumberingSystem(const std::string& identifier, int32_t& code)
334 {
335     int pid = getpid();
336     StartTask(pid);
337     I18nErrorCode errCode = CheckPermission();
338     if (errCode != I18nErrorCode::SUCCESS) {
339         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemNumberingSystem: Check permission failed.");
340         code = static_cast<int32_t>(errCode);
341         FinishTask(pid);
342         return ERR_OK;
343     }
344 #ifdef SUPPORT_MULTI_USER
345     int32_t userId = GetCallingUserId();
346     errCode = LocaleConfig::SetSystemNumberingSystem(identifier, userId);
347 #else
348     errCode = LocaleConfig::SetSystemNumberingSystem(identifier);
349 #endif
350     code = static_cast<int32_t>(errCode);
351     FinishTask(pid);
352     return ERR_OK;
353 }
354 
GetSystemNumberPatterns(std::unordered_map<std::string,std::string> & systemNumberPatterns,int32_t & code)355 ErrCode I18nServiceAbility::GetSystemNumberPatterns(std::unordered_map<std::string, std::string>& systemNumberPatterns,
356     int32_t& code)
357 {
358     int pid = getpid();
359     StartTask(pid);
360     I18nErrorCode errCode = CheckSystemPermission();
361     if (errCode != I18nErrorCode::SUCCESS) {
362         HILOG_ERROR_I18N("I18nServiceAbility::GetSystemNumberPatterns: Check system permission failed.");
363         code = static_cast<int32_t>(errCode);
364         FinishTask(pid);
365         return ERR_OK;
366     }
367     errCode = LocaleConfig::GetSystemNumberPatterns(systemNumberPatterns);
368     code = static_cast<int32_t>(errCode);
369     FinishTask(pid);
370     return ERR_OK;
371 }
372 
GetUsingNumberPattern(std::string & usingNumberPattern,int32_t & code)373 ErrCode I18nServiceAbility::GetUsingNumberPattern(std::string& usingNumberPattern, int32_t& code)
374 {
375     int pid = getpid();
376     StartTask(pid);
377     I18nErrorCode errCode = CheckSystemPermission();
378     if (errCode != I18nErrorCode::SUCCESS) {
379         HILOG_ERROR_I18N("I18nServiceAbility::GetUsingNumberPattern: Check system permission failed.");
380         code = static_cast<int32_t>(errCode);
381         FinishTask(pid);
382         return ERR_OK;
383     }
384     errCode = LocaleConfig::GetUsingNumberPattern(usingNumberPattern);
385     code = static_cast<int32_t>(errCode);
386     FinishTask(pid);
387     return ERR_OK;
388 }
389 
SetSystemNumberPattern(const std::string & pattern,int32_t & code)390 ErrCode I18nServiceAbility::SetSystemNumberPattern(const std::string& pattern, int32_t& code)
391 {
392     int pid = getpid();
393     StartTask(pid);
394     I18nErrorCode errCode = CheckPermission();
395     if (errCode != I18nErrorCode::SUCCESS) {
396         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemNumberPattern: Check permission failed.");
397         code = static_cast<int32_t>(errCode);
398         FinishTask(pid);
399         return ERR_OK;
400     }
401 #ifdef SUPPORT_MULTI_USER
402     int32_t userId = GetCallingUserId();
403     errCode = LocaleConfig::SetSystemNumberPattern(pattern, userId);
404 #else
405     errCode = LocaleConfig::SetSystemNumberPattern(pattern);
406 #endif
407     code = static_cast<int32_t>(errCode);
408     FinishTask(pid);
409     return ERR_OK;
410 }
411 
GetSystemMeasurements(std::unordered_map<std::string,std::string> & systemMeasurements,int32_t & code)412 ErrCode I18nServiceAbility::GetSystemMeasurements(std::unordered_map<std::string, std::string>& systemMeasurements,
413     int32_t& code)
414 {
415     int pid = getpid();
416     StartTask(pid);
417     I18nErrorCode errCode = CheckSystemPermission();
418     if (errCode != I18nErrorCode::SUCCESS) {
419         HILOG_ERROR_I18N("I18nServiceAbility::GetSystemMeasurements: Check system permission failed.");
420         code = static_cast<int32_t>(errCode);
421         FinishTask(pid);
422         return ERR_OK;
423     }
424     errCode = LocaleConfig::GetSystemMeasurements(systemMeasurements);
425     code = static_cast<int32_t>(errCode);
426     FinishTask(pid);
427     return ERR_OK;
428 }
429 
GetUsingMeasurement(std::string & identifier,int32_t & code)430 ErrCode I18nServiceAbility::GetUsingMeasurement(std::string& identifier, int32_t& code)
431 {
432     int pid = getpid();
433     StartTask(pid);
434     I18nErrorCode errCode = CheckSystemPermission();
435     if (errCode != I18nErrorCode::SUCCESS) {
436         HILOG_ERROR_I18N("I18nServiceAbility::GetUsingMeasurement: Check system permission failed.");
437         code = static_cast<int32_t>(errCode);
438         FinishTask(pid);
439         return ERR_OK;
440     }
441     errCode = LocaleConfig::GetUsingMeasurement(identifier);
442     code = static_cast<int32_t>(errCode);
443     FinishTask(pid);
444     return ERR_OK;
445 }
446 
SetSystemMeasurement(const std::string & identifier,int32_t & code)447 ErrCode I18nServiceAbility::SetSystemMeasurement(const std::string& identifier, int32_t& code)
448 {
449     int pid = getpid();
450     StartTask(pid);
451     I18nErrorCode errCode = CheckPermission();
452     if (errCode != I18nErrorCode::SUCCESS) {
453         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemMeasurement: Check permission failed.");
454         code = static_cast<int32_t>(errCode);
455         FinishTask(pid);
456         return ERR_OK;
457     }
458 #ifdef SUPPORT_MULTI_USER
459     int32_t userId = GetCallingUserId();
460     errCode = LocaleConfig::SetSystemMeasurement(identifier, userId);
461 #else
462     errCode = LocaleConfig::SetSystemMeasurement(identifier);
463 #endif
464     code = static_cast<int32_t>(errCode);
465     FinishTask(pid);
466     return ERR_OK;
467 }
468 
GetSystemNumericalDatePatterns(std::unordered_map<std::string,std::string> & numericalDatePatterns,int32_t & code)469 ErrCode I18nServiceAbility::GetSystemNumericalDatePatterns(
470     std::unordered_map<std::string, std::string>& numericalDatePatterns, int32_t& code)
471 {
472     int pid = getpid();
473     StartTask(pid);
474     I18nErrorCode errCode = CheckSystemPermission();
475     if (errCode != I18nErrorCode::SUCCESS) {
476         HILOG_ERROR_I18N("I18nServiceAbility::GetSystemNumericalDatePatterns: Check system permission failed.");
477         code = static_cast<int32_t>(errCode);
478         FinishTask(pid);
479         return ERR_OK;
480     }
481     errCode = LocaleConfig::GetSystemNumericalDatePatterns(numericalDatePatterns);
482     code = static_cast<int32_t>(errCode);
483     FinishTask(pid);
484     return ERR_OK;
485 }
486 
GetUsingNumericalDatePattern(std::string & identifier,int32_t & code)487 ErrCode I18nServiceAbility::GetUsingNumericalDatePattern(std::string& identifier, int32_t& code)
488 {
489     int pid = getpid();
490     StartTask(pid);
491     I18nErrorCode errCode = CheckSystemPermission();
492     if (errCode != I18nErrorCode::SUCCESS) {
493         HILOG_ERROR_I18N("I18nServiceAbility::GetUsingNumericalDatePattern: Check system permission failed.");
494         code = static_cast<int32_t>(errCode);
495         FinishTask(pid);
496         return ERR_OK;
497     }
498     errCode = LocaleConfig::GetUsingNumericalDatePattern(identifier);
499     code = static_cast<int32_t>(errCode);
500     FinishTask(pid);
501     return ERR_OK;
502 }
503 
SetSystemNumericalDatePattern(const std::string & identifier,int32_t & code)504 ErrCode I18nServiceAbility::SetSystemNumericalDatePattern(const std::string& identifier, int32_t& code)
505 {
506     int pid = getpid();
507     StartTask(pid);
508     I18nErrorCode errCode = CheckPermission();
509     if (errCode != I18nErrorCode::SUCCESS) {
510         HILOG_ERROR_I18N("I18nServiceAbility::SetSystemNumericalDatePattern: Check permission failed.");
511         code = static_cast<int32_t>(errCode);
512         FinishTask(pid);
513         return ERR_OK;
514     }
515 #ifdef SUPPORT_MULTI_USER
516     int32_t userId = GetCallingUserId();
517     errCode = LocaleConfig::SetSystemNumericalDatePattern(identifier, userId);
518 #else
519     errCode = LocaleConfig::SetSystemNumericalDatePattern(identifier);
520 #endif
521     code = static_cast<int32_t>(errCode);
522     FinishTask(pid);
523     return ERR_OK;
524 }
525 
GetLanguageFromUserId(int32_t userId,std::string & language)526 ErrCode I18nServiceAbility::GetLanguageFromUserId(int32_t userId, std::string& language)
527 {
528     int pid = getpid();
529     StartTask(pid);
530 #ifdef SUPPORT_MULTI_USER
531     language = MultiUsers::GetLanguageFromUserId(userId);
532 #endif
533     FinishTask(pid);
534     return ERR_OK;
535 }
536 
DelayUnloadI18nServiceAbility()537 void I18nServiceAbility::DelayUnloadI18nServiceAbility()
538 {
539     if (handler != nullptr) {
540         handler->RemoveTask(UNLOAD_TASK);
541     }
542     auto task = [this]() {
543         auto i18nSaLoadManager = DelayedSingleton<I18nServiceAbilityLoadManager>::GetInstance();
544         if (i18nSaLoadManager != nullptr) {
545             HILOG_INFO_I18N("I18nServiceAbility::UnloadI18nServiceAbility start to unload i18n sa.");
546             i18nSaLoadManager->UnloadI18nService(I18N_SA_ID);
547         }
548     };
549     if (handler != nullptr) {
550         handler->PostTask(task, UNLOAD_TASK, DELAY_MILLISECONDS_FOR_UNLOAD_SA);
551     }
552 }
553 
OnStart(const SystemAbilityOnDemandReason & startReason)554 void I18nServiceAbility::OnStart(const SystemAbilityOnDemandReason& startReason)
555 {
556     HILOG_INFO_I18N("I18nServiceAbility start.");
557     i18nServiceEvent = std::make_unique<I18nServiceEvent>();
558     if (i18nServiceEvent != nullptr) {
559         i18nServiceEvent->SubscriberEvent();
560         i18nServiceEvent->CheckStartReason(startReason);
561     }
562 #ifdef SUPPORT_MULTI_USER
563     MultiUsers::InitMultiUser();
564 #endif
565     bool status = Publish(this);
566     if (status) {
567         HILOG_INFO_I18N("I18nServiceAbility Publish success.");
568     } else {
569         HILOG_INFO_I18N("I18nServiceAbility Publish failed.");
570     }
571     handler = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
572     DelayUnloadI18nServiceAbility();
573     int pid = getpid();
574     Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 1, I18N_SA_ID);
575 }
576 
OnStop()577 void I18nServiceAbility::OnStop()
578 {
579     int pid = getpid();
580     Memory::MemMgrClient::GetInstance().NotifyProcessStatus(pid, 1, 0, I18N_SA_ID);
581     HILOG_INFO_I18N("I18nServiceAbility Stop.");
582 }
583 
OnIdle(const SystemAbilityOnDemandReason & idleReason)584 int32_t I18nServiceAbility::OnIdle(const SystemAbilityOnDemandReason& idleReason)
585 {
586     if (UpdateTaskNumber(ModifyTaskNumber::QUERY) == 0) {
587         HILOG_INFO_I18N("I18nServiceAbility::OnIdle: Immediately unload.");
588         return 0;
589     }
590     HILOG_INFO_I18N("I18nServiceAbility::OnIdle: Delay unload.");
591     return DELAY_MILLISECONDS_FOR_UNLOAD_SA;
592 }
593 
UpdateTaskNumber(ModifyTaskNumber action)594 int32_t I18nServiceAbility::UpdateTaskNumber(ModifyTaskNumber action)
595 {
596     std::lock_guard<std::mutex> taskLock(taskNumberMutex);
597     taskNumber += static_cast<int32_t>(action);
598     return taskNumber;
599 }
600 
StartTask(int32_t pid)601 void I18nServiceAbility::StartTask(int32_t pid)
602 {
603     if (Memory::MemMgrClient::GetInstance().SetCritical(pid, true, I18N_SA_ID) != 0) {
604         HILOG_ERROR_I18N("I18nServiceAbility::StartTask: Set critical true failed.");
605     }
606     UpdateTaskNumber(ModifyTaskNumber::INCREASE);
607     DelayUnloadI18nServiceAbility();
608 }
609 
FinishTask(int32_t pid)610 void I18nServiceAbility::FinishTask(int32_t pid)
611 {
612     if (Memory::MemMgrClient::GetInstance().SetCritical(pid, false, I18N_SA_ID) != 0) {
613         HILOG_ERROR_I18N("I18nServiceAbility::StartTask: Set critical true failed.");
614     }
615     UpdateTaskNumber(ModifyTaskNumber::DECREASE);
616 }
617 
GetCallingUserId()618 int32_t I18nServiceAbility::GetCallingUserId()
619 {
620     int32_t userId = OHOS::IPCSkeleton::GetCallingUid() / UID_TRANSFORM_DIVISOR;
621     if (userId == 0) {
622         auto err = OHOS::AccountSA::OsAccountManager::GetForegroundOsAccountLocalId(userId);
623         if (err != 0) {
624             HILOG_ERROR_I18N("I18nServiceAbility::GetCallingUserId: GetForegroundOsAccountLocalId failed.");
625             userId = USER_DEFAULT;
626         }
627     }
628     return userId;
629 }
630 
631 /**
632  * check whether request process has correct tokenType and permission.
633  */
CheckPermission()634 I18nErrorCode I18nServiceAbility::CheckPermission()
635 {
636     I18nErrorCode errCode = I18nServiceAbility::CheckSystemPermission();
637     if (errCode != I18nErrorCode::SUCCESS) {
638         return errCode;
639     }
640     return I18nServiceAbility::CheckUpdatePermission();
641 }
642 
CheckSystemPermission()643 I18nErrorCode I18nServiceAbility::CheckSystemPermission()
644 {
645     uint64_t callerFullToken = IPCSkeleton::GetCallingFullTokenID();
646     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
647     bool isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(callerFullToken);
648     Security::AccessToken::ATokenTypeEnum tokenType =
649         Security::AccessToken::AccessTokenKit::GetTokenTypeFlag(callerToken);
650     bool isShell = tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_SHELL;
651     bool isNative = tokenType == Security::AccessToken::ATokenTypeEnum::TOKEN_NATIVE;
652     if (!isSystemApp && !isShell && !isNative) {
653         HILOG_ERROR_I18N("I18nServiceAbility caller process is not System app, Shell or Native.");
654         return I18nErrorCode::NOT_SYSTEM_APP;
655     }
656     return I18nErrorCode::SUCCESS;
657 }
658 
CheckUpdatePermission()659 I18nErrorCode I18nServiceAbility::CheckUpdatePermission()
660 {
661     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
662     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken,
663         "ohos.permission.UPDATE_CONFIGURATION");
664     if (result != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
665         HILOG_ERROR_I18N("I18nServiceAbility caller process doesn't have UPDATE_CONFIGURATION permission.");
666         return I18nErrorCode::NO_PERMISSION;
667     }
668     return I18nErrorCode::SUCCESS;
669 }
670 } // namespace I18n
671 } // namespace Global
672 } // namespace OHOS