• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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             LOGE("renderWeb is null");
60             return;
61         }
62         auto pipelineContext = renderWeb->GetContext().Upgrade();
63         if (!pipelineContext) {
64             LOGE("fail to create Update due to context is null");
65             return;
66         }
67         auto uiTaskExecutor = SingleTaskExecutor::Make(pipelineContext->GetTaskExecutor(),
68                                                        TaskExecutor::TaskType::UI);
69         uiTaskExecutor.PostTask([weakRender = weakRenderWeb, weakWeb = weakCom] {
70             auto renderWeb = weakRender.Upgrade();
71             if (renderWeb) {
72                 renderWeb->Update(weakWeb.Upgrade());
73             }
74         });
75     });
76     renderWeb->SetDelegate(delegate_);
77     return renderNode;
78 }
79 
CreateElement()80 RefPtr<Element> WebComponent::CreateElement()
81 {
82     auto webElement = AceType::MakeRefPtr<WebElement>();
83     focusElement_ = webElement;
84     return webElement;
85 }
86 
RequestFocus()87 void WebComponent::RequestFocus()
88 {
89     auto focus = focusElement_.Upgrade();
90     if (focus) {
91         focus->RequestFocusImmediately();
92     }
93 }
94 
95 } // namespace OHOS::Ace
96