Chromium Code Reviews| Index: components/bubble/bubble_manager.cc |
| diff --git a/components/bubble/bubble_manager.cc b/components/bubble/bubble_manager.cc |
| new file mode 100644 |
| index 0000000000000000000000000000000000000000..8633ed0082ab3a8e5fd708c439400ec9ff3cb1c4 |
| --- /dev/null |
| +++ b/components/bubble/bubble_manager.cc |
| @@ -0,0 +1,86 @@ |
| +// Copyright 2015 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. |
| + |
| +#include "components/bubble/bubble_manager.h" |
| + |
| +#include <vector> |
| + |
| +#include "components/bubble/bubble_controller.h" |
| + |
| +BubbleManager::~BubbleManager() {} |
| + |
| +BubbleReference BubbleManager::ShowBubble(scoped_ptr<BubbleDelegate> bubble) { |
| + DCHECK(bubble); |
| + scoped_ptr<BubbleController> controller(new BubbleController(bubble.Pass())); |
| + |
| + BubbleReference bubble_ref = controller->AsWeakPtr(); |
| + ShowBubbleUI(bubble_ref); |
| + |
| + controllers_.push_back(controller.Pass()); |
| + return bubble_ref; |
| +} |
| + |
| +bool BubbleManager::CloseBubble(BubbleReference bubble, |
| + BubbleCloseReason reason) { |
| + for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) { |
| + if (*iter == bubble.get()) { |
| + if (ShouldClose((*iter)->AsWeakPtr(), reason)) { |
| + // Remove the controller before attempting to delete because some |
| + // delegates may chain showing more bubbles. |
| + BubbleController* controller = *iter; |
| + controllers_.weak_erase(iter); |
|
msw
2015/08/18 17:26:19
This should update iter, otherwise it'll be invali
hcarmona
2015/08/18 23:08:48
Done.
|
| + delete controller; |
| + return true; |
| + } |
| + return false; |
| + } |
| + } |
| + |
| + // Attempting to close a bubble that is already closed or that this manager |
| + // doesn't own is a bug. |
| + NOTREACHED(); |
| + return false; |
| +} |
| + |
| +void BubbleManager::UpdateAllBubbleAnchors() { |
| + for (auto controller : controllers_) { |
|
msw
2015/08/18 17:26:19
nit: curly braces not needed here.
hcarmona
2015/08/18 23:08:48
Done.
|
| + UpdateAnchorPosition(controller->AsWeakPtr()); |
| + } |
| +} |
| + |
| +BubbleManager::BubbleManager() {} |
| + |
| +void BubbleManager::CloseAllBubbles(BubbleCloseReason reason) { |
| + std::vector<BubbleController*> closing; |
| + |
| + for (auto iter = controllers_.begin(); iter != controllers_.end();) { |
| + if (ShouldClose((*iter)->AsWeakPtr(), reason)) { |
| + // Remove the controllers before attempting to delete because some |
|
msw
2015/08/18 17:26:19
What happens on BubbleManager destruction (browser
hcarmona
2015/08/18 23:08:48
Yes, I'll create a test for this scenario.
Added
msw
2015/08/19 00:18:11
sgtm, thanks.
|
| + // delegates may chain showing more bubbles. |
| + closing.push_back(*iter); |
| + iter = controllers_.weak_erase(iter); |
| + continue; |
| + } |
| + ++iter; |
| + } |
| + |
| + for (auto iter : closing) { |
|
msw
2015/08/18 17:26:20
Use STLDeleteElements or STLElementDeleter.
hcarmona
2015/08/18 23:08:48
Done.
|
| + delete iter; |
| + // No need to remove from the vector after deleting. |
| + } |
| +} |
| + |
| +void BubbleManager::ShowBubbleUI(base::WeakPtr<BubbleController> controller) { |
|
msw
2015/08/18 17:26:20
Why not just inline this?
hcarmona
2015/08/18 23:08:48
Can't inline virtual functions. These functions ar
msw
2015/08/19 00:18:11
Sorry, what I meant is that we shouldn't define a
hcarmona
2015/08/20 02:34:31
Done. Moved the UI thread check to here.
|
| + controller->Show(); |
| +} |
| + |
| +bool BubbleManager::ShouldClose(base::WeakPtr<BubbleController> controller, |
|
msw
2015/08/18 17:26:20
Ditto: Inline this.
hcarmona
2015/08/18 23:08:48
Same: virtual
|
| + BubbleCloseReason reason) { |
| + return controller->ShouldClose(reason); |
| +} |
| + |
| +void BubbleManager::UpdateAnchorPosition( |
|
msw
2015/08/18 17:26:20
Ditto: Inline this.
hcarmona
2015/08/18 23:08:48
Same: virtual
|
| + base::WeakPtr<BubbleController> controller) { |
| + controller->UpdateAnchorPosition(); |
| +} |