blob: 75bf59722b9fe6d0b4d636edc6cbe39174fded40 [file] [log] [blame]
erickungde09d572016-11-01 21:40:241// 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
miu9f7788e2017-01-25 00:46:095#ifndef MEDIA_REMOTING_RPC_BROKER_H_
6#define MEDIA_REMOTING_RPC_BROKER_H_
erickungde09d572016-11-01 21:40:247
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"
miu9f7788e2017-01-25 00:46:0917#include "media/remoting/rpc.pb.h"
erickungde09d572016-11-01 21:40:2418
19namespace media {
20namespace remoting {
21
erickungde09d572016-11-01 21:40:2422// 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
miu9f7788e2017-01-25 00:46:0928// handle kAcquireHandle.
erickungde09d572016-11-01 21:40:2429//
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//
miu3c44af5b2016-12-13 23:12:1037// Note this is single-threaded class running on main thread. It provides
38// WeakPtr() for caller to post tasks to the main thread.
erickungde09d572016-11-01 21:40:2439class 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
miu3c44af5b2016-12-13 23:12:1046 // Get unique handle value (larger than 0) for RPC message handles.
47 int GetUniqueHandle();
erickungde09d572016-11-01 21:40:2448
49 using ReceiveMessageCallback =
50 base::Callback<void(std::unique_ptr<pb::RpcMessage>)>;
miu3c44af5b2016-12-13 23:12:1051 // 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.
erickungde09d572016-11-01 21:40:2456 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
miu9f7788e2017-01-25 00:46:0975 // 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
erickungde09d572016-11-01 21:40:2485 private:
miu3c44af5b2016-12-13 23:12:1086 // Checks that all method calls occur on the same thread.
erickungde09d572016-11-01 21:40:2487 base::ThreadChecker thread_checker_;
miu3c44af5b2016-12-13 23:12:1088
89 // Next unique handle to return from GetUniqueHandle().
90 int next_handle_;
91
erickungde09d572016-11-01 21:40:2492 // Maps to hold handle value associated to MessageReceiver.
93 std::map<int, ReceiveMessageCallback> receive_callbacks_;
miu3c44af5b2016-12-13 23:12:1094
95 // Callback that is run to send a serialized message.
erickungde09d572016-11-01 21:40:2496 SendMessageCallback send_message_cb_;
miu3c44af5b2016-12-13 23:12:1097
erickungde09d572016-11-01 21:40:2498 base::WeakPtrFactory<RpcBroker> weak_factory_;
99
100 DISALLOW_COPY_AND_ASSIGN(RpcBroker);
101};
102
103} // namespace remoting
104} // namespace media
105
miu9f7788e2017-01-25 00:46:09106#endif // MEDIA_REMOTING_RPC_BROKER_H_