Chromium Code Reviews
chromiumcodereview-hr@appspot.gserviceaccount.com (chromiumcodereview-hr) | Please choose your nickname with Settings | Help | Chromium Project | Gerrit Changes | Sign out
(216)

Unified Diff: ios/clean/chrome/browser/ui/overlays/overlay_queue.mm

Issue 2921833002: [iOS Clean] Created OverlayService.
Patch Set: Use service pattern Created 3 years, 6 months ago
Use n/p to move between diff chunks; N/P to move between comments. Draft comments are only viewable by you.
Jump to:
View side-by-side diff with in-line comments
Download patch
Index: ios/clean/chrome/browser/ui/overlays/overlay_queue.mm
diff --git a/ios/clean/chrome/browser/ui/overlays/overlay_queue.mm b/ios/clean/chrome/browser/ui/overlays/overlay_queue.mm
new file mode 100644
index 0000000000000000000000000000000000000000..8b447ce4a337a09751d307924f2d30021d71ff04
--- /dev/null
+++ b/ios/clean/chrome/browser/ui/overlays/overlay_queue.mm
@@ -0,0 +1,97 @@
+// 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_queue.h"
+
+#include "base/logging.h"
+
+#if !defined(__has_feature) || !__has_feature(objc_arc)
+#error "This file requires ARC support."
+#endif
+
+OverlayQueue::OverlayQueue()
+ : overlays_([[NSMutableArray<BrowserCoordinator*> alloc] init]) {}
+
+OverlayQueue::~OverlayQueue() {
+ CancelOverlays();
+}
+
+void OverlayQueue::AddObserver(OverlayQueueObserver* observer) {
+ observers_.AddObserver(observer);
+}
+
+void OverlayQueue::RemoveObserver(OverlayQueueObserver* observer) {
+ observers_.RemoveObserver(observer);
+}
+
+void OverlayQueue::OverlayWasStopped(BrowserCoordinator* overlay_coordinator) {
+ DCHECK(overlay_coordinator);
+ DCHECK_EQ(GetFirstOverlay(), overlay_coordinator);
+ DCHECK(showing_overlay_);
+ [overlays_ removeObjectAtIndex:0];
+ showing_overlay_ = false;
+ for (auto& observer : observers_)
+ observer.OverlayQueueDidStopVisibleOverlay(this);
+}
+
+void OverlayQueue::ReplaceVisibleOverlay(
+ BrowserCoordinator* overlay_coordinator) {
+ DCHECK(overlay_coordinator);
+ DCHECK(IsShowingOverlay());
+ DCHECK_GE([overlays_ count], 1U);
+ // Add the overlay after the currently displayed overlay and notify observers
+ // before stopping the current overlay.
+ for (auto& observer : observers_)
+ observer.OverlayQueueWillReplaceVisibleOverlay(this);
+ [overlays_ insertObject:overlay_coordinator atIndex:1];
+ overlay_coordinator.queue = this;
+ BrowserCoordinator* visible_overlay = GetFirstOverlay();
+ [visible_overlay cancelOverlay];
+ [visible_overlay stop];
+}
+
+bool OverlayQueue::HasQueuedOverlays() const {
+ return GetCount() > 0U;
+}
+
+bool OverlayQueue::IsShowingOverlay() const {
+ return showing_overlay_;
+}
+
+void OverlayQueue::CancelOverlays() {
+ // Cancel all overlays in the queue.
+ for (BrowserCoordinator* overlay_coordinator in overlays_) {
marq (ping after 24h) 2017/06/21 09:41:53 Braceless loops are okay, but be consistent with t
kkhorimoto 2017/06/23 06:11:16 If we need to add a newline after the loop, going
+ [overlay_coordinator cancelOverlay];
+ }
+ for (auto& observer : observers_)
+ observer.OverlayQueueDidCancelOverlays(this);
+ // Stop the visible overlay before emptying the queue.
+ [GetFirstOverlay() stop];
+ [overlays_ removeAllObjects];
+}
+
+web::WebState* OverlayQueue::GetWebState() const {
+ return nullptr;
+}
+
+void OverlayQueue::AddOverlay(BrowserCoordinator* overlay_coordinator) {
+ // Add the overlay coordinator to the queue, then notify observers.
+ DCHECK(overlay_coordinator);
+ [overlays_ addObject:overlay_coordinator];
+ overlay_coordinator.queue = this;
+ for (auto& observer : observers_)
+ observer.OverlayQueueDidAddOverlay(this);
+}
+
+NSUInteger OverlayQueue::GetCount() const {
+ return [overlays_ count];
+}
+
+BrowserCoordinator* OverlayQueue::GetFirstOverlay() {
+ return HasQueuedOverlays() ? [overlays_ firstObject] : nil;
+}
+
+void OverlayQueue::OverlayWasStarted() {
+ showing_overlay_ = true;
+}

Powered by Google App Engine
This is Rietveld 408576698