OLD | NEW |
(Empty) | |
| 1 // Copyright 2015 The Chromium Authors. All rights reserved. |
| 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. |
| 4 |
| 5 #import "ui/base/test/windowed_nsnotification_observer.h" |
| 6 |
| 7 #import <Cocoa/Cocoa.h> |
| 8 |
| 9 #include "base/run_loop.h" |
| 10 |
| 11 @interface WindowedNSNotificationObserver () |
| 12 - (void)onNotification:(NSNotification*)notification; |
| 13 @end |
| 14 |
| 15 @implementation WindowedNSNotificationObserver |
| 16 |
| 17 - (id)initForNotification:(NSString*)name { |
| 18 if ((self = [super init])) { |
| 19 [[NSNotificationCenter defaultCenter] addObserver:self |
| 20 selector:@selector(onNotification:) |
| 21 name:name |
| 22 object:nil]; |
| 23 } |
| 24 return self; |
| 25 } |
| 26 |
| 27 - (id)initForWorkspaceNotification:(NSString*)name |
| 28 bundleId:(NSString*)bundleId { |
| 29 if ((self = [super init])) { |
| 30 bundleId_.reset([bundleId copy]); |
| 31 [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 32 addObserver:self |
| 33 selector:@selector(onNotification:) |
| 34 name:name |
| 35 object:nil]; |
| 36 } |
| 37 return self; |
| 38 } |
| 39 |
| 40 - (void)onNotification:(NSNotification*)notification { |
| 41 if (bundleId_) { |
| 42 NSRunningApplication* application = |
| 43 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
| 44 if (![[application bundleIdentifier] isEqualToString:bundleId_]) |
| 45 return; |
| 46 |
| 47 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
| 48 } else { |
| 49 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 50 } |
| 51 |
| 52 notificationReceived_ = YES; |
| 53 if (runLoop_) |
| 54 runLoop_->Quit(); |
| 55 } |
| 56 |
| 57 - (void)wait { |
| 58 if (notificationReceived_) |
| 59 return; |
| 60 |
| 61 base::RunLoop runLoop; |
| 62 runLoop_ = &runLoop; |
| 63 runLoop.Run(); |
| 64 runLoop_ = nullptr; |
| 65 } |
| 66 |
| 67 @end |
OLD | NEW |