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
27 namespace OHOS::Ace {
28
WebComponent(const std::string & type)29 WebComponent::WebComponent(const std::string& type) : type_(type)
30 {
31 ACE_DCHECK(!type_.empty());
32 if (!declaration_) {
33 declaration_ = AceType::MakeRefPtr<WebDeclaration>();
34 declaration_->Init();
35 }
36 webController_ = AceType::MakeRefPtr<WebController>();
37 }
38
CreateRenderNode()39 RefPtr<RenderNode> WebComponent::CreateRenderNode()
40 {
41 RefPtr<RenderNode> renderNode = RenderWeb::Create();
42 delegate_ = AceType::MakeRefPtr<WebDelegate>(renderNode->GetContext(),
43 std::move(errorCallback_), type_);
44 delegate_->SetComponent(AceType::Claim(this));
45 delegate_->SetPopup(isPopup_);
46 delegate_->SetParentNWebId(parentNWebId_);
47 if (createdCallback_ != nullptr) {
48 delegate_->AddCreatedCallback(createdCallback_);
49 }
50 auto renderWeb = AceType::DynamicCast<RenderWeb>(renderNode);
51 delegate_->AddCreatedCallback([weakRenderWeb = AceType::WeakClaim(AceType::RawPtr(renderWeb)),
52 weakCom = AceType::WeakClaim(this)]() {
53 auto renderWeb = weakRenderWeb.Upgrade();
54 if (!renderWeb) {
55 LOGE("renderWeb is null");
56 return;
57 }
58 auto pipelineContext = renderWeb->GetContext().Upgrade();
59 if (!pipelineContext) {
60 LOGE("fail to create Update due to context is null");
61 return;
62 }
63 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(),
64 TaskExecutor::TaskType::UI);
65 uiTaskExecutor.PostTask([weakRender = weakRenderWeb, weakWeb = weakCom] {
66 auto renderWeb = weakRender.Upgrade();
67 if (renderWeb) {
68 renderWeb->Update(weakWeb.Upgrade());
69 }
70 });
71 });
72 renderWeb->SetDelegate(delegate_);
73 return renderNode;
74 }
75
CreateElement()76 RefPtr<Element> WebComponent::CreateElement()
77 {
78 auto webElement = AceType::MakeRefPtr<WebElement>();
79 focusElement_ = webElement;
80 return webElement;
81 }
82
RequestFocus()83 void WebComponent::RequestFocus()
84 {
85 auto focus = focusElement_.Upgrade();
86 if (focus) {
87 focus->RequestFocusImmediately();
88 }
89 }
90
91 } // namespace OHOS::Ace
92