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 return;
51 }
52 controller_->StartAbility(args[0]->ToString());
53 }
54
PerformBackPress(const JSCallbackInfo & args)55 void JSAbilityComponentController::PerformBackPress(const JSCallbackInfo& args)
56 {
57 if (controller_) {
58 controller_->PerformBackPress();
59 }
60 }
61
GetStackCount(const JSCallbackInfo & args)62 void JSAbilityComponentController::GetStackCount(const JSCallbackInfo& args)
63 {
64 if (controller_) {
65 auto stackCount = controller_->GetStackCount();
66 auto returnValue = JSVal(ToJSValue(stackCount));
67 auto returnPtr = JSRef<JSVal>::Make(returnValue);
68 args.SetReturnValue(returnPtr);
69 }
70 }
71
72 } // namespace OHOS::Ace::Framework