OLD | NEW |
1 // Copyright 2012 The Chromium Authors. All rights reserved. | 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 | 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 BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ | 5 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ |
6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ | 6 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ |
7 | 7 |
8 #include "base/base_export.h" | 8 #include "base/base_export.h" |
9 #include "base/mac/scoped_cffiledescriptorref.h" | 9 #include "base/mac/scoped_cffiledescriptorref.h" |
10 #include "base/mac/scoped_cftyperef.h" | 10 #include "base/mac/scoped_cftyperef.h" |
11 #include "base/memory/ref_counted.h" | 11 #include "base/memory/ref_counted.h" |
| 12 #include "base/memory/weak_ptr.h" |
12 #include "base/message_loop/message_pump_mac.h" | 13 #include "base/message_loop/message_pump_mac.h" |
13 #include "base/observer_list.h" | 14 #include "base/observer_list.h" |
14 | 15 |
15 namespace base { | 16 namespace base { |
16 | 17 |
17 // This file introduces a class to monitor sockets and issue callbacks when | 18 // This file introduces a class to monitor sockets and issue callbacks when |
18 // sockets are ready for I/O on iOS. | 19 // sockets are ready for I/O on iOS. |
19 class BASE_EXPORT MessagePumpIOSForIO : public MessagePumpNSRunLoop { | 20 class BASE_EXPORT MessagePumpIOSForIO : public MessagePumpNSRunLoop { |
20 public: | 21 public: |
21 class IOObserver { | 22 class IOObserver { |
(...skipping 41 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
63 friend class MessagePumpIOSForIO; | 64 friend class MessagePumpIOSForIO; |
64 friend class MessagePumpIOSForIOTest; | 65 friend class MessagePumpIOSForIOTest; |
65 | 66 |
66 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| | 67 // Called by MessagePumpIOSForIO, ownership of |fdref| and |fd_source| |
67 // is transferred to this object. | 68 // is transferred to this object. |
68 void Init(CFFileDescriptorRef fdref, | 69 void Init(CFFileDescriptorRef fdref, |
69 CFOptionFlags callback_types, | 70 CFOptionFlags callback_types, |
70 CFRunLoopSourceRef fd_source, | 71 CFRunLoopSourceRef fd_source, |
71 bool is_persistent); | 72 bool is_persistent); |
72 | 73 |
73 void set_pump(MessagePumpIOSForIO* pump) { pump_ = pump; } | 74 void set_pump(base::WeakPtr<MessagePumpIOSForIO> pump) { pump_ = pump; } |
74 MessagePumpIOSForIO* pump() const { return pump_; } | 75 const base::WeakPtr<MessagePumpIOSForIO>& pump() const { return pump_; } |
75 | 76 |
76 void set_watcher(Watcher* watcher) { watcher_ = watcher; } | 77 void set_watcher(Watcher* watcher) { watcher_ = watcher; } |
77 | 78 |
78 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | 79 void OnFileCanReadWithoutBlocking(int fd, MessagePumpIOSForIO* pump); |
79 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); | 80 void OnFileCanWriteWithoutBlocking(int fd, MessagePumpIOSForIO* pump); |
80 | 81 |
81 bool is_persistent_; // false if this event is one-shot. | 82 bool is_persistent_; // false if this event is one-shot. |
82 base::mac::ScopedCFFileDescriptorRef fdref_; | 83 base::mac::ScopedCFFileDescriptorRef fdref_; |
83 CFOptionFlags callback_types_; | 84 CFOptionFlags callback_types_; |
84 base::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; | 85 base::ScopedCFTypeRef<CFRunLoopSourceRef> fd_source_; |
85 scoped_refptr<MessagePumpIOSForIO> pump_; | 86 base::WeakPtr<MessagePumpIOSForIO> pump_; |
86 Watcher* watcher_; | 87 Watcher* watcher_; |
87 | 88 |
88 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); | 89 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher); |
89 }; | 90 }; |
90 | 91 |
91 enum Mode { | 92 enum Mode { |
92 WATCH_READ = 1 << 0, | 93 WATCH_READ = 1 << 0, |
93 WATCH_WRITE = 1 << 1, | 94 WATCH_WRITE = 1 << 1, |
94 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE | 95 WATCH_READ_WRITE = WATCH_READ | WATCH_WRITE |
95 }; | 96 }; |
96 | 97 |
97 MessagePumpIOSForIO(); | 98 MessagePumpIOSForIO(); |
| 99 virtual ~MessagePumpIOSForIO(); |
98 | 100 |
99 // Have the current thread's message loop watch for a a situation in which | 101 // 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. | 102 // reading/writing to the FD can be performed without blocking. |
101 // Callers must provide a preallocated FileDescriptorWatcher object which | 103 // Callers must provide a preallocated FileDescriptorWatcher object which |
102 // can later be used to manage the lifetime of this event. | 104 // can later be used to manage the lifetime of this event. |
103 // If a FileDescriptorWatcher is passed in which is already attached to | 105 // 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| | 106 // an event, then the effect is cumulative i.e. after the call |controller| |
105 // will watch both the previous event and the new one. | 107 // will watch both the previous event and the new one. |
106 // If an error occurs while calling this method in a cumulative fashion, the | 108 // If an error occurs while calling this method in a cumulative fashion, the |
107 // event previously attached to |controller| is aborted. | 109 // event previously attached to |controller| is aborted. |
108 // Returns true on success. | 110 // Returns true on success. |
109 // Must be called on the same thread the message_pump is running on. | 111 // Must be called on the same thread the message_pump is running on. |
110 bool WatchFileDescriptor(int fd, | 112 bool WatchFileDescriptor(int fd, |
111 bool persistent, | 113 bool persistent, |
112 int mode, | 114 int mode, |
113 FileDescriptorWatcher *controller, | 115 FileDescriptorWatcher *controller, |
114 Watcher *delegate); | 116 Watcher *delegate); |
115 | 117 |
116 void RemoveRunLoopSource(CFRunLoopSourceRef source); | 118 void RemoveRunLoopSource(CFRunLoopSourceRef source); |
117 | 119 |
118 void AddIOObserver(IOObserver* obs); | 120 void AddIOObserver(IOObserver* obs); |
119 void RemoveIOObserver(IOObserver* obs); | 121 void RemoveIOObserver(IOObserver* obs); |
120 | 122 |
121 protected: | |
122 virtual ~MessagePumpIOSForIO(); | |
123 | |
124 private: | 123 private: |
125 friend class MessagePumpIOSForIOTest; | 124 friend class MessagePumpIOSForIOTest; |
126 | 125 |
127 void WillProcessIOEvent(); | 126 void WillProcessIOEvent(); |
128 void DidProcessIOEvent(); | 127 void DidProcessIOEvent(); |
129 | 128 |
130 static void HandleFdIOEvent(CFFileDescriptorRef fdref, | 129 static void HandleFdIOEvent(CFFileDescriptorRef fdref, |
131 CFOptionFlags callback_types, | 130 CFOptionFlags callback_types, |
132 void* context); | 131 void* context); |
133 | 132 |
134 ObserverList<IOObserver> io_observers_; | 133 ObserverList<IOObserver> io_observers_; |
135 ThreadChecker watch_file_descriptor_caller_checker_; | 134 ThreadChecker watch_file_descriptor_caller_checker_; |
136 | 135 |
| 136 base::WeakPtrFactory<MessagePumpIOSForIO> weak_factory_; |
| 137 |
137 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO); | 138 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO); |
138 }; | 139 }; |
139 | 140 |
140 } // namespace base | 141 } // namespace base |
141 | 142 |
142 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ | 143 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_IO_IOS_H_ |
OLD | NEW |