| 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 #include <memory> | 8 #include <memory> |
| 9 #include <set> | 9 #include <set> |
| 10 #include <unordered_map> | 10 #include <unordered_map> |
| 11 #include <unordered_set> | 11 #include <unordered_set> |
| 12 #include <utility> | 12 #include <utility> |
| 13 #include <vector> | 13 #include <vector> |
| 14 | 14 |
| 15 #include "base/barrier_closure.h" | 15 #include "base/barrier_closure.h" |
| 16 #include "base/files/file_util.h" | 16 #include "base/files/file_util.h" |
| 17 #include "base/metrics/histogram_macros.h" | 17 #include "base/metrics/histogram_macros.h" |
| 18 #include "base/strings/nullable_string16.h" | 18 #include "base/strings/nullable_string16.h" |
| 19 #include "base/strings/string_number_conversions.h" | 19 #include "base/strings/string_number_conversions.h" |
| 20 #include "base/strings/string_split.h" |
| 20 #include "base/strings/string_util.h" | 21 #include "base/strings/string_util.h" |
| 21 #include "base/strings/utf_string_conversions.h" | 22 #include "base/strings/utf_string_conversions.h" |
| 22 #include "base/task_scheduler/post_task.h" | 23 #include "base/task_scheduler/post_task.h" |
| 24 #include "base/version.h" |
| 23 #include "chrome/browser/browser_process.h" | 25 #include "chrome/browser/browser_process.h" |
| 24 #include "chrome/browser/chrome_notification_types.h" | 26 #include "chrome/browser/chrome_notification_types.h" |
| 25 #include "chrome/browser/notifications/native_notification_display_service.h" | 27 #include "chrome/browser/notifications/native_notification_display_service.h" |
| 26 #include "chrome/browser/notifications/notification.h" | 28 #include "chrome/browser/notifications/notification.h" |
| 27 #include "chrome/browser/notifications/notification_display_service_factory.h" | 29 #include "chrome/browser/notifications/notification_display_service_factory.h" |
| 28 #include "chrome/browser/profiles/profile_manager.h" | 30 #include "chrome/browser/profiles/profile_manager.h" |
| 29 #include "chrome/browser/shell_integration_linux.h" | 31 #include "chrome/browser/shell_integration_linux.h" |
| 30 #include "content/public/browser/browser_thread.h" | 32 #include "content/public/browser/browser_thread.h" |
| 31 #include "content/public/browser/notification_service.h" | 33 #include "content/public/browser/notification_service.h" |
| 32 #include "dbus/bus.h" | 34 #include "dbus/bus.h" |
| (...skipping 11 matching lines...) Expand all Loading... |
| 44 | 46 |
| 45 // The values in this enumeration correspond to those of the | 47 // The values in this enumeration correspond to those of the |
| 46 // Linux.NotificationPlatformBridge.InitializationStatus histogram, so | 48 // Linux.NotificationPlatformBridge.InitializationStatus histogram, so |
| 47 // the ordering should not be changed. New error codes should be | 49 // the ordering should not be changed. New error codes should be |
| 48 // added at the end, before NUM_ITEMS. | 50 // added at the end, before NUM_ITEMS. |
| 49 enum class ConnectionInitializationStatusCode { | 51 enum class ConnectionInitializationStatusCode { |
| 50 SUCCESS = 0, | 52 SUCCESS = 0, |
| 51 NATIVE_NOTIFICATIONS_NOT_SUPPORTED = 1, | 53 NATIVE_NOTIFICATIONS_NOT_SUPPORTED = 1, |
| 52 MISSING_REQUIRED_CAPABILITIES = 2, | 54 MISSING_REQUIRED_CAPABILITIES = 2, |
| 53 COULD_NOT_CONNECT_TO_SIGNALS = 3, | 55 COULD_NOT_CONNECT_TO_SIGNALS = 3, |
| 56 INCOMPATIBLE_SPEC_VERSION = 4, |
| 54 NUM_ITEMS | 57 NUM_ITEMS |
| 55 }; | 58 }; |
| 56 | 59 |
| 57 gfx::Image DeepCopyImage(const gfx::Image& image) { | 60 gfx::Image DeepCopyImage(const gfx::Image& image) { |
| 58 if (image.IsEmpty()) | 61 if (image.IsEmpty()) |
| 59 return gfx::Image(); | 62 return gfx::Image(); |
| 60 std::unique_ptr<gfx::ImageSkia> image_skia(image.CopyImageSkia()); | 63 std::unique_ptr<gfx::ImageSkia> image_skia(image.CopyImageSkia()); |
| 61 return gfx::Image(*image_skia); | 64 return gfx::Image(*image_skia); |
| 62 } | 65 } |
| 63 | 66 |
| (...skipping 276 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 340 &get_capabilities_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); | 343 &get_capabilities_call, dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
| 341 if (capabilities_response) { | 344 if (capabilities_response) { |
| 342 dbus::MessageReader reader(capabilities_response.get()); | 345 dbus::MessageReader reader(capabilities_response.get()); |
| 343 std::vector<std::string> capabilities; | 346 std::vector<std::string> capabilities; |
| 344 reader.PopArrayOfStrings(&capabilities); | 347 reader.PopArrayOfStrings(&capabilities); |
| 345 for (const std::string& capability : capabilities) | 348 for (const std::string& capability : capabilities) |
| 346 capabilities_.insert(capability); | 349 capabilities_.insert(capability); |
| 347 } | 350 } |
| 348 RecordMetricsForCapabilities(); | 351 RecordMetricsForCapabilities(); |
| 349 | 352 |
| 353 dbus::MethodCall get_server_information_call(kFreedesktopNotificationsName, |
| 354 "GetServerInformation"); |
| 355 std::unique_ptr<dbus::Response> server_information_response = |
| 356 notification_proxy_->CallMethodAndBlock( |
| 357 &get_server_information_call, |
| 358 dbus::ObjectProxy::TIMEOUT_USE_DEFAULT); |
| 359 if (server_information_response) { |
| 360 dbus::MessageReader reader(server_information_response.get()); |
| 361 std::string spec_version; |
| 362 reader.PopString(&spec_version); // name |
| 363 reader.PopString(&spec_version); // vendor |
| 364 reader.PopString(&spec_version); // version |
| 365 reader.PopString(&spec_version); // spec_version |
| 366 spec_version_ = base::Version(spec_version); |
| 367 } |
| 368 // The minimum supported spec version is 1.1, because this was the |
| 369 // version that added image hints. |
| 370 if (!spec_version_.IsValid() || |
| 371 spec_version_ < base::Version(std::vector<uint32_t>{1, 1})) { |
| 372 OnConnectionInitializationFinishedOnTaskRunner( |
| 373 ConnectionInitializationStatusCode::INCOMPATIBLE_SPEC_VERSION); |
| 374 return; |
| 375 } |
| 376 |
| 350 connected_signals_barrier_ = base::BarrierClosure( | 377 connected_signals_barrier_ = base::BarrierClosure( |
| 351 2, base::Bind(&NotificationPlatformBridgeLinuxImpl:: | 378 2, base::Bind(&NotificationPlatformBridgeLinuxImpl:: |
| 352 OnConnectionInitializationFinishedOnTaskRunner, | 379 OnConnectionInitializationFinishedOnTaskRunner, |
| 353 this, ConnectionInitializationStatusCode::SUCCESS)); | 380 this, ConnectionInitializationStatusCode::SUCCESS)); |
| 354 notification_proxy_->ConnectToSignal( | 381 notification_proxy_->ConnectToSignal( |
| 355 kFreedesktopNotificationsName, "ActionInvoked", | 382 kFreedesktopNotificationsName, "ActionInvoked", |
| 356 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnActionInvoked, this), | 383 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnActionInvoked, this), |
| 357 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnSignalConnected, | 384 base::Bind(&NotificationPlatformBridgeLinuxImpl::OnSignalConnected, |
| 358 this)); | 385 this)); |
| 359 notification_proxy_->ConnectToSignal( | 386 notification_proxy_->ConnectToSignal( |
| (...skipping 105 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 465 hints_writer.OpenDictEntry(&desktop_entry_writer); | 492 hints_writer.OpenDictEntry(&desktop_entry_writer); |
| 466 desktop_entry_writer.AppendString("desktop-entry"); | 493 desktop_entry_writer.AppendString("desktop-entry"); |
| 467 desktop_entry_writer.AppendVariantOfString(desktop_file.value()); | 494 desktop_entry_writer.AppendVariantOfString(desktop_file.value()); |
| 468 hints_writer.CloseContainer(&desktop_entry_writer); | 495 hints_writer.CloseContainer(&desktop_entry_writer); |
| 469 | 496 |
| 470 std::unique_ptr<ResourceFile> icon_file = | 497 std::unique_ptr<ResourceFile> icon_file = |
| 471 WriteDataToTmpFile(notification->icon().As1xPNGBytes()); | 498 WriteDataToTmpFile(notification->icon().As1xPNGBytes()); |
| 472 if (icon_file) { | 499 if (icon_file) { |
| 473 dbus::MessageWriter image_path_writer(nullptr); | 500 dbus::MessageWriter image_path_writer(nullptr); |
| 474 hints_writer.OpenDictEntry(&image_path_writer); | 501 hints_writer.OpenDictEntry(&image_path_writer); |
| 475 image_path_writer.AppendString("image-path"); | 502 image_path_writer.AppendString( |
| 503 spec_version_ == base::Version(std::vector<uint32_t>{1, 1}) |
| 504 ? "image_path" |
| 505 : "image-path"); |
| 476 image_path_writer.AppendVariantOfString(icon_file->file_path().value()); | 506 image_path_writer.AppendVariantOfString(icon_file->file_path().value()); |
| 477 hints_writer.CloseContainer(&image_path_writer); | 507 hints_writer.CloseContainer(&image_path_writer); |
| 478 data->resource_files.push_back(std::move(icon_file)); | 508 data->resource_files.push_back(std::move(icon_file)); |
| 479 } | 509 } |
| 480 | 510 |
| 481 writer.CloseContainer(&hints_writer); | 511 writer.CloseContainer(&hints_writer); |
| 482 | 512 |
| 483 const int32_t kExpireTimeoutDefault = -1; | 513 const int32_t kExpireTimeoutDefault = -1; |
| 484 writer.AppendInt32(kExpireTimeoutDefault); | 514 writer.AppendInt32(kExpireTimeoutDefault); |
| 485 | 515 |
| (...skipping 207 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 693 | 723 |
| 694 ////////////////////////////////////////////////////////////////////////////// | 724 ////////////////////////////////////////////////////////////////////////////// |
| 695 // Members used only on the task runner thread. | 725 // Members used only on the task runner thread. |
| 696 | 726 |
| 697 scoped_refptr<dbus::Bus> bus_; | 727 scoped_refptr<dbus::Bus> bus_; |
| 698 | 728 |
| 699 dbus::ObjectProxy* notification_proxy_ = nullptr; | 729 dbus::ObjectProxy* notification_proxy_ = nullptr; |
| 700 | 730 |
| 701 std::unordered_set<std::string> capabilities_; | 731 std::unordered_set<std::string> capabilities_; |
| 702 | 732 |
| 733 base::Version spec_version_; |
| 734 |
| 703 base::Closure connected_signals_barrier_; | 735 base::Closure connected_signals_barrier_; |
| 704 | 736 |
| 705 // A std::set<std::unique_ptr<T>> doesn't work well because | 737 // A std::set<std::unique_ptr<T>> doesn't work well because |
| 706 // eg. std::set::erase(T) would require a std::unique_ptr<T> | 738 // eg. std::set::erase(T) would require a std::unique_ptr<T> |
| 707 // argument, so the data would get double-destructed. | 739 // argument, so the data would get double-destructed. |
| 708 template <typename T> | 740 template <typename T> |
| 709 using UnorderedUniqueSet = std::unordered_map<T*, std::unique_ptr<T>>; | 741 using UnorderedUniqueSet = std::unordered_map<T*, std::unique_ptr<T>>; |
| 710 | 742 |
| 711 UnorderedUniqueSet<NotificationData> notifications_; | 743 UnorderedUniqueSet<NotificationData> notifications_; |
| 712 | 744 |
| (...skipping 35 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
| 748 } | 780 } |
| 749 | 781 |
| 750 void NotificationPlatformBridgeLinux::SetReadyCallback( | 782 void NotificationPlatformBridgeLinux::SetReadyCallback( |
| 751 NotificationBridgeReadyCallback callback) { | 783 NotificationBridgeReadyCallback callback) { |
| 752 impl_->SetReadyCallback(std::move(callback)); | 784 impl_->SetReadyCallback(std::move(callback)); |
| 753 } | 785 } |
| 754 | 786 |
| 755 void NotificationPlatformBridgeLinux::CleanUp() { | 787 void NotificationPlatformBridgeLinux::CleanUp() { |
| 756 impl_->CleanUp(); | 788 impl_->CleanUp(); |
| 757 } | 789 } |
| OLD | NEW |