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 MOJO_CORE_BROKER_HOST_H_ 6 #define MOJO_CORE_BROKER_HOST_H_ 7 8 #include <stdint.h> 9 #include <vector> 10 11 #include "base/macros.h" 12 #include "base/message_loop/message_loop_current.h" 13 #include "base/process/process_handle.h" 14 #include "base/strings/string_piece.h" 15 #include "build/build_config.h" 16 #include "mojo/core/channel.h" 17 #include "mojo/core/connection_params.h" 18 #include "mojo/core/embedder/process_error_callback.h" 19 #include "mojo/core/platform_handle_in_transit.h" 20 #include "mojo/core/scoped_process_handle.h" 21 #include "mojo/public/cpp/platform/platform_handle.h" 22 23 namespace mojo { 24 namespace core { 25 26 // The BrokerHost is a channel to a broker client process, servicing synchronous 27 // IPCs issued by the client. 28 class BrokerHost : public Channel::Delegate, 29 public base::MessageLoopCurrent::DestructionObserver { 30 public: 31 BrokerHost(base::ProcessHandle client_process, 32 ConnectionParams connection_params, 33 const ProcessErrorCallback& process_error_callback); 34 35 // Send |handle| to the client, to be used to establish a NodeChannel to us. 36 bool SendChannel(PlatformHandle handle); 37 38 #if defined(OS_WIN) 39 // Sends a named channel to the client. Like above, but for named pipes. 40 void SendNamedChannel(const base::StringPiece16& pipe_name); 41 #endif 42 43 private: 44 ~BrokerHost() override; 45 46 bool PrepareHandlesForClient(std::vector<PlatformHandleInTransit>* handles); 47 48 // Channel::Delegate: 49 void OnChannelMessage(const void* payload, 50 size_t payload_size, 51 std::vector<PlatformHandle> handles) override; 52 void OnChannelError(Channel::Error error) override; 53 54 // base::MessageLoopCurrent::DestructionObserver: 55 void WillDestroyCurrentMessageLoop() override; 56 57 void OnBufferRequest(uint32_t num_bytes); 58 59 const ProcessErrorCallback process_error_callback_; 60 61 #if defined(OS_WIN) 62 ScopedProcessHandle client_process_; 63 #endif 64 65 scoped_refptr<Channel> channel_; 66 67 DISALLOW_COPY_AND_ASSIGN(BrokerHost); 68 }; 69 70 } // namespace core 71 } // namespace mojo 72 73 #endif // MOJO_CORE_BROKER_HOST_H_ 74