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

Side by Side Diff: components/bubble/bubble_manager.cc

Issue 1251633002: Add BubbleManager to manage bubbles and ChromeBubbleManager for events. (Closed) Base URL: https://chromium.googlesource.com/chromium/src.git@master
Patch Set: Feedback Created 5 years, 3 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 unified diff | Download patch
OLDNEW
(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 DCHECK_NE(manager_state_, QUEUE_BUBBLES);
50 if (manager_state_ == SHOW_BUBBLES)
51 manager_state_ = QUEUE_BUBBLES;
52
53 for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) {
54 if (*iter == bubble.get()) {
55 bool closed = (*iter)->ShouldClose(reason);
56 if (closed)
57 iter = controllers_.erase(iter);
58 ShowPendingBubbles();
59 return closed;
60 }
61 }
62
63 // Attempting to close a bubble that is already closed or that this manager
64 // doesn't own is a bug.
65 NOTREACHED();
66 return false;
67 }
68
69 void BubbleManager::CloseAllBubbles(BubbleCloseReason reason) {
70 // The following close reasons don't make sense for multiple bubbles:
71 DCHECK_NE(reason, BUBBLE_CLOSE_ACCEPTED);
72 DCHECK_NE(reason, BUBBLE_CLOSE_CANCELED);
73
74 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
75 DCHECK_NE(manager_state_, QUEUE_BUBBLES);
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 ShowPendingBubbles();
83 }
84
85 void BubbleManager::UpdateAllBubbleAnchors() {
86 DCHECK_CURRENTLY_ON(content::BrowserThread::UI);
87 for (auto controller : controllers_)
88 controller->UpdateAnchorPosition();
89 }
90
91 void BubbleManager::ShowPendingBubbles() {
92 if (manager_state_ == QUEUE_BUBBLES)
93 manager_state_ = SHOW_BUBBLES;
94
95 if (manager_state_ == SHOW_BUBBLES) {
96 for (auto controller : show_queue_)
97 controller->Show();
98
99 controllers_.insert(controllers_.end(), show_queue_.begin(),
100 show_queue_.end());
101
102 show_queue_.weak_clear();
103 } else {
104 // Clear the queue if bubbles can't be shown.
105 show_queue_.clear();
106 }
107 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698