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 "base/mac/sdk_forward_declarations.h" |
| 8 |
| 9 @interface WindowedNSNotificationObserver () |
| 10 - (void)observe:(NSNotification*)notification; |
| 11 @end |
| 12 |
| 13 @implementation WindowedNSNotificationObserver |
| 14 |
| 15 - (id)initForNotification:(NSString*)name { |
| 16 if (self = [super init]) { |
| 17 [[NSNotificationCenter defaultCenter] addObserver:self |
| 18 selector:@selector(observe:) |
| 19 name:name |
| 20 object:nil]; |
| 21 } |
| 22 return self; |
| 23 } |
| 24 |
| 25 - (id)initForNotification:(NSString*)name |
| 26 andBundleId:(NSString*)bundleId { |
| 27 if (self = [super init]) { |
| 28 bundleId_.reset([[bundleId copy] retain]); |
| 29 [[[NSWorkspace sharedWorkspace] notificationCenter] |
| 30 addObserver:self |
| 31 selector:@selector(observe:) |
| 32 name:name |
| 33 object:nil]; |
| 34 } |
| 35 return self; |
| 36 } |
| 37 |
| 38 - (void)observe:(NSNotification*)notification { |
| 39 if (bundleId_) { |
| 40 NSRunningApplication* application = |
| 41 [[notification userInfo] objectForKey:NSWorkspaceApplicationKey]; |
| 42 if (![[application bundleIdentifier] isEqualToString:bundleId_]) |
| 43 return; |
| 44 |
| 45 [[[NSWorkspace sharedWorkspace] notificationCenter] removeObserver:self]; |
| 46 } else { |
| 47 [[NSNotificationCenter defaultCenter] removeObserver:self]; |
| 48 } |
| 49 |
| 50 notificationReceived_ = YES; |
| 51 if (runLoop_.get()) |
| 52 runLoop_->Quit(); |
| 53 } |
| 54 |
| 55 - (void)wait { |
| 56 if (notificationReceived_) |
| 57 return; |
| 58 |
| 59 runLoop_.reset(new base::RunLoop); |
| 60 runLoop_->Run(); |
| 61 } |
| 62 |
| 63 @end |
OLD | NEW |