• 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 "frameworks/bridge/declarative_frontend/jsview/js_view_common_def.h"
21 
22 namespace OHOS::Ace::Framework {
JSBind(BindingTarget globalObj)23 void JSXComponentController::JSBind(BindingTarget globalObj)
24 {
25     JSClass<JSXComponentController>::Declare("XComponentController");
26     JSClass<JSXComponentController>::CustomMethod("getXComponentSurfaceId", &JSXComponentController::GetSurfaceId);
27     JSClass<JSXComponentController>::CustomMethod("getXComponentContext",
28         &JSXComponentController::GetXComponentContext);
29     JSClass<JSXComponentController>::CustomMethod("setXComponentSurfaceSize",
30         &JSXComponentController::SetSurfaceConfig);
31     JSClass<JSXComponentController>::Bind(globalObj, JSXComponentController::Constructor,
32         JSXComponentController::Destructor);
33 }
34 
Constructor(const JSCallbackInfo & args)35 void JSXComponentController::Constructor(const JSCallbackInfo& args)
36 {
37     auto xcomponentController = Referenced::MakeRefPtr<JSXComponentController>();
38     xcomponentController->IncRefCount();
39     RefPtr<XComponentController> controller = AceType::MakeRefPtr<XComponentController>();
40     xcomponentController->SetController(controller);
41     args.SetReturnValue(Referenced::RawPtr(xcomponentController));
42 }
43 
Destructor(JSXComponentController * xcomponentController)44 void JSXComponentController::Destructor(JSXComponentController* xcomponentController)
45 {
46     if (xcomponentController) {
47         const auto& controller = xcomponentController->GetController();
48         xcomponentController->DecRefCount();
49         if (controller) {
50             controller->Clear();
51         }
52     }
53 }
54 
GetSurfaceId(const JSCallbackInfo & args)55 void JSXComponentController::GetSurfaceId(const JSCallbackInfo& args)
56 {
57     if (xcomponentController_) {
58         auto surfaceId = xcomponentController_->GetSurfaceId();
59         auto returnValue = JSVal(ToJSValue(surfaceId));
60         auto returnPtr = JSRef<JSVal>::Make(returnValue);
61         args.SetReturnValue(returnPtr);
62     }
63 }
64 
SetSurfaceConfig(const JSCallbackInfo & args)65 void JSXComponentController::SetSurfaceConfig(const JSCallbackInfo& args)
66 {
67     if (args.Length() < 1 || !args[0]->IsObject()) {
68         LOGW("Invalid params");
69         return;
70     }
71 
72     JSRef<JSObject> obj = JSRef<JSObject>::Cast(args[0]);
73     uint32_t surfaceWidth;
74     uint32_t surfaceHeight;
75     if (!ConvertFromJSValue(obj->GetProperty("surfaceWidth"), surfaceWidth) ||
76         !ConvertFromJSValue(obj->GetProperty("surfaceHeight"), surfaceHeight)) {
77         LOGW("Failed to parse param 'surfaceWidth' or 'surfaceHeight'");
78         return;
79     }
80 
81     if (xcomponentController_) {
82         xcomponentController_->ConfigSurface(surfaceWidth, surfaceHeight);
83     }
84 }
85 } // namespace OHOS::Ace::Framework