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/web_contents/overlays/web_overlay_scheduler
.h" |
| 6 |
| 7 #include <list> |
| 8 |
| 9 #include "base/logging.h" |
| 10 #import "ios/clean/chrome/browser/ui/commands/tab_grid_commands.h" |
| 11 #import "ios/clean/chrome/browser/ui/web_contents/overlays/web_overlay_queue.h" |
| 12 #import "ios/web/public/web_state/web_state.h" |
| 13 |
| 14 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 15 #error "This file requires ARC support." |
| 16 #endif |
| 17 |
| 18 @interface WebOverlayScheduler () { |
| 19 // The WebStates that have queued overlays. |
| 20 std::list<web::WebState*> web_states_; |
| 21 } |
| 22 |
| 23 // The dispatcher passed on initialization. |
| 24 @property(nonatomic, readonly, weak) |
| 25 id<TabGridCommands, WebOverlayPresentationCommands> |
| 26 dispatcher; |
| 27 |
| 28 // Attempts to show the next queued overlay. |
| 29 - (void)tryToStartNextOverlay; |
| 30 |
| 31 @end |
| 32 |
| 33 @implementation WebOverlayScheduler |
| 34 |
| 35 @synthesize dispatcher = _dispatcher; |
| 36 |
| 37 - (instancetype)initWithDispatcher: |
| 38 (id<TabGridCommands, WebOverlayPresentationCommands>)dispatcher { |
| 39 if ((self = [super init])) |
| 40 _dispatcher = dispatcher; |
| 41 return self; |
| 42 } |
| 43 |
| 44 #pragma mark - WebOverlayCommands |
| 45 |
| 46 - (void)scheduleWebOverlayForWebState:(web::WebState*)webState { |
| 47 DCHECK(webState); |
| 48 web_states_.push_back(webState); |
| 49 [self tryToStartNextOverlay]; |
| 50 } |
| 51 |
| 52 - (void)webOverlayWasStoppedForWebState:(web::WebState*)webState { |
| 53 DCHECK(!web_states_.empty()); |
| 54 DCHECK_EQ(web_states_.front(), webState); |
| 55 web_states_.pop_front(); |
| 56 [self tryToStartNextOverlay]; |
| 57 } |
| 58 |
| 59 - (void)cancelWebOverlaysForWebState:(web::WebState*)webState { |
| 60 DCHECK(webState); |
| 61 // Remove all scheduled instances of |webState| from the queue. |
| 62 auto i = web_states_.begin(); |
| 63 while (i != web_states_.end()) { |
| 64 if (*i == webState) |
| 65 web_states_.erase(i); |
| 66 } |
| 67 // Remove all overlays from |webState|'s queue. |
| 68 WebOverlayQueue::FromWebState(webState)->CancelOverlays(); |
| 69 [self tryToStartNextOverlay]; |
| 70 } |
| 71 |
| 72 #pragma mark - |
| 73 |
| 74 - (void)tryToStartNextOverlay { |
| 75 // Early return if there are no overlays to show. |
| 76 if (web_states_.empty()) |
| 77 return; |
| 78 // Also early return if the front queue is already presenting an overlay. |
| 79 WebOverlayQueue* queue = WebOverlayQueue::FromWebState(web_states_.front()); |
| 80 if (queue->IsShowingOverlay()) |
| 81 return; |
| 82 // Show the next WebState's content view and start its overlay. |
| 83 [self.dispatcher showTabGridTabWithWebState:web_states_.front()]; |
| 84 [self.dispatcher startNextWebOverlayForWebState:web_states_.front()]; |
| 85 } |
| 86 |
| 87 @end |
OLD | NEW |