Battery Status API: add UMA logging for Linux.
[chromium-blink-merge.git] / ipc / mojo / ipc_channel_mojo.h
blob4d4df6359908b4acfb5aa150ab2a39434a2e1ff4
1 // Copyright 2014 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.
5 #ifndef IPC_IPC_CHANNEL_MOJO_H_
6 #define IPC_IPC_CHANNEL_MOJO_H_
8 #include <vector>
10 #include "base/memory/scoped_ptr.h"
11 #include "base/memory/scoped_vector.h"
12 #include "base/memory/weak_ptr.h"
13 #include "ipc/ipc_channel.h"
14 #include "ipc/ipc_channel_factory.h"
15 #include "ipc/ipc_export.h"
16 #include "ipc/mojo/ipc_message_pipe_reader.h"
17 #include "mojo/public/cpp/system/core.h"
19 namespace mojo {
20 namespace embedder {
21 struct ChannelInfo;
25 namespace IPC {
27 // Mojo-based IPC::Channel implementation over a platform handle.
29 // ChannelMojo builds Mojo MessagePipe using underlying pipe given by
30 // "bootstrap" IPC::Channel which creates and owns platform pipe like
31 // named socket. The bootstrap Channel is used only for establishing
32 // the underlying connection. ChannelMojo takes its handle over once
33 // the it is made and puts MessagePipe on it.
35 // ChannelMojo has a couple of MessagePipes:
37 // * The first MessagePipe, which is built on top of bootstrap handle,
38 // is the "control" pipe. It is used to communicate out-of-band
39 // control messages that aren't visible from IPC::Listener.
41 // * The second MessagePipe, which is created by the server channel
42 // and sent to client Channel over the control pipe, is used
43 // to send IPC::Messages as an IPC::Sender.
45 // TODO(morrita): Extract handle creation part of IPC::Channel into
46 // separate class to clarify what ChannelMojo relies
47 // on.
48 // TODO(morrita): Add APIs to create extra MessagePipes to let
49 // Mojo-based objects talk over this Channel.
51 class IPC_MOJO_EXPORT ChannelMojo : public Channel {
52 public:
53 // Create ChannelMojo on top of given |bootstrap| channel.
54 static scoped_ptr<ChannelMojo> Create(
55 scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener,
56 scoped_refptr<base::TaskRunner> io_thread_task_runner);
58 // Create ChannelMojo. A bootstrap channel is created as well.
59 static scoped_ptr<ChannelMojo> Create(
60 const ChannelHandle &channel_handle, Mode mode, Listener* listener,
61 scoped_refptr<base::TaskRunner> io_thread_task_runner);
63 // Create a factory object for ChannelMojo.
64 // The factory is used to create Mojo-based ChannelProxy family.
65 static scoped_ptr<ChannelFactory> CreateFactory(
66 const ChannelHandle &channel_handle, Mode mode,
67 scoped_refptr<base::TaskRunner> io_thread_task_runner);
69 virtual ~ChannelMojo();
71 // Channel implementation
72 virtual bool Connect() OVERRIDE;
73 virtual void Close() OVERRIDE;
74 virtual bool Send(Message* message) OVERRIDE;
75 virtual base::ProcessId GetPeerPID() const OVERRIDE;
76 virtual base::ProcessId GetSelfPID() const OVERRIDE;
77 virtual ChannelHandle TakePipeHandle() OVERRIDE;
79 #if defined(OS_POSIX) && !defined(OS_NACL)
80 virtual int GetClientFileDescriptor() const OVERRIDE;
81 virtual int TakeClientFileDescriptor() OVERRIDE;
82 #endif // defined(OS_POSIX) && !defined(OS_NACL)
84 // Called from MessagePipeReader implementations
85 void OnMessageReceived(Message& message);
86 void OnConnected(mojo::ScopedMessagePipeHandle pipe);
87 void OnPipeClosed(internal::MessagePipeReader* reader);
88 void OnPipeError(internal::MessagePipeReader* reader);
89 void set_peer_pid(base::ProcessId pid) { peer_pid_ = pid; }
91 private:
92 struct ChannelInfoDeleter {
93 void operator()(mojo::embedder::ChannelInfo* ptr) const;
96 // ChannelMojo needs to kill its MessagePipeReader in delayed manner
97 // because the channel wants to kill these readers during the
98 // notifications invoked by them.
99 typedef internal::MessagePipeReader::DelayedDeleter ReaderDeleter;
101 class ControlReader;
102 class ServerControlReader;
103 class ClientControlReader;
104 class MessageReader;
106 ChannelMojo(scoped_ptr<Channel> bootstrap, Mode mode, Listener* listener,
107 scoped_refptr<base::TaskRunner> io_thread_task_runner);
109 void InitOnIOThread();
111 scoped_ptr<Channel> bootstrap_;
112 Mode mode_;
113 Listener* listener_;
114 base::ProcessId peer_pid_;
115 scoped_ptr<mojo::embedder::ChannelInfo,
116 ChannelInfoDeleter> channel_info_;
118 scoped_ptr<ControlReader, ReaderDeleter> control_reader_;
119 scoped_ptr<MessageReader, ReaderDeleter> message_reader_;
120 ScopedVector<Message> pending_messages_;
122 base::WeakPtrFactory<ChannelMojo> weak_factory_;
124 DISALLOW_COPY_AND_ASSIGN(ChannelMojo);
127 } // namespace IPC
129 #endif // IPC_IPC_CHANNEL_MOJO_H_