OLD | NEW |
1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MEDIA_REMOTING_RPC_RPC_BROKER_H_ | 5 #ifndef MEDIA_REMOTING_RPC_BROKER_H_ |
6 #define MEDIA_REMOTING_RPC_RPC_BROKER_H_ | 6 #define MEDIA_REMOTING_RPC_BROKER_H_ |
7 | 7 |
8 #include <map> | 8 #include <map> |
9 #include <memory> | 9 #include <memory> |
10 #include <string> | 10 #include <string> |
11 #include <vector> | 11 #include <vector> |
12 | 12 |
13 #include "base/callback.h" | 13 #include "base/callback.h" |
14 #include "base/macros.h" | 14 #include "base/macros.h" |
15 #include "base/memory/weak_ptr.h" | 15 #include "base/memory/weak_ptr.h" |
16 #include "base/threading/thread_checker.h" | 16 #include "base/threading/thread_checker.h" |
17 #include "media/remoting/remoting_rpc_message.pb.h" | 17 #include "media/remoting/rpc.pb.h" |
18 | 18 |
19 namespace media { | 19 namespace media { |
20 namespace remoting { | 20 namespace remoting { |
21 | 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 | 22 // Utility class to process incoming and outgoing RPC message to desired |
29 // components on both end points. On sender side, for outgoing message, sender | 23 // components on both end points. On sender side, for outgoing message, sender |
30 // sends RPC message with associated handle value. On receiver side, for | 24 // 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 | 25 // 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 | 26 // RpcBroker. Before the RPC transmission starts, both sender and receiver need |
33 // to negotiate the handle value in the existing RPC communication channel using | 27 // to negotiate the handle value in the existing RPC communication channel using |
34 // handle kReceiverHandle. | 28 // handle kAcquireHandle. |
35 // | 29 // |
36 // The class doesn't actually send RPC message to remote end point. Actual | 30 // 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 | 31 // sender needs to set SendMessageCallback to RpcBroker. The class doesn't |
38 // actually receive RPC message from the remote end point, either. Actually | 32 // actually receive RPC message from the remote end point, either. Actually |
39 // receiver needs to call ProcessMessageFromRemote() when RPC message is | 33 // receiver needs to call ProcessMessageFromRemote() when RPC message is |
40 // received. RpcBroker will distribute each RPC message to the components based | 34 // received. RpcBroker will distribute each RPC message to the components based |
41 // on the handle value in the RPC message. | 35 // on the handle value in the RPC message. |
42 // | 36 // |
43 // Note this is single-threaded class running on main thread. It provides | 37 // Note this is single-threaded class running on main thread. It provides |
44 // WeakPtr() for caller to post tasks to the main thread. | 38 // WeakPtr() for caller to post tasks to the main thread. |
(...skipping 26 matching lines...) Expand all Loading... |
71 // data transmission. | 65 // data transmission. |
72 void SendMessageToRemote(std::unique_ptr<pb::RpcMessage> message); | 66 void SendMessageToRemote(std::unique_ptr<pb::RpcMessage> message); |
73 | 67 |
74 // Gets weak pointer of RpcBroker. This allows callers to post tasks to | 68 // Gets weak pointer of RpcBroker. This allows callers to post tasks to |
75 // RpcBroker on the main thread. | 69 // RpcBroker on the main thread. |
76 base::WeakPtr<RpcBroker> GetWeakPtr(); | 70 base::WeakPtr<RpcBroker> GetWeakPtr(); |
77 | 71 |
78 // Overwrites |send_message_cb_|. This is used only for test purposes. | 72 // Overwrites |send_message_cb_|. This is used only for test purposes. |
79 void SetMessageCallbackForTesting(const SendMessageCallback& send_message_cb); | 73 void SetMessageCallbackForTesting(const SendMessageCallback& send_message_cb); |
80 | 74 |
| 75 // Predefined invalid handle value for RPC message. |
| 76 static constexpr int kInvalidHandle = -1; |
| 77 |
| 78 // Predefined handle value for RPC messages related to initialization (before |
| 79 // the receiver handle(s) are known). |
| 80 static constexpr int kAcquireHandle = 0; |
| 81 |
| 82 // The first handle to return from GetUniqueHandle(). |
| 83 static constexpr int kFirstHandle = 1; |
| 84 |
81 private: | 85 private: |
82 // Checks that all method calls occur on the same thread. | 86 // Checks that all method calls occur on the same thread. |
83 base::ThreadChecker thread_checker_; | 87 base::ThreadChecker thread_checker_; |
84 | 88 |
85 // Next unique handle to return from GetUniqueHandle(). | 89 // Next unique handle to return from GetUniqueHandle(). |
86 int next_handle_; | 90 int next_handle_; |
87 | 91 |
88 // Maps to hold handle value associated to MessageReceiver. | 92 // Maps to hold handle value associated to MessageReceiver. |
89 std::map<int, ReceiveMessageCallback> receive_callbacks_; | 93 std::map<int, ReceiveMessageCallback> receive_callbacks_; |
90 | 94 |
91 // Callback that is run to send a serialized message. | 95 // Callback that is run to send a serialized message. |
92 SendMessageCallback send_message_cb_; | 96 SendMessageCallback send_message_cb_; |
93 | 97 |
94 base::WeakPtrFactory<RpcBroker> weak_factory_; | 98 base::WeakPtrFactory<RpcBroker> weak_factory_; |
95 | 99 |
96 DISALLOW_COPY_AND_ASSIGN(RpcBroker); | 100 DISALLOW_COPY_AND_ASSIGN(RpcBroker); |
97 }; | 101 }; |
98 | 102 |
99 } // namespace remoting | 103 } // namespace remoting |
100 } // namespace media | 104 } // namespace media |
101 | 105 |
102 #endif // MEDIA_REMOTING_RPC_RPC_BROKER_H_ | 106 #endif // MEDIA_REMOTING_RPC_BROKER_H_ |
OLD | NEW |