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..73466613c2b14d5b929c63e7c1894bbf4366dfc3 |
| --- /dev/null |
| +++ b/components/bubble/bubble_manager.cc |
| @@ -0,0 +1,99 @@ |
| +// 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" |
| +#include "content/public/browser/browser_thread.h" |
| + |
| +BubbleManager::~BubbleManager() { |
| + can_show_bubbles_ = false; |
| + CloseAllBubbles(BUBBLE_CLOSE_FORCED); |
| +} |
| + |
| +BubbleReference BubbleManager::ShowBubble(scoped_ptr<BubbleDelegate> bubble) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + DCHECK(bubble); |
| + scoped_ptr<BubbleController> controller(new BubbleController(bubble.Pass())); |
| + |
| + BubbleReference bubble_ref = controller->AsWeakPtr(); |
| + |
| + if (!can_show_bubbles_) { |
|
msw
2015/08/21 01:48:30
As I eluded to with my suggestion in the header, c
hcarmona
2015/08/25 02:13:37
Acknowledged.
|
| + // The controller will be cleaned up and this reference will be invalidated. |
| + // It's important that the controller is created even though it's destroyed |
| + // immediately because it will collect metrics about the bubble. |
| + return bubble_ref; |
| + } |
| + |
| + if (show_queue_) { |
| + show_queue_->push_back(controller.Pass()); |
| + } else { |
| + controller->Show(); |
| + controllers_.push_back(controller.Pass()); |
| + } |
| + |
| + return bubble_ref; |
| +} |
| + |
| +bool BubbleManager::CloseBubble(BubbleReference bubble, |
| + BubbleCloseReason reason) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + show_queue_.reset(new ScopedVector<BubbleController>); |
| + |
| + for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) { |
| + if (*iter == bubble.get()) { |
| + if ((*iter)->ShouldClose(reason)) { |
| + iter = controllers_.erase(iter); |
| + ShowPendingBubbles(); |
| + return true; |
| + } |
| + ShowPendingBubbles(); |
|
msw
2015/08/21 01:48:31
I don't think this call would be necessary... If t
hcarmona
2015/08/25 02:13:37
A bubble could theoretically trigger another bubbl
msw
2015/08/26 01:42:35
I guess so.
|
| + 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() { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + for (auto controller : controllers_) |
| + controller->UpdateAnchorPosition(); |
| +} |
| + |
| +BubbleManager::BubbleManager() : can_show_bubbles_(true) {} |
| + |
| +void BubbleManager::CloseAllBubbles(BubbleCloseReason reason) { |
| + DCHECK_CURRENTLY_ON(content::BrowserThread::UI); |
| + show_queue_.reset(new ScopedVector<BubbleController>); |
| + |
| + for (auto iter = controllers_.begin(); iter != controllers_.end();) { |
| + if ((*iter)->ShouldClose(reason)) { |
|
msw
2015/08/21 01:48:30
optionally consider: iter = (*iter)->ShouldClose(r
hcarmona
2015/08/25 02:13:37
Done.
|
| + iter = controllers_.erase(iter); |
| + continue; |
| + } |
| + ++iter; |
| + } |
| + |
| + ShowPendingBubbles(); |
| +} |
| + |
| +void BubbleManager::ShowPendingBubbles() { |
| + DCHECK(show_queue_); |
| + if (can_show_bubbles_) { |
| + for (auto controller : *show_queue_) |
| + controller->Show(); |
| + |
| + controllers_.insert(controllers_.end(), show_queue_->begin(), |
| + show_queue_->end()); |
| + |
| + show_queue_->weak_clear(); |
| + } |
| + show_queue_.reset(); |
| +} |