Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(480)

Side by Side Diff: base/message_loop/message_pump_mac.h

Issue 19661004: Made MessagePump a non-thread safe class. (Closed) Base URL: svn://svn.chromium.org/chrome/trunk/src
Patch Set: Adding a missing header. Created 7 years, 5 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View unified diff | Download patch | Annotate | Revision Log
OLDNEW
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. 1 // Copyright (c) 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 // The basis for all native run loops on the Mac is the CFRunLoop. It can be 5 // The basis for all native run loops on the Mac is the CFRunLoop. It can be
6 // used directly, it can be used as the driving force behind the similar 6 // used directly, it can be used as the driving force behind the similar
7 // Foundation NSRunLoop, and it can be used to implement higher-level event 7 // Foundation NSRunLoop, and it can be used to implement higher-level event
8 // loops such as the NSApplication event loop. 8 // loops such as the NSApplication event loop.
9 // 9 //
10 // This file introduces a basic CFRunLoop-based implementation of the 10 // This file introduces a basic CFRunLoop-based implementation of the
(...skipping 14 matching lines...) Expand all
25 // any other thread, one of the other concrete subclasses is preferrable. 25 // any other thread, one of the other concrete subclasses is preferrable.
26 // MessagePumpMac::Create is defined, which returns a new NSApplication-based 26 // MessagePumpMac::Create is defined, which returns a new NSApplication-based
27 // or NSRunLoop-based MessagePump subclass depending on which thread it is 27 // or NSRunLoop-based MessagePump subclass depending on which thread it is
28 // called on. 28 // called on.
29 29
30 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ 30 #ifndef BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_
31 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ 31 #define BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_
32 32
33 #include "base/message_loop/message_pump.h" 33 #include "base/message_loop/message_pump.h"
34 34
35 #include "base/basictypes.h"
36
35 #include <CoreFoundation/CoreFoundation.h> 37 #include <CoreFoundation/CoreFoundation.h>
36 38
37 #if !defined(__OBJC__) 39 #if !defined(__OBJC__)
38 class NSAutoreleasePool; 40 class NSAutoreleasePool;
39 #else // !defined(__OBJC__) 41 #else // !defined(__OBJC__)
40 #if defined(OS_IOS) 42 #if defined(OS_IOS)
41 #import <Foundation/Foundation.h> 43 #import <Foundation/Foundation.h>
42 #else 44 #else
43 #import <AppKit/AppKit.h> 45 #import <AppKit/AppKit.h>
44 46
(...skipping 11 matching lines...) Expand all
56 namespace base { 58 namespace base {
57 59
58 class RunLoop; 60 class RunLoop;
59 class TimeTicks; 61 class TimeTicks;
60 62
61 class MessagePumpCFRunLoopBase : public MessagePump { 63 class MessagePumpCFRunLoopBase : public MessagePump {
62 // Needs access to CreateAutoreleasePool. 64 // Needs access to CreateAutoreleasePool.
63 friend class MessagePumpScopedAutoreleasePool; 65 friend class MessagePumpScopedAutoreleasePool;
64 public: 66 public:
65 MessagePumpCFRunLoopBase(); 67 MessagePumpCFRunLoopBase();
68 virtual ~MessagePumpCFRunLoopBase();
66 69
67 // Subclasses should implement the work they need to do in MessagePump::Run 70 // Subclasses should implement the work they need to do in MessagePump::Run
68 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly. 71 // in the DoRun method. MessagePumpCFRunLoopBase::Run calls DoRun directly.
69 // This arrangement is used because MessagePumpCFRunLoopBase needs to set 72 // This arrangement is used because MessagePumpCFRunLoopBase needs to set
70 // up and tear down things before and after the "meat" of DoRun. 73 // up and tear down things before and after the "meat" of DoRun.
71 virtual void Run(Delegate* delegate) OVERRIDE; 74 virtual void Run(Delegate* delegate) OVERRIDE;
72 virtual void DoRun(Delegate* delegate) = 0; 75 virtual void DoRun(Delegate* delegate) = 0;
73 76
74 virtual void ScheduleWork() OVERRIDE; 77 virtual void ScheduleWork() OVERRIDE;
75 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE; 78 virtual void ScheduleDelayedWork(const TimeTicks& delayed_work_time) OVERRIDE;
76 79
77 protected: 80 protected:
78 virtual ~MessagePumpCFRunLoopBase();
79
80 // Accessors for private data members to be used by subclasses. 81 // Accessors for private data members to be used by subclasses.
81 CFRunLoopRef run_loop() const { return run_loop_; } 82 CFRunLoopRef run_loop() const { return run_loop_; }
82 int nesting_level() const { return nesting_level_; } 83 int nesting_level() const { return nesting_level_; }
83 int run_nesting_level() const { return run_nesting_level_; } 84 int run_nesting_level() const { return run_nesting_level_; }
84 85
85 // Sets this pump's delegate. Signals the appropriate sources if 86 // Sets this pump's delegate. Signals the appropriate sources if
86 // |delegateless_work_| is true. |delegate| can be NULL. 87 // |delegateless_work_| is true. |delegate| can be NULL.
87 void SetDelegate(Delegate* delegate); 88 void SetDelegate(Delegate* delegate);
88 89
89 // Return an autorelease pool to wrap around any work being performed. 90 // Return an autorelease pool to wrap around any work being performed.
(...skipping 102 matching lines...) Expand 10 before | Expand all | Expand 10 after
192 // work on entry and redispatch it as needed once a delegate is available. 193 // work on entry and redispatch it as needed once a delegate is available.
193 bool delegateless_work_; 194 bool delegateless_work_;
194 bool delegateless_idle_work_; 195 bool delegateless_idle_work_;
195 196
196 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase); 197 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoopBase);
197 }; 198 };
198 199
199 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase { 200 class MessagePumpCFRunLoop : public MessagePumpCFRunLoopBase {
200 public: 201 public:
201 MessagePumpCFRunLoop(); 202 MessagePumpCFRunLoop();
203 virtual ~MessagePumpCFRunLoop();
202 204
203 virtual void DoRun(Delegate* delegate) OVERRIDE; 205 virtual void DoRun(Delegate* delegate) OVERRIDE;
204 virtual void Quit() OVERRIDE; 206 virtual void Quit() OVERRIDE;
205 207
206 protected:
207 virtual ~MessagePumpCFRunLoop();
208
209 private: 208 private:
210 virtual void EnterExitRunLoop(CFRunLoopActivity activity) OVERRIDE; 209 virtual void EnterExitRunLoop(CFRunLoopActivity activity) OVERRIDE;
211 210
212 // True if Quit is called to stop the innermost MessagePump 211 // True if Quit is called to stop the innermost MessagePump
213 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_) 212 // (innermost_quittable_) but some other CFRunLoopRun loop (nesting_level_)
214 // is running inside the MessagePump's innermost Run call. 213 // is running inside the MessagePump's innermost Run call.
215 bool quit_pending_; 214 bool quit_pending_;
216 215
217 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop); 216 DISALLOW_COPY_AND_ASSIGN(MessagePumpCFRunLoop);
218 }; 217 };
219 218
220 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase { 219 class MessagePumpNSRunLoop : public MessagePumpCFRunLoopBase {
221 public: 220 public:
222 BASE_EXPORT MessagePumpNSRunLoop(); 221 BASE_EXPORT MessagePumpNSRunLoop();
222 virtual ~MessagePumpNSRunLoop();
223 223
224 virtual void DoRun(Delegate* delegate) OVERRIDE; 224 virtual void DoRun(Delegate* delegate) OVERRIDE;
225 virtual void Quit() OVERRIDE; 225 virtual void Quit() OVERRIDE;
226 226
227 protected:
228 virtual ~MessagePumpNSRunLoop();
229
230 private: 227 private:
231 // A source that doesn't do anything but provide something signalable 228 // A source that doesn't do anything but provide something signalable
232 // attached to the run loop. This source will be signalled when Quit 229 // attached to the run loop. This source will be signalled when Quit
233 // is called, to cause the loop to wake up so that it can stop. 230 // is called, to cause the loop to wake up so that it can stop.
234 CFRunLoopSourceRef quit_source_; 231 CFRunLoopSourceRef quit_source_;
235 232
236 // False after Quit is called. 233 // False after Quit is called.
237 bool keep_running_; 234 bool keep_running_;
238 235
239 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop); 236 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSRunLoop);
240 }; 237 };
241 238
242 #if defined(OS_IOS) 239 #if defined(OS_IOS)
243 // This is a fake message pump. It attaches sources to the main thread's 240 // This is a fake message pump. It attaches sources to the main thread's
244 // CFRunLoop, so PostTask() will work, but it is unable to drive the loop 241 // CFRunLoop, so PostTask() will work, but it is unable to drive the loop
245 // directly, so calling Run() or Quit() are errors. 242 // directly, so calling Run() or Quit() are errors.
246 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase { 243 class MessagePumpUIApplication : public MessagePumpCFRunLoopBase {
247 public: 244 public:
248 MessagePumpUIApplication(); 245 MessagePumpUIApplication();
246 virtual ~MessagePumpUIApplication();
249 virtual void DoRun(Delegate* delegate) OVERRIDE; 247 virtual void DoRun(Delegate* delegate) OVERRIDE;
250 virtual void Quit() OVERRIDE; 248 virtual void Quit() OVERRIDE;
251 249
252 // This message pump can not spin the main message loop directly. Instead, 250 // This message pump can not spin the main message loop directly. Instead,
253 // call |Attach()| to set up a delegate. It is an error to call |Run()|. 251 // call |Attach()| to set up a delegate. It is an error to call |Run()|.
254 virtual void Attach(Delegate* delegate); 252 virtual void Attach(Delegate* delegate);
255 253
256 protected:
257 virtual ~MessagePumpUIApplication();
258
259 private: 254 private:
260 RunLoop* run_loop_; 255 RunLoop* run_loop_;
261 256
262 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication); 257 DISALLOW_COPY_AND_ASSIGN(MessagePumpUIApplication);
263 }; 258 };
264 259
265 #else 260 #else
266 261
267 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase { 262 class MessagePumpNSApplication : public MessagePumpCFRunLoopBase {
268 public: 263 public:
269 MessagePumpNSApplication(); 264 MessagePumpNSApplication();
265 virtual ~MessagePumpNSApplication();
270 266
271 virtual void DoRun(Delegate* delegate) OVERRIDE; 267 virtual void DoRun(Delegate* delegate) OVERRIDE;
272 virtual void Quit() OVERRIDE; 268 virtual void Quit() OVERRIDE;
273 269
274 protected:
275 virtual ~MessagePumpNSApplication();
276
277 private: 270 private:
278 // False after Quit is called. 271 // False after Quit is called.
279 bool keep_running_; 272 bool keep_running_;
280 273
281 // True if DoRun is managing its own run loop as opposed to letting 274 // True if DoRun is managing its own run loop as opposed to letting
282 // -[NSApplication run] handle it. The outermost run loop in the application 275 // -[NSApplication run] handle it. The outermost run loop in the application
283 // is managed by -[NSApplication run], inner run loops are handled by a loop 276 // is managed by -[NSApplication run], inner run loops are handled by a loop
284 // in DoRun. 277 // in DoRun.
285 bool running_own_loop_; 278 bool running_own_loop_;
286 279
287 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication); 280 DISALLOW_COPY_AND_ASSIGN(MessagePumpNSApplication);
288 }; 281 };
289 282
290 class MessagePumpCrApplication : public MessagePumpNSApplication { 283 class MessagePumpCrApplication : public MessagePumpNSApplication {
291 public: 284 public:
292 MessagePumpCrApplication(); 285 MessagePumpCrApplication();
286 virtual ~MessagePumpCrApplication();
293 287
294 protected: 288 protected:
295 virtual ~MessagePumpCrApplication() {}
296
297 // Returns nil if NSApp is currently in the middle of calling 289 // Returns nil if NSApp is currently in the middle of calling
298 // -sendEvent. Requires NSApp implementing CrAppProtocol. 290 // -sendEvent. Requires NSApp implementing CrAppProtocol.
299 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE; 291 virtual NSAutoreleasePool* CreateAutoreleasePool() OVERRIDE;
300 292
301 private: 293 private:
302 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication); 294 DISALLOW_COPY_AND_ASSIGN(MessagePumpCrApplication);
303 }; 295 };
304 #endif // !defined(OS_IOS) 296 #endif // !defined(OS_IOS)
305 297
306 class MessagePumpMac { 298 class MessagePumpMac {
(...skipping 21 matching lines...) Expand all
328 BASE_EXPORT static bool IsHandlingSendEvent(); 320 BASE_EXPORT static bool IsHandlingSendEvent();
329 #endif // !defined(OS_IOS) 321 #endif // !defined(OS_IOS)
330 322
331 private: 323 private:
332 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac); 324 DISALLOW_IMPLICIT_CONSTRUCTORS(MessagePumpMac);
333 }; 325 };
334 326
335 } // namespace base 327 } // namespace base
336 328
337 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_ 329 #endif // BASE_MESSAGE_LOOP_MESSAGE_PUMP_MAC_H_
OLDNEW
« no previous file with comments | « base/message_loop/message_pump_libevent_unittest.cc ('k') | base/message_loop/message_pump_mac.mm » ('j') | no next file with comments »

Powered by Google App Engine
This is Rietveld 408576698