notification_presenter_win.cc 2.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293
  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::vector<unsigned char> png_data;
  28. if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
  29. return false;
  30. return base::WriteFile(path, png_data);
  31. }
  32. } // namespace
  33. // static
  34. NotificationPresenter* NotificationPresenter::Create() {
  35. if (!WindowsToastNotification::Initialize())
  36. return nullptr;
  37. auto presenter = std::make_unique<NotificationPresenterWin>();
  38. if (!presenter->Init())
  39. return nullptr;
  40. if (IsDebuggingNotifications())
  41. LOG(INFO) << "Successfully created Windows notifications presenter";
  42. return presenter.release();
  43. }
  44. NotificationPresenterWin::NotificationPresenterWin() = default;
  45. NotificationPresenterWin::~NotificationPresenterWin() = default;
  46. bool NotificationPresenterWin::Init() {
  47. ScopedAllowBlockingForElectron allow_blocking;
  48. return temp_dir_.CreateUniqueTempDir();
  49. }
  50. std::wstring NotificationPresenterWin::SaveIconToFilesystem(
  51. const SkBitmap& icon,
  52. const GURL& origin) {
  53. std::string filename;
  54. if (origin.is_valid()) {
  55. filename = base::MD5String(origin.spec()) + ".png";
  56. } else {
  57. const int64_t now_usec = base::Time::Now().since_origin().InMicroseconds();
  58. filename = base::NumberToString(now_usec) + ".png";
  59. }
  60. ScopedAllowBlockingForElectron allow_blocking;
  61. base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToWide(filename));
  62. if (base::PathExists(path))
  63. return path.value();
  64. if (SaveIconToPath(icon, path))
  65. return path.value();
  66. return base::UTF8ToWide(origin.spec());
  67. }
  68. Notification* NotificationPresenterWin::CreateNotificationObject(
  69. NotificationDelegate* delegate) {
  70. return new WindowsToastNotification(delegate, this);
  71. }
  72. } // namespace electron