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.
5 #ifndef BASE_MESSAGE_PUMP_IO_IOS_H_
6 #define BASE_MESSAGE_PUMP_IO_IOS_H_
8 #include "base/base_export.h"
9 #include "base/mac/scoped_cffiledescriptorref.h"
10 #include "base/mac/scoped_cftyperef.h"
11 #include "base/memory/ref_counted.h"
12 #include "base/message_pump_mac.h"
13 #include "base/observer_list.h"
17 // This file introduces a class to monitor sockets and issue callbacks when
18 // sockets are ready for I/O on iOS.
19 class BASE_EXPORT MessagePumpIOSForIO
: public MessagePumpNSRunLoop
{
25 // An IOObserver is an object that receives IO notifications from the
28 // NOTE: An IOObserver implementation should be extremely fast!
29 virtual void WillProcessIOEvent() = 0;
30 virtual void DidProcessIOEvent() = 0;
33 virtual ~IOObserver() {}
36 // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
37 // of a file descriptor.
40 // Called from MessageLoop::Run when an FD can be read from/written to
42 virtual void OnFileCanReadWithoutBlocking(int fd
) = 0;
43 virtual void OnFileCanWriteWithoutBlocking(int fd
) = 0;
49 // Object returned by WatchFileDescriptor to manage further watching.
50 class FileDescriptorWatcher
{
52 FileDescriptorWatcher();
53 ~FileDescriptorWatcher(); // Implicitly calls StopWatchingFileDescriptor.
55 // NOTE: These methods aren't called StartWatching()/StopWatching() to
56 // avoid confusion with the win32 ObjectWatcher class.
58 // Stop watching the FD, always safe to call. No-op if there's nothing
60 bool StopWatchingFileDescriptor();
63 friend class MessagePumpIOSForIO
;
64 friend class MessagePumpIOSForIOTest
;
66 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source|
67 // is transferred to this object.
68 void Init(CFFileDescriptorRef fdref
,
69 CFOptionFlags callback_types
,
70 CFRunLoopSourceRef fd_source
,
73 void set_pump(MessagePumpIOSForIO
* pump
) { pump_
= pump
; }
74 MessagePumpIOSForIO
* pump() const { return pump_
; }
76 void set_watcher(Watcher
* watcher
) { watcher_
= watcher
; }
78 void OnFileCanReadWithoutBlocking(int fd
, MessagePumpIOSForIO
* pump
);
79 void OnFileCanWriteWithoutBlocking(int fd
, MessagePumpIOSForIO
* pump
);
81 bool is_persistent_
; // false if this event is one-shot.
82 base::mac::ScopedCFFileDescriptorRef fdref_
;
83 CFOptionFlags callback_types_
;
84 base::mac::ScopedCFTypeRef
<CFRunLoopSourceRef
> fd_source_
;
85 scoped_refptr
<MessagePumpIOSForIO
> pump_
;
88 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher
);
94 WATCH_READ_WRITE
= WATCH_READ
| WATCH_WRITE
97 MessagePumpIOSForIO();
99 // Have the current thread's message loop watch for a a situation in which
100 // reading/writing to the FD can be performed without blocking.
101 // Callers must provide a preallocated FileDescriptorWatcher object which
102 // can later be used to manage the lifetime of this event.
103 // If a FileDescriptorWatcher is passed in which is already attached to
104 // an event, then the effect is cumulative i.e. after the call |controller|
105 // will watch both the previous event and the new one.
106 // If an error occurs while calling this method in a cumulative fashion, the
107 // event previously attached to |controller| is aborted.
108 // Returns true on success.
109 // Must be called on the same thread the message_pump is running on.
110 bool WatchFileDescriptor(int fd
,
113 FileDescriptorWatcher
*controller
,
116 void RemoveRunLoopSource(CFRunLoopSourceRef source
);
118 void AddIOObserver(IOObserver
* obs
);
119 void RemoveIOObserver(IOObserver
* obs
);
122 virtual ~MessagePumpIOSForIO();
125 friend class MessagePumpIOSForIOTest
;
127 void WillProcessIOEvent();
128 void DidProcessIOEvent();
130 static void HandleFdIOEvent(CFFileDescriptorRef fdref
,
131 CFOptionFlags callback_types
,
134 ObserverList
<IOObserver
> io_observers_
;
135 ThreadChecker watch_file_descriptor_caller_checker_
;
137 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO
);
142 #endif // BASE_MESSAGE_PUMP_IO_IOS_H_