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 "bridge/declarative_frontend/jsview/js_richtext.h"
17
18 #include <string>
19
20 #include "bridge/declarative_frontend/engine/functions/js_function.h"
21 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
22 #include "bridge/declarative_frontend/view_stack_processor.h"
23 #include "core/common/container.h"
24 #include "core/components/web/web_component.h"
25 #include "core/components/web/web_property.h"
26 #include "core/components_ng/pattern/web/web_view.h"
27
28 namespace OHOS::Ace::Framework {
29
Create(const JSCallbackInfo & info)30 void JSRichText::Create(const JSCallbackInfo& info)
31 {
32 if (info.Length() < 1) {
33 LOGI("richtext create error, info is non-valid");
34 return;
35 }
36
37 std::string data;
38
39 if (Container::IsCurrentUseNewPipeline()) {
40 if (ParseJsString(info[0], data)) {
41 RefPtr<WebController> webController;
42 NG::WebView::Create(data);
43 }
44 return;
45 }
46
47 RefPtr<WebComponent> webComponent;
48 if (ParseJsString(info[0], data)) {
49 webComponent = AceType::MakeRefPtr<OHOS::Ace::WebComponent>("");
50 webComponent->SetData(data);
51 } else {
52 LOGE("richtext component failed to parse data");
53 return;
54 }
55 ViewStackProcessor::GetInstance()->Push(webComponent);
56 }
57
JSBind(BindingTarget globalObj)58 void JSRichText::JSBind(BindingTarget globalObj)
59 {
60 JSClass<JSRichText>::Declare("RichText");
61 JSClass<JSRichText>::StaticMethod("create", &JSRichText::Create);
62 JSClass<JSRichText>::StaticMethod("onStart", &JSRichText::OnStart);
63 JSClass<JSRichText>::StaticMethod("onComplete", &JSRichText::OnComplete);
64 JSClass<JSRichText>::Inherit<JSViewAbstract>();
65 JSClass<JSRichText>::Bind<>(globalObj);
66 }
67
OnStart(const JSCallbackInfo & info)68 void JSRichText::OnStart(const JSCallbackInfo& info)
69 {
70 if (info.Length() > 0 && info[0]->IsFunction()) {
71 if (Container::IsCurrentUseNewPipeline()) {
72 RefPtr<JsFunction> jsFunc =
73 AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
74 auto onStart = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
75 const std::shared_ptr<BaseEventInfo>& info) {
76 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
77 func->Execute();
78 };
79 NG::WebView::SetOnPageStart(onStart);
80 return;
81 }
82 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
83 auto eventMarker =
84 EventMarker([execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
85 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
86 func->Execute();
87 });
88 auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
89 webComponent->SetPageStartedEventId(eventMarker);
90 }
91 }
92
OnComplete(const JSCallbackInfo & info)93 void JSRichText::OnComplete(const JSCallbackInfo& info)
94 {
95 if (info.Length() > 0 && info[0]->IsFunction()) {
96 if (Container::IsCurrentUseNewPipeline()) {
97 RefPtr<JsFunction> jsFunc =
98 AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
99 auto onComplete = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](
100 const std::shared_ptr<BaseEventInfo>& info) {
101 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
102 func->Execute();
103 };
104 NG::WebView::SetOnPageFinish(onComplete);
105 return;
106 }
107 RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
108 auto eventMarker =
109 EventMarker([execCtx = info.GetExecutionContext(), func = std::move(jsFunc)](const BaseEventInfo* info) {
110 JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
111 func->Execute();
112 });
113 auto webComponent = AceType::DynamicCast<WebComponent>(ViewStackProcessor::GetInstance()->GetMainComponent());
114 webComponent->SetPageFinishedEventId(eventMarker);
115 }
116 }
117 } // namespace OHOS::Ace::Framework
118