• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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 "frameworks/bridge/card_frontend/card_frontend_delegate.h"
17 
18 #include <string>
19 
20 #include "base/log/event_report.h"
21 #include "base/utils/utils.h"
22 #include "core/common/thread_checker.h"
23 
24 namespace OHOS::Ace::Framework {
25 
CardFrontendDelegate()26 CardFrontendDelegate::CardFrontendDelegate() : jsAccessibilityManager_(AccessibilityNodeManager::Create()) {}
27 
~CardFrontendDelegate()28 CardFrontendDelegate::~CardFrontendDelegate()
29 {
30     CHECK_RUN_ON(JS);
31     LOG_DESTROY();
32 }
33 
FireCardEvent(const EventMarker & eventMarker,const std::string & params)34 void CardFrontendDelegate::FireCardEvent(const EventMarker& eventMarker, const std::string& params)
35 {
36     CHECK_NULL_VOID(page_);
37     auto context = page_->GetPipelineContext().Upgrade();
38     if (!context) {
39         LOGE("FireCardEvent get pipeline context failed");
40         EventReport::SendFormException(FormExcepType::FIRE_FORM_EVENT_ERR);
41         return;
42     }
43     auto nodeId = eventMarker.GetData().eventId == "_root" ? DOM_ROOT_NODE_ID_BASE + page_->GetPageId()
44                                                            : StringToInt(eventMarker.GetData().eventId);
45     auto action = page_->GetNodeEventAction(nodeId, eventMarker.GetData().eventType);
46     auto node = page_->GetDomDocument()->GetDOMNodeById(nodeId);
47     if (!node) {
48         LOGE("FireCardEvent get node failed");
49         EventReport::SendFormException(FormExcepType::FIRE_FORM_EVENT_ERR);
50         return;
51     }
52 
53     auto routerAction = JsonUtil::ParseJsonString(action);
54     auto paramsJson = JsonUtil::ParseJsonString(params);
55     if (!routerAction->IsValid()) {
56         LOGE("FireCardEvent router action is null");
57         return;
58     }
59     auto paramsInfo = routerAction->GetValue("params");
60     if (paramsInfo->IsValid()) {
61         auto child = paramsInfo->GetChild();
62         while (child->IsValid()) {
63             auto key = child->GetKey();
64             auto value = child->GetString();
65             child = child->GetNext();
66             if (Framework::StartWith(value, "$event.")) {
67                 paramsInfo->Replace(key.c_str(), paramsJson->GetValue(value.substr(7)));
68             }
69         }
70     }
71     action = routerAction->ToString();
72     context->OnActionEvent(action);
73 
74     // send event to accessibility
75     AccessibilityEvent accessibilityEvent;
76     accessibilityEvent.nodeId = nodeId;
77     accessibilityEvent.eventType = eventMarker.GetData().eventType;
78     context->SendEventToAccessibility(accessibilityEvent);
79 }
80 
CreatePage(int32_t pageId,const std::string & url)81 RefPtr<JsAcePage>& CardFrontendDelegate::CreatePage(int32_t pageId, const std::string& url)
82 {
83     if (!page_) {
84         auto document = AceType::MakeRefPtr<DOMDocument>(pageId);
85         page_ = AceType::MakeRefPtr<Framework::JsAcePage>(pageId, document, url);
86     }
87     return page_;
88 }
89 
CreatePage(int32_t pageId,const std::string & url,const WeakPtr<StageElement> & container)90 RefPtr<JsAcePage>& CardFrontendDelegate::CreatePage(
91     int32_t pageId, const std::string& url, const WeakPtr<StageElement>& container)
92 {
93     if (!page_) {
94         auto document = AceType::MakeRefPtr<DOMDocument>(pageId);
95         page_ = AceType::MakeRefPtr<Framework::JsAcePage>(pageId, document, url, container);
96     }
97     return page_;
98 }
99 
GetPage()100 RefPtr<JsAcePage>& CardFrontendDelegate::GetPage()
101 {
102     return page_;
103 }
104 
105 } // namespace OHOS::Ace::Framework
106