Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2017 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 "ios/clean/chrome/browser/ui/overlays/overlay_scheduler.h" | |
| 6 | |
| 7 #include <list> | |
| 8 | |
| 9 #include "base/logging.h" | |
| 10 #import "ios/chrome/browser/web_state_list/web_state_list.h" | |
| 11 #import "ios/clean/chrome/browser/ui/commands/tab_grid_commands.h" | |
| 12 #import "ios/clean/chrome/browser/ui/overlays/overlay_queue.h" | |
| 13 #import "ios/web/public/web_state/web_state.h" | |
| 14 | |
| 15 #if !defined(__has_feature) || !__has_feature(objc_arc) | |
| 16 #error "This file requires ARC support." | |
| 17 #endif | |
| 18 | |
| 19 @interface OverlayScheduler () { | |
| 20 // Backing object for property of the same name. | |
| 21 WebStateList* _webStateList; | |
| 22 // The WebStates that have queued overlays. | |
| 23 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.
| |
| 24 } | |
| 25 | |
| 26 // The WebStateList passed on initialization. | |
| 27 @property(nonatomic, readonly, assign) WebStateList& webStateList; | |
| 28 | |
| 29 // The dispatcher passed on initialization. | |
| 30 @property(nonatomic, readonly, weak) | |
| 31 id<TabGridCommands, OverlayPresentationCommands> | |
| 32 dispatcher; | |
| 33 | |
| 34 // Attempts to show the next queued overlay. | |
| 35 - (void)tryToStartNextOverlay; | |
| 36 | |
| 37 @end | |
| 38 | |
| 39 @implementation OverlayScheduler | |
| 40 | |
| 41 @synthesize dispatcher = _dispatcher; | |
| 42 | |
| 43 - (instancetype)initWithWebStateList:(WebStateList*)webStateList | |
| 44 dispatcher: | |
| 45 (id<TabGridCommands, OverlayPresentationCommands>) | |
| 46 dispatcher { | |
| 47 DCHECK(webStateList); | |
| 48 DCHECK(dispatcher); | |
| 49 if ((self = [super init])) { | |
| 50 _webStateList = webStateList; | |
| 51 _dispatcher = dispatcher; | |
| 52 } | |
| 53 return self; | |
| 54 } | |
| 55 | |
| 56 #pragma mark - Accessors | |
| 57 | |
| 58 - (WebStateList&)webStateList { | |
| 59 return *_webStateList; | |
| 60 } | |
| 61 | |
| 62 #pragma mark - OverlaySchedulerCommands | |
| 63 | |
| 64 - (void)scheduleOverlayForWebState:(web::WebState*)webState { | |
| 65 DCHECK(webState); | |
| 66 _webStates.push_back(webState); | |
| 67 [self tryToStartNextOverlay]; | |
| 68 } | |
| 69 | |
| 70 - (void)visibleOverlayWasReplacedForWebState:(web::WebState*)webState { | |
| 71 DCHECK(webState); | |
| 72 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
| |
| 73 DCHECK(OverlayQueue::FromWebState(webState)->IsShowingOverlay()); | |
| 74 _webStates.push_front(webState); | |
| 75 } | |
| 76 | |
| 77 - (void)overlayWasStoppedForWebState:(web::WebState*)webState { | |
| 78 DCHECK(!_webStates.empty()); | |
| 79 DCHECK_EQ(_webStates.front(), webState); | |
| 80 _webStates.pop_front(); | |
| 81 [self tryToStartNextOverlay]; | |
| 82 } | |
| 83 | |
| 84 - (void)cancelOverlaysForWebState:(web::WebState*)webState { | |
| 85 DCHECK(webState); | |
| 86 // Remove all scheduled instances of |webState| from the queue. | |
| 87 auto i = _webStates.begin(); | |
| 88 while (i != _webStates.end()) { | |
| 89 if (*i == webState) | |
| 90 _webStates.erase(i); | |
| 91 } | |
| 92 // Remove all overlays from |webState|'s queue. | |
| 93 OverlayQueue::FromWebState(webState)->CancelOverlays(); | |
| 94 [self tryToStartNextOverlay]; | |
| 95 } | |
| 96 | |
| 97 #pragma mark - | |
| 98 | |
| 99 - (void)tryToStartNextOverlay { | |
| 100 // Early return if there are no overlays to show. | |
| 101 if (_webStates.empty()) | |
| 102 return; | |
| 103 // Also early return if the front queue is already presenting an overlay. | |
| 104 web::WebState* webState = _webStates.front(); | |
| 105 OverlayQueue* queue = OverlayQueue::FromWebState(webState); | |
| 106 if (queue->IsShowingOverlay()) | |
| 107 return; | |
| 108 // Show the next WebState's content view and start its overlay. | |
| 109 int newActiveIndex = self.webStateList.GetIndexOfWebState(webState); | |
| 110 DCHECK_NE(newActiveIndex, WebStateList::kInvalidIndex); | |
| 111 self.webStateList.ActivateWebStateAt(newActiveIndex); | |
| 112 [self.dispatcher showTabGridTabForActiveWebState]; | |
| 113 [self.dispatcher startNextOverlayForWebState:webState]; | |
| 114 } | |
| 115 | |
| 116 @end | |
| OLD | NEW |