Chromium Code Reviews| 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/barrier_closure.h" | |
| 9 #include "base/files/file_util.h" | 10 #include "base/files/file_util.h" |
| 10 #include "base/memory/ptr_util.h" | 11 #include "base/run_loop.h" |
| 11 #include "base/stl_util.h" | |
| 12 #include "base/strings/nullable_string16.h" | 12 #include "base/strings/nullable_string16.h" |
| 13 #include "base/strings/string_number_conversions.h" | 13 #include "base/strings/string_number_conversions.h" |
| 14 #include "base/strings/string_util.h" | 14 #include "base/strings/string_util.h" |
| 15 #include "base/strings/utf_string_conversions.h" | 15 #include "base/strings/utf_string_conversions.h" |
| 16 #include "base/synchronization/lock.h" | |
| 16 #include "base/task_scheduler/post_task.h" | 17 #include "base/task_scheduler/post_task.h" |
| 17 #include "chrome/browser/browser_process.h" | 18 #include "chrome/browser/browser_process.h" |
| 18 #include "chrome/browser/chrome_notification_types.h" | 19 #include "chrome/browser/chrome_notification_types.h" |
| 19 #include "chrome/browser/notifications/native_notification_display_service.h" | 20 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 20 #include "chrome/browser/notifications/notification.h" | 21 #include "chrome/browser/notifications/notification.h" |
| 21 #include "chrome/browser/notifications/notification_display_service_factory.h" | 22 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 22 #include "chrome/browser/profiles/profile_manager.h" | 23 #include "chrome/browser/profiles/profile_manager.h" |
| 23 #include "chrome/browser/shell_integration_linux.h" | 24 #include "chrome/browser/shell_integration_linux.h" |
| 25 #include "content/public/browser/browser_thread.h" | |
| 24 #include "content/public/browser/notification_service.h" | 26 #include "content/public/browser/notification_service.h" |
| 27 #include "dbus/bus.h" | |
| 28 #include "dbus/message.h" | |
| 29 #include "dbus/object_proxy.h" | |
| 30 #include "ui/gfx/image/image_skia.h" | |
| 25 | 31 |
| 26 namespace { | 32 namespace { |
| 27 | 33 |
| 28 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; | 34 const char kFreedesktopNotificationsName[] = "org.freedesktop.Notifications"; |
| 29 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; | 35 const char kFreedesktopNotificationsPath[] = "/org/freedesktop/Notifications"; |
| 30 | 36 |
| 31 void AddActionToNotification(GVariantBuilder* actions_builder, | 37 gfx::Image DeepCopyImage(const gfx::Image& image) { |
| 32 const char* action_id, | 38 std::unique_ptr<gfx::ImageSkia> image_skia(image.CopyImageSkia()); |
| 33 const char* button_label) { | 39 return gfx::Image(*image_skia); |
| 34 g_variant_builder_add(actions_builder, "s", action_id); | |
| 35 g_variant_builder_add(actions_builder, "s", button_label); | |
| 36 } | 40 } |
| 37 | 41 |
| 38 int NotificationPriorityToFdoUrgency(int priority) { | 42 int NotificationPriorityToFdoUrgency(int priority) { |
| 39 enum FdoUrgency { | 43 enum FdoUrgency { |
| 40 LOW = 0, | 44 LOW = 0, |
| 41 NORMAL = 1, | 45 NORMAL = 1, |
| 42 CRITICAL = 2, | 46 CRITICAL = 2, |
| 43 }; | 47 }; |
| 44 switch (priority) { | 48 switch (priority) { |
| 45 case message_center::MIN_PRIORITY: | 49 case message_center::MIN_PRIORITY: |
| 46 case message_center::LOW_PRIORITY: | 50 case message_center::LOW_PRIORITY: |
| 47 return LOW; | 51 return LOW; |
| 48 case message_center::HIGH_PRIORITY: | 52 case message_center::HIGH_PRIORITY: |
| 49 case message_center::MAX_PRIORITY: | 53 case message_center::MAX_PRIORITY: |
| 50 return CRITICAL; | 54 return CRITICAL; |
| 51 default: | 55 default: |
| 52 NOTREACHED(); | 56 NOTREACHED(); |
| 53 // fallthrough | |
| 54 case message_center::DEFAULT_PRIORITY: | 57 case message_center::DEFAULT_PRIORITY: |
| 55 return NORMAL; | 58 return NORMAL; |
| 56 } | 59 } |
| 57 } | 60 } |
| 58 | 61 |
| 59 // Callback used by GLib when the "Notify" message completes for the | |
| 60 // first time. | |
| 61 void NotifyCompleteReceiver(GObject* source_object, | |
| 62 GAsyncResult* result, | |
| 63 gpointer user_data) { | |
| 64 GDBusProxy* proxy = G_DBUS_PROXY(source_object); | |
| 65 GVariant* value = g_dbus_proxy_call_finish(proxy, result, nullptr); | |
| 66 if (!value) { | |
| 67 // The message might have been cancelled, in which case | |
| 68 // |user_data| points to a destroyed NotificationData. | |
| 69 return; | |
| 70 } | |
| 71 auto* platform_bridge_linux = static_cast<NotificationPlatformBridgeLinux*>( | |
| 72 g_browser_process->notification_platform_bridge()); | |
| 73 platform_bridge_linux->NotifyCompleteInternal(user_data, value); | |
| 74 } | |
| 75 | |
| 76 // Runs once the profile has been loaded in order to perform a given | 62 // Runs once the profile has been loaded in order to perform a given |
| 77 // |operation| on a notification. | 63 // |operation| on a notification. |
| 78 void ProfileLoadedCallback(NotificationCommon::Operation operation, | 64 void ProfileLoadedCallback(NotificationCommon::Operation operation, |
| 79 NotificationCommon::Type notification_type, | 65 NotificationCommon::Type notification_type, |
| 80 const std::string& origin, | 66 const std::string& origin, |
| 81 const std::string& notification_id, | 67 const std::string& notification_id, |
| 82 int action_index, | 68 int action_index, |
| 83 const base::NullableString16& reply, | 69 const base::NullableString16& reply, |
| 84 Profile* profile) { | 70 Profile* profile) { |
| 85 if (!profile) | 71 if (!profile) |
| (...skipping 18 matching lines...) Expand all Loading... | |
| 104 if (!base::CreateTemporaryFile(&file_path)) | 90 if (!base::CreateTemporaryFile(&file_path)) |
| 105 return base::FilePath(); | 91 return base::FilePath(); |
| 106 if (base::WriteFile(file_path, data->front_as<char>(), data_len) != | 92 if (base::WriteFile(file_path, data->front_as<char>(), data_len) != |
| 107 data_len) { | 93 data_len) { |
| 108 base::DeleteFile(file_path, false); | 94 base::DeleteFile(file_path, false); |
| 109 return base::FilePath(); | 95 return base::FilePath(); |
| 110 } | 96 } |
| 111 return file_path; | 97 return file_path; |
| 112 } | 98 } |
| 113 | 99 |
| 114 void DeleteNotificationResourceFile(const base::FilePath& file_path) { | |
| 115 if (file_path.empty()) | |
| 116 return; | |
| 117 base::PostTaskWithTraits( | |
| 118 FROM_HERE, | |
| 119 base::TaskTraits() | |
| 120 .MayBlock() | |
| 121 .WithPriority(base::TaskPriority::BACKGROUND) | |
| 122 .WithShutdownBehavior(base::TaskShutdownBehavior::BLOCK_SHUTDOWN), | |
| 123 base::BindOnce(base::IgnoreResult(base::DeleteFile), file_path, false)); | |
| 124 } | |
| 125 | |
| 126 } // namespace | 100 } // namespace |
| 127 | 101 |
| 128 // static | 102 // static |
| 129 NotificationPlatformBridge* NotificationPlatformBridge::Create() { | 103 NotificationPlatformBridge* NotificationPlatformBridge::Create() { |
| 130 GDBusProxy* notification_proxy = g_dbus_proxy_new_for_bus_sync( | 104 return new NotificationPlatformBridgeLinux(); |
| 131 G_BUS_TYPE_SESSION, | 105 } |
| 132 static_cast<GDBusProxyFlags>(G_DBUS_PROXY_FLAGS_DO_NOT_LOAD_PROPERTIES | | 106 |
| 133 G_DBUS_PROXY_FLAGS_DO_NOT_AUTO_START), | 107 class NotificationPlatformBridgeLinuxImpl |
| 134 nullptr, kFreedesktopNotificationsName, kFreedesktopNotificationsPath, | 108 : public NotificationPlatformBridge, |
| 135 kFreedesktopNotificationsName, nullptr, nullptr); | 109 public content::NotificationObserver, |
| 136 if (!notification_proxy) | 110 public base::RefCountedThreadSafe<NotificationPlatformBridgeLinuxImpl> { |
| 111 public: | |
| 112 NotificationPlatformBridgeLinuxImpl() | |
| 113 : task_runner_(base::CreateSingleThreadTaskRunnerWithTraits( | |
| 114 base::TaskTraits().MayBlock().WithPriority( | |
| 115 base::TaskPriority::USER_BLOCKING))) { | |
| 116 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | |
| 117 content::NotificationService::AllSources()); | |
| 118 PostTaskToTaskRunnerThread( | |
| 119 base::BindOnce(&NotificationPlatformBridgeLinuxImpl::Init, this)); | |
| 120 } | |
| 121 | |
| 122 void Display(NotificationCommon::Type notification_type, | |
| 123 const std::string& notification_id, | |
| 124 const std::string& profile_id, | |
| 125 bool is_incognito, | |
| 126 const Notification& notification) override { | |
| 127 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 128 // Notifications contain gfx::Image's which have reference counts | |
| 129 // that are not thread safe. Because of this, we duplicate the | |
| 130 // notification and its images. Wrap the notification in a | |
| 131 // unique_ptr to transfer ownership of the notification (and the | |
| 132 // non-thread-safe reference counts) to the task runner thread. | |
| 133 auto notification_copy = base::MakeUnique<Notification>(notification); | |
| 134 notification_copy->set_icon(DeepCopyImage(notification_copy->icon())); | |
| 135 notification_copy->set_image(gfx::Image()); | |
| 136 notification_copy->set_small_image(gfx::Image()); | |
| 137 for (size_t i = 0; i < notification_copy->buttons().size(); i++) | |
| 138 notification_copy->SetButtonIcon(i, gfx::Image()); | |
| 139 | |
| 140 PostTaskToTaskRunnerThread( | |
| 141 base::Bind(&NotificationPlatformBridgeLinuxImpl::DisplayOnTaskRunner, | |
| 142 this, notification_type, notification_id, profile_id, | |
| 143 is_incognito, base::Passed(¬ification_copy))); | |
| 144 } | |
| 145 | |
| 146 void Close(const std::string& profile_id, | |
| 147 const std::string& notification_id) override { | |
| 148 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 149 PostTaskToTaskRunnerThread( | |
| 150 base::Bind(&NotificationPlatformBridgeLinuxImpl::CloseOnTaskRunner, | |
| 151 this, profile_id, notification_id)); | |
| 152 } | |
| 153 | |
| 154 void GetDisplayed( | |
| 155 const std::string& profile_id, | |
| 156 bool incognito, | |
| 157 const GetDisplayedNotificationsCallback& callback) const override { | |
| 158 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 159 PostTaskToTaskRunnerThread(base::Bind( | |
| 160 &NotificationPlatformBridgeLinuxImpl::GetDisplayedOnTaskRunner, this, | |
| 161 profile_id, incognito, callback)); | |
| 162 } | |
| 163 | |
| 164 void SetReadyCallback(NotificationBridgeReadyCallback callback) override { | |
| 165 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 166 base::AutoLock lock(connected_lock_); | |
| 167 if (connected_.has_value()) { | |
| 168 content::BrowserThread::PostTask( | |
| 169 content::BrowserThread::UI, FROM_HERE, | |
| 170 base::BindOnce(std::move(callback), connected_.value())); | |
| 171 } else { | |
| 172 on_connected_callbacks_.push_back(std::move(callback)); | |
| 173 } | |
| 174 } | |
| 175 | |
| 176 private: | |
| 177 friend class base::RefCountedThreadSafe<NotificationPlatformBridgeLinuxImpl>; | |
| 178 | |
| 179 struct ResourceFile { | |
| 180 explicit ResourceFile(const base::FilePath& file_path) | |
| 181 : file_path(file_path) {} | |
| 182 ~ResourceFile() { base::DeleteFile(file_path, false); } | |
| 183 const base::FilePath file_path; | |
| 184 }; | |
| 185 | |
| 186 struct NotificationData { | |
| 187 NotificationData(NotificationCommon::Type notification_type, | |
| 188 const std::string& notification_id, | |
| 189 const std::string& profile_id, | |
| 190 bool is_incognito, | |
| 191 const GURL& origin_url) | |
| 192 : notification_type(notification_type), | |
| 193 notification_id(notification_id), | |
| 194 profile_id(profile_id), | |
| 195 is_incognito(is_incognito), | |
| 196 origin_url(origin_url) {} | |
| 197 | |
| 198 // The ID used by the notification server. Will be 0 until the | |
| 199 // first "Notify" message completes. | |
| 200 uint32_t dbus_id = 0; | |
| 201 | |
| 202 // Same parameters used by NotificationPlatformBridge::Display(). | |
| 203 NotificationCommon::Type notification_type; | |
| 204 const std::string notification_id; | |
| 205 const std::string profile_id; | |
| 206 const bool is_incognito; | |
| 207 | |
| 208 // A copy of the origin_url from the underlying | |
| 209 // message_center::Notification. Used to pass back to | |
| 210 // NativeNotificationDisplayService. | |
| 211 const GURL origin_url; | |
| 212 | |
| 213 // Used to keep track of the IDs of the buttons currently displayed | |
| 214 // on this notification. The valid range of action IDs is | |
| 215 // [action_start, action_end). | |
| 216 size_t action_start = 0; | |
| 217 size_t action_end = 0; | |
| 218 | |
| 219 // Temporary resource files associated with the notification that | |
| 220 // should be cleaned up when the notification is closed or on | |
| 221 // shutdown. | |
| 222 std::vector<std::unique_ptr<ResourceFile>> resource_files; | |
| 223 }; | |
| 224 | |
| 225 ~NotificationPlatformBridgeLinuxImpl() override { | |
| 226 DCHECK(!bus_); | |
| 227 DCHECK(notifications_.empty()); | |
| 228 } | |
| 229 | |
| 230 void Observe(int type, | |
| 231 const content::NotificationSource& source, | |
| 232 const content::NotificationDetails& details) override { | |
| 233 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 234 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); | |
| 235 // The browser process is about to exit. Post the CleanUp() task | |
| 236 // while we still can. | |
| 237 CleanUp(); | |
| 238 } | |
| 239 | |
| 240 void PostTaskToTaskRunnerThread(base::OnceClosure closure) const { | |
| 241 DCHECK(task_runner_); | |
| 242 bool success = task_runner_->PostTask(FROM_HERE, std::move(closure)); | |
| 243 DCHECK(success); | |
| 244 } | |
| 245 | |
| 246 // Sets up the D-Bus connection. | |
| 247 void Init() { | |
| 248 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 249 dbus::Bus::Options bus_options; | |
| 250 bus_options.bus_type = dbus::Bus::SESSION; | |
| 251 bus_options.connection_type = dbus::Bus::PRIVATE; | |
| 252 bus_options.dbus_task_runner = task_runner_; | |
| 253 bus_ = make_scoped_refptr(new dbus::Bus(bus_options)); | |
| 254 | |
| 255 notification_proxy_ = | |
| 256 bus_->GetObjectProxy(kFreedesktopNotificationsName, | |
| 257 dbus::ObjectPath(kFreedesktopNotificationsPath)); | |
| 258 if (!notification_proxy_) { | |
| 259 OnConnectionInitializationFinished(false); | |
| 260 return; | |
| 261 } | |
| 262 | |
| 263 connected_signals_barrier_ = base::BarrierClosure( | |
| 264 2, base::Bind(&NotificationPlatformBridgeLinuxImpl:: | |
| 265 OnConnectionInitializationFinished, | |
| 266 this, true)); | |
| 267 notification_proxy_->ConnectToSignal( | |
| 268 kFreedesktopNotificationsName, "ActionInvoked", | |
| 269 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnActionInvoked, this), | |
| 270 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnSignalConnected, | |
| 271 this)); | |
| 272 notification_proxy_->ConnectToSignal( | |
| 273 kFreedesktopNotificationsName, "NotificationClosed", | |
| 274 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnNotificationClosed, | |
| 275 this), | |
| 276 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnSignalConnected, | |
| 277 this)); | |
| 278 } | |
| 279 | |
| 280 void CleanUp() { | |
| 281 DCHECK_CURRENTLY_ON(content::BrowserThread::UI); | |
| 282 PostTaskToTaskRunnerThread(base::BindOnce( | |
| 283 &NotificationPlatformBridgeLinuxImpl::CleanUpOnTaskRunnerThread, this)); | |
| 284 } | |
| 285 | |
| 286 void CleanUpOnTaskRunnerThread() { | |
| 287 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 288 bus_->ShutdownAndBlock(); | |
| 289 bus_ = nullptr; | |
| 290 notification_proxy_ = nullptr; | |
| 291 notifications_.clear(); | |
| 292 } | |
| 293 | |
| 294 // Makes the "Notify" call to D-Bus. | |
| 295 void DisplayOnTaskRunner(NotificationCommon::Type notification_type, | |
| 296 const std::string& notification_id, | |
| 297 const std::string& profile_id, | |
| 298 bool is_incognito, | |
| 299 std::unique_ptr<Notification> notification) { | |
| 300 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 301 NotificationData* data = | |
| 302 FindNotificationData(notification_id, profile_id, is_incognito); | |
| 303 if (data) { | |
| 304 // Update an existing notification. | |
| 305 data->notification_type = notification_type; | |
| 306 data->resource_files.clear(); | |
| 307 } else { | |
| 308 // Send the notification for the first time. | |
| 309 data = | |
| 310 new NotificationData(notification_type, notification_id, profile_id, | |
| 311 is_incognito, notification->origin_url()); | |
| 312 notifications_.emplace(data, base::WrapUnique(data)); | |
| 313 } | |
| 314 | |
| 315 dbus::MethodCall method_call(kFreedesktopNotificationsName, "Notify"); | |
| 316 dbus::MessageWriter writer(&method_call); | |
| 317 | |
| 318 // app_name passed implicitly via desktop-entry. | |
| 319 writer.AppendString(""); | |
| 320 | |
| 321 writer.AppendUint32(data->dbus_id); | |
| 322 | |
| 323 // app_icon passed implicitly via desktop-entry. | |
| 324 writer.AppendString(""); | |
| 325 | |
| 326 const std::string title = base::UTF16ToUTF8(notification->title()); | |
| 327 writer.AppendString(title); | |
| 328 | |
| 329 const std::string message = base::UTF16ToUTF8(notification->message()); | |
| 330 writer.AppendString(message); | |
| 331 | |
| 332 // Even-indexed elements in this vector are action IDs passed back to | |
| 333 // us in OnActionInvoked(). Odd-indexed ones contain the button text. | |
| 334 std::vector<std::string> actions; | |
| 335 data->action_start = data->action_end; | |
| 336 for (const auto& button_info : notification->buttons()) { | |
| 337 // FDO notification buttons can contain either an icon or a label, | |
| 338 // but not both, and the type of all buttons must be the same (all | |
| 339 // labels or all icons), so always use labels. | |
| 340 const std::string id = base::SizeTToString(data->action_end++); | |
| 341 const std::string label = base::UTF16ToUTF8(button_info.title); | |
| 342 actions.push_back(id); | |
| 343 actions.push_back(label); | |
| 344 } | |
| 345 if (notification->clickable()) { | |
| 346 // Special case: the pair ("default", "") will not add a button, | |
| 347 // but instead makes the entire notification clickable. | |
| 348 actions.push_back("default"); | |
| 349 actions.push_back(""); | |
| 350 } | |
| 351 // Always add a settings button. | |
| 352 actions.push_back("settings"); | |
| 353 actions.push_back("Settings"); | |
| 354 writer.AppendArrayOfStrings(actions); | |
| 355 | |
| 356 dbus::MessageWriter hints_writer(nullptr); | |
| 357 writer.OpenArray("{sv}", &hints_writer); | |
| 358 dbus::MessageWriter urgency_writer(nullptr); | |
| 359 hints_writer.OpenDictEntry(&urgency_writer); | |
| 360 urgency_writer.AppendString("urgency"); | |
| 361 urgency_writer.AppendVariantOfUint32( | |
| 362 NotificationPriorityToFdoUrgency(notification->priority())); | |
| 363 hints_writer.CloseContainer(&urgency_writer); | |
| 364 | |
| 365 std::unique_ptr<base::Environment> env = base::Environment::Create(); | |
| 366 base::FilePath desktop_file( | |
| 367 shell_integration_linux::GetDesktopName(env.get())); | |
| 368 const char kDesktopFileSuffix[] = ".desktop"; | |
| 369 DCHECK(base::EndsWith(desktop_file.value(), kDesktopFileSuffix, | |
| 370 base::CompareCase::SENSITIVE)); | |
| 371 desktop_file = desktop_file.RemoveFinalExtension(); | |
| 372 dbus::MessageWriter desktop_entry_writer(nullptr); | |
| 373 hints_writer.OpenDictEntry(&desktop_entry_writer); | |
| 374 desktop_entry_writer.AppendString("desktop-entry"); | |
| 375 desktop_entry_writer.AppendVariantOfString(desktop_file.value()); | |
| 376 hints_writer.CloseContainer(&desktop_entry_writer); | |
| 377 | |
| 378 base::FilePath icon_file = | |
| 379 WriteDataToTmpFile(notification->icon().As1xPNGBytes()); | |
| 380 if (!icon_file.empty()) { | |
| 381 dbus::MessageWriter image_path_writer(nullptr); | |
| 382 hints_writer.OpenDictEntry(&image_path_writer); | |
| 383 image_path_writer.AppendString("image-path"); | |
| 384 image_path_writer.AppendVariantOfString(icon_file.value()); | |
| 385 hints_writer.CloseContainer(&image_path_writer); | |
| 386 data->resource_files.push_back(base::MakeUnique<ResourceFile>(icon_file)); | |
| 387 } | |
| 388 | |
| 389 writer.CloseContainer(&hints_writer); | |
| 390 | |
| 391 const int32_t kExpireTimeoutDefault = -1; | |
| 392 writer.AppendInt32(kExpireTimeoutDefault); | |
| 393 | |
| 394 std::unique_ptr<dbus::Response> response = | |
| 395 notification_proxy_->CallMethodAndBlock( | |
| 396 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | |
| 397 if (response) { | |
| 398 dbus::MessageReader reader(response.get()); | |
| 399 reader.PopUint32(&data->dbus_id); | |
| 400 } | |
| 401 if (!data->dbus_id) { | |
| 402 // There was some sort of error with creating the notification. | |
| 403 notifications_.erase(data); | |
| 404 } | |
| 405 } | |
| 406 | |
| 407 // Makes the "CloseNotification" call to D-Bus. | |
| 408 void CloseOnTaskRunner(const std::string& profile_id, | |
| 409 const std::string& notification_id) { | |
| 410 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 411 std::vector<NotificationData*> to_erase; | |
| 412 for (const auto& pair : notifications_) { | |
| 413 NotificationData* data = pair.first; | |
| 414 if (data->notification_id == notification_id && | |
| 415 data->profile_id == profile_id) { | |
| 416 dbus::MethodCall method_call(kFreedesktopNotificationsName, | |
| 417 "CloseNotification"); | |
| 418 dbus::MessageWriter writer(&method_call); | |
| 419 writer.AppendUint32(data->dbus_id); | |
| 420 notification_proxy_->CallMethodAndBlock( | |
| 421 &method_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | |
| 422 } | |
| 423 } | |
| 424 for (NotificationData* data : to_erase) | |
| 425 notifications_.erase(data); | |
| 426 } | |
| 427 | |
| 428 void GetDisplayedOnTaskRunner( | |
| 429 const std::string& profile_id, | |
| 430 bool incognito, | |
| 431 const GetDisplayedNotificationsCallback& callback) const { | |
| 432 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 433 // TODO(thomasanderson): Implement. | |
| 434 content::BrowserThread::PostTask( | |
| 435 content::BrowserThread::UI, FROM_HERE, | |
| 436 base::Bind(callback, | |
| 437 base::Passed(base::MakeUnique<std::set<std::string>>()), | |
| 438 false)); | |
| 439 } | |
| 440 | |
| 441 NotificationData* FindNotificationData(const std::string& notification_id, | |
| 442 const std::string& profile_id, | |
| 443 bool is_incognito) { | |
| 444 for (const auto& pair : notifications_) { | |
| 445 NotificationData* data = pair.first; | |
| 446 if (data->notification_id == notification_id && | |
| 447 data->profile_id == profile_id && | |
| 448 data->is_incognito == is_incognito) { | |
| 449 return data; | |
| 450 } | |
| 451 } | |
| 452 | |
| 137 return nullptr; | 453 return nullptr; |
| 138 return new NotificationPlatformBridgeLinux(notification_proxy); | 454 } |
| 139 } | 455 |
| 140 | 456 NotificationData* FindNotificationData(uint32_t dbus_id) { |
| 141 struct NotificationPlatformBridgeLinux::NotificationData { | 457 for (const auto& pair : notifications_) { |
| 142 NotificationData(NotificationCommon::Type notification_type, | 458 NotificationData* data = pair.first; |
| 143 const std::string& notification_id, | 459 if (data->dbus_id == dbus_id) |
| 144 const std::string& profile_id, | 460 return data; |
| 145 bool is_incognito, | 461 } |
| 146 const GURL& origin_url) | 462 |
| 147 : notification_type(notification_type), | 463 return nullptr; |
| 148 notification_id(notification_id), | 464 } |
| 149 profile_id(profile_id), | 465 |
| 150 is_incognito(is_incognito), | 466 void ForwardNotificationOperation(NotificationData* data, |
| 151 origin_url(origin_url), | 467 NotificationCommon::Operation operation, |
| 152 weak_factory(this) {} | 468 int action_index) { |
| 153 | 469 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 154 ~NotificationData() { | 470 content::BrowserThread::PostTask( |
| 155 if (cancellable) | 471 content::BrowserThread::UI, FROM_HERE, |
| 156 g_cancellable_cancel(cancellable); | 472 base::Bind( |
| 157 ResetResourceFiles(); | 473 [](NotificationCommon::Operation operation, |
| 158 } | 474 NotificationCommon::Type notification_type, |
| 159 | 475 const std::string& origin, const std::string& notification_id, |
| 160 void ResetResourceFiles() { | 476 int action_index, const std::string& profile_id, |
| 161 for (const base::FilePath& file : resource_files) | 477 bool is_incognito) { |
| 162 DeleteNotificationResourceFile(file); | 478 ProfileManager* profile_manager = |
| 163 resource_files.clear(); | 479 g_browser_process->profile_manager(); |
| 164 } | 480 DCHECK(profile_manager); |
| 165 | 481 |
| 166 // The ID used by the notification server. Will be 0 until the | 482 profile_manager->LoadProfile( |
| 167 // first "Notify" message completes. | 483 profile_id, is_incognito, |
| 168 uint32_t dbus_id = 0; | 484 base::Bind(&ProfileLoadedCallback, operation, |
| 169 | 485 notification_type, origin, notification_id, |
| 170 // Same parameters used by NotificationPlatformBridge::Display(). | 486 action_index, base::NullableString16())); |
| 171 NotificationCommon::Type notification_type; | 487 }, |
| 172 const std::string notification_id; | 488 operation, data->notification_type, data->origin_url.spec(), |
| 173 const std::string profile_id; | 489 data->notification_id, action_index, data->profile_id, |
| 174 const bool is_incognito; | 490 data->is_incognito)); |
| 175 | 491 } |
| 176 // A copy of the origin_url from the underlying | 492 |
| 177 // message_center::Notification. Used to pass back to | 493 void OnActionInvoked(dbus::Signal* signal) { |
| 178 // NativeNotificationDisplayService. | 494 DCHECK(task_runner_->RunsTasksOnCurrentThread()); |
| 179 const GURL origin_url; | 495 dbus::MessageReader reader(signal); |
| 180 | 496 uint32_t dbus_id; |
| 181 // Used to keep track of the IDs of the buttons currently displayed | 497 if (!reader.PopUint32(&dbus_id)) |
| 182 // on this notification. The valid range of action IDs is | 498 return; |
| 183 // [action_start, action_end). | 499 std::string action; |
| 184 size_t action_start = 0; | 500 if (!reader.PopString(&action)) |
| 185 size_t action_end = 0; | 501 return; |
| 186 | 502 |
| 187 // Temporary resource files associated with the notification that | 503 NotificationData* data = FindNotificationData(dbus_id); |
| 188 // should be cleaned up when the notification is closed or on | 504 if (!data) |
| 189 // shutdown. | 505 return; |
| 190 std::vector<base::FilePath> resource_files; | 506 |
| 191 | 507 if (action == "default") { |
| 192 // Used to cancel the initial "Notify" message so we don't call | 508 ForwardNotificationOperation(data, NotificationCommon::CLICK, -1); |
| 193 // NotificationPlatformBridgeLinux::NotifyCompleteInternal() with a | 509 } else if (action == "settings") { |
| 194 // destroyed Notification. | 510 ForwardNotificationOperation(data, NotificationCommon::SETTINGS, -1); |
| 195 ScopedGObject<GCancellable> cancellable; | 511 } else { |
| 196 | 512 size_t id; |
| 197 // If not null, the data to update the notification with once | 513 if (!base::StringToSizeT(action, &id)) |
| 198 // |dbus_id| becomes available. | 514 return; |
| 199 std::unique_ptr<Notification> update_data; | 515 size_t n_buttons = data->action_end - data->action_start; |
| 200 NotificationCommon::Type update_notification_type = | 516 size_t id_zero_based = id - data->action_start; |
| 201 NotificationCommon::TYPE_MAX; | 517 if (id_zero_based >= n_buttons) |
| 202 | 518 return; |
| 203 // If true, indicates the notification should be closed once | 519 ForwardNotificationOperation(data, NotificationCommon::CLICK, |
| 204 // |dbus_id| becomes available. | 520 id_zero_based); |
| 205 bool should_close = false; | 521 } |
| 206 | 522 } |
| 207 base::WeakPtrFactory<NotificationData> weak_factory; | 523 |
| 524 void OnNotificationClosed(dbus::Signal* signal) { | |
| 525 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 526 dbus::MessageReader reader(signal); | |
| 527 uint32_t dbus_id; | |
| 528 if (!reader.PopUint32(&dbus_id)) | |
| 529 return; | |
| 530 | |
| 531 NotificationData* data = FindNotificationData(dbus_id); | |
| 532 if (!data) | |
| 533 return; | |
| 534 | |
| 535 ForwardNotificationOperation(data, NotificationCommon::CLOSE, -1); | |
| 536 notifications_.erase(data); | |
| 537 } | |
| 538 | |
| 539 // Called once the connection has been set up (or not). |success| | |
| 540 // indicates the connection is ready to use. | |
| 541 void OnConnectionInitializationFinished(bool success) { | |
| 542 DCHECK(task_runner_->RunsTasksOnCurrentThread()); | |
| 543 { | |
| 544 base::AutoLock lock(connected_lock_); | |
| 545 connected_ = success; | |
| 546 for (auto& callback : on_connected_callbacks_) { | |
| 547 content::BrowserThread::PostTask( | |
| 548 content::BrowserThread::UI, FROM_HERE, | |
| 549 base::BindOnce(std::move(callback), success)); | |
| 550 } | |
| 551 on_connected_callbacks_.clear(); | |
| 552 } | |
| 553 if (!success) | |
| 554 CleanUpOnTaskRunnerThread(); | |
| 555 } | |
| 556 | |
| 557 void OnSignalConnected(const std::string& interface_name, | |
| 558 const std::string& signal_name, | |
| 559 bool success) { | |
| 560 if (!success) { | |
| 561 OnConnectionInitializationFinished(false); | |
|
Lei Zhang
2017/04/25 19:05:50
If both signals fail to connect, won't this run tw
Tom (Use chromium acct)
2017/04/26 03:12:28
Yes but it should have been harmless.
Added |clean
| |
| 562 return; | |
| 563 } | |
| 564 connected_signals_barrier_.Run(); | |
| 565 } | |
| 566 | |
| 567 // State necessary for OnConnectionInitializationFinished() and | |
| 568 // SetReadyCallback(). | |
| 569 base::Optional<bool> connected_; | |
| 570 std::vector<base::OnceCallback<void(bool)>> on_connected_callbacks_; | |
| 571 // Protects both |connected_| and |on_connected_callbacks_|. | |
| 572 base::Lock connected_lock_; | |
| 573 | |
| 574 scoped_refptr<base::SequencedTaskRunner> task_runner_; | |
| 575 | |
| 576 scoped_refptr<dbus::Bus> bus_; | |
|
Lei Zhang
2017/04/25 19:05:50
You may want to group the member variables below i
Tom (Use chromium acct)
2017/04/26 03:12:28
Done.
| |
| 577 | |
| 578 base::Closure connected_signals_barrier_; | |
| 579 | |
| 580 dbus::ObjectProxy* notification_proxy_ = nullptr; | |
| 581 | |
| 582 // A std::set<std::unique_ptr<T>> doesn't work well because | |
| 583 // eg. std::set::erase(T) would require a std::unique_ptr<T> | |
| 584 // argument, so the data would get double-destructed. | |
| 585 template <typename T> | |
| 586 using UnorderedUniqueSet = std::unordered_map<T*, std::unique_ptr<T>>; | |
| 587 | |
| 588 UnorderedUniqueSet<NotificationData> notifications_; | |
| 589 | |
| 590 content::NotificationRegistrar registrar_; | |
| 591 | |
| 592 DISALLOW_COPY_AND_ASSIGN(NotificationPlatformBridgeLinuxImpl); | |
| 208 }; | 593 }; |
| 209 | 594 |
| 210 struct NotificationPlatformBridgeLinux::ResourceFiles { | 595 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux() |
| 211 explicit ResourceFiles(const base::FilePath& icon_file) | 596 : impl_(new NotificationPlatformBridgeLinuxImpl()) {} |
| 212 : icon_file(icon_file) {} | 597 |
| 213 ~ResourceFiles() { DeleteNotificationResourceFile(icon_file); } | 598 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() = default; |
| 214 base::FilePath icon_file; | |
| 215 }; | |
| 216 | |
| 217 NotificationPlatformBridgeLinux::NotificationPlatformBridgeLinux( | |
| 218 GDBusProxy* notification_proxy) | |
| 219 : notification_proxy_(notification_proxy), weak_factory_(this) { | |
| 220 proxy_signal_handler_ = g_signal_connect( | |
| 221 notification_proxy_, "g-signal", G_CALLBACK(GSignalReceiverThunk), this); | |
| 222 registrar_.Add(this, chrome::NOTIFICATION_APP_TERMINATING, | |
| 223 content::NotificationService::AllSources()); | |
| 224 } | |
| 225 | |
| 226 NotificationPlatformBridgeLinux::~NotificationPlatformBridgeLinux() { | |
| 227 if (proxy_signal_handler_) | |
| 228 g_signal_handler_disconnect(notification_proxy_, proxy_signal_handler_); | |
| 229 } | |
| 230 | 599 |
| 231 void NotificationPlatformBridgeLinux::Display( | 600 void NotificationPlatformBridgeLinux::Display( |
| 232 NotificationCommon::Type notification_type, | 601 NotificationCommon::Type notification_type, |
| 233 const std::string& notification_id, | 602 const std::string& notification_id, |
| 234 const std::string& profile_id, | 603 const std::string& profile_id, |
| 235 bool is_incognito, | 604 bool is_incognito, |
| 236 const Notification& notification) { | 605 const Notification& notification) { |
| 237 NotificationData* data = | 606 impl_->Display(notification_type, notification_id, profile_id, is_incognito, |
| 238 FindNotificationData(notification_id, profile_id, is_incognito); | 607 notification); |
| 239 if (data) { | |
| 240 // Update an existing notification. | |
| 241 if (data->dbus_id) { | |
| 242 data->notification_type = notification_type; | |
| 243 Notify(notification, data, nullptr, nullptr); | |
| 244 } else { | |
| 245 data->update_notification_type = notification_type; | |
| 246 data->update_data = base::MakeUnique<Notification>(notification); | |
| 247 } | |
| 248 } else { | |
| 249 // Send the notification for the first time. | |
| 250 data = new NotificationData(notification_type, notification_id, profile_id, | |
| 251 is_incognito, notification.origin_url()); | |
| 252 data->cancellable.reset(g_cancellable_new()); | |
| 253 notifications_.emplace(data, base::WrapUnique(data)); | |
| 254 Notify(notification, data, NotifyCompleteReceiver, data); | |
| 255 } | |
| 256 } | 608 } |
| 257 | 609 |
| 258 void NotificationPlatformBridgeLinux::Close( | 610 void NotificationPlatformBridgeLinux::Close( |
| 259 const std::string& profile_id, | 611 const std::string& profile_id, |
| 260 const std::string& notification_id) { | 612 const std::string& notification_id) { |
| 261 std::vector<NotificationData*> to_erase; | 613 impl_->Close(profile_id, notification_id); |
| 262 for (const auto& pair : notifications_) { | |
| 263 NotificationData* data = pair.first; | |
| 264 if (data->notification_id == notification_id && | |
| 265 data->profile_id == profile_id) { | |
| 266 if (data->dbus_id) { | |
| 267 CloseNow(data->dbus_id); | |
| 268 to_erase.push_back(data); | |
| 269 } else { | |
| 270 data->should_close = true; | |
| 271 } | |
| 272 } | |
| 273 } | |
| 274 for (NotificationData* data : to_erase) | |
| 275 notifications_.erase(data); | |
| 276 } | 614 } |
| 277 | 615 |
| 278 void NotificationPlatformBridgeLinux::GetDisplayed( | 616 void NotificationPlatformBridgeLinux::GetDisplayed( |
| 279 const std::string& profile_id, | 617 const std::string& profile_id, |
| 280 bool incognito, | 618 bool incognito, |
| 281 const GetDisplayedNotificationsCallback& callback) const { | 619 const GetDisplayedNotificationsCallback& callback) const { |
| 282 // TODO(thomasanderson): implement. | 620 impl_->GetDisplayed(profile_id, incognito, callback); |
| 283 callback.Run(base::MakeUnique<std::set<std::string>>(), false); | |
| 284 } | 621 } |
| 285 | 622 |
| 286 void NotificationPlatformBridgeLinux::NotifyCompleteInternal(gpointer user_data, | 623 void NotificationPlatformBridgeLinux::SetReadyCallback( |
| 287 GVariant* value) { | 624 NotificationBridgeReadyCallback callback) { |
| 288 NotificationData* data = reinterpret_cast<NotificationData*>(user_data); | 625 impl_->SetReadyCallback(std::move(callback)); |
| 289 if (!base::ContainsKey(notifications_, data)) | |
| 290 return; | |
| 291 data->cancellable.reset(); | |
| 292 if (value && g_variant_is_of_type(value, G_VARIANT_TYPE("(u)"))) | |
| 293 g_variant_get(value, "(u)", &data->dbus_id); | |
| 294 | |
| 295 if (!data->dbus_id) { | |
| 296 // There was some sort of error with creating the notification. | |
| 297 notifications_.erase(data); | |
| 298 } else if (data->should_close) { | |
| 299 CloseNow(data->dbus_id); | |
| 300 notifications_.erase(data); | |
| 301 } else if (data->update_data) { | |
| 302 data->notification_type = data->update_notification_type; | |
| 303 Notify(*data->update_data, data, nullptr, nullptr); | |
| 304 data->update_data.reset(); | |
| 305 } | |
| 306 } | 626 } |
| 307 | |
| 308 void NotificationPlatformBridgeLinux::Observe( | |
| 309 int type, | |
| 310 const content::NotificationSource& source, | |
| 311 const content::NotificationDetails& details) { | |
| 312 DCHECK_EQ(chrome::NOTIFICATION_APP_TERMINATING, type); | |
| 313 // The browser process is about to exit. Clean up all notification | |
| 314 // resource files. | |
| 315 notifications_.clear(); | |
| 316 } | |
| 317 | |
| 318 void NotificationPlatformBridgeLinux::Notify(const Notification& notification, | |
| 319 NotificationData* data, | |
| 320 GAsyncReadyCallback callback, | |
| 321 gpointer user_data) { | |
| 322 const scoped_refptr<base::RefCountedMemory> icon_data = | |
| 323 notification.icon().As1xPNGBytes(); | |
| 324 if (!icon_data->size()) { | |
| 325 NotifyNow(notification, data->weak_factory.GetWeakPtr(), callback, | |
| 326 user_data, base::MakeUnique<ResourceFiles>(base::FilePath())); | |
| 327 } else { | |
| 328 base::PostTaskWithTraitsAndReplyWithResult( | |
| 329 FROM_HERE, | |
| 330 base::TaskTraits() | |
| 331 .MayBlock() | |
| 332 .WithPriority(base::TaskPriority::USER_BLOCKING) | |
| 333 .WithShutdownBehavior(base::TaskShutdownBehavior::SKIP_ON_SHUTDOWN), | |
| 334 base::Bind( | |
| 335 [](scoped_refptr<base::RefCountedMemory> icon) { | |
| 336 return base::MakeUnique<ResourceFiles>(WriteDataToTmpFile(icon)); | |
| 337 }, | |
| 338 icon_data), | |
| 339 base::Bind(&NotificationPlatformBridgeLinux::NotifyNow, | |
| 340 weak_factory_.GetWeakPtr(), notification, | |
| 341 data->weak_factory.GetWeakPtr(), callback, user_data)); | |
| 342 } | |
| 343 } | |
| 344 | |
| 345 void NotificationPlatformBridgeLinux::NotifyNow( | |
| 346 const Notification& notification, | |
| 347 base::WeakPtr<NotificationData> data, | |
| 348 GAsyncReadyCallback callback, | |
| 349 gpointer user_data, | |
| 350 std::unique_ptr<ResourceFiles> resource_files) { | |
| 351 if (!data) | |
| 352 return; | |
| 353 | |
| 354 if (data->dbus_id) | |
| 355 DCHECK(!data->cancellable); | |
| 356 | |
| 357 data->ResetResourceFiles(); | |
| 358 | |
| 359 GVariantBuilder actions_builder; | |
| 360 // Even-indexed elements in this array are action IDs passed back to | |
| 361 // us in GSignalReceiver. Odd-indexed ones contain the button text. | |
| 362 g_variant_builder_init(&actions_builder, G_VARIANT_TYPE("as")); | |
| 363 data->action_start = data->action_end; | |
| 364 for (const auto& button_info : notification.buttons()) { | |
| 365 // FDO notification buttons can contain either an icon or a label, | |
| 366 // but not both, and the type of all buttons must be the same (all | |
| 367 // labels or all icons), so always use labels. | |
| 368 std::string id = base::SizeTToString(data->action_end++); | |
| 369 const std::string label = base::UTF16ToUTF8(button_info.title); | |
| 370 AddActionToNotification(&actions_builder, id.c_str(), label.c_str()); | |
| 371 } | |
| 372 if (notification.clickable()) { | |
| 373 // Special case: the pair ("default", "") will not add a button, | |
| 374 // but instead makes the entire notification clickable. | |
| 375 AddActionToNotification(&actions_builder, "default", ""); | |
| 376 } | |
| 377 // Always add a settings button. | |
| 378 AddActionToNotification(&actions_builder, "settings", "Settings"); | |
| 379 | |
| 380 GVariantBuilder hints_builder; | |
| 381 g_variant_builder_init(&hints_builder, G_VARIANT_TYPE("a{sv}")); | |
| 382 | |
| 383 g_variant_builder_add(&hints_builder, "{sv}", "urgency", | |
| 384 g_variant_new_byte(NotificationPriorityToFdoUrgency( | |
| 385 notification.priority()))); | |
| 386 | |
| 387 std::unique_ptr<base::Environment> env = base::Environment::Create(); | |
| 388 base::FilePath desktop_file( | |
| 389 shell_integration_linux::GetDesktopName(env.get())); | |
| 390 const char kDesktopFileSuffix[] = ".desktop"; | |
| 391 DCHECK(base::EndsWith(desktop_file.value(), kDesktopFileSuffix, | |
| 392 base::CompareCase::SENSITIVE)); | |
| 393 | |
| 394 desktop_file = desktop_file.RemoveFinalExtension(); | |
| 395 g_variant_builder_add(&hints_builder, "{sv}", "desktop-entry", | |
| 396 g_variant_new_string(desktop_file.value().c_str())); | |
| 397 | |
| 398 if (!resource_files->icon_file.empty()) { | |
| 399 g_variant_builder_add( | |
| 400 &hints_builder, "{sv}", "image-path", | |
| 401 g_variant_new_string(resource_files->icon_file.value().c_str())); | |
| 402 data->resource_files.push_back(resource_files->icon_file); | |
| 403 resource_files->icon_file.clear(); | |
| 404 } | |
| 405 | |
| 406 const std::string title = base::UTF16ToUTF8(notification.title()); | |
| 407 const std::string message = base::UTF16ToUTF8(notification.message()); | |
| 408 | |
| 409 GVariant* parameters = g_variant_new( | |
| 410 "(susssasa{sv}i)", "" /* app_name passed implicitly via desktop-entry */, | |
| 411 data->dbus_id, "" /* app_icon passed implicitly via desktop-entry */, | |
| 412 title.c_str(), message.c_str(), &actions_builder, &hints_builder, -1); | |
| 413 g_dbus_proxy_call(notification_proxy_, "Notify", parameters, | |
| 414 G_DBUS_CALL_FLAGS_NONE, -1, data->cancellable, callback, | |
| 415 user_data); | |
| 416 } | |
| 417 | |
| 418 void NotificationPlatformBridgeLinux::CloseNow(uint32_t dbus_id) { | |
| 419 g_dbus_proxy_call(notification_proxy_, "CloseNotification", | |
| 420 g_variant_new("(u)", dbus_id), G_DBUS_CALL_FLAGS_NONE, -1, | |
| 421 nullptr, nullptr, nullptr); | |
| 422 } | |
| 423 | |
| 424 NotificationPlatformBridgeLinux::NotificationData* | |
| 425 NotificationPlatformBridgeLinux::FindNotificationData( | |
| 426 const std::string& notification_id, | |
| 427 const std::string& profile_id, | |
| 428 bool is_incognito) { | |
| 429 for (const auto& pair : notifications_) { | |
| 430 NotificationData* data = pair.first; | |
| 431 if (data->notification_id == notification_id && | |
| 432 data->profile_id == profile_id && data->is_incognito == is_incognito) { | |
| 433 return data; | |
| 434 } | |
| 435 } | |
| 436 | |
| 437 return nullptr; | |
| 438 } | |
| 439 | |
| 440 NotificationPlatformBridgeLinux::NotificationData* | |
| 441 NotificationPlatformBridgeLinux::FindNotificationData(uint32_t dbus_id) { | |
| 442 for (const auto& pair : notifications_) { | |
| 443 NotificationData* data = pair.first; | |
| 444 if (data->dbus_id == dbus_id) | |
| 445 return data; | |
| 446 } | |
| 447 | |
| 448 return nullptr; | |
| 449 } | |
| 450 | |
| 451 void NotificationPlatformBridgeLinux::ForwardNotificationOperation( | |
| 452 uint32_t dbus_id, | |
| 453 NotificationCommon::Operation operation, | |
| 454 int action_index) { | |
| 455 NotificationData* data = FindNotificationData(dbus_id); | |
| 456 if (!data) { | |
| 457 // This notification either belongs to a different app or we | |
| 458 // already removed the NotificationData after sending a | |
| 459 // "CloseNotification" message. | |
| 460 return; | |
| 461 } | |
| 462 | |
| 463 ProfileManager* profile_manager = g_browser_process->profile_manager(); | |
| 464 DCHECK(profile_manager); | |
| 465 | |
| 466 profile_manager->LoadProfile( | |
| 467 data->profile_id, data->is_incognito, | |
| 468 base::Bind(&ProfileLoadedCallback, operation, data->notification_type, | |
| 469 data->origin_url.spec(), data->notification_id, action_index, | |
| 470 base::NullableString16())); | |
| 471 } | |
| 472 | |
| 473 void NotificationPlatformBridgeLinux::GSignalReceiver(GDBusProxy* proxy, | |
| 474 const char* sender_name, | |
| 475 const char* sender_signal, | |
| 476 GVariant* parameters) { | |
| 477 uint32_t dbus_id = 0; | |
| 478 if (strcmp("NotificationClosed", sender_signal) == 0 && | |
| 479 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(uu)"))) { | |
| 480 uint32_t reason; | |
| 481 g_variant_get(parameters, "(uu)", &dbus_id, &reason); | |
| 482 ForwardNotificationOperation(dbus_id, NotificationCommon::CLOSE, -1); | |
| 483 // std::unordered_map::erase(nullptr) is safe here. | |
| 484 notifications_.erase(FindNotificationData(dbus_id)); | |
| 485 } else if (strcmp("ActionInvoked", sender_signal) == 0 && | |
| 486 g_variant_is_of_type(parameters, G_VARIANT_TYPE("(us)"))) { | |
| 487 const gchar* action = nullptr; | |
| 488 g_variant_get(parameters, "(u&s)", &dbus_id, &action); | |
| 489 DCHECK(action); | |
| 490 | |
| 491 if (strcmp(action, "default") == 0) { | |
| 492 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, -1); | |
| 493 } else if (strcmp(action, "settings") == 0) { | |
| 494 ForwardNotificationOperation(dbus_id, NotificationCommon::SETTINGS, -1); | |
| 495 } else { | |
| 496 size_t id; | |
| 497 if (!base::StringToSizeT(action, &id)) | |
| 498 return; | |
| 499 NotificationData* data = FindNotificationData(dbus_id); | |
| 500 if (!data) | |
| 501 return; | |
| 502 size_t n_buttons = data->action_end - data->action_start; | |
| 503 size_t id_zero_based = id - data->action_start; | |
| 504 if (id_zero_based >= n_buttons) | |
| 505 return; | |
| 506 ForwardNotificationOperation(dbus_id, NotificationCommon::CLICK, | |
| 507 id_zero_based); | |
| 508 } | |
| 509 } | |
| 510 } | |
| OLD | NEW |