1 /*
2  * Copyright (c) 2021 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 #ifndef FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICHTEXT_RESOURCE_RICHTEXT_DELEGATE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICHTEXT_RESOURCE_RICHTEXT_DELEGATE_H
18 
19 #include <list>
20 
21 #include "core/components/common/layout/constants.h"
22 #include "core/components/rich_text/resource/rich_text_resource.h"
23 #include "core/components/rich_text/rich_text_component.h"
24 
25 namespace OHOS::Ace {
26 
27 class RichTextDelegate : public RichTextResource {
28     DECLARE_ACE_TYPE(RichTextDelegate, RichTextResource);
29 
30 public:
31     using CreatedCallback = std::function<void()>;
32     using ReleasedCallback = std::function<void(bool)>;
33     using EventCallback = std::function<void(const std::string&)>;
34     using UpdateWebViewLayoutCallback = std::function<void(int32_t, int32_t, int32_t)>;
35     enum class State: char {
36         WAITINGFORSIZE,
37         CREATING,
38         CREATED,
39         CREATEFAILED,
40         RELEASED,
41     };
42 
43     RichTextDelegate() = delete;
44     ~RichTextDelegate() override;
RichTextDelegate(const WeakPtr<RichTextComponent> & webComponent,const WeakPtr<PipelineContext> & context,ErrorCallback && onError,const std::string & type)45     RichTextDelegate(const WeakPtr<RichTextComponent>& webComponent,
46         const WeakPtr<PipelineContext>& context, ErrorCallback&& onError, const std::string& type)
47         : RichTextResource(type, context, std::move(onError)),
48           webComponent_(webComponent),
49           state_(State::WAITINGFORSIZE) {
50             ACE_DCHECK(!type.empty());
51     }
52 
53     void CreatePlatformResource(const WeakPtr<PipelineContext>& context,
54         const int32_t top, const int32_t left, const bool visible);
55     void ReleasePlatformResource();
56     void AddCreatedCallback(const CreatedCallback& createdCallback);
57     void RemoveCreatedCallback();
58     void AddReleasedCallback(const ReleasedCallback& releasedCallback);
59     void RemoveReleasedCallback();
60     void AddWebLayoutChangeCallback(const UpdateWebViewLayoutCallback& layoutChangeCallback);
61     void UpdateRichTextData(const std::string& data);
62     void UpdateWebPostion(const int32_t top, const int32_t left);
63     void UpdateContentScroll(const int32_t x, const int32_t y);
64     void ChangeRichTextVisibility(const bool visible);
65     void HideRichText();
66 
67 private:
68     void CreatePluginResource(const WeakPtr<PipelineContext>& context,
69         const int32_t top, const int32_t left, const bool visible);
70     void Stop();
71     void InitWebEvent();
72     void RegisterWebEvent();
73     void UnregisterEvent();
74 
75     void OnPageStarted(const std::string& param);
76     void OnPageFinished(const std::string& param);
77     void OnPageError(const std::string& param);
78     void OnGotLayoutParam(const std::string& param);
79 
80     std::string GetUrlStringParam(const std::string& param, const std::string& name) const;
81     void CallPopPageSuccessPageUrl(const std::string& url, const int32_t pageId);
82     void CallIsPagePathInvalid(const bool& isPageInvalid);
83 
84     void BindPopPageSuccessMethod();
85     void BindIsPagePathInvalidMethod();
86 
87     WeakPtr<RichTextComponent> webComponent_;
88     std::list<CreatedCallback> createdCallbacks_;
89     std::list<ReleasedCallback> releasedCallbacks_;
90     EventCallback onPageStarted_;
91     EventCallback onPageFinished_;
92     EventCallback onPageError_;
93     UpdateWebViewLayoutCallback webviewLayoutCallback_;
94     State state_ { State::WAITINGFORSIZE };
95     std::string richTextData_;
96     std::string pageUrl_;
97     int32_t pageId_ = -1;
98 };
99 
100 } // namespace OHOS::Ace
101 
102 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICHTEXT_RESOURCE_RICHTEXT_DELEGATE_H
103