michael@0: // Copyright (c) 2009 The Chromium Authors. All rights reserved. michael@0: // Use of this source code is governed by a BSD-style license that can be michael@0: // found in the LICENSE file. michael@0: michael@0: #import "chrome_application_mac.h" michael@0: michael@0: #include "base/logging.h" michael@0: michael@0: @interface CrApplication () michael@0: @property(readwrite, michael@0: getter=isHandlingSendEvent, michael@0: nonatomic) BOOL handlingSendEvent; michael@0: @end michael@0: michael@0: @implementation CrApplication michael@0: @synthesize handlingSendEvent = handlingSendEvent_; michael@0: michael@0: // Initialize NSApplication using the custom subclass. Check whether NSApp michael@0: // was already initialized using another class, because that would break michael@0: // some things. michael@0: + (NSApplication*)sharedApplication { michael@0: NSApplication* app = [super sharedApplication]; michael@0: if (![NSApp isKindOfClass:self]) { michael@0: CHROMIUM_LOG(ERROR) << "NSApp should be of type " << [[self className] UTF8String] michael@0: << ", not " << [[NSApp className] UTF8String]; michael@0: DCHECK(false) << "NSApp is of wrong type"; michael@0: } michael@0: return app; michael@0: } michael@0: michael@0: - (id)init { michael@0: if ((self = [super init])) { michael@0: eventHooks_.reset([[NSMutableArray alloc] init]); michael@0: } michael@0: return self; michael@0: } michael@0: michael@0: - (void)sendEvent:(NSEvent*)event { michael@0: chrome_application_mac::ScopedSendingEvent sendingEventScoper; michael@0: for (id handler in eventHooks_.get()) { michael@0: [handler hookForEvent:event]; michael@0: } michael@0: [super sendEvent:event]; michael@0: } michael@0: michael@0: - (void)addEventHook:(id)handler { michael@0: [eventHooks_ addObject:handler]; michael@0: } michael@0: michael@0: - (void)removeEventHook:(id)handler { michael@0: [eventHooks_ removeObject:handler]; michael@0: } michael@0: michael@0: @end michael@0: michael@0: namespace chrome_application_mac { michael@0: michael@0: ScopedSendingEvent::ScopedSendingEvent() michael@0: : app_(static_cast([CrApplication sharedApplication])), michael@0: handling_([app_ isHandlingSendEvent]) { michael@0: [app_ setHandlingSendEvent:YES]; michael@0: } michael@0: michael@0: ScopedSendingEvent::~ScopedSendingEvent() { michael@0: [app_ setHandlingSendEvent:handling_]; michael@0: } michael@0: michael@0: } // namespace chrome_application_mac