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_XCOMPONENT_RESOURCE_XCOMPONENT_RESOURCE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_RESOURCE_XCOMPONENT_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 constexpr int64_t X_INVALID_ID = -1;
26 extern const char XCOMPONENT_PARAM_INIT[];
27 extern const char XCOMPONENT_PARAM_NONE[];
28 extern const char XCOMPONENT_PARAM_AND[];
29 extern const char XCOMPONENT_PARAM_VALUE[];
30 extern const char XCOMPONENT_PARAM_EQUALS[];
31 extern const char XCOMPONENT_PARAM_BEGIN[];
32 extern const char XCOMPONENT_METHOD[];
33 extern const char XCOMPONENT_EVENT[];
34 extern const char XCOMPONENT_RESULT_FAIL[];
35 
36 class XComponentResource : public virtual AceType {
37     DECLARE_ACE_TYPE(XComponentResource, AceType);
38 
39 public:
40     using ErrorCallback = std::function<void(const std::string&, const std::string&)>;
41 
XComponentResource(const std::string & type,const WeakPtr<PipelineContext> & context,ErrorCallback && onError)42     XComponentResource(const std::string& type,
43         const WeakPtr<PipelineContext>& context, ErrorCallback&& onError)
44         : type_(type), context_(context), onError_(std::move(onError))
45     {}
46     virtual ~XComponentResource() = default;
47 
48     void Release(const std::function<void(bool)>& onRelease = nullptr);
49 
50     void CallResRegisterMethod(const std::string& method, const std::string& param,
51                                const std::function<void(std::string&)>& callback = nullptr);
52 
GetId()53     int64_t GetId() const
54     {
55         return id_;
56     }
57 
GetHashCode()58     const std::string& GetHashCode() const
59     {
60         return hash_;
61     }
62 
63 protected:
64     double GetDoubleParam(const std::string& param, const std::string& name) const;
65     int32_t GetIntParam(const std::string& param, const std::string& name) const;
66     std::string GetStringParam(const std::string& param, const std::string& name) const;
67     std::string MakeResourceHash() const;
68     std::string MakeEventHash(const std::string& event) const;
69     std::string MakeMethodHash(const std::string& method) const;
70 
71     void OnError(const std::string& errorCode, const std::string& errorMsg);
72 
73     int64_t id_ = X_INVALID_ID;
74     std::string hash_;
75     std::string type_;
76     WeakPtr<PipelineContext> context_;
77     ErrorCallback onError_;
78 };
79 } // namespace OHOS::Ace
80 
81 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMPONENTS_XCOMPONENT_RESOURCE_XCOMPONENT_RESOURCE_H
82