• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 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_controller.h"
17 
18 #include "base/utils/linear_map.h"
19 #include "base/utils/utils.h"
20 #include "core/components/xcomponent/xcomponent_controller_impl.h"
21 #include "core/components_ng/pattern/xcomponent/xcomponent_controller_ng.h"
22 #include "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
23 
24 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)25 void JSXComponentController::JSBind(BindingTarget globalObj)
26 {
27     JSClass<JSXComponentController>::Declare("XComponentController");
28     JSClass<JSXComponentController>::CustomMethod("getXComponentSurfaceId", &JSXComponentController::GetSurfaceId);
29     JSClass<JSXComponentController>::CustomMethod(
30         "getXComponentContext", &JSXComponentController::GetXComponentContext);
31     JSClass<JSXComponentController>::CustomMethod(
32         "setXComponentSurfaceSize", &JSXComponentController::SetSurfaceConfig);
33     JSClass<JSXComponentController>::Bind(
34         globalObj, JSXComponentController::Constructor, JSXComponentController::Destructor);
35 }
36 
Constructor(const JSCallbackInfo & args)37 void JSXComponentController::Constructor(const JSCallbackInfo& args)
38 {
39     auto xcomponentController = Referenced::MakeRefPtr<JSXComponentController>();
40     xcomponentController->IncRefCount();
41     std::shared_ptr<InnerXComponentController> controller;
42 #ifdef NG_BUILD
43     controller = std::make_shared<NG::XComponentControllerNG>();
44 #else
45     if (Container::IsCurrentUseNewPipeline()) {
46         controller = std::make_shared<NG::XComponentControllerNG>();
47     } else {
48         controller = std::make_shared<XComponentControllerImpl>();
49     }
50 #endif
51     xcomponentController->SetController(controller);
52     args.SetReturnValue(Referenced::RawPtr(xcomponentController));
53 }
54 
Destructor(JSXComponentController * xcomponentController)55 void JSXComponentController::Destructor(JSXComponentController* xcomponentController)
56 {
57     if (xcomponentController) {
58         xcomponentController->DecRefCount();
59     }
60 }
61 
GetSurfaceId(const JSCallbackInfo & args)62 void JSXComponentController::GetSurfaceId(const JSCallbackInfo& args)
63 {
64     if (xcomponentController_) {
65         auto surfaceId = xcomponentController_->GetSurfaceId();
66         auto returnValue = JSVal(ToJSValue(surfaceId));
67         auto returnPtr = JSRef<JSVal>::Make(returnValue);
68         args.SetReturnValue(returnPtr);
69     }
70 }
71 
SetSurfaceConfig(const JSCallbackInfo & args)72 void JSXComponentController::SetSurfaceConfig(const JSCallbackInfo& args)
73 {
74     if (args.Length() < 1 || !args[0]->IsObject()) {
75         LOGW("Invalid params");
76         return;
77     }
78 
79     JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
80     uint32_t surfaceWidth;
81     uint32_t surfaceHeight;
82     if (!ConvertFromJSValue(obj->GetProperty("surfaceWidth"), surfaceWidth) ||
83         !ConvertFromJSValue(obj->GetProperty("surfaceHeight"), surfaceHeight)) {
84         LOGW("Failed to parse param 'surfaceWidth' or 'surfaceHeight'");
85         return;
86     }
87 
88     if (xcomponentController_) {
89         xcomponentController_->ConfigSurface(surfaceWidth, surfaceHeight);
90     }
91 }
92 } // namespace OHOS::Ace::Framework