summaryrefslogtreecommitdiffstats
path: root/base/mac/scoped_nsexception_enabler.mm
blob: 7b8ad9266b1a75ca18ec81631f1f90b01683e7f9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
// Copyright (c) 2012 The Chromium Authors. All rights reserved.
// Use of this source code is governed by a BSD-style license that can be
// found in the LICENSE file.

#import "base/mac/scoped_nsexception_enabler.h"

#import "base/lazy_instance.h"
#import "base/threading/thread_local.h"

// To make the |g_exceptionsAllowed| declaration readable.
using base::LazyInstance;
using base::ThreadLocalBoolean;

// When C++ exceptions are disabled, the C++ library defines |try| and
// |catch| so as to allow exception-expecting C++ code to build properly when
// language support for exceptions is not present.  These macros interfere
// with the use of |@try| and |@catch| in Objective-C files such as this one.
// Undefine these macros here, after everything has been #included, since
// there will be no C++ uses and only Objective-C uses from this point on.
#undef try
#undef catch

namespace {

// Whether to allow NSExceptions to be raised on the current thread.
LazyInstance<ThreadLocalBoolean>::Leaky
    g_exceptionsAllowed = LAZY_INSTANCE_INITIALIZER;

}  // namespace

namespace base {
namespace mac {

bool GetNSExceptionsAllowed() {
  return g_exceptionsAllowed.Get().Get();
}

void SetNSExceptionsAllowed(bool allowed) {
  return g_exceptionsAllowed.Get().Set(allowed);
}

id RunBlockIgnoringExceptions(BlockReturningId block) {
  id ret = nil;
  @try {
    base::mac::ScopedNSExceptionEnabler enable;
    ret = block();
  }
  @catch(id exception) {
  }
  return ret;
}

ScopedNSExceptionEnabler::ScopedNSExceptionEnabler() {
  was_enabled_ = GetNSExceptionsAllowed();
  SetNSExceptionsAllowed(true);
}

ScopedNSExceptionEnabler::~ScopedNSExceptionEnabler() {
  SetNSExceptionsAllowed(was_enabled_);
}

}  // namespace mac
}  // namespace base