Commit 3c052431 authored by Mark Cogan's avatar Mark Cogan Committed by Commit Bot

[iOS] Remove TabModel Notifications (2/3) -- StackView

This CL removes kTabModelAllTabsDidCloseNotification, one of the two remaining TabModel
notifications. StackViewController was its only observer.

This CL moves the notification into a TabModelObserver method. Since StackViewController isn't
a TabModelObserver itself, a method is added to CardSetObserver (since the card sets are the
objects that observe the tab models in the stack view).

Prior to this CL, the flow of events when closing all tabs in the stack view was:

[1] The card set whose cards are being deleted (in removeAllCardsFromSet:) calls
    [cardSet setIgnoresTabModelChanges:YES].
[2] The cards are animated out
[3] A completion handler is called when the animation completes; it calls -closeAllTabs on the
    cardSet's TabModel.
[4] The tab model deletes the tabs and sends the kTabModelAllTabsDidCloseNotification
[5] StackViewController catches the notification and calls -allModelTabsHaveClosed:, which in
    turn sets [cardSet setIgnoresTabModelChanges:NO]

It's important to note that [cardSet setIgnoresTabModelChanges:NO] removes |cardSet| as a
TabModelObserver. Functionally in this flow, tab model observation is disabled, then the
notification is used to bypass the disabled observation and to signal that observation should be
reenabled.

After this CL some of the steps in the above flow are changed:
[3] A completion handler is called when the animation completes; it calls
    [cardSet setIgnoresTabModelChanges:NO] and then -closeAllTabs on the cardSet's TabModel.
[4] The tab model deletes the tabs and tells its observers.
[5] CardSet (a TabModelObserver) gets the -tabModelClosedAllTabs call and sends its observer
    a cardSetDidCloseAllTabs: method.
[6] StackViewController gets the cardSetDidCloseAllTabs: call and handles the cleanup.

Bug: 320877
Cq-Include-Trybots: master.tryserver.chromium.mac:ios-simulator-cronet;master.tryserver.chromium.mac:ios-simulator-full-configs
Change-Id: I98ac82d272d696f2182aa54c5b603cf6c737e073
Reviewed-on: https://chromium-review.googlesource.com/847004
Commit-Queue: Mark Cogan <marq@chromium.org>
Reviewed-by: default avatarGauthier Ambard <gambard@chromium.org>
Cr-Commit-Position: refs/heads/master@{#526656}
parent 260ccf1f
......@@ -39,11 +39,6 @@ class WebState;
// kTabModelTabKey.
extern NSString* const kTabModelTabDidFinishLoadingNotification;
// All tabs have finished their shutdown sequences.
// NOTE: This notification is not sent when closing a single tab that happens
// to be the last tab.
extern NSString* const kTabModelAllTabsDidCloseNotification;
// ---------------------------------------------------------------------------
namespace TabModelConstants {
......
......@@ -69,8 +69,6 @@
NSString* const kTabModelTabDidFinishLoadingNotification =
@"kTabModelTabDidFinishLoadingNotification";
NSString* const kTabModelAllTabsDidCloseNotification =
@"kTabModelAllTabsDidCloseNotification";
namespace {
......@@ -472,9 +470,7 @@ void CleanCertificatePolicyCache(
- (void)closeAllTabs {
_webStateList->CloseAllWebStates(WebStateList::CLOSE_USER_ACTION);
[[NSNotificationCenter defaultCenter]
postNotificationName:kTabModelAllTabsDidCloseNotification
object:self];
[_observers tabModelClosedAllTabs:self];
}
- (void)haltAllTabs {
......
......@@ -24,6 +24,9 @@
// The given tab will be removed.
- (void)tabModel:(TabModel*)model willRemoveTab:(Tab*)tab;
// All tabs in the model will close.
- (void)tabModelClosedAllTabs:(TabModel*)model;
// A tab was removed at the given index.
- (void)tabModel:(TabModel*)model
didRemoveTab:(Tab*)tab
......
......@@ -20,6 +20,9 @@ struct LayoutRect;
// Observer delegate for clients interested in changes to cards in a CardSet.
@protocol CardSetObserver
// |cardSet| closed all of its tabs.
- (void)cardSetDidCloseAllTabs:(CardSet*)cardSet;
// |newCard| has been added to |cardSet|.
- (void)cardSet:(CardSet*)cardSet didAddCard:(StackCard*)newCard;
......
......@@ -535,6 +535,11 @@ const CGFloat kMaxCardStaggerPercentage = 0.35;
[self removeCardAtIndex:index];
}
// All tabs were closed in the model
- (void)tabModelClosedAllTabs:(TabModel*)model {
[self.observer cardSetDidCloseAllTabs:self];
}
- (CGFloat)maximumStackLength {
return [stackModel_ maximumStackLength];
}
......
......@@ -222,8 +222,6 @@ NSString* const kTransitionToolbarAnimationKey =
// scroll to one of its boundaries without also having reached the
// corresponding boundary of the stack being scrolled.
- (void)updateScrollViewContentSize;
// Deregisters for the notifications |registerForNotifications| specifies.
- (void)deregisterForNotifications;
// Eliminates the ability for the user to perform any further interactions
// with the stack view. Should be called when the stack view starts being
// dismissed.
......@@ -404,10 +402,6 @@ NSString* const kTransitionToolbarAnimationKey =
// so this should *not* be called frequently.
- (StackCard*)cardForView:(CardView*)view;
// All local tab state is cleared, and |currentlyClosingAllTabs_| is set to
// |NO|.
- (void)allModelTabsHaveClosed:(NSNotification*)notify;
// Updates the display views so that they are aligned to the scroll view's
// viewport. Should be called any time the scroll view's content offset
// changes.
......@@ -669,11 +663,6 @@ NSString* const kTransitionToolbarAnimationKey =
[[_mainCardSet displayView] removeFromSuperview];
[[_otrCardSet displayView] removeFromSuperview];
// Only deregister from the specific notifications for which this class
// registered. Do not use the blanket |removeObserver|, otherwise the low
// memory notification is not received and the view is never unloaded.
[self deregisterForNotifications];
[_mainCardSet disconnect];
_mainCardSet = nil;
......@@ -752,8 +741,6 @@ NSString* const kTransitionToolbarAnimationKey =
? UIInterfaceOrientationPortrait
: UIInterfaceOrientationLandscapeRight;
}
[self registerForNotifications];
// TODO(blundell): Why isn't this recognizer initialized with the
// pinch and mode switch recognizers?
UIPanGestureRecognizer* panGestureRecognizer =
......@@ -1035,21 +1022,6 @@ NSString* const kTransitionToolbarAnimationKey =
completion:nil];
}
- (void)registerForNotifications {
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter addObserver:self
selector:@selector(allModelTabsHaveClosed:)
name:kTabModelAllTabsDidCloseNotification
object:nil];
}
- (void)deregisterForNotifications {
NSNotificationCenter* defaultCenter = [NSNotificationCenter defaultCenter];
[defaultCenter removeObserver:self
name:kTabModelAllTabsDidCloseNotification
object:nil];
}
- (void)prepareForDismissal {
UIView* activeView = [_activeCardSet displayView];
[activeView removeGestureRecognizer:_pinchRecognizer];
......@@ -1169,6 +1141,7 @@ NSString* const kTransitionToolbarAnimationKey =
void (^toDoWhenDone)(void) = NULL;
if (card == lastVisibleCard) {
toDoWhenDone = ^{
[cardSet setIgnoresTabModelChanges:NO];
[cardSet.tabModel closeAllTabs];
};
}
......@@ -2922,27 +2895,17 @@ NSString* const kTransitionToolbarAnimationKey =
#pragma mark Notification Handlers
- (void)allModelTabsHaveClosed:(NSNotification*)notify {
- (void)cardSetDidCloseAllTabs:(CardSet*)cardSet {
// Early return if the stack view is not active. This can sometimes occur if
// |clearInternalState| triggers the deletion of a tab model.
if (!_isActive)
return;
CardSet* closedSet =
(notify.object == [_mainCardSet tabModel]) ? _mainCardSet : _otrCardSet;
// If the tabModel that send the notification is not one handled by one of
// the two card sets, just return. This will happen when the otr tab model is
// deleted because the incognito profile is deleted.
if (notify.object != [closedSet tabModel])
return;
if (closedSet == _activeCardSet)
if (cardSet == _activeCardSet)
[self activeCardCountChanged];
for (UIView* card in [closedSet.displayView subviews]) {
for (UIView* card in [cardSet.displayView subviews]) {
[card removeFromSuperview];
}
[closedSet setIgnoresTabModelChanges:NO];
// No need to re-sync with the card set here, since the tab model (and thus
// the card set) is known to be empty.
......@@ -2951,8 +2914,8 @@ NSString* const kTransitionToolbarAnimationKey =
// finishes closing while the main set is still animating (in the case of
// closing all cards at once) wait until the main set finishes before updating
// the display (necessary so the state is right if a new tab is opened).
if ((closedSet == _otrCardSet && ![_mainCardSet ignoresTabModelChanges]) ||
(closedSet == _mainCardSet && [[_otrCardSet cards] count] == 0)) {
if ((cardSet == _otrCardSet && ![_mainCardSet ignoresTabModelChanges]) ||
(cardSet == _mainCardSet && [[_otrCardSet cards] count] == 0)) {
[self displayMainCardSetOnly];
}
......
......@@ -42,9 +42,6 @@ typedef enum {
// attaching to the card sets.
- (void)setUpDisplayViews;
// Registers listeners for the appropriate notifications.
- (void)registerForNotifications;
// Used during |-dealloc| and |-didReceiveMemoryWaring| to clear any references
// to the cards and removes |self| as an observer to any notifications.
- (void)cleanUpViewsAndNotifications;
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment