Chromium Code Reviews| Index: ios/clean/chrome/browser/ui/overlays/overlay_scheduler.mm |
| diff --git a/ios/clean/chrome/browser/ui/overlays/overlay_scheduler.mm b/ios/clean/chrome/browser/ui/overlays/overlay_scheduler.mm |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..878ddac9ed7e9aea6c2cd7a018f6708cfc41d7cb |
| --- /dev/null |
| +++ b/ios/clean/chrome/browser/ui/overlays/overlay_scheduler.mm |
| @@ -0,0 +1,116 @@ |
| +// Copyright 2017 The Chromium Authors. All rights reserved. |
| +// Use of this source code is governed by a BSD-style license that can be |
| +// found in the LICENSE file. |
| + |
| +#import "ios/clean/chrome/browser/ui/overlays/overlay_scheduler.h" |
| + |
| +#include <list> |
| + |
| +#include "base/logging.h" |
| +#import "ios/chrome/browser/web_state_list/web_state_list.h" |
| +#import "ios/clean/chrome/browser/ui/commands/tab_grid_commands.h" |
| +#import "ios/clean/chrome/browser/ui/overlays/overlay_queue.h" |
| +#import "ios/web/public/web_state/web_state.h" |
| + |
| +#if !defined(__has_feature) || !__has_feature(objc_arc) |
| +#error "This file requires ARC support." |
| +#endif |
| + |
| +@interface OverlayScheduler () { |
| + // Backing object for property of the same name. |
| + WebStateList* _webStateList; |
| + // The WebStates that have queued overlays. |
| + std::list<web::WebState*> _webStates; |
|
marq (ping after 24h)
2017/06/14 10:02:32
Several places below make assertions about what's
kkhorimoto
2017/06/15 08:26:28
Done.
|
| +} |
| + |
| +// The WebStateList passed on initialization. |
| +@property(nonatomic, readonly, assign) WebStateList& webStateList; |
| + |
| +// The dispatcher passed on initialization. |
| +@property(nonatomic, readonly, weak) |
| + id<TabGridCommands, OverlayPresentationCommands> |
| + dispatcher; |
| + |
| +// Attempts to show the next queued overlay. |
| +- (void)tryToStartNextOverlay; |
| + |
| +@end |
| + |
| +@implementation OverlayScheduler |
| + |
| +@synthesize dispatcher = _dispatcher; |
| + |
| +- (instancetype)initWithWebStateList:(WebStateList*)webStateList |
| + dispatcher: |
| + (id<TabGridCommands, OverlayPresentationCommands>) |
| + dispatcher { |
| + DCHECK(webStateList); |
| + DCHECK(dispatcher); |
| + if ((self = [super init])) { |
| + _webStateList = webStateList; |
| + _dispatcher = dispatcher; |
| + } |
| + return self; |
| +} |
| + |
| +#pragma mark - Accessors |
| + |
| +- (WebStateList&)webStateList { |
| + return *_webStateList; |
| +} |
| + |
| +#pragma mark - OverlaySchedulerCommands |
| + |
| +- (void)scheduleOverlayForWebState:(web::WebState*)webState { |
| + DCHECK(webState); |
| + _webStates.push_back(webState); |
| + [self tryToStartNextOverlay]; |
| +} |
| + |
| +- (void)visibleOverlayWasReplacedForWebState:(web::WebState*)webState { |
| + DCHECK(webState); |
| + DCHECK_EQ(_webStates.front(), webState); |
|
marq (ping after 24h)
2017/06/14 10:02:32
This makes me slightly nervous, because it makes t
kkhorimoto
2017/06/15 08:26:29
It's been converted to an observer pattern now. T
|
| + DCHECK(OverlayQueue::FromWebState(webState)->IsShowingOverlay()); |
| + _webStates.push_front(webState); |
| +} |
| + |
| +- (void)overlayWasStoppedForWebState:(web::WebState*)webState { |
| + DCHECK(!_webStates.empty()); |
| + DCHECK_EQ(_webStates.front(), webState); |
| + _webStates.pop_front(); |
| + [self tryToStartNextOverlay]; |
| +} |
| + |
| +- (void)cancelOverlaysForWebState:(web::WebState*)webState { |
| + DCHECK(webState); |
| + // Remove all scheduled instances of |webState| from the queue. |
| + auto i = _webStates.begin(); |
| + while (i != _webStates.end()) { |
| + if (*i == webState) |
| + _webStates.erase(i); |
| + } |
| + // Remove all overlays from |webState|'s queue. |
| + OverlayQueue::FromWebState(webState)->CancelOverlays(); |
| + [self tryToStartNextOverlay]; |
| +} |
| + |
| +#pragma mark - |
| + |
| +- (void)tryToStartNextOverlay { |
| + // Early return if there are no overlays to show. |
| + if (_webStates.empty()) |
| + return; |
| + // Also early return if the front queue is already presenting an overlay. |
| + web::WebState* webState = _webStates.front(); |
| + OverlayQueue* queue = OverlayQueue::FromWebState(webState); |
| + if (queue->IsShowingOverlay()) |
| + return; |
| + // Show the next WebState's content view and start its overlay. |
| + int newActiveIndex = self.webStateList.GetIndexOfWebState(webState); |
| + DCHECK_NE(newActiveIndex, WebStateList::kInvalidIndex); |
| + self.webStateList.ActivateWebStateAt(newActiveIndex); |
| + [self.dispatcher showTabGridTabForActiveWebState]; |
| + [self.dispatcher startNextOverlayForWebState:webState]; |
| +} |
| + |
| +@end |