• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
1 /*
2  * Copyright (c) 2024 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_form_menu_item.h"
17 
18 #if !defined(PREVIEW) && defined(OHOS_PLATFORM)
19 #include "interfaces/inner_api/ui_session/ui_session_manager.h"
20 #endif
21 
22 #include "want.h"
23 
24 #include "base/log/ace_scoring_log.h"
25 #include "base/log/log_wrapper.h"
26 #include "bridge/declarative_frontend/engine/functions/js_click_function.h"
27 #include "bridge/declarative_frontend/engine/js_types.h"
28 #include "bridge/declarative_frontend/jsview/js_utils.h"
29 #include "bridge/declarative_frontend/jsview/models/form_model_impl.h"
30 #include "bridge/declarative_frontend/jsview/models/menu_item_model_impl.h"
31 #include "bridge/declarative_frontend/jsview/js_menu_item.h"
32 #include "bridge/declarative_frontend/view_stack_processor.h"
33 #include "core/components_ng/base/view_abstract.h"
34 #include "core/components_ng/base/view_abstract_model.h"
35 #include "core/components_ng/pattern/form/form_model_ng.h"
36 #include "core/components_ng/pattern/menu/menu_item/menu_item_model.h"
37 #include "core/components_ng/pattern/menu/menu_item/menu_item_model_ng.h"
38 
39 namespace OHOS::Ace::Framework {
40 namespace {
41 constexpr int NUM_WANT_1 = 0;
42 constexpr int NUM_DATA_2 = 1;
43 constexpr int NUM_FUN_3 = 2;
44 constexpr int NUM_CALLBACKNUM = 2;
45 }
46 
JSBind(BindingTarget globalObj)47 void JSFormMenuItem::JSBind(BindingTarget globalObj)
48 {
49     JSClass<JSFormMenuItem>::Declare("FormMenuItem");
50     MethodOptions opt = MethodOptions::NONE;
51     JSClass<JSFormMenuItem>::StaticMethod("create", &JSMenuItem::Create, opt);
52     JSClass<JSFormMenuItem>::StaticMethod("onClick", &JSInteractableView::JsOnClick);
53     JSClass<JSFormMenuItem>::StaticMethod("onRequestPublishFormWithSnapshot",
54         &JSFormMenuItem::JsOnRequestPublishFormWithSnapshot);
55     JSClass<JSFormMenuItem>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
56     JSClass<JSFormMenuItem>::InheritAndBind<JSViewAbstract>(globalObj);
57 }
58 
RequestPublishFormWithSnapshot(JSRef<JSVal> wantValue,const std::string & formBindingDataStr,RefPtr<JsFunction> jsCBFunc)59 void JSFormMenuItem::RequestPublishFormWithSnapshot(JSRef<JSVal> wantValue,
60     const std::string& formBindingDataStr, RefPtr<JsFunction> jsCBFunc)
61 {
62     RefPtr<WantWrap> wantWrap = CreateWantWrapFromNapiValue(wantValue);
63     if (!wantWrap) {
64         TAG_LOGE(AceLogTag::ACE_FORM, "onTap wantWrap is NULL.");
65         return;
66     }
67 
68     int64_t formId = 0;
69     AAFwk::Want& want = const_cast<AAFwk::Want&>(wantWrap->GetWant());
70     if (!want.HasParameter("ohos.extra.param.key.add_form_to_host_snapshot") ||
71         !want.HasParameter("ohos.extra.param.key.add_form_to_host_width") ||
72         !want.HasParameter("ohos.extra.param.key.add_form_to_host_height") ||
73         !want.HasParameter("ohos.extra.param.key.add_form_to_host_screenx") ||
74         !want.HasParameter("ohos.extra.param.key.add_form_to_host_screeny")) {
75         TAG_LOGE(AceLogTag::ACE_FORM, "want has no component snapshot info");
76         return;
77     }
78 
79     std::string errMsg;
80     int32_t errCode = FormModel::GetInstance()->RequestPublishFormWithSnapshot(want, formBindingDataStr, formId,
81                                                                                errMsg);
82     if (!jsCBFunc) {
83         TAG_LOGE(AceLogTag::ACE_FORM, "jsCBFunc is null");
84         return;
85     }
86 
87     JSRef<JSVal> params[NUM_CALLBACKNUM];
88     JSRef<JSObject> errObj = JSRef<JSObject>::New();
89     errObj->SetProperty<int32_t>("code", errCode);
90     errObj->SetProperty<std::string>("message", errMsg);
91     params[0] = errObj;
92     params[1] = JSRef<JSVal>::Make(ToJSValue(std::to_string(formId)));
93     jsCBFunc->ExecuteJS(NUM_CALLBACKNUM, params);
94 }
95 
JsOnRequestPublishFormWithSnapshot(const JSCallbackInfo & info)96 void JSFormMenuItem::JsOnRequestPublishFormWithSnapshot(const JSCallbackInfo& info)
97 {
98     bool retFlag;
99     OnClickParameterCheck(info, retFlag);
100     if (retFlag) {
101         return;
102     }
103 
104     auto want = info[NUM_WANT_1];
105     JSRef<JSVal> wantValue = JSRef<JSVal>::Cast(want);
106     if (wantValue->IsNull()) {
107         TAG_LOGE(AceLogTag::ACE_FORM, "JsOnClick wantValue is null");
108         return;
109     }
110 
111     std::string formBindingDataStr;
112     JSViewAbstract::ParseJsString(info[NUM_DATA_2], formBindingDataStr);
113     if (formBindingDataStr.empty()) {
114         TAG_LOGW(AceLogTag::ACE_FORM, "JsOnClick formBindingDataStr is empty");
115     }
116 
117     RefPtr<JsFunction> jsCallBackFunc = nullptr;
118     if (!info[NUM_FUN_3]->IsUndefined() && info[NUM_FUN_3]->IsFunction()) {
119         jsCallBackFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[NUM_FUN_3]));
120     }
121 
122     RequestPublishFormWithSnapshot(wantValue, formBindingDataStr, jsCallBackFunc);
123 }
124 
OnClickParameterCheck(const JSCallbackInfo & info,bool & retFlag)125 void JSFormMenuItem::OnClickParameterCheck(const JSCallbackInfo& info, bool& retFlag)
126 {
127     retFlag = true;
128 
129     if (info[NUM_WANT_1]->IsUndefined() || !info[NUM_WANT_1]->IsObject()) {
130         TAG_LOGE(AceLogTag::ACE_FORM, "OnClickParameterCheck bad parameter info[1]");
131         return;
132     }
133     retFlag = false;
134 }
135 } // namespace OHOS::Ace::Framework
136