1 /*
2  * Copyright (c) 2020-2021 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 "component_utils.h"
17 #include "ace_log.h"
18 #include "component.h"
19 #include "descriptor_utils.h"
20 
21 namespace OHOS {
22 namespace ACELite {
GetViewFromBindingObject(jerry_value_t value)23 UIView* ComponentUtils::GetViewFromBindingObject(jerry_value_t value)
24 {
25     Component* component = GetComponentFromBindingObject(value);
26     return component ? component->GetComponentRootView() : nullptr;
27 }
28 
GetComponentFromBindingObject(jerry_value_t value)29 Component* ComponentUtils::GetComponentFromBindingObject(jerry_value_t value)
30 {
31     if (IS_UNDEFINED(value)) {
32         return nullptr;
33     }
34 
35     Component* component = nullptr;
36     bool isBinded = jerry_get_object_native_pointer(value, reinterpret_cast<void**>(&component), nullptr);
37     if (!isBinded || (component == nullptr)) {
38         return nullptr;
39     }
40     return component;
41 }
42 
ReleaseComponents(Component * rootComponent)43 void ComponentUtils::ReleaseComponents(Component *rootComponent)
44 {
45     if (rootComponent == nullptr) {
46         return;
47     }
48     // for list component, release all the list items first
49     if (rootComponent->GetComponentName() != K_LIST) {
50         JSValue descriptors = rootComponent->GetDescriptors();
51         if (!JSUndefined::Is(descriptors)) {
52             DescriptorUtils::ReleaseDescriptorOrElements(descriptors);
53         }
54     }
55     Component *child = const_cast<Component *>(rootComponent->GetChildHead());
56     if (child == nullptr) {
57         // has no children, can be released directly
58         rootComponent->Release();
59         delete rootComponent;
60         return;
61     }
62     while (child != nullptr) {
63         ReleaseComponents(child);
64         child = const_cast<Component *>(child->GetNextSibling());
65     }
66 }
67 
GetComponentType(jerry_value_t options)68 uint16_t ComponentUtils::GetComponentType(jerry_value_t options)
69 {
70     if (IS_UNDEFINED(options)) {
71         return 0;
72     }
73     jerry_value_t attrsPropValue = jerryx_get_property_str(options, ATTR_ATTRS);
74     // create component according to attribute type
75     const char * const attrType = "type";
76     jerry_value_t typeValHandler = jerryx_get_property_str(attrsPropValue, attrType);
77     uint16_t typeId = 0;
78     if (!jerry_value_is_undefined(typeValHandler)) {
79         uint16_t len = 0;
80         char *type = MallocStringOf(typeValHandler, &len);
81         typeId = KeyParser::ParseKeyId(type, len);
82         ACE_FREE(type);
83     }
84     ReleaseJerryValue(typeValHandler, attrsPropValue, VA_ARG_END_FLAG);
85     return typeId;
86 }
87 } // namespace ACELite
88 } // namespace OHOS
89