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_RESOURCE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICHTEXT_RESOURCE_RICHTEXT_RESOURCE_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/pipeline/pipeline_context.h"
23 
24 namespace OHOS::Ace {
25 
26 constexpr int64_t INVALID_ID = -1;
27 constexpr int64_t CREATING_ID = -2;
28 
29 extern const char RICHTEXT_PARAM_NONE[];
30 extern const char RICHTEXT_PARAM_AND[];
31 extern const char RICHTEXT_PARAM_VALUE[];
32 extern const char RICHTEXT_PARAM_EQUALS[];
33 extern const char RICHTEXT_PARAM_BEGIN[];
34 extern const char RICHTEXT_METHOD[];
35 extern const char RICHTEXT_EVENT[];
36 extern const char RICHTEXT_RESULT_FAIL[];
37 
38 class RichTextResource : public virtual AceType {
39     DECLARE_ACE_TYPE(RichTextResource, AceType);
40 
41 public:
42     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
43     using Method = std::string;
44 
RichTextResource(const std::string & type,const WeakPtr<PipelineContext> & context,ErrorCallback && onError)45     RichTextResource(const std::string& type,
46         const WeakPtr<PipelineContext>& context, ErrorCallback&& onError)
47         : type_(type), context_(context), onError_(std::move(onError))
48     {}
49     virtual ~RichTextResource() = default;
50 
51     void Release(const std::function<void(bool)>& onRelease = nullptr);
52 
53     void CallResRegisterMethod(const Method& method, const std::string& param,
54                                const std::function<void(std::string&)>& callback = nullptr);
55 
GetId()56     int64_t GetId() const
57     {
58         return id_;
59     }
60 
GetHashCode()61     const std::string& GetHashCode() const
62     {
63         return hash_;
64     }
65 
66 protected:
67     double GetDoubleParam(const std::string& param, const std::string& name) const;
68     int32_t GetIntParam(const std::string& param, const std::string& name) const;
69     std::string GetStringParam(const std::string& param, const std::string& name) const;
70     std::string MakeResourceHash() const;
71     std::string MakeEventHash(const std::string& event) const;
72     std::string MakeMethodHash(const std::string& method) const;
73 
74     void OnError(const std::string& errorCode, const std::string& errorMsg);
Reset()75     void Reset()
76     {
77         id_ = INVALID_ID;
78         hash_.clear();
79     }
80 
81     int64_t id_ = INVALID_ID;
82     std::string hash_;
83     std::string type_;
84     WeakPtr<PipelineContext> context_;
85     ErrorCallback onError_;
86 };
87 
88 } // namespace OHOS::Ace
89 
90 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_RICHTEXT_RESOURCE_RICHTEXT_RESOURCE_H
91