• 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 "bridge/declarative_frontend/jsview/js_richtext.h"
17 
18 #include <optional>
19 #include <string>
20 
21 #include "bridge/declarative_frontend/engine/functions/js_function.h"
22 #include "bridge/declarative_frontend/engine/js_ref_ptr.h"
23 #include "bridge/declarative_frontend/jsview/js_view_abstract.h"
24 #include "bridge/declarative_frontend/jsview/js_view_common_def.h"
25 #include "bridge/declarative_frontend/jsview/models/richtext_model_impl.h"
26 #include "core/common/container.h"
27 #include "core/common/container_scope.h"
28 #include "core/components_ng/base/view_stack_processor.h"
29 #include "core/components_ng/pattern/web/richtext_model_ng.h"
30 
31 namespace OHOS::Ace {
32 const std::string RAWFILE_PREFIX = "resource://RAWFILE/";
33 const std::string BUNDLE_NAME_PREFIX = "bundleName:";
34 const std::string MODULE_NAME_PREFIX = "moduleName:";
35 std::unique_ptr<RichTextModel> RichTextModel::instance_ = nullptr;
36 std::mutex RichTextModel::mutex_;
GetInstance()37 RichTextModel* RichTextModel::GetInstance()
38 {
39     if (!instance_) {
40         std::lock_guard<std::mutex> lock(mutex_);
41         if (!instance_) {
42 #ifdef NG_BUILD
43             instance_.reset(new NG::RichTextModelNG());
44 #else
45             if (Container::IsCurrentUseNewPipeline()) {
46                 instance_.reset(new NG::RichTextModelNG());
47             } else {
48                 instance_.reset(new Framework::RichTextModelImpl());
49             }
50 #endif
51         }
52     }
53     return instance_.get();
54 }
55 } // namespace OHOS::Ace
56 
57 namespace OHOS::Ace::Framework {
Create(const JSCallbackInfo & info)58 void JSRichText::Create(const JSCallbackInfo& info)
59 {
60     if (info.Length() < 1) {
61         return;
62     }
63 
64     std::string data;
65     std::string webSrc;
66     std::optional<std::string> dstSrc;
67     if (ParseJsString(info[0], data)) {
68         RichTextModel::GetInstance()->Create(data);
69     } else if (ParseJsMedia(info[0], webSrc)) {
70         ParseRawfileWebSrc(info[0], webSrc);
71         int np = static_cast<int>(webSrc.find_first_of("/"));
72         dstSrc = np < 0 ? webSrc : webSrc.erase(np, 1);
73         RichTextModel::GetInstance()->Create(dstSrc.value());
74     } else {
75         TAG_LOGW(AceLogTag::ACE_RICH_TEXT, "richtext component failed to parse data");
76     }
77 }
78 
JSBind(BindingTarget globalObj)79 void JSRichText::JSBind(BindingTarget globalObj)
80 {
81     JSClass<JSRichText>::Declare("RichText");
82     JSClass<JSRichText>::StaticMethod("create", &JSRichText::Create);
83     JSClass<JSRichText>::StaticMethod("onStart", &JSRichText::OnStart);
84     JSClass<JSRichText>::StaticMethod("onComplete", &JSRichText::OnComplete);
85     JSClass<JSRichText>::StaticMethod("onAttach", &JSInteractableView::JsOnAttach);
86     JSClass<JSRichText>::StaticMethod("onAppear", &JSInteractableView::JsOnAppear);
87     JSClass<JSRichText>::StaticMethod("onDetach", &JSInteractableView::JsOnDetach);
88     JSClass<JSRichText>::StaticMethod("onDisAppear", &JSInteractableView::JsOnDisAppear);
89     JSClass<JSRichText>::StaticMethod("onTouch", &JSInteractableView::JsOnTouch);
90     JSClass<JSRichText>::InheritAndBind<JSViewAbstract>(globalObj);
91 }
92 
OnStart(const JSCallbackInfo & info)93 void JSRichText::OnStart(const JSCallbackInfo& info)
94 {
95     if (info.Length() > 0 && info[0]->IsFunction()) {
96         RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
97         auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
98         auto onStartEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
99                                 const BaseEventInfo* info) {
100             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
101             PipelineContext::SetCallBackNode(node);
102             func->Execute();
103         };
104 
105         RichTextModel::GetInstance()->SetOnPageStart(onStartEvent);
106     }
107 }
108 
OnComplete(const JSCallbackInfo & info)109 void JSRichText::OnComplete(const JSCallbackInfo& info)
110 {
111     if (info.Length() > 0 && info[0]->IsFunction()) {
112         RefPtr<JsFunction> jsFunc = AceType::MakeRefPtr<JsFunction>(JSRef<JSObject>(), JSRef<JSFunc>::Cast(info[0]));
113         auto targetNode = AceType::WeakClaim(NG::ViewStackProcessor::GetInstance()->GetMainFrameNode());
114         auto onCompleteEvent = [execCtx = info.GetExecutionContext(), func = std::move(jsFunc), node = targetNode](
115                                    const BaseEventInfo* info) {
116             JAVASCRIPT_EXECUTION_SCOPE_WITH_CHECK(execCtx);
117             PipelineContext::SetCallBackNode(node);
118             func->Execute();
119         };
120 
121         RichTextModel::GetInstance()->SetOnPageFinish(onCompleteEvent);
122     }
123 }
124 
ParseRawfileWebSrc(const JSRef<JSVal> & srcValue,std::string & webSrc)125 void JSRichText::ParseRawfileWebSrc(const JSRef<JSVal>& srcValue, std::string& webSrc)
126 {
127     if (!srcValue->IsObject() || webSrc.substr(0, RAWFILE_PREFIX.size()) != RAWFILE_PREFIX) {
128         return;
129     }
130     std::string bundleName;
131     std::string moduleName;
132     GetJsMediaBundleInfo(srcValue, bundleName, moduleName);
133     auto container = Container::Current();
134     CHECK_NULL_VOID(container);
135     if ((!bundleName.empty() && !moduleName.empty()) &&
136         (bundleName != AceApplicationInfo::GetInstance().GetPackageName() ||
137         moduleName != container->GetModuleName())) {
138         webSrc = RAWFILE_PREFIX + BUNDLE_NAME_PREFIX + bundleName + "/" + MODULE_NAME_PREFIX + moduleName + "/" +
139             webSrc.substr(RAWFILE_PREFIX.size());
140     }
141 }
142 } // namespace OHOS::Ace::Framework
143