1 /*
2 * Copyright (c) 2021-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 #include "frameworks/bridge/declarative_frontend/engine/functions/js_function.h"
17
18 #include "base/json/json_util.h"
19 #include "base/log/ace_performance_monitor.h"
20 #include "base/log/ace_trace.h"
21 #include "base/log/log.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h"
23
24 namespace OHOS::Ace::Framework {
25
JsFunction(const JSRef<JSFunc> & jsFunction)26 JsFunction::JsFunction(const JSRef<JSFunc>& jsFunction)
27 {
28 jsFunction_ = jsFunction;
29 }
30
JsFunction(const JSRef<JSObject> & jsObject,const JSRef<JSFunc> & jsFunction)31 JsFunction::JsFunction(const JSRef<JSObject>& jsObject, const JSRef<JSFunc>& jsFunction)
32 {
33 jsThis_ = jsObject;
34 jsFunction_ = jsFunction;
35 }
36
JsWeakFunction(const JSRef<JSFunc> & jsFunction)37 JsWeakFunction::JsWeakFunction(const JSRef<JSFunc>& jsFunction)
38 {
39 jsWeakFunction_ = jsFunction;
40 }
41
JsWeakFunction(const JSRef<JSObject> & jsObject,const JSRef<JSFunc> & jsFunction)42 JsWeakFunction::JsWeakFunction(const JSRef<JSObject>& jsObject, const JSRef<JSFunc>& jsFunction)
43 {
44 jsThis_ = jsObject;
45 jsWeakFunction_ = jsFunction;
46 }
47
~JsFunction()48 JsFunction::~JsFunction() {}
49
Execute(const JSRef<JSObject> & jsParamsObject)50 void JsFunctionBase::Execute(const JSRef<JSObject>& jsParamsObject)
51 {
52 JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(jsParamsObject);
53 ExecuteJS(1, ¶mObj);
54 }
55
Execute(const std::vector<std::string> & keys,const std::string & param)56 void JsFunctionBase::Execute(const std::vector<std::string>& keys, const std::string& param)
57 {
58 std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(param);
59 if (!argsPtr) {
60 return;
61 }
62 JSRef<JSObject> eventInfo = JSRef<JSObject>::New();
63 for (auto iter = keys.begin(); iter != keys.end(); iter++) {
64 const std::string key = *iter;
65 const auto value = argsPtr->GetValue(key);
66 if (!value) {
67 LOGD("key[%{public}s] not exist.", key.c_str());
68 continue;
69 }
70 ExecuteInternal(value, key, eventInfo);
71 }
72
73 JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(eventInfo);
74 ExecuteJS(1, ¶mObj);
75 }
76
ExecuteInternal(const std::unique_ptr<JsonValue> & value,const std::string & key,const JSRef<JSObject> & eventInfo)77 void JsFunctionBase::ExecuteInternal(const std::unique_ptr<JsonValue>& value, const std::string& key,
78 const JSRef<JSObject>& eventInfo)
79 {
80 if (value->IsString()) {
81 eventInfo->SetProperty<std::string>(key.c_str(), value->GetString().c_str());
82 } else if (value->IsNumber()) {
83 eventInfo->SetProperty<double>(key.c_str(), value->GetDouble());
84 } else if (value->IsBool()) {
85 eventInfo->SetProperty<bool>(key.c_str(), value->GetBool());
86 } else if (value->IsObject()) {
87 eventInfo->SetPropertyJsonObject(key.c_str(), value->ToString().c_str());
88 } else if (value->IsArray()) {
89 JSRef<JSArray> valueArray = JSRef<JSArray>::New();
90 for (auto index = 0; index < value->GetArraySize(); index++) {
91 auto item = value->GetArrayItem(index);
92 if (item && item->IsString()) {
93 valueArray->SetValueAt(index, JSRef<JSVal>::Make(ToJSValue(item->GetString())));
94 }
95 if (item && item->IsNumber()) {
96 valueArray->SetValueAt(index, JSRef<JSVal>::Make(ToJSValue(item->GetInt())));
97 }
98 }
99 eventInfo->SetPropertyObject(key.c_str(), valueArray);
100 }
101 }
102
ExecuteNew(const std::vector<std::string> & keys,const std::string & param)103 void JsFunctionBase::ExecuteNew(const std::vector<std::string>& keys, const std::string& param)
104 {
105 JSRef<JSVal> jsVal;
106 if (keys.size() > 1) {
107 auto result = XComponentClient::GetInstance().GetJSVal(keys[1], jsVal);
108 RefPtr<JSXComponentController> controller =
109 XComponentClient::GetInstance().GetControllerFromJSXComponentControllersMap(keys[1]);
110 if (result && controller) {
111 controller->SetXComponentContext(jsVal);
112 }
113 }
114 ExecuteJS(1, &jsVal);
115 }
116
ExecuteJS(int argc,JSRef<JSVal> argv[])117 JSRef<JSVal> JsWeakFunction::ExecuteJS(int argc, JSRef<JSVal> argv[])
118 {
119 JS_CALLBACK_DURATION();
120 JAVASCRIPT_EXECUTION_SCOPE_STATIC
121 ACE_FUNCTION_TRACE();
122 JSRef<JSVal> jsObject = jsThis_.Lock();
123 auto jsFunction = jsWeakFunction_.Lock();
124 if (jsFunction.IsEmpty()) {
125 LOGW("js function is null.");
126 return {};
127 }
128 JSRef<JSVal> result = jsFunction->Call(jsObject, argc, argv);
129 return result;
130 }
131
ExecuteJS(int argc,JSRef<JSVal> argv[])132 JSRef<JSVal> JsFunction::ExecuteJS(int argc, JSRef<JSVal> argv[])
133 {
134 JS_CALLBACK_DURATION();
135 JAVASCRIPT_EXECUTION_SCOPE_STATIC
136 ACE_FUNCTION_TRACE();
137
138 JSRef<JSVal> jsObject = jsThis_.Lock();
139 JSRef<JSVal> result = jsFunction_->Call(jsObject, argc, argv);
140 return result;
141 }
142
CreateEventTargetObject(const BaseEventInfo & info)143 JSRef<JSObject> CreateEventTargetObject(const BaseEventInfo& info)
144 {
145 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
146 JSRef<JSObject> target = objectTemplate->NewInstance();
147 JSRef<JSObject> area = objectTemplate->NewInstance();
148 JSRef<JSObject> offset = objectTemplate->NewInstance();
149 JSRef<JSObject> globalOffset = objectTemplate->NewInstance();
150 const auto& localOffset = info.GetTarget().area.GetOffset();
151 const auto& origin = info.GetTarget().origin;
152 offset->SetProperty<double>("x", localOffset.GetX().ConvertToVp());
153 offset->SetProperty<double>("y", localOffset.GetY().ConvertToVp());
154 globalOffset->SetProperty<double>("x", (origin.GetX().ConvertToVp() + localOffset.GetX().ConvertToVp()));
155 globalOffset->SetProperty<double>("y", (origin.GetY().ConvertToVp() + localOffset.GetY().ConvertToVp()));
156 area->SetPropertyObject("position", offset);
157 area->SetPropertyObject("globalPosition", globalOffset);
158 area->SetProperty<double>("width", info.GetTarget().area.GetWidth().ConvertToVp());
159 area->SetProperty<double>("height", info.GetTarget().area.GetHeight().ConvertToVp());
160 target->SetPropertyObject("area", area);
161 return target;
162 }
163
164 } // namespace OHOS::Ace::Framework
165