diff --git a/chrome/app_shim/app_shim_application.mm b/chrome/app_shim/app_shim_application.mm index 319232f31fe7e..d59f2bb918759 100644 --- a/chrome/app_shim/app_shim_application.mm +++ b/chrome/app_shim/app_shim_application.mm @@ -20,6 +20,31 @@ _handlingSendEvent = handlingSendEvent; } +- (BOOL)sendAction:(SEL)anAction to:(id)aTarget from:(id)sender { + // The Dock menu contains an automagic section where you can select + // amongst open windows. If a window is closed (e.g. via JavaScript from + // the hosting browser over remote_cocoa) while the menu is up, the menu + // item for that window continues to exist. When a window is selected + // this method is called with the now-freed window as |aTarget|. + // Short-circuit the call if |aTarget| is not a valid window. This + // mirrors the same guard in BrowserCrApplication. + if (anAction == @selector(_selectWindow:)) { + // Not using -[NSArray containsObject:] because |aTarget| may be a + // freed object. + BOOL found = NO; + for (NSWindow* window in self.windows) { + if (window == aTarget) { + found = YES; + break; + } + } + if (!found) { + return NO; + } + } + return [super sendAction:anAction to:aTarget from:sender]; +} + - (void)enableScreenReaderCompleteModeAfterDelay:(BOOL)enable { [NSObject cancelPreviousPerformRequestsWithTarget:self selector:@selector