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