OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 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 MOJO_EDK_SYSTEM_ROUTED_RAW_CHANNEL_H_ |
| 6 #define MOJO_EDK_SYSTEM_ROUTED_RAW_CHANNEL_H_ |
| 7 |
| 8 #include "base/callback.h" |
| 9 #include "base/containers/hash_tables.h" |
| 10 #include "base/macros.h" |
| 11 #include "base/synchronization/lock.h" |
| 12 #include "mojo/edk/embedder/platform_handle_vector.h" |
| 13 #include "mojo/edk/embedder/scoped_platform_handle.h" |
| 14 #include "mojo/edk/system/broker.h" |
| 15 #include "mojo/edk/system/raw_channel.h" |
| 16 #include "mojo/edk/system/system_impl_export.h" |
| 17 |
| 18 namespace mojo { |
| 19 namespace edk { |
| 20 class RawChannel; |
| 21 |
| 22 // This class wraps a RawChannel and adds routing on top of it. |
| 23 // Non-transferable MessagePipeDispatchers call here, indirectly through the |
| 24 // Broker interface, to associate with their pipe_id. |
| 25 class RoutedRawChannel : public RawChannel::Delegate { |
| 26 public: |
| 27 RoutedRawChannel( |
| 28 ScopedPlatformHandle handle, |
| 29 const base::Callback<void(RoutedRawChannel*)>& destruct_callback); |
| 30 |
| 31 // Connect the given |pipe| with the pipe_id route. Only non-transferable |
| 32 // message pipes can call this, and they can only call it once. |
| 33 void AddRoute(uint64_t pipe_id, MessagePipeDispatcher* pipe); |
| 34 |
| 35 // Called when the MessagePipeDispatcher is going away. |
| 36 void RemoveRoute(uint64_t pipe_id, MessagePipeDispatcher* pipe); |
| 37 |
| 38 RawChannel* channel() { return channel_; } |
| 39 |
| 40 private: |
| 41 friend class base::DeleteHelper<RoutedRawChannel>; |
| 42 ~RoutedRawChannel() override; |
| 43 |
| 44 // RawChannel::Delegate implementation: |
| 45 void OnReadMessage( |
| 46 const MessageInTransit::View& message_view, |
| 47 ScopedPlatformHandleVectorPtr platform_handles) override; |
| 48 void OnError(Error error) override; |
| 49 |
| 50 RawChannel* channel_; |
| 51 |
| 52 base::Lock lock_; // Guards access to below. |
| 53 base::hash_map<uint64_t, MessagePipeDispatcher*> routes_; |
| 54 |
| 55 // If we got messages before the route was added (due to race conditions |
| 56 // between different channels), this is used to buffer them. |
| 57 struct PendingMessage { |
| 58 PendingMessage(); |
| 59 ~PendingMessage(); |
| 60 std::vector<char> message; |
| 61 ScopedPlatformHandleVectorPtr handles; |
| 62 }; |
| 63 std::vector<scoped_ptr<PendingMessage>> pending_messages_; |
| 64 |
| 65 // If we got a ROUTE_CLOSED message for a route before it registered with us, |
| 66 // we need to hold on to this information so that we can tell it that the |
| 67 // connetion is closed when it does connect. |
| 68 base::hash_set<uint64_t> close_routes_; |
| 69 |
| 70 base::Callback<void(RoutedRawChannel*)> destruct_callback_; |
| 71 |
| 72 DISALLOW_COPY_AND_ASSIGN(RoutedRawChannel); |
| 73 }; |
| 74 |
| 75 } // namespace edk |
| 76 } // namespace mojo |
| 77 |
| 78 #endif // MOJO_EDK_SYSTEM_ROUTED_RAW_CHANNEL_H_ |
OLD | NEW |