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> | |
8 | |
7 #include "base/memory/ptr_util.h" | 9 #include "base/memory/ptr_util.h" |
8 #include "base/strings/utf_string_conversions.h" | 10 #include "base/strings/utf_string_conversions.h" |
9 #include "chrome/browser/notifications/notification.h" | 11 #include "chrome/browser/notifications/notification.h" |
10 | 12 |
11 namespace { | 13 namespace { |
12 | 14 |
13 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; | 15 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; |
14 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; | 16 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; |
15 | 17 |
16 } // namespace | 18 } // namespace |
17 | 19 |
18 // static | 20 // static |
19 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 21 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
20 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( | 22 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( |
21 G_BUS_TYPE_SESSION, | 23 G_BUS_TYPE_SESSION, |
22 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | | 24 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | |
23 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | | 25 G_DBUS_PROXY_FLAGS_DO_NOT_CONNECT_SIGNALS | |
24 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), | 26 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), |
25 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, | 27 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, |
26 kFreedesktopNotificationsName, nullptr, nullptr); | 28 kFreedesktopNotificationsName, nullptr, nullptr); |
27 if (!notification_proxy) | 29 if (!notification_proxy) |
28 return nullptr; | 30 return nullptr; |
29 return new NotificationPlatformBridgeLinux(notification_proxy); | 31 return new NotificationPlatformBridgeLinux(notification_proxy); |
30 } | 32 } |
31 | 33 |
34 NotificationPlatformBridgeLinux::NotificationData::NotificationData( | |
35 const std::string& notification_id, | |
36 const std::string& profile_id, | |
37 bool is_incognito, | |
38 NotificationPlatformBridgeLinux* platform_bridge) | |
39 : notification_id(notification_id), | |
40 profile_id(profile_id), | |
41 is_incognito(is_incognito), | |
42 platform_bridge(platform_bridge) {} | |
43 | |
44 NotificationPlatformBridgeLinux::NotificationData::~NotificationData() { | |
45 if (cancellable) | |
46 g_cancellable_cancel(cancellable); | |
Lei Zhang
2017/04/06 03:59:54
Thought this would need to be cancellable.get().
Tom (Use chromium acct)
2017/04/06 05:05:35
ScopedGObject has "operator T*() { return obj_; }"
| |
47 } | |
48 | |
49 void OnNotifyComplete(GObject* source_object, | |
50 GAsyncResult* result, | |
51 gpointer user_data) { | |
52 GDBusProxy* proxy = G_DBUS_PROXY(source_object); | |
53 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); | |
54 if (!value) { | |
55 // The message might have been cancelled, in which case | |
56 // |user_data| points to a destroyed NotificationData. | |
57 return; | |
58 } | |
59 auto* data = | |
60 reinterpret_cast<NotificationPlatformBridgeLinux::NotificationData*>( | |
61 user_data); | |
62 data->platform_bridge->OnNotifyComplete(data, value); | |
63 } | |
64 | |
32 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( | 65 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( |
33 GDBusProxy* notification_proxy) | 66 GDBusProxy* notification_proxy) |
34 : notification_proxy_(notification_proxy) {} | 67 : notification_proxy_(notification_proxy) {} |
35 | 68 |
36 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() { | 69 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() {} |
37 g_object_unref(notification_proxy_); | |
38 } | |
39 | 70 |
40 void NotificationPlatformBridgeLinux::Display( | 71 void NotificationPlatformBridgeLinux::Display( |
41 NotificationCommon::Type notification_type, | 72 NotificationCommon::Type notification_type, |
42 const std::string& notification_id, | 73 const std::string& notification_id, |
43 const std::string& profile_id, | 74 const std::string& profile_id, |
44 bool is_incognito, | 75 bool is_incognito, |
45 const Notification& notification) { | 76 const Notification& notification) { |
46 // TODO(thomasanderson): Add a complete implementation. | 77 NotificationData* data = FindNotificationData(notification_id, profile_id); |
47 g_dbus_proxy_call( | 78 if (data) { |
48 notification_proxy_, "Notify", | 79 // Update an existing notification. |
49 g_variant_new("(susssasa{sv}i)", "", 0, "", | 80 if (data->dbus_id) { |
50 base::UTF16ToUTF8(notification.title()).c_str(), | 81 NotifyNow(data->dbus_id, notification_type, notification, nullptr, |
51 base::UTF16ToUTF8(notification.message()).c_str(), nullptr, | 82 nullptr, nullptr); |
52 nullptr, -1), | 83 } else { |
53 G_DBUS_CALL_FLAGS_NONE, -1, nullptr, nullptr, nullptr); | 84 data->update_type = notification_type; |
85 data->update_data.reset(new Notification(notification)); | |
86 } | |
87 } else { | |
88 // Send the notification for the first time. | |
89 data = | |
90 new NotificationData(notification_id, profile_id, is_incognito, this); | |
91 data->cancellable.reset(g_cancellable_new()); | |
92 notifications_.emplace(data, std::unique_ptr<NotificationData>(data)); | |
93 NotifyNow(0, notification_type, notification, data->cancellable, | |
94 ::OnNotifyComplete, data); | |
95 } | |
54 } | 96 } |
55 | 97 |
56 void NotificationPlatformBridgeLinux::Close( | 98 void NotificationPlatformBridgeLinux::Close( |
57 const std::string& profile_id, | 99 const std::string& profile_id, |
58 const std::string& notification_id) { | 100 const std::string& notification_id) { |
59 NOTIMPLEMENTED(); | 101 NotificationData* data = FindNotificationData(notification_id, profile_id); |
Lei Zhang
2017/04/06 03:59:54
Is this expected to never be NULL? i.e. We can saf
Tom (Use chromium acct)
2017/04/06 05:05:35
Probably not; added a nullptr check.
| |
102 if (data->dbus_id) { | |
103 CloseNow(data->dbus_id); | |
104 notifications_.erase(data); | |
105 } else { | |
106 data->should_close = true; | |
107 } | |
60 } | 108 } |
61 | 109 |
62 void NotificationPlatformBridgeLinux::GetDisplayed( | 110 void NotificationPlatformBridgeLinux::GetDisplayed( |
63 const std::string& profile_id, | 111 const std::string& profile_id, |
64 bool incognito, | 112 bool incognito, |
65 const DisplayedNotificationsCallback& callback) const { | 113 const DisplayedNotificationsCallback& callback) const { |
66 callback.Run(base::MakeUnique<std::set<std::string>>(), false); | 114 callback.Run(base::MakeUnique<std::set<std::string>>(), false); |
67 } | 115 } |
116 | |
117 void NotificationPlatformBridgeLinux::OnNotifyComplete(NotificationData* data, | |
118 GVariant* value) { | |
119 data->cancellable.reset(nullptr); | |
120 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) | |
121 g_variant_get(value, "(u)", &data->dbus_id); | |
122 | |
123 if (!data->dbus_id) { | |
124 // There was some sort of error with creating the notification. | |
125 notifications_.erase(data); | |
126 } else if (data->should_close) { | |
127 CloseNow(data->dbus_id); | |
128 notifications_.erase(data); | |
129 } else if (data->update_data) { | |
130 NotifyNow(data->dbus_id, data->update_type, *data->update_data, nullptr, | |
131 nullptr, nullptr); | |
132 data->update_data.reset(); | |
133 } | |
134 } | |
135 | |
136 void NotificationPlatformBridgeLinux::NotifyNow( | |
137 uint32_t dbus_id, | |
138 NotificationCommon::Type notification_type, | |
139 const Notification& notification, | |
140 GCancellable* cancellable, | |
141 GAsyncReadyCallback callback, | |
142 gpointer user_data) { | |
143 // TODO(thomasanderson): Add a complete implementation. | |
144 GVariant* parameters = g_variant_new( | |
145 "(susssasa{sv}i)", "", dbus_id, "", | |
146 base::UTF16ToUTF8(notification.title()).c_str(), | |
147 base::UTF16ToUTF8(notification.message()).c_str(), nullptr, nullptr, -1); | |
148 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, | |
149 G_DBUS_CALL_FLAGS_NONE, -1, cancellable, callback, | |
150 user_data); | |
151 } | |
152 | |
153 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { | |
154 g_dbus_proxy_call(notification_proxy_, "CloseNotification", | |
155 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, | |
156 nullptr, nullptr, nullptr); | |
157 } | |
158 | |
159 NotificationPlatformBridgeLinux::NotificationData* | |
160 NotificationPlatformBridgeLinux::FindNotificationData( | |
161 const std::string& notification_id, | |
162 const std::string& profile_id) { | |
163 auto it = std::find_if( | |
164 notifications_.begin(), notifications_.end(), | |
165 [&](std::pair<NotificationData* const, std::unique_ptr<NotificationData>>& | |
166 data) { | |
167 return data.first->notification_id == notification_id && | |
168 data.first->profile_id == profile_id; | |
169 | |
170 }); | |
171 if (it == notifications_.end()) | |
172 return nullptr; | |
173 return it->first; | |
174 } | |
OLD | NEW |