OLD | NEW |
| (Empty) |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | |
2 // Use of this source code is governed by a BSD-style license that can be | |
3 // found in the LICENSE file. | |
4 | |
5 #ifndef MEDIA_REMOTING_RPC_RPC_BROKER_H_ | |
6 #define MEDIA_REMOTING_RPC_RPC_BROKER_H_ | |
7 | |
8 #include <map> | |
9 #include <memory> | |
10 #include <string> | |
11 #include <vector> | |
12 | |
13 #include "base/callback.h" | |
14 #include "base/macros.h" | |
15 #include "base/memory/weak_ptr.h" | |
16 #include "base/threading/thread_checker.h" | |
17 #include "media/remoting/remoting_rpc_message.pb.h" | |
18 | |
19 namespace media { | |
20 namespace remoting { | |
21 | |
22 // Predefined invalid handle value for RPC message. | |
23 constexpr int kInvalidHandle = -1; | |
24 | |
25 // Predefined handle value for RPC messages related to initialization. | |
26 constexpr int kReceiverHandle = 0; | |
27 | |
28 // Utility class to process incoming and outgoing RPC message to desired | |
29 // components on both end points. On sender side, for outgoing message, sender | |
30 // sends RPC message with associated handle value. On receiver side, for | |
31 // component which is interested in this RPC message has to register itself to | |
32 // RpcBroker. Before the RPC transmission starts, both sender and receiver need | |
33 // to negotiate the handle value in the existing RPC communication channel using | |
34 // handle kReceiverHandle. | |
35 // | |
36 // The class doesn't actually send RPC message to remote end point. Actual | |
37 // sender needs to set SendMessageCallback to RpcBroker. The class doesn't | |
38 // actually receive RPC message from the remote end point, either. Actually | |
39 // receiver needs to call ProcessMessageFromRemote() when RPC message is | |
40 // received. RpcBroker will distribute each RPC message to the components based | |
41 // on the handle value in the RPC message. | |
42 // | |
43 // Note this is single-threaded class running on main thread. It provides | |
44 // WeakPtr() for caller to post tasks to the main thread. | |
45 class RpcBroker { | |
46 public: | |
47 using SendMessageCallback = | |
48 base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>; | |
49 explicit RpcBroker(const SendMessageCallback& send_message_cb); | |
50 ~RpcBroker(); | |
51 | |
52 // Get unique handle value (larger than 0) for RPC message handles. | |
53 int GetUniqueHandle(); | |
54 | |
55 using ReceiveMessageCallback = | |
56 base::Callback<void(std::unique_ptr<pb::RpcMessage>)>; | |
57 // Register a component to receive messages via the given | |
58 // ReceiveMessageCallback. |handle| is a unique handle value provided by a | |
59 // prior call to GetUniqueHandle() and is used to reference the component in | |
60 // the RPC messages. The receiver can then use it to direct an RPC message | |
61 // back to a specific component. | |
62 void RegisterMessageReceiverCallback(int handle, | |
63 const ReceiveMessageCallback& callback); | |
64 // Allows components to unregister in order to stop receiving message. | |
65 void UnregisterMessageReceiverCallback(int handle); | |
66 | |
67 // Allows RpcBroker to distribute incoming RPC message to desired components. | |
68 void ProcessMessageFromRemote(std::unique_ptr<pb::RpcMessage> message); | |
69 // Sends RPC message to remote end point. The actually sender which sets | |
70 // SendMessageCallback to RpcBrokwer will receive RPC message to do actual | |
71 // data transmission. | |
72 void SendMessageToRemote(std::unique_ptr<pb::RpcMessage> message); | |
73 | |
74 // Gets weak pointer of RpcBroker. This allows callers to post tasks to | |
75 // RpcBroker on the main thread. | |
76 base::WeakPtr<RpcBroker> GetWeakPtr(); | |
77 | |
78 // Overwrites |send_message_cb_|. This is used only for test purposes. | |
79 void SetMessageCallbackForTesting(const SendMessageCallback& send_message_cb); | |
80 | |
81 private: | |
82 // Checks that all method calls occur on the same thread. | |
83 base::ThreadChecker thread_checker_; | |
84 | |
85 // Next unique handle to return from GetUniqueHandle(). | |
86 int next_handle_; | |
87 | |
88 // Maps to hold handle value associated to MessageReceiver. | |
89 std::map<int, ReceiveMessageCallback> receive_callbacks_; | |
90 | |
91 // Callback that is run to send a serialized message. | |
92 SendMessageCallback send_message_cb_; | |
93 | |
94 base::WeakPtrFactory<RpcBroker> weak_factory_; | |
95 | |
96 DISALLOW_COPY_AND_ASSIGN(RpcBroker); | |
97 }; | |
98 | |
99 } // namespace remoting | |
100 } // namespace media | |
101 | |
102 #endif // MEDIA_REMOTING_RPC_RPC_BROKER_H_ | |
OLD | NEW |