notification_presenter_win.cc 2.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Copyright (c) 2015 Felix Rieseberg <[email protected]> and
  3. // Jason Poon <[email protected]>. All rights reserved.
  4. // Use of this source code is governed by a BSD-style license that can be
  5. // found in the LICENSE-CHROMIUM file.
  6. #include "shell/browser/notifications/win/notification_presenter_win.h"
  7. #include <memory>
  8. #include <string>
  9. #include <vector>
  10. #include "base/environment.h"
  11. #include "base/files/file_util.h"
  12. #include "base/hash/md5.h"
  13. #include "base/logging.h"
  14. #include "base/strings/utf_string_conversions.h"
  15. #include "base/time/time.h"
  16. #include "shell/browser/notifications/win/windows_toast_notification.h"
  17. #include "shell/common/thread_restrictions.h"
  18. #include "third_party/skia/include/core/SkBitmap.h"
  19. #include "ui/gfx/codec/png_codec.h"
  20. #pragma comment(lib, "runtimeobject.lib")
  21. namespace electron {
  22. namespace {
  23. bool IsDebuggingNotifications() {
  24. return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
  25. }
  26. bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
  27. std::optional<std::vector<uint8_t>> png_data =
  28. gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
  29. if (!png_data.has_value())
  30. return false;
  31. return base::WriteFile(path, png_data.value());
  32. }
  33. } // namespace
  34. // static
  35. std::unique_ptr<NotificationPresenter> NotificationPresenter::Create() {
  36. if (!WindowsToastNotification::Initialize())
  37. return {};
  38. auto presenter = std::make_unique<NotificationPresenterWin>();
  39. if (!presenter->Init())
  40. return {};
  41. if (IsDebuggingNotifications())
  42. LOG(INFO) << "Successfully created Windows notifications presenter";
  43. return presenter;
  44. }
  45. NotificationPresenterWin::NotificationPresenterWin() = default;
  46. NotificationPresenterWin::~NotificationPresenterWin() = default;
  47. bool NotificationPresenterWin::Init() {
  48. ScopedAllowBlockingForElectron allow_blocking;
  49. return temp_dir_.CreateUniqueTempDir();
  50. }
  51. std::wstring NotificationPresenterWin::SaveIconToFilesystem(
  52. const SkBitmap& icon,
  53. const GURL& origin) {
  54. std::string filename;
  55. if (origin.is_valid()) {
  56. filename = base::MD5String(origin.spec()) + ".png";
  57. } else {
  58. const int64_t now_usec = base::Time::Now().since_origin().InMicroseconds();
  59. filename = base::NumberToString(now_usec) + ".png";
  60. }
  61. ScopedAllowBlockingForElectron allow_blocking;
  62. base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToWide(filename));
  63. if (base::PathExists(path))
  64. return path.value();
  65. if (SaveIconToPath(icon, path))
  66. return path.value();
  67. return base::UTF8ToWide(origin.spec());
  68. }
  69. Notification* NotificationPresenterWin::CreateNotificationObject(
  70. NotificationDelegate* delegate) {
  71. return new WindowsToastNotification(delegate, this);
  72. }
  73. } // namespace electron