• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 
2 /*
3  * Copyright (c) 2024 Huawei Device Co., Ltd.
4  * Licensed under the Apache License, Version 2.0 (the "License");
5  * you may not use this file except in compliance with the License.
6  * You may obtain a copy of the License at
7  *
8  * http://www.apache.org/licenses/LICENSE-2.0
9  *
10  * Unless required by applicable law or agreed to in writing, software
11  * distributed under the License is distributed on an "AS IS" BASIS,
12  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13  * See the License for the specific language governing permissions and
14  * limitations under the License.
15  */
16 
17 #include "securec.h"
18 
19 #include "unified_data_ffi.h"
20 #include "unified_data_impl.h"
21 #include "unified_record_impl.h"
22 #include "unified_data_parameter_parse.h"
23 #include "cj_common_ffi.h"
24 
25 #include "utils.h"
26 
27 using namespace OHOS::FFI;
28 using namespace OHOS::UDMF;
29 
30 namespace OHOS {
31 namespace UDMF {
32 extern "C" {
33 
FfiUDMFUnifiedDataConstructor()34 int64_t FfiUDMFUnifiedDataConstructor()
35 {
36     auto nativeCJUnifiedData = FFIData::Create<CUnifiedData>();
37     if (nativeCJUnifiedData == nullptr) {
38         return -1;
39     }
40     return nativeCJUnifiedData->GetID();
41 }
42 
FfiUDMFUnifiedDataConstructorWithRecord(int64_t unifiedRecordId)43 int64_t FfiUDMFUnifiedDataConstructorWithRecord(int64_t unifiedRecordId)
44 {
45     auto record = FFIData::GetData<CUnifiedRecord>(unifiedRecordId);
46     if (record == nullptr) {
47         return -1;
48     }
49 
50     auto nativeCJUnifiedData = FFIData::Create<CUnifiedData>(record);
51     if (nativeCJUnifiedData == nullptr) {
52         return -1;
53     }
54     return nativeCJUnifiedData->GetID();
55 }
56 
FfiUDMFUnifiedDataAddRecord(int64_t unifiedDataId,int64_t unifiedRecordId)57 void FfiUDMFUnifiedDataAddRecord(int64_t unifiedDataId, int64_t unifiedRecordId)
58 {
59     auto record = FFIData::GetData<CUnifiedRecord>(unifiedRecordId);
60     if (record == nullptr) {
61         return;
62     }
63 
64     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
65     if (data == nullptr) {
66         return;
67     }
68 
69     data->AddRecord(record);
70 }
71 
FfiUDMFUnifiedDataGetRecords(int64_t unifiedDataId)72 CArrUnifiedRecord FfiUDMFUnifiedDataGetRecords(int64_t unifiedDataId)
73 {
74     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
75     if (data == nullptr) {
76         return CArrUnifiedRecord {};
77     }
78 
79     return data->GetRecords();
80 }
81 
FfiUDMFUnifiedDataHasType(int64_t unifiedDataId,const char * type)82 bool FfiUDMFUnifiedDataHasType(int64_t unifiedDataId, const char *type)
83 {
84     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
85     if (data == nullptr) {
86         return false;
87     }
88 
89     return data->HasType(type);
90 }
91 
FfiUDMFUnifiedDataGetTypes(int64_t unifiedDataId)92 CArrString FfiUDMFUnifiedDataGetTypes(int64_t unifiedDataId)
93 {
94     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
95     if (data == nullptr) {
96         return CArrString {};
97     }
98 
99     return data->GetTypes();
100 }
101 
FfiUDMFGetProperties(int64_t unifiedDataId)102 CUnifiedDataProperties FfiUDMFGetProperties(int64_t unifiedDataId)
103 {
104     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
105     CUnifiedDataProperties ret = { .extras = {}, .tag = nullptr, .timestamp = 0, .shareOptions = 1 };
106     if (data == nullptr) {
107         return ret;
108     }
109     std::shared_ptr<UDMF::UnifiedDataProperties> uni = data->unifiedData_->GetProperties();
110     int32_t code = NO_ERROR;
111     ParseParameters(uni->extras, ret.extras, code);
112     if (code != NO_ERROR) {
113         return ret;
114     }
115     ret.tag = Utils::MallocCString(uni->tag);
116     ret.timestamp = uni->timestamp;
117     // In arkts initial enum value is not public ,so use public initial value CROSS_APP.
118     if (uni->shareOptions == IN_APP || uni->shareOptions == CROSS_APP) {
119         ret.shareOptions = uni->shareOptions;
120     }
121     return ret;
122 }
123 
FfiUDMFSetProperties(int64_t unifiedDataId,CUnifiedDataProperties properties)124 void FfiUDMFSetProperties(int64_t unifiedDataId, CUnifiedDataProperties properties)
125 {
126     auto data = FFIData::GetData<CUnifiedData>(unifiedDataId);
127     if (data == nullptr) {
128         return;
129     }
130     auto prop = data->unifiedData_->GetProperties();
131     AAFwk::WantParams wantP;
132     SetDataParameters(properties.extras, wantP);
133     prop->extras = wantP;
134     prop->tag = Utils::MallocCString(properties.tag);
135     prop->shareOptions = UDMF::ShareOptions(properties.shareOptions);
136     data->unifiedData_->SetProperties(prop);
137 }
138 
FfiUDMFFreeDataProperties(CUnifiedDataProperties * properties)139 FFI_EXPORT void FfiUDMFFreeDataProperties(CUnifiedDataProperties *properties)
140 {
141     CParameters *p = properties->extras.head;
142     for (int64_t i = 0; i < properties->extras.size; i++) {
143         free(p[i].key);
144         free(p[i].value);
145         p[i].key = nullptr;
146         p[i].value = nullptr;
147     }
148     free(p);
149     free(properties->tag);
150     p = nullptr;
151     properties->tag = nullptr;
152 }
153 
FfiUDMFFreeCArrString(CArrString * arrStr)154 FFI_EXPORT void FfiUDMFFreeCArrString(CArrString *arrStr)
155 {
156     Utils::FreeCArrString(*arrStr);
157 }
158 }
159 } // namespace UDMF
160 } // namespace OHOS
161