OLD | NEW |
---|---|
1 // Copyright 2017 The Chromium Authors. All rights reserved. | 1 // Copyright 2017 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" | 5 #include "chrome/browser/notifications/notification_platform_bridge_linux.h" |
6 | 6 |
7 #include <algorithm> | 7 #include <algorithm> |
8 | 8 |
9 #include "base/files/file_path.h" | |
10 #include "base/files/file_util.h" | |
9 #include "base/memory/ptr_util.h" | 11 #include "base/memory/ptr_util.h" |
10 #include "base/stl_util.h" | 12 #include "base/stl_util.h" |
11 #include "base/strings/nullable_string16.h" | 13 #include "base/strings/nullable_string16.h" |
12 #include "base/strings/utf_string_conversions.h" | 14 #include "base/strings/utf_string_conversions.h" |
13 #include "chrome/browser/browser_process.h" | 15 #include "chrome/browser/browser_process.h" |
14 #include "chrome/browser/notifications/native_notification_display_service.h" | 16 #include "chrome/browser/notifications/native_notification_display_service.h" |
15 #include "chrome/browser/notifications/notification.h" | 17 #include "chrome/browser/notifications/notification.h" |
16 #include "chrome/browser/notifications/notification_display_service_factory.h" | 18 #include "chrome/browser/notifications/notification_display_service_factory.h" |
17 #include "chrome/browser/profiles/profile_manager.h" | 19 #include "chrome/browser/profiles/profile_manager.h" |
18 | 20 |
19 namespace { | 21 namespace { |
20 | 22 |
21 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; | 23 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; |
22 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; | 24 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; |
23 | 25 |
24 void AddActionToNotification(GVariantBuilder* actions_builder, | 26 void AddActionToNotification(GVariantBuilder* actions_builder, |
25 const char* action_id, | 27 const char* action_id, |
26 const char* button_label) { | 28 const char* button_label) { |
27 g_variant_builder_add(actions_builder, "s", action_id); | 29 g_variant_builder_add(actions_builder, "s", action_id); |
28 g_variant_builder_add(actions_builder, "s", button_label); | 30 g_variant_builder_add(actions_builder, "s", button_label); |
29 } | 31 } |
30 | 32 |
33 int NotificationPriorityToFdoUrgency(int priority) { | |
34 enum FdoUrgency { | |
35 LOW = 0, | |
36 NORMAL = 1, | |
37 CRITICAL = 2, | |
38 }; | |
39 switch (priority) { | |
40 case message_center::MIN_PRIORITY: | |
41 case message_center::LOW_PRIORITY: | |
42 return LOW; | |
43 case message_center::HIGH_PRIORITY: | |
44 case message_center::MAX_PRIORITY: | |
45 return CRITICAL; | |
46 default: | |
47 NOTREACHED(); | |
48 // fallthrough | |
Lei Zhang
2017/04/11 01:27:05
super nitty: Intent 2 more because it's the NOTREA
Tom (Use chromium acct)
2017/04/11 23:21:08
Done.
| |
49 case message_center::DEFAULT_PRIORITY: | |
50 return NORMAL; | |
51 } | |
52 } | |
53 | |
31 // Callback used by GLib when the "Notify" message completes for the | 54 // Callback used by GLib when the "Notify" message completes for the |
32 // first time. | 55 // first time. |
33 void NotifyCompleteReceiver(GObject* source_object, | 56 void NotifyCompleteReceiver(GObject* source_object, |
34 GAsyncResult* result, | 57 GAsyncResult* result, |
35 gpointer user_data) { | 58 gpointer user_data) { |
36 GDBusProxy* proxy = G_DBUS_PROXY(source_object); | 59 GDBusProxy* proxy = G_DBUS_PROXY(source_object); |
37 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); | 60 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); |
38 if (!value) { | 61 if (!value) { |
39 // The message might have been cancelled, in which case | 62 // The message might have been cancelled, in which case |
40 // |user_data| points to a destroyed NotificationData. | 63 // |user_data| points to a destroyed NotificationData. |
(...skipping 17 matching lines...) Expand all Loading... | |
58 return; | 81 return; |
59 | 82 |
60 NotificationDisplayService* display_service = | 83 NotificationDisplayService* display_service = |
61 NotificationDisplayServiceFactory::GetForProfile(profile); | 84 NotificationDisplayServiceFactory::GetForProfile(profile); |
62 | 85 |
63 static_cast<NativeNotificationDisplayService*>(display_service) | 86 static_cast<NativeNotificationDisplayService*>(display_service) |
64 ->ProcessNotificationOperation(operation, notification_type, origin, | 87 ->ProcessNotificationOperation(operation, notification_type, origin, |
65 notification_id, action_index, reply); | 88 notification_id, action_index, reply); |
66 } | 89 } |
67 | 90 |
91 // Writes |image| to a new temporary file whose path will be returned | |
92 // in |file_path|. The file will be memory-mapped, so this function | |
93 // shouldn't block on disk IO. Returns true on success. | |
94 bool WriteImageToTmpFile(const gfx::Image& image, base::FilePath* file_path) { | |
95 // Writing to a memory-mapped file doesn't require disk IO. | |
Lei Zhang
2017/04/11 01:27:05
/dev/shm is backed by tmpfs. If there is enough me
| |
96 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
97 | |
98 base::FilePath shmem_temp_dir; | |
99 if (image.IsEmpty() || !GetShmemTempDir(false, &shmem_temp_dir) || | |
Lei Zhang
2017/04/11 01:27:05
Assuming we want to go with PostTask, don't bother
Tom (Use chromium acct)
2017/04/11 23:21:08
Done.
| |
100 !base::CreateTemporaryFileInDir(shmem_temp_dir, file_path)) { | |
101 return false; | |
102 } | |
103 auto image_data = image.As1xPNGBytes(); | |
104 int data_len = image_data->size(); | |
105 if (data_len == 0 || base::WriteFile(*file_path, image_data->front_as<char>(), | |
106 data_len) != data_len) { | |
107 base::DeleteFile(*file_path, false); | |
108 return false; | |
109 } | |
110 return true; | |
111 } | |
112 | |
68 } // namespace | 113 } // namespace |
69 | 114 |
70 // static | 115 // static |
71 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 116 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
72 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( | 117 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( |
73 G_BUS_TYPE_SESSION, | 118 G_BUS_TYPE_SESSION, |
74 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | | 119 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
75 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), | 120 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), |
76 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, | 121 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, |
77 kFreedesktopNotificationsName, nullptr, nullptr); | 122 kFreedesktopNotificationsName, nullptr, nullptr); |
(...skipping 10 matching lines...) Expand all Loading... | |
88 const GURL& origin_url) | 133 const GURL& origin_url) |
89 : notification_type(notification_type), | 134 : notification_type(notification_type), |
90 notification_id(notification_id), | 135 notification_id(notification_id), |
91 profile_id(profile_id), | 136 profile_id(profile_id), |
92 is_incognito(is_incognito), | 137 is_incognito(is_incognito), |
93 origin_url(origin_url) {} | 138 origin_url(origin_url) {} |
94 | 139 |
95 ~NotificationData() { | 140 ~NotificationData() { |
96 if (cancellable) | 141 if (cancellable) |
97 g_cancellable_cancel(cancellable); | 142 g_cancellable_cancel(cancellable); |
143 ResetResourceFiles(); | |
144 } | |
145 | |
146 void ResetResourceFiles() { | |
147 // Resource files are memory-mapped, so deletion shouldn't require | |
148 // disk IO. | |
149 base::ThreadRestrictions::ScopedAllowIO allow_io; | |
150 | |
151 for (const base::FilePath& file : resource_files) | |
152 base::DeleteFile(file, false); | |
153 resource_files.clear(); | |
98 } | 154 } |
99 | 155 |
100 // The ID used by the notification server. Will be 0 until the | 156 // The ID used by the notification server. Will be 0 until the |
101 // first "Notify" message completes. | 157 // first "Notify" message completes. |
102 uint32_t dbus_id = 0; | 158 uint32_t dbus_id = 0; |
103 | 159 |
104 // Same parameters used by NotificationPlatformBridge::Display(). | 160 // Same parameters used by NotificationPlatformBridge::Display(). |
105 const NotificationCommon::Type notification_type; | 161 NotificationCommon::Type notification_type; |
106 const std::string notification_id; | 162 const std::string notification_id; |
107 const std::string profile_id; | 163 const std::string profile_id; |
108 const bool is_incognito; | 164 const bool is_incognito; |
109 | 165 |
110 // A copy of the origin_url from the underlying | 166 // A copy of the origin_url from the underlying |
111 // message_center::Notification. Used to pass back to | 167 // message_center::Notification. Used to pass back to |
112 // NativeNotificationDisplayService. | 168 // NativeNotificationDisplayService. |
113 const GURL origin_url; | 169 const GURL origin_url; |
114 | 170 |
171 // Temporary resource files associated with the notification that | |
172 // should be cleaned up when the notification is closed. | |
173 std::vector<base::FilePath> resource_files; | |
174 | |
115 // Used to cancel the initial "Notify" message so we don't call | 175 // Used to cancel the initial "Notify" message so we don't call |
116 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a | 176 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a |
117 // destroyed Notification. | 177 // destroyed Notification. |
118 ScopedGObject<GCancellable> cancellable; | 178 ScopedGObject<GCancellable> cancellable; |
119 | 179 |
120 // If not null, the data to update the notification with once | 180 // If not null, the data to update the notification with once |
121 // |dbus_id| becomes available. | 181 // |dbus_id| becomes available. |
122 std::unique_ptr<Notification> update_data; | 182 std::unique_ptr<Notification> update_data; |
183 NotificationCommon::Type update_notification_type = | |
184 NotificationCommon::TYPE_MAX; | |
123 | 185 |
124 // If true, indicates the notification should be closed once | 186 // If true, indicates the notification should be closed once |
125 // |dbus_id| becomes available. | 187 // |dbus_id| becomes available. |
126 bool should_close = false; | 188 bool should_close = false; |
127 }; | 189 }; |
128 | 190 |
129 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( | 191 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( |
130 GDBusProxy* notification_proxy) | 192 GDBusProxy* notification_proxy) |
131 : notification_proxy_(notification_proxy) { | 193 : notification_proxy_(notification_proxy) { |
132 proxy_signal_handler_ = g_signal_connect( | 194 proxy_signal_handler_ = g_signal_connect( |
133 notification_proxy_, "g-signal", G_CALLBACK(GSignalReceiverThunk), this); | 195 notification_proxy_, "g-signal", G_CALLBACK(GSignalReceiverThunk), this); |
134 } | 196 } |
135 | 197 |
136 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() { | 198 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() { |
137 if (proxy_signal_handler_) | 199 if (proxy_signal_handler_) |
138 g_signal_handler_disconnect(notification_proxy_, proxy_signal_handler_); | 200 g_signal_handler_disconnect(notification_proxy_, proxy_signal_handler_); |
139 } | 201 } |
140 | 202 |
141 void NotificationPlatformBridgeLinux::Display( | 203 void NotificationPlatformBridgeLinux::Display( |
142 NotificationCommon::Type notification_type, | 204 NotificationCommon::Type notification_type, |
143 const std::string& notification_id, | 205 const std::string& notification_id, |
144 const std::string& profile_id, | 206 const std::string& profile_id, |
145 bool is_incognito, | 207 bool is_incognito, |
146 const Notification& notification) { | 208 const Notification& notification) { |
147 NotificationData* data = | 209 NotificationData* data = |
148 FindNotificationData(notification_id, profile_id, is_incognito); | 210 FindNotificationData(notification_id, profile_id, is_incognito); |
149 if (data) { | 211 if (data) { |
150 // Update an existing notification. | 212 // Update an existing notification. |
151 DCHECK_EQ(data->notification_type, notification_type); | |
152 if (data->dbus_id) { | 213 if (data->dbus_id) { |
153 NotifyNow(data->dbus_id, notification_type, notification, nullptr, | 214 data->notification_type = notification_type; |
154 nullptr, nullptr); | 215 NotifyNow(notification, data, nullptr, nullptr); |
155 } else { | 216 } else { |
217 data->update_notification_type = notification_type; | |
156 data->update_data = base::MakeUnique<Notification>(notification); | 218 data->update_data = base::MakeUnique<Notification>(notification); |
157 } | 219 } |
158 } else { | 220 } else { |
159 // Send the notification for the first time. | 221 // Send the notification for the first time. |
160 data = new NotificationData(notification_type, notification_id, profile_id, | 222 data = new NotificationData(notification_type, notification_id, profile_id, |
161 is_incognito, notification.origin_url()); | 223 is_incognito, notification.origin_url()); |
162 data->cancellable.reset(g_cancellable_new()); | 224 data->cancellable.reset(g_cancellable_new()); |
163 notifications_.emplace(data, base::WrapUnique(data)); | 225 notifications_.emplace(data, base::WrapUnique(data)); |
164 NotifyNow(0, notification_type, notification, data->cancellable, | 226 NotifyNow(notification, data, NotifyCompleteReceiver, data); |
165 NotifyCompleteReceiver, data); | |
166 } | 227 } |
167 } | 228 } |
168 | 229 |
169 void NotificationPlatformBridgeLinux::Close( | 230 void NotificationPlatformBridgeLinux::Close( |
170 const std::string& profile_id, | 231 const std::string& profile_id, |
171 const std::string& notification_id) { | 232 const std::string& notification_id) { |
172 std::vector<NotificationData*> to_erase; | 233 std::vector<NotificationData*> to_erase; |
173 for (const auto& pair : notifications_) { | 234 for (const auto& pair : notifications_) { |
174 NotificationData* data = pair.first; | 235 NotificationData* data = pair.first; |
175 if (data->notification_id == notification_id && | 236 if (data->notification_id == notification_id && |
(...skipping 26 matching lines...) Expand all Loading... | |
202 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) | 263 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) |
203 g_variant_get(value, "(u)", &data->dbus_id); | 264 g_variant_get(value, "(u)", &data->dbus_id); |
204 | 265 |
205 if (!data->dbus_id) { | 266 if (!data->dbus_id) { |
206 // There was some sort of error with creating the notification. | 267 // There was some sort of error with creating the notification. |
207 notifications_.erase(data); | 268 notifications_.erase(data); |
208 } else if (data->should_close) { | 269 } else if (data->should_close) { |
209 CloseNow(data->dbus_id); | 270 CloseNow(data->dbus_id); |
210 notifications_.erase(data); | 271 notifications_.erase(data); |
211 } else if (data->update_data) { | 272 } else if (data->update_data) { |
212 NotifyNow(data->dbus_id, data->notification_type, *data->update_data, | 273 data->notification_type = data->update_notification_type; |
213 nullptr, nullptr, nullptr); | 274 NotifyNow(*data->update_data, data, nullptr, nullptr); |
214 data->update_data.reset(); | 275 data->update_data.reset(); |
215 } | 276 } |
216 } | 277 } |
217 | 278 |
218 void NotificationPlatformBridgeLinux::NotifyNow( | 279 void NotificationPlatformBridgeLinux::NotifyNow( |
219 uint32_t dbus_id, | |
220 NotificationCommon::Type notification_type, | |
221 const Notification& notification, | 280 const Notification& notification, |
222 GCancellable* cancellable, | 281 NotificationData* data, |
223 GAsyncReadyCallback callback, | 282 GAsyncReadyCallback callback, |
224 gpointer user_data) { | 283 gpointer user_data) { |
225 // TODO(thomasanderson): Add a complete implementation. | 284 if (data->dbus_id) |
285 DCHECK(!data->cancellable); | |
286 | |
287 data->ResetResourceFiles(); | |
226 | 288 |
227 GVariantBuilder actions_builder; | 289 GVariantBuilder actions_builder; |
228 // Even-indexed elements in this array are action IDs passed back to | 290 // Even-indexed elements in this array are action IDs passed back to |
229 // us in GSignalReceiver. Odd-indexed ones contain the button text. | 291 // us in GSignalReceiver. Odd-indexed ones contain the button text. |
230 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); | 292 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); |
231 if (notification.clickable()) { | 293 if (notification.clickable()) { |
232 // Special case: the pair ("default", "") will not add a button, | 294 // Special case: the pair ("default", "") will not add a button, |
233 // but instead makes the entire notification clickable. | 295 // but instead makes the entire notification clickable. |
234 AddActionToNotification(&actions_builder, "default", ""); | 296 AddActionToNotification(&actions_builder, "default", ""); |
235 } | 297 } |
236 // Always add a settings button. | 298 // Always add a settings button. |
237 AddActionToNotification(&actions_builder, "settings", "Settings"); | 299 AddActionToNotification(&actions_builder, "settings", "Settings"); |
238 | 300 |
301 GVariantBuilder hints_builder; | |
302 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}")); | |
303 g_variant_builder_add(&hints_builder, "{sv}", "urgency", | |
304 g_variant_new_byte(NotificationPriorityToFdoUrgency( | |
305 notification.priority()))); | |
306 | |
307 base::FilePath icon_file; | |
308 if (WriteImageToTmpFile(notification.icon(), &icon_file)) { | |
309 g_variant_builder_add(&hints_builder, "{sv}", "image-path", | |
Tom (Use chromium acct)
2017/04/10 23:49:12
Initially, this used "image-data" so we didn't hav
Lei Zhang
2017/04/11 01:04:32
Any idea where that time is going? Maybe it's not
| |
310 g_variant_new_string(icon_file.value().c_str())); | |
311 data->resource_files.push_back(icon_file); | |
312 } | |
313 | |
239 const std::string title = base::UTF16ToUTF8(notification.title()); | 314 const std::string title = base::UTF16ToUTF8(notification.title()); |
240 const std::string message = base::UTF16ToUTF8(notification.message()); | 315 const std::string message = base::UTF16ToUTF8(notification.message()); |
241 | 316 |
242 GVariant* parameters = | 317 GVariant* parameters = |
243 g_variant_new("(susssasa{sv}i)", "", dbus_id, "", title.c_str(), | 318 g_variant_new("(susssasa{sv}i)", "", data->dbus_id, "", title.c_str(), |
244 message.c_str(), &actions_builder, nullptr, -1); | 319 message.c_str(), &actions_builder, &hints_builder, -1); |
245 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, | 320 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, |
246 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback, | 321 G_DBUS_CALL_FLAGS_NONE, -1, data->cancellable, callback, |
247 user_data); | 322 user_data); |
248 } | 323 } |
249 | 324 |
250 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { | 325 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { |
251 g_dbus_proxy_call(notification_proxy_, "CloseNotification", | 326 g_dbus_proxy_call(notification_proxy_, "CloseNotification", |
252 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, | 327 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, |
253 nullptr, nullptr, nullptr); | 328 nullptr, nullptr, nullptr); |
254 } | 329 } |
255 | 330 |
256 NotificationPlatformBridgeLinux::NotificationData* | 331 NotificationPlatformBridgeLinux::NotificationData* |
(...skipping 65 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... | |
322 | 397 |
323 if (strcmp(action, "default") == 0) { | 398 if (strcmp(action, "default") == 0) { |
324 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, 0); | 399 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, 0); |
325 } else if (strcmp(action, "settings") == 0) { | 400 } else if (strcmp(action, "settings") == 0) { |
326 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); | 401 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); |
327 } else { | 402 } else { |
328 NOTIMPLEMENTED() << "No custom buttons just yet!"; | 403 NOTIMPLEMENTED() << "No custom buttons just yet!"; |
329 } | 404 } |
330 } | 405 } |
331 } | 406 } |
OLD | NEW |