Chromium Code Reviews| OLD | NEW |
|---|---|
| (Empty) | |
| 1 // Copyright 2015 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 #include "components/bubble/bubble_manager.h" | |
| 6 | |
| 7 #include <vector> | |
| 8 | |
| 9 #include "components/bubble/bubble_controller.h" | |
| 10 #include "components/bubble/bubble_delegate.h" | |
| 11 #include "content/public/browser/browser_thread.h" | |
| 12 | |
| 13 BubbleManager::BubbleManager() : manager_state_(SHOW_BUBBLES) {} | |
| 14 | |
| 15 BubbleManager::~BubbleManager() { | |
| 16 manager_state_ = NO_MORE_BUBBLES; | |
| 17 CloseAllBubbles(BUBBLE_CLOSE_FORCED); | |
| 18 } | |
| 19 | |
| 20 BubbleReference BubbleManager::ShowBubble(scoped_ptr<BubbleDelegate> bubble) { | |
| 21 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 22 DCHECK(bubble); | |
| 23 scoped_ptr<BubbleController> controller( | |
| 24 new BubbleController(this, bubble.Pass())); | |
| 25 | |
| 26 BubbleReference bubble_ref = controller->AsWeakPtr(); | |
| 27 | |
| 28 switch (manager_state_) { | |
| 29 case SHOW_BUBBLES: | |
| 30 controller->Show(); | |
| 31 controllers_.push_back(controller.Pass()); | |
| 32 break; | |
| 33 case QUEUE_BUBBLES: | |
| 34 show_queue_.push_back(controller.Pass()); | |
| 35 break; | |
| 36 case NO_MORE_BUBBLES: | |
| 37 // The controller will be cleaned up and |bubble_ref| will be invalidated. | |
| 38 // It's important that the controller is created even though it's | |
| 39 // destroyed immediately because it will collect metrics about the bubble. | |
| 40 break; | |
| 41 } | |
| 42 | |
| 43 return bubble_ref; | |
| 44 } | |
| 45 | |
| 46 bool BubbleManager::CloseBubble(BubbleReference bubble, | |
| 47 BubbleCloseReason reason) { | |
| 48 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 49 if (manager_state_ == SHOW_BUBBLES) | |
| 50 manager_state_ = QUEUE_BUBBLES; | |
| 51 | |
| 52 for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) { | |
| 53 if (*iter == bubble.get()) { | |
| 54 bool closed = (*iter)->ShouldClose(reason); | |
| 55 if (closed) | |
| 56 iter = controllers_.erase(iter); | |
| 57 if (manager_state_ == QUEUE_BUBBLES) | |
|
msw
2015/08/26 01:42:36
Hmm, I wonder if this function could somehow be ca
hcarmona
2015/08/26 17:25:58
If a bubble closed another bubble through the mana
| |
| 58 manager_state_ = SHOW_BUBBLES; | |
| 59 ShowPendingBubbles(); | |
| 60 return closed; | |
| 61 } | |
| 62 } | |
| 63 | |
| 64 // Attempting to close a bubble that is already closed or that this manager | |
| 65 // doesn't own is a bug. | |
| 66 NOTREACHED(); | |
| 67 return false; | |
| 68 } | |
| 69 | |
| 70 void BubbleManager::CloseAllBubbles(BubbleCloseReason reason) { | |
| 71 // The following close reasons don't make sense for multiple bubbles: | |
| 72 DCHECK_NE(reason, BUBBLE_CLOSE_ACCEPTED); | |
| 73 DCHECK_NE(reason, BUBBLE_CLOSE_CANCELED); | |
| 74 | |
| 75 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 76 if (manager_state_ == SHOW_BUBBLES) | |
| 77 manager_state_ = QUEUE_BUBBLES; | |
| 78 | |
| 79 for (auto iter = controllers_.begin(); iter != controllers_.end();) | |
| 80 iter = (*iter)->ShouldClose(reason) ? controllers_.erase(iter) : iter + 1; | |
| 81 | |
| 82 if (manager_state_ == QUEUE_BUBBLES) | |
| 83 manager_state_ = SHOW_BUBBLES; | |
| 84 ShowPendingBubbles(); | |
| 85 } | |
| 86 | |
| 87 void BubbleManager::UpdateAllBubbleAnchors() { | |
| 88 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 89 for (auto controller : controllers_) | |
| 90 controller->UpdateAnchorPosition(); | |
| 91 } | |
| 92 | |
| 93 void BubbleManager::ShowPendingBubbles() { | |
| 94 if (manager_state_ == SHOW_BUBBLES) { | |
| 95 for (auto controller : show_queue_) | |
| 96 controller->Show(); | |
| 97 | |
| 98 controllers_.insert(controllers_.end(), show_queue_.begin(), | |
| 99 show_queue_.end()); | |
| 100 | |
| 101 show_queue_.weak_clear(); | |
| 102 } else { | |
| 103 // Clear the queue if bubbles can't be shown. | |
| 104 show_queue_.clear(); | |
| 105 } | |
| 106 } | |
| OLD | NEW |