1 /*
2 * Copyright (c) 2021-2022 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_trace.h"
20 #include "base/log/log.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent.h"
22
23 namespace OHOS::Ace::Framework {
24
JsFunction(const JSRef<JSFunc> & jsFunction)25 JsFunction::JsFunction(const JSRef<JSFunc>& jsFunction)
26 {
27 jsFunction_ = jsFunction;
28 }
29
JsFunction(const JSRef<JSObject> & jsObject,const JSRef<JSFunc> & jsFunction)30 JsFunction::JsFunction(const JSRef<JSObject>& jsObject, const JSRef<JSFunc>& jsFunction)
31 {
32 jsThis_ = jsObject;
33 jsFunction_ = jsFunction;
34 }
35
~JsFunction()36 JsFunction::~JsFunction()
37 {
38 LOGD("Destroy: JsFunction");
39 }
40
Execute()41 void JsFunction::Execute()
42 {
43 JsFunction::ExecuteJS();
44 }
45
Execute(const JSRef<JSObject> & jsParamsObject)46 void JsFunction::Execute(const JSRef<JSObject>& jsParamsObject)
47 {
48 JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(jsParamsObject);
49 ExecuteJS(1, ¶mObj);
50 }
51
Execute(const std::vector<std::string> & keys,const std::string & param)52 void JsFunction::Execute(const std::vector<std::string>& keys, const std::string& param)
53 {
54 LOGI("param : %{private}s", param.c_str());
55 std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(param);
56 if (!argsPtr) {
57 LOGW("Parse param failed!");
58 return;
59 }
60 JSRef<JSObject> eventInfo = JSRef<JSObject>::New();
61 for (auto iter = keys.begin(); iter != keys.end(); iter++) {
62 const std::string key = *iter;
63 const auto value = argsPtr->GetValue(key);
64 if (!value) {
65 LOGI("key[%{public}s] is not exist.", key.c_str());
66 continue;
67 }
68
69 if (value->IsString()) {
70 eventInfo->SetProperty<std::string>(key.c_str(), value->GetString().c_str());
71 } else if (value->IsNumber()) {
72 eventInfo->SetProperty<double>(key.c_str(), value->GetDouble());
73 } else if (value->IsBool()) {
74 eventInfo->SetProperty<bool>(key.c_str(), value->GetBool());
75 } else if (value->IsObject()) {
76 eventInfo->SetPropertyJsonObject(key.c_str(), value->ToString().c_str());
77 }
78 }
79
80 JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(eventInfo);
81 JsFunction::ExecuteJS(1, ¶mObj);
82 }
83
ExecuteNew(const std::vector<std::string> & keys,const std::string & param)84 void JsFunction::ExecuteNew(const std::vector<std::string>& keys, const std::string& param)
85 {
86 JSRef<JSVal> jsVal;
87 if (keys.size() > 1) {
88 auto result = XComponentClient::GetInstance().GetJSVal(keys[1], jsVal);
89 RefPtr<JSXComponentController> controller =
90 XComponentClient::GetInstance().GetControllerFromJSXComponentControllersMap(keys[1]);
91 if (result && controller) {
92 controller->SetXComponentContext(jsVal);
93 }
94 }
95 JsFunction::ExecuteJS(1, &jsVal);
96 }
97
ExecuteJS(int argc,JSRef<JSVal> argv[])98 JSRef<JSVal> JsFunction::ExecuteJS(int argc, JSRef<JSVal> argv[])
99 {
100 JAVASCRIPT_EXECUTION_SCOPE_STATIC
101 ACE_FUNCTION_TRACE();
102
103 JSRef<JSVal> jsObject = jsThis_.Lock();
104 JSRef<JSVal> result = jsFunction_->Call(jsObject, argc, argv);
105 return result;
106 }
107
CreateEventTargetObject(const BaseEventInfo & info)108 JSRef<JSObject> CreateEventTargetObject(const BaseEventInfo& info)
109 {
110 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
111 JSRef<JSObject> target = objectTemplate->NewInstance();
112 JSRef<JSObject> area = objectTemplate->NewInstance();
113 JSRef<JSObject> offset = objectTemplate->NewInstance();
114 JSRef<JSObject> globalOffset = objectTemplate->NewInstance();
115 const auto& localOffset = info.GetTarget().area.GetOffset();
116 const auto& origin = info.GetTarget().origin;
117 offset->SetProperty<double>("x", localOffset.GetX().ConvertToVp());
118 offset->SetProperty<double>("y", localOffset.GetY().ConvertToVp());
119 globalOffset->SetProperty<double>("x", (origin.GetX().ConvertToVp() + localOffset.GetX().ConvertToVp()));
120 globalOffset->SetProperty<double>("y", (origin.GetY().ConvertToVp() + localOffset.GetY().ConvertToVp()));
121 area->SetPropertyObject("position", offset);
122 area->SetPropertyObject("globalPosition", globalOffset);
123 area->SetProperty<double>("width", info.GetTarget().area.GetWidth().ConvertToVp());
124 area->SetProperty<double>("height", info.GetTarget().area.GetHeight().ConvertToVp());
125 target->SetPropertyObject("area", area);
126 return target;
127 }
128
129 } // namespace OHOS::Ace::Framework
130