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/ui/views/ash/balloon_collection_impl_ash.h" | |
6 | |
7 #include "ash/shell.h" | |
8 #include "ash/system/web_notification/web_notification_tray.h" | |
9 #include "base/utf_string_conversions.h" | |
10 #include "base/values.h" | |
11 #include "chrome/browser/extensions/extension_service.h" | |
12 #include "chrome/browser/notifications/balloon.h" | |
13 #include "chrome/browser/notifications/desktop_notification_service.h" | |
14 #include "chrome/browser/notifications/desktop_notification_service_factory.h" | |
15 #include "chrome/browser/notifications/message_center_settings_controller.h" | |
16 #include "chrome/browser/notifications/notification.h" | |
17 #include "chrome/browser/profiles/profile_manager.h" | |
18 #include "chrome/browser/ui/browser_finder.h" | |
19 #include "chrome/browser/ui/chrome_pages.h" | |
20 #include "chrome/browser/ui/views/ash/balloon_view_ash.h" | |
21 #include "chrome/browser/ui/views/notifications/balloon_view_host.h" | |
22 #include "chrome/browser/ui/views/notifications/balloon_view_views.h" | |
23 #include "chrome/common/extensions/extension.h" | |
24 #include "chrome/common/extensions/extension_constants.h" | |
25 #include "chrome/common/extensions/extension_set.h" | |
26 #include "chrome/common/extensions/permissions/api_permission.h" | |
27 #include "ui/message_center/views/notifier_settings_view.h" | |
28 #include "ui/views/widget/widget.h" | |
29 | |
30 BalloonCollectionImplAsh::BalloonCollectionImplAsh() | |
31 : settings_controller_(new MessageCenterSettingsController) { | |
32 ash::Shell::GetInstance()->GetWebNotificationTray()->message_center()-> | |
33 SetDelegate(this); | |
34 } | |
35 | |
36 BalloonCollectionImplAsh::~BalloonCollectionImplAsh() { | |
37 } | |
38 | |
39 bool BalloonCollectionImplAsh::HasSpace() const { | |
40 return true; // Overflow is handled by ash::WebNotificationTray. | |
41 } | |
42 | |
43 void BalloonCollectionImplAsh::Add(const Notification& notification, | |
44 Profile* profile) { | |
45 if (notification.is_html()) | |
46 return; // HTML notifications are not supported in Ash. | |
47 if (notification.title().empty() && notification.body().empty()) | |
48 return; // Empty notification, don't show. | |
49 // TODO(mukai): add a filter if the source extension is disabled or not. | |
50 // The availability can be checked from DesktopNotificationService. | |
51 return BalloonCollectionImpl::Add(notification, profile); | |
52 } | |
53 | |
54 void BalloonCollectionImplAsh::DisableExtension( | |
55 const std::string& notification_id) { | |
56 Balloon* balloon = base().FindBalloonById(notification_id); | |
57 const extensions::Extension* extension = GetBalloonExtension(balloon); | |
58 if (!extension) | |
59 return; | |
60 balloon->profile()->GetExtensionService()->DisableExtension( | |
61 extension->id(), extensions::Extension::DISABLE_USER_ACTION); | |
62 } | |
63 | |
64 void BalloonCollectionImplAsh::DisableNotificationsFromSource( | |
65 const std::string& notification_id) { | |
66 Balloon* balloon = base().FindBalloonById(notification_id); | |
67 if (!balloon) | |
68 return; | |
69 DesktopNotificationService* service = | |
70 DesktopNotificationServiceFactory::GetForProfile(balloon->profile()); | |
71 service->DenyPermission(balloon->notification().origin_url()); | |
72 } | |
73 | |
74 void BalloonCollectionImplAsh::NotificationRemoved( | |
75 const std::string& notification_id, | |
76 bool by_user) { | |
77 RemoveById(notification_id); | |
78 } | |
79 | |
80 void BalloonCollectionImplAsh::ShowSettings( | |
81 const std::string& notification_id) { | |
82 Balloon* balloon = base().FindBalloonById(notification_id); | |
83 Profile* profile = | |
84 balloon ? balloon->profile() : ProfileManager::GetDefaultProfile(); | |
85 Browser* browser = | |
86 chrome::FindOrCreateTabbedBrowser(profile, | |
87 chrome::HOST_DESKTOP_TYPE_ASH); | |
88 if (GetBalloonExtension(balloon)) | |
89 chrome::ShowExtensions(browser, std::string()); | |
90 else | |
91 chrome::ShowContentSettings(browser, CONTENT_SETTINGS_TYPE_NOTIFICATIONS); | |
92 } | |
93 | |
94 void BalloonCollectionImplAsh::ShowSettingsDialog(gfx::NativeView context) { | |
95 settings_controller_->ShowSettingsDialog(context); | |
96 } | |
97 | |
98 void BalloonCollectionImplAsh::OnClicked(const std::string& notification_id) { | |
99 Balloon* balloon = base().FindBalloonById(notification_id); | |
100 if (!balloon) | |
101 return; | |
102 balloon->OnClick(); | |
103 } | |
104 | |
105 void BalloonCollectionImplAsh::OnButtonClicked( | |
106 const std::string& notification_id, int button_index) { | |
107 Balloon* balloon = base().FindBalloonById(notification_id); | |
108 if (balloon) | |
109 balloon->OnButtonClick(button_index); | |
110 } | |
111 | |
112 bool BalloonCollectionImplAsh::AddWebUIMessageCallback( | |
113 const Notification& notification, | |
114 const std::string& message, | |
115 const chromeos::BalloonViewHost::MessageCallback& callback) { | |
116 #if defined(OS_CHROMEOS) | |
117 Balloon* balloon = base().FindBalloon(notification); | |
118 if (!balloon) | |
119 return false; | |
120 | |
121 BalloonHost* balloon_host = balloon->balloon_view()->GetHost(); | |
122 if (!balloon_host) | |
123 return false; | |
124 chromeos::BalloonViewHost* balloon_view_host = | |
125 static_cast<chromeos::BalloonViewHost*>(balloon_host); | |
126 return balloon_view_host->AddWebUIMessageCallback(message, callback); | |
127 #else | |
128 return false; | |
129 #endif | |
130 } | |
131 | |
132 bool BalloonCollectionImplAsh::UpdateNotification( | |
133 const Notification& notification) { | |
134 Balloon* balloon = base().FindBalloon(notification); | |
135 if (!balloon) | |
136 return false; | |
137 balloon->Update(notification); | |
138 return true; | |
139 } | |
140 | |
141 bool BalloonCollectionImplAsh::UpdateAndShowNotification( | |
142 const Notification& notification) { | |
143 return UpdateNotification(notification); | |
144 } | |
145 | |
146 Balloon* BalloonCollectionImplAsh::MakeBalloon( | |
147 const Notification& notification, Profile* profile) { | |
148 Balloon* balloon = new Balloon(notification, profile, this); | |
149 BalloonViewAsh* balloon_view = new BalloonViewAsh(this); | |
150 balloon->set_view(balloon_view); | |
151 return balloon; | |
152 } | |
153 | |
154 const extensions::Extension* BalloonCollectionImplAsh::GetBalloonExtension( | |
155 Balloon* balloon) { | |
156 if (!balloon) | |
157 return NULL; | |
158 ExtensionService* extension_service = | |
159 balloon->profile()->GetExtensionService(); | |
160 const GURL& origin = balloon->notification().origin_url(); | |
161 return extension_service->extensions()->GetExtensionOrAppByURL( | |
162 ExtensionURLInfo(origin)); | |
163 } | |
164 | |
165 #if defined(OS_CHROMEOS) | |
166 // static | |
167 BalloonCollection* BalloonCollection::Create() { | |
168 return new BalloonCollectionImplAsh(); | |
169 } | |
170 #endif | |
OLD | NEW |