• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2023 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 "utd_client.h"
17 #include "logger.h"
18 #include "utd_graph.h"
19 #include "custom_utd_store.h"
20 namespace OHOS {
21 namespace UDMF {
22 constexpr const char* CUSTOM_TYPE_CFG_PATH = "/data/utd/utd-adt.json";
UtdClient()23 UtdClient::UtdClient()
24 {
25     Init();
26     LOG_INFO(UDMF_CLIENT, "construct UtdClient sucess.");
27 }
28 
~UtdClient()29 UtdClient::~UtdClient()
30 {
31 }
32 
GetInstance()33 UtdClient &UtdClient::GetInstance()
34 {
35     static auto instance = new UtdClient();
36     return *instance;
37 }
38 
Init()39 void UtdClient::Init()
40 {
41     descriptorCfgs_ = PresetTypeDescriptors::GetInstance().GetPresetTypes();
42     std::vector<TypeDescriptorCfg> customTypes =
43         CustomUtdStore::GetInstance().GetTypeCfgs(CUSTOM_TYPE_CFG_PATH);
44     if (!customTypes.empty()) {
45         descriptorCfgs_.insert(descriptorCfgs_.end(), customTypes.begin(), customTypes.end());
46     }
47     UtdGraph::GetInstance().InitUtdGraph(descriptorCfgs_);
48 }
49 
GetTypeDescriptor(const std::string & typeId,std::shared_ptr<TypeDescriptor> & descriptor)50 Status UtdClient::GetTypeDescriptor(const std::string &typeId, std::shared_ptr<TypeDescriptor> &descriptor)
51 {
52     for (const auto &utdTypeCfg : descriptorCfgs_) {
53         if (utdTypeCfg.typeId == typeId) {
54             descriptor = std::make_shared<TypeDescriptor>(utdTypeCfg);
55             LOG_DEBUG(UDMF_CLIENT, "get descriptor success. %{public}s ", typeId.c_str());
56             return Status::E_OK;
57         }
58     }
59     return Status::E_OK;
60 }
61 
GetUniformDataTypeByFilenameExtension(const std::string & fileExtension,std::string & typeId,std::string belongsTo)62 Status UtdClient::GetUniformDataTypeByFilenameExtension(const std::string &fileExtension, std::string &typeId,
63                                                         std::string belongsTo)
64 {
65     if (belongsTo != DEFAULT_TYPE_ID && !UtdGraph::GetInstance().IsValidType(belongsTo)) {
66         LOG_ERROR(UDMF_CLIENT, "invalid belongsTo. fileExtension:%{public}s, belongsTo:%{public}s ",
67                   fileExtension.c_str(), belongsTo.c_str());
68         return Status::E_INVALID_PARAMETERS;
69     }
70 
71     for (const auto &utdTypeCfg : descriptorCfgs_) {
72         std::vector<std::string> fileExtensions = utdTypeCfg.filenameExtensions;
73         if (find(fileExtensions.begin(), fileExtensions.end(), fileExtension) != fileExtensions.end()) {
74             typeId = utdTypeCfg.typeId;
75             break;
76         }
77     }
78 
79     // the find typeId is not belongsTo to the belongsTo.
80     if (!typeId.empty() && belongsTo != DEFAULT_TYPE_ID && belongsTo != typeId &&
81         !UtdGraph::GetInstance().IsLowerLevelType(belongsTo, typeId)) {
82         typeId = "";
83     }
84     return Status::E_OK;
85 }
86 
GetUniformDataTypeByMIMEType(const std::string & mimeType,std::string & typeId,std::string belongsTo)87 Status UtdClient::GetUniformDataTypeByMIMEType(const std::string &mimeType, std::string &typeId,
88                                                std::string belongsTo)
89 {
90     if (belongsTo != DEFAULT_TYPE_ID && !UtdGraph::GetInstance().IsValidType(belongsTo)) {
91         LOG_ERROR(UDMF_CLIENT, "invalid belongsTo. mimeType:%{public}s, belongsTo:%{public}s ",
92                   mimeType.c_str(), belongsTo.c_str());
93         return Status::E_INVALID_PARAMETERS;
94     }
95 
96     for (const auto &utdTypeCfg : descriptorCfgs_) {
97         std::vector<std::string> mimeTypes = utdTypeCfg.mimeTypes;
98         if (find(mimeTypes.begin(), mimeTypes.end(), mimeType) != mimeTypes.end()) {
99             typeId = utdTypeCfg.typeId;
100             break;
101         }
102     }
103     // the find typeId is not belongsTo to the belongsTo.
104     if (!typeId.empty() && belongsTo != DEFAULT_TYPE_ID && belongsTo != typeId &&
105         !UtdGraph::GetInstance().IsLowerLevelType(belongsTo, typeId)) {
106         typeId = "";
107     }
108     return Status::E_OK;
109 }
110 } // namespace UDMF
111 } // namespace OHOS
112