1 /*
2  * Copyright (c) 2023 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_NG_PATTERNS_WEB_CORS_WEB_RESOURCE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_CORS_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 constexpr int INVALID_ID = -1;
26 
27 class WebResource : public virtual AceType {
28     DECLARE_ACE_TYPE(WebResource, AceType);
29 
30 public:
31     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
32     using Method = std::string;
33 
WebResource(const std::string & type,const WeakPtr<PipelineBase> & context,ErrorCallback && onError)34     WebResource(const std::string& type, const WeakPtr<PipelineBase>& context, ErrorCallback&& onError)
35         : type_(type), context_(context), onError_(std::move(onError))
36     {}
37     ~WebResource() override;
38 
39     void Release(const std::function<void(bool)>& onRelease = nullptr);
40 
41     void CallResRegisterMethod(
42         const Method& method, const std::string& param, const std::function<void(std::string&)>& callback = nullptr);
43 
GetId()44     int GetId() const
45     {
46         return id_;
47     }
48 
GetHashCode()49     const std::string& GetHashCode() const
50     {
51         return hash_;
52     }
53 
54 protected:
55     double GetDoubleParam(const std::string& param, const std::string& name) const;
56     int32_t GetIntParam(const std::string& param, const std::string& name) const;
57     std::string GetStringParam(const std::string& param, const std::string& name) const;
58     std::string MakeResourceHash() const;
59     std::string MakeEventHash(const std::string& event) const;
60     std::string MakeMethodHash(const std::string& method) const;
61 
62     void OnError(const std::string& errorCode, const std::string& errorMsg);
63 
64     int id_ = INVALID_ID;
65     std::string hash_;
66     std::string type_;
67     WeakPtr<PipelineBase> context_;
68     ErrorCallback onError_;
69 };
70 } // namespace OHOS::Ace
71 
72 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_NG_PATTERNS_WEB_CORS_WEB_RESOURCE_H
73