xref: /aosp_15_r20/external/cronet/base/mac/scoped_sending_event.h (revision 6777b5387eb2ff775bb5750e3f5d96f37fb7352b)
1 // Copyright 2012 The Chromium Authors
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_MAC_SCOPED_SENDING_EVENT_H_
6 #define BASE_MAC_SCOPED_SENDING_EVENT_H_
7 
8 #include "base/base_export.h"
9 #include "base/message_loop/message_pump_apple.h"
10 
11 // Nested event loops can pump IPC messages, including
12 // script-initiated tab closes, which could release objects that the
13 // nested event loop might message.  CrAppProtocol defines how to ask
14 // the embedding NSApplication subclass if an event is currently being
15 // handled, in which case such closes are deferred to the top-level
16 // event loop.
17 //
18 // ScopedSendingEvent allows script-initiated event loops to work like
19 // a nested event loop, as such events do not arrive via -sendEvent:.
20 // CrAppControlProtocol lets ScopedSendingEvent tell the embedding
21 // NSApplication what to return from -handlingSendEvent.
22 
23 @protocol CrAppControlProtocol<CrAppProtocol>
24 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent;
25 @end
26 
27 namespace base::mac {
28 
29 class BASE_EXPORT ScopedSendingEvent {
30  public:
31   ScopedSendingEvent();
32 
33   ScopedSendingEvent(const ScopedSendingEvent&) = delete;
34   ScopedSendingEvent& operator=(const ScopedSendingEvent&) = delete;
35 
36   ~ScopedSendingEvent();
37 
38  private:
39   // The NSApp in control at the time the constructor was run, to be
40   // sure the |handling_| setting is restored appropriately.
41   NSObject<CrAppControlProtocol>* app_;
42   BOOL handling_;  // Value of -[app_ handlingSendEvent] at construction.
43 };
44 
45 }  // namespace base::mac
46 
47 #endif  // BASE_MAC_SCOPED_SENDING_EVENT_H_
48