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_WEB_RESOURCE_WEB_RESOURCE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_H
18 
19 #include <string>
20 
21 #include "base/memory/ace_type.h"
22 #include "core/pipeline/pipeline_base.h"
23 
24 namespace OHOS::Ace {
25 
26 constexpr int64_t WEB_INVALID_ID = -1;
27 
28 extern const char WEB_PARAM_NONE[];
29 extern const char WEB_PARAM_AND[];
30 extern const char WEB_PARAM_VALUE[];
31 extern const char WEB_PARAM_EQUALS[];
32 extern const char WEB_PARAM_BEGIN[];
33 extern const char WEB_METHOD[];
34 extern const char WEB_EVENT[];
35 extern const char WEB_RESULT_FAIL[];
36 
37 class WebResource : public virtual AceType {
38     DECLARE_ACE_TYPE(WebResource, AceType);
39 
40 public:
41     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
42     using Method = std::string;
43 
WebResource(const std::string & type,const WeakPtr<PipelineBase> & context,ErrorCallback && onError)44     WebResource(const std::string& type, const WeakPtr<PipelineBase>& context, ErrorCallback&& onError)
45         : type_(type), context_(context), onError_(std::move(onError))
46     {}
47     ~WebResource() override = default;
48 
49     void Release(const std::function<void(bool)>& onRelease = nullptr);
50 
51     void CallResRegisterMethod(
52         const Method& method, const std::string& param, const std::function<void(std::string&)>& callback = nullptr);
53 
GetId()54     int64_t GetId() const
55     {
56         return id_;
57     }
58 
GetHashCode()59     const std::string& GetHashCode() const
60     {
61         return hash_;
62     }
63 
64 protected:
65     double GetDoubleParam(const std::string& param, const std::string& name) const;
66     int32_t GetIntParam(const std::string& param, const std::string& name) const;
67     std::string GetStringParam(const std::string& param, const std::string& name) const;
68     std::string MakeResourceHash() const;
69     std::string MakeEventHash(const std::string& event) const;
70     std::string MakeMethodHash(const std::string& method) const;
71 
72     void OnError(const std::string& errorCode, const std::string& errorMsg);
73 
74     int64_t id_ = WEB_INVALID_ID;
75     std::string hash_;
76     std::string type_;
77     WeakPtr<PipelineBase> context_;
78     ErrorCallback onError_;
79 };
80 
81 } // namespace OHOS::Ace
82 
83 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_WEB_RESOURCE_WEB_RESOURCE_H
84