• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024-2025 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_manager_server.h"
17 
18 #include "accesstoken_kit.h"
19 #include "file_utils.h"
20 #include "font_define.h"
21 #include "font_hilog.h"
22 #include "font_manager.h"
23 #include "font_service_load_manager.h"
24 #include "ipc_skeleton.h"
25 #include "tokenid_kit.h"
26 
27 namespace OHOS {
28 namespace Global {
29 namespace FontManager {
30 REGISTER_SYSTEM_ABILITY_BY_ID(FontManagerServer, FONT_SA_ID, false);
31 static const std::string FONTS_TEMP_PATH = "/data/service/el1/public/for-all-app/fonts/temp/";
32 static const std::string UNLOAD_TASK = "font_service_unload";
33 static const std::string PERMISSION_UPDATE_FONT = "ohos.permission.UPDATE_FONT";
34 static const uint32_t DELAY_MILLISECONDS_FOR_UNLOAD_SA = 10000;
35 
FontManagerServer(int32_t saId,bool runOnCreate)36 FontManagerServer::FontManagerServer(int32_t saId, bool runOnCreate) : SystemAbility(saId, runOnCreate)
37 {
38 }
39 
InstallFont(const int32_t fd,int32_t & outValue)40 int32_t FontManagerServer::InstallFont(const int32_t fd, int32_t &outValue)
41 {
42     RemoveUnloadFontServiceTask();
43     int32_t ret = CheckPermission();
44     if (ret != SUCCESS) {
45         outValue = ret;
46     } else {
47         outValue = FontManager::GetInstance()->InstallFont(fd);
48     }
49     AddUnloadFontServiceTask();
50     return SUCCESS;
51 }
52 
UninstallFont(const std::string & fontName,int32_t & outValue)53 int32_t FontManagerServer::UninstallFont(const std::string &fontName, int32_t &outValue)
54 {
55     RemoveUnloadFontServiceTask();
56     int32_t ret = CheckPermission();
57     if (ret != SUCCESS) {
58         outValue = ret;
59     } else {
60         outValue = FontManager::GetInstance()->UninstallFont(fontName);
61     }
62     AddUnloadFontServiceTask();
63     return SUCCESS;
64 }
65 
AddUnloadFontServiceTask()66 void FontManagerServer::AddUnloadFontServiceTask()
67 {
68     auto task = [this]() {
69         auto fontSaLoadManager = DelayedSingleton<FontServiceLoadManager>::GetInstance();
70         if (fontSaLoadManager != nullptr) {
71             FONT_LOGI("FontManagerServer start to unload fontManager SA.");
72             fontSaLoadManager->UnloadFontService(FONT_SA_ID);
73         }
74     };
75     if (handler_ != nullptr) {
76         handler_->PostTask(task, UNLOAD_TASK, DELAY_MILLISECONDS_FOR_UNLOAD_SA);
77     }
78 }
79 
RemoveUnloadFontServiceTask()80 void FontManagerServer::RemoveUnloadFontServiceTask()
81 {
82     if (handler_ != nullptr) {
83         handler_->RemoveTask(UNLOAD_TASK);
84     }
85 }
86 
OnStart(const SystemAbilityOnDemandReason & startReason)87 void FontManagerServer::OnStart(const SystemAbilityOnDemandReason &startReason)
88 {
89     FONT_LOGI("FontManagerServer OnStart, startReason name %{public}s", startReason.GetName().c_str());
90     bool status = Publish(this);
91     if (status) {
92         FONT_LOGI("FontManagerServer Publish success.");
93     } else {
94         FONT_LOGI("FontManagerServer Publish failed.");
95     }
96     FileUtils::DeleteDir(FONTS_TEMP_PATH, false);
97     handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
98     AddUnloadFontServiceTask();
99 }
100 
OnStop(const SystemAbilityOnDemandReason & stopReason)101 void FontManagerServer::OnStop(const SystemAbilityOnDemandReason &stopReason)
102 {
103     FONT_LOGI("FontManagerServer OnStop, stopReason name %{public}s", stopReason.GetName().c_str());
104 }
105 
CheckPermission()106 int32_t FontManagerServer::CheckPermission()
107 {
108     uint64_t accessTokenID = IPCSkeleton::GetCallingFullTokenID();
109     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
110     bool isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenID);
111     if (!isSystemApp) {
112         FONT_LOGE("caller process is not System app.");
113         return ERR_NOT_SYSTEM_APP;
114     }
115     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, PERMISSION_UPDATE_FONT);
116     if (result != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
117         FONT_LOGE("FontManagerServer caller process doesn't have UPDATE_FONT permission.");
118         return ERR_NO_PERMISSION;
119     }
120     return SUCCESS;
121 }
122 } // namespace FontManager
123 } // namespace Global
124 } // namespace OHOS