OLD | NEW |
1 // Copyright 2013 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #ifndef MOJO_SYSTEM_RAW_CHANNEL_H_ | 5 #ifndef MOJO_SYSTEM_RAW_CHANNEL_H_ |
6 #define MOJO_SYSTEM_RAW_CHANNEL_H_ | 6 #define MOJO_SYSTEM_RAW_CHANNEL_H_ |
7 | 7 |
8 #include <vector> | 8 #include <vector> |
9 | 9 |
10 #include "base/basictypes.h" | 10 #include "base/basictypes.h" |
(...skipping 28 matching lines...) Expand all Loading... |
39 // |Create()| static factory method. | 39 // |Create()| static factory method. |
40 // | 40 // |
41 // With the exception of |WriteMessage()|, this class is thread-unsafe (and in | 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). | 42 // general its methods should only be used on the I/O thread). |
43 class MOJO_SYSTEM_EXPORT RawChannel { | 43 class MOJO_SYSTEM_EXPORT RawChannel { |
44 public: | 44 public: |
45 virtual ~RawChannel() {} | 45 virtual ~RawChannel() {} |
46 | 46 |
47 // The |Delegate| is only accessed on the same thread as the message loop | 47 // The |Delegate| is only accessed on the same thread as the message loop |
48 // (passed in on creation). | 48 // (passed in on creation). |
49 class Delegate { | 49 class MOJO_SYSTEM_EXPORT Delegate { |
50 public: | 50 public: |
51 enum FatalError { | 51 enum FatalError { |
52 FATAL_ERROR_UNKNOWN = 0, | 52 FATAL_ERROR_UNKNOWN = 0, |
53 FATAL_ERROR_FAILED_READ, | 53 FATAL_ERROR_FAILED_READ, |
54 FATAL_ERROR_FAILED_WRITE | 54 FATAL_ERROR_FAILED_WRITE |
55 }; | 55 }; |
56 | 56 |
57 // Called when a message is read. This may call |Shutdown()| on the | 57 // Called when a message is read. This may call |Shutdown()| on the |
58 // |RawChannel|, but must not destroy it. | 58 // |RawChannel|, but must not destroy it. |
59 virtual void OnReadMessage(const MessageInTransit& message) = 0; | 59 virtual void OnReadMessage(const MessageInTransit& message) = 0; |
60 | 60 |
61 // Called when there's a fatal error, which leads to the channel no longer | 61 // Called when there's a fatal error, which leads to the channel no longer |
62 // being viable. | 62 // being viable. |
63 virtual void OnFatalError(FatalError fatal_error) = 0; | 63 virtual void OnFatalError(FatalError fatal_error) = 0; |
64 | 64 |
65 protected: | 65 protected: |
66 virtual ~Delegate() {} | 66 virtual ~Delegate() {} |
67 }; | 67 }; |
68 | 68 |
69 // Static factory method. Takes ownership of |handle| (i.e., will close it). | 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 | 70 // Does *not* take ownership of |delegate| and |message_loop|, which must |
71 // remain alive while this object does. | 71 // remain alive while this object does. |
72 static RawChannel* Create(const PlatformChannelHandle& handle, | 72 static RawChannel* Create(const PlatformChannelHandle& handle, |
73 Delegate* delegate, | 73 Delegate* delegate, |
74 base::MessageLoop* message_loop); | 74 base::MessageLoop* message_loop); |
75 | 75 |
76 // This must be called (on the I/O thread) before this object is used. | 76 // This must be called (on the I/O thread) before this object is used. Returns |
77 virtual void Init() = 0; | 77 // true on success. On failure, |Shutdown()| should *not* be called. |
| 78 virtual bool Init() = 0; |
78 | 79 |
79 // This must be called (on the I/O thread) before this object is destroyed. | 80 // This must be called (on the I/O thread) before this object is destroyed. |
80 virtual void Shutdown() = 0; | 81 virtual void Shutdown() = 0; |
81 | 82 |
82 // This is thread-safe. It takes ownership of |message| (always, even on | 83 // This is thread-safe. It takes ownership of |message| (always, even on |
83 // failure). Returns true on success. | 84 // failure). Returns true on success. |
84 virtual bool WriteMessage(MessageInTransit* message) = 0; | 85 virtual bool WriteMessage(MessageInTransit* message) = 0; |
85 | 86 |
86 protected: | 87 protected: |
87 RawChannel(Delegate* delegate, base::MessageLoop* message_loop) | 88 RawChannel(Delegate* delegate, base::MessageLoop* message_loop) |
88 : delegate_(delegate), message_loop_(message_loop) {} | 89 : delegate_(delegate), message_loop_(message_loop) {} |
89 | 90 |
90 Delegate* delegate() { return delegate_; } | 91 Delegate* delegate() { return delegate_; } |
91 base::MessageLoop* message_loop() { return message_loop_; } | 92 base::MessageLoop* message_loop() { return message_loop_; } |
92 | 93 |
93 private: | 94 private: |
94 Delegate* const delegate_; | 95 Delegate* const delegate_; |
95 base::MessageLoop* const message_loop_; | 96 base::MessageLoop* const message_loop_; |
96 | 97 |
97 DISALLOW_COPY_AND_ASSIGN(RawChannel); | 98 DISALLOW_COPY_AND_ASSIGN(RawChannel); |
98 }; | 99 }; |
99 | 100 |
100 } // namespace system | 101 } // namespace system |
101 } // namespace mojo | 102 } // namespace mojo |
102 | 103 |
103 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ | 104 #endif // MOJO_SYSTEM_RAW_CHANNEL_H_ |
OLD | NEW |