Chromium Code Reviews| OLD | NEW |
|---|---|
| 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 #import "chrome/browser/chrome_browser_application_mac.h" | 5 #import "chrome/browser/chrome_browser_application_mac.h" |
| 6 | 6 |
| 7 #include <AvailabilityMacros.h> | 7 #include "base/auto_reset.h" |
| 8 #include <objc/objc-exception.h> | |
| 9 | |
| 10 #import "base/auto_reset.h" | |
| 11 #include "base/command_line.h" | 8 #include "base/command_line.h" |
| 12 #include "base/debug/crash_logging.h" | 9 #include "base/debug/crash_logging.h" |
| 13 #include "base/debug/stack_trace.h" | 10 #include "base/logging.h" |
| 14 #import "base/logging.h" | |
| 15 #include "base/mac/call_with_eh_frame.h" | 11 #include "base/mac/call_with_eh_frame.h" |
| 16 #import "base/mac/scoped_nsobject.h" | |
| 17 #import "base/mac/scoped_objc_class_swizzler.h" | |
| 18 #include "base/macros.h" | |
| 19 #import "base/metrics/histogram_macros.h" | |
| 20 #include "base/strings/stringprintf.h" | 12 #include "base/strings/stringprintf.h" |
| 21 #import "base/strings/sys_string_conversions.h" | |
| 22 #import "chrome/browser/app_controller_mac.h" | 13 #import "chrome/browser/app_controller_mac.h" |
| 23 #include "chrome/browser/ui/tab_contents/tab_contents_iterator.h" | 14 #import "chrome/browser/mac/exception_processor.h" |
| 24 #include "chrome/common/chrome_switches.h" | 15 #include "chrome/common/chrome_switches.h" |
| 25 #include "chrome/common/crash_keys.h" | 16 #include "chrome/common/crash_keys.h" |
| 26 #import "components/crash/core/common/objc_zombie.h" | 17 #import "components/crash/core/common/objc_zombie.h" |
| 27 #include "content/public/browser/browser_accessibility_state.h" | 18 #include "content/public/browser/browser_accessibility_state.h" |
| 28 #include "content/public/browser/render_view_host.h" | |
| 29 #include "content/public/browser/web_contents.h" | |
| 30 | 19 |
| 31 namespace chrome_browser_application_mac { | 20 namespace chrome_browser_application_mac { |
| 32 | 21 |
| 33 // Maximum number of known named exceptions we'll support. There is | |
| 34 // no central registration, but I only find about 75 possibilities in | |
| 35 // the system frameworks, and many of them are probably not | |
| 36 // interesting to track in aggregate (those relating to distributed | |
| 37 // objects, for instance). | |
| 38 const size_t kKnownNSExceptionCount = 25; | |
| 39 | |
| 40 const size_t kUnknownNSException = kKnownNSExceptionCount; | |
| 41 | |
| 42 size_t BinForException(NSException* exception) { | |
| 43 // A list of common known exceptions. The list position will | |
| 44 // determine where they live in the histogram, so never move them | |
| 45 // around, only add to the end. | |
| 46 static NSString* const kKnownNSExceptionNames[] = { | |
| 47 // Grab-bag exception, not very common. CFArray (or other | |
| 48 // container) mutated while being enumerated is one case seen in | |
| 49 // production. | |
| 50 NSGenericException, | |
| 51 | |
| 52 // Out-of-range on NSString or NSArray. Quite common. | |
| 53 NSRangeException, | |
| 54 | |
| 55 // Invalid arg to method, unrecognized selector. Quite common. | |
| 56 NSInvalidArgumentException, | |
| 57 | |
| 58 // malloc() returned null in object creation, I think. Turns out | |
| 59 // to be very uncommon in production, because of the OOM killer. | |
| 60 NSMallocException, | |
| 61 | |
| 62 // This contains things like windowserver errors, trying to draw | |
| 63 // views which aren't in windows, unable to read nib files. By | |
| 64 // far the most common exception seen on the crash server. | |
| 65 NSInternalInconsistencyException, | |
| 66 | |
| 67 nil | |
| 68 }; | |
| 69 | |
| 70 // Make sure our array hasn't outgrown our abilities to track it. | |
| 71 DCHECK_LE(arraysize(kKnownNSExceptionNames), kKnownNSExceptionCount); | |
| 72 | |
| 73 NSString* name = [exception name]; | |
| 74 for (int i = 0; kKnownNSExceptionNames[i]; ++i) { | |
| 75 if (name == kKnownNSExceptionNames[i]) { | |
| 76 return i; | |
| 77 } | |
| 78 } | |
| 79 return kUnknownNSException; | |
| 80 } | |
| 81 | |
| 82 void RecordExceptionWithUma(NSException* exception) { | |
| 83 UMA_HISTOGRAM_ENUMERATION("OSX.NSException", | |
| 84 BinForException(exception), kUnknownNSException); | |
| 85 } | |
| 86 | |
| 87 namespace { | |
| 88 | |
| 89 objc_exception_preprocessor g_next_preprocessor = nullptr; | |
| 90 | |
| 91 id ExceptionPreprocessor(id exception) { | |
| 92 static bool seen_first_exception = false; | |
| 93 | |
| 94 RecordExceptionWithUma(exception); | |
| 95 | |
| 96 const char* const kExceptionKey = | |
| 97 seen_first_exception ? crash_keys::mac::kLastNSException | |
| 98 : crash_keys::mac::kFirstNSException; | |
| 99 NSString* value = [NSString stringWithFormat:@"%@ reason %@", | |
| 100 [exception name], [exception reason]]; | |
| 101 base::debug::SetCrashKeyValue(kExceptionKey, [value UTF8String]); | |
| 102 | |
| 103 const char* const kExceptionTraceKey = | |
| 104 seen_first_exception ? crash_keys::mac::kLastNSExceptionTrace | |
| 105 : crash_keys::mac::kFirstNSExceptionTrace; | |
| 106 // This exception preprocessor runs prior to the one in libobjc, which sets | |
| 107 // the -[NSException callStackReturnAddresses]. | |
| 108 base::debug::SetCrashKeyToStackTrace(kExceptionTraceKey, | |
| 109 base::debug::StackTrace()); | |
| 110 | |
| 111 seen_first_exception = true; | |
| 112 | |
| 113 // Forward to the original version. | |
| 114 if (g_next_preprocessor) | |
| 115 return g_next_preprocessor(exception); | |
| 116 return exception; | |
| 117 } | |
| 118 | |
| 119 } // namespace | |
| 120 | |
| 121 void RegisterBrowserCrApp() { | 22 void RegisterBrowserCrApp() { |
| 122 [BrowserCrApplication sharedApplication]; | 23 [BrowserCrApplication sharedApplication]; |
| 123 }; | 24 }; |
| 124 | 25 |
| 125 void Terminate() { | 26 void Terminate() { |
| 126 [NSApp terminate:nil]; | 27 [NSApp terminate:nil]; |
| 127 } | 28 } |
| 128 | 29 |
| 129 void CancelTerminate() { | 30 void CancelTerminate() { |
| 130 [NSApp cancelTerminate:nil]; | 31 [NSApp cancelTerminate:nil]; |
| 131 } | 32 } |
| 132 | 33 |
| 133 } // namespace chrome_browser_application_mac | 34 } // namespace chrome_browser_application_mac |
| 134 | 35 |
| 135 // Method exposed for the purposes of overriding. | 36 // Method exposed for the purposes of overriding. |
| 136 // Used to determine when a Panel window can become the key window. | 37 // Used to determine when a Panel window can become the key window. |
| 137 @interface NSApplication (PanelsCanBecomeKey) | 38 @interface NSApplication (PanelsCanBecomeKey) |
| 138 - (void)_cycleWindowsReversed:(BOOL)arg1; | 39 - (void)_cycleWindowsReversed:(BOOL)arg1; |
| 139 @end | 40 @end |
| 140 | 41 |
| 141 @implementation BrowserCrApplication | 42 @implementation BrowserCrApplication |
| 142 | 43 |
| 143 + (void)initialize { | 44 + (void)initialize { |
| 144 // Turn all deallocated Objective-C objects into zombies, keeping | 45 // Turn all deallocated Objective-C objects into zombies, keeping |
| 145 // the most recent 10,000 of them on the treadmill. | 46 // the most recent 10,000 of them on the treadmill. |
| 146 ObjcEvilDoers::ZombieEnable(true, 10000); | 47 ObjcEvilDoers::ZombieEnable(true, 10000); |
| 147 | 48 |
| 148 if (!chrome_browser_application_mac::g_next_preprocessor) { | 49 chrome::InstallObjcExceptionPreprocessor(); |
| 149 chrome_browser_application_mac::g_next_preprocessor = | |
| 150 objc_setExceptionPreprocessor( | |
| 151 &chrome_browser_application_mac::ExceptionPreprocessor); | |
| 152 } | |
| 153 } | 50 } |
| 154 | 51 |
| 155 - (id)init { | 52 - (id)init { |
| 156 self = [super init]; | 53 self = [super init]; |
| 157 | 54 |
| 158 // Sanity check to alert if overridden methods are not supported. | 55 // Sanity check to alert if overridden methods are not supported. |
| 159 DCHECK([NSApplication | 56 DCHECK([NSApplication |
| 160 instancesRespondToSelector:@selector(_cycleWindowsReversed:)]); | 57 instancesRespondToSelector:@selector(_cycleWindowsReversed:)]); |
| 161 DCHECK([NSApplication | 58 DCHECK([NSApplication |
| 162 instancesRespondToSelector:@selector(_removeWindow:)]); | 59 instancesRespondToSelector:@selector(_removeWindow:)]); |
| (...skipping 169 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 332 } | 229 } |
| 333 | 230 |
| 334 NSString* actionString = NSStringFromSelector(anAction); | 231 NSString* actionString = NSStringFromSelector(anAction); |
| 335 std::string value = base::StringPrintf("%s tag %ld sending %s to %p", | 232 std::string value = base::StringPrintf("%s tag %ld sending %s to %p", |
| 336 [[sender className] UTF8String], | 233 [[sender className] UTF8String], |
| 337 static_cast<long>(tag), | 234 static_cast<long>(tag), |
| 338 [actionString UTF8String], | 235 [actionString UTF8String], |
| 339 aTarget); | 236 aTarget); |
| 340 base::debug::ScopedCrashKey key(crash_keys::mac::kSendAction, value); | 237 base::debug::ScopedCrashKey key(crash_keys::mac::kSendAction, value); |
| 341 | 238 |
| 342 return [super sendAction:anAction to:aTarget from:sender]; | 239 __block BOOL rv; |
| 240 base::mac::CallWithEHFrame(^{ | |
| 241 rv = [super sendAction:anAction to:aTarget from:sender]; | |
|
Mark Mentovai
2016/12/01 19:02:32
I’ve got half a mind to suggest making the block r
Robert Sesek
2016/12/06 23:18:09
I prefer this, since it's strongly typed.
| |
| 242 }); | |
| 243 return rv; | |
| 343 } | 244 } |
| 344 | 245 |
| 345 - (BOOL)isHandlingSendEvent { | 246 - (BOOL)isHandlingSendEvent { |
| 346 return handlingSendEvent_; | 247 return handlingSendEvent_; |
| 347 } | 248 } |
| 348 | 249 |
| 349 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { | 250 - (void)setHandlingSendEvent:(BOOL)handlingSendEvent { |
| 350 handlingSendEvent_ = handlingSendEvent; | 251 handlingSendEvent_ = handlingSendEvent; |
| 351 } | 252 } |
| 352 | 253 |
| (...skipping 36 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 389 - (void)_cycleWindowsReversed:(BOOL)arg1 { | 290 - (void)_cycleWindowsReversed:(BOOL)arg1 { |
| 390 base::AutoReset<BOOL> pin(&cyclingWindows_, YES); | 291 base::AutoReset<BOOL> pin(&cyclingWindows_, YES); |
| 391 [super _cycleWindowsReversed:arg1]; | 292 [super _cycleWindowsReversed:arg1]; |
| 392 } | 293 } |
| 393 | 294 |
| 394 - (BOOL)isCyclingWindows { | 295 - (BOOL)isCyclingWindows { |
| 395 return cyclingWindows_; | 296 return cyclingWindows_; |
| 396 } | 297 } |
| 397 | 298 |
| 398 @end | 299 @end |
| OLD | NEW |