Chromium Code Reviews| OLD | NEW |
|---|---|
| 1 // Copyright 2016 The Chromium Authors. All rights reserved. | 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 | 2 // Use of this source code is governed by a BSD-style license that can be |
| 3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
| 4 | 4 |
| 5 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator.h" | 5 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator.h" |
| 6 | 6 |
| 7 #import "base/logging.h" | 7 #import "base/logging.h" |
| 8 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal. h" | 8 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator+internal. h" |
| 9 | 9 |
| 10 #if !defined(__has_feature) || !__has_feature(objc_arc) | 10 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 11 #error "This file requires ARC support." | 11 #error "This file requires ARC support." |
| 12 #endif | 12 #endif |
| 13 | 13 |
| 14 @interface BrowserCoordinator () | 14 @interface BrowserCoordinator () |
| 15 // Child coordinators owned by this object. | 15 // Child coordinators owned by this object. |
| 16 @property(nonatomic, strong) | 16 @property(nonatomic, strong) |
| 17 NSMutableSet<BrowserCoordinator*>* childCoordinators; | 17 NSMutableSet<BrowserCoordinator*>* childCoordinators; |
| 18 // Parent coordinator of this object, if any. | 18 // Parent coordinator of this object, if any. |
| 19 @property(nonatomic, readwrite, weak) BrowserCoordinator* parentCoordinator; | 19 @property(nonatomic, readwrite, weak) BrowserCoordinator* parentCoordinator; |
| 20 @property(nonatomic, readwrite) BOOL started; | 20 @property(nonatomic, readwrite) BOOL started; |
| 21 @property(nonatomic, readwrite) BOOL overlaying; | |
|
marq (ping after 24h)
2017/06/21 09:53:19
You will also need to fix the unit tests for overl
kkhorimoto
2017/06/23 06:22:39
Done.
| |
| 22 @end | 21 @end |
| 23 | 22 |
| 24 @implementation BrowserCoordinator | 23 @implementation BrowserCoordinator |
| 25 | 24 |
| 26 @synthesize browser = _browser; | 25 @synthesize browser = _browser; |
| 27 @synthesize childCoordinators = _childCoordinators; | 26 @synthesize childCoordinators = _childCoordinators; |
| 28 @synthesize parentCoordinator = _parentCoordinator; | 27 @synthesize parentCoordinator = _parentCoordinator; |
| 29 @synthesize started = _started; | 28 @synthesize started = _started; |
| 30 @synthesize overlaying = _overlaying; | |
| 31 | 29 |
| 32 - (instancetype)init { | 30 - (instancetype)init { |
| 33 if (self = [super init]) { | 31 if (self = [super init]) { |
| 34 _childCoordinators = [NSMutableSet set]; | 32 _childCoordinators = [NSMutableSet set]; |
| 35 } | 33 } |
| 36 return self; | 34 return self; |
| 37 } | 35 } |
| 38 | 36 |
| 39 #pragma mark - Public API | 37 #pragma mark - Public API |
| 40 | 38 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
| 76 - (void)addChildCoordinator:(BrowserCoordinator*)childCoordinator { | 74 - (void)addChildCoordinator:(BrowserCoordinator*)childCoordinator { |
| 77 CHECK([self respondsToSelector:@selector(viewController)]) | 75 CHECK([self respondsToSelector:@selector(viewController)]) |
| 78 << "BrowserCoordinator implementations must provide a viewController " | 76 << "BrowserCoordinator implementations must provide a viewController " |
| 79 "property."; | 77 "property."; |
| 80 [self.childCoordinators addObject:childCoordinator]; | 78 [self.childCoordinators addObject:childCoordinator]; |
| 81 childCoordinator.parentCoordinator = self; | 79 childCoordinator.parentCoordinator = self; |
| 82 childCoordinator.browser = self.browser; | 80 childCoordinator.browser = self.browser; |
| 83 [childCoordinator wasAddedToParentCoordinator:self]; | 81 [childCoordinator wasAddedToParentCoordinator:self]; |
| 84 } | 82 } |
| 85 | 83 |
| 86 - (BrowserCoordinator*)overlayCoordinator { | |
| 87 if (self.overlaying) | |
| 88 return self; | |
| 89 for (BrowserCoordinator* child in self.children) { | |
| 90 BrowserCoordinator* overlay = child.overlayCoordinator; | |
| 91 if (overlay) | |
| 92 return overlay; | |
| 93 } | |
| 94 return nil; | |
| 95 } | |
| 96 | |
| 97 - (void)addOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator { | |
| 98 // If this object has no children, then add |overlayCoordinator| as a child | |
| 99 // and mark it as such. | |
| 100 if ([self canAddOverlayCoordinator:overlayCoordinator]) { | |
| 101 [self addChildCoordinator:overlayCoordinator]; | |
| 102 overlayCoordinator.overlaying = YES; | |
| 103 } else if (self.childCoordinators.count == 1) { | |
| 104 [[self.childCoordinators anyObject] | |
| 105 addOverlayCoordinator:overlayCoordinator]; | |
| 106 } else if (self.childCoordinators.count > 1) { | |
| 107 CHECK(NO) << "Coordinators with multiple children must explicitly " | |
| 108 << "handle -addOverlayCoordinator: or return NO to " | |
| 109 << "-canAddOverlayCoordinator:"; | |
| 110 } | |
| 111 // If control reaches here, the terminal child of the coordinator hierarchy | |
| 112 // has returned NO to -canAddOverlayCoordinator, so no overlay can be added. | |
| 113 // This is by default a silent no-op. | |
| 114 } | |
| 115 | |
| 116 - (void)removeOverlayCoordinator { | |
| 117 BrowserCoordinator* overlay = self.overlayCoordinator; | |
| 118 [overlay.parentCoordinator removeChildCoordinator:overlay]; | |
| 119 overlay.overlaying = NO; | |
| 120 } | |
| 121 | |
| 122 - (BOOL)canAddOverlayCoordinator:(BrowserCoordinator*)overlayCoordinator { | |
| 123 // By default, a hierarchy with an overlay can't add a new one. | |
| 124 // By default, coordinators with parents can't be added as overlays. | |
| 125 // By default, coordinators with no other children can add an overlay. | |
| 126 return self.overlayCoordinator == nil && | |
| 127 overlayCoordinator.parentCoordinator == nil && | |
| 128 self.childCoordinators.count == 0; | |
| 129 } | |
| 130 | |
| 131 - (void)removeChildCoordinator:(BrowserCoordinator*)childCoordinator { | 84 - (void)removeChildCoordinator:(BrowserCoordinator*)childCoordinator { |
| 132 if (![self.childCoordinators containsObject:childCoordinator]) | 85 if (![self.childCoordinators containsObject:childCoordinator]) |
| 133 return; | 86 return; |
| 134 // Remove the grand-children first. | 87 // Remove the grand-children first. |
| 135 for (BrowserCoordinator* grandChild in childCoordinator.children) { | 88 for (BrowserCoordinator* grandChild in childCoordinator.children) { |
| 136 [childCoordinator removeChildCoordinator:grandChild]; | 89 [childCoordinator removeChildCoordinator:grandChild]; |
| 137 } | 90 } |
| 138 // Remove the child. | 91 // Remove the child. |
| 139 [childCoordinator willBeRemovedFromParentCoordinator]; | 92 [childCoordinator willBeRemovedFromParentCoordinator]; |
| 140 [self.childCoordinators removeObject:childCoordinator]; | 93 [self.childCoordinators removeObject:childCoordinator]; |
| (...skipping 11 matching lines...) Expand all Loading... | |
| 152 | 105 |
| 153 - (void)childCoordinatorDidStart:(BrowserCoordinator*)childCoordinator { | 106 - (void)childCoordinatorDidStart:(BrowserCoordinator*)childCoordinator { |
| 154 // Default implementation is a no-op. | 107 // Default implementation is a no-op. |
| 155 } | 108 } |
| 156 | 109 |
| 157 - (void)childCoordinatorWillStop:(BrowserCoordinator*)childCoordinator { | 110 - (void)childCoordinatorWillStop:(BrowserCoordinator*)childCoordinator { |
| 158 // Default implementation is a no-op. | 111 // Default implementation is a no-op. |
| 159 } | 112 } |
| 160 | 113 |
| 161 @end | 114 @end |
| OLD | NEW |