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 "base/log/log.h"
24 #include "core/components/web/render_web.h"
25 #include "core/components/web/resource/web_delegate.h"
26 #include "core/components/web/web_element.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 }
39
CreateRenderNode()40 RefPtr<RenderNode> WebComponent::CreateRenderNode()
41 {
42 RefPtr<RenderNode> renderNode = RenderWeb::Create();
43 delegate_ = AceType::MakeRefPtr<WebDelegate>(renderNode->GetContext(),
44 std::move(errorCallback_), type_);
45 delegate_->SetComponent(AceType::Claim(this));
46 if (createdCallback_ != nullptr) {
47 delegate_->AddCreatedCallback(createdCallback_);
48 }
49 auto renderWeb = AceType::DynamicCast<RenderWeb>(renderNode);
50 delegate_->AddCreatedCallback([renderWeb, this]() {
51 if (!renderWeb) {
52 LOGE("renderWeb is null");
53 return;
54 }
55 auto pipelineContext = renderWeb->GetContext().Upgrade();
56 if (!pipelineContext) {
57 LOGE("fail to create Update due to context is null");
58 return;
59 }
60 auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(),
61 TaskExecutor::TaskType::UI);
62 auto weakRender = AceType::WeakClaim(AceType::RawPtr(renderWeb));
63 uiTaskExecutor.PostTask([weakRender, weakWeb = AceType::WeakClaim(this)] {
64 auto renderWeb = weakRender.Upgrade();
65 if (renderWeb) {
66 renderWeb->Update(weakWeb.Upgrade());
67 }
68 });
69 });
70 renderWeb->SetDelegate(delegate_);
71 return renderNode;
72 }
73
CreateElement()74 RefPtr<Element> WebComponent::CreateElement()
75 {
76 auto webElement = AceType::MakeRefPtr<WebElement>();
77 focusElement_ = webElement;
78 return webElement;
79 }
80
RequestFocus()81 void WebComponent::RequestFocus()
82 {
83 auto focus = focusElement_.Upgrade();
84 if (focus) {
85 focus->RequestFocusImmediately();
86 }
87 }
88
89 } // namespace OHOS::Ace
90