• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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_COMMON_PLATFORM_RES_REGISTER_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLATFORM_RES_REGISTER_H
18 
19 #include <string>
20 #include <functional>
21 #include <unordered_map>
22 
23 #include "base/log/log.h"
24 #include "base/memory/referenced.h"
25 
26 namespace OHOS::Ace {
27 
28 class PipelineBase;
29 
30 class PlatformResRegister : public Referenced {
31 public:
32     using EventCallback = std::function<void(const std::string&)>;
33 
34     ~PlatformResRegister() override = default;
35 
RegisterEvent(const std::string & eventId,const EventCallback && eventCallback)36     void RegisterEvent(const std::string& eventId, const EventCallback&& eventCallback)
37     {
38         eventMap_[eventId] = std::move(eventCallback);
39     }
40 
UnregisterEvent(const std::string & eventId)41     void UnregisterEvent(const std::string& eventId)
42     {
43         eventMap_.erase(eventId);
44     }
45 
OnEvent(const std::string & eventId,const std::string & param)46     void OnEvent(const std::string& eventId, const std::string& param)
47     {
48         auto event = eventMap_.find(eventId);
49         if (event != eventMap_.end() && event->second) {
50             event->second(param);
51         } else {
52             LOGW("failed to find eventId = %{public}s", eventId.c_str());
53         }
54     }
55 
56     virtual bool OnMethodCall(const std::string& method, const std::string& param, std::string& result) = 0;
57     virtual int64_t CreateResource(const std::string& resourceType, const std::string& param) = 0;
58     virtual bool ReleaseResource(const std::string& resourceHash) = 0;
59 
SetPipelineContext(const WeakPtr<PipelineBase> & pipelineContext)60     void SetPipelineContext(const WeakPtr<PipelineBase>& pipelineContext)
61     {
62         pipelineContext_ = pipelineContext;
63     }
64 
GetPipelineContext()65     WeakPtr<PipelineBase> GetPipelineContext() const
66     {
67         return pipelineContext_;
68     }
69 
70 private:
71     std::unordered_map<std::string, EventCallback> eventMap_;
72     WeakPtr<PipelineBase> pipelineContext_;
73 };
74 
75 } // namespace OHOS::Ace
76 
77 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLATFORM_RES_REGISTER_H
78