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