OLD | NEW |
| (Empty) |
1 // Copyright (c) 2012 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 "chrome/browser/chromeos/notifications/balloon_collection_impl.h" | |
6 | |
7 #include <algorithm> | |
8 | |
9 #include "base/logging.h" | |
10 #include "chrome/browser/chromeos/notifications/balloon_view.h" | |
11 #include "chrome/browser/chromeos/notifications/notification_panel.h" | |
12 #include "chrome/browser/notifications/balloon.h" | |
13 #include "chrome/browser/notifications/notification.h" | |
14 #include "chrome/browser/ui/browser_list.h" | |
15 #include "chrome/common/chrome_notification_types.h" | |
16 #include "content/public/browser/notification_service.h" | |
17 #include "ui/gfx/rect.h" | |
18 #include "ui/gfx/size.h" | |
19 | |
20 namespace { | |
21 | |
22 // Margin from the edge of the work area | |
23 const int kVerticalEdgeMargin = 5; | |
24 const int kHorizontalEdgeMargin = 5; | |
25 | |
26 } // namespace | |
27 | |
28 namespace chromeos { | |
29 | |
30 BalloonCollectionImpl::BalloonCollectionImpl() | |
31 : notification_ui_(new NotificationPanel()) { | |
32 registrar_.Add(this, chrome::NOTIFICATION_BROWSER_CLOSED, | |
33 content::NotificationService::AllSources()); | |
34 } | |
35 | |
36 BalloonCollectionImpl::~BalloonCollectionImpl() { | |
37 Shutdown(); | |
38 } | |
39 | |
40 void BalloonCollectionImpl::Add(const Notification& notification, | |
41 Profile* profile) { | |
42 Balloon* new_balloon = MakeBalloon(notification, profile); | |
43 base_.Add(new_balloon, false); | |
44 new_balloon->Show(); | |
45 notification_ui_->Add(new_balloon); | |
46 | |
47 // There may be no listener in a unit test. | |
48 if (space_change_listener_) | |
49 space_change_listener_->OnBalloonSpaceChanged(); | |
50 | |
51 // This is used only for testing. | |
52 if (!on_collection_changed_callback_.is_null()) | |
53 on_collection_changed_callback_.Run(); | |
54 } | |
55 | |
56 bool BalloonCollectionImpl::AddWebUIMessageCallback( | |
57 const Notification& notification, | |
58 const std::string& message, | |
59 const BalloonViewHost::MessageCallback& callback) { | |
60 Balloon* balloon = FindBalloon(notification); | |
61 if (!balloon) | |
62 return false; | |
63 | |
64 BalloonViewHost* host = | |
65 static_cast<BalloonViewHost*>(balloon->view()->GetHost()); | |
66 return host->AddWebUIMessageCallback(message, callback); | |
67 } | |
68 | |
69 // Called from SystemNotification::Show for system notifications. | |
70 void BalloonCollectionImpl::AddSystemNotification( | |
71 const Notification& notification, | |
72 Profile* profile, | |
73 bool sticky) { | |
74 Balloon* new_balloon = new Balloon(notification, profile, this); | |
75 new_balloon->set_view( | |
76 new chromeos::BalloonViewImpl( | |
77 sticky, false /*no controls*/, true /*enable webui*/)); | |
78 base_.Add(new_balloon, false); | |
79 new_balloon->Show(); | |
80 notification_ui_->Add(new_balloon); | |
81 | |
82 // There may be no listener in a unit test. | |
83 if (space_change_listener_) | |
84 space_change_listener_->OnBalloonSpaceChanged(); | |
85 } | |
86 | |
87 bool BalloonCollectionImpl::UpdateNotification( | |
88 const Notification& notification) { | |
89 Balloon* balloon = FindBalloon(notification); | |
90 if (!balloon) | |
91 return false; | |
92 balloon->Update(notification); | |
93 notification_ui_->Update(balloon); | |
94 return true; | |
95 } | |
96 | |
97 bool BalloonCollectionImpl::UpdateAndShowNotification( | |
98 const Notification& notification) { | |
99 Balloon* balloon = FindBalloon(notification); | |
100 if (!balloon) | |
101 return false; | |
102 balloon->Update(notification); | |
103 bool updated = notification_ui_->Update(balloon); | |
104 DCHECK(updated); | |
105 notification_ui_->Show(balloon); | |
106 return true; | |
107 } | |
108 | |
109 bool BalloonCollectionImpl::RemoveById(const std::string& id) { | |
110 return base_.CloseById(id); | |
111 } | |
112 | |
113 bool BalloonCollectionImpl::RemoveBySourceOrigin(const GURL& origin) { | |
114 return base_.CloseAllBySourceOrigin(origin); | |
115 } | |
116 | |
117 void BalloonCollectionImpl::RemoveAll() { | |
118 base_.CloseAll(); | |
119 } | |
120 | |
121 bool BalloonCollectionImpl::HasSpace() const { | |
122 return true; | |
123 } | |
124 | |
125 void BalloonCollectionImpl::ResizeBalloon(Balloon* balloon, | |
126 const gfx::Size& size) { | |
127 notification_ui_->ResizeNotification(balloon, size); | |
128 } | |
129 | |
130 void BalloonCollectionImpl::OnBalloonClosed(Balloon* source) { | |
131 notification_ui_->Remove(source); | |
132 base_.Remove(source); // Deletes |source|. | |
133 | |
134 // There may be no listener in a unit test. | |
135 if (space_change_listener_) | |
136 space_change_listener_->OnBalloonSpaceChanged(); | |
137 | |
138 // This is used only for testing. | |
139 if (!on_collection_changed_callback_.is_null()) | |
140 on_collection_changed_callback_.Run(); | |
141 } | |
142 | |
143 const BalloonCollectionImpl::Balloons& | |
144 BalloonCollectionImpl::GetActiveBalloons() { | |
145 return base_.balloons(); | |
146 } | |
147 | |
148 void BalloonCollectionImpl::Observe( | |
149 int type, | |
150 const content::NotificationSource& source, | |
151 const content::NotificationDetails& details) { | |
152 DCHECK(type == chrome::NOTIFICATION_BROWSER_CLOSED); | |
153 bool app_closing = *content::Details<bool>(details).ptr(); | |
154 // When exiting, we need to shutdown all renderers in | |
155 // BalloonViewImpl before IO thread gets deleted in the | |
156 // BrowserProcessImpl's destructor. See http://crbug.com/40810 | |
157 // for details. | |
158 if (app_closing) | |
159 RemoveAll(); | |
160 } | |
161 | |
162 void BalloonCollectionImpl::Shutdown() { | |
163 // We need to remove the panel first because deleting | |
164 // views that are not owned by parent will not remove | |
165 // themselves from the parent. | |
166 DVLOG(1) << "Shutting down notification UI"; | |
167 notification_ui_.reset(); | |
168 } | |
169 | |
170 // Called from BalloonCollectionImpl::Add for non system notifications. | |
171 Balloon* BalloonCollectionImpl::MakeBalloon(const Notification& notification, | |
172 Profile* profile) { | |
173 Balloon* new_balloon = new Balloon(notification, profile, this); | |
174 new_balloon->set_view(new chromeos::BalloonViewImpl( | |
175 false /*not sticky*/, true /*has controls*/, false /*no web ui*/)); | |
176 return new_balloon; | |
177 } | |
178 | |
179 } // namespace chromeos | |
180 | |
181 // static | |
182 BalloonCollection* BalloonCollection::Create() { | |
183 return new chromeos::BalloonCollectionImpl(); | |
184 } | |
OLD | NEW |