Fix crash when JS alert from background page appears during lock screen
[chromium-blink-merge.git] / base / message_pump_io_ios.h
blob81f529d5131f295c80b23780dd0a0909a1fbdcf5
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"
15 namespace base {
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 {
20 public:
21 class IOObserver {
22 public:
23 IOObserver() {}
25 // An IOObserver is an object that receives IO notifications from the
26 // MessagePump.
28 // NOTE: An IOObserver implementation should be extremely fast!
29 virtual void WillProcessIOEvent() = 0;
30 virtual void DidProcessIOEvent() = 0;
32 protected:
33 virtual ~IOObserver() {}
36 // Used with WatchFileDescriptor to asynchronously monitor the I/O readiness
37 // of a file descriptor.
38 class Watcher {
39 public:
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;
45 protected:
46 virtual ~Watcher() {}
49 // Object returned by WatchFileDescriptor to manage further watching.
50 class FileDescriptorWatcher {
51 public:
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
59 // to do.
60 bool StopWatchingFileDescriptor();
62 private:
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,
71 bool is_persistent);
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_;
86 Watcher* watcher_;
88 DISALLOW_COPY_AND_ASSIGN(FileDescriptorWatcher);
91 enum Mode {
92 WATCH_READ = 1 << 0,
93 WATCH_WRITE = 1 << 1,
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,
111 bool persistent,
112 int mode,
113 FileDescriptorWatcher *controller,
114 Watcher *delegate);
116 void RemoveRunLoopSource(CFRunLoopSourceRef source);
118 void AddIOObserver(IOObserver* obs);
119 void RemoveIOObserver(IOObserver* obs);
121 protected:
122 virtual ~MessagePumpIOSForIO();
124 private:
125 friend class MessagePumpIOSForIOTest;
127 void WillProcessIOEvent();
128 void DidProcessIOEvent();
130 static void HandleFdIOEvent(CFFileDescriptorRef fdref,
131 CFOptionFlags callback_types,
132 void* context);
134 ObserverList<IOObserver> io_observers_;
135 ThreadChecker watch_file_descriptor_caller_checker_;
137 DISALLOW_COPY_AND_ASSIGN(MessagePumpIOSForIO);
140 } // namespace base
142 #endif // BASE_MESSAGE_PUMP_IO_IOS_H_