• 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/jsview/js_xcomponent.h"
17 
18 #include "base/log/ace_scoring_log.h"
19 #include "base/memory/referenced.h"
20 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
23 #include "bridge/declarative_frontend/jsview/models/xcomponent_model_impl.h"
24 #include "core/components_ng/pattern/xcomponent/xcomponent_model.h"
25 #include "core/components_ng/pattern/xcomponent/xcomponent_model_ng.h"
26 
27 namespace OHOS::Ace {
28 
29 std::unique_ptr<XComponentModel> XComponentModel::instance_ = nullptr;
30 
GetInstance()31 XComponentModel* XComponentModel::GetInstance()
32 {
33     if (!instance_) {
34 #ifdef NG_BUILD
35         instance_.reset(new NG::XComponentModelNG());
36 #else
37         if (Container::IsCurrentUseNewPipeline()) {
38             instance_.reset(new NG::XComponentModelNG());
39         } else {
40             instance_.reset(new Framework::XComponentModelImpl());
41         }
42 #endif
43     }
44     return instance_.get();
45 }
46 
47 } // namespace OHOS::Ace
48 
49 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)50 void JSXComponent::JSBind(BindingTarget globalObj)
51 {
52     JSClass<JSXComponent>::Declare("XComponent");
53     JSClass<JSXComponent>::StaticMethod("create", &JSXComponent::Create);
54     JSClass<JSXComponent>::StaticMethod("onLoad", &JSXComponent::JsOnLoad);
55     JSClass<JSXComponent>::StaticMethod("onDestroy", &JSXComponent::JsOnDestroy);
56     JSClass<JSXComponent>::StaticMethod("onAppear", &JSXComponent::OmitEvent);
57     JSClass<JSXComponent>::StaticMethod("onDisAppear", &JSXComponent::OmitEvent);
58     JSClass<JSXComponent>::StaticMethod("onTouch", &JSXComponent::OmitEvent);
59     JSClass<JSXComponent>::StaticMethod("onClick", &JSXComponent::OmitEvent);
60     JSClass<JSXComponent>::StaticMethod("onKeyEvent", &JSXComponent::OmitEvent);
61     JSClass<JSXComponent>::StaticMethod("onMouse", &JSXComponent::OmitEvent);
62     JSClass<JSXComponent>::StaticMethod("onHover", &JSXComponent::OmitEvent);
63     JSClass<JSXComponent>::Inherit<JSViewAbstract>();
64     JSClass<JSXComponent>::Bind(globalObj);
65 }
66 
Create(const JSCallbackInfo & info)67 void JSXComponent::Create(const JSCallbackInfo& info)
68 {
69     if (info.Length() < 1 || !info[0]->IsObject()) {
70         LOGI("xcomponent create error, info is invalid");
71         return;
72     }
73     auto paramObject = JSRef<JSObject>::Cast(info[0]);
74     auto id = paramObject->GetProperty("id");
75     if (!id->IsString()) {
76         LOGI("xcomponent create error, id is invalid");
77         return;
78     }
79 
80     auto type = paramObject->GetProperty("type");
81     auto libraryname = paramObject->GetProperty("libraryname");
82     auto controllerObj = paramObject->GetProperty("controller");
83     RefPtr<XComponentController> xcomponentController = nullptr;
84     if (controllerObj->IsObject()) {
85         auto* jsXComponentController = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSXComponentController>();
86         if (jsXComponentController) {
87             XComponentClient::GetInstance().AddControllerToJSXComponentControllersMap(
88                 id->ToString(), jsXComponentController);
89             xcomponentController = jsXComponentController->GetController();
90         }
91     }
92     XComponentModel::GetInstance()->Create(
93         id->ToString(), type->ToString(), libraryname->ToString(), xcomponentController);
94 
95     if (info.Length() > 1 && info[1]->IsString()) {
96         auto soPath = info[1]->ToString();
97         XComponentModel::GetInstance()->SetSoPath(soPath);
98     }
99 }
100 
JsOnLoad(const JSCallbackInfo & args)101 void JSXComponent::JsOnLoad(const JSCallbackInfo& args)
102 {
103     if (args.Length() < 1 || !args[0]->IsFunction()) {
104         LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
105         return;
106     }
107     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
108     auto onLoad = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)](const std::string& xcomponentId) {
109         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
110         ACE_SCORING_EVENT("XComponent.onLoad");
111         std::vector<std::string> keys = { "load", xcomponentId };
112         func->ExecuteNew(keys, "");
113     };
114     XComponentModel::GetInstance()->SetOnLoad(std::move(onLoad));
115 }
116 
JsOnDestroy(const JSCallbackInfo & args)117 void JSXComponent::JsOnDestroy(const JSCallbackInfo& args)
118 {
119     if (args.Length() < 1 || !args[0]->IsFunction()) {
120         LOGE("The arg is wrong, it is supposed to have atleast 1 argument.");
121         return;
122     }
123     auto jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(args[0]));
124     auto onDestroy = [execCtx = args.GetExecutionContext(), func = std::move(jsFunc)]() {
125         JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
126         ACE_SCORING_EVENT("XComponent.onDestroy");
127         std::vector<std::string> keys = { "destroy" };
128         func->Execute(keys, "");
129     };
130     XComponentModel::GetInstance()->SetOnDestroy(std::move(onDestroy));
131 }
132 
OmitEvent(const JSCallbackInfo &)133 void JSXComponent::OmitEvent(const JSCallbackInfo& /*args*/)
134 {
135     LOGW("This event is omitted, please use apis of native_xcomponent instead");
136 }
137 
138 } // namespace OHOS::Ace::Framework