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

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: Apply Feedback Created 5 years, 4 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_delegate.h"
10 #include "content/public/browser/browser_thread.h"
11
12 BubbleManager::~BubbleManager() {
13 // The delegate should NOT outlive the manager. When a delegate is destroyed
msw 2015/08/13 03:37:23 Each delegate is owned by a controller, and there
hcarmona 2015/08/15 02:03:20 Assumption is no longer here. The controller will
14 // it should hide itself. This means that when the bubble manager is being
15 // destroyed there should be no visible bubbles.
16 DCHECK_EQ(controllers_.size(), 0);
17 }
18
19 BubbleReference BubbleManager::ShowBubble(scoped_ptr<BubbleDelegate> bubble) {
20 DCHECK(bubble);
21 DCHECK(bubble->GetContext());
22
23 scoped_ptr<BubbleController> controller(new BubbleController(bubble.Pass()));
24
25 BubbleReference bubble_ref = controller->AsWeakPtr();
26 ShowBubbleUI(bubble_ref);
27
28 controllers_.push_back(controller.Pass());
29 return bubble_ref;
msw 2015/08/13 03:37:23 By exposing a BubbleReference, don't you allow use
hcarmona 2015/08/13 20:50:35 True, do we have an existing pattern that would al
msw 2015/08/13 21:41:37 Maybe remove the controller functions or make them
hcarmona 2015/08/15 02:03:20 Added a TODO item, I'll look into this next week.
30 }
31
32 void BubbleManager::CloseBubble(BubbleReference bubble) {
msw 2015/08/13 03:37:23 q: might bubblemanager users ever want to specify
hcarmona 2015/08/15 02:03:20 Now they can :D
33 for (auto iter = controllers_.begin(); iter != controllers_.end(); ++iter) {
34 if ((*iter) == bubble.get()) {
msw 2015/08/13 03:37:23 nit: parens around *iter aren't really necessary.
hcarmona 2015/08/15 02:03:20 Done.
35 BubbleController* controller = *iter;
36 controllers_.weak_erase(iter);
37
38 controller->Hide(BUBBLE_CLOSE_IGNORE);
39 controller->Close();
msw 2015/08/13 03:37:23 nit: perhaps closing a bubble should first hide it
hcarmona 2015/08/15 02:03:20 No longer necessary.
40
41 // Dancing around this because some bubbles will chain another on close.
msw 2015/08/13 03:37:23 I'm not quite sure what you're saying here. Does c
hcarmona 2015/08/13 20:50:35 Permission bubbles will sometimes show another bub
msw 2015/08/13 21:41:37 Acknowledged; maybe rephrase the comment to make t
hcarmona 2015/08/15 02:03:20 Done. Better comment.
42 delete controller;
43 return;
44 }
45 }
46
47 // Hidden/unmanaged bubbles should not be hidden: this could indicate a bug.
msw 2015/08/13 03:37:23 nit: replace the colon with a semicolon or a comma
hcarmona 2015/08/15 02:03:20 Acknowledged.
48 NOTREACHED();
49 }
50
51 bool BubbleManager::ShouldUpdateBubble(BubbleReference bubble) {
52 return bubble->ShouldUpdateBubble();
53 }
54
55 void BubbleManager::UpdateBubble(BubbleReference bubble) {
56 if (bubble->ShouldUpdateBubble()) {
57 bubble->Hide(BUBBLE_CLOSE_IGNORE);
58 ShowBubbleUI(bubble);
59 }
60 }
61
62 BubbleManager::BubbleManager() {}
63
64 void BubbleManager::MassUpdateBubbles(
65 void* context,
66 base::Callback<void(base::WeakPtr<BubbleController>)> callback) {
67 DCHECK(context);
68
69 for (auto controller : controllers_) {
70 if (controller->MatchesContext(context))
71 callback.Run(controller->AsWeakPtr());
72 }
73 }
74
75 void BubbleManager::CloseMatchingBubbles(void* context,
76 CloseOption close_option,
77 BubbleCloseReason reason) {
78 DCHECK(context);
79
80 std::vector<BubbleController*> closing;
81
82 for (auto iter = controllers_.begin(); iter != controllers_.end();) {
83 if ((*iter)->MatchesContext(context)) {
84 (*iter)->Hide(reason);
85 if ((close_option == FORCE_CLOSE) || (*iter)->ShouldClose()) {
86 // Don't call close here to avoid bubbles trying to show the next in a
msw 2015/08/13 03:37:23 Ugh, horrifying... This is why BubbleManager shoul
groby-ooo-7-16 2015/08/13 18:44:38 The problem is that bubbles will be shown _in resp
msw 2015/08/13 19:18:58 Okay, this pattern (weak_erase first then Close la
87 // series and messing up iterating the controllers.
88 // I'm looking at you, permission bubbles!
89 closing.push_back(*iter);
90 iter = controllers_.weak_erase(iter);
91 continue;
92 }
93 }
94 ++iter;
msw 2015/08/13 03:37:23 Shouldn't this also be called if a bubble matches
hcarmona 2015/08/13 20:50:35 Yes, it's outside both checks.
msw 2015/08/13 21:41:37 Oh duh, you're right, sorry.
95 }
96
97 for (auto iter : closing) {
98 iter->Close();
99 // No need to remove from the vector.
100 delete iter;
msw 2015/08/13 03:37:22 ditto: dtor should call close or vice versa.
hcarmona 2015/08/15 02:03:20 Acknowledged.
101 }
102 }
103
104 void BubbleManager::ShowBubbleUI(base::WeakPtr<BubbleController> controller) {
105 DCHECK(content::BrowserThread::CurrentlyOn(content::BrowserThread::UI));
msw 2015/08/13 03:37:23 As I noted in the DEPS change, this seems like a j
hcarmona 2015/08/15 02:03:20 Done.
106 controller->Show();
107 }
108
109 void BubbleManager::UpdateBubbleUI(base::WeakPtr<BubbleController> controller) {
110 controller->UpdateAnchorPosition();
msw 2015/08/13 03:37:23 nit: unify naming... the layers of controllers and
hcarmona 2015/08/15 02:03:20 Done.
111 }
OLDNEW

Powered by Google App Engine
This is Rietveld 408576698