erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 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 | |
miu | 9f7788e | 2017-01-25 00:46:09 | [diff] [blame] | 5 | #ifndef MEDIA_REMOTING_RPC_BROKER_H_ |
| 6 | #define MEDIA_REMOTING_RPC_BROKER_H_ |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 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" |
miu | 9f7788e | 2017-01-25 00:46:09 | [diff] [blame] | 17 | #include "media/remoting/rpc.pb.h" |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 18 | |
| 19 | namespace media { |
| 20 | namespace remoting { |
| 21 | |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 22 | // Utility class to process incoming and outgoing RPC message to desired |
| 23 | // components on both end points. On sender side, for outgoing message, sender |
| 24 | // sends RPC message with associated handle value. On receiver side, for |
| 25 | // component which is interested in this RPC message has to register itself to |
| 26 | // RpcBroker. Before the RPC transmission starts, both sender and receiver need |
| 27 | // to negotiate the handle value in the existing RPC communication channel using |
miu | 9f7788e | 2017-01-25 00:46:09 | [diff] [blame] | 28 | // handle kAcquireHandle. |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 29 | // |
| 30 | // The class doesn't actually send RPC message to remote end point. Actual |
| 31 | // sender needs to set SendMessageCallback to RpcBroker. The class doesn't |
| 32 | // actually receive RPC message from the remote end point, either. Actually |
| 33 | // receiver needs to call ProcessMessageFromRemote() when RPC message is |
| 34 | // received. RpcBroker will distribute each RPC message to the components based |
| 35 | // on the handle value in the RPC message. |
| 36 | // |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 37 | // Note this is single-threaded class running on main thread. It provides |
| 38 | // WeakPtr() for caller to post tasks to the main thread. |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 39 | class RpcBroker { |
| 40 | public: |
| 41 | using SendMessageCallback = |
| 42 | base::Callback<void(std::unique_ptr<std::vector<uint8_t>>)>; |
| 43 | explicit RpcBroker(const SendMessageCallback& send_message_cb); |
| 44 | ~RpcBroker(); |
| 45 | |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 46 | // Get unique handle value (larger than 0) for RPC message handles. |
| 47 | int GetUniqueHandle(); |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 48 | |
| 49 | using ReceiveMessageCallback = |
| 50 | base::Callback<void(std::unique_ptr<pb::RpcMessage>)>; |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 51 | // Register a component to receive messages via the given |
| 52 | // ReceiveMessageCallback. |handle| is a unique handle value provided by a |
| 53 | // prior call to GetUniqueHandle() and is used to reference the component in |
| 54 | // the RPC messages. The receiver can then use it to direct an RPC message |
| 55 | // back to a specific component. |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 56 | void RegisterMessageReceiverCallback(int handle, |
| 57 | const ReceiveMessageCallback& callback); |
| 58 | // Allows components to unregister in order to stop receiving message. |
| 59 | void UnregisterMessageReceiverCallback(int handle); |
| 60 | |
| 61 | // Allows RpcBroker to distribute incoming RPC message to desired components. |
| 62 | void ProcessMessageFromRemote(std::unique_ptr<pb::RpcMessage> message); |
| 63 | // Sends RPC message to remote end point. The actually sender which sets |
| 64 | // SendMessageCallback to RpcBrokwer will receive RPC message to do actual |
| 65 | // data transmission. |
| 66 | void SendMessageToRemote(std::unique_ptr<pb::RpcMessage> message); |
| 67 | |
| 68 | // Gets weak pointer of RpcBroker. This allows callers to post tasks to |
| 69 | // RpcBroker on the main thread. |
| 70 | base::WeakPtr<RpcBroker> GetWeakPtr(); |
| 71 | |
| 72 | // Overwrites |send_message_cb_|. This is used only for test purposes. |
| 73 | void SetMessageCallbackForTesting(const SendMessageCallback& send_message_cb); |
| 74 | |
miu | 9f7788e | 2017-01-25 00:46:09 | [diff] [blame] | 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 | |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 85 | private: |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 86 | // Checks that all method calls occur on the same thread. |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 87 | base::ThreadChecker thread_checker_; |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 88 | |
| 89 | // Next unique handle to return from GetUniqueHandle(). |
| 90 | int next_handle_; |
| 91 | |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 92 | // Maps to hold handle value associated to MessageReceiver. |
| 93 | std::map<int, ReceiveMessageCallback> receive_callbacks_; |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 94 | |
| 95 | // Callback that is run to send a serialized message. |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 96 | SendMessageCallback send_message_cb_; |
miu | 3c44af5b | 2016-12-13 23:12:10 | [diff] [blame] | 97 | |
erickung | de09d57 | 2016-11-01 21:40:24 | [diff] [blame] | 98 | base::WeakPtrFactory<RpcBroker> weak_factory_; |
| 99 | |
| 100 | DISALLOW_COPY_AND_ASSIGN(RpcBroker); |
| 101 | }; |
| 102 | |
| 103 | } // namespace remoting |
| 104 | } // namespace media |
| 105 | |
miu | 9f7788e | 2017-01-25 00:46:09 | [diff] [blame] | 106 | #endif // MEDIA_REMOTING_RPC_BROKER_H_ |