OLD | NEW |
(Empty) | |
| 1 // Copyright 2016 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 // ====== New Architecture ===== |
| 6 // = This code is only used in the new iOS Chrome architecture. = |
| 7 // ============================================================================ |
| 8 |
| 9 #import "base/logging.h" |
| 10 #import "ios/chrome/browser/browser_coordinator+internal.h" |
| 11 #import "ios/chrome/browser/browser_coordinator.h" |
| 12 |
| 13 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 14 #error "This file requires ARC support." |
| 15 #endif |
| 16 |
| 17 @interface BrowserCoordinator () |
| 18 // Child coordinators owned by this object. |
| 19 @property(nonatomic, strong) |
| 20 NSMutableSet<BrowserCoordinator*>* childCoordinators; |
| 21 // Parent coordinator of this object, if any. |
| 22 @property(nonatomic, readwrite, weak) BrowserCoordinator* parentCoordinator; |
| 23 @property(nonatomic, readwrite) BOOL overlaying; |
| 24 @end |
| 25 |
| 26 @implementation BrowserCoordinator |
| 27 |
| 28 @synthesize browserState = _browserState; |
| 29 @synthesize rootViewController = _rootViewController; |
| 30 @synthesize childCoordinators = _childCoordinators; |
| 31 @synthesize parentCoordinator = _parentCoordinator; |
| 32 @synthesize overlaying = _overlaying; |
| 33 |
| 34 - (instancetype)init { |
| 35 if (self = [super init]) { |
| 36 _childCoordinators = [NSMutableSet set]; |
| 37 } |
| 38 return self; |
| 39 } |
| 40 |
| 41 - (void)dealloc { |
| 42 [self stop]; |
| 43 } |
| 44 |
| 45 #pragma mark - Public API |
| 46 |
| 47 - (void)start { |
| 48 // Default implementation is a no-op. |
| 49 } |
| 50 |
| 51 - (void)stop { |
| 52 // Default implementation is a no-op. |
| 53 } |
| 54 |
| 55 @end |
| 56 |
| 57 @implementation BrowserCoordinator (Internal) |
| 58 // Concrete implementations must implement a |viewController| property. |
| 59 @dynamic viewController; |
| 60 |
| 61 - (NSSet*)children { |
| 62 return [self.childCoordinators copy]; |
| 63 } |
| 64 |
| 65 - (void)addChildCoordinator:(BrowserCoordinator*)coordinator { |
| 66 [self.childCoordinators addObject:coordinator]; |
| 67 coordinator.browserState = self.browserState; |
| 68 coordinator.parentCoordinator = self; |
| 69 coordinator.rootViewController = self.viewController; |
| 70 } |
| 71 |
| 72 - (BrowserCoordinator*)overlayCoordinator { |
| 73 if (self.overlaying) |
| 74 return self; |
| 75 for (BrowserCoordinator* child in self.children) { |
| 76 BrowserCoordinator* overlay = child.overlayCoordinator; |
| 77 if (overlay) |
| 78 return overlay; |
| 79 } |
| 80 return nil; |
| 81 } |
| 82 |
| 83 - (void)addOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator { |
| 84 // If this object has no children, then add |overlayCoordinator| as a child |
| 85 // and mark it as such. |
| 86 if ([self canAddOverlayCoordinator:overlayCoordinator]) { |
| 87 [self addChildCoordinator:overlayCoordinator]; |
| 88 overlayCoordinator.overlaying = YES; |
| 89 } else if (self.childCoordinators.count == 1) { |
| 90 [[self.childCoordinators anyObject] |
| 91 addOverlayCoordinator:overlayCoordinator]; |
| 92 } else { |
| 93 CHECK(NO) << "Coordinators with multiple children must explicitly " |
| 94 << "handle -addOverlayCoordinator: or return NO to " |
| 95 << "-canAddOverlayCoordinator:"; |
| 96 } |
| 97 } |
| 98 |
| 99 - (void)removeOverlayCoordinator { |
| 100 BrowserCoordinator* overlay = self.overlayCoordinator; |
| 101 [overlay.parentCoordinator removeChildCoordinator:overlay]; |
| 102 } |
| 103 |
| 104 - (BOOL)canAddOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator { |
| 105 // By default, coordinators with no other children can add an overlay. |
| 106 return self.childCoordinators.count == 0; |
| 107 } |
| 108 |
| 109 - (void)removeChildCoordinator:(BrowserCoordinator*)coordinator { |
| 110 [self.childCoordinators removeObject:coordinator]; |
| 111 } |
| 112 |
| 113 @end |
OLD | NEW |