1 /*
2  * Copyright (c) 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 #ifndef UDMF_CAPI_COMMON_H
17 #define UDMF_CAPI_COMMON_H
18 
19 #include "unified_record.h"
20 #include "unified_data.h"
21 #include <mutex>
22 #include <cstdint>
23 
24 # define MAX_GENERAL_ENTRY_SIZE (100 * 1024 * 1024)
25 
26 struct UdsObject {
27     const int64_t cid;
28     std::shared_ptr<OHOS::UDMF::Object> obj;
29     std::mutex mutex;
30     explicit UdsObject(int cid);
31     template<typename T> bool HasObjectKey(const char* paramName);
32     template<typename T> T* GetUdsValue(const char* paramName);
33     template<typename T> int SetUdsValue(const char* paramName, const T pramValue);
34 };
35 
36 enum NdkStructId : std::int64_t {
37     UTD_STRUCT_ID = 1002930,
38     UDS_PLAIN_TEXT_STRUCT_ID,
39     UDS_HYPERLINK_STRUCT_ID,
40     UDS_HTML_STRUCT_ID,
41     UDS_APP_ITEM_STRUCT_ID,
42     UDMF_UNIFIED_DATA_STRUCT_ID,
43     UDMF_UNIFIED_RECORD_STRUCT_ID,
44     UDMF_UNIFIED_DATA_PROPERTIES_ID,
45     UDS_FILE_URI_STRUCT_ID,
46     UDS_PIXEL_MAP_STRUCT_ID,
47     UDS_ARRAY_BUFFER_STRUCT_ID,
48     UDS_CONTENT_FORM_STRUCT_ID
49 };
50 
51 struct OH_Utd {
52     const int64_t cid = UTD_STRUCT_ID;
53     std::mutex mutex;
54     std::string typeId;
55     const char** belongingToTypes{nullptr};
56     unsigned int belongingToTypesCount{0};
57     const char** filenameExtensions{nullptr};
58     unsigned int filenameExtensionsCount{0};
59     const char** mimeTypes{nullptr};
60     unsigned int mimeTypeCount{0};
61     std::string description;
62     std::string referenceURL;
63     std::string iconFile;
64 };
65 
66 struct OH_UdsPlainText : public UdsObject {
67     OH_UdsPlainText();
68 };
69 struct OH_UdsHyperlink : public UdsObject {
70     OH_UdsHyperlink();
71 };
72 struct OH_UdsHtml : public UdsObject {
73     OH_UdsHtml();
74 };
75 struct OH_UdsAppItem : public UdsObject {
76     OH_UdsAppItem();
77 };
78 struct OH_UdsFileUri : public UdsObject {
79     OH_UdsFileUri();
80 };
81 struct OH_UdsPixelMap : public UdsObject {
82     OH_UdsPixelMap();
83 };
84 struct OH_UdsArrayBuffer : public UdsObject {
85     OH_UdsArrayBuffer();
86 };
87 struct OH_UdsContentForm : public UdsObject {
88     OH_UdsContentForm();
89 };
90 
91 struct OH_UdmfRecord {
92     const int64_t cid = UDMF_UNIFIED_RECORD_STRUCT_ID;
93     std::shared_ptr<OHOS::UDMF::UnifiedRecord> record_;
94     unsigned char *recordData{nullptr};
95     unsigned int recordDataLen{0};
96     char **typesArray{nullptr};
97     unsigned int typesCount{0};
98     char *lastType{nullptr};
99     std::mutex mutex;
100 };
101 
102 struct OH_UdmfData {
103     const int64_t cid = UDMF_UNIFIED_DATA_STRUCT_ID;
104     std::shared_ptr<OHOS::UDMF::UnifiedData> unifiedData_;
105     char **typesArray{nullptr};
106     unsigned int typesCount{0};
107     OH_UdmfRecord **records{nullptr};
108     unsigned int recordsCount{0};
109     std::mutex mutex;
110 };
111 
112 struct OH_UdmfProperty {
113     const int64_t cid = UDMF_UNIFIED_DATA_PROPERTIES_ID;
114     std::shared_ptr<OHOS::UDMF::UnifiedDataProperties> properties_;
115     std::mutex mutex;
116     std::string extraStr;
117 };
118 
119 bool IsInvalidUdsObjectPtr(const UdsObject* pThis, int cid);
120 
121 bool IsInvalidUdsObjectByType(const UdsObject* pThis, const OHOS::UDMF::UDType& type);
122 
123 #endif
124