1 // Copyright 2013 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 MOJO_SYSTEM_RAW_CHANNEL_H_
6 #define MOJO_SYSTEM_RAW_CHANNEL_H_
10 #include "base/basictypes.h"
11 #include "mojo/public/system/system_export.h"
12 #include "mojo/system/limits.h"
21 class MessageInTransit;
23 // This simply wraps an |int| file descriptor on POSIX and a |HANDLE| on
24 // Windows, but we don't want to impose, e.g., the inclusion of windows.h on
26 struct PlatformChannelHandle;
28 // |RawChannel| is an interface to objects that wrap an OS "pipe". It presents
29 // the following interface to users:
30 // - Receives and dispatches messages on a thread (running a |MessageLoop|; it
31 // must be a |MessageLoopForIO| in the case of the POSIX libevent
33 // - Provides a thread-safe way of writing messages (|WriteMessage()|);
34 // writing/queueing messages will not block and is atomic from the point of
35 // view of the caller. If necessary, messages are queued (to be written on
36 // the aforementioned thread).
38 // OS-specific implementation subclasses are to be instantiated using the
39 // |Create()| static factory method.
41 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in
42 // general its methods should only be used on the I/O thread).
43 class MOJO_SYSTEM_EXPORT RawChannel {
45 virtual ~RawChannel() {}
47 // The |Delegate| is only accessed on the same thread as the message loop
48 // (passed in on creation).
52 FATAL_ERROR_UNKNOWN = 0,
53 FATAL_ERROR_FAILED_READ,
54 FATAL_ERROR_FAILED_WRITE
57 // Called when a message is read. This may call |Shutdown()| on the
58 // |RawChannel|, but must not destroy it.
59 virtual void OnReadMessage(const MessageInTransit& message) = 0;
61 // Called when there's a fatal error, which leads to the channel no longer
63 virtual void OnFatalError(FatalError fatal_error) = 0;
66 virtual ~Delegate() {}
69 // Static factory method. Takes ownership of |handle| (i.e., will close it).
70 // Does *not* take ownership of |delegate| and |message_loop|, which must
71 // remain alive while this object does.
72 static RawChannel* Create(const PlatformChannelHandle& handle,
74 base::MessageLoop* message_loop);
76 // This must be called (on the I/O thread) before this object is used.
77 virtual void Init() = 0;
79 // This must be called (on the I/O thread) before this object is destroyed.
80 virtual void Shutdown() = 0;
82 // This is thread-safe. It takes ownership of |message| (always, even on
83 // failure). Returns true on success.
84 virtual bool WriteMessage(MessageInTransit* message) = 0;
87 RawChannel(Delegate* delegate, base::MessageLoop* message_loop)
88 : delegate_(delegate), message_loop_(message_loop) {}
90 Delegate* delegate() { return delegate_; }
91 base::MessageLoop* message_loop() { return message_loop_; }
94 Delegate* const delegate_;
95 base::MessageLoop* const message_loop_;
97 DISALLOW_COPY_AND_ASSIGN(RawChannel);
100 } // namespace system
103 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_