Repository: electron/electron
File: shell/browser/notifications/win/notification_presenter_win.cc (C++)

Code before:
#include "shell/browser/notifications/win/windows_toast_notification.h"
#include "shell/common/thread_restrictions.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"

#pragma comment(lib, "runtimeobject.lib")

namespace electron {

namespace {

bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
  ScopedAllowBlockingForElectron allow_blocking;
  std::optional<std::vector<uint8_t>> png_data =
      gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
  if (!png_data.has_value())
    return false;

  if (!base::WriteFile(path, png_data.value()))
    return false;

  if (!base::PathExists(path))
    return false;

  std::optional<int64_t> size = base::GetFileSize(path);
  return size.has_value();
}

}  // namespace

// static
std::unique_ptr<NotificationPresenter> NotificationPresenter::Create() {
  if (!WindowsToastNotification::Initialize())
    return {};
  auto presenter = std::make_unique<NotificationPresenterWin>();
  if (!presenter->Init())
    return {};

  if (electron::debug_notifications)
    LOG(INFO) << "Successfully created Windows notifications presenter";

  return presenter;
}

NotificationPresenterWin::NotificationPresenterWin() = default;

NotificationPresenterWin::~NotificationPresenterWin() = default;

bool NotificationPresenterWin::Init() {
  ScopedAllowBlockingForElectron allow_blocking;
  return temp_dir_.CreateUniqueTempDir();

Review comment (suggestion):
```suggestion
bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
  std::optional<std::vector<uint8_t>> png_data =
      gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
  if (!png_data.has_value() || !png_data.value().size())
    return false;
  return base::WriteFile(path, png_data.value());
```

1) Checking the input size is enough if we want to avoid zero bytes write operation, the output size is checked in https://source.chromium.org/chromium/chromium/src/+/main:base/files/file_util_win.cc;l=1060-1065;bpv=0;bpt=1 so we can remove the `base::GetFileSize` in favor of it.
2) `base::WriteFile` creates the file if it does not exist so `base::PathExists` is redundant
3) `base::WriteFile` already has scope for blocking I/O

Code after:
#include "shell/browser/notifications/win/windows_toast_notification.h"
#include "shell/common/thread_restrictions.h"
#include "third_party/skia/include/core/SkBitmap.h"
#include "ui/gfx/codec/png_codec.h"

#pragma comment(lib, "runtimeobject.lib")

namespace electron {

namespace {

bool SaveIconToPath(const SkBitmap& bitmap, const base::FilePath& path) {
  std::optional<std::vector<uint8_t>> png_data =
      gfx::PNGCodec::EncodeBGRASkBitmap(bitmap, false);
  if (!png_data.has_value() || !png_data.value().size())
    return false;
  return base::WriteFile(path, png_data.value());
}

}  // namespace

// static
std::unique_ptr<NotificationPresenter> NotificationPresenter::Create() {
  if (!WindowsToastNotification::Initialize())
    return {};
  auto presenter = std::make_unique<NotificationPresenterWin>();
  if (!presenter->Init())
    return {};

  if (electron::debug_notifications)
    LOG(INFO) << "Successfully created Windows notifications presenter";

  return presenter;
}

NotificationPresenterWin::NotificationPresenterWin() = default;

NotificationPresenterWin::~NotificationPresenterWin() = default;

bool NotificationPresenterWin::Init() {
  ScopedAllowBlockingForElectron allow_blocking;
  return temp_dir_.CreateUniqueTempDir();
}

std::wstring NotificationPresenterWin::SaveIconToFilesystem(
    const SkBitmap& icon,
    const GURL& origin) {
  if (icon.drawsNothing())
    return L"";

  std::string filename;