• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/jsview/js_xcomponent.h"
17 
18 #include "base/memory/referenced.h"
19 #include "frameworks/bridge/declarative_frontend/engine/js_ref_ptr.h"
20 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
21 #include "frameworks/bridge/declarative_frontend/jsview/js_xcomponent_controller.h"
22 #include "frameworks/bridge/declarative_frontend/view_stack_processor.h"
23 
24 namespace OHOS::Ace::Framework {
25 RefPtr<JSXComponentController> JSXComponent::jsXComponentController_ = nullptr;
26 
JSBind(BindingTarget globalObj)27 void JSXComponent::JSBind(BindingTarget globalObj)
28 {
29     JSClass<JSXComponent>::Declare("XComponent");
30     JSClass<JSXComponent>::StaticMethod("create", &JSXComponent::Create);
31     JSClass<JSXComponent>::StaticMethod("onLoad", &JSXComponent::JsOnLoad);
32     JSClass<JSXComponent>::StaticMethod("onDestroy", &JSXComponent::JsOnDestroy);
33     JSClass<JSXComponent>::Inherit<JSViewAbstract>();
34     JSClass<JSXComponent>::Bind(globalObj);
35 }
36 
Create(const JSCallbackInfo & info)37 void JSXComponent::Create(const JSCallbackInfo& info)
38 {
39     if (info.Length() < 1 || !info[0]->IsObject()) {
40         LOGI("xcomponent create error, info is non-vaild");
41         return;
42     }
43     auto paramObject = JSRef<JSObject>::Cast(info[0]);
44     auto id = paramObject->GetProperty("id");
45 
46     auto type = paramObject->GetProperty("type");
47     auto libraryname = paramObject->GetProperty("libraryname");
48     auto xcomponentComponent = AceType::MakeRefPtr<OHOS::Ace::XComponentComponent>("xcomponent");
49 
50     if (!id->IsString()) {
51         LOGI("xcomponent create error, id is non-vaild");
52         return;
53     }
54     xcomponentComponent->SetId(id->ToString());
55     xcomponentComponent->SetXComponentType(type->ToString());
56     xcomponentComponent->SetLibraryName(libraryname->ToString());
57 
58     auto controllerObj = paramObject->GetProperty("controller");
59     if (controllerObj->IsObject()) {
60         jsXComponentController_ = JSRef<JSObject>::Cast(controllerObj)->Unwrap<JSXComponentController>();
61         if (jsXComponentController_) {
62             xcomponentComponent->SetXComponentController(jsXComponentController_->GetController());
63         }
64     }
65 
66     ViewStackProcessor::GetInstance()->Push(xcomponentComponent);
67 }
68 
JsOnLoad(const JSCallbackInfo & args)69 void JSXComponent::JsOnLoad(const JSCallbackInfo& args)
70 {
71     auto stack = ViewStackProcessor::GetInstance();
72     auto xcomponentComponent = AceType::DynamicCast<XComponentComponent>(stack->GetMainComponent());
73     if (!xcomponentComponent) {
74         LOGE("JSXComponent::JsOnLoad xcomponentComponent is null.");
75         return;
76     }
77     auto getXComponentCallback = [xcomponentComponent](RefPtr<XComponentComponent> &component) {
78         component = xcomponentComponent;
79         if (!component) {
80             LOGE("component is null");
81         }
82         return true;
83     };
84     XComponentClient::GetInstance().RegisterCallback(getXComponentCallback);
85 
86     JSRef<JSVal> jsVal;
87     XComponentClient::GetInstance().GetJSVal(jsVal);
88     args.SetReturnValue(jsVal);
89 
90     std::vector<std::string> keys = {"load"};
91     xcomponentComponent->SetXComponentInitEventId(GetEventMarker(args, keys));
92 }
93 
JsOnDestroy(const JSCallbackInfo & args)94 void JSXComponent::JsOnDestroy(const JSCallbackInfo& args)
95 {
96     auto stack = ViewStackProcessor::GetInstance();
97     auto xcomponentComponent = AceType::DynamicCast<XComponentComponent>(stack->GetMainComponent());
98     if (!xcomponentComponent) {
99         LOGE("JSXComponent::JsOnLoad xcomponentComponent is.");
100         return;
101     }
102     std::vector<std::string> keys = {"destroy"};
103     xcomponentComponent->SetXComponentDestroyEventId(GetEventMarker(args, keys));
104 }
105 
GetEventMarker(const JSCallbackInfo & info,const std::vector<std::string> & keys)106 EventMarker JSXComponent::GetEventMarker(const JSCallbackInfo& info, const std::vector<std::string>& keys)
107 {
108     if (!info[0]->IsFunction()) {
109         LOGE("info[0] is not a function.");
110         return EventMarker();
111     }
112 
113     RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
114     auto eventMarker =
115         EventMarker([execCtx = info.GetExecutionContext(), func = std::move(jsFunc), keys](const std::string& param) {
116             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
117             std::string::size_type posLoad = param.find("load");
118             // load callback method, need to return a napi instance
119             if (posLoad != std::string::npos) {
120                 ACE_SCORING_EVENT("XComponent.onLoad");
121                 func->ExecuteNew(keys, param, jsXComponentController_);
122             } else {
123                 ACE_SCORING_EVENT("XComponent.onLoad");
124                 func->Execute(keys, param);
125             }
126         });
127     return eventMarker;
128 }
129 } // namespace OHOS::Ace::Framework