1 /*
2 * Copyright (c) 2021 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 "bridge/declarative_frontend/jsview/js_ability_component_controller.h"
17
18 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
19
20 namespace OHOS::Ace::Framework {
21
JSBind(BindingTarget globalObj)22 void JSAbilityComponentController::JSBind(BindingTarget globalObj)
23 {
24 JSClass<JSAbilityComponentController>::Declare("AbilityController");
25 JSClass<JSAbilityComponentController>::CustomMethod("startAbility", &JSAbilityComponentController::StartAbility);
26 JSClass<JSAbilityComponentController>::CustomMethod(
27 "performBackPress", &JSAbilityComponentController::PerformBackPress);
28 JSClass<JSAbilityComponentController>::CustomMethod("getStackCount", &JSAbilityComponentController::GetStackCount);
29 JSClass<JSAbilityComponentController>::Bind(
30 globalObj, JSAbilityComponentController::Constructor, JSAbilityComponentController::Destructor);
31 }
32
Constructor(const JSCallbackInfo & args)33 void JSAbilityComponentController::Constructor(const JSCallbackInfo& args)
34 {
35 auto controller = Referenced::MakeRefPtr<JSAbilityComponentController>();
36 controller->IncRefCount();
37 args.SetReturnValue(Referenced::RawPtr(controller));
38 }
39
Destructor(JSAbilityComponentController * controller)40 void JSAbilityComponentController::Destructor(JSAbilityComponentController* controller)
41 {
42 if (controller) {
43 controller->DecRefCount();
44 }
45 }
46
StartAbility(const JSCallbackInfo & args)47 void JSAbilityComponentController::StartAbility(const JSCallbackInfo& args)
48 {
49 if (!controller_ || !args[0]->IsObject()) {
50 LOGE("Start ability fail, controller is empty or params is not valid.");
51 return;
52 }
53 controller_->StartAbility(args[0]->ToString());
54 }
55
PerformBackPress(const JSCallbackInfo & args)56 void JSAbilityComponentController::PerformBackPress(const JSCallbackInfo& args)
57 {
58 if (controller_) {
59 controller_->PerformBackPress();
60 }
61 }
62
GetStackCount(const JSCallbackInfo & args)63 void JSAbilityComponentController::GetStackCount(const JSCallbackInfo& args)
64 {
65 if (controller_) {
66 auto stackCount = controller_->GetStackCount();
67 auto returnValue = JSVal(ToJSValue(stackCount));
68 auto returnPtr = JSRef<JSVal>::Make(returnValue);
69 args.SetReturnValue(returnPtr);
70 }
71 }
72
73 } // namespace OHOS::Ace::Framework