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 std::vector<std::string> & keys,const std::string & param)46 void JsFunction::Execute(const std::vector<std::string>& keys, const std::string& param)
47 {
48 LOGI("param : %{private}s", param.c_str());
49 std::unique_ptr<JsonValue> argsPtr = JsonUtil::ParseJsonString(param);
50 if (!argsPtr) {
51 LOGW("Parse param failed!");
52 return;
53 }
54 JSRef<JSObject> eventInfo = JSRef<JSObject>::New();
55 for (auto iter = keys.begin(); iter != keys.end(); iter++) {
56 const std::string key = *iter;
57 const auto value = argsPtr->GetValue(key);
58 if (!value) {
59 LOGI("key[%{public}s] is not exist.", key.c_str());
60 continue;
61 }
62
63 if (value->IsString()) {
64 eventInfo->SetProperty<std::string>(key.c_str(), value->GetString().c_str());
65 } else if (value->IsNumber()) {
66 eventInfo->SetProperty<double>(key.c_str(), value->GetDouble());
67 } else if (value->IsBool()) {
68 eventInfo->SetProperty<bool>(key.c_str(), value->GetBool());
69 } else if (value->IsObject()) {
70 eventInfo->SetPropertyJsonObject(key.c_str(), value->ToString().c_str());
71 }
72 }
73
74 JSRef<JSVal> paramObj = JSRef<JSVal>::Cast(eventInfo);
75 JsFunction::ExecuteJS(1, ¶mObj);
76 }
77
78 #if defined(XCOMPONENT_SUPPORTED)
ExecuteNew(const std::vector<std::string> & keys,const std::string & param,RefPtr<JSXComponentController> & jsXComponentController)79 void JsFunction::ExecuteNew(const std::vector<std::string>& keys, const std::string& param,
80 RefPtr<JSXComponentController>& jsXComponentController)
81 {
82 JSRef<JSVal> jsVal;
83 XComponentClient::GetInstance().GetJSVal(jsVal);
84 if (jsXComponentController) {
85 jsXComponentController->SetXComponentContext(jsVal);
86 }
87 JsFunction::ExecuteJS(1, &jsVal);
88 }
89 #endif
90
ExecuteJS(int argc,JSRef<JSVal> argv[])91 JSRef<JSVal> JsFunction::ExecuteJS(int argc, JSRef<JSVal> argv[])
92 {
93 ACE_FUNCTION_TRACE();
94
95 JSRef<JSVal> jsObject = jsThis_.Lock();
96 JSRef<JSVal> result = jsFunction_->Call(jsObject, argc, argv);
97 return result;
98 }
99
CreateEventTargetObject(const BaseEventInfo & info)100 JSRef<JSObject> CreateEventTargetObject(const BaseEventInfo& info)
101 {
102 JSRef<JSObjTemplate> objectTemplate = JSRef<JSObjTemplate>::New();
103 JSRef<JSObject> target = objectTemplate->NewInstance();
104 JSRef<JSObject> area = objectTemplate->NewInstance();
105 JSRef<JSObject> offset = objectTemplate->NewInstance();
106 JSRef<JSObject> globalOffset = objectTemplate->NewInstance();
107 const auto& localOffset = info.GetTarget().area.GetOffset();
108 const auto& origin = info.GetTarget().origin;
109 offset->SetProperty<double>("x", localOffset.GetX().ConvertToVp());
110 offset->SetProperty<double>("y", localOffset.GetY().ConvertToVp());
111 globalOffset->SetProperty<double>("x", (origin.GetX().ConvertToVp() + localOffset.GetX().ConvertToVp()));
112 globalOffset->SetProperty<double>("y", (origin.GetY().ConvertToVp() + localOffset.GetY().ConvertToVp()));
113 area->SetPropertyObject("position", offset);
114 area->SetPropertyObject("globalPosition", globalOffset);
115 area->SetProperty<double>("width", info.GetTarget().area.GetWidth().ConvertToVp());
116 area->SetProperty<double>("height", info.GetTarget().area.GetHeight().ConvertToVp());
117 target->SetPropertyObject("area", area);
118 return target;
119 }
120
121 } // namespace OHOS::Ace::Framework
122