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 #ifndef UTD_CLIENT_H
17 #define UTD_CLIENT_H
18 
19 #include <shared_mutex>
20 #include <string>
21 #include <vector>
22 #include <map>
23 
24 #include "error_code.h"
25 #include "flexible_type.h"
26 #include "preset_type_descriptors.h"
27 #include "preset_type_descriptors.h"
28 #include "type_descriptor.h"
29 #include "utd_common.h"
30 #include "visibility.h"
31 namespace OHOS {
32 namespace UDMF {
33 class TypeDescriptor;
34 class UtdChangeSubscriber;
35 class API_EXPORT UtdClient {
36 public:
37     static UtdClient &GetInstance();
38     Status GetTypeDescriptor(const std::string &typeId, std::shared_ptr<TypeDescriptor> &descriptor);
39     Status GetUniformDataTypeByFilenameExtension(const std::string &fileExtension, std::string &typeId,
40                                                  std::string belongsTo = DEFAULT_TYPE_ID);
41     Status GetUniformDataTypesByFilenameExtension(const std::string &fileExtension,
42                     std::vector<std::string> &typeIds, const std::string &belongsTo = DEFAULT_TYPE_ID);
43     Status GetUniformDataTypeByMIMEType(const std::string &mimeType, std::string &typeId,
44                                         std::string belongsTo = DEFAULT_TYPE_ID);
45     Status GetUniformDataTypesByMIMEType(const std::string &mimeType, std::vector<std::string> &typeIds,
46                                         const std::string &belongsTo = DEFAULT_TYPE_ID);
47     Status IsUtd(std::string typeId, bool &result);
48 
49 private:
50     UtdClient();
51     ~UtdClient();
52     UtdClient(const UtdClient &obj) = delete;
53     UtdClient &operator=(const UtdClient &obj) = delete;
54     void Init();
55     bool IsHapTokenType();
56     std::string GetCustomUtdPath();
57     Status GetCurrentActiveUserId(int32_t& userId);
58     bool IsValidFileExtension(const std::string &fileExtension);
59     bool IsValidMimeType(const std::string &mimeType);
60     Status GetFlexibleTypeDescriptor(const std::string &typeId, std::shared_ptr<TypeDescriptor> &descriptor);
61     std::string GetTypeIdFromCfg(const std::string &mimeType);
62     std::vector<std::string> GetTypeIdsFromCfg(const std::string &mimeType);
63     void SubscribeUtdChange();
64 
65     std::vector<TypeDescriptorCfg> descriptorCfgs_;
66     std::shared_ptr<UtdChangeSubscriber> subscriber_;
67     std::shared_mutex utdMutex_;
68 };
69 } // namespace UDMF
70 } // namespace OHOS
71 #endif // UTD_CLIENT_H