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_queue.h" |
| 6 |
| 7 #include "base/logging.h" |
| 8 #import "ios/clean/chrome/browser/ui/commands/overlay_commands.h" |
| 9 #import "ios/shared/chrome/browser/ui/coordinators/browser_coordinator.h" |
| 10 |
| 11 #if !defined(__has_feature) || !__has_feature(objc_arc) |
| 12 #error "This file requires ARC support." |
| 13 #endif |
| 14 |
| 15 DEFINE_WEB_STATE_USER_DATA_KEY(OverlayQueue); |
| 16 |
| 17 // static |
| 18 void OverlayQueue::CreateForWebState(web::WebState* web_state, |
| 19 id<OverlaySchedulerCommands> dispatcher) { |
| 20 DCHECK(web_state); |
| 21 DCHECK(dispatcher); |
| 22 if (!FromWebState(web_state)) { |
| 23 std::unique_ptr<OverlayQueue> queue = |
| 24 base::WrapUnique(new OverlayQueue(web_state, dispatcher)); |
| 25 web_state->SetUserData(UserDataKey(), std::move(queue)); |
| 26 } |
| 27 } |
| 28 |
| 29 OverlayQueue::OverlayQueue(web::WebState* web_state, |
| 30 id<OverlaySchedulerCommands> dispatcher) |
| 31 : web_state_(web_state), |
| 32 dispatcher_(dispatcher), |
| 33 overlays_([[NSMutableArray alloc] init]), |
| 34 showing_overlay_(false) { |
| 35 DCHECK(web_state); |
| 36 DCHECK(dispatcher); |
| 37 } |
| 38 |
| 39 OverlayQueue::~OverlayQueue() { |
| 40 [dispatcher_ cancelOverlaysForWebState:web_state_]; |
| 41 } |
| 42 |
| 43 void OverlayQueue::AddOverlay(OverlayCoordinator* overlay_coordinator) { |
| 44 // Add the overlay coordinator to the queue, then notify the scheduler. |
| 45 DCHECK(overlay_coordinator); |
| 46 [overlays_ addObject:overlay_coordinator]; |
| 47 [dispatcher_ scheduleOverlayForWebState:web_state_]; |
| 48 } |
| 49 |
| 50 void OverlayQueue::ReplaceVisibleOverlay( |
| 51 OverlayCoordinator* overlay_coordinator) { |
| 52 DCHECK(overlay_coordinator); |
| 53 DCHECK(IsShowingOverlay()); |
| 54 DCHECK_GE([overlays_ count], 1U); |
| 55 // Add the overlay after the currently displayed overlay and notify the |
| 56 // scheduler that the overlay was replaced. Stopping the front overlay below |
| 57 // will cause |overlay_cordinator| to start. |
| 58 [overlays_ insertObject:overlay_coordinator atIndex:1]; |
| 59 [dispatcher_ visibleOverlayWasReplacedForWebState:web_state_]; |
| 60 OverlayCoordinator* visible_overlay = [overlays_ firstObject]; |
| 61 [visible_overlay cancelOverlay]; |
| 62 [visible_overlay stop]; |
| 63 } |
| 64 |
| 65 bool OverlayQueue::HasQueuedOverlays() const { |
| 66 return [overlays_ count] > 0U; |
| 67 } |
| 68 |
| 69 void OverlayQueue::StartNextOverlay(BrowserCoordinator* web_coordinator) { |
| 70 DCHECK(web_coordinator); |
| 71 DCHECK(HasQueuedOverlays()); |
| 72 DCHECK(!IsShowingOverlay()); |
| 73 [[overlays_ firstObject] startOverlayingCoordinator:web_coordinator]; |
| 74 showing_overlay_ = true; |
| 75 } |
| 76 |
| 77 bool OverlayQueue::IsShowingOverlay() const { |
| 78 return showing_overlay_; |
| 79 } |
| 80 |
| 81 void OverlayQueue::OverlayWasStopped(OverlayCoordinator* overlay_coordinator) { |
| 82 DCHECK(overlay_coordinator); |
| 83 DCHECK_EQ([overlays_ firstObject], overlay_coordinator); |
| 84 DCHECK(showing_overlay_); |
| 85 [overlays_ removeObjectAtIndex:0]; |
| 86 showing_overlay_ = false; |
| 87 [dispatcher_ overlayWasStoppedForWebState:web_state_]; |
| 88 } |
| 89 |
| 90 void OverlayQueue::CancelOverlays() { |
| 91 // Call the WebKit completion blocks for all queued overlays. |
| 92 for (OverlayCoordinator* overlay_coordinator in overlays_) { |
| 93 [overlay_coordinator cancelOverlay]; |
| 94 } |
| 95 // Remove the queued overlays and stop the visible overlay if one is |
| 96 // presented. Stopping the visible overlay will trigger the next scheduled |
| 97 // overlay, so the queue should be emptied before calling |-stop|. |
| 98 OverlayCoordinator* visible_overlay = |
| 99 showing_overlay_ ? [overlays_ firstObject] : nil; |
| 100 [overlays_ removeAllObjects]; |
| 101 [visible_overlay stop]; |
| 102 } |
OLD | NEW |