notification_presenter_win.cc 3.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101
  1. // Copyright (c) 2012 The Chromium Authors. All rights reserved.
  2. // Copyright (c) 2015 Felix Rieseberg <feriese@microsoft.com> and
  3. // Jason Poon <jason.poon@microsoft.com>. 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 "brightray/browser/win/notification_presenter_win.h"
  7. #include <string>
  8. #include <vector>
  9. #include "base/environment.h"
  10. #include "base/files/file_util.h"
  11. #include "base/md5.h"
  12. #include "base/strings/utf_string_conversions.h"
  13. #include "base/threading/thread_restrictions.h"
  14. #include "base/time/time.h"
  15. #include "base/win/windows_version.h"
  16. #include "brightray/browser/win/notification_presenter_win7.h"
  17. #include "brightray/browser/win/windows_toast_notification.h"
  18. #include "content/public/common/platform_notification_data.h"
  19. #include "third_party/skia/include/core/SkBitmap.h"
  20. #include "ui/gfx/codec/png_codec.h"
  21. #pragma comment(lib, "runtimeobject.lib")
  22. namespace brightray {
  23. namespace {
  24. bool IsDebuggingNotifications() {
  25. return base::Environment::Create()->HasVar("ELECTRON_DEBUG_NOTIFICATIONS");
  26. }
  27. bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
  28. std::vector<unsigned char> png_data;
  29. if (!gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false, &png_data))
  30. return false;
  31. char* data = reinterpret_cast<char*>(&png_data[0]);
  32. int size = static_cast<int>(png_data.size());
  33. return base::WriteFile(path, data, size) == size;
  34. }
  35. } // namespace
  36. // static
  37. NotificationPresenter* NotificationPresenter::Create() {
  38. auto version = base::win::GetVersion();
  39. if (version < base::win::VERSION_WIN8)
  40. return new NotificationPresenterWin7;
  41. if (!WindowsToastNotification::Initialize())
  42. return nullptr;
  43. std::unique_ptr<NotificationPresenterWin> presenter(
  44. new NotificationPresenterWin);
  45. if (!presenter->Init())
  46. return nullptr;
  47. if (IsDebuggingNotifications())
  48. LOG(INFO) << "Successfully created Windows notifications presenter";
  49. return presenter.release();
  50. }
  51. NotificationPresenterWin::NotificationPresenterWin() {}
  52. NotificationPresenterWin::~NotificationPresenterWin() {}
  53. bool NotificationPresenterWin::Init() {
  54. base::ThreadRestrictions::ScopedAllowIO allow_io;
  55. return temp_dir_.CreateUniqueTempDir();
  56. }
  57. base::string16 NotificationPresenterWin::SaveIconToFilesystem(
  58. const SkBitmap& icon,
  59. const GURL& origin) {
  60. std::string filename;
  61. if (origin.is_valid()) {
  62. filename = base::MD5String(origin.spec()) + ".png";
  63. } else {
  64. base::TimeTicks now = base::TimeTicks::Now();
  65. filename = std::to_string(now.ToInternalValue()) + ".png";
  66. }
  67. base::ThreadRestrictions::ScopedAllowIO allow_io;
  68. base::FilePath path = temp_dir_.GetPath().Append(base::UTF8ToUTF16(filename));
  69. if (base::PathExists(path))
  70. return path.value();
  71. if (SaveIconToPath(icon, path))
  72. return path.value();
  73. return base::UTF8ToUTF16(origin.spec());
  74. }
  75. Notification* NotificationPresenterWin::CreateNotificationObject(
  76. NotificationDelegate* delegate) {
  77. return new WindowsToastNotification(delegate, this);
  78. }
  79. } // namespace brightray