OLD | NEW |
---|---|
(Empty) | |
1 // Copyright 2012 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 BASE_MESSAGE_PUMP_IO_IOS_H_ | |
6 #define BASE_MESSAGE_PUMP_IO_IOS_H_ | |
7 | |
8 #include "base/mac/scoped_cffiledescriptorref.h" | |
9 #include "base/mac/scoped_cftyperef.h" | |
10 #include "base/message_pump_mac.h" | |
11 #include "base/observer_list.h" | |
12 | |
13 namespace base { | |
14 | |
15 // This file introduces a class to monitor sockets and issue callbacks when | |
16 // sockets are ready for I/O on iOS. | |
17 class MessagePumpIOSForIO : public MessagePumpNSRunLoop { | |
Mark Mentovai
2012/11/20 14:47:58
Is there a reason you used the NSRunLoop flavor an
blundell
2012/11/20 20:38:54
When we were originally bringing up this code, we
wtc
2012/11/20 21:04:09
I remember I tracked that down and discussed the p
| |
18 public: | |
19 class IOObserver { | |
20 public: | |
21 IOObserver() {} | |
22 | |
23 // An IOObserver is an object that receives IO notifications from the | |
24 // MessagePump. | |
25 // | |
26 // NOTE: An IOObserver implementation should be extremely fast! | |
27 virtual void WillProcessIOEvent() = 0; | |
28 virtual void DidProcessIOEvent() = 0; | |
29 | |
30 protected: | |
31 virtual ~IOObserver() {} | |
32 }; | |
33 | |
34 // Used with WatchFileDescptor to asynchronously monitor the I/O readiness of | |
35 // a File Descriptor. | |
36 class Watcher { | |
37 public: | |
38 virtual ~Watcher() {} | |
39 | |
40 // Called from MessageLoop::Run when an FD can be read from/written to | |
41 // without blocking | |
42 virtual void OnFileCanReadWithoutBlocking(int fd) = 0; | |
43 virtual void OnFileCanWriteWithoutBlocking(int fd) = 0; | |
44 }; | |
45 | |
46 // Object returned by WatchFileDescriptor to manage further watching. | |
47 class FileDescriptorWatcher { | |
48 public: | |
49 FileDescriptorWatcher(); | |
50 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor. | |
51 | |
52 // NOTE: These methods aren't called StartWatching()/StopWatching() to | |
53 // avoid confusion with the win32 ObjectWatcher class. | |
54 | |
55 // Stop watching the FD, always safe to call. No-op if there's nothing | |
56 // to do. | |
57 bool StopWatchingFileDescriptor(); | |
58 | |
59 private: | |
60 friend class MessagePumpIOSForIO; | |
61 | |
62 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| | |
63 // is transferred to this object. | |
64 void Init(CFFileDescriptorRef fdref, | |
65 CFOptionFlags callback_types, | |
66 CFRunLoopSourceRef fd_source, | |
67 bool is_persistent); | |
68 | |
69 void set_pump(MessagePumpIOSForIO* pump) { pump_ = pump; } | |
70 MessagePumpIOSForIO* pump() const { return pump_; } | |
71 | |
72 void set_watcher(Watcher* watcher) { watcher_ = watcher; } | |
73 | |
74 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | |
75 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | |
76 | |
77 bool is_persistent_; // false if this event is one-shot. | |
78 base::mac::ScopedCFFileDescriptorRef fdref_; | |
79 CFOptionFlags callback_types_; | |
80 base::mac::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; | |
81 MessagePumpIOSForIO* pump_; | |
82 Watcher* watcher_; | |
83 | |
84 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | |
85 }; | |
86 | |
87 enum Mode { | |
88 WATCH_READ = 1 << 0, | |
89 WATCH_WRITE = 1 << 1, | |
90 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE | |
Mark Mentovai
2012/11/20 14:47:58
I don’t know that this needs to be distinct here.
blundell
2012/11/20 20:38:54
Done. I noticed that the same argument could be ap
| |
91 }; | |
92 | |
93 MessagePumpIOSForIO(); | |
94 | |
95 // Have the current thread's message loop watch for a a situation in which | |
96 // reading/writing to the FD can be performed without blocking. | |
97 // Callers must provide a preallocated FileDescriptorWatcher object which | |
98 // can later be used to manage the lifetime of this event. | |
99 // If a FileDescriptorWatcher is passed in which is already attached to | |
100 // an event, then the effect is cumulative i.e. after the call |controller| | |
101 // will watch both the previous event and the new one. | |
102 // If an error occurs while calling this method in a cumulative fashion, the | |
103 // event previously attached to |controller| is aborted. | |
104 // Returns true on success. | |
105 // Must be called on the same thread the message_pump is running on. | |
106 bool WatchFileDescriptor(int fd, | |
107 bool persistent, | |
108 Mode mode, | |
109 FileDescriptorWatcher *controller, | |
110 Watcher *delegate); | |
111 | |
112 void RemoveRunLoopSource(CFRunLoopSourceRef source); | |
113 | |
114 void AddIOObserver(IOObserver* obs); | |
115 void RemoveIOObserver(IOObserver* obs); | |
116 | |
117 protected: | |
118 virtual ~MessagePumpIOSForIO(); | |
119 | |
120 private: | |
121 void WillProcessIOEvent(); | |
122 void DidProcessIOEvent(); | |
123 | |
124 static void HandleFdIOEvent(CFFileDescriptorRef fdref, | |
125 CFOptionFlags callback_types, | |
126 void* context); | |
127 | |
128 ObserverList<IOObserver> io_observers_; | |
129 ThreadChecker watch_file_descriptor_caller_checker_; | |
130 | |
131 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO); | |
132 }; | |
133 | |
134 } // namespace base | |
135 | |
136 #endif // BASE_MESSAGE_PUMP_IO_IOS_H_ | |
OLD | NEW |