ipc_channel_mach.h (4233B)
1 /* -*- Mode: C++; tab-width: 8; indent-tabs-mode: nil; c-basic-offset: 2 -*- */ 2 /* vim: set ts=8 sts=2 et sw=2 tw=80: */ 3 // Copyright (c) 2008 The Chromium Authors. All rights reserved. 4 // Use of this source code is governed by a BSD-style license that can be 5 // found in the LICENSE file. 6 7 #ifndef CHROME_COMMON_IPC_CHANNEL_MACH_H_ 8 #define CHROME_COMMON_IPC_CHANNEL_MACH_H_ 9 10 #include "chrome/common/ipc_channel.h" 11 12 #include "base/message_loop.h" 13 #include "base/process.h" 14 15 #include "mozilla/EventTargetAndLockCapability.h" 16 #include "mozilla/Maybe.h" 17 #include "mozilla/Mutex.h" 18 #include "mozilla/Queue.h" 19 #include "mozilla/UniquePtr.h" 20 #include "mozilla/UniquePtrExtensions.h" 21 #include "nsISupports.h" 22 23 namespace IPC { 24 25 // An implementation of ChannelImpl for macOS and iOS that works via 26 // mach ports. See the .cc file for an overview of the implementation. 27 class ChannelMach final : public Channel, 28 public MessageLoopForIO::MachPortWatcher { 29 public: 30 ChannelMach(mozilla::UniqueMachReceiveRight receive, 31 mozilla::UniqueMachSendRight send, Mode mode, 32 base::ProcessId other_pid); 33 34 bool Connect(Listener* listener) MOZ_EXCLUDES(SendMutex()) override; 35 void Close() MOZ_EXCLUDES(SendMutex()) override; 36 37 // NOTE: `Send` may be called on threads other than the I/O thread. 38 bool Send(mozilla::UniquePtr<Message> message) 39 MOZ_EXCLUDES(SendMutex()) override; 40 41 void SetOtherPid(base::ProcessId other_pid) override; 42 43 // These are only relevant to ChannelPosix, so we ignore them. 44 void SetOtherMachTask(task_t) override {} 45 46 const ChannelKind* GetKind() const override { return &sKind; } 47 48 static const ChannelKind sKind; 49 50 private: 51 ~ChannelMach() { Close(); } 52 53 static bool CreateRawPipe(ChannelHandle* server, ChannelHandle* client); 54 static bool CreateRawPipe(mozilla::UniqueMachReceiveRight* server, 55 mozilla::UniqueMachSendRight* client); 56 static uint32_t NumRelayedAttachments(const Message& message); 57 static bool IsValidHandle(const ChannelHandle& handle); 58 59 bool EnqueueHelloMessage() MOZ_REQUIRES(SendMutex(), IOThread()); 60 bool ContinueConnect(mozilla::UniqueMachSendRight send_port) 61 MOZ_REQUIRES(SendMutex(), IOThread()); 62 void CloseLocked() MOZ_REQUIRES(SendMutex(), IOThread()); 63 64 bool ProcessIncomingMessage() MOZ_REQUIRES(IOThread()); 65 bool ProcessOutgoingMessages() MOZ_REQUIRES(SendMutex()); 66 67 // MessageLoopForIO::MachPortWatcher implementation. 68 virtual void OnMachMessageReceived(mach_port_t port) override; 69 70 void OutputQueuePush(mozilla::UniquePtr<Message> msg) 71 MOZ_REQUIRES(SendMutex()); 72 void OutputQueuePop() MOZ_REQUIRES(SendMutex()); 73 74 // Watch controller for |receive_port_|, calls OnMachMessageReceived() when 75 // new messages are available. 76 MessageLoopForIO::MachPortWatchController watch_controller_; 77 78 // We always initialize |receive_port_| in the constructor, but |send_port_| 79 // may not be initialized until we've received a message from our peer. 80 mozilla::UniqueMachReceiveRight receive_port_ MOZ_GUARDED_BY(chan_cap_); 81 mozilla::UniqueMachSendRight send_port_ MOZ_GUARDED_BY(chan_cap_); 82 83 Listener* listener_ MOZ_GUARDED_BY(IOThread()) = nullptr; 84 85 // Buffers used for constructing mach IPC message payloads. 86 mozilla::UniquePtr<char[]> send_buffer_ MOZ_GUARDED_BY(SendMutex()); 87 mozilla::UniquePtr<char[]> receive_buffer_ MOZ_GUARDED_BY(IOThread()); 88 89 // Messages to be sent are queued here. 90 mozilla::Queue<mozilla::UniquePtr<Message>, 64> output_queue_ 91 MOZ_GUARDED_BY(SendMutex()); 92 93 // Indicates whether we've already serialized into the send buffer. 94 bool send_buffer_has_message_ MOZ_GUARDED_BY(SendMutex()) = false; 95 96 // Will be set to `true` until `Connect()` has been called and communication 97 // is ready. 98 bool waiting_connect_ MOZ_GUARDED_BY(chan_cap_) = true; 99 100 // We keep track of the PID of the other side of this channel so that we can 101 // record this when generating logs of IPC messages. 102 base::ProcessId other_pid_ MOZ_GUARDED_BY(chan_cap_) = 103 base::kInvalidProcessId; 104 105 mozilla::Maybe<audit_token_t> peer_audit_token_ MOZ_GUARDED_BY(IOThread()); 106 }; 107 108 } // namespace IPC 109 110 #endif // CHROME_COMMON_IPC_CHANNEL_MACH_H_