1 /*
2 * Copyright (c) 2021-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 "core/components/web/web_component.h"
17
18 #include "core/components/web/render_web.h"
19 #include "core/components/web/resource/web_delegate.h"
20 #include "core/components/web/web_element.h"
21
22 namespace OHOS::Ace {
23
WebComponent(const std::string & type)24 WebComponent::WebComponent(const std::string& type) : type_(type)
25 {
26 ACE_DCHECK(!type_.empty());
27 if (!declaration_) {
28 declaration_ = AceType::MakeRefPtr<WebDeclaration>();
29 declaration_->Init();
30 }
31 webController_ = AceType::MakeRefPtr<WebController>();
32 if (SystemProperties::GetAllowWindowOpenMethodEnabled()) {
33 isAllowWindowOpenMethod_ = true;
34 }
35 }
36
CreateRenderNode()37 RefPtr<RenderNode> WebComponent::CreateRenderNode()
38 {
39 RefPtr<RenderNode> renderNode = RenderWeb::Create();
40 delegate_ = AceType::MakeRefPtr<WebDelegate>(renderNode->GetContext(),
41 std::move(errorCallback_), type_);
42 delegate_->SetComponent(AceType::Claim(this));
43 delegate_->SetPopup(isPopup_);
44 delegate_->SetParentNWebId(parentNWebId_);
45 if (createdCallback_ != nullptr) {
46 delegate_->AddCreatedCallback(createdCallback_);
47 }
48 auto renderWeb = AceType::DynamicCast<RenderWeb>(renderNode);
49 delegate_->AddCreatedCallback([weakRenderWeb = AceType::WeakClaim(AceType::RawPtr(renderWeb)),
50 weakCom = AceType::WeakClaim(this)]() {
51 auto renderWeb = weakRenderWeb.Upgrade();
52 if (!renderWeb) {
53 return;
54 }
55 auto pipelineContext = renderWeb->GetContext().Upgrade();
56 if (!pipelineContext) {
57 return;
58 }
59 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(),
60 TaskExecutor::TaskType::UI);
61 uiTaskExecutor.PostTask([weakRender = weakRenderWeb, weakWeb = weakCom] {
62 auto renderWeb = weakRender.Upgrade();
63 if (renderWeb) {
64 renderWeb->Update(weakWeb.Upgrade());
65 }
66 }, "ArkUIWebUpdateRenderNode");
67 });
68 renderWeb->SetDelegate(delegate_);
69 return renderNode;
70 }
71
CreateElement()72 RefPtr<Element> WebComponent::CreateElement()
73 {
74 auto webElement = AceType::MakeRefPtr<WebElement>();
75 focusElement_ = webElement;
76 return webElement;
77 }
78
RequestFocus()79 bool WebComponent::RequestFocus()
80 {
81 bool result = false;
82 #ifndef NG_BUILD
83 auto focus = focusElement_.Upgrade();
84 if (focus) {
85 result = focus->RequestFocusImmediately();
86 }
87 #else
88 TAG_LOGW(AceLogTag::ACE_WEB, "not support focus node in new pipeline");
89 #endif
90 return result;
91 }
92
93 } // namespace OHOS::Ace
94