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 #include "cj_want_ffi.h"
17
18 #include <cstring>
19 #include <string>
20 #include <vector>
21
22 #include "cj_utils_ffi.h"
23 #include "want.h"
24 #include "want_params_wrapper.h"
25 #include "hilog_tag_wrapper.h"
26
27 using OHOS::AAFwk::Want;
28 using OHOS::AppExecFwk::ElementName;
29
30 // Attention: The function does not handle entities.
FFICJWantCreateWithWantInfo(CJWantParams params)31 WantHandle FFICJWantCreateWithWantInfo(CJWantParams params)
32 {
33 Want* want = new (std::nothrow) Want();
34 if (want == nullptr) {
35 TAG_LOGE(AAFwkTag::DEFAULT, "null want");
36 return nullptr;
37 }
38
39 auto element = reinterpret_cast<ElementName*>(params.elementName);
40 want->SetElement(*element);
41 want->SetFlags(params.flags);
42 want->SetUri(params.uri);
43 want->SetAction(params.action);
44 want->SetType(params.wantType);
45 want->SetParams(OHOS::AAFwk::WantParamWrapper::ParseWantParams(params.parameters));
46
47 return want;
48 }
49
FFICJWantDelete(WantHandle want)50 void FFICJWantDelete(WantHandle want)
51 {
52 if (want == nullptr) {
53 TAG_LOGE(AAFwkTag::DEFAULT, "null want");
54 return;
55 }
56 auto actualWant = reinterpret_cast<Want*>(want);
57 delete actualWant;
58 actualWant = nullptr;
59 }
60
FFICJWantGetWantInfo(WantHandle want)61 CJWantParams* FFICJWantGetWantInfo(WantHandle want)
62 {
63 CJWantParams* buffer = static_cast<CJWantParams*>(malloc(sizeof(CJWantParams)));
64 if (buffer == nullptr) {
65 TAG_LOGE(AAFwkTag::DEFAULT, "null buffer");
66 return nullptr;
67 }
68
69 auto actualWant = reinterpret_cast<Want*>(want);
70 auto element = actualWant->GetElement();
71 ElementNameHandle elementName = new ElementName(
72 element.GetDeviceID(), element.GetBundleName(), element.GetAbilityName(), element.GetModuleName());
73 if (elementName == nullptr) {
74 free(buffer);
75 TAG_LOGE(AAFwkTag::DEFAULT, "element name null");
76 return nullptr;
77 }
78 buffer->elementName = elementName;
79 buffer->flags = actualWant->GetFlags();
80 buffer->uri = CreateCStringFromString(actualWant->GetUriString());
81 buffer->action = CreateCStringFromString(actualWant->GetAction());
82 buffer->wantType = CreateCStringFromString(actualWant->GetType());
83 buffer->entities = const_cast<std::vector<std::string>*>(&(actualWant->GetEntities())); // reference vector<String>
84 buffer->parameters = CreateCStringFromString(OHOS::AAFwk::WantParamWrapper(actualWant->GetParams()).ToString());
85 return buffer;
86 }
87
FFICJWantParamsDelete(CJWantParams * params)88 void FFICJWantParamsDelete(CJWantParams* params)
89 {
90 if (params == nullptr) {
91 TAG_LOGE(AAFwkTag::DEFAULT, "argc null");
92 return;
93 }
94 auto actualElementName = reinterpret_cast<ElementName*>(params->elementName);
95 delete actualElementName;
96 actualElementName = nullptr;
97
98 free(static_cast<void*>(params->uri));
99 free(static_cast<void*>(params->action));
100 free(static_cast<void*>(params->wantType));
101 free(static_cast<void*>(params->parameters));
102 // Entities are reference, do not free.
103 free(static_cast<void*>(params));
104 }
105
FFICJWantAddEntity(WantHandle want,const char * entity)106 void FFICJWantAddEntity(WantHandle want, const char* entity)
107 {
108 if (want == nullptr || entity == nullptr) {
109 TAG_LOGE(AAFwkTag::DEFAULT, "Want or entity null");
110 return;
111 }
112 auto actualWant = reinterpret_cast<Want*>(want);
113 actualWant->AddEntity(entity);
114 }
115
FFICJWantParseUri(const char * uri)116 WantHandle FFICJWantParseUri(const char* uri)
117 {
118 if (uri == nullptr) {
119 TAG_LOGE(AAFwkTag::DEFAULT, "Uri null");
120 return nullptr;
121 }
122 return Want::ParseUri(uri);
123 }
124