• 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     int32_t err = CheckPermission();
43     if (err != SUCCESS) {
44         outValue = err;
45         return SUCCESS;
46     }
47     outValue = FontManager::GetInstance()->InstallFont(fd);
48     UnloadFontServiceAbility();
49     return SUCCESS;
50 }
51 
UninstallFont(const std::string & fontName,int32_t & outValue)52 int32_t FontManagerServer::UninstallFont(const std::string &fontName, int32_t &outValue)
53 {
54     int32_t err = CheckPermission();
55     if (err != SUCCESS) {
56         outValue = err;
57         return SUCCESS;
58     }
59     outValue = FontManager::GetInstance()->UninstallFont(fontName);
60     UnloadFontServiceAbility();
61     return SUCCESS;
62 }
63 
UnloadFontServiceAbility()64 void FontManagerServer::UnloadFontServiceAbility()
65 {
66     auto task = [this]() {
67         auto fontSaLoadManager = DelayedSingleton<FontServiceLoadManager>::GetInstance();
68         if (fontSaLoadManager != nullptr) {
69             FONT_LOGI("FontManagerServer::UnloadFontServiceAbility start to unload fontManager SA.");
70             fontSaLoadManager->UnloadFontService(FONT_SA_ID);
71         }
72     };
73     if (handler_ != nullptr) {
74         handler_->RemoveTask(UNLOAD_TASK);
75         handler_->PostTask(task, UNLOAD_TASK, DELAY_MILLISECONDS_FOR_UNLOAD_SA);
76     }
77 }
78 
OnStart(const SystemAbilityOnDemandReason & startReason)79 void FontManagerServer::OnStart(const SystemAbilityOnDemandReason &startReason)
80 {
81     FONT_LOGI("FontManagerServer OnStart, startReason name %{public}s", startReason.GetName().c_str());
82     bool status = Publish(this);
83     if (status) {
84         FONT_LOGI("FontManagerServer Publish success.");
85     } else {
86         FONT_LOGI("FontManagerServer Publish failed.");
87     }
88     FileUtils::DeleteDir(FONTS_TEMP_PATH, false);
89     handler_ = std::make_shared<AppExecFwk::EventHandler>(AppExecFwk::EventRunner::Create(true));
90     UnloadFontServiceAbility();
91 }
92 
OnStop(const SystemAbilityOnDemandReason & stopReason)93 void FontManagerServer::OnStop(const SystemAbilityOnDemandReason &stopReason)
94 {
95     FONT_LOGI("FontManagerServer OnStop, stopReason name %{public}s", stopReason.GetName().c_str());
96 }
97 
CheckPermission()98 int32_t FontManagerServer::CheckPermission()
99 {
100     uint64_t accessTokenID = IPCSkeleton::GetCallingFullTokenID();
101     uint32_t callerToken = IPCSkeleton::GetCallingTokenID();
102     bool isSystemApp = Security::AccessToken::TokenIdKit::IsSystemAppByFullTokenID(accessTokenID);
103     if (!isSystemApp) {
104         FONT_LOGE("caller process is not System app.");
105         return ERR_NOT_SYSTEM_APP;
106     }
107     int result = Security::AccessToken::AccessTokenKit::VerifyAccessToken(callerToken, PERMISSION_UPDATE_FONT);
108     if (result != Security::AccessToken::PermissionState::PERMISSION_GRANTED) {
109         FONT_LOGE("FontManagerServer caller process doesn't have UPDATE_FONT permission.");
110         return ERR_NO_PERMISSION;
111     }
112     return SUCCESS;
113 }
114 } // namespace FontManager
115 } // namespace Global
116 } // namespace OHOS