OLD | NEW |
1 // Copyright (c) 2011 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2011 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 #import "base/mac/scoped_nsexception_enabler.h" | 5 #import "base/mac/scoped_nsexception_enabler.h" |
6 | 6 |
7 #import "base/lazy_instance.h" | 7 #import "base/lazy_instance.h" |
8 #import "base/threading/thread_local.h" | 8 #import "base/threading/thread_local.h" |
9 | 9 |
10 // To make the |g_exceptionsAllowed| declaration readable. | 10 // To make the |g_exceptionsAllowed| declaration readable. |
11 using base::LazyInstance; | 11 using base::LazyInstance; |
12 using base::LeakyLazyInstanceTraits; | 12 using base::LeakyLazyInstanceTraits; |
13 using base::ThreadLocalBoolean; | 13 using base::ThreadLocalBoolean; |
14 | 14 |
| 15 // When C++ exceptions are disabled, the C++ library defines |try| and |
| 16 // |catch| so as to allow exception-expecting C++ code to build properly when |
| 17 // language support for exceptions is not present. These macros interfere |
| 18 // with the use of |@try| and |@catch| in Objective-C files such as this one. |
| 19 // Undefine these macros here, after everything has been #included, since |
| 20 // there will be no C++ uses and only Objective-C uses from this point on. |
| 21 #undef try |
| 22 #undef catch |
| 23 |
15 namespace { | 24 namespace { |
16 | 25 |
17 // Whether to allow NSExceptions to be raised on the current thread. | 26 // Whether to allow NSExceptions to be raised on the current thread. |
18 LazyInstance<ThreadLocalBoolean, LeakyLazyInstanceTraits<ThreadLocalBoolean> > | 27 LazyInstance<ThreadLocalBoolean, LeakyLazyInstanceTraits<ThreadLocalBoolean> > |
19 g_exceptionsAllowed = LAZY_INSTANCE_INITIALIZER; | 28 g_exceptionsAllowed = LAZY_INSTANCE_INITIALIZER; |
20 | 29 |
21 } // namespace | 30 } // namespace |
22 | 31 |
23 namespace base { | 32 namespace base { |
24 namespace mac { | 33 namespace mac { |
25 | 34 |
26 bool GetNSExceptionsAllowed() { | 35 bool GetNSExceptionsAllowed() { |
27 return g_exceptionsAllowed.Get().Get(); | 36 return g_exceptionsAllowed.Get().Get(); |
28 } | 37 } |
29 | 38 |
30 void SetNSExceptionsAllowed(bool allowed) { | 39 void SetNSExceptionsAllowed(bool allowed) { |
31 return g_exceptionsAllowed.Get().Set(allowed); | 40 return g_exceptionsAllowed.Get().Set(allowed); |
32 } | 41 } |
33 | 42 |
| 43 id PerformSelectorIgnoringExceptions(NSObject* target, SEL sel) { |
| 44 id ret = nil; |
| 45 @try { |
| 46 base::mac::ScopedNSExceptionEnabler enable; |
| 47 ret = [target performSelector:sel]; |
| 48 } |
| 49 @catch(id exception) { |
| 50 } |
| 51 return ret; |
| 52 } |
| 53 |
34 ScopedNSExceptionEnabler::ScopedNSExceptionEnabler() { | 54 ScopedNSExceptionEnabler::ScopedNSExceptionEnabler() { |
35 was_enabled_ = GetNSExceptionsAllowed(); | 55 was_enabled_ = GetNSExceptionsAllowed(); |
36 SetNSExceptionsAllowed(true); | 56 SetNSExceptionsAllowed(true); |
37 } | 57 } |
38 | 58 |
39 ScopedNSExceptionEnabler::~ScopedNSExceptionEnabler() { | 59 ScopedNSExceptionEnabler::~ScopedNSExceptionEnabler() { |
40 SetNSExceptionsAllowed(was_enabled_); | 60 SetNSExceptionsAllowed(was_enabled_); |
41 } | 61 } |
42 | 62 |
43 } // namespace mac | 63 } // namespace mac |
44 } // namespace base | 64 } // namespace base |
OLD | NEW |