| 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 |
| 11 BubbleManager::~BubbleManager() {} |
| 12 |
| 13 BubbleReference BubbleManager::ShowBubble(scoped_ptr<BubbleDelegate> bubble) { |
| 14 DCHECK(bubble); |
| 15 scoped_ptr<BubbleController> controller(new BubbleController(bubble.Pass())); |
| 16 |
| 17 BubbleReference bubble_ref = controller->AsWeakPtr(); |
| 18 ShowBubbleUI(bubble_ref); |
| 19 |
| 20 controllers_.push_back(controller.Pass()); |
| 21 return bubble_ref; |
| 22 } |
| 23 |
| 24 bool BubbleManager::CloseBubble(BubbleReference bubble, |
| 25 BubbleCloseReason reason) { |
| 26 for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) { |
| 27 if (*iter == bubble.get()) { |
| 28 if (ShouldClose((*iter)->AsWeakPtr(), reason)) { |
| 29 // Remove the controller before attempting to delete because some |
| 30 // delegates may chain showing more bubbles. |
| 31 BubbleController* controller = *iter; |
| 32 controllers_.weak_erase(iter); |
| 33 delete controller; |
| 34 return true; |
| 35 } |
| 36 return false; |
| 37 } |
| 38 } |
| 39 |
| 40 // Attempting to close a bubble that is already closed or that this manager |
| 41 // doesn't own is a bug. |
| 42 NOTREACHED(); |
| 43 return false; |
| 44 } |
| 45 |
| 46 void BubbleManager::UpdateAllBubbleAnchors() { |
| 47 for (auto controller : controllers_) { |
| 48 UpdateAnchorPosition(controller->AsWeakPtr()); |
| 49 } |
| 50 } |
| 51 |
| 52 BubbleManager::BubbleManager() {} |
| 53 |
| 54 void BubbleManager::CloseAllBubbles(BubbleCloseReason reason) { |
| 55 std::vector<BubbleController*> closing; |
| 56 |
| 57 for (auto iter = controllers_.begin(); iter != controllers_.end();) { |
| 58 if (ShouldClose((*iter)->AsWeakPtr(), reason)) { |
| 59 // Remove the controllers before attempting to delete because some |
| 60 // delegates may chain showing more bubbles. |
| 61 closing.push_back(*iter); |
| 62 iter = controllers_.weak_erase(iter); |
| 63 continue; |
| 64 } |
| 65 ++iter; |
| 66 } |
| 67 |
| 68 for (auto iter : closing) { |
| 69 delete iter; |
| 70 // No need to remove from the vector after deleting. |
| 71 } |
| 72 } |
| 73 |
| 74 void BubbleManager::ShowBubbleUI(base::WeakPtr<BubbleController> controller) { |
| 75 controller->Show(); |
| 76 } |
| 77 |
| 78 bool BubbleManager::ShouldClose(base::WeakPtr<BubbleController> controller, |
| 79 BubbleCloseReason reason) { |
| 80 return controller->ShouldClose(reason); |
| 81 } |
| 82 |
| 83 void BubbleManager::UpdateAnchorPosition( |
| 84 base::WeakPtr<BubbleController> controller) { |
| 85 controller->UpdateAnchorPosition(); |
| 86 } |
| OLD | NEW |