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_BRIDGE_H
17 #define FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLATFORM_BRIDGE_H
18 
19 #include <functional>
20 #include <map>
21 #include <vector>
22 
23 #include "base/utils/macros.h"
24 #include "base/utils/noncopyable.h"
25 #include "bridge/codec/codec_data.h"
26 #include "core/common/js_message_dispatcher.h"
27 
28 namespace OHOS::Ace {
29 
30 using PlatformCallbackHandler = std::function<void(const std::string&)>;
31 using PlatformCallbackMap = std::map<int32_t, PlatformCallbackHandler>;
32 
33 class PlatformBridge : public AceType {
34     DECLARE_ACE_TYPE(PlatformBridge, AceType);
35 
36 public:
37     PlatformBridge() = default;
38     virtual ~PlatformBridge() = default;
39 
SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher> & dispatcher)40     void SetJsMessageDispatcher(const RefPtr<JsMessageDispatcher>& dispatcher)
41     {
42         dispatcher_ = dispatcher;
43     }
44 
45     void SendMessage(const std::vector<Framework::CodecData>& args, const PlatformCallbackHandler& handler);
46     void HandleCallback(int32_t callbackId, std::vector<uint8_t>&& messageData);
47 
48 private:
49     WeakPtr<JsMessageDispatcher> dispatcher_;
50     PlatformCallbackMap callBackHandlers_;
51     std::atomic_int callbackIds_ = 1;
52 };
53 
54 } // namespace OHOS::Ace
55 
56 #endif // FOUNDATION_ACE_FRAMEWORKS_CORE_COMMON_PLATFORM_BRIDGE_H
57